home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / includes / plugin.php < prev    next >
Encoding:
PHP Script  |  2008-08-02  |  15.8 KB  |  555 lines

  1. <?php
  2.  
  3. function get_plugin_data( $plugin_file ) {
  4.     $plugin_data = implode( '', file( $plugin_file ));
  5.     preg_match( '|Plugin Name:(.*)$|mi', $plugin_data, $plugin_name );
  6.     preg_match( '|Plugin URI:(.*)$|mi', $plugin_data, $plugin_uri );
  7.     preg_match( '|Description:(.*)$|mi', $plugin_data, $description );
  8.     preg_match( '|Author:(.*)$|mi', $plugin_data, $author_name );
  9.     preg_match( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
  10.  
  11.     if ( preg_match( "|Version:(.*)|i", $plugin_data, $version ))
  12.         $version = trim( $version[1] );
  13.     else
  14.         $version = '';
  15.  
  16.     $description = wptexturize( trim( $description[1] ));
  17.  
  18.     $name = $plugin_name[1];
  19.     $name = trim( $name );
  20.     $plugin = $name;
  21.     if ('' != trim($plugin_uri[1]) && '' != $name ) {
  22.         $plugin = '<a href="' . trim( $plugin_uri[1] ) . '" title="'.__( 'Visit plugin homepage' ).'">'.$plugin.'</a>';
  23.     }
  24.  
  25.     if ('' == $author_uri[1] ) {
  26.         $author = trim( $author_name[1] );
  27.     } else {
  28.         $author = '<a href="' . trim( $author_uri[1] ) . '" title="'.__( 'Visit author homepage' ).'">' . trim( $author_name[1] ) . '</a>';
  29.     }
  30.  
  31.     return array('Name' => $name, 'Title' => $plugin, 'Description' => $description, 'Author' => $author, 'Version' => $version);
  32. }
  33.  
  34. function get_plugins($plugin_folder = '') {
  35.     
  36.     if ( ! $cache_plugins = wp_cache_get('plugins', 'plugins') )
  37.         $cache_plugins = array();
  38.     
  39.     if ( isset($cache_plugins[ $plugin_folder ]) )
  40.         return $cache_plugins[ $plugin_folder ];
  41.     
  42.     $wp_plugins = array ();
  43.     $plugin_root = WP_PLUGIN_DIR;
  44.     if( !empty($plugin_folder) )
  45.         $plugin_root .= $plugin_folder;
  46.  
  47.     // Files in wp-content/plugins directory
  48.     $plugins_dir = @ opendir( $plugin_root);
  49.     if ( $plugins_dir ) {
  50.         while (($file = readdir( $plugins_dir ) ) !== false ) {
  51.             if ( substr($file, 0, 1) == '.' )
  52.                 continue;
  53.             if ( is_dir( $plugin_root.'/'.$file ) ) {
  54.                 $plugins_subdir = @ opendir( $plugin_root.'/'.$file );
  55.                 if ( $plugins_subdir ) {
  56.                     while (($subfile = readdir( $plugins_subdir ) ) !== false ) {
  57.                         if ( substr($subfile, 0, 1) == '.' )
  58.                             continue;
  59.                         if ( substr($subfile, -4) == '.php' )
  60.                             $plugin_files[] = "$file/$subfile";
  61.                     }
  62.                 }
  63.             } else {
  64.                 if ( substr($file, -4) == '.php' )
  65.                     $plugin_files[] = $file;
  66.             }
  67.         }
  68.     }
  69.     @closedir( $plugins_dir );
  70.     @closedir( $plugins_subdir );
  71.  
  72.     if ( !$plugins_dir || !$plugin_files )
  73.         return $wp_plugins;
  74.  
  75.     foreach ( $plugin_files as $plugin_file ) {
  76.         if ( !is_readable( "$plugin_root/$plugin_file" ) )
  77.             continue;
  78.  
  79.         $plugin_data = get_plugin_data( "$plugin_root/$plugin_file" );
  80.  
  81.         if ( empty ( $plugin_data['Name'] ) )
  82.             continue;
  83.  
  84.         $wp_plugins[plugin_basename( $plugin_file )] = $plugin_data;
  85.     }
  86.  
  87.     uasort( $wp_plugins, create_function( '$a, $b', 'return strnatcasecmp( $a["Name"], $b["Name"] );' ));
  88.  
  89.     $cache_plugins[ $plugin_folder ] = $wp_plugins; 
  90.     wp_cache_set('plugins', $cache_plugins, 'plugins'); 
  91.  
  92.     return $wp_plugins;
  93. }
  94.  
  95. function is_plugin_active($plugin){
  96.     return in_array($plugin, get_option('active_plugins'));
  97. }
  98.  
  99. function activate_plugin($plugin, $redirect = '') {
  100.         $current = get_option('active_plugins');
  101.         $plugin = trim($plugin);
  102.  
  103.         $valid = validate_plugin($plugin);
  104.         if ( is_wp_error($valid) )
  105.             return $valid;
  106.  
  107.         if ( !in_array($plugin, $current) ) {
  108.             if ( !empty($redirect) )
  109.                 wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
  110.             ob_start();
  111.             @include(WP_PLUGIN_DIR . '/' . $plugin);
  112.             $current[] = $plugin;
  113.             sort($current);
  114.             update_option('active_plugins', $current);
  115.             do_action('activate_' . $plugin);
  116.             ob_end_clean();
  117.         }
  118.  
  119.         return null;
  120. }
  121.  
  122. function deactivate_plugins($plugins, $silent= false) {
  123.     $current = get_option('active_plugins');
  124.  
  125.     if ( !is_array($plugins) )
  126.         $plugins = array($plugins);
  127.  
  128.     foreach ( $plugins as $plugin ) {
  129.         if( ! is_plugin_active($plugin) )
  130.             continue;
  131.         array_splice($current, array_search( $plugin, $current), 1 ); // Fixed Array-fu!
  132.         if ( ! $silent ) //Used by Plugin updater to internally deactivate plugin, however, not to notify plugins of the fact to prevent plugin output.
  133.             do_action('deactivate_' . trim( $plugin ));
  134.     }
  135.  
  136.     update_option('active_plugins', $current);
  137. }
  138.  
  139. function activate_plugins($plugins, $redirect = '') {
  140.     if ( !is_array($plugins) )
  141.         $plugins = array($plugins);
  142.  
  143.     $errors = array();
  144.     foreach ( (array) $plugins as $plugin ) {
  145.         if ( !empty($redirect) )
  146.             $redirect = add_query_arg('plugin', $plugin, $redirect);
  147.         $result = activate_plugin($plugin, $redirect);
  148.         if ( is_wp_error($result) )
  149.             $errors[$plugin] = $result;
  150.     }
  151.  
  152.     if ( !empty($errors) )
  153.         return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
  154.  
  155.     return true;
  156. }
  157.  
  158. function delete_plugins($plugins, $redirect = '' ) {
  159.     global $wp_filesystem;
  160.  
  161.     if( empty($plugins) )
  162.         return false;
  163.  
  164.     $checked = array();
  165.     foreach( $plugins as $plugin )
  166.         $checked[] = 'checked[]=' . $plugin;
  167.  
  168.     ob_start();
  169.     $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-manage-plugins');
  170.     if ( false === ($credentials = request_filesystem_credentials($url)) ) {
  171.         $data = ob_get_contents();
  172.         ob_end_clean();
  173.         if( ! empty($data) ){
  174.             include_once( ABSPATH . 'wp-admin/admin-header.php');
  175.             echo $data;
  176.             include( ABSPATH . 'wp-admin/admin-footer.php');
  177.             exit;
  178.         }
  179.         return;
  180.     }
  181.  
  182.     if ( ! WP_Filesystem($credentials) ) {
  183.         request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
  184.         $data = ob_get_contents();
  185.         ob_end_clean();
  186.         if( ! empty($data) ){
  187.             include_once( ABSPATH . 'wp-admin/admin-header.php');
  188.             echo $data;
  189.             include( ABSPATH . 'wp-admin/admin-footer.php');
  190.             exit;
  191.         }
  192.         return;
  193.     }
  194.  
  195.     if ( $wp_filesystem->errors->get_error_code() ) {
  196.         return $wp_filesystem->errors;
  197.     }
  198.  
  199.     if ( ! is_object($wp_filesystem) )
  200.         return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
  201.  
  202.     if ( $wp_filesystem->errors->get_error_code() )
  203.         return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
  204.  
  205.     //Get the base plugin folder
  206.     $plugins_dir = $wp_filesystem->wp_plugins_dir();
  207.     if ( empty($plugins_dir) )
  208.         return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));
  209.     
  210.     $plugins_dir = trailingslashit( $plugins_dir );
  211.  
  212.     $errors = array();
  213.  
  214.     foreach( $plugins as $plugin_file ) {
  215.         $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
  216.         // If plugin is in its own directory, recursively delete the directory.
  217.         if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory seperator AND that its not the root plugin folder
  218.             $deleted = $wp_filesystem->delete($this_plugin_dir, true);
  219.         else
  220.             $deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);
  221.     
  222.         if ( ! $deleted )
  223.             $errors[] = $plugin_file;
  224.     }
  225.     
  226.     if( ! empty($errors) )
  227.         return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s'), implode(', ', $errors)) );
  228.     
  229.     return true;
  230. }
  231.  
  232. function validate_active_plugins() {
  233.     $check_plugins = get_option('active_plugins');
  234.  
  235.     // Sanity check.  If the active plugin list is not an array, make it an
  236.     // empty array.
  237.     if ( !is_array($check_plugins) ) {
  238.         update_option('active_plugins', array());
  239.         return;
  240.     }
  241.  
  242.     //Invalid is any plugin that is deactivated due to error.
  243.     $invalid = array(); 
  244.  
  245.     // If a plugin file does not exist, remove it from the list of active
  246.     // plugins.
  247.     foreach ( $check_plugins as $check_plugin ) {
  248.         $result = validate_plugin($check_plugin);
  249.         if ( is_wp_error( $result ) ) {
  250.             $invalid[$check_plugin] = $result;
  251.             deactivate_plugins( $check_plugin, true);
  252.         }
  253.     }
  254.     return $invalid;
  255. }
  256.  
  257. function validate_plugin($plugin) {
  258.     if ( validate_file($plugin) )
  259.         return new WP_Error('plugin_invalid', __('Invalid plugin.'));
  260.     if ( ! file_exists(WP_PLUGIN_DIR . '/' . $plugin) )
  261.         return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
  262.  
  263.     return 0;
  264. }
  265.  
  266. //
  267. // Menu
  268. //
  269.  
  270. function add_menu_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  271.     global $menu, $admin_page_hooks;
  272.  
  273.     $file = plugin_basename( $file );
  274.  
  275.     $menu[] = array ( $menu_title, $access_level, $file, $page_title );
  276.  
  277.     $admin_page_hooks[$file] = sanitize_title( $menu_title );
  278.  
  279.     $hookname = get_plugin_page_hookname( $file, '' );
  280.     if (!empty ( $function ) && !empty ( $hookname ))
  281.         add_action( $hookname, $function );
  282.  
  283.     return $hookname;
  284. }
  285.  
  286. function add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function = '' ) {
  287.     global $submenu;
  288.     global $menu;
  289.     global $_wp_real_parent_file;
  290.     global $_wp_submenu_nopriv;
  291.  
  292.     $file = plugin_basename( $file );
  293.  
  294.     $parent = plugin_basename( $parent);
  295.     if ( isset( $_wp_real_parent_file[$parent] ) )
  296.         $parent = $_wp_real_parent_file[$parent];
  297.  
  298.     if ( !current_user_can( $access_level ) ) {
  299.         $_wp_submenu_nopriv[$parent][$file] = true;
  300.         return false;
  301.     }
  302.  
  303.     // If the parent doesn't already have a submenu, add a link to the parent
  304.     // as the first item in the submenu.  If the submenu file is the same as the
  305.     // parent file someone is trying to link back to the parent manually.  In
  306.     // this case, don't automatically add a link back to avoid duplication.
  307.     if (!isset( $submenu[$parent] ) && $file != $parent  ) {
  308.         foreach ( $menu as $parent_menu ) {
  309.             if ( $parent_menu[2] == $parent && current_user_can( $parent_menu[1] ) )
  310.                 $submenu[$parent][] = $parent_menu;
  311.         }
  312.     }
  313.  
  314.     $submenu[$parent][] = array ( $menu_title, $access_level, $file, $page_title );
  315.  
  316.     $hookname = get_plugin_page_hookname( $file, $parent);
  317.     if (!empty ( $function ) && !empty ( $hookname ))
  318.         add_action( $hookname, $function );
  319.  
  320.     return $hookname;
  321. }
  322.  
  323. function add_management_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  324.     return add_submenu_page( 'edit.php', $page_title, $menu_title, $access_level, $file, $function );
  325. }
  326.  
  327. function add_options_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  328.     return add_submenu_page( 'options-general.php', $page_title, $menu_title, $access_level, $file, $function );
  329. }
  330.  
  331. function add_theme_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  332.     return add_submenu_page( 'themes.php', $page_title, $menu_title, $access_level, $file, $function );
  333. }
  334.  
  335. function add_users_page( $page_title, $menu_title, $access_level, $file, $function = '' ) {
  336.     if ( current_user_can('edit_users') )
  337.         $parent = 'users.php';
  338.     else
  339.         $parent = 'profile.php';
  340.     return add_submenu_page( $parent, $page_title, $menu_title, $access_level, $file, $function );
  341. }
  342.  
  343. //
  344. // Pluggable Menu Support -- Private
  345. //
  346.  
  347. function get_admin_page_parent() {
  348.     global $parent_file;
  349.     global $menu;
  350.     global $submenu;
  351.     global $pagenow;
  352.     global $plugin_page;
  353.     global $_wp_real_parent_file;
  354.     global $_wp_menu_nopriv;
  355.     global $_wp_submenu_nopriv;
  356.  
  357.     if ( !empty ( $parent_file ) ) {
  358.         if ( isset( $_wp_real_parent_file[$parent_file] ) )
  359.             $parent_file = $_wp_real_parent_file[$parent_file];
  360.  
  361.         return $parent_file;
  362.     }
  363.  
  364.     if ( $pagenow == 'admin.php' && isset( $plugin_page ) ) {
  365.         foreach ( $menu as $parent_menu ) {
  366.             if ( $parent_menu[2] == $plugin_page ) {
  367.                 $parent_file = $plugin_page;
  368.                 if ( isset( $_wp_real_parent_file[$parent_file] ) )
  369.                     $parent_file = $_wp_real_parent_file[$parent_file];
  370.                 return $parent_file;
  371.             }
  372.         }
  373.         if ( isset( $_wp_menu_nopriv[$plugin_page] ) ) {
  374.             $parent_file = $plugin_page;
  375.             if ( isset( $_wp_real_parent_file[$parent_file] ) )
  376.                     $parent_file = $_wp_real_parent_file[$parent_file];
  377.             return $parent_file;
  378.         }
  379.     }
  380.  
  381.     if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) ) {
  382.         $parent_file = $pagenow;
  383.         if ( isset( $_wp_real_parent_file[$parent_file] ) )
  384.             $parent_file = $_wp_real_parent_file[$parent_file];
  385.         return $parent_file;
  386.     }
  387.  
  388.     foreach (array_keys( $submenu ) as $parent) {
  389.         foreach ( $submenu[$parent] as $submenu_array ) {
  390.             if ( isset( $_wp_real_parent_file[$parent] ) )
  391.                 $parent = $_wp_real_parent_file[$parent];
  392.             if ( $submenu_array[2] == $pagenow ) {
  393.                 $parent_file = $parent;
  394.                 return $parent;
  395.             } else
  396.                 if ( isset( $plugin_page ) && ($plugin_page == $submenu_array[2] ) ) {
  397.                     $parent_file = $parent;
  398.                     return $parent;
  399.                 }
  400.         }
  401.     }
  402.  
  403.     $parent_file = '';
  404.     return '';
  405. }
  406.  
  407. function get_admin_page_title() {
  408.     global $title;
  409.     global $menu;
  410.     global $submenu;
  411.     global $pagenow;
  412.     global $plugin_page;
  413.  
  414.     if ( isset( $title ) && !empty ( $title ) ) {
  415.         return $title;
  416.     }
  417.  
  418.     $hook = get_plugin_page_hook( $plugin_page, $pagenow );
  419.  
  420.     $parent = $parent1 = get_admin_page_parent();
  421.     if ( empty ( $parent) ) {
  422.         foreach ( $menu as $menu_array ) {
  423.             if ( isset( $menu_array[3] ) ) {
  424.                 if ( $menu_array[2] == $pagenow ) {
  425.                     $title = $menu_array[3];
  426.                     return $menu_array[3];
  427.                 } else
  428.                     if ( isset( $plugin_page ) && ($plugin_page == $menu_array[2] ) && ($hook == $menu_array[3] ) ) {
  429.                         $title = $menu_array[3];
  430.                         return $menu_array[3];
  431.                     }
  432.             } else {
  433.                 $title = $menu_array[0];
  434.                 return $title;
  435.             }
  436.         }
  437.     } else {
  438.         foreach (array_keys( $submenu ) as $parent) {
  439.             foreach ( $submenu[$parent] as $submenu_array ) {
  440.                 if ( isset( $plugin_page ) &&
  441.                     ($plugin_page == $submenu_array[2] ) &&
  442.                     (($parent == $pagenow ) || ($parent == $plugin_page ) || ($plugin_page == $hook ) || (($pagenow == 'admin.php' ) && ($parent1 != $submenu_array[2] ) ) )
  443.                     ) {
  444.                         $title = $submenu_array[3];
  445.                         return $submenu_array[3];
  446.                     }
  447.  
  448.                 if ( $submenu_array[2] != $pagenow || isset( $_GET['page'] ) ) // not the current page
  449.                     continue;
  450.  
  451.                 if ( isset( $submenu_array[3] ) ) {
  452.                     $title = $submenu_array[3];
  453.                     return $submenu_array[3];
  454.                 } else {
  455.                     $title = $submenu_array[0];
  456.                     return $title;
  457.                 }
  458.             }
  459.         }
  460.     }
  461.  
  462.     return $title;
  463. }
  464.  
  465. function get_plugin_page_hook( $plugin_page, $parent_page ) {
  466.     $hook = get_plugin_page_hookname( $plugin_page, $parent_page );
  467.     if ( has_action($hook) )
  468.         return $hook;
  469.     else
  470.         return null;
  471. }
  472.  
  473. function get_plugin_page_hookname( $plugin_page, $parent_page ) {
  474.     global $admin_page_hooks;
  475.  
  476.     $parent = get_admin_page_parent();
  477.  
  478.     $page_type = 'admin';
  479.     if ( empty ( $parent_page ) || 'admin.php' == $parent_page ) {
  480.         if ( isset( $admin_page_hooks[$plugin_page] ))
  481.             $page_type = 'toplevel';
  482.         else
  483.             if ( isset( $admin_page_hooks[$parent] ))
  484.                 $page_type = $admin_page_hooks[$parent];
  485.     } else if ( isset( $admin_page_hooks[$parent_page] ) ) {
  486.         $page_type = $admin_page_hooks[$parent_page];
  487.     }
  488.  
  489.     $plugin_name = preg_replace( '!\.php!', '', $plugin_page );
  490.  
  491.     return $page_type.'_page_'.$plugin_name;
  492. }
  493.  
  494. function user_can_access_admin_page() {
  495.     global $pagenow;
  496.     global $menu;
  497.     global $submenu;
  498.     global $_wp_menu_nopriv;
  499.     global $_wp_submenu_nopriv;
  500.     global $plugin_page;
  501.  
  502.     $parent = get_admin_page_parent();
  503.  
  504.     if ( isset( $_wp_submenu_nopriv[$parent][$pagenow] ) )
  505.         return false;
  506.  
  507.     if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$parent][$plugin_page] ) )
  508.         return false;
  509.  
  510.     if ( empty( $parent) ) {
  511.         if ( isset( $_wp_menu_nopriv[$pagenow] ) )
  512.             return false;
  513.         if ( isset( $_wp_submenu_nopriv[$pagenow][$pagenow] ) )
  514.             return false;
  515.         if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$pagenow][$plugin_page] ) )
  516.             return false;
  517.         foreach (array_keys( $_wp_submenu_nopriv ) as $key ) {
  518.             if ( isset( $_wp_submenu_nopriv[$key][$pagenow] ) )
  519.                 return false;
  520.             if ( isset( $plugin_page ) && isset( $_wp_submenu_nopriv[$key][$plugin_page] ) )
  521.             return false;
  522.         }
  523.         return true;
  524.     }
  525.  
  526.     if ( isset( $submenu[$parent] ) ) {
  527.         foreach ( $submenu[$parent] as $submenu_array ) {
  528.             if ( isset( $plugin_page ) && ( $submenu_array[2] == $plugin_page ) ) {
  529.                 if ( current_user_can( $submenu_array[1] ))
  530.                     return true;
  531.                 else
  532.                     return false;
  533.             } else if ( $submenu_array[2] == $pagenow ) {
  534.                 if ( current_user_can( $submenu_array[1] ))
  535.                     return true;
  536.                 else
  537.                     return false;
  538.             }
  539.         }
  540.     }
  541.  
  542.     foreach ( $menu as $menu_array ) {
  543.         if ( $menu_array[2] == $parent) {
  544.             if ( current_user_can( $menu_array[1] ))
  545.                 return true;
  546.             else
  547.                 return false;
  548.         }
  549.     }
  550.  
  551.     return true;
  552. }
  553.  
  554. ?>
  555.