BEdita API doc | 4-cactus
  • Class
  • Tree
  • Deprecated
  • Download

Classes

  • Addon
  • AddressbookController
  • AddressbookShell
  • AdminController
  • Alias
  • Annotation
  • ApiAuthComponent
  • ApiBaseController
  • ApiFormatterComponent
  • ApiUploadComponent
  • ApiValidatorComponent
  • AppController
  • AppError
  • AppHelper
  • Application
  • AppModel
  • Area
  • AreasController
  • Audio
  • AuthenticationsController
  • BannedIp
  • BEAppModel
  • BEAppObjectModel
  • BeAuthComponent
  • BeAuthFacebookComponent
  • BeAuthGoogleComponent
  • BeAuthTwitterComponent
  • BeCallbackManager
  • BeConfigure
  • BeCustomPropertyComponent
  • BeditaAnnotationModel
  • BeditaBaseShell
  • BeditaCollectionModel
  • BeditaContentModel
  • BeditaExportFilter
  • BeditaImportFilter
  • BeditaObjectModel
  • BeditaProductModel
  • BeditaShell
  • BeditaSimpleObjectModel
  • BeditaSimpleStreamModel
  • BeditaStreamModel
  • BeEmbedFlashHelper
  • BeEmbedHtml5Helper
  • BeEmbedMediaHelper
  • BeErrorCode
  • BeExceptionHandler
  • BEFile
  • BeFileHandlerComponent
  • BeFormHelper
  • BeFrontHelper
  • BeHashComponent
  • BeHtmlHelper
  • BeLangTextComponent
  • BeLib
  • BeMailComponent
  • BEObject
  • BeObjectCache
  • BePaginatorToolbarHelper
  • BeSchema
  • BeSecurityComponent
  • BeSystem
  • BeSystemComponent
  • BeThumb
  • BeTimeHelper
  • BeToolbarHelper
  • BeTreeComponent
  • BeTreeHelper
  • BeUploadToObjComponent
  • BeurlHelper
  • BeVimeoComponent
  • BeYoutubeComponent
  • BuildFilterBehavior
  • CacheableBehavior
  • CacheShell
  • CallbackBehavior
  • CaptchaComponent
  • Caption
  • CaptionsBehavior
  • Card
  • CardsImportFilter
  • Category
  • CleanupDataTask
  • CleanupTask
  • Comment
  • CommentsController
  • CompactResultBehavior
  • Content
  • CreateIndexFieldsBehavior
  • DataShell
  • DataTransfer
  • DateItem
  • DbadminShell
  • DeleteDependentObjectBehavior
  • DeleteObjectBehavior
  • DeployShell
  • Document
  • DocumentsController
  • DumpModel
  • EditorialContentsShell
  • EditorNote
  • Event
  • EventLog
  • EventsController
  • FilesController
  • ForeignDependenceSaveBehavior
  • FrontendController
  • FrontendShell
  • GalleriesController
  • Gallery
  • GeoTag
  • GettextShell
  • Git
  • GravatarHelper
  • Group
  • HashJob
  • History
  • HomeController
  • Image
  • ImageInfoHelper
  • JsonExportFilter
  • JsonImportFilter
  • JsonView
  • LangText
  • Link
  • MailGroup
  • MailGroupCard
  • MailJob
  • MailLog
  • MailMessage
  • MailShell
  • MailTemplate
  • Mask
  • MediaShell
  • MigrateDumpModel
  • MigrateShell
  • Migration
  • MigrationBase
  • MigrationDateItemsShell
  • Module
  • ModulesController
  • ModuleShell
  • MultimediaController
  • NewsController
  • NewsletterController
  • NewsletterShell
  • NotifyBehavior
  • ObjectEditor
  • ObjectProperty
  • ObjectRelation
  • ObjectType
  • ObjectUser
  • PagesController
  • Permission
  • PermissionModule
  • PermsHelper
  • Product
  • Property
  • PropertyOption
  • PublicationShell
  • RedisShell
  • RelationCheck
  • RelationRepair
  • RelationShell
  • RelationStats
  • RemoveDummyNameBehavior
  • ResponseHandlerComponent
  • RestClientModel
  • Revision
  • RevisionObjectBehavior
  • SearchText
  • SearchTextSaveBehavior
  • Section
  • SectionDummy
  • SessionFilterComponent
  • SessionFilterHelper
  • ShortNews
  • SmartyView
  • SoapClientModel
  • SplitterSql
  • StatisticsController
  • Stream
  • StreamLog
  • StreamsController
  • Svn
  • TagsController
  • ThemeSmartyView
  • ThumbnailHelper
  • ThumbShell
  • TransactionComponent
  • TransactionFS
  • TranslationsController
  • Tree
  • TrHelper
  • User
  • UserProperty
  • UsersController
  • Utility
  • Version
  • Video
  • VimeoHelper
  • WebmarksController
  • XmlExportFilter
  • XmlImportFilter
  • XmlJsonConverter
  • XmlView
  • YoutubeHelper

Interfaces

  • ApiAuthInterface
  • MediaProviderInterface
  • UploadableInterface

