public function handle_api_requests() { WPI_Log::get_instance()->log('wpi api request : ' . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); global $wp; if (isset($_GET['wpi-api'])) { $wp->query_vars['wpi-api'] = $_GET['wpi-api']; } if (isset($wp->query_vars['wpi-api'])) { $this->server = new WPI_Server(); $n = strpos($wp->query_vars['wpi-api'], '/'); if (!empty($n)) { $qs = explode('/', $wp->query_vars['wpi-api']); $api = $qs[0]; $method = $qs[1]; if (file_exists(WPI_DIR . '/api/class-wpi-' . $api . ".php")) { include_once WPI_DIR . '/api/class-wpi-' . $api . ".php"; $args = array_splice($qs, 2, 2); call_user_func_array(array($api, $method), $args); } else { $this->server->response_failure('api not found !'); } } else { $api = $wp->query_vars['wpi-api']; if (file_exists(WPI_DIR . '/api/class-wpi-' . $api . '.php')) { include_once WPI_DIR . '/api/class-wpi-' . $api . '.php'; } else { $this->server->response_failure('api not found !'); } } WPI_Log::get_instance()->close(); die; } }
private function __construct() { define('WPI_DIR', plugin_dir_path(__FILE__)); define('WPI_URL', plugin_dir_url(__FILE__)); register_activation_hook(__FILE__, array($this, 'activate')); register_deactivation_hook(__FILE__, array($this, 'deactivate')); add_action('generate_rewrite_rules', array($this, 'add_rewrite_rules')); add_filter('query_vars', array($this, 'query_vars')); include_once 'wpi-api.php'; include_once 'api/class-wpi-server.php'; include_once 'wpi-functions.php'; include_once 'api/class-wpi-log.php'; $this->wpi_api = new WPI_API(); $this->wpi_log = WPI_Log::get_instance(); $this->wpi_log->set_log_switch(true); }
public static function image() { WPI_Log::get_instance()->log('wpi api upload image : ' . json_encode($_FILES)); $server_now = new WPI_Server(); $dir = wp_upload_dir(); $img_types = array('image/gif', 'image/pjpeg', 'image/jpeg', 'image/png'); if (!in_array($_FILES['img']['type'], $img_types)) { $server_now->response_failure('type not allowed '); } else { $save_to = $dir['path'] . '/' . $_FILES['img']['name']; if (file_exists($save_to)) { $server_now->response_failure('file has exist !'); } else { $result = move_uploaded_file($_FILES['img']['tmp_name'], $save_to); $server_now->response_success_with_data('image uploaded !', '"' . $dir['url'] . '/' . $_FILES['img']['name'] . '"'); } } }
/** * * 日志记录 * * @param int $type 0 -> 记录(THING LOG) / 1 -> 错误(ERROR LOG) * @param string $desc * @param string $time * * @since alpha 0.0.1 * @date 2014.02.04 * @author genialx * */ public function log($desc = 'empty', $type = 0) { if ($this->log_switch) { $time = date('Y-n-j H:m:s'); if (self::$handle == NULL) { $filename = $this->log_file_pre . $this->get_max_log_file_suf(); self::$handle = fopen($this->log_file_path . $filename, 'a'); } switch ($type) { case 0: fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13)); break; case 1: fwrite(self::$handle, 'ERROR LOG:' . ' ' . $desc . ' ' . $time . chr(13)); break; default: fwrite(self::$handle, 'THING LOG:' . ' ' . $desc . ' ' . $time . chr(13)); break; } } }
public static function check($uname, $pwd) { WPI_Log::get_instance()->log('wpi api auth check : ' . json_encode($_POST)); $server_now = new WPI_Server(); if (isset($_POST['uname'])) { $uname = $_POST['uname']; } if (isset($_POST['pwd'])) { $pwd = $_POST['pwd']; } if (!empty($uname) && !empty($pwd)) { // require_once ABSPATH . WPINC . '/class-phpass.php'; // $wp_hasher = new PasswordHash( 8, true ); // $hashed = $wp_hasher->HashPassword($pwd); // $credentials = array('user_login' => $uname, 'user_password' => $pwd); $user = wp_signon($credentials); if (!is_wp_error($user)) { include_once ABSPATH . 'wp-blog-header.php'; global $wpdb; include_once ABSPATH . 'wp-config.php'; $table_prefix = preg_replace('/[0-9](.+?)*/', '', $wpdb->prefix); $sql_id = sprintf("SELECT ID FROM %susers WHERE user_login='******'", $table_prefix, $uname); $result_id = $wpdb->get_results("SELECT ID FROM " . $table_prefix . "users WHERE user_login='******'"); $sql_key = "SELECT meta_value FROM " . $table_prefix . "usermeta WHERE user_id=" . $result_id[0]->ID . " AND meta_key='woocommerce_api_consumer_key'"; $sql_screct = "SELECT meta_value FROM " . $table_prefix . "usermeta WHERE user_id=" . $result_id[0]->ID . " AND meta_key='woocommerce_api_consumer_secret'"; $result_key = $wpdb->get_results($sql_key); $result_screct = $wpdb->get_results($sql_screct); $server_now->response(sprintf('{"status":"success","msg":{"key":"%s","secret":"%s"},"code":1}', $result_key[0]->meta_value, $result_screct[0]->meta_value)); } else { $server_now->response_failure('login failure ! please check your uname and pwd ! uname:' . $uname . ' pwd:' . $pwd); } } else { $server_now->response_failure('login failure ! please check your uname and pwd ! uname:' . $uname . ' pwd:' . $pwd); } }
public function response($data) { $this->status(200); echo $data; WPI_Log::get_instance()->log('wpi server response : ' . $data); }