config
1 month ago
Amazon.php
4 years ago
Helper.php
1 month ago
InstalledPlugin.php
3 years ago
InstalledTheme.php
4 years ago
Login.php
1 month ago
Product.php
4 years ago
ProductActions.php
4 years ago
ProductState.php
1 year ago
WpAjaxUpgraderSkin.php
4 years ago
Product.php
444 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tenweb_Authorization { |
| 4 | |
| 5 | |
| 6 | class Product |
| 7 | { |
| 8 | |
| 9 | public $id = 0; |
| 10 | public $slug = ""; |
| 11 | public $title = ""; |
| 12 | public $description = ""; |
| 13 | public $author = ""; |
| 14 | public $author_url = ""; |
| 15 | public $with_addons = false; |
| 16 | public $zip_name = ""; |
| 17 | public $parent_id = 0; |
| 18 | public $logo = ""; |
| 19 | public $info_link = ""; |
| 20 | public $demo_link = ""; |
| 21 | public $support_link = ""; |
| 22 | public $review_link = ""; |
| 23 | public $downloads = 0; |
| 24 | public $rating = "0.0"; |
| 25 | public $reviews = array(); |
| 26 | public $popular_rank = 0; |
| 27 | public $latest_versions = array('free' => '0.0.0', 'paid' => '0.0.0'); |
| 28 | public $is_paid = false; |
| 29 | public $last_update_date = ""; |
| 30 | public $requires = array(); |
| 31 | public $featured = true; |
| 32 | public $purchase_info = array(); |
| 33 | private $type = "plugin"; |
| 34 | |
| 35 | protected $download_data = array(); |
| 36 | protected $download_link = ""; |
| 37 | protected $installed; |
| 38 | protected $error = array('code' => '', 'msg' => ''); |
| 39 | protected $rest_parameters = array(); |
| 40 | protected $origin = "10web"; |
| 41 | |
| 42 | public function __construct($id, $slug, $title, $description, $type = "plugin") |
| 43 | { |
| 44 | $this->id = $id; |
| 45 | $this->slug = $slug; |
| 46 | $this->title = $title; |
| 47 | $this->description = $description; |
| 48 | $this->type = $type; |
| 49 | $this->installed = false; |
| 50 | } |
| 51 | |
| 52 | public function set_product_data($data) |
| 53 | { |
| 54 | |
| 55 | $this->type = isset($data['type']) ? $data['type'] : null; |
| 56 | $this->author = isset($data['author']) ? $data['author'] : null; |
| 57 | $this->author_url = isset($data['author_url']) ? $data['author_url'] : null; |
| 58 | $this->with_addons = isset($data['with_addons']) ? $data['with_addons'] : null; |
| 59 | $this->zip_name = isset($data['zip_name']) ? $data['zip_name'] : null; |
| 60 | $this->parent_id = isset($data['parent_id']) ? $data['parent_id'] : null; |
| 61 | $this->logo = isset($data['logo']) ? $data['logo'] : null; |
| 62 | $this->info_link = isset($data['info_link']) ? $data['info_link'] : null; |
| 63 | $this->demo_link = isset($data['demo_link']) ? $data['demo_link'] : null; |
| 64 | $this->support_link = isset($data['support_link']) ? $data['support_link'] : null; |
| 65 | $this->review_link = isset($data['review_link']) ? $data['review_link'] : null; |
| 66 | $this->downloads = isset($data['downloads']) ? $data['downloads'] : null; |
| 67 | $this->rating = isset($data['rating']) ? $data['rating'] : null; |
| 68 | $this->reviews = isset($data['reviews']) ? $data['reviews'] : null; |
| 69 | $this->popular_rank = isset($data['popular_rank']) ? $data['popular_rank'] : null; |
| 70 | $this->is_paid = isset($data['is_paid']) ? $data['is_paid'] : null; |
| 71 | $this->latest_versions = isset($data['current_version']) ? $data['current_version'] : null; |
| 72 | $this->last_update_date = isset($data['last_updated']) ? $data['last_updated'] : null; |
| 73 | $this->requires = isset($data['requires']) ? $data['requires'] : null; |
| 74 | $this->featured = isset($data['featured']) ? $data['featured'] : null; |
| 75 | $this->purchase_info = isset($data['purchase_info']) ? $data['purchase_info'] : null; |
| 76 | $this->download_link = isset($data['download_link']) ? $data['download_link'] : null; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | public function is_addon() |
| 81 | { |
| 82 | return ($this->parent_id != 0); |
| 83 | } |
| 84 | |
| 85 | public function is_installed() |
| 86 | { |
| 87 | return $this->installed; |
| 88 | } |
| 89 | |
| 90 | public function install($no_error_when_installed = false) |
| 91 | { |
| 92 | if ($this->type == "theme") { |
| 93 | return $this->install_theme(); |
| 94 | } else { |
| 95 | return $this->install_plugin($no_error_when_installed); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function install_plugin($no_error_when_installed = false) |
| 100 | { |
| 101 | |
| 102 | /*check if dir exists*/ |
| 103 | $plugin_folder = dirname($this->slug); |
| 104 | $plugin_folder = str_replace(array('/', "\\"), array('', ''), $plugin_folder); |
| 105 | if (!empty($plugin_folder) && $plugin_folder != '.' && file_exists(WP_PLUGIN_DIR . "/" . $plugin_folder)) { |
| 106 | $this->set_error('install_error', "Plugin already installed."); |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | $this->include_upgrade_libs(); |
| 112 | $skin = $this->get_skin(); |
| 113 | |
| 114 | if ($skin == null) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | $upgrader = new \Plugin_Upgrader($skin); |
| 119 | $fs_options = apply_filters('upgrader_package_options', array('destination' => WP_PLUGIN_DIR)); |
| 120 | if ($upgrader->fs_connect(array(WP_CONTENT_DIR, $fs_options['destination'])) !== true) { |
| 121 | $this->set_error('fs_error', "File system error. Invalid file permissions or FTP credentials."); |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if ($this->set_download_data() == false) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | add_filter('http_request_args', array($this, 'add_headers'), 9999, 2); |
| 131 | $result = $upgrader->install($this->download_data['url']); |
| 132 | if ($result === true) { |
| 133 | |
| 134 | if (!empty($upgrader->result['destination_name'])) { |
| 135 | $this->slug = $upgrader->result['destination_name']; |
| 136 | } else if (isset($skin->upgrader->result['destination_name'])) { |
| 137 | $this->slug = $skin->upgrader->result['destination_name']; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | } else { |
| 142 | |
| 143 | $skin_errors = $skin->get_errors()->errors; |
| 144 | $errors = (!empty($skin_errors)) ? $skin_errors : array(); |
| 145 | |
| 146 | if (!empty($errors)) { |
| 147 | Helper::set_error_log('try_to_install_plugin_' . $this->slug, json_encode($errors)); |
| 148 | } |
| 149 | |
| 150 | if (!empty($errors['folder_exists']) && $no_error_when_installed) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | if (!empty($errors['folder_exists'])) { |
| 155 | $this->set_error('product_already_installed', 'Product already installed.'); |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | if (is_wp_error($result)) { |
| 160 | $this->set_error('failed_to_install', $result->get_error_message()); |
| 161 | |
| 162 | return false; |
| 163 | } else { |
| 164 | $this->set_error('failed_to_install', 'Something went wrong.'); |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | private function install_theme() |
| 174 | { |
| 175 | |
| 176 | $theme_folder = str_replace(array('/', "\\"), array('', ''), $this->slug); |
| 177 | if (!empty($theme_folder) && $theme_folder != '.' && file_exists(WP_CONTENT_DIR . "/themes/" . $theme_folder)) { |
| 178 | $this->set_error('install_error', "Theme already installed."); |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | $this->include_upgrade_libs(); |
| 184 | $skin = $this->get_skin(); |
| 185 | |
| 186 | if ($skin == null) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | $upgrader = new \Theme_Upgrader($skin); |
| 191 | |
| 192 | |
| 193 | $fs_options = apply_filters('upgrader_package_options', array('destination' => get_theme_root())); |
| 194 | if ($upgrader->fs_connect(array(WP_CONTENT_DIR, $fs_options['destination'])) !== true) { |
| 195 | $this->set_error('fs_error', "File system error. Invalid file permissions or FTP credentials."); |
| 196 | |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | if ($this->set_download_data() == false) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | add_filter('http_request_args', array($this, 'add_headers'), 9999, 2); |
| 205 | $result = $upgrader->install($this->download_data['url']); |
| 206 | |
| 207 | if ($result === true) { |
| 208 | |
| 209 | if (isset($skin->upgrader->result['destination_name'])) { |
| 210 | $this->slug = $skin->upgrader->result['destination_name']; |
| 211 | $all_themes = wp_get_themes(array('errors' => null)); |
| 212 | if (!empty($all_themes[$this->slug])) { |
| 213 | $this->title = $all_themes[$this->slug]['Name']; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return true; |
| 218 | } else { |
| 219 | |
| 220 | $skin_errors = $skin->get_errors()->errors; |
| 221 | $errors = (!empty($skin_errors)) ? $skin_errors : array(); |
| 222 | |
| 223 | if (!empty($errors)) { |
| 224 | Helper::set_error_log('try_to_install_theme_' . $this->slug, json_encode($errors)); |
| 225 | } |
| 226 | |
| 227 | if (!empty($errors['folder_exists'])) { |
| 228 | $this->set_error('product_already_installed', 'Product already installed.'); |
| 229 | |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | if (is_wp_error($result)) { |
| 235 | $this->set_error('failed_to_install', $result->get_error_message()); |
| 236 | |
| 237 | return false; |
| 238 | } else { |
| 239 | $this->set_error('failed_to_install', 'Something went wrong.'); |
| 240 | |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | |
| 249 | public function add_headers($args, $url) |
| 250 | { |
| 251 | |
| 252 | if ($url != $this->download_data['url']) { |
| 253 | return $args; |
| 254 | } |
| 255 | |
| 256 | if (isset($this->download_data['headers'])) { |
| 257 | $args['headers'] = $this->download_data['headers']; |
| 258 | } |
| 259 | |
| 260 | return $args; |
| 261 | } |
| 262 | |
| 263 | protected function include_upgrade_libs() |
| 264 | { |
| 265 | if ($this->type == "theme") { |
| 266 | require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 267 | require_once(ABSPATH . 'wp-admin/includes/theme.php'); |
| 268 | require_once(ABSPATH . 'wp-admin/includes/admin.php'); |
| 269 | } else { |
| 270 | require_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); |
| 271 | require_once(ABSPATH . 'wp-admin/includes/plugin-install.php'); |
| 272 | require_once(ABSPATH . 'wp-admin/includes/admin.php'); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @return boolean |
| 278 | */ |
| 279 | protected function set_download_data() |
| 280 | { |
| 281 | |
| 282 | /*if($this->id === TENWEB_MANAGER_ID) { |
| 283 | |
| 284 | $this->download_data = array( |
| 285 | 'headers' => null, |
| 286 | 'url' => $this->get_download_link() |
| 287 | ); |
| 288 | |
| 289 | return true; |
| 290 | }*/ |
| 291 | |
| 292 | if ($this->origin === "wp.org") { |
| 293 | $wp_org_package = $this->get_wp_package(); |
| 294 | |
| 295 | |
| 296 | if ($this->type === "theme") { |
| 297 | $this->download_data = array( |
| 298 | 'headers' => array(), |
| 299 | 'url' => !empty($wp_org_package) ? $wp_org_package : "https://downloads.wordpress.org/theme/" . $this->slug . ".zip" |
| 300 | ); |
| 301 | } else { |
| 302 | $this->download_data = array( |
| 303 | 'headers' => array(), |
| 304 | 'url' => !empty($wp_org_package) ? $wp_org_package : "https://downloads.wordpress.org/plugin/" . $this->slug . ".zip" |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | return true; |
| 309 | } else if ($this->origin === "upload") { |
| 310 | $this->download_data = array( |
| 311 | 'headers' => array(), |
| 312 | 'url' => $this->download_link |
| 313 | ); |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | $tokens = Helper::get_instance()->get_amazon_tokens($this->id); |
| 319 | |
| 320 | if (is_null($tokens)) { |
| 321 | $this->set_error('token_error', "An error happened when trying to download item"); |
| 322 | |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | //include_once TENWEB_INCLUDES_DIR . '/class-amazon.php'; |
| 327 | $file = ltrim($tokens['path'], TENWEB_S3_BUCKET); |
| 328 | |
| 329 | $amazon = new Amazon( |
| 330 | $tokens['key'], |
| 331 | $tokens['secret'], |
| 332 | $tokens['token'], |
| 333 | $file |
| 334 | ); |
| 335 | |
| 336 | $this->download_data = $amazon->getRequestData(); |
| 337 | |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | protected function get_wp_package() |
| 342 | { |
| 343 | |
| 344 | if ($this->type === "theme") { |
| 345 | |
| 346 | include_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 347 | $request = themes_api('theme_information', array('slug' => $this->slug, 'fields' => array('sections' => false, 'tags' => false))); |
| 348 | |
| 349 | } else { |
| 350 | $request = wp_remote_get('https://api.wordpress.org/plugins/info/1.0/' . $this->slug); |
| 351 | } |
| 352 | |
| 353 | if (is_wp_error($request)) { |
| 354 | $this->set_error('get_wp_package_error', $request->get_error_message()); |
| 355 | |
| 356 | return ""; |
| 357 | } |
| 358 | |
| 359 | if (is_array($request) && isset($request['body'])) { |
| 360 | $body = unserialize($request['body']); |
| 361 | } else { |
| 362 | $body = $request; |
| 363 | } |
| 364 | |
| 365 | if (empty($body->download_link)) { |
| 366 | $this->set_error('get_wp_package_error', "Something went wrong."); |
| 367 | |
| 368 | return ""; |
| 369 | } |
| 370 | |
| 371 | return $body->download_link; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * @return null on fail or skin object on success |
| 376 | */ |
| 377 | protected function get_skin() |
| 378 | { |
| 379 | |
| 380 | if (class_exists('\WP_Ajax_Upgrader_Skin')) { |
| 381 | $skin = new \WP_Ajax_Upgrader_Skin(); |
| 382 | } else if (file_exists(ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php')) { |
| 383 | require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 384 | $skin = new \WP_Ajax_Upgrader_Skin(); |
| 385 | } else { |
| 386 | //require_once TENWEB_INCLUDES_DIR . '/class-wp-ajax-upgrader-skin.php'; |
| 387 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 388 | } |
| 389 | |
| 390 | $credentials = $skin->request_filesystem_credentials(false, WP_CONTENT_DIR, false); |
| 391 | if ($credentials == false) { |
| 392 | $this->set_error('fs_error', "File system error. Invalid file permissions or FTP credentials."); |
| 393 | |
| 394 | return null; |
| 395 | } |
| 396 | |
| 397 | return $skin; |
| 398 | } |
| 399 | |
| 400 | protected function set_error($code, $msg) |
| 401 | { |
| 402 | $this->error['code'] = $code; |
| 403 | $this->error['msg'] = $msg; |
| 404 | } |
| 405 | |
| 406 | public function get_error() |
| 407 | { |
| 408 | return $this->error; |
| 409 | } |
| 410 | |
| 411 | public function get_type() |
| 412 | { |
| 413 | return $this->type; |
| 414 | } |
| 415 | |
| 416 | public function set_download_link($download_link) |
| 417 | { |
| 418 | $this->download_link = $download_link; |
| 419 | } |
| 420 | |
| 421 | public function set_rest_parameters($rest_parameters) |
| 422 | { |
| 423 | $this->rest_parameters = $rest_parameters; |
| 424 | } |
| 425 | |
| 426 | public function set_origin($origin) |
| 427 | { |
| 428 | $this->origin = $origin; |
| 429 | } |
| 430 | |
| 431 | public function get_origin() |
| 432 | { |
| 433 | return $this->origin; |
| 434 | } |
| 435 | |
| 436 | public function get_download_link() |
| 437 | { |
| 438 | return $this->download_link; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
| 443 | } |
| 444 |