Exceptions

  • BeditaAjaxException
  • BEditaAllowURLException
  • BeditaBadRequestException
  • BeditaConflictException
  • BEditaDeleteStreamObjException
  • BeditaException
  • BEditaFileExistException
  • BeditaForbiddenException
  • BeditaFrontAccessException
  • BeditaHashException
  • BEditaInfoException
  • BeditaInternalErrorException
  • BEditaIOException
  • BeditaLengthRequiredException
  • BeditaMailException
  • BEditaMediaProviderException
  • BeditaMethodNotAllowedException
  • BEditaMIMEException
  • BeditaNotFoundException
  • BeditaNotImplementedException
  • BeditaPublicationException
  • BeditaRuntimeException
  • BEditaSaveStreamObjException
  • BeditaServiceUnavailableException
  • BeditaUnauthorizedException
  • BEditaUploadPHPException
  • BEditaURLException
   1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99  100  101  102  103  104  105  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 
<?php
/*-----8<--------------------------------------------------------------------
 *
 * BEdita - a semantic content management framework
 *
 * Copyright 2013-2015 ChannelWeb Srl, Chialab Srl
 *
 * This file is part of BEdita: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with BEdita (see LICENSE.LGPL).
 * If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
 *
 *------------------------------------------------------------------->8-----
 */

/**
 * Thumbnail utilities class
 */
class BeThumb {
    
    // supported image types, "mime type" => "img type for internal use"
    private $supportedTypes = array(
        'image/gif' => 'gif',
        'image/jpeg' => 'jpg',
        'image/pjpeg' => 'jpg',
        'image/png' => 'png',
        'image/svg+xml' => 'svg'
    );

    /**
     * Source image data
     * @var array
     */
    private $imageInfo = array();


    /**
     * Target image data (thumbnail)
     * @var array
     */
    private $imageTarget = array();
        
    
    /**
     * Link to "missing image" url img (could be different in backend o frontend)
     */
    private $imgMissingFile = null;

    /**
     * Link to "unsupported mime" url image (could be different in backend o frontend)
     */
    private $imgUnsupported = null;
    
    /**
     * Path to BEdita media root
     */
    private $mediaRoot = null;
    
    /**
     * Path to local root folder used for writing thumbs 
     * if null $this->mediaRoot is used
     */
    private $localThumbRoot = null;

    /**
     * Input params passed to image() method gor thumb creation
     */
    private $inputParams = array();

    /** 
     * List of allowed input params
     */
    public $allowedParams = array(
        'width', 'height', 'fillcolor', 'longside', 'mode', 'modeparam', 'type', 'upscale',
        'cache', 'watermark', 'quality', 'interlace',
    );

    /** 
     * List of input params to ignore
     */
    public $ignoredParams = array('presentation', 'URLonly');


    /**
     * All known mime types
     * internal use (if needed), read from config/mime.types.php
     */
    private $knownMimeTypes = array();
    
    /**
     * It says if cache dir for thumbnail has been already checked and created.
     * Its value is `null` when it hasn't evaluated yet.
     *
     * @var bool|null
     */
    private $cacheDirChecked = null;
    
    function __construct() {
        $this->imgMissingFile = Configure::read('imgMissingFile');
        if (!BACKEND_APP) {
            if (!file_exists(WWW_ROOT . $this->imgMissingFile)) {
                $this->imgMissingFile = Configure::read('beditaUrl') . $this->imgMissingFile;
            }
        }
        $this->imgUnsupported = Configure::read('imgUnsupported');
        if (!BACKEND_APP) {
            if (!file_exists(WWW_ROOT . $this->imgUnsupported)) {
                $this->imgUnsupported = Configure::read('beditaUrl') . $this->imgUnsupported;
            }
        }
        $this->mediaRoot = Configure::read('mediaRoot');
        $this->localThumbRoot = Configure::read('localThumbRoot');
    }

    public function getValidImplementations() {
        App::import ('Vendor', 'phpthumb', array ('file' => 'php_thumb' . DS . 'ThumbLib.inc.php') );
        return PhpThumbFactory::getValidImplementations();
    }

    /**
     * Reset internal arrays
     */
    private function reset() {
        $this->imageInfo = array(
            'filename' => '', // filename
            'filenameBase' => '', // filename without extension
            'ext' => '', // file extension
            'path' => '', // relative path, included file
            'filepath' => '', // absolute file path
            'cachePath' => '', // relative cache dir path
            'cacheDirectory' => '', // absolute cache dir path
            'filesize' => '',
            'w' => '',
            'h' => '',
            'orientation' => '',
            'type' => '', // 'gif', 'jpg', 'png', 'jpeg', 'svg'
            'mime_type' => '',
            'animated' => false,
        );

        $this->imageTarget = array(
            'filename' => '',
            'filepath' => '',
            'uri' => '',
            'w' => '',
            'h' => '',
            'offsetx' => 0,
            'offsety' => 0,
            'type' => '', // 'gif', 'jpg', 'png', 'jpeg', 'svg'
            'mode' => '',
            'fillcolor' => '',
            'cropmode' => '',
            'upscale' => false,
            'q' => 95,
            'interlace' => false
        );
        
        $this->inputParams = array();

        $this->mediaRoot = Configure::read('mediaRoot');
        $this->localThumbRoot = Configure::read('localThumbRoot');

        $this->cacheDirChecked = null;
    }

