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: Ethna_Plugin_Validator_File.php 460 2007-04-11 13:12:30Z cocoitiban $
  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 'アップロードされたファイルは、php.ini の upload_max_filesize ディレクティブの値を超えています。';
  74.             break;
  75.         case UPLOAD_ERR_FORM_SIZE:
  76.             $msg 'アップロードされたファイルは、HTML フォームで指定された MAX_FILE_SIZE を超えています。';
  77.             break;
  78.         case UPLOAD_ERR_PARTIAL:
  79.             $msg'アップロードされたファイルは一部のみしかアップロードされていません。';
  80.             break;
  81.         case UPLOAD_ERR_NO_FILE:
  82.             $msg 'ファイルはアップロードされませんでした。';
  83.             break;
  84.         case UPLOAD_ERR_NO_TMP_DIR:
  85.             $msg 'テンポラリフォルダがありません。';
  86.             break;
  87.         case UPLOAD_ERR_CANT_WRITE:
  88.             $msg'ディスクへの書き込みに失敗しました。';
  89.             break;
  90.         }
  91.         if ($msg != ''{
  92.             if (isset($params['error'])) {
  93.                 $msg $params['error'];
  94.             }
  95.             return Ethna::raiseNotice($msgE_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 'tmp_name が不正です。';
  105.             }
  106.             return Ethna::raiseNotice($msgE_FORM_WRONGTYPE_FILE);
  107.         }
  108.  
  109.  
  110.         // size の検査
  111.         if (isset($params['size_max'])) {
  112.             $st stat($var['tmp_name']);
  113.             if ($st[7$this->_getSizeAsBytes($params['size_max'])) {
  114.                 if (isset($params['error'])) {
  115.                     $msg $params['error'];
  116.                 else {
  117.                     $msg 'ファイルサイズは%s以下にしてください。';
  118.                 }
  119.                 return Ethna::raiseNotice($msgE_FORM_WRONGTYPE_FILEarray($params['size_max']));
  120.             }
  121.         }
  122.         if (isset($params['size_min'])) {
  123.             $st stat($var['tmp_name']);
  124.             if ($st[7$this->_getSizeAsBytes($params['size_min'])) {
  125.                 if (isset($params['error'])) {
  126.                     $msg $params['error'];
  127.                 else {
  128.                     $msg 'ファイルサイズは%s以上にしてください。';
  129.                 }
  130.                 return Ethna::raiseNotice($msgE_FORM_WRONGTYPE_FILEarray($params['size_min']));
  131.             }
  132.         }
  133.  
  134.  
  135.         // type の検査
  136.         if (isset($params['type'])) {
  137.             $type_list to_array($params['type']);
  138.             $posted_mime explode('/'$var['type']2);
  139.             foreach ($type_list as $type{
  140.                 $wanted_mime explode('/'$type2);
  141.                 $test (count($wanted_mime== 1)
  142.                         ? (strcasecmp($wanted_mime[0]$posted_mime[0]== 0)
  143.                 : (strcasecmp($type$var['type']== 0);  
  144.                 if ($test == true{
  145.                     break;
  146.                 }
  147.             }
  148.             if ($test == false{
  149.                 if (isset($params['error'])) {
  150.                     $msg $params['error'];
  151.                 else {
  152.                     $msg 'ファイルタイプが正しくありません。';
  153.                 }
  154.                 return Ethna::raiseNotice($msgE_FORM_WRONGTYPE_FILE);
  155.             }
  156.         }
  157.  
  158.         // name(ファイル名)の検査
  159.         if (isset($params['name'])) {
  160.             $test ($params['name']{0== '/')
  161.                 ? preg_match($params['name']$var['name'])
  162.                 : (strcmp($params['name']$var['name']== 0);
  163.             if ($test == false{
  164.                 if (isset($params['error'])) {
  165.                     $msg $params['error'];
  166.                 else {
  167.                     $msg 'ファイル名が正しくありません。';
  168.                 }
  169.                 return Ethna::raiseNotice($msgE_FORM_WRONGTYPE_FILE);
  170.             }
  171.         }
  172.  
  173.         return $true;
  174.     }
  175.  
  176.  
  177.     function _getSizeAsBytes($size)
  178.     {
  179.         $unit 1;
  180.         if (preg_match('/^([0-9]+)([mk])?(b(ytes?)?)?$/i'trim($size)$matches)) {
  181.             if (isset($matches[1])) {
  182.                 $size $matches[1];
  183.             }
  184.             if (isset($matches[2])) {
  185.                 if (strtolower($matches[2]=== 'm'{
  186.                     $unit 1048576;
  187.                 else if (strtolower($matches[2]=== 'k'{
  188.                     $unit 1024;
  189.                 }
  190.             }
  191.         }
  192.         return intval($matches[1]$unit;
  193.     }
  194. }
  195. // }}}
  196. ?>

Documentation generated on Thu, 08 May 2008 00:15:21 +0900 by phpDocumentor 1.4.2