Warning, file /webapps/ocs-webserver/application/modules/default/views/helpers/Image.php was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
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 Default_View_Helper_Image extends Zend_View_Helper_Abstract
0024 {
0025 
0026     protected $_operations = array(
0027         'crop'        => '%d',
0028         'width'       => '%d',
0029         'height'      => '%d',
0030         'quality'     => '%d',
0031         'bgColor'     => '%s',
0032         'progressive' => '%d'
0033     );
0034 
0035     protected $_options = array(
0036         'temporal' => false
0037     );
0038 
0039     protected $_separator = '-';
0040 
0041     /**
0042      * @param       $filename
0043      * @param array $options
0044      * @return string|null
0045      */
0046     public function Image($filename, $options = array())
0047     {
0048         if (false === $this->validUri($filename)) {
0049 
0050             return $this->createImageUri($filename, $options);
0051         }
0052 
0053         $uri = $filename;
0054         if (false === $this->isLocalhost($filename)) {
0055             $httpScheme = 'https';
0056             $uri = $this->replaceScheme($filename, $httpScheme);
0057         }
0058 
0059         if (empty($options)) {
0060 
0061             return $uri;
0062         }
0063 
0064         return $this->updateImageUri($uri, $options);
0065     }
0066 
0067     /**
0068      * @param $filename
0069      * @return bool
0070      */
0071     private function validUri($filename)
0072     {
0073         return Zend_Uri::check($filename);
0074     }
0075 
0076     /**
0077      * @param $filename
0078      * @param $options
0079      * @return string
0080      */
0081     private function createImageUri($filename, $options)
0082     {
0083         $operations = "";
0084 
0085         if (isset($options['width']) && isset($options['height'])) {
0086             $operations .= $options['width'] . 'x' . $options['height'];
0087         } else {
0088             //$operations .= '80x80';
0089             $operations .= '';
0090         }
0091         if (isset($options['crop'])) {
0092             $operations .= '-' . $options['crop'];
0093         } else {
0094             //$operations .= '-2';
0095             $operations .= '';
0096         }
0097 
0098         if ($filename == "") {
0099             $filename = 'default.png';
0100         }
0101 
0102         if (isset($options['temporal'])) {
0103             $filename = '/img/default/tmp/' . $filename;
0104             $url = $filename;
0105         } else {
0106             if (strpos($filename, '.gif') > 0 || $operations == '') {
0107                 $url = IMAGES_MEDIA_SERVER . '/img/' . $filename;
0108             } else {
0109                 $url = IMAGES_MEDIA_SERVER . '/cache/' . $operations . '/img/' . $filename;
0110             }
0111         }
0112 
0113         return $url;
0114     }
0115 
0116     /**
0117      * @param $filename
0118      * @param $getScheme
0119      * @return string|string[]|null
0120      */
0121     private function replaceScheme($filename, $getScheme)
0122     {
0123         $result = preg_replace("|^https?|", $getScheme, $filename);
0124 
0125         return $result;
0126     }
0127 
0128     /**
0129      * @param $filename
0130      * @param $options
0131      * @return string|string[]|null
0132      */
0133     private function updateImageUri($filename, $options)
0134     {
0135         $dimension = '';
0136         if (isset($options['width']) && isset($options['height'])) {
0137             $dimension = $options['width'] . 'x' . $options['height'];
0138         } else {
0139             if (isset($options['width']) && (false === isset($options['height']))) {
0140                 $dimension = $options['width'] . 'x' . $options['width'];
0141             } else {
0142                 if (isset($options['height']) && (false === isset($options['width']))) {
0143                     $dimension = $options['height'] . 'x' . $options['height'];
0144                 }
0145             }
0146         }
0147         $uri = preg_replace("/\d\d\dx\d\d\d/", $dimension, $filename);
0148 
0149         return $uri;
0150     }
0151 
0152     /**
0153      * @param string $filename
0154      * @return bool
0155      */
0156     private function isLocalhost($filename)
0157     {
0158         $host = parse_url($filename, PHP_URL_HOST);
0159 
0160         $whitelist = array('127.0.0.1', '::1', 'localhost');
0161 
0162         if (in_array($host, $whitelist)) {
0163             return true;
0164         }
0165 
0166         return false;
0167     }
0168 
0169     public function getDataURI($image, $mime = '') {
0170         return 'data: '.(function_exists('mime_content_type') ? mime_content_type($image) : $mime).';base64,'.base64_encode(file_get_contents($image));
0171     }
0172 
0173     public function getImageDataFromUrl($filename, $options)
0174     {
0175         $url = $this->Image($filename, $options);
0176         $urlParts = pathinfo($url);
0177         $extension = $urlParts['extension'];
0178         $ch = curl_init();
0179         curl_setopt($ch, CURLOPT_URL, $url);
0180         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0181         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
0182         curl_setopt($ch, CURLOPT_HEADER, 0);
0183         $response = curl_exec($ch);
0184         curl_close($ch);
0185         $base64 = 'data:image/' . $extension . ';base64,' . base64_encode($response);
0186 
0187         return $base64;
0188     }
0189 
0190 }