    /**
     * Returns image URI after resample (thumbnail version of the input image data provided)
     * Creates thumbnail image in $config['mediaRoot']/cache dir if thumb not created or outdated
     * If a cache configuration 'thumbs' is found presence check is done through cache and not on filesystem (S3 optimization)
     *
     * @param: array $data, required, source image data (may be BE media object or other source) - required "uri"
     * @param: array $params
     * extra info about $params:
     *
     *         width, height, longside, optional: [integer]
     *         if no parameters is set, use default width & height in bedita.ini.php (['image']['thumbWidth'], ['image']['thumbHeight'])
     *         if longside is set, width & height are ignored and mode is forced to resize.
     *         if only width or only height is specified, the mode and modeparam are ignored and forced to 'resize'.
     *
     *         mode, optional: [crop, croponly, resize, 'fill', 'stretch']
     *         if not specified default bedita.ini.php (['image']['thumbMode']) is used (but always overrided by the rules above).
     *         'fill' is legacy options: it will set mode always to 'resize' but modeparam to 'fill' only if longside is not set.
     *         'stretch' is legacy options: it will set mode always to 'resize' and modeparam to 'stretch'.
     *
     *         modeparam, optional: [fill, stretch, 'C', 'T', 'B', 'L', 'R', 'TL', 'TR', 'BL', 'BR']
     *         depends on mode:
     *             if resize: 'fill' or 'stretch' and if left empty do simple resize.
     *             if crop, a string describing crop zone 'C', 'T', 'B', 'L', 'R', 'TL', 'TR', 'BL', 'BR' (default: ['image']['thumbCrop'] )
     *
     *         cache, optional: [true, false]
     *         allow the caching of images, default in bedita.ini (['image']['cache'] )
     *
     *         bgcolor, optional: [string]
     *         string representing hex color, i.e. 'FFFFFF' for the background (only on mode=resize and modeparam=fill)
     *         default in bedita.ini (['image']['background'] )
     *
     *         upscale, optional: [true, false]
     *         allow or not upscale. default bedita.ini.php (['image']['thumbUpscale'])
     *
     *         type, optional: ['gif'/'png'/'jpg']
     *         force image target type (default the same as original)
     *
     *         watermark, optional: [array: 'text', 'font', 'fontSize', 'textColor', 'background', 'file', 'align', 'opacity' ]
     *         add simple watermark to image, the missing parameters are replaced with the defaults in bedita.ini (['image']['wmi']* )
     *
     * @param: boolean $skipCheck, if true don't perform thumbnail presence check and creation but returns URL immediately
     * 
     *
     * @return: string, resampled and cached image URI (using $html helper)
     *
     */
    public function image($data, $params = array(), $skipCheck = false) {
        $this->reset();

        // Setup internal `imageInfo` path and remote flags
        if (!$this->setupImagePath($data)) {
            return $this->imgMissingFile;
        }
        if (!empty($data['file_size']) && $this->isExceedingSize($data['file_size'])) {
            $this->log('Exceeding file size: ' . $data['file_size'] . 
                ' max allowed: ' . Configure::read('imgFilesizeLimit') . ' uri: ' . $data['uri'], 'warn');
            return (!$this->imageInfo['remote'] ? Configure::read('mediaUrl') : '') . $this->imageInfo['path'];
        }
        // #769 - avoid file access
        if (!$this->setupImageInfo($data)) {
            return (!empty($data['error']) && $data['error'] == 'unsupported') ? $this->imgUnsupported : $this->imgMissingFile;
        }

        // If image is of type SVG or animated GIF skip any elaboration and return original image.
        if ($data['mime_type'] === 'image/svg+xml' || $this->imageInfo['animated']) {
            return (!$this->imageInfo['remote'] ? Configure::read('mediaUrl') : '') . $this->imageInfo['path'];
        }

        // test source file available
        // #769 - avoid file access
        if (!$this->imageInfo['remote'] && empty($this->imageInfo['cache']['info'])) {
            if (!$this->checkSourceFile()) {
                return $this->imgMissingFile;
            }
        }

        // setup internal image target array (uses $this->imageInfo)
        $this->inputParams = $params;
        $this->setupImageTarget();

        // #769 - avoid file access
        $checkThumb = ($this->imageTarget['type'] != 'svg' && !$this->imageInfo['animated']);
        if ($checkThumb) {
            $cacheExists = false;
            $cacheItem = $this->imageInfo['cachePath'] . DS . $this->imageTarget['filename'];
            if (!empty($this->imageInfo['cache']['thumbs'][$cacheItem])) {
                $cacheExists = true;
            }
            if (!$cacheExists) {
                $this->imageTarget['thumbCreated'] = false;
                $createThumb = false;
                if (!file_exists($this->imageTarget['filepath'])) {
                    $createThumb = true;
                } else {
                    $this->imageTarget['thumbFileSize'] = filesize($this->imageTarget['filepath']);
                    if ($this->imageTarget['thumbFileSize'] === 0) {
                        $this->log('empty file size found for ' . $this->imageTarget['filepath'], 'warn');
                        $createThumb = true;
                        unset($this->imageTarget['thumbFileSize']);
                    }
                }

                if ($createThumb) {
                    if (!$this->resample()) {
                        return $this->imgMissingFile;
                    }
                    $this->imageTarget['thumbCreated'] = true;
                }
                $this->storeCacheThumbnail($cacheItem);
            }
        }

        // return HTML <img> tag
        return $this->imageTarget['uri'];
    }

