Advertisement
Guest User

display-cl.php

a guest
Jan 17th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.48 KB | None | 0 0
  1. <?php
  2.  
  3. // MAIN CLASS
  4.  
  5. class Wpr_Core extends Wpr_Helper
  6. {
  7.     var $name;
  8.     var $slug;
  9.     var $settings;
  10.     var $remote_client;
  11.     var $plugin_instance;
  12.     var $theme_instance;
  13.     var $wp_instance;
  14.  
  15.     function __construct(){
  16.  
  17.         add_filter('xmlrpc_methods', array($this, 'add_xmlrpc_methods'));
  18.  
  19.     }
  20.  
  21.     function add_xmlrpc_methods($methods) {
  22.  
  23.         $methods['wprUpgradeWorker'] = 'wpr_worker_upgrade';
  24.         // stats
  25.         $methods['wprGetStats'] = 'wpr_stats_get';
  26.         $methods['wprGetServerStatus'] = 'wpr_stats_server_get';
  27.         $methods['wprGetUserHitStats'] = 'wpr_stats_hit_count_get';
  28.  
  29.         // plugins
  30.         $methods['wprGetPluginList'] = 'wpr_plugin_get_list';
  31.         $methods['wprActivatePlugin'] = 'wpr_plugin_activate';
  32.         $methods['wprDeactivatePlugin'] = 'wpr_plugin_deactivate';
  33.         $methods['wprUpgradePlugin'] = 'wpr_plugin_upgrade';
  34.         $methods['wprUpgradePlugins'] = 'wpr_plugin_upgrade_multiple';
  35.         $methods['wprUpgradeAllPlugins'] = 'wpr_plugin_upgrade_all';
  36.         $methods['wprDeletePlugin'] = 'wpr_plugin_delete';
  37.         $methods['wprInstallPlugin'] = 'wpr_plugin_install';
  38.         $methods['wprUploadPluginByURL'] = 'wpr_plugin_upload_by_url';
  39.  
  40.          //themes
  41.         $methods['wprGetThemeList'] = 'wpr_theme_get_list';
  42.         $methods['wprActivateTheme'] = 'wpr_theme_activate';
  43.         $methods['wprDeleteTheme'] = 'wpr_theme_delete';
  44.         $methods['wprInstallTheme'] = 'wpr_theme_install';
  45.         $methods['wprUploadThemeByURL'] = 'wpr_theme_upload_by_url';
  46.  
  47.         // wordpress update
  48.         $methods['wprWPCheckVersion'] = 'wpr_wp_checkversion';
  49.         $methods['wprWPUpgrade'] = 'wpr_wp_upgrade';
  50.         $methods['wprWPGetUpdates'] = 'wpr_wp_get_updates';
  51.  
  52.         return $methods;
  53.     }
  54.  
  55.     function get_plugin_instance() {
  56.         if (!isset($this->plugin_instance)) {
  57.             $this->plugin_instance = new Wpr_Plugin();
  58.         }
  59.  
  60.         return $this->plugin_instance;
  61.     }
  62.  
  63.     function get_theme_instance() {
  64.         if (!isset($this->theme_instance)) {
  65.             $this->theme_instance = new Wpr_Theme();
  66.         }
  67.  
  68.         return $this->theme_instance;
  69.     }
  70.  
  71.     function get_wp_instance() {
  72.         if (!isset($this->wp_instance)) {
  73.             $this->wp_instance = new Wpr_WP();
  74.         }
  75.  
  76.         return $this->wp_instance;
  77.     }
  78.  
  79.     function _save_options() {
  80.         if (get_option($this->slug)) {
  81.             update_option($this->slug, $this->settings);
  82.         } else {
  83.             add_option($this->slug, $this->settings);
  84.         }
  85.     }
  86.  
  87.     function _construct_url($params = array(), $base_page = 'index.php') {
  88.         $url = "$base_page?_wpnonce=" . wp_create_nonce($this->slug);
  89.         foreach ($params as $key => $value) {
  90.             $url .= "&$key=$value";
  91.         }
  92.  
  93.         return $url;
  94.     }
  95.  
  96.     function login($username, $password) {
  97.         if (!get_option( 'enable_xmlrpc')) {
  98.             update_option('enable_xmlrpc', 1);
  99.         }
  100.  
  101.         $user = wp_authenticate($username, $password);
  102.  
  103.         if (is_wp_error($user)) {
  104.             $this->error = new IXR_Error(403, __('Bad login/pass combination.'));
  105.             return false;
  106.         }
  107.  
  108.         set_current_user( $user->ID );
  109.         return $user;
  110.     }
  111. }
  112.  
  113. // HELPER CLASS
  114.  
  115. class Wpr_Helper
  116. {
  117.  
  118.     function _filter_content($str) {
  119.         return nl2br($this->_strip_tags($str));
  120.     }
  121.  
  122.     function _escape(&$array) {
  123.         global $wpdb;
  124.  
  125.         if(!is_array($array)) {
  126.             return($wpdb->escape($array));
  127.         }
  128.         else {
  129.             foreach ( (array) $array as $k => $v ) {
  130.                 if (is_array($v)) {
  131.                     $this->_escape($array[$k]);
  132.                 } else if (is_object($v)) {
  133.                     //skip
  134.                 } else {
  135.                     $array[$k] = $wpdb->escape($v);
  136.                 }
  137.             }
  138.         }
  139.     }
  140.  
  141.     function _init_filesystem() {
  142.         global $wp_filesystem;
  143.  
  144.         if (!$wp_filesystem || !is_object($wp_filesystem)) {
  145.             WP_Filesystem();
  146.         }
  147.  
  148.         if (!is_object($wp_filesystem))
  149.             return FALSE;
  150.  
  151.         return TRUE;
  152.     }
  153.  
  154.     function wpr_get_transient($option_name) {
  155.  
  156.         if(trim($option_name) == ''){
  157.             return FALSE;
  158.         }
  159.  
  160.          global $wp_version;
  161.  
  162.         if (version_compare($wp_version, '2.8', '<'))
  163.          return get_option($option_name);
  164.  
  165.       else if (version_compare($wp_version, '3.0', '<'))
  166.            return get_transient($option_name);
  167.  
  168.       else
  169.            return get_site_transient($option_name);
  170.  
  171.     }
  172.  
  173.     function wpr_null_op_buffer($buffer) {
  174.         if(!ob_get_level())
  175.             ob_start(array($this, 'wpr_null_op_buffer'));
  176.         return '';
  177.     }
  178.  
  179.     function _deleteTempDir($directory) {
  180.         if(substr($directory,-1) == "/") {
  181.             $directory = substr($directory,0,-1);
  182.         }
  183.  
  184.         if(!file_exists($directory) || !is_dir($directory)) {
  185.             return false;
  186.         } elseif(!is_readable($directory)) {
  187.             return false;
  188.         } else {
  189.             $directoryHandle = opendir($directory);
  190.  
  191.             while ($contents = readdir($directoryHandle)) {
  192.                 if($contents != '.' && $contents != '..') {
  193.                     $path = $directory . "/" . $contents;
  194.  
  195.                     if(is_dir($path)) {
  196.                         $this->_deleteTempDir($path);
  197.                     } else {
  198.                         unlink($path);
  199.                     }
  200.                 }
  201.             }
  202.             closedir($directoryHandle);
  203.             rmdir($directory);
  204.             return true;
  205.         }
  206.     }
  207. }
  208.  
  209. // PLUGINS CLASS
  210.  
  211. class Wpr_Plugin extends Wpr_Core
  212. {
  213.     function __construct() {
  214.         parent::__construct();
  215.     }
  216.  
  217.     function get_list($args) {
  218.         $this->_escape($args);
  219.         $username = $args[0];
  220.         $password = $args[1];
  221.  
  222.         if($password != get_option('wpr_cron')) {
  223.             if (!$user = $this->login($username, $password)) {
  224.                 return $this->error;
  225.             }
  226.             if(!current_user_can('activate_plugins')) {
  227.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  228.             }
  229.         }
  230.  
  231.         $this->refresh_transient();
  232.  
  233.         $all_plugins = get_plugins();
  234.  
  235.          $wpr_plug = basename(WPR_URLPATH).'/wprobot.php';
  236.          unset($all_plugins[$wpr_plug]);
  237.  
  238.         $current = $this->wpr_get_transient('update_plugins');
  239.  
  240.         foreach ((array)$all_plugins as $plugin_file => $plugin_data) {
  241.             //Translate, Apply Markup, Sanitize HTML
  242.             $plugin_data = _get_plugin_data_markup_translate($plugin_file, $plugin_data, false, true);
  243.             $all_plugins[$plugin_file] = $plugin_data;
  244.  
  245.             //Filter into individual sections
  246.             if (is_plugin_active($plugin_file))
  247.             {
  248.                 $all_plugins[$plugin_file]['status'] = 'active';
  249.                 $active_plugins[$plugin_file] = $plugin_data;
  250.             }
  251.             else
  252.             {
  253.                 $all_plugins[$plugin_file]['status'] = 'inactive';
  254.                 $inactive_plugins[$plugin_file] = $plugin_data;
  255.             }
  256.  
  257.             if (isset($current->response[$plugin_file]))
  258.             {
  259.                 $all_plugins[$plugin_file]['new_version'] = $current->response[$plugin_file];
  260.             }
  261.         }
  262.  
  263.         return $all_plugins;
  264.     }
  265.  
  266.     function deactivate($args) {
  267.         $this->_escape($args);
  268.         $username = $args[0];
  269.         $password = $args[1];
  270.         $plugin_files = $args[2];
  271.  
  272.         if($password != get_option('wpr_cron')) {
  273.             if (!$user = $this->login($username, $password)) {
  274.                 return $this->error;
  275.             }
  276.             if(!current_user_can('activate_plugins')) {
  277.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  278.             }
  279.         }
  280.  
  281.         $this->refresh_transient();
  282.  
  283.         $success = deactivate_plugins($plugin_files);
  284.         if(is_wp_error($success))
  285.             return false;
  286.         chdir(WP_PLUGIN_DIR);
  287.  
  288.         if(is_array($plugin_files)) return true;
  289.         // get the plugin again
  290.         return $this->_get_plugin_data($plugin_files);
  291.     }
  292.  
  293.     function activate($args) {
  294.         $this->_escape($args);
  295.         $username = $args[0];
  296.         $password = $args[1];
  297.         $plugin_files = $args[2];
  298.  
  299.         if($password != get_option('wpr_cron')) {
  300.             if (!$user = $this->login($username, $password)) {
  301.                 return $this->error;
  302.             }
  303.             if(!current_user_can('activate_plugins')) {
  304.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  305.             }
  306.         }
  307.  
  308.         $this->refresh_transient();
  309.  
  310.         $success = activate_plugins($plugin_files, '', FALSE);
  311.         if(is_wp_error($success))
  312.             return false;
  313.         chdir(WP_PLUGIN_DIR);
  314.  
  315.         if(is_array($plugin_files)) return true;
  316.         // get the plugin again
  317.         return $this->_get_plugin_data($plugin_files);
  318.     }
  319.  
  320.     function upgrade($args, $login_required = TRUE, $reget_plugin_data = TRUE) {
  321.         $this->_escape($args);
  322.         $username = $args[0];
  323.         $password = $args[1];
  324.         $plugin_file = $args[2];
  325.  
  326.         if($login_required && $password != get_option('wpr_cron')) {
  327.             if (!$user = $this->login($username, $password)) {
  328.                 return $this->error;
  329.             }
  330.             if(!current_user_can('activate_plugins')) {
  331.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  332.             }
  333.         }
  334.  
  335.  
  336.         $current = $this->wpr_get_transient('update_plugins');
  337.  
  338.         $needs_reactivaton = is_plugin_active($plugin_file);
  339.  
  340.         ob_start();
  341.  
  342.         $upgrader = new Wpr_Plugin_Upgrader();
  343.         $result = $upgrader->upgrade($plugin_file);
  344.  
  345.         if (is_wp_error($result)) {
  346.             return new IXR_Error(401, 'Sorry, this plugin could not be upgraded. ' . $result->get_error_message());
  347.         }
  348.  
  349.         if($needs_reactivaton) {
  350.             activate_plugin($plugin_file);
  351.         }
  352.  
  353.         unset($current->response[$plugin_file]);
  354.         set_transient('update_plugins', $current);
  355.  
  356.         $output = ob_get_clean();
  357.  
  358.         if ($reget_plugin_data) {
  359.             chdir(WP_PLUGIN_DIR);
  360.  
  361.             return $this->_get_plugin_data($plugin_file);
  362.         }
  363.     }
  364.  
  365.     function upgrade_multiple($args) {
  366.         $this->_escape($args);
  367.         $username = $args[0];
  368.         $password = $args[1];
  369.         $plugin_files = $args[2];
  370.  
  371.         if($password != get_option('wpr_cron')) {
  372.             if (!$user = $this->login($username, $password)) {
  373.                 return $this->error;
  374.             }
  375.             if(!current_user_can('activate_plugins')) {
  376.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  377.             }
  378.         }
  379.  
  380.         foreach ($plugin_files as $plugin_file) {
  381.             $this->upgrade(array(FALSE, FALSE, $plugin_file), FALSE, FALSE);
  382.         }
  383.  
  384.         return $this->get_upgradable_plugins();
  385.     }
  386.  
  387.     function upgrade_all($args) {
  388.         $this->_escape($args);
  389.         $username = $args[0];
  390.         $password = $args[1];
  391.  
  392.         if($password != get_option('wpr_cron')) {
  393.             if (!$user = $this->login($username, $password)) {
  394.                 return $this->error;
  395.             }
  396.         }
  397.  
  398.         $current = $this->wpr_get_transient('update_plugins');
  399.         foreach ((array)$current->response as $file => $data){
  400.             $this->upgrade(array($username, $password, $file), FALSE, FALSE);
  401.  
  402.             unset($current->response[$file]);
  403.             set_transient('update_plugins', $current);
  404.         }
  405.     }
  406.  
  407.     function delete($args) {
  408.         $this->_escape($args);
  409.         $username = $args[0];
  410.         $password = $args[1];
  411.         $plugin_files = $args[2];
  412.  
  413.         if($password != get_option('wpr_cron')) {
  414.             if (!$user = $this->login($username, $password)) {
  415.                 return $this->error;
  416.             }
  417.             if(!current_user_can('delete_plugins')) {
  418.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  419.             }
  420.         }
  421.  
  422.         $this->refresh_transient();
  423.  
  424.         ob_start();
  425.  
  426.         if(!is_array($plugin_files))
  427.             $plugin_files = array($plugin_files);
  428.  
  429.         $result = delete_plugins($plugin_files);
  430.         ob_end_clean();
  431.         if (is_wp_error($result)) {
  432.             return new IXR_Error(401, 'Sorry, this plugin could not be deleted. ' . $result->get_error_message());
  433.         }
  434.  
  435.         return TRUE;
  436.     }
  437.  
  438.     function _get_plugin_data($plugin_file) {
  439.         $plugin = get_plugin_data($plugin_file);
  440.         $plugin['status'] = is_plugin_active($plugin_file) ? 'active' : 'inactive';
  441.  
  442.         $current = $this->wpr_get_transient('update_plugins');
  443.  
  444.         if (isset($current->response[$plugin_file])) {
  445.             $plugin['new_version'] = $current->response[$plugin_file];
  446.         }
  447.  
  448.         return $plugin;
  449.     }
  450.  
  451.     function get_upgradable_plugins() {
  452.         $all_plugins = get_plugins();
  453.         $upgrade_plugins = array();
  454.  
  455.         $this->refresh_transient();
  456.  
  457.         $current = $this->wpr_get_transient('update_plugins');
  458.         foreach ((array)$all_plugins as $plugin_file => $plugin_data) {
  459.             $plugin_data = _get_plugin_data_markup_translate($plugin_file, $plugin_data, false, true);
  460.             if (isset($current->response[$plugin_file]))
  461.             {
  462.                 $current->response[$plugin_file]->name = $plugin_data['Name'];
  463.                 $current->response[$plugin_file]->old_version = $plugin_data['Version'];
  464.                 $current->response[$plugin_file]->file = $plugin_file;
  465.                 $upgrade_plugins[] = $current->response[$plugin_file];
  466.             }
  467.         }
  468.  
  469.         return $upgrade_plugins;
  470.     }
  471.  
  472.     function install($args) {
  473.         $this->_escape($args);
  474.         $username = $args[0];
  475.         $password = $args[1];
  476.         $slug = $args[2];
  477.         $activate = (bool)$args[3];
  478.  
  479.         if($password != get_option('wpr_cron')) {
  480.             if (!$user = $this->login($username, $password)) {
  481.                 return $this->error;
  482.             }
  483.             if(!current_user_can('install_plugins')) {
  484.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  485.             }
  486.         }
  487.  
  488.         $this->refresh_transient();
  489.  
  490.         ob_start();
  491.         include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  492.  
  493.         $api = plugins_api('plugin_information', array('slug' => $slug, 'fields' => array('sections' => false)));
  494.  
  495.         if (is_wp_error($api))
  496.              return new IXR_Error(401, 'Failed to install plugin. ' . $api->get_error_message());
  497.  
  498.         $upgrader = new Wpr_Plugin_Upgrader();
  499.         $upgrader->install($api->download_link);
  500.  
  501.         $output = ob_get_clean();
  502.  
  503.         if ($activate) {
  504.             $this->activate(array($username, $password, $upgrader->plugin_info()));
  505.         }
  506.  
  507.         return TRUE;
  508.     }
  509.  
  510.     function refresh_transient() {
  511.         delete_transient('update_plugins');
  512.         $current = $this->wpr_get_transient('update_plugins');
  513.         wp_update_plugins();
  514.  
  515.         return $current;
  516.     }
  517.  
  518.     function upload_by_url($args) {
  519.         $this->_escape($args);
  520.         $username = $args[0];
  521.         $password = $args[1];
  522.         $url = $args[2];
  523.         $activate = $args[3];
  524.  
  525.         if($password != get_option('wpr_cron')) {
  526.             if (!$user = $this->login($username, $password)) {
  527.                 return $this->error;
  528.             }
  529.             if(!current_user_can('install_plugins')) {
  530.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  531.             }
  532.         }
  533.  
  534.         if (!$this->_init_filesystem())
  535.             return new IXR_Error(401, 'Plugin could not be installed: Failed to initialize file system.');
  536.  
  537.         if($activate == 'true'){
  538.             wp_cache_delete('plugins', 'plugins');
  539.             $old_plugin_list = get_plugins();
  540.         }
  541.  
  542.         ob_start();
  543.         $tmp_file = download_url($url);
  544.         if(is_wp_error($tmp_file))
  545.             return new IXR_Error(401, 'Plugin could not be installed. ' . $tmp_file->get_error_message());
  546.  
  547.         $result = unzip_file($tmp_file, WP_PLUGIN_DIR);
  548.         unlink($tmp_file);
  549.  
  550.         if($activate == 'true'){
  551.             wp_cache_delete('plugins', 'plugins');
  552.             $new_plugin_list = get_plugins();
  553.  
  554.             $new_plugin = array_keys(array_diff_key($new_plugin_list, $old_plugin_list));
  555.             $this->activate(array($username, $password, $new_plugin[0]));
  556.         }
  557.  
  558.  
  559.        if(is_wp_error($result)) {
  560.             return new IXR_Error(401, 'Plugin could not be extracted. ' . $result->get_error_message());
  561.         }
  562.  
  563.         unset($args[2]);
  564.  
  565.         return $this->get_list($args);
  566.     }
  567. }
  568.  
  569. // THEMES CLASS
  570.  
  571. class Wpr_Theme extends Wpr_Core
  572. {
  573.     function __construct() {
  574.         parent::__construct();
  575.     }
  576.  
  577.     function get_list($args) {
  578.         $this->_escape($args);
  579.         $username = $args[0];
  580.         $password = $args[1];
  581.  
  582.         if($password != get_option('wpr_cron')) {
  583.             if (!$user = $this->login($username, $password)) {
  584.                 return $this->error;
  585.             }
  586.             if(!current_user_can('switch_themes')) {
  587.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  588.             }
  589.         }
  590.  
  591.         $themes = get_themes();
  592.  
  593.         $current_theme = current_theme_info();
  594.  
  595.         unset($themes[$current_theme->name]);
  596.  
  597.         return array(
  598.             'current'   => $current_theme,
  599.             'inactive'  => $themes,
  600.         );
  601.     }
  602.  
  603.     function activate($args) {
  604.         $this->_escape($args);
  605.         $username = $args[0];
  606.         $password = $args[1];
  607.         $template = $args[2];
  608.         $stylesheet = $args[3];
  609.  
  610.         if($password != get_option('wpr_cron')) {
  611.             if (!$user = $this->login($username, $password)) {
  612.                 return $this->error;
  613.             }
  614.             if(!current_user_can('switch_themes')) {
  615.                 return new IXR_Error(401, 'Sorry, you cannot manage plugins on the remote blog.');
  616.             }
  617.         }
  618.  
  619.         switch_theme($template, $stylesheet);
  620.  
  621.         return $this->get_list($args);
  622.     }
  623.  
  624.     function delete($args) {
  625.         $this->_escape($args);
  626.         $username = $args[0];
  627.         $password = $args[1];
  628.         $template = $args[2];
  629.  
  630.         if($password != get_option('wpr_cron')) {
  631.             if (!$user = $this->login($username, $password)) {
  632.                 return $this->error;
  633.             }
  634.             if(!current_user_can('update_themes')) {
  635.                 return new IXR_Error(401, 'Sorry, you are not allowed to delete themes from the remote blog.');
  636.             }
  637.         }
  638.  
  639.         ob_start();
  640.         $result = delete_theme($template);
  641.         ob_end_clean();
  642.         if (is_wp_error($result)) {
  643.             return new IXR_Error(401, 'Theme could not be deleted. ' . $result->get_error_message());
  644.         }
  645.  
  646.         return TRUE;
  647.     }
  648.  
  649.     function install($args) {
  650.         $this->_escape($args);
  651.         $username = $args[0];
  652.         $password = $args[1];
  653.         $theme = $args[2];
  654.         $activate = (bool)$args[3];
  655.  
  656.         if($password != get_option('wpr_cron')) {
  657.             if (!$user = $this->login($username, $password)) {
  658.                 return $this->error;
  659.             }
  660.             if(!current_user_can('install_themes')) {
  661.                 return new IXR_Error(401, 'Sorry, you are not allowed to install themes on the remote blog.');
  662.             }
  663.         }
  664.  
  665.         ob_start();
  666.  
  667.         include_once(ABSPATH . 'wp-admin/includes/theme-install.php');
  668.  
  669.         $api = themes_api('theme_information', array('slug' => $theme, 'fields' => array('sections' => false)));
  670.  
  671.         if (is_wp_error($api)) {
  672.             return new IXR_Error(401, 'Could not install theme. ' . $api->get_error_message());
  673.         }
  674.  
  675.         $upgrader = new Wpr_Theme_Upgrader();
  676.         $result = $upgrader->install($api->download_link);
  677.  
  678.         if (is_wp_error($result)) {
  679.             return new IXR_Error(401, 'Theme could not be installed. ' . $result->get_error_message());
  680.         }
  681.  
  682.         if ($activate && $theme_info = $upgrader->theme_info()) {
  683.             $stylesheet = $upgrader->result['destination_name'];
  684.             $template = !empty($theme_info['Template']) ? $theme_info['Template'] : $stylesheet;
  685.  
  686.             $this->activate(array($username, $password, $template, $stylesheet));
  687.         }
  688.  
  689.         ob_end_clean();
  690.  
  691.         return $this->get_list($args);
  692.     }
  693.  
  694.     function upload_by_url($args) {
  695.         $this->_escape($args);
  696.         $username = $args[0];
  697.         $password = $args[1];
  698.         $url = $args[2];
  699.  
  700.         if($password != get_option('wpr_cron')) {
  701.             if (!$user = $this->login($username, $password)) {
  702.                 return $this->error;
  703.             }
  704.             if(!current_user_can('install_themes')) {
  705.                 return new IXR_Error(401, 'Sorry, you are not allowed to install themes on the remote blog.');
  706.             }
  707.         }
  708.  
  709.         if (!$this->_init_filesystem())
  710.             return new IXR_Error(401, 'Theme could not be installed: Failed to initialize file system.');
  711.  
  712.  
  713.         ob_start();
  714.         $tmp_file = download_url($url);
  715.  
  716.         if(is_wp_error($tmp_file))
  717.             return new IXR_Error(401, 'Theme could not be installed. ' . $response->get_error_message());
  718.  
  719.         $result = unzip_file($tmp_file, WP_CONTENT_DIR . '/themes');
  720.         unlink($tmp_file);
  721.  
  722.         if(is_wp_error($result)) {
  723.             return new IXR_Error(401, 'Theme could not be extracted. ' . $result->get_error_message());
  724.         }
  725.  
  726.         unset($args[2]);
  727.  
  728.         return $this->get_list($args);
  729.     }
  730. }
  731.  
  732. // WORDPRESS CLASS
  733.  
  734. class Wpr_WP extends Wpr_Core
  735. {
  736.     function __construct() {
  737.         parent::__construct();
  738.     }
  739.  
  740.     function check_version($args, $login_required = TRUE) {
  741.         $this->_escape($args);
  742.  
  743.         $username = $args[0];
  744.         if($login_required)
  745.             $password = $args[1];
  746.  
  747.         $get_default_data = (bool) $args[2];
  748.  
  749.         if($login_required && $password != get_option('wpr_cron')) {
  750.             if (!$user = $this->login($username, $password)) {
  751.                 return $this->error;
  752.             }
  753.             if(!current_user_can('update_plugins')) {
  754.                 return new IXR_Error(401, 'You do not have sufficient permissions to upgrade WordPress on the remote blog.');
  755.             }
  756.         }
  757.  
  758.         require_once(ABSPATH . 'wp-includes/version.php');
  759.  
  760.         $updates = get_core_updates();
  761.         $update = $updates[0];
  762.         global $wp_version;
  763.         if (!isset($update->response) || 'latest' == $update->response) {
  764.             if (!$get_default_data)
  765.                 return new IXR_Error(999, 'The remote blog has the latest version of WordPress. You do not need to upgrade.');
  766.  
  767.             return array(
  768.                 'current_version'   => $wp_version,
  769.                 'latest_version'    => FALSE,
  770.             );
  771.         } else {
  772.             return array(
  773.                 'current_version'   => $wp_version,
  774.                 'latest_version'    => $update,
  775.             );
  776.         }
  777.         }
  778.  
  779.         function upgrade($args) {
  780.         $username = $args[0];
  781.         $password = $args[1];
  782.  
  783.         if($password != get_option('wpr_cron')) {
  784.             if (!$user = $this->login($username, $password)) {
  785.                 return $this->error;
  786.             }
  787.             if(!current_user_can('administrator')) {
  788.                 return new IXR_Error(401, "You don't have permissions to upgrade this blog.");
  789.             }
  790.         }
  791.  
  792.         $upgrade_info = $this->check_version($args);
  793.  
  794.         if (is_a($upgrade_info, 'IXR_Error')) {
  795.             return $upgrade_info;
  796.         }
  797.  
  798.         ob_start();
  799.         global $wp_filesystem;
  800.  
  801.         $url = 'update-core.php?action=do-core-upgrade';
  802.         $url = wp_nonce_url($url, 'upgrade-core');
  803.         if (FALSE === ($credentials = request_filesystem_credentials($url, '', false, ABSPATH))) {
  804.             return new IXR_Error(401, 'Failed to request file system credentials.');
  805.         }
  806.     $upgrader = new Wpr_Core_Upgrader();
  807.     $result =  $upgrader->upgrade($upgrade_info['latest_version']);
  808.  
  809.         ob_end_clean();
  810.  
  811.         if (is_wp_error($result)) {
  812.             return new IXR_Error(401, $result->get_error_message());
  813.         }
  814.  
  815.         return array(
  816.             'current_version'  => $upgrade_info['latest_version']->current,
  817.         );
  818.     }
  819.  
  820.     function get_updates($args) {
  821.         $this->_escape($args);
  822.  
  823.         $username = $args[0];
  824.         $password = $args[1];
  825.  
  826.         if($password != get_option('wpr_cron')) {
  827.             if (!$user = $this->login($username, $password)) {
  828.                 return $this->error;
  829.             }
  830.         }
  831.  
  832.         $args[] = 1;
  833.  
  834.         return array(
  835.             'core'      => $this->check_version($args, FALSE),
  836.             'plugins'   => $this->get_plugin_instance()->get_upgradable_plugins(),
  837.         );
  838.     }
  839. }
  840.  
  841. // CONTROLS
  842.  
  843. $wpr_core = new Wpr_Core();
  844.  
  845. function wpr_plugin_get_list($args) {
  846.     global $wpr_core;
  847.     return $wpr_core->get_plugin_instance()->get_list($args);
  848. }
  849.  
  850. function wpr_plugin_activate($args) {
  851.     global $wpr_core;
  852.     return $wpr_core->get_plugin_instance()->activate($args);
  853. }
  854.  
  855. function wpr_plugin_deactivate($args) {
  856.     global $wpr_core;
  857.     return $wpr_core->get_plugin_instance()->deactivate($args);
  858. }
  859.  
  860. function wpr_plugin_upgrade($args) {
  861.     global $wpr_core;
  862.     return $wpr_core->get_plugin_instance()->upgrade($args);
  863. }
  864.  
  865. function wpr_plugin_upgrade_multiple($args) {
  866.     global $wpr_core;
  867.     return $wpr_core->get_plugin_instance()->upgrade_multiple($args);
  868. }
  869.  
  870. function wpr_plugin_upgrade_all($args) {
  871.     global $wpr_core;
  872.     return $wpr_core->get_plugin_instance()->upgrade_all($args);
  873. }
  874.  
  875. function wpr_plugin_delete($args) {
  876.     global $wpr_core;
  877.     return $wpr_core->get_plugin_instance()->delete($args);
  878. }
  879.  
  880. function wpr_plugin_install($args) {
  881.     global $wpr_core;
  882.     return $wpr_core->get_plugin_instance()->install($args);
  883. }
  884.  
  885. function wpr_plugin_upload_by_url($args) {
  886.     global $wpr_core;
  887.     return $wpr_core->get_plugin_instance()->upload_by_url($args);
  888. }
  889.  
  890. function wpr_theme_get_list($args) {
  891.     global $wpr_core;
  892.     return $wpr_core->get_theme_instance()->get_list($args);
  893. }
  894.  
  895. function wpr_theme_activate($args) {
  896.     global $wpr_core;
  897.     return $wpr_core->get_theme_instance()->activate($args);
  898. }
  899.  
  900. function wpr_theme_delete($args) {
  901.     global $wpr_core;
  902.     return $wpr_core->get_theme_instance()->delete($args);
  903. }
  904.  
  905. function wpr_theme_install($args) {
  906.     global $wpr_core;
  907.     return $wpr_core->get_theme_instance()->install($args);
  908. }
  909.  
  910. function wpr_theme_upload_by_url($args) {
  911.     global $wpr_core;
  912.     return $wpr_core->get_theme_instance()->upload_by_url($args);
  913. }
  914.  
  915. function wpr_wp_checkversion($args) {
  916.     global $wpr_core;
  917.     return $wpr_core->get_wp_instance()->check_version($args);
  918. }
  919.  
  920. function wpr_wp_upgrade($args) {
  921.     global $wpr_core;
  922.     return $wpr_core->get_wp_instance()->upgrade($args);
  923. }
  924.  
  925. function wpr_wp_get_updates($args) {
  926.     global $wpr_core;
  927.     return $wpr_core->get_wp_instance()->get_updates($args);
  928. }
  929.  
  930. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement