Esempio n. 1
0
 /**
  * Parse body to get data
  * @return JRegistry
  */
 public function parse()
 {
     $contentType = $this->getInfo('content_type');
     if ($contentType) {
         if (strpos($contentType, ';') !== false) {
             $contentType = explode(';', $contentType);
             $contentType = trim($contentType[0]);
         }
         switch ($contentType) {
             case 'text/html':
                 $parser = CParsers::getParser('metas', array('content' => $this->getBody(), 'url' => $this->getInfo('url')));
                 /* extract meta data from body */
                 $data = $parser->extract();
                 /**
                  * Images process
                  * @todo need to improve
                  */
                 $images = $data->get('image');
                 $counter = 0;
                 $limit = $this->get('max_images', 5);
                 if (is_array($images)) {
                     $_images = array();
                     foreach ($images as $key => $imageUrl) {
                         //$imageUrl = strtolower($imageUrl);
                         /* This imageurl already have valid path */
                         if (strpos(strtolower($imageUrl), 'http://') !== false || strpos(strtolower($imageUrl), 'https://') !== false) {
                         } else {
                             /* Image have no URL than we need add it */
                             $host = parse_url($this->getInfo('url'), PHP_URL_HOST);
                             $url = JUri::getInstance($this->getInfo('url'));
                             if (substr($imageUrl, 0, 2) == '//') {
                                 $imageUrl = $url->getScheme() . ':' . $imageUrl;
                             } else {
                                 $url = JUri::getInstance($this->getInfo('url'));
                                 $url = $url->getScheme() . '://' . $host;
                                 $imageUrl = strtolower($url . $imageUrl);
                             }
                         }
                         $imgArr = explode('.', $imageUrl);
                         if (count($imgArr) == 1 || !in_array($imgArr[count($imgArr) - 1], array('jpg', 'png', 'jpeg', 'gif'))) {
                             continue;
                         }
                         /**
                          * Read image and make sure image size is valid
                          */
                         $imageContent = @file_get_contents($imageUrl);
                         if ($imageContent) {
                             $im = imagecreatefromstring($imageContent);
                             $width = imagesx($im);
                             $height = imagesy($im);
                             if ($width >= $this->get('max_image_width', 128) && $height >= $this->get('max_image_height', 128) && $counter <= $limit) {
                                 $_images[] = $imageUrl;
                                 $counter++;
                             }
                         }
                     }
                 }
                 $data->set('image', $_images);
                 return $data;
         }
     }
 }
Esempio n. 2
0
 /**
  * Parse body to get data
  * @return JRegistry
  */
 public function parse()
 {
     $contentType = $this->getInfo('content_type');
     if ($contentType) {
         if (strpos($contentType, ';') !== false) {
             $contentType = explode(';', $contentType);
             $contentType = trim($contentType[0]);
         }
         switch ($contentType) {
             case 'text/html':
                 $parser = CParsers::getParser('metas', array('content' => $this->getBody(), 'url' => $this->getInfo('url')));
                 /* extract meta data from body */
                 $data = $parser->extract();
                 /**
                  * Images process
                  * @todo need to improve
                  */
                 $images = $data->get('image');
                 $limit = $this->get('max_images', 4);
                 if (is_array($images)) {
                     $_images = array();
                     foreach ($images as $key => $imageUrl) {
                         // Stop if max_images reached.
                         if ($limit > 0 && count($_images) >= $limit) {
                             break;
                         }
                         //$imageUrl = strtolower($imageUrl);
                         /* This imageurl already have valid path */
                         if (strpos(strtolower($imageUrl), 'http://') !== false || strpos(strtolower($imageUrl), 'https://') !== false) {
                         } else {
                             /* Image have no URL than we need add it */
                             $host = parse_url($this->getInfo('url'), PHP_URL_HOST);
                             $url = JUri::getInstance($this->getInfo('url'));
                             if (substr($imageUrl, 0, 2) == '//') {
                                 $imageUrl = $url->getScheme() . ':' . $imageUrl;
                             } else {
                                 $url = JUri::getInstance($this->getInfo('url'));
                                 $url = $url->getScheme() . '://' . $host;
                                 $imageUrl = strtolower($url . $imageUrl);
                             }
                         }
                         // Exclude base64 image data.
                         if (strpos($imageUrl, ';base64,')) {
                             continue;
                         }
                         // Read image and make sure image size is valid
                         try {
                             $ch = curl_init();
                             curl_setopt($ch, CURLOPT_HEADER, false);
                             curl_setopt($ch, CURLOPT_URL, $imageUrl);
                             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                             curl_setopt($ch, CURLOPT_TIMEOUT, 20);
                             $imageContent = curl_exec($ch);
                             curl_close($ch);
                             if ($imageContent) {
                                 $im = @imagecreatefromstring($imageContent);
                                 $width = @imagesx($im);
                                 $height = @imagesy($im);
                                 if ($width >= $this->get('max_image_width', 128) && $height >= $this->get('max_image_height', 128)) {
                                     $_images[] = $imageUrl;
                                 }
                             }
                         } catch (Exception $e) {
                             // do nothing
                         }
                     }
                 }
                 $data->set('image', $_images);
                 return $data;
         }
     }
 }