    /**
     * Setup source image path and remote flag
     *
     * @param array $data
     * @return bool Success.
     */
    protected function setupImagePath(array &$data) {
        // Check URI.
        if (empty($data['uri'])) {
            $source = !empty($data['id']) ? ' - obj id: ' . $data['id'] : '';
            $this->triggerError("Missing image 'uri'" . $source);
            $data['error'] = 'notFund';
            return false;
        }

        // `$data['uri']` could use `/` as path separator on systems using `\` as separator.
        $this->imageInfo['remote'] = ($data['uri'][0] !== '/' && $data['uri'][0] !== DS);
        if ($this->imageInfo['remote']) {
            $uriParts = parse_url($data['uri']);
            if (!$uriParts || !in_array($uriParts['scheme'], array('http', 'https'))) {
                $this->triggerError("'{$data['uri']}' unsupported uri protocol (only http/https)");
                $data['error'] = 'notFund';
                return false;
            }
            if (empty($data['path'])) {
                $data['path'] = $uriParts['path'];
            }
        } else {
            $data['path'] = $data['uri'];
        }

        // Relative path (local files and remote URI).
        $this->imageInfo['path'] = $data['path'];
        return true;
    }

    /**
     * Setup internal imageInfo data array
     * On error $data['error'] is populated with:
     *    - 'notFund' if image is missing or unreachable
     *    - 'unsupported' if image format is not supported 
     * 
     * @param array $data
     * @return true on success, false on error
     */
    public function setupImageInfo(array &$data) {

        // thumbnail setup
        $pathParts =  pathinfo($data['path']);

        // complete file name
        $this->imageInfo['filename'] = $pathParts['basename'];
        // file name without extension
        $this->imageInfo['filenameBase'] = $pathParts['filename'];
        // file extension
        $this->imageInfo['ext'] = (!empty($pathParts['extension']))? $pathParts['extension'] : "";
        $this->imageInfo['dirname'] = $pathParts['dirname'];
        if (!$this->imageInfo["remote"]) {
            $this->imageInfo['filepath'] = $this->mediaRoot . $this->imageInfo['path'];  // absolute
            if (DS != "/") {
                $this->imageInfo['filepath'] = str_replace("/", DS, $this->imageInfo['filepath']);
            }
        } else {
            $this->imageInfo['filepath'] = $data['uri'];
        }
        // relative cachePath
        $cachePrefix = DS . "cache" . ($this->imageInfo["remote"] ? DS . "ext" : "");
        $this->imageInfo['cachePath'] = $cachePrefix . 
            BeLib::getInstance()->friendlyUrlString($this->imageInfo['path'], "\.\/");
        // absolute cache dir path
        $this->imageInfo['cacheDirectory'] = $this->mediaRoot . $this->imageInfo['cachePath'];
        if ($this->localThumbRoot) {
            $this->imageInfo['localThumbCacheDirectory'] = $this->localThumbRoot . $this->imageInfo['cachePath'];
            $this->checkLocalThumbCacheDirectory();
        }

        // #769 - avoid file access / read from cache
        $cacheData = $this->readCacheImageInfo();
        $data = array_merge($data, $cacheData);

        // check mime type
        if (empty($data['mime_type'])) {
            $data['mime_type'] = $this->mimeTypeByExtension($this->imageInfo['ext']);
        }
        if (!in_array($data['mime_type'], array_keys($this->supportedTypes))) {
            $this->triggerError("'" . $data['uri'] . "' mime type not supported: " . $data['mime_type']);
            $data['error'] = 'unsupported';
            return false;
        }
        $this->imageInfo['mime_type'] = $data['mime_type'];
        $this->imageInfo['type'] = $this->supportedTypes[$data['mime_type']];       

        // if SVG skip thumbnail and other info
        if ($data['mime_type'] === 'image/svg+xml') { 
            return true;
        }

        if (empty($data['width']) || empty($data['height']) ) {

            $imageFilePath = $this->imagePathCached();
            $imageData = @getimagesize($imageFilePath);
            if (!$imageData) {
                $this->triggerError("'" . $this->imageInfo['filepath'] . "' is not a valid image file");
                $data['error'] = 'unsupported';
                return false;
            }
            // set up the rest of image info array
            $this->imageInfo['w'] = $imageData[0];
            $this->imageInfo['h'] = $imageData[1];
            // http://www.php.net/manual/en/function.getimagesize.php -- 
            // http://www.php.net/manual/en/function.exif-imagetype.php (constants)
            $types = array(1 => 'gif', 2 => 'jpg', 3 => 'png');
            if ($imageData[2] < 1 || $imageData[2] > 3) {
                $this->triggerError('Image type not supported [' . $imageData[2] . '] ' 
                            . $this->imageInfo['filepath']);
                $data['error'] = 'unsupported';
                return false;
            }
            $this->imageInfo['type'] = $types[$imageData[2]];
            $this->imageInfo['mime_type'] = $imageData['mime'];
                
        } else {

            $this->imageInfo['w'] = $data['width'];
            $this->imageInfo['h'] = $data['height'];
        }

        // check if it's an animated gif
        if (!isset($data['animated'])) {
            $this->imageInfo['animated'] = $this->isAnimatedGif();
        } else {
            $this->imageInfo['animated'] = $data['animated'];
        }

        if (empty($cacheData)) {
            $this->storeCacheImageInfo();
        }

        return true;
    }

