File indexing completed on 2025-05-04 05:29:12
0001 <?php 0002 /** 0003 * ocs-webserver 0004 * 0005 * Copyright 2016 by pling GmbH. 0006 * 0007 * This file is part of ocs-webserver. 0008 * 0009 * This program is free software: you can redistribute it and/or modify 0010 * it under the terms of the GNU Affero General Public License as 0011 * published by the Free Software Foundation, either version 3 of the 0012 * License, or (at your option) any later version. 0013 * 0014 * This program is distributed in the hope that it will be useful, 0015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 * GNU Affero General Public License for more details. 0018 * 0019 * You should have received a copy of the GNU Affero General Public License 0020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 **/ 0022 class Default_Model_Discovery 0023 { 0024 0025 protected $_discoveryDomains = array( 0026 'YouTube' => array( 0027 'www.youtube.com', 0028 'youtube.com' 0029 ) 0030 ); 0031 0032 public function guessGeneralData($url) 0033 { 0034 if (!preg_match('/(http|https)\:\/\//', $url)) { 0035 $url = 'http://' . $url; 0036 } 0037 $url_host = parse_url($url, PHP_URL_HOST); 0038 if (in_array($url_host, $this->_discoveryDomains['YouTube'])) { 0039 $data = $this->getYoutubeData($this->getYoutubeCode($url)); 0040 $data['content_url'] = $url; 0041 return $data; 0042 } else { 0043 try { 0044 return $this->getGeneralData($url); 0045 } catch (Exception $e) { 0046 return null; 0047 } 0048 0049 } 0050 } 0051 0052 public function getYoutubeData($code) 0053 { 0054 $yt = new Zend_Gdata_YouTube(); 0055 $video = $yt->getVideoEntry($code); 0056 $data = array( 0057 'description' => $video->getVideoDescription(), 0058 'title' => $video->getVideoTitle(), 0059 'content_type' => 'youtube', 0060 'thumbnails' => array() 0061 ); 0062 0063 $image_m = new Default_Model_DbTable_Image(); 0064 $ImageHelper = new Default_View_Helper_Image(); 0065 $thumbs = $video->getVideoThumbnails(); 0066 foreach ($thumbs as $thumbnail) { 0067 $img_src = $thumbnail['url']; 0068 $filename = $image_m->storeRemoteImage($img_src, $file_info); 0069 $thumb = array( 0070 'full_url' => $ImageHelper->Image($filename, array('temporal' => true)), 0071 'filename' => $filename 0072 ); 0073 $data['thumbnails'][] = $thumb; 0074 } 0075 0076 return $data; 0077 } 0078 0079 public function getYouTubeCode($url) 0080 { 0081 $url_host = parse_url($url, PHP_URL_HOST); 0082 if (in_array($url_host, $this->_discoveryDomains['YouTube'])) { 0083 parse_str(parse_url($url, PHP_URL_QUERY), $url_vars); 0084 if (!empty($url_vars['v'])) { 0085 return $url_vars['v']; 0086 } else { 0087 return false; 0088 } 0089 } else { 0090 return false; 0091 } 0092 } 0093 0094 public function getGeneralData($url) 0095 { 0096 $meta_info = array( 0097 'description', 0098 'og:image' 0099 ); 0100 $dom = new DOMDocument; 0101 if (@$dom->loadHTMLFile($url)) { 0102 $result = array(); 0103 $result['content_url'] = $url; 0104 $result['title'] = $dom->getElementsByTagName('title')->item(0)->textContent; 0105 $meta = $dom->getElementsByTagName('meta'); 0106 for ($i = 0; $i < $meta->length; $i++) { 0107 $m = $meta->item($i); 0108 if (in_array($m->getAttribute('name'), $meta_info) || in_array($m->getAttribute('property'), $meta_info)) { 0109 $name = $m->getAttribute('name'); 0110 if ($name == '') { 0111 $name = $m->getAttribute('property'); 0112 } 0113 $result[$name] = $m->getAttribute('content'); 0114 } 0115 } 0116 $host = parse_url($url, PHP_URL_HOST); 0117 0118 $dom_img = $dom->getElementsByTagName('img'); 0119 $result['thumbnails'] = array(); 0120 $image_m = new Default_Model_DbTable_Image(); 0121 $ImageHelper = new Default_View_Helper_Image(); 0122 0123 $n_images = 0; 0124 $src_images = array(); 0125 if (isset($result['og:image'])) { 0126 $src_images[] = $result['og:image']; 0127 } 0128 0129 for ($i = 0; $i < $dom_img->length; $i++) { 0130 $src_images[] = $dom_img->item($i)->getAttribute('src'); 0131 } 0132 0133 foreach ($src_images as $img_src) { 0134 if (parse_url($img_src, PHP_URL_HOST) == null) { 0135 $img_src = dirname($url) . '/' . $img_src; 0136 } 0137 $file_info = null; 0138 try { 0139 $filename = $image_m->storeRemoteImage($img_src, $file_info); 0140 if ($file_info['size'] > 4096) { #4Kb 0141 $thumb = array( 0142 'full_url' => $ImageHelper->Image($filename, array('temporal' => true)), 0143 'filename' => $filename 0144 ); 0145 $result['thumbnails'][] = $thumb; 0146 $n_images++; 0147 } 0148 0149 } catch (Exception $e) { 0150 } 0151 if ($n_images >= 5) { 0152 break; 0153 } 0154 } 0155 0156 $result['content_type'] = 'text'; 0157 return $result; 0158 } else { 0159 throw new Exception('Invalid url'); 0160 } 0161 } 0162 0163 }