Source for file Ethna_Plugin_Validator_File.php

Documentation is available at Ethna_Plugin_Validator_File.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Ethna_Plugin_Validator_File.php
  5.  *
  6.  *  @author     ICHII Takashi <ichii386@schweetheart.jp>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id$
  10.  */
  11.  
  12. // UPLOAD_ERR_* が未定義の場合
  13. if (defined('UPLOAD_ERR_OK') == false) { // PHP 4.3.0
  14.     define('UPLOAD_ERR_OK', 0);
  15. }
  16. if (defined('UPLOAD_ERR_INI_SIZE') == false) { // PHP 4.3.0
  17.     define('UPLOAD_ERR_INI_SIZE', 1);
  18. }
  19. if (defined('UPLOAD_ERR_FORM_SIZE') == false) { // PHP 4.3.0
  20.     define('UPLOAD_ERR_FORM_SIZE', 2);
  21. }
  22. if (defined('UPLOAD_ERR_PARTIAL') == false) { // PHP 4.3.0
  23.     define('UPLOAD_ERR_PARTIAL', 3);
  24. }
  25. if (defined('UPLOAD_ERR_NO_FILE') == false) { // PHP 4.3.0
  26.     define('UPLOAD_ERR_NO_FILE', 4);
  27. }
  28. if (defined('UPLOAD_ERR_NO_TMP_DIR') == false) { // PHP 4.3.10, 5.0.3
  29.     define('UPLOAD_ERR_NO_TMP_DIR', 6);
  30. }
  31. if (defined('UPLOAD_ERR_CANT_WRITE') == false) { // PHP 5.1.0
  32.     define('UPLOAD_ERR_CANT_WRITE', 7);
  33. }
  34.  
  35. // {{{ Ethna_Plugin_Validator_File
  36. /**
  37.  *  ファイルチェックプラグイン
  38.  *
  39.  *  @author     ICHII Takashi <ichii386@schweetheart.jp>
  40.  *  @access     public
  41.  *  @package    Ethna
  42.  */
  43. {
  44.     /** @var    bool    配列を受け取るかフラグ */
  45.     var $accept_array = false;
  46.  
  47.     /**
  48.      *  アップロードされたファイルのチェックを行う
  49.      *  XXX: プラグインのエラーコードを修正する
  50.      *
  51.      *  @access public
  52.      *  @param  string  $name       フォームの名前
  53.      *  @param  mixed   $var        フォームの値
  54.      *  @param  array   $params     プラグインのパラメータ
  55.      */
  56.     function &validate($name, $var, $params)
  57.     {
  58.         $true = true;
  59.         if ($this->getFormType($name) != VAR_TYPE_FILE) {
  60.             return $true;
  61.         }
  62.  
  63.         // そもそもアップロードされていない場合はスキップ
  64.         if ($var['error'] == UPLOAD_ERR_NO_FILE) {
  65.             return $true;
  66.         }
  67.  
  68.  
  69.         // エラーコードの検査
  70.         $msg = '';
  71.         switch ($var['error']) {
  72.         case UPLOAD_ERR_INI_SIZE: 
  73.             $msg = _et("Uploaded file size exceeds php.ini's upload_max_filesize directive.");
  74.             break;
  75.         case UPLOAD_ERR_FORM_SIZE:
  76.             $msg = _et('Uploaded File size exceeds MAX_FILE_SIZE specified in HTML Form.');
  77.             break;
  78.         case UPLOAD_ERR_PARTIAL:
  79.             $msg= _et('File was only uploaded patially.');
  80.             break;
  81.         case UPLOAD_ERR_NO_FILE:
  82.             $msg = _et('File was not uploaded.');
  83.             break;
  84.         case UPLOAD_ERR_NO_TMP_DIR:
  85.             $msg = _et('Temporary folder was not found.');
  86.             break;
  87.         case UPLOAD_ERR_CANT_WRITE:
  88.             $msg= _et('Could not write uploaded file to disk.');
  89.             break;
  90.         }
  91.         if ($msg != '') {
  92.             if (isset($params['error'])) {
  93.                 $msg = $params['error'];
  94.             }
  95.             return Ethna::raiseNotice($msg, E_FORM_WRONGTYPE_FILE);
  96.         }
  97.  
  98.  
  99.         // tmp_name の検査
  100.         if (isset($var['tmp_name']) == false || is_uploaded_file($var['tmp_name']) == false) {
  101.             if (isset($params['error'])) {
  102.                 $msg = $params['error'];
  103.             } else {
  104.                 $msg = _et('invalid tmp_name.');
  105.             }
  106.             return Ethna::raiseNotice($msg, E_FORM_WRONGTYPE_FILE);
  107.         }
  108.  
  109.         // size の検査
  110.         if (isset($params['size_max'])) {
  111.             $st = stat($var['tmp_name']);
  112.             if ($st[7] > $this->_getSizeAsBytes($params['size_max'])) {
  113.                 if (isset($params['error'])) {
  114.                     $msg = $params['error'];
  115.                 } else {
  116.                     $msg = _et('Uploaded file size must be less than %s.');
  117.                 }
  118.                 return Ethna::raiseNotice($msg, E_FORM_WRONGTYPE_FILE, array($params['size_max']));
  119.             }
  120.         }
  121.         if (isset($params['size_min'])) {
  122.             $st = stat($var['tmp_name']);
  123.             if ($st[7] < $this->_getSizeAsBytes($params['size_min'])) {
  124.                 if (isset($params['error'])) {
  125.                     $msg = $params['error'];
  126.                 } else {
  127.                     $msg = _et('Uploaded file size must be more than %s.');
  128.                 }
  129.                 return Ethna::raiseNotice($msg, E_FORM_WRONGTYPE_FILE, array($params['size_min']));
  130.             }
  131.         }
  132.  
  133.  
  134.         // type の検査
  135.         if (isset($params['type'])) {
  136.             $type_list = to_array($params['type']);
  137.             $posted_mime = explode('/', $var['type'], 2);
  138.             foreach ($type_list as $type) {
  139.                 $wanted_mime = explode('/', $type, 2);
  140.                 $test = (count($wanted_mime) == 1)
  141.                         ? (strcasecmp($wanted_mime[0], $posted_mime[0]) == 0)
  142.                 : (strcasecmp($type, $var['type']) == 0);  
  143.                 if ($test == true) {
  144.                     break;
  145.                 }
  146.             }
  147.             if ($test == false) {
  148.                 if (isset($params['error'])) {
  149.                     $msg = $params['error'];
  150.                 } else {
  151.                     $msg = _et('Invalid file type.');
  152.                 }
  153.                 return Ethna::raiseNotice($msg, E_FORM_WRONGTYPE_FILE);
  154.             }
  155.         }
  156.  
  157.         // name(ファイル名)の検査
  158.         if (isset($params['name'])) {
  159.             $test = ($params['name']{0} == '/')
  160.                 ? preg_match($params['name'], $var['name'])
  161.                 : (strcmp($params['name'], $var['name']) == 0);
  162.             if ($test == false) {
  163.                 if (isset($params['error'])) {
  164.                     $msg = $params['error'];
  165.                 } else {
  166.                     $msg = _et('Invalid file name.');
  167.                 }
  168.                 return Ethna::raiseNotice($msg, E_FORM_WRONGTYPE_FILE);
  169.             }
  170.         }
  171.  
  172.         return $true;
  173.     }
  174.  
  175.  
  176.     function _getSizeAsBytes($size)
  177.     {
  178.         $unit = 1;
  179.         if (preg_match('/^([0-9]+)([mk])?(b(ytes?)?)?$/i', trim($size), $matches)) {
  180.             if (isset($matches[1])) {
  181.                 $size = $matches[1];
  182.             }
  183.             if (isset($matches[2])) {
  184.                 if (strtolower($matches[2]) === 'm') {
  185.                     $unit = 1048576;
  186.                 } else if (strtolower($matches[2]) === 'k') {
  187.                     $unit = 1024;
  188.                 }
  189.             }
  190.         }
  191.         return intval($matches[1]) * $unit;
  192.     }
  193. }
  194. // }}}
  195. ?>

Documentation generated on Fri, 11 Nov 2011 04:00:40 +0900 by phpDocumentor 1.4.3