    /**
     * Get image file path, get cached local copy behind a proxy 
     */
    public function imagePathCached($imageFilePath = null) {
        if (!empty($imageFilePath)) {
            $data['uri'] = str_replace($this->mediaRoot, '', $imageFilePath);
            $this->setupImagePath($data);
            $this->setupImageInfo($data);
        }
        if ($this->imageInfo["remote"] && Configure::read("proxyOptions") != null) {
            $imageFilePath = $this->remoteImageCachePathProxy();
        } else {
            $imageFilePath = $this->imageInfo["filepath"];
        }
        return $imageFilePath;
    }
    
    
    /**************************************************************************
    ** private methods follow
    **************************************************************************/

    /**
     * Get internal img info cache file name
     */
    private function cacheImageInfoFilePath() {
        return $this->imageInfo['cacheDirectory'] . DS . ".imginfo";
    }
    
    /**
     * Read cached image (i.e. if under proxy)
     */
    private function remoteImageCachePathProxy() {
        $path =  $this->imageInfo['cacheDirectory'] . DS . ".source";
        if (!file_exists($path)) {
            if (!mkdir($path, 0777, true)) {
                $this->triggerError("Error creating cache directory: " . $path);
                return false;
            }
        }
        $path =  $path . DS . $this->imageInfo['filename'];
        if (!file_exists($path)) {
            $cont = file_get_contents($this->imageInfo['filepath'], false);
            if($cont) {
                file_put_contents($path, $cont);
            }
        }
        return $path;
    }

    /**
     * Read cached img info, if present 
    * @return array
    */
    private function readCacheImageInfo() {
        $res = array();
        $cacheThumbs = Cache::settings('thumbs');
        if (!empty($cacheThumbs)) {
            $path = ($this->imageInfo['remote'] ? DS . 'ext' : '') . $this->imageInfo['path'];
            $this->imageInfo['cache'] = Cache::read($path, 'thumbs');
            if (!empty($this->imageInfo['cache']['info'])) {
                $res = $this->imageInfo['cache']['info'];
            }
        } else {
            $imgInfoFile = $this->cacheImageInfoFilePath();
            // #769 - avoid file access
            if(file_exists($imgInfoFile)) {
                $cacheData = file_get_contents($imgInfoFile);
                $res = unserialize($cacheData);
            }
        }
        return $res;
    }

    /**
     * Store img info in cache (file or engine(
     */
    private function storeCacheImageInfo() {
        $imgData = array(
            'width' => $this->imageInfo['w'],
            'height' => $this->imageInfo['h'],
            'type' => $this->imageInfo['type'],
            'mime_type' => $this->imageInfo['mime_type'],
            'animated' => $this->imageInfo['animated']
        );
        $cacheThumbs = Cache::settings('thumbs');
        if (!empty($cacheThumbs)) {
            $path = ($this->imageInfo['remote'] ? DS . 'ext' : '') . $this->imageInfo['path'];
            $this->imageInfo['cache'] = Cache::read($path, 'thumbs');
            if (empty($this->imageInfo['cache']['info'])) {
                $imgData['modified'] = 0;
                if (!$this->imageInfo['remote']) {
                    $imgData['modified'] = filemtime($this->imageInfo['filepath']);
                }
                $this->imageInfo['cache']['info'] = $imgData;
                Cache::write($path, $this->imageInfo['cache'], 'thumbs');
            }
        } else {
            $imgInfoFile = $this->cacheImageInfoFilePath();
            if ($this->checkCacheDirectory() && !file_exists($imgInfoFile)) {
                file_put_contents($imgInfoFile, serialize($imgData));
            }
        }
    }

    /**
     * Store cache thumbnail item
     */
    private function storeCacheThumbnail($cacheItem) {
        if (!empty($this->imageInfo['cache'])) {
            $thumbFileSize = isset($this->imageTarget['thumbFileSize']) ? 
                 $this->imageTarget['thumbFileSize'] : filesize($this->imageTarget['filepath']);
            $targetFileSize = isset($this->imageTarget['filesize']) ? 
                ' - local size: ' . $this->imageTarget['filesize'] : '';
            $thumbCreated = !empty($this->imageTarget['thumbCreated']) ? ' - thumb created ' : '';
            if ($thumbFileSize === 0) {
                $this->triggerError(
                    'empty file size for thumbnail: '
                    . $this->imageTarget['filepath']
                    . $thumbCreated
                    . $targetFileSize
                    . ' input params: '
                    . print_r($this->inputParams, true)
                );
            }
            $this->imageInfo['cache']['thumbs'][$cacheItem] = array('size' => $thumbFileSize);
            $path = ($this->imageInfo['remote'] ? DS . 'ext' : '') . $this->imageInfo['path'];
            Cache::write($path, $this->imageInfo['cache'], 'thumbs');
        }
    }

    /**
     * Get thumbnail size in bytes reading from cache or filsystem
     * Input thumbnail path should be relative to media root (not includin media root)
     * May use cached data
     * @param unknown $thumbPath
     */
    public function thumbnailSize($thumbPath) {
        $size = 0;
        if (!empty($this->imageInfo['cache']) && !empty($this->imageInfo['cache']['thumbs'][$thumbPath])) {
            $size = $this->imageInfo['cache']['thumbs'][$thumbPath]['size'];
        } else {
            $s = @filesize($this->mediaRoot . $thumbPath); // avoid generate warning
            if ($s !== false) {
                $size = $s;
            }
        }
        return $size;
    }

