HomeHelpTrac

Changeset 15453


Ignore:
Timestamp:
08/15/11 01:43:59 (10 months ago)
Author:
Alexander Trofimov
Message:

Storage Engine (allow deny by file extension)

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/inc/classes/BxDolStorage.php

    r15452 r15453  
    3636define('BX_DOL_STORAGE_INVALID_FILE', 1002); ///< uploaded file is invalid or hack attempts 
    3737define('BX_DOL_STORAGE_ERR_FILE_TOO_BIG', 1003); ///< file is too big 
    38 define('BX_DOL_STORAGE_ERR_USER_QUOTA_EXCEEDED', 1004); ///< user quota exceeded 
    39 define('BX_DOL_STORAGE_ERR_OBJECT_QUOTA_EXCEEDED', 1005); ///< storage object quota exceeded 
    40 define('BX_DOL_STORAGE_ERR_SITE_QUOTA_EXCEEDED', 1006); ///< site quota exceeded 
    41 define('BX_DOL_STORAGE_ERR_ENGINE_ADD', 1007); ///< some other error during file adding occured, related to particular storage engine 
     38define('BX_DOL_STORAGE_ERR_WRONG_EXT', 1004); ///< wrong file extension 
     39define('BX_DOL_STORAGE_ERR_USER_QUOTA_EXCEEDED', 1005); ///< user quota exceeded 
     40define('BX_DOL_STORAGE_ERR_OBJECT_QUOTA_EXCEEDED', 1006); ///< storage object quota exceeded 
     41define('BX_DOL_STORAGE_ERR_SITE_QUOTA_EXCEEDED', 1007); ///< site quota exceeded 
     42define('BX_DOL_STORAGE_ERR_ENGINE_ADD', 1008); ///< some other error during file adding occured, related to particular storage engine 
    4243 
    4344define('BX_DOL_STORAGE_ERR_FILE_NOT_FOUND', 2001); ///< file not found 
     
    277278 
    278279    // ------------ internal functions 
     280 
     281    protected function storeUploadedFileBegin($aFile, $isPrivate = false, $iProfileId = 0) {  
     282 
     283        if (!$aFile['size'] || !$aFile['tmp_name']) { 
     284            $this->setErrorCode(BX_DOL_STORAGE_ERR_NO_FILE); 
     285            return false; 
     286        } 
     287 
     288        if (UPLOAD_ERR_OK != $aFile['error']) { 
     289            $this->setErrorCode((int)$aFile['error']); 
     290            return false; 
     291        } 
     292 
     293        $sExt = $this->getFileExt($aFile['name']); 
     294        $sMimeType = $this->getMimeTypeByFileName($aFile['name']); 
     295 
     296        if (!$this->isValidExt($sExt)) { 
     297            $this->setErrorCode(BX_DOL_STORAGE_ERR_WRONG_EXT); 
     298            return false; 
     299        } 
     300 
     301        if (!$this->onBeforeFileAdd (array( 
     302            'profile_id' => $iProfileId, 
     303            'file_name' => $aFile['name'], 
     304            'mime_type' => $sMimeType, 
     305            'ext' => $sExt, 
     306            'size' => $aFile['size'], 
     307            'private' => $isPrivate ? 1 : 0, 
     308        ))) { 
     309            return false; 
     310        } 
     311 
     312        return true; 
     313    } 
    279314 
    280315    protected function setErrorCode($i) { 
     
    313348        return $sMimeType; 
    314349    } 
     350 
     351    protected function isValidExt ($sExt) { 
     352        switch ($this->_aObject['ext_mode']) { 
     353            case 'allow-deny': 
     354                if ($this->isAllowedExt($sExt)) 
     355                    return true; 
     356                return false; 
     357            case 'deny-allow': 
     358                if ($this->isDeniedExt($sExt)) 
     359                    return false; 
     360                return true; 
     361            default: 
     362                return false; 
     363        } 
     364    } 
     365 
     366    protected function isAllowedExt ($sExt) { 
     367        return $this->isAllowedDeniedExt($sExt, 'ext_allow'); 
     368    } 
     369 
     370    protected function isDeniedExt ($sExt) { 
     371        return $this->isAllowedDeniedExt($sExt, 'ext_deny'); 
     372    } 
     373 
     374    protected function isAllowedDeniedExt ($sExt, $sExtMode) { 
     375        if ('' == $this->_aObject[$sExtMode]) 
     376            return false; 
     377        if (!is_array($this->_aObject[$sExtMode]))     
     378            $this->_aObject[$sExtMode] = explode(',', $this->_aObject[$sExtMode]); 
     379        return in_array ($sExt, $this->_aObject[$sExtMode]); 
     380    } 
     381 
    315382} 
    316383 
  • trunk/inc/classes/BxDolStorageLocal.php

    r15452 r15453  
    3535    public function storeUploadedFile($aFile, $isPrivate = false, $iProfileId = 0) {  
    3636 
    37         if (!$aFile['size'] || !$aFile['tmp_name']) { 
    38             $this->setErrorCode(BX_DOL_STORAGE_ERR_NO_FILE); 
    39             return false; 
    40         } 
    41  
    42         if (UPLOAD_ERR_OK != $aFile['error']) { 
    43             $this->setErrorCode((int)$aFile['error']); 
    44             return false; 
    45         } 
     37        if (!$this->storeUploadedFileBegin($aFile, $isPrivate, $iProfileId)) 
     38            return false; 
    4639 
    4740        $sExt = $this->getFileExt($aFile['name']); 
    4841        $sMimeType = $this->getMimeTypeByFileName($aFile['name']); 
    49  
    50         if (!$this->onBeforeFileAdd (array( 
    51             'profile_id' => $iProfileId, 
    52             'file_name' => $aFile['name'], 
    53             'mime_type' => $sMimeType, 
    54             'ext' => $sExt, 
    55             'size' => $aFile['size'], 
    56             'private' => $isPrivate ? 1 : 0, 
    57         ))) { 
    58             return false; 
    59         } 
    6042 
    6143        $sLocalId = $this->genRandName(); 
     
    131113            return false; 
    132114        } 
    133   
    134         if (!is_writable($sFileLocation)) { 
    135             $this->setErrorCode(BX_DOL_STORAGE_ERR_FILESYSTEM_PERM); 
    136             return false; 
    137         } 
    138115 
    139116        if (!$this->onBeforeFileDelete ($aFile, $iProfileId)) { 
  • trunk/inc/classes/BxDolStorageS3.php

    r15452 r15453  
    5555    public function storeUploadedFile($aFile, $isPrivate = false, $iProfileId = 0) {  
    5656 
    57         if (!$aFile['size'] || !$aFile['tmp_name']) { 
    58             $this->setErrorCode(BX_DOL_STORAGE_ERR_NO_FILE); 
     57        if (!$this->storeUploadedFileBegin($aFile, $isPrivate, $iProfileId)) 
    5958            return false; 
    60         } 
    61  
    62         if (UPLOAD_ERR_OK != $aFile['error']) { 
    63             $this->setErrorCode((int)$aFile['error']); 
    64             return false; 
    65         } 
    6659 
    6760        $sExt = $this->getFileExt($aFile['name']); 
    68         $sMimeType = $this->getMimeTypeByFileName($aFile['name']);         
    69  
    70         if (!$this->onBeforeFileAdd (array( 
    71             'profile_id' => $iProfileId, 
    72             'file_name' => $aFile['name'], 
    73             'mime_type' => $sMimeType, 
    74             'ext' => $sExt, 
    75             'size' => $aFile['size'], 
    76             'private' => $isPrivate ? 1 : 0, 
    77         ))) { 
    78             return false; 
    79         } 
     61        $sMimeType = $this->getMimeTypeByFileName($aFile['name']); 
    8062         
    8163        $sLocalId = $this->genRandName(); 
     
    133115        ))) { 
    134116            $this->_oDb->deleteFile($iId); 
    135             unlink($sNewFilePath); 
     117            $this->_s3->deleteObject($this->_sBucket, $this->getObjectBaseDir($isPrivate) . $sRemoteNamePath); 
    136118            return false; 
    137119        } 
  • trunk/inc/images.inc.php

    r15339 r15453  
    5252function produceSecurityImage( $text, $hash ) 
    5353{ 
    54     $use_gd = false; 
     54    global $use_gd; 
    5555    global $gdInstalled; 
    5656 
     57    bx_import('BxDolConfig'); 
    5758    $sTmpPath = BxDolConfig::getInstance()->get('path_dynamic', 'tmp'); 
    5859 
  • trunk/install/sql/v70.sql

    r15452 r15453  
    35383538  `levels` tinyint(4) NOT NULL, 
    35393539  `table_files` varchar(64) NOT NULL, 
     3540  `ext_mode` enum('allow-deny','deny-allow') NOT NULL, 
     3541  `ext_allow` text NOT NULL, 
     3542  `ext_deny` text NOT NULL, 
    35403543  `quota_size` int(11) NOT NULL, 
    35413544  `current_size` int(11) NOT NULL, 
     
    35453548  `ts` int(11) NOT NULL, 
    35463549  PRIMARY KEY (`id`) 
    3547 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 
     3550) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
    35483551 
    35493552CREATE TABLE IF NOT EXISTS `sys_storage_user_quotas` ( 
Note: See TracChangeset for help on using the changeset viewer.