SHARE
TWEET

Untitled

a guest Oct 17th, 2018 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function fetchImage($image_url, $folder='') {
  2.  
  3.         $error = '';
  4.         $info = '';
  5.         $filename = '';
  6.  
  7.         $image_url = trim($image_url);
  8.  
  9.         if (strstr($image_url, 'catalog/') == 0 && file_exists(DIR_IMAGE . $folder . $image_url)) {
  10.             $error = sprintf("[%s] - %s", $this->language->get('log_level_info'), $image_url . ' is not a URL, using local file instead.');
  11.             $filename = $image_url;
  12.         }
  13.  
  14.         if (strpos($image_url, 'http') !== 0) {
  15.             $error = sprintf("[%s] - %s", $this->language->get('log_level_info'), $image_url . ' is not a valid URL, skipping image.');
  16.         }
  17.  
  18.         if (empty($error)) {
  19.  
  20.             if($folder != '') {
  21.                 $new_folder = DIR_IMAGE . 'catalog/' . $folder;
  22.                 if (!file_exists($new_folder)) {
  23.                     mkdir($new_folder, 0777, true);
  24.                 }
  25.             }
  26.  
  27.             if (strstr($image_url, '?')) {
  28.                 if($folder != '' && $folder != '/') {
  29.                     $filename = 'catalog/' . $folder . '/' . md5($image_url) . '.jpg';
  30.                 } else {
  31.                     $filename = 'catalog/' . md5($image_url) . '.jpg';
  32.                 }
  33.             } else {
  34.                 $url_parts = explode('/', $image_url);
  35.                 // Decode html space for image filename
  36.                 $end = str_replace('%20', ' ', end($url_parts));
  37.                 if($folder != '' && $folder != '/') {
  38.                     $filename = 'catalog/' . $folder . '/' . $end;
  39.                 } else {
  40.                     $filename = 'catalog/' . $end;
  41.                 }
  42.             }
  43.  
  44.             if (pathinfo($filename, PATHINFO_EXTENSION) == "")
  45.                 $filename = $filename.'.jpg';
  46.  
  47.             if (!file_exists(DIR_IMAGE . $filename)) {
  48.                 $fp = fopen(DIR_IMAGE . $filename, 'w');
  49.                 $ch = curl_init();
  50.                 $ports = array();
  51.                 // Encode spaces in url
  52.                 $image_url = str_replace(' ', '%20', $image_url);
  53.                 if (preg_match('/:(\d+)/', $image_url, $ports)) {
  54.                     $image_url = preg_replace('/:\d+/', '', $image_url);
  55.                     curl_setopt($ch, CURLOPT_PORT, (int)$ports[1]);
  56.                 }
  57.                 curl_setopt($ch, CURLOPT_URL, $image_url);
  58.                 curl_setopt($ch, CURLOPT_FILE, $fp);
  59.                 if (ini_get('open_basedir') == '') {
  60.                     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  61.                     curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  62.                 }
  63.                 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  64.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  65.                 curl_exec($ch);
  66.  
  67.                 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  68.                 $info = curl_getinfo($ch);
  69.                 curl_close($ch);
  70.                 fclose($fp);
  71.                 $file_info = "";
  72.                 if (file_exists(DIR_IMAGE . $filename) && filesize(DIR_IMAGE . $filename) > 0)
  73.                     $file_info = getimagesize(DIR_IMAGE . $filename);
  74.                 if($httpCode == 404 || empty($file_info) || (isset($file_info['mime']) && strpos($file_info['mime'], 'image/') !== 0)) {
  75.                     unlink(DIR_IMAGE . $filename);
  76.                     $filename = '';
  77.                     if (isset($file_info['mime']) && strpos($file_info['mime'], 'image/') !== 0) {
  78.                         $error = sprintf("[%s] - %s", $this->language->get('log_level_warning'), $image_url . " was not an image.");
  79.                     }
  80.                     if($httpCode == 404) {
  81.                         $error = sprintf("[%s] - %s", $this->language->get('log_level_warning'), $image_url . " not found. (404)");
  82.                     }
  83.                     if(empty($file_info)) {
  84.                         $error = sprintf("[%s] - %s", $this->language->get('log_level_warning'), $image_url . " was empty.");
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.         return array('filename' => $filename, 'error' => $error, 'info' => $info);
  90.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top