    /**
     * Setup internal imageTarget data array, used in thumbnails creation
     * 
     */
    private function setupImageTarget() {

        // check input params
        $inputKeys = array_keys($this->inputParams);
        $paramsDiff = array_diff($inputKeys, $this->allowedParams);
        $paramsDiff = array_diff($paramsDiff, $this->ignoredParams);
        if (!empty($paramsDiff)) {
            $this->log('input params not supported: ' . print_r($paramsDiff, true), 'warn');
        }
        extract($this->inputParams);

        $imageConf = Configure::read('media.image');
        
        if (isset($watermark))  {
            $this->imageTarget['watermark'] = array_merge($imageConf['watermark'], $watermark);
        }
        
        // upscale
        if (isset($upscale))    {
            $this->imageTarget['upscale'] = $upscale;
        } else {
            $this->imageTarget['upscale'] = $imageConf['thumbUpscale'];
        }
        
        //background
        if ( isset ($bgcolor) ) {
            $this->imageTarget['fillcolor'] = $bgcolor;
        } else {
            $this->imageTarget['fillcolor'] = $imageConf['background'];
        }

        // Quality.
        if (isset($quality)) {
            $this->imageTarget['q'] = $quality;
        }

        // Interlace.
        if (isset($interlace)) {
            $this->imageTarget['interlace'] = $interlace;
        }

        // cropmode
        $this->imageTarget['cropmode'] = $imageConf['thumbCrop'];
        
        // default mode, if not specified
        if ( !isset ($mode) ) {
            $mode = $imageConf['thumbMode'];
        }
        
        // target image type
        if ( !@empty($type) ) {
            $this->imageTarget['type'] = $type;
        } else {
            $this->imageTarget['type'] = $this->imageInfo['type'];
        }
        
        // Target image size: imageTarget['w'] & imageTarget['h']
        // [unused $this->_targetSize ($width, $height)]
        if ( empty($width) && empty($height) && !isset($longside) ) { //no parameter set, use default
            $width  = $imageConf['thumbWidth'];
            $height = $imageConf['thumbHeight'];
        
        } else if( !empty($longside) && is_numeric($longside) ) { // if is set longside, ignore the others parameters
            if ($this->imageInfo["w"] > $this->imageInfo["h"]) {
                $width  = $longside;
                $height = 0;
            }else {
                $width  = 0;
                $height = $longside;
            }
            //forcing the mode to "resize" if longside
            $mode = "resize";
            $modeparam = null;
        } else if(empty($width) || empty($height) ) { // case with only one parameter w/h
        
            if ( empty($width) ){
                $width  = 0;
            }
            if ( empty($height) ){
                $height = 0;
            }
            //forcing the mode to "resize" if one coordinate is empty
            $mode = "resize";
            $modeparam = null;
        }
        
        $this->imageTarget['w'] = $width;
        $this->imageTarget['h'] = $height;
        
        //Set the embed mode
        switch ($mode) {
            case "croponly":  //general crop
                $this->imageTarget['mode'] = 0;
                if ( isset ($modeparam) ) {
                    $this->imageTarget['cropmode'] = $modeparam; // overwrite crop mode
                }
                break;
        
            case "crop": //adaptive crop
                $this->imageTarget['mode'] = 1;
                //if ( isset ($modeparam) ) $this->imageTarget['cropmode'] = $modeparam; // overwrite crop mode
                break;
        
            case "resize":
            default:
                $this->imageTarget['mode'] = 2;
                if ( isset ($modeparam) ) {
                    $this->imageTarget['resizetype'] = $modeparam;
                }
        
                break;
        
                // legacy methods
            case "fill":
                $this->imageTarget['mode'] = 2;
        
                if (empty($longside)) {
                    $this->imageTarget['resizetype'] = 'fill';
                    if ( isset ($modeparam) )   {
                        $this->imageTarget['fillcolor'] = $modeparam;
                    }else {
                        $this->imageTarget['fillcolor'] = $imageConf['thumbFill'];
                    }
                }
                break;
        
            case "stretch":
                $this->imageTarget['mode'] = 2;
                $this->imageTarget['resizetype'] = 'stretch';
                break;
        }
        
        // target filename, filepath, uri
        $this->imageTarget['filename'] = $this->targetFileName();
        $this->imageTarget['filepath'] =  $this->imageInfo['cacheDirectory'] . DS . $this->imageTarget['filename'];
        if (!empty($this->imageInfo['localThumbCacheDirectory'])) {
            $this->imageTarget['localThumbFilepath'] = $this->imageInfo['localThumbCacheDirectory'] . DS . $this->imageTarget['filename'];
        }
        $this->imageTarget['uri'] = Configure::read('mediaUrl') . 
                $this->imageInfo['cachePath'] . "/" . $this->imageTarget['filename'];
    }
    
