File indexing completed on 2024-12-15 05:21:36
0001 <?php 0002 0003 /** 0004 * ocs-apiserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-apiserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 **/ 0023 class Application_Model_DbTable_Image extends Zend_Db_Table_Abstract 0024 { 0025 protected $_name = "image"; 0026 protected $_fields = array( 0027 'id' => null, 0028 'filename' => null, 0029 'code' => null, 0030 'name' => null, 0031 'member_id' => null, 0032 'model' => null, 0033 'foreign_key' => null, 0034 'foreign_id' => null, 0035 'created' => null 0036 ); 0037 protected $_allowed = array( 0038 'image/jpeg' => '.jpg', 0039 'image/jpg' => '.jpg', 0040 'image/png' => '.png', 0041 'image/gif' => '.gif', 0042 'application/x-empty' => '.png' 0043 ); 0044 protected $_allowedFileExtension = array( 0045 'jpg', 0046 'jpeg', 0047 'png', 0048 'gif' 0049 ); 0050 protected $_maxsize = array( 0051 'width' => 1024, 0052 'height' => 768 0053 ); 0054 protected $_errorMsg = null; 0055 0056 public function getMemberImages($member_id) 0057 { 0058 $images = $this->select()->where('member_id = ?', $member_id)->query()->fetchAll(); 0059 0060 return $images; 0061 } 0062 0063 public function storeExternalImage($url, $fileExtension = null) 0064 { 0065 Zend_Registry::get('logger')->debug(__METHOD__ . ' - ' . print_r(func_get_args(), true)); 0066 $tmpFileName = $this->storeRemoteImage($url, $fileExtension); 0067 0068 if (file_exists(IMAGES_UPLOAD_PATH . 'tmp/' . $tmpFileName)) { 0069 $content_type = mime_content_type(IMAGES_UPLOAD_PATH . 'tmp/' . $tmpFileName); 0070 $filePath = $this->saveImageOnMediaServer($tmpFileName); 0071 $filename = $filePath; 0072 $file_info['size'] = filesize(IMAGES_UPLOAD_PATH . '/' . $filename); 0073 $this->save(array('code' => $content_type, 'filename' => $filename)); 0074 } 0075 0076 return $filename; 0077 } 0078 0079 public function storeRemoteImage($url, $fileExtention = null, &$file_info = null) 0080 { 0081 //$host = parse_url( $url, PHP_URL_HOST ); 0082 //$path = parse_url($url, PHP_URL_PATH); 0083 //$query = parse_url($url, PHP_URL_QUERY); 0084 0085 //$url = 'http://'.$host.$path.'?'.urlencode($query); 0086 $limit = 4194304; #4Mb 0087 $filename = md5($url); 0088 if ($fileExtention) { 0089 $filename .= '.' . $fileExtention; 0090 } 0091 $file_info = array(); 0092 if (file_exists(IMAGES_UPLOAD_PATH . 'tmp/' . $filename)) { 0093 // Delete old file. Maybe an updated version is available. 0094 if (false == unlink(IMAGES_UPLOAD_PATH . 'tmp/' . $filename)) { 0095 throw new Exception('Cannot delete file: ' . IMAGES_UPLOAD_PATH . 'tmp/' . $filename); 0096 } 0097 } 0098 try { 0099 #$file = file_get_contents($url, NULL, NULL, -1, $limit); 0100 $file = $this->file_get_contents_curl($url); 0101 } catch (Exception $e) { 0102 $file = null; 0103 } 0104 if (file_put_contents(IMAGES_UPLOAD_PATH . 'tmp/' . $filename, $file)) { 0105 $content_type = $this->_get_mime_content_type(IMAGES_UPLOAD_PATH . 'tmp/' . $filename); 0106 if (!in_array($content_type, array_keys($this->_allowed))) { 0107 throw new Exception('Format not allowed ' . $content_type . ' for url ' . $url); 0108 } 0109 touch(IMAGES_UPLOAD_PATH . 'tmp/' . $filename); 0110 } else { 0111 throw new Exception('Error storing remote image'); 0112 } 0113 0114 $file_info['size'] = filesize(IMAGES_UPLOAD_PATH . 'tmp/' . $filename); 0115 0116 return $filename; 0117 } 0118 0119 public function file_get_contents_curl($url) 0120 { 0121 $ch = curl_init(); 0122 0123 curl_setopt($ch, CURLOPT_AUTOREFERER, true); 0124 curl_setopt($ch, CURLOPT_HEADER, 0); 0125 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 0126 curl_setopt($ch, CURLOPT_URL, $url); 0127 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 0128 0129 $data = curl_exec($ch); 0130 curl_close($ch); 0131 0132 return $data; 0133 } 0134 0135 public function _get_mime_content_type($filename) 0136 { 0137 0138 if (!function_exists('mime_content_type')) { 0139 0140 $mime_types = array( 0141 0142 'txt' => 'text/plain', 0143 'htm' => 'text/html', 0144 'html' => 'text/html', 0145 'php' => 'text/html', 0146 'css' => 'text/css', 0147 'js' => 'application/javascript', 0148 'json' => 'application/json', 0149 'xml' => 'application/xml', 0150 'swf' => 'application/x-shockwave-flash', 0151 'flv' => 'video/x-flv', 0152 // images 0153 'png' => 'image/png', 0154 'jpe' => 'image/jpeg', 0155 'jpeg' => 'image/jpeg', 0156 'jpg' => 'image/jpeg', 0157 'gif' => 'image/gif', 0158 'bmp' => 'image/bmp', 0159 'ico' => 'image/vnd.microsoft.icon', 0160 'tiff' => 'image/tiff', 0161 'tif' => 'image/tiff', 0162 'svg' => 'image/svg+xml', 0163 'svgz' => 'image/svg+xml', 0164 // archives 0165 'zip' => 'application/zip', 0166 'rar' => 'application/x-rar-compressed', 0167 'exe' => 'application/x-msdownload', 0168 'msi' => 'application/x-msdownload', 0169 'cab' => 'application/vnd.ms-cab-compressed', 0170 // audio/video 0171 'mp3' => 'audio/mpeg', 0172 'qt' => 'video/quicktime', 0173 'mov' => 'video/quicktime', 0174 // adobe 0175 'pdf' => 'application/pdf', 0176 'psd' => 'image/vnd.adobe.photoshop', 0177 'ai' => 'application/postscript', 0178 'eps' => 'application/postscript', 0179 'ps' => 'application/postscript', 0180 // ms office 0181 'doc' => 'application/msword', 0182 'rtf' => 'application/rtf', 0183 'xls' => 'application/vnd.ms-excel', 0184 'ppt' => 'application/vnd.ms-powerpoint', 0185 // open office 0186 'odt' => 'application/vnd.oasis.opendocument.text', 0187 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', 0188 ); 0189 0190 $ext = strtolower(array_pop(explode('.', $filename))); 0191 if (array_key_exists($ext, $mime_types)) { 0192 return $mime_types[$ext]; 0193 } else if (function_exists('finfo_open')) { 0194 $finfo = finfo_open(FILEINFO_MIME); 0195 $mimetype = finfo_file($finfo, $filename); 0196 finfo_close($finfo); 0197 0198 return $mimetype; 0199 } else { 0200 return 'application/octet-stream'; 0201 } 0202 } else { 0203 return mime_content_type($filename); 0204 } 0205 } 0206 0207 public function saveImageOnMediaServer($filePathName) 0208 { 0209 if (empty($filePathName)) { 0210 return null; 0211 } 0212 0213 $content_type = mime_content_type($filePathName); 0214 0215 if (!in_array($content_type, array_keys($this->_allowed))) { 0216 throw new Exception('Format not allowed: ' . $content_type . ' for img: ' . $filePathName); 0217 } 0218 0219 // Generate filename 0220 $generatedFilename = $this->_generateFilename($filePathName); 0221 $destinationFile = IMAGES_UPLOAD_PATH . $generatedFilename . $this->_allowed[$content_type]; 0222 0223 if (copy($filePathName, $destinationFile)) { 0224 if (file_exists($filePathName)) { 0225 if (false === unlink($filePathName)) { 0226 Zend_Registry::get('logger')->warn(__METHOD__ . ' - can not delete temp file: ' . $filePathName); 0227 } 0228 } 0229 Zend_Registry::get('logger')->debug(__METHOD__ . ' - Start upload picture - ' . print_r($destinationFile, 0230 true)) 0231 ; 0232 $srcPathOnMediaServer = $this->sendImageToMediaServer($destinationFile, $content_type); 0233 if (file_exists($destinationFile)) { 0234 if (false === unlink($destinationFile)) { 0235 Zend_Registry::get('logger')->warn(__METHOD__ . ' - can not delete file: ' . $destinationFile); 0236 } 0237 } 0238 if (!$srcPathOnMediaServer) { 0239 throw new Exception("Error in upload to CDN-Server. \n Server message:\n" . $this->_errorMsg); 0240 } 0241 0242 Zend_Registry::get('logger')->debug(__METHOD__ . ' - End upload picture - ' . print_r(IMAGES_UPLOAD_PATH 0243 . $srcPathOnMediaServer, true)) 0244 ; 0245 0246 return $srcPathOnMediaServer; 0247 } 0248 } 0249 0250 private function _generateFilename($filePathName) 0251 { 0252 return sha1_file($filePathName); 0253 } 0254 0255 /** 0256 * @param $fullFilePath 0257 * @param $mimeType 0258 * 0259 * @return string 0260 */ 0261 protected function sendImageToMediaServer($fullFilePath, $mimeType) 0262 { 0263 $config = Zend_Registry::get('config'); 0264 $url = $config->images->media->upload; 0265 0266 $client = new Zend_Http_Client($url); 0267 $client->setFileUpload($fullFilePath, basename($fullFilePath), null, $mimeType); 0268 0269 $response = $client->request('POST'); 0270 0271 if ($response->getStatus() > 200) { 0272 $this->_errorMsg = $response->getBody(); 0273 0274 return null; 0275 } 0276 0277 return $response->getBody(); 0278 } 0279 0280 public function save($image) 0281 { 0282 foreach ($image as $key => $value) { 0283 if (!in_array($key, array_keys($this->_fields))) { 0284 unset($image[$key]); 0285 } 0286 } 0287 0288 if (isset($image['filename']) && !isset($image['code'])) { 0289 $image['code'] = $this->_trimExtension($image['filename']); 0290 } 0291 0292 if (isset($image['id'])) { 0293 return $this->_update($image); 0294 } else { 0295 return $this->_add($image); 0296 } 0297 } 0298 0299 private function _update($image) 0300 { 0301 if (!isset($image['id'])) { 0302 throw new Exception('Invalid update without an id'); 0303 } else { 0304 $id = (int)$image['id']; 0305 } 0306 0307 return $this->update($image, array('id = ?' => $id)); 0308 } 0309 0310 private function _add($image) 0311 { 0312 return $this->insert($image); 0313 } 0314 0315 /** 0316 * @param Zend_Form_Element_File $formFileElement 0317 * 0318 * @return string 0319 * @throws Zend_Exception 0320 * @todo wrong place for this method 0321 */ 0322 public function saveImage($formFileElement) 0323 { 0324 if (empty($formFileElement)) { 0325 Zend_Registry::get('logger')->err(__METHOD__ . ' - form file element empty'); 0326 return null; 0327 } 0328 0329 $filesInfo = $formFileElement->getFileInfo(); 0330 0331 if (1 < count($filesInfo)) { 0332 throw new Zend_Exception('Element contains more than one file elements'); 0333 } 0334 0335 foreach ($filesInfo as $file => $fileInfo) { 0336 if (null == $fileInfo['size']) { 0337 return null; 0338 } 0339 $contentType = mime_content_type($fileInfo['tmp_name']); 0340 if (false === in_array($contentType, array_keys($this->_allowed))) { 0341 throw new Zend_Exception('Format not allowed: ' . $contentType . ' for img: ' . $fileInfo['name']); 0342 } 0343 $generatedFilename = $this->_generateFilename($fileInfo['tmp_name']); 0344 $destinationFile = IMAGES_UPLOAD_PATH . $generatedFilename . $this->_allowed[$contentType]; 0345 Zend_Registry::get('logger')->info(__METHOD__ . ' - destination file path: ' . $destinationFile); 0346 0347 if (copy($fileInfo['tmp_name'], $destinationFile)) { 0348 if (file_exists($fileInfo['tmp_name'])) { 0349 if (false === unlink($fileInfo['tmp_name'])) { 0350 Zend_Registry::get('logger')->warn(__METHOD__ . ' - can not delete temp file: ' 0351 . $fileInfo['tmp_name']) 0352 ; 0353 } 0354 } 0355 Zend_Registry::get('logger')->debug(__METHOD__ . ' - Start upload picture - ' 0356 . print_r($destinationFile, true)) 0357 ; 0358 $srcPathOnMediaServer = $this->sendImageToMediaServer($destinationFile, $contentType); 0359 if (file_exists($destinationFile)) { 0360 if (false === unlink($destinationFile)) { 0361 Zend_Registry::get('logger')->warn(__METHOD__ . ' - can not delete file: ' . $destinationFile); 0362 } 0363 } 0364 if (!$srcPathOnMediaServer) { 0365 throw new Zend_Exception("Error in upload to CDN-Server. \n Server message:\n" . $this->_errorMsg); 0366 } 0367 Zend_Registry::get('logger')->debug(__METHOD__ . ' - End upload a picture - ' 0368 . print_r(IMAGES_UPLOAD_PATH . $srcPathOnMediaServer, true)) 0369 ; 0370 0371 return $srcPathOnMediaServer; 0372 } 0373 } 0374 } 0375 0376 /** 0377 * @param Zend_Form_Element_File $formFileElement 0378 * 0379 * @return array 0380 * @throws Zend_Exception 0381 */ 0382 public function saveImages($formFileElement) 0383 { 0384 if (empty($formFileElement)) { 0385 return array(); 0386 } 0387 0388 $resultPath = array(); 0389 $filesInfo = $formFileElement->getFileInfo(); 0390 0391 foreach ($filesInfo as $file => $fileInfo) { 0392 if (null == $fileInfo['size']) { 0393 continue; 0394 } 0395 $contentType = mime_content_type($fileInfo['tmp_name']); 0396 if (!in_array($contentType, array_keys($this->_allowed))) { 0397 throw new Zend_Exception('Format not allowed: ' . $contentType . ' for img: ' . $fileInfo['name']); 0398 } 0399 $generatedFilename = $this->_generateFilename($fileInfo['tmp_name']); 0400 $destinationFile = IMAGES_UPLOAD_PATH . $generatedFilename . $this->_allowed[$contentType]; 0401 0402 if (copy($fileInfo['tmp_name'], $destinationFile)) { 0403 Zend_Registry::get('logger')->debug(__METHOD__ . ' - Start upload picture - ' 0404 . print_r($destinationFile, true)) 0405 ; 0406 $srcPathOnMediaServer = $this->sendImageToMediaServer($destinationFile, $contentType); 0407 if (!$srcPathOnMediaServer) { 0408 throw new Zend_Exception("Error in upload to CDN-Server. \n Server message:\n" . $this->_errorMsg); 0409 } 0410 Zend_Registry::get('logger')->debug(__METHOD__ . ' - End upload a picture - ' 0411 . print_r(IMAGES_UPLOAD_PATH . $srcPathOnMediaServer, true)) 0412 ; 0413 0414 $resultPath[] = $srcPathOnMediaServer; 0415 } 0416 } 0417 0418 return $resultPath; 0419 } 0420 0421 public function getAllowedFileExtension() 0422 { 0423 return $this->_allowedFileExtension; 0424 } 0425 0426 public function getAllowedMimeTypes() 0427 { 0428 return array_keys($this->getAllowed()); 0429 } 0430 0431 public function getAllowed() 0432 { 0433 return $this->_allowed; 0434 } 0435 0436 public function setAllowed($allowed) 0437 { 0438 $this->_allowed = $allowed; 0439 } 0440 0441 public function getMaxsize() 0442 { 0443 return $this->_maxsize; 0444 } 0445 0446 public function setMaxsize($maxsize) 0447 { 0448 $this->_maxsize = $maxsize; 0449 } 0450 0451 }