Changeset 1695207
- Timestamp:
- 07/12/2017 01:13:09 PM (9 years ago)
- File:
-
- 1 edited
-
raychat/trunk/raychat.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
raychat/trunk/raychat.php
r1685745 r1695207 1 1 <?php 2 2 3 /** 3 4 * Plugin Name: raychat … … 11 12 * Domain Path: / 12 13 */ 13 14 if ( ! defined( 'ABSPATH' ) ) { 15 die( "go away!" ); 16 } 17 18 load_plugin_textdomain( 'raychat', PLUGINDIR . '/' . dirname( plugin_basename( __FILE__ ) ) ); 19 $lang = get_bloginfo( "language" ); 14 if (!defined('ABSPATH')) { 15 die("go away!"); 16 } 17 18 load_plugin_textdomain('raychat', PLUGINDIR . '/' . dirname(plugin_basename(__FILE__))); 19 $lang = get_bloginfo("language"); 20 20 $raychat_addr = 'https://www.raychat.io'; 21 21 22 22 23 define( "RAYCHAT_LANG", substr( $lang, 0, 2 ));24 25 define( "RAYCHAT_URL", "https://raychat.io/admin");26 define( "RAYCHAT_WIDGET_URL", "raychat.io");27 define( "RAYCHAT_URL", $raychat_addr);28 define( "RAYCHAT_INTEGRATION_URL", RAYCHAT_URL . "/integration");29 define( "RAYCHAT_PLUGIN_URL", plugin_dir_url( __FILE__ ));30 define( "RAYCHAT_IMG_URL", plugin_dir_url( __FILE__ ) . "/img/");23 define("RAYCHAT_LANG", substr($lang, 0, 2)); 24 25 define("RAYCHAT_URL", "https://raychat.io/admin"); 26 define("RAYCHAT_WIDGET_URL", "raychat.io"); 27 define("RAYCHAT_URL", $raychat_addr); 28 define("RAYCHAT_INTEGRATION_URL", RAYCHAT_URL . "/integration"); 29 define("RAYCHAT_PLUGIN_URL", plugin_dir_url(__FILE__)); 30 define("RAYCHAT_IMG_URL", plugin_dir_url(__FILE__) . "/img/"); 31 31 // //register hooks for plugin 32 register_activation_hook( __FILE__, 'raychatInstall' ); 33 register_deactivation_hook( __FILE__, 'raychatDelete' ); 34 32 register_activation_hook(__FILE__, 'raychatInstall'); 33 register_deactivation_hook(__FILE__, 'raychatDelete'); 35 34 36 35 //add plugin to options menu 37 36 function catalog_admin_menu_raychat() { 38 load_plugin_textdomain( 'raychat', PLUGINDIR . '/' . dirname( plugin_basename( __FILE__ ) ) ); 39 add_menu_page( __( 'رایچت', 'raychat' ), __( 'رایچت', 'raychat' ), 8, basename( __FILE__ ), 'raychatPreferences', RAYCHAT_IMG_URL . "raychat.png" ); 40 } 41 42 add_action( 'admin_menu', 'catalog_admin_menu_raychat' ); 43 44 function raychat_options_validate( $args ) { 45 return $args; 46 } 47 37 load_plugin_textdomain('raychat', PLUGINDIR . '/' . dirname(plugin_basename(__FILE__))); 38 add_menu_page(__('رایچت', 'raychat'), __('رایچت', 'raychat'), 8, basename(__FILE__), 'raychatPreferences', RAYCHAT_IMG_URL . "raychat.png"); 39 } 40 41 add_action('admin_menu', 'catalog_admin_menu_raychat'); 42 43 function raychat_options_validate($args) { 44 return $args; 45 } 48 46 49 47 /* 50 48 * Register the settings 51 49 */ 52 add_action( 'admin_init', 'raychat_register_settings' ); 50 add_action('admin_init', 'raychat_register_settings'); 51 53 52 function raychat_register_settings() { 54 register_setting( 'raychat_token', 'raychat_token', 'raychat_options_validate' ); 55 register_setting( 'raychat_widget_id', 'raychat_widget_id', 'raychat_options_validate' ); 56 } 57 58 59 add_action( 'admin_post_wp_save_token', 'wp_save_token_function_raychat' ); 60 add_action( 'admin_post_nopriv_wp_save_token', 'wp_save_token_function_raychat' ); 61 62 add_action( 'wp_footer', 'raychatAppend', 100000 ); 53 register_setting('raychat_token', 'raychat_token', 'raychat_options_validate'); 54 register_setting('raychat_widget_id', 'raychat_widget_id', 'raychat_options_validate'); 55 } 56 57 add_action('admin_post_wp_save_token', 'wp_save_token_function_raychat'); 58 add_action('admin_post_nopriv_wp_save_token', 'wp_save_token_function_raychat'); 59 60 add_action('wp_footer', 'raychatAppend', 100000); 63 61 64 62 function raychatInstall() { 65 return raychat::getInstance()->install();63 return raychat::getInstance()->install(); 66 64 } 67 65 68 66 function raychatDelete() { 69 return raychat::getInstance()->delete();67 return raychat::getInstance()->delete(); 70 68 } 71 69 72 70 function raychatAppend() { 73 echo raychat::getInstance()->append( raychat::getInstance()->getId());71 echo raychat::getInstance()->append(raychat::getInstance()->getId()); 74 72 } 75 73 76 74 function raychatPreferences() { 77 if ( isset( $_POST["widget_id"] )) {78 raychat::getInstance()->save();79 }80 81 load_plugin_textdomain( 'raychat', PLUGINDIR . '/' . dirname( plugin_basename( __FILE__ ) ));82 83 wp_register_style( 'raychat_style', plugins_url( 'raychat.css', __FILE__ ));84 wp_enqueue_style( 'raychat_style');85 echo raychat::getInstance()->render();75 if (isset($_POST["widget_id"])) { 76 raychat::getInstance()->save(); 77 } 78 79 load_plugin_textdomain('raychat', PLUGINDIR . '/' . dirname(plugin_basename(__FILE__))); 80 81 wp_register_style('raychat_style', plugins_url('raychat.css', __FILE__)); 82 wp_enqueue_style('raychat_style'); 83 echo raychat::getInstance()->render(); 86 84 } 87 85 88 86 function wp_save_token_function_raychat() { 89 $tokenError = null; 90 if ( trim( esc_html( $_POST['submit'] ) ) !== '' ) { 91 $upload_dir = wp_get_upload_dir(); 92 $path = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'raychat'; 93 $token = esc_html( $_POST['token-id'] ); 94 if ( $token !== '' ) { 95 if ( preg_match( "/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/", $token ) ) { 96 if ( file_exists( $path . 'id' ) ) { 97 file_put_contents( $path . DIRECTORY_SEPARATOR . 'id', $token ); 98 } else { 99 if ( ! file_exists( $path ) ) { 100 if(!wp_mkdir_p($path)){ 101 $tokenError = 'مجوز ساخت پوشه ندارید'; 102 } 103 } 104 $myfile = fopen( $path . DIRECTORY_SEPARATOR . 'id', "w" ); 105 fwrite( $myfile, $token ); 106 fclose( $myfile ); 107 } 108 $raychat = raychat::getInstance(); 109 $raychat->install(); 110 } else { 111 $tokenError = "توکن نامعتبر است."; 112 } 113 } else { 114 $tokenError = "توکن نمی تواند خالی باشد."; 115 } 116 set_transient( 'error_token_uuid', $tokenError ); 117 } 118 wp_redirect( $_SERVER['HTTP_REFERER'] ); 87 $tokenError = null; 88 if (trim(esc_html($_POST['submit'])) !== '') { 89 $token = esc_html($_POST['token-id']); 90 if ($token !== '') { 91 if (preg_match("/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/", $token)) { 92 if (get_option('raychat_widget_id') !== false) { 93 update_option('raychat_widget_id', $token); 94 } else { 95 add_option('raychat_widget_id', $token, null, 'no'); 96 } 97 $raychat = raychat::getInstance(); 98 $raychat->install(); 99 } else { 100 $tokenError = "توکن نامعتبر است."; 101 } 102 } else { 103 $tokenError = "توکن نمی تواند خالی باشد."; 104 } 105 set_transient('error_token_uuid', $tokenError); 106 } 107 wp_redirect($_SERVER['HTTP_REFERER']); 119 108 // wp_redirect(admin_url('admin.php?page=raychat.php')); 120 exit(); 121 } 122 109 exit(); 110 } 123 111 124 112 class raychat { 125 113 126 protected static $instance, $db, $table, $lang; 127 128 private function __construct() { 129 $this->token = get_option( 'raychat_token' ); 130 $this->widget_id = get_option( 'raychat_widget_id' ); 131 } 132 133 private function __clone() { } 134 135 private function __wakeup() { } 136 137 private $widget_id = ''; 138 private $token = ''; 139 140 public static function getInstance() { 141 142 if ( is_null( self::$instance ) ) { 143 self::$instance = new raychat(); 144 } 145 self::$lang = "en"; 146 if ( isset( $_GET["lang"] ) ) { 147 switch ( $_GET["lang"] ) { 148 case 'ru': 149 self::$lang = "ru"; 150 break; 151 default: 152 self::$lang = "en"; 153 break; 154 } 155 } 156 157 return self::$instance; 158 } 159 160 public function setID( $id ) { 161 $this->widget_id = $id; 162 } 163 164 public function setToken( $token ) { 165 $this->token = $token; 166 } 167 168 /** 169 * Install 170 */ 171 public function install() { 172 $uploadPath = wp_get_upload_dir(); 173 $path = $uploadPath['basedir'] . DIRECTORY_SEPARATOR . 'raychat'; 174 $file = dirname(__FILE__). '/id'; 175 if(file_exists($file) && !file_exists($path . '/id')){ 176 $ok = true; 177 if ( ! file_exists( $path ) ) { 178 if(!wp_mkdir_p($path)){ 179 set_transient( 'error_token_uuid', 'مجوز ساخت پوشه ندارید' ); 180 $ok = false; 181 } 182 } 183 if ($ok){ 184 $myfile = fopen( $path . DIRECTORY_SEPARATOR . 'id', "w" ); 185 $uuid = file_get_contents($file); 186 fwrite( $myfile, $uuid); 187 fclose( $myfile ); 188 unlink($file); 189 $this->widget_id = $uuid; 190 $this->save(); 191 } 192 }else{ 193 if ( ! $this->widget_id ) { 194 $default_widget_id = ''; 195 if ( file_exists( $path . DIRECTORY_SEPARATOR . 'id' ) ) { 196 $default_widget_id = file_get_contents( $path . '/id' ); 197 } 198 } 199 $this->widget_id = $default_widget_id; 200 $this->save(); 201 } 202 } 203 204 public function catchPost() { 205 if ( isset( $_GET['mode'] ) && $_GET['mode'] == 'reset' ) { 206 $this->widget_id = ''; 207 $this->token = ''; 208 $this->save(); 209 } 210 if ( isset( $_POST['widget_id'] ) ) { 211 $this->widget_id = $_POST['widget_id']; 212 $this->save(); 213 } elseif ( isset( $_POST['email'] ) && isset( $_POST['userPassword'] ) ) { 214 // получаем данные для запроса 215 $query = $_POST; 216 $query['siteUrl'] = get_site_url(); 217 $query['partnerId'] = "wordpress"; 218 $authToken = md5( time() . get_site_url() ); 219 $query['authToken'] = $authToken; 220 if ( ! $query['agent_id'] ) { 221 $query['agent_id'] = 0; 222 } 223 $query['lang'] = RAYCHAT_LANG; 224 $content = http_build_query( $query ); 225 226 if ( ini_get( 'allow_url_fopen' ) ) { 227 $useCurl = false; 228 } elseif ( ! extension_loaded( 'curl' ) ) { 229 if ( ! dl( 'curl.so' ) ) { 230 $useCurl = false; 231 } else { 232 $useCurl = true; 233 } 234 } else { 235 $useCurl = true; 236 } 237 // отправляем запрос 238 try{ 239 $path = RAYCHAT_INTEGRATION_URL . "/install"; 240 if ( ! extension_loaded( 'openssl' ) ) { 241 $path = str_replace( 'https:', 'http:', $path ); 242 } 243 if ( $useCurl ) { 244 if ( $curl = curl_init() ) { 245 $responce = wp_remote_get($path, array( 246 'CURLOPT_RETURNTRANSFER' => true, 247 'CURLOPT_POST' => true, 248 'CURLOPT_POSTFIELDS' => $content 249 )); 250 // curl_setopt( $curl, CURLOPT_URL, $path ); 251 // curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); 252 // curl_setopt( $curl, CURLOPT_POST, true ); 253 // curl_setopt( $curl, CURLOPT_POSTFIELDS, $content ); 254 // $responce = curl_exec( $curl ); 255 // curl_close( $curl ); 256 } 257 } else { 258 $responce = file_get_contents( $path, false, stream_context_create( array( 259 'http' => array( 260 'method' => 'POST', 261 'header' => 'Content-Type: application/x-www-form-urlencoded', 262 'content' => $content 263 ) 264 ) ) ); 265 } 266 if ( $responce ) { 267 if ( strstr( $responce, 'Error' ) ) { 268 return array( "error" => $responce ); 269 } else { 270 $this->widget_id = $responce; 271 $this->token = $authToken; 272 $this->save(); 273 274 return true; 275 } 276 } 277 }catch( Exception $e ){ 278 _e( "Connection error", 'raychat' ); 279 } 280 } 281 282 } 283 284 /** 285 * delete plugin 286 */ 287 public function delete() { 288 289 } 290 291 292 public function getId() { 293 return $this->widget_id; 294 } 295 296 /** 297 * render admin page 298 */ 299 public function render() { 300 $result = $this->catchPost(); 301 $error = ''; 302 $widget_id = $this->widget_id; 303 if ( is_array( $result ) && isset( $result['error'] ) ) { 304 $error = $result['error']; 305 } 306 307 if ( ini_get( 'allow_url_fopen' ) ) { 308 $requirementsOk = true; 309 } elseif ( ! extension_loaded( 'curl' ) ) { 310 if ( ! dl( 'curl.so' ) ) { 311 $requirementsOk = false; 312 } else { 313 $requirementsOk = true; 314 } 315 } else { 316 $requirementsOk = true; 317 } 318 319 if ( $requirementsOk ) { 320 require_once "templates/page.php"; 321 } else { 322 require_once "templates/error.php"; 323 } 324 } 325 326 public function append( $widget_id = false ) { 327 if ( $widget_id ) { 328 require_once "templates/script.php"; 329 } 330 } 331 332 public function save() { 114 protected static $instance, $db, $table, $lang; 115 116 private function __construct() { 117 $this->token = get_option('raychat_token'); 118 $this->widget_id = get_option('raychat_widget_id'); 119 } 120 121 private function __clone() { 122 123 } 124 125 private function __wakeup() { 126 127 } 128 129 private $widget_id = ''; 130 private $token = ''; 131 132 public static function getInstance() { 133 134 if (is_null(self::$instance)) { 135 self::$instance = new raychat(); 136 } 137 self::$lang = "en"; 138 if (isset($_GET["lang"])) { 139 switch ($_GET["lang"]) { 140 case 'ru': 141 self::$lang = "ru"; 142 break; 143 default: 144 self::$lang = "en"; 145 break; 146 } 147 } 148 149 return self::$instance; 150 } 151 152 public function setID($id) { 153 $this->widget_id = $id; 154 } 155 156 public function setToken($token) { 157 $this->token = $token; 158 } 159 160 /** 161 * Install 162 */ 163 public function install() { 164 $file = dirname(__FILE__) . '/id'; 165 if (file_exists($file)) { 166 $uuid = file_get_contents($file); 167 if(get_option('raychat_widget_id') !== false){ 168 update_option('raychat_widget_id', $uuid); 169 }else{ 170 add_option('raychat_widget_id', $uuid, null, 'no'); 171 } 172 unlink($file); 173 $this->widget_id = $uuid; 174 $this->save(); 175 } else { 176 if (!$this->widget_id) { 177 if(($out = get_option('raychat_widget_id')) !== false){ 178 $this->widget_id = $out; 179 } 180 } 181 $this->save(); 182 } 183 } 184 185 public function catchPost() { 186 if (isset($_GET['mode']) && $_GET['mode'] == 'reset') { 187 $this->widget_id = ''; 188 $this->token = ''; 189 $this->save(); 190 } 191 if (isset($_POST['widget_id'])) { 192 $this->widget_id = $_POST['widget_id']; 193 $this->save(); 194 } elseif (isset($_POST['email']) && isset($_POST['userPassword'])) { 195 // получаем данные для запроса 196 $query = $_POST; 197 $query['siteUrl'] = get_site_url(); 198 $query['partnerId'] = "wordpress"; 199 $authToken = md5(time() . get_site_url()); 200 $query['authToken'] = $authToken; 201 if (!$query['agent_id']) { 202 $query['agent_id'] = 0; 203 } 204 $query['lang'] = RAYCHAT_LANG; 205 $content = http_build_query($query); 206 207 if (ini_get('allow_url_fopen')) { 208 $useCurl = false; 209 } elseif (!extension_loaded('curl')) { 210 if (!dl('curl.so')) { 211 $useCurl = false; 212 } else { 213 $useCurl = true; 214 } 215 } else { 216 $useCurl = true; 217 } 218 // отправляем запрос 219 try { 220 $path = RAYCHAT_INTEGRATION_URL . "/install"; 221 if (!extension_loaded('openssl')) { 222 $path = str_replace('https:', 'http:', $path); 223 } 224 if ($useCurl) { 225 if ($curl = curl_init()) { 226 $responce = wp_remote_get($path, array( 227 'CURLOPT_RETURNTRANSFER' => true, 228 'CURLOPT_POST' => true, 229 'CURLOPT_POSTFIELDS' => $content 230 )); 231 // curl_setopt( $curl, CURLOPT_URL, $path ); 232 // curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); 233 // curl_setopt( $curl, CURLOPT_POST, true ); 234 // curl_setopt( $curl, CURLOPT_POSTFIELDS, $content ); 235 // $responce = curl_exec( $curl ); 236 // curl_close( $curl ); 237 } 238 } else { 239 $responce = file_get_contents($path, false, stream_context_create(array( 240 'http' => array( 241 'method' => 'POST', 242 'header' => 'Content-Type: application/x-www-form-urlencoded', 243 'content' => $content 244 ) 245 ))); 246 } 247 if ($responce) { 248 if (strstr($responce, 'Error')) { 249 return array("error" => $responce); 250 } else { 251 $this->widget_id = $responce; 252 $this->token = $authToken; 253 $this->save(); 254 255 return true; 256 } 257 } 258 } catch (Exception $e) { 259 _e("Connection error", 'raychat'); 260 } 261 } 262 } 263 264 /** 265 * delete plugin 266 */ 267 public function delete() { 268 269 } 270 271 public function getId() { 272 return $this->widget_id; 273 } 274 275 /** 276 * render admin page 277 */ 278 public function render() { 279 $result = $this->catchPost(); 280 $error = ''; 281 $widget_id = $this->widget_id; 282 if (is_array($result) && isset($result['error'])) { 283 $error = $result['error']; 284 } 285 286 if (ini_get('allow_url_fopen')) { 287 $requirementsOk = true; 288 } elseif (!extension_loaded('curl')) { 289 if (!dl('curl.so')) { 290 $requirementsOk = false; 291 } else { 292 $requirementsOk = true; 293 } 294 } else { 295 $requirementsOk = true; 296 } 297 298 if ($requirementsOk) { 299 require_once "templates/page.php"; 300 } else { 301 require_once "templates/error.php"; 302 } 303 } 304 305 public function append($widget_id = false) { 306 if ($widget_id) { 307 require_once "templates/script.php"; 308 } 309 } 310 311 public function save() { 333 312 // do_settings_sections( __FILE__ ); 334 313 335 update_option( 'raychat_widget_id', $this->widget_id ); 336 update_option( 'raychat_token', $this->token ); 337 } 338 339 340 } 314 update_option('raychat_widget_id', $this->widget_id); 315 update_option('raychat_token', $this->token); 316 } 317 318 }
Note: See TracChangeset
for help on using the changeset viewer.