    /**
     * Resample image using PhpThumb
     *
     * @return boolean
     */
    private function resample() {
        if (!$this->checkCacheDirectory()) {
            return false;
        }

        $imageFilePath = $this->imageInfo['filepath'];
        if($this->imageInfo["remote"] && Configure::read("proxyOptions") != null) {
            $imageFilePath = $this->remoteImageCachePathProxy();
        }
        App::import ('Vendor', 'phpthumb', array ('file' => 'php_thumb' . DS . 'ThumbLib.inc.php') );
        
        try {
            
            $targetThumbPath = $this->imageTarget['filepath'];
            if (!empty($this->imageTarget['localThumbFilepath'])) {
                $targetThumbPath = $this->imageTarget['localThumbFilepath'];
            } 
 
            $thumbnail = PhpThumbFactory::create($imageFilePath, Configure::read('media.image'));
            $thumbnail->setDestination($targetThumbPath, $this->imageTarget['type']);

            if (array_key_exists('q', $this->imageTarget)) {
                // Set quality.
                $thumbnail->setOptions(array('jpegQuality' => $this->imageTarget['q']));
            }

            //set upscale
            if ($this->imageTarget['upscale']) {
                $thumbnail->setOptions(array("resizeUp" => true));
            }

            // more params about resample mode
            switch ( $this->imageTarget['mode'] ) {
                // croponly
                case 0:
                    list ($starX, $startY)  = $this->getCropCoordinates($this->imageInfo['w'], 
                            $this->imageInfo['h'], $this->imageTarget['w'], $this->imageTarget['h'], 
                            $this->imageTarget['cropmode'] );
                    $thumbnail->crop($starX, $startY, $this->imageTarget['w'], $this->imageTarget['h']);
    
                    break;
                //crop: adaptive crop
                case 1:
                default:
                    $thumbnail->adaptiveResize($this->imageTarget['w'], $this->imageTarget['h']);
                    break;
    
                // resize
                case 2:
    
                    //stretch or fill of simple resize
                    if (empty($this->imageTarget['resizetype'])){
    
                        $thumbnail->resize($this->imageTarget['w'], $this->imageTarget['h']);
    
                    }else if ($this->imageTarget['resizetype'] == 'stretch') {
    
                        $thumbnail->resizeStretch($this->imageTarget['w'], $this->imageTarget['h']);
    
                    } else if ($this->imageTarget['resizetype'] == 'fill') {
    
                        $thumbnail->resizeFill($this->imageTarget['w'], $this->imageTarget['h'],  
                                $this->imageTarget['fillcolor']);
                    }
                    break;
    
            }

            // add watermark
            if (isset($this->imageTarget['watermark'])) {
                $thumbnail->wmark($targetThumbPath, $this->imageTarget['watermark']);
            }

            // Interlace image.
            if (!empty($this->imageTarget['interlace'])) {
                $thumbnail->interlace(true);
            }

            $thumbnail->save($targetThumbPath, $this->imageTarget['type']);

            if (!empty($this->imageTarget['localThumbFilepath'])) {
                if (!copy($targetThumbPath, $this->imageTarget['filepath'])) {
                    $this->triggerError('Error copying local thumbnail ' . $targetThumbPath . ' to ' . $this->imageTarget['filepath']);

                    return false;
                }

                $this->imageTarget['filesize'] = filesize($targetThumbPath);
                if (!unlink($targetThumbPath)) {
                    $this->triggerError('Error removing local thumbnail ' . $targetThumbPath);

                    return false;
                }
            }
        } catch (Exception $e) {
            $this->triggerError(sprintf('Error saving thumbnail: %s with message %s', $targetThumbPath, $e->getMessage()));

            return false;
        }
        
        return true;
    }

    /**
     * Check source file existence/correctness
     *
     * @return boolean
     */
    private function checkSourceFile() {
        // #769 - avoid file access
        if(!file_exists($this->imageInfo['filepath']) ) {
            $this->triggerError("file '" . $this->imageInfo['filepath'] . "' does not exist");
            return false;
        } else if(!is_readable ($this->imageInfo['filepath'])) {
            // cannot access source file on filesystem
            $this->triggerError("cannot read file '" . $this->imageInfo['filepath'] . "' on filesystem");
            return false;
        }
        return true;
    }

    /**
     * Build target filename
     *
     * @return string
     */
    private function targetFileName() {
        // build hash on file path, modification time and mode
        if ($this->imageInfo['remote']) {
            $modified = 0;
        } else {
            if (!empty($this->imageInfo['cache']['info']['modified'])) {
                $modified = $this->imageInfo['cache']['info']['modified'];
            } else {
                $modified = filemtime($this->imageInfo['filepath']);
            }
        }

        $wmString = '';
        $wm = null;
        if (isset($this->imageTarget['watermark'])) {
            $wm = $this->imageTarget['watermark'];
            $wmString = implode($wm);
            unset($this->imageTarget['watermark']);
        }
        $targetStr = implode($this->imageTarget) . $wmString;
        if (!empty($wmString)) {
            $this->imageTarget['watermark'] = $wm;
        }

        $this->imageInfo['hash'] = md5($this->imageInfo['filename'] . $modified . $targetStr);

        // target filename = orig_filename + "_" + w + "x" + h + "_" + hash + "." + ext
        $target = $this->imageInfo['filenameBase'] . '_' . $this->imageTarget['w'] 
            . 'x' . $this->imageTarget['h'] . '_' . $this->imageInfo['hash'] . '.' . $this->imageTarget['type'];
        return BeLib::getInstance()->friendlyUrlString($target, "\.");
    }

    /**
     * Check target cache directory existence and create it if missing
     *
     * @return boolean
     */
    private function checkCacheDirectory() {
        if ($this->cacheDirChecked !== null) {
            return $this->cacheDirChecked;
        }

        if (file_exists($this->imageInfo['cacheDirectory'])) {
            if (!is_dir($this->imageInfo['cacheDirectory'])) {
                $this->triggerError($this->imageInfo['cacheDirectory'] . ' is not a directory');
            
                return $this->cacheDirChecked = false;
            }
            
            return $this->cacheDirChecked = true;
        }

        if (!mkdir($this->imageInfo['cacheDirectory'], 0777, true)) {
            $this->triggerError("Error creating cache directory: " . $this->imageInfo['cacheDirectory']);
            
            return $this->cacheDirChecked = false;
        }

        return $this->cacheDirChecked = true;
    }

