* @link http://www.themepunch.com/ * @copyright 2016 ThemePunch */ class RevSliderObjectLibrary { private $library_url = 'http://library.themepunch.tools/'; private $library_list = 'library.php'; private $library_download = 'download.php'; private $object_thumb_path = '/revslider/objects/thumbs/'; private $object_orig_path = '/revslider/objects/'; private $curl_check = null; const LIBRARY_VERSION = '1.0.0'; /** * get list of objects * @since: 5.3.0 */ public function _get_list($force = false){ global $wp_version; $last_check = get_option('revslider-library-check'); if($last_check == false){ //first time called $last_check = 1296001; update_option('revslider-library-check', time()); } // Get latest object list if(time() - $last_check > 1296000 || $force == true){ //30 days update_option('revslider-library-check', time()); $code = get_option('revslider-code', ''); $library_version = self::LIBRARY_VERSION; $validated = get_option('revslider-valid', 'false'); if($validated == 'false'){ $code = ''; } $rattr = array( 'code' => urlencode($code), 'library_version' => urlencode($library_version), 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION), 'product' => urlencode('revslider_prestashop') ); $request = wp_remote_post($this->library_url.$this->library_list, array( 'user-agent' => 'Prestashop'.$wp_version.'; '.get_bloginfo('url'), 'body' => $rattr )); if($request != null) { if($response = maybe_unserialize($request['body'])) { $library = json_decode($response, true); if(is_array($library)) { update_option('rs-library', $library, false); } } } } } public function _get_object_data($object_handle){ $data = array('thumb' => $object_handle, 'orig' => $object_handle); //$upload_dir = wp_upload_dir(); // Set upload folder $upload_dir['basedir'] = wp_upload_dir(); $upload_dir['baseurl'] = wp_upload_url(); $file = $upload_dir['basedir'] . $this->object_thumb_path . $object_handle; if(file_exists($file)){ $url_file = $upload_dir['baseurl'] . $this->object_thumb_path . $object_handle; $data['thumb'] = $url_file; } $file = $upload_dir['basedir'] . $this->object_orig_path . $object_handle; if(file_exists($file)){ $url_file = $upload_dir['baseurl'] . $this->object_orig_path . $object_handle; $data['orig'] = $url_file; //check } return $data; } /** * check if given URL is an object from object library * @since: 5.3.0 */ public function _is_object($url){ $is_object = false; $upload_dir = wp_upload_dir(); // Set upload folder $upload_url = wp_upload_url(); // Set upload folder $upload_directory = $upload_dir . $this->object_orig_path; //$upload_directory = $upload_dir['basedir'] . $this->object_orig_path; $upload_url = $upload_url . $this->object_orig_path; $file_name = explode('/', $url); $file_name = $file_name[count($file_name) - 1]; if(strpos($url, $upload_url) !== false){ //check now if handle is inside of the array of objects $obj = $this->load_objects(); $online = $obj['online']['objects']; foreach($online as $object){ if($object['handle'] == $file_name){ $is_object = true; break; } } } return $is_object; } /** * check if given URL is existing in the object library * @since: 5.3.0 */ public function _does_exist($url){ $does_exist = false; //$upload_dir = wp_upload_dir(); // Set upload folder $upload_dir ['basedir']= wp_upload_dir(); $upload_dir['baseurl'] = wp_upload_url(); $upload_directory = $upload_dir['basedir'] . $this->object_orig_path; $upload_url = $upload_dir['baseurl'] . $this->object_orig_path; $url = str_replace($upload_url, '', $url); if(file_exists($upload_directory.$url)){ $does_exist = true; } return $does_exist; } /** * check if certain object needs to be redownloaded * @since: 5.3.0 */ public function _check_object_exist($object_url){ //first check if it is an object $is_obj = $this->_is_object($object_url); //then check if it is existing if($is_obj){ if($this->_does_exist($object_url)){ //all cool }else{ //if not, redownload if allowed //need to redownload $file_name_with_ending = explode("/", $object_url); $file_name_with_ending = $file_name_with_ending[count($file_name_with_ending) - 1]; $this->_get_object_thumb($file_name_with_ending, 'orig'); } } } /** * get certain objects thumbnail, download if needed and if not, simply return path * @since: 5.3.0 */ public function _get_object_thumb($object_handle, $type){ global $wp_version; $error = ''; if($type == 'thumb'){ $path = $this->object_thumb_path; }else{ $path = $this->object_orig_path; } $download = false; //$upload_dir = wp_upload_dir(); // Set upload folder $upload_dir ['basedir']= wp_upload_dir(); $upload_dir['baseurl'] = wp_upload_url(); $file = $upload_dir['basedir'] . $path . $object_handle; $url_file = $upload_dir['baseurl'] . $path . $object_handle; //check if object thumb is already downloaded $download = (!file_exists($file)) ? true : false; //check if new version of object thumb is available // Check folder permission and define file location if($download && wp_mkdir_p( $upload_dir['basedir'].$path ) ) { //$curl = ($this->check_curl_connection()) ? new WP_Http_Curl() : false; $curl = true; $file = $upload_dir['basedir'] . $path . $object_handle; if(!file_exists($file) || isset($temp['push_image'])){ $image_data = false; if($curl !== false){ $validated = get_option('revslider-valid', 'false'); if($validated == 'false' && $type != 'thumb'){ $error = __('Plugin not activated', 'revslider'); }else{ $code = ($validated == 'false') ? '' : get_option('revslider-code', ''); $image_data = wp_remote_post($this->library_url.$this->library_download, array( 'user-agent' => 'Prestashop'.$wp_version.'; '.get_bloginfo('url'), 'body' => array( 'code' => urlencode($code), 'library_version' => urlencode(self::LIBRARY_VERSION), 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION), 'handle' => urlencode($object_handle), 'download' => urlencode($type), 'product' => urlencode('revslider_prestashop') ), 'timeout' => 45 )); //var_dump($image_data);die(); if(!is_wp_error($image_data) && isset($image_data['body']) && isset($image_data['response']) && isset($image_data['response']['code']) && $image_data['response']['code'] == '200'){ $image_data = $image_data['body']; //check body for errors in here $check = json_decode($image_data, true); if(!empty($check)){ if(isset($check['error'])){ $image_data = false; $error = $check['error']; } }elseif(trim($image_data) == ''){ $error = __('No data received', 'revslider'); } }else{ $image_data = false; $error = __('Error downloading object', 'revslider'); } } }else{ //cant download file } if($image_data !== false){ @mkdir(dirname($file)); @file_put_contents( $file, $image_data ); $this->create_image_dimensions($object_handle); }else{//could not connect to server $error = __('Error downloading object', 'revslider'); } }else{//use default image $error = __('Error downloading object', 'revslider'); } }else{//use default images $error = __('Error downloading object', 'revslider'); } if($error !== ''){ return array('error' => true); } $width = false; $height = false; //get dimensions of image $imgsize = getimagesize( $file ); if($imgsize !== false){ $width = $imgsize['0']; $height = $imgsize['1']; } return array('error' => false, 'url' => $url_file, 'width' => $width, 'height' => $height); } /** * import object to media library * @since: 5.3.0 */ public function _import_object($file_path){ //$curl = ($this->check_curl_connection()) ? new WP_Http_Curl() : false; //$upload_dir = wp_upload_dir(); // Set upload folder $upload_dir ['basedir']= wp_upload_dir(); $upload_dir['baseurl'] = wp_upload_url(); $path = $this->object_orig_path; $object_handle = basename($file_path); $file = $upload_dir['basedir'] . $path . $object_handle; $url_file = $upload_dir['baseurl'] . $path . $object_handle; $image_handle = @fopen($file_path, "r"); if($image_handle != false){ $image_data = stream_get_contents($image_handle); if($image_data !== false){ @mkdir(dirname($file)); @file_put_contents( $file, $image_data ); $this->create_image_dimensions($object_handle); return array('path' => $url_file); } } return false; } public function write_markup(){ ?>