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