    /**
     * Check local thumb target cache directory existence and create it if missing
     *
     * @return string
     */
    private function checkLocalThumbCacheDirectory() {
        if (!empty($this->imageInfo['localThumbCacheDirectory'])) {
            if (!file_exists($this->imageInfo['localThumbCacheDirectory'])) {
                if (!mkdir($this->imageInfo['localThumbCacheDirectory'], 0777, true)) {
                    $this->triggerError("Error creating thumb cache directory: " . $this->imageInfo['localThumbCacheDirectory']);
                    return false;
                }
            } elseif (!is_dir($this->imageInfo['localThumbCacheDirectory'])) {
                $this->triggerError("Not a direcotory: " . $this->imageInfo['localThumbCacheDirectory']);
                return false;
            }
        }
        return true;
    }


    /**
     * calculate cropping coordinates (top, left)
     *
     * @param int $origW, original width
     * @param int $origH, original height
     * @param int $targetW, target object width
     * @param int $targetH, target object height
     * @param string $position: TL (top left), T (top), TR (top right), L (left), R (right), BL (bottom left), B (bottom), BR (bottom right), C (center), default center
     * @return array
     */
    private function getCropCoordinates ( $origW, $origH, $targetW, $targetH, $position ) {
        $coordinates = array ();

        switch ($position) {
            case "TL":
                $coordinates['x']  = 0;
                $coordinates['y']  = 0;
                break;

            case "T":
                $coordinates['x']  = ($origW / 2) - ($targetW / 2);
                $coordinates['y']  = 0;
                break;

            case "TR":
                $coordinates['x']  = $origW - $targetW;
                $coordinates['y']  = 0;
                break;

            case "L":
                $coordinates['x']  = 0;
                $coordinates['y']  = ($origH / 2) - ($targetH / 2);
                break;

            case "R":
                $coordinates['x']  = $origW - $targetW;
                $coordinates['y']  = ($origH / 2) - ($targetH / 2);
                break;

            case "BL":
                $coordinates['x']  = 0;
                $coordinates['y']  = $origH - $targetH;
                break;

            case "B":
                $coordinates['x']  = ($origW / 2) - ($targetW / 2);
                $coordinates['y']  = $origH - $targetH;
                break;

            case "BR":
                $coordinates['x']  = $origW - $targetW;
                $coordinates['y']  = $origH - $targetH;
                break;

            case "C":
            case 1:
            default:
                $coordinates['x']  = ($origW / 2) - ($targetW / 2);
                $coordinates['y']  = ($origH / 2) - ($targetH / 2);
                break;
        }

        return array ($coordinates['x'], $coordinates['y']);
    }

    /**
     * Log/report error
     *
     * @param string $errorMsg
     */
    private function triggerError($errorMsg) {
        if (!class_exists('CakeLog')) {
            return;
        }
        CakeLog::write('error', get_class($this) . ": " . $errorMsg);
    }

    /**
     * Log/report error
     *
     * @param string $errorMsg
     */
    private function log($msg, $type) {
        if (!class_exists('CakeLog')) {
            return;
        }
        CakeLog::write($type, get_class($this) . ": " . $msg);
    }

    /**
     * Check whether current image is an animated GIF.
     *
     * @return bool
     * @see http://php.net/manual/en/function.imagecreatefromgif.php#59787
     */
    private function isAnimatedGif() {
        if ($this->imageInfo['type'] != 'gif') {
            return false;
        }

        $data = file_get_contents($this->imageInfo['filepath']);

        $pointer = 0;
        $frames = 0;
        while ($frames < 2) {
            $pos1 = strpos($data, "\x00\x21\xF9\x04", $pointer);
            if ($pos1 === FALSE) {
                break;
            } else {
                $pointer = $pos1 + 1;
                $pos2 = min(strpos($data, "\x00\x2C", $pointer), strpos($data, "\x00\x21", $pointer));
                if ($pos2 === FALSE) {
                    break;
                } else {
                    if ($pos1 + 8 == $pos2) {
                        $frames++;
                    }
                    $pointer = $pos2 + 1;
                }
            }
        }

        return $frames >= 2;
    }

    /**
     * Check whether file size is exceeding size limits for image thumbnail generation.
     *
     * @param int $size File size.
     * @return bool
     */
    protected function isExceedingSize($size) {
        $maxSize = Configure::read('imgFilesizeLimit');
        return (!empty($maxSize) && $size > $maxSize);
    }

    /***************************/
    /* minor private functions */
    /***************************/

    /**
     * Mime type by file extension
     *
     * @param $ext, extension
     * @return string or false if mime_type not found
     */
    private function mimeTypeByExtension($ext) {
        $mime_type = false;
        if (empty($this->knownMimeTypes)) {
            include(BEDITA_CORE_PATH.DS.'config'.DS.'mime.types.php');
            $this->knownMimeTypes = $config["mimeTypes"];
        }
        $ext = strtolower($ext);
        if (!empty($ext) && array_key_exists($ext, $this->knownMimeTypes)) {
                $mime_type = $this->knownMimeTypes[$ext];
        }
        return $mime_type;
    }
}
BEdita API doc | 4-cactus API documentation generated by ApiGen