Source for file FileUpload.php

Documentation is available at FileUpload.php

  1. <?php
  2. /**
  3.  * Teeple2 - PHP5 Web Application Framework inspired by Seasar2
  4.  *
  5.  * PHP versions 5
  6.  *
  7.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  8.  * that is available through the world-wide-web at the following URI:
  9.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10.  * the PHP License and are unable to obtain it through the web, please
  11.  * send a note to license@php.net so we can mail you a copy immediately.
  12.  *
  13.  * @package     teeple
  14.  * @author      Mitsutaka Sato <miztaka@gmail.com>
  15.  * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
  16.  */
  17.  
  18. if (!defined('UPLOAD_ERR_OK')) {
  19.     define('UPLOAD_ERR_OK',        0);
  20.     define('UPLOAD_ERR_INI_SIZE',  1);
  21.     define('UPLOAD_ERR_FORM_SIZE'2);
  22.     define('UPLOAD_ERR_PARTIAL',   3);
  23.     define('UPLOAD_ERR_NO_FILE',   4);
  24. }
  25.  
  26. define('UPLOAD_ERROR_TYPE''FileUpload');
  27.  
  28. /**
  29.  * FileUpload処理を行うFilter
  30.  * TODO 未検証
  31.  *
  32.  * @package     teeple.filter
  33.  */
  34. {
  35.     
  36.     /**
  37.      * @var Teeple_ActionChain 
  38.      */
  39.     private $actionChain;
  40.     public function setComponent_Teeple_ActionChain($c{
  41.         $this->actionChain = $c;
  42.     }
  43.     
  44.     /**
  45.      * @var Teeple_FileUpload 
  46.      */
  47.     private $fileUpload;
  48.     public function setComponent_Teeple_FileUpload($c{
  49.         $this->fileUpload $c;
  50.     }
  51.     
  52.     /**
  53.      * @var Teeple_Request 
  54.      */
  55.     private $request;
  56.     public function setComponent_Teeple_Request($c{
  57.         $this->request = $c;
  58.     }
  59.     
  60.     /**
  61.      * コンストラクタ
  62.      *
  63.      */
  64.     public function __construct({
  65.         parent::__construct();
  66.     }
  67.     
  68.     /**
  69.      * ファイルアップロード処理を行う
  70.      *
  71.      */
  72.     public function prefilter({
  73.  
  74.         if ($this->isCompleteAction()) {
  75.             $this->log->info("CompleteActionフラグが立っているため、FileUploadは実行されませんでした。");
  76.             return;
  77.         }
  78.         
  79.         $attributes $this->getAttributes();
  80.         if (isset($attributes["name"])) {
  81.             $this->fileUpload->setName($attributes["name"]);
  82.             
  83.             if (isset($attributes["filemode"])) {
  84.                 $this->fileUpload->setFilemode($attributes["filemode"]);
  85.             }
  86.             
  87.             //maple.iniを分析
  88.             $maxsize_ini array();
  89.             $type_ini array();
  90.             $sizeError_ini array();
  91.             $typeError_ini array();
  92.             $noFileError_ini array();
  93.             foreach($attributes as $key => $value{
  94.                 if (substr($key,0,7== "maxsize"{
  95.                     if (strlen($key== 7{
  96.                         $maxsize_ini["default"$value;
  97.                     else if (is_numeric(substr($key,8,strlen($key)-9))) {
  98.                         $maxsize_ini[substr($key,8,strlen($key)-9)$value;
  99.                     }
  100.                 }
  101.                 
  102.                 if (substr($key,0,4== "type"{
  103.                     $typeArray array();
  104.                     if (strlen($key== 4{
  105.                         $typeArray explode(","$value);
  106.                         $type_ini["default"$typeArray;
  107.                     else if (is_numeric(substr($key,5,strlen($key)-6))) {
  108.                         $typeArray explode(","$value);
  109.                         $type_ini[substr($key,5,strlen($key)-6)$typeArray;
  110.                     }
  111.                 }
  112.                 
  113.                 if (substr($key,0,9== "sizeError"{
  114.                     if (strlen($key== 9{
  115.                         $sizeError_ini["default"$value;
  116.                     else if (is_numeric(substr($key,10,strlen($key)-11))) {
  117.                         $sizeError_ini[substr($key,10,strlen($key)-11)$value;
  118.                     }
  119.                 }
  120.                 
  121.                 if (substr($key,0,9== "typeError"{
  122.                     if (strlen($key== 9{
  123.                         $typeError_ini["default"$value;
  124.                     else if (is_numeric(substr($key,10,strlen($key)-11))) {
  125.                         $typeError_ini[substr($key,10,strlen($key)-11)$value;
  126.                     }
  127.                 }
  128.                 
  129.                 if (substr($key,0,11== "noFileError"{
  130.                     if (strlen($key== 11{
  131.                         $noFileError_ini["default"$value;
  132.                     else if (is_numeric(substr($key,12,strlen($key)-13))) {
  133.                         $noFileError_ini[substr($key,12,strlen($key)-13)$value;
  134.                     else if (substr($key,12,7== "whether"{
  135.                         $noFileError_ini["whether"$value;
  136.                         $noFileError_whether 0;
  137.                     }
  138.                 }
  139.             }
  140.             
  141.             //関連配列
  142.             $error $this->fileUpload->getError();
  143.             
  144.             $mime_type_check $this->fileUpload->checkMimeType($type_ini);
  145.             
  146.             $filesize_check $this->fileUpload->checkFilesize($maxsize_ini);
  147.             //以下はforeachで各ファイルでエラーチェックを行う
  148.             foreach ($error as $key => $val{
  149.                 if ($val != UPLOAD_ERR_OK{// PHP自体が感知するエラーが発生した場合
  150.                     if ($val == UPLOAD_ERR_INI_SIZE{
  151.                         $this->request->setFilterError('FileUpload');
  152.                         //$errorList->setType(UPLOAD_ERROR_TYPE);
  153.                         if (isset($attributes["iniSizeError"])) {
  154.                             $message $attributes["iniSizeError"];
  155.                         else {
  156.                             $message "アップロードされたファイルは、php.ini の upload_max_filesize ディレクティブの値を超えています。";
  157.                         }
  158.                         //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  159.                         $this->request->addErrorMessage($message);
  160.                         break;
  161.                     else if ($val == UPLOAD_ERR_FORM_SIZE{
  162.                         //$errorList->setType(UPLOAD_ERROR_TYPE);
  163.                         $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  164.                         if (isset($attributes["formSizeError"])) {
  165.                             $message $attributes["formSizeError"];
  166.                         else {
  167.                             $message "アップロードされたファイルは、HTMLフォームで指定された MAX_FILE_SIZE を超えています。";
  168.                         }
  169.                         //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  170.                         $this->request->addErrorMessage($message);
  171.                         break;
  172.                     else if ($val == UPLOAD_ERR_PARTIAL{
  173.                         //$errorList->setType(UPLOAD_ERROR_TYPE);
  174.                         $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  175.                         if (isset($attributes["partialError"])) {
  176.                             $message $attributes["partialError"];
  177.                         else {
  178.                             $message "アップロードされたファイルは一部のみしかアップロードされていません。";
  179.                         }
  180.                         //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  181.                         $this->request->addErrorMessage($message);
  182.                         break;
  183.                     else if ($val == UPLOAD_ERR_NO_FILE{
  184.                         if (isset($noFileError_ini[$key])) {
  185.                             //$errorList->setType(UPLOAD_ERROR_TYPE);
  186.                             $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  187.                             $message $noFileError_ini[$key];
  188.                             //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  189.                             $this->request->addErrorMessage($message);
  190.                         }else if (isset($noFileError_ini["default"])) {
  191.                             //$errorList->setType(UPLOAD_ERROR_TYPE);
  192.                             $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  193.                             $message $noFileError_ini["default"];
  194.                             //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  195.                             $this->request->addErrorMessage($message);
  196.                             break;
  197.                         else if (isset($noFileError_ini["whether"])) {
  198.                             $noFileError_whether $noFileError_whether +1;
  199.                         }
  200.                     }
  201.                 }else {// PHP自体が感知するエラーは発生していない場合
  202.                     //
  203.                     // maple.iniで設定されたサイズを超えていた場合
  204.                     //
  205.                     if (count($maxsize_ini0{
  206.                         if (!$filesize_check[$key]{
  207.                             //$errorList->setType(UPLOAD_ERROR_TYPE);
  208.                             $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  209.                             if (isset($sizeError_ini[$key])) {
  210.                                 $message $sizeError_ini[$key];
  211.                             }else if (isset($sizeError_ini["default"])) {
  212.                                 $message $sizeError_ini["default"];
  213.                             else {
  214.                                 $message "ファイルはアップロードされませんでした。";
  215.                             }
  216.                             //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  217.                             $this->request->addErrorMessage($message);
  218.                         }
  219.                     }
  220.                     //
  221.                     // maple.iniで設定されたMIME-Typeではなかった場合
  222.                     //
  223.                     if (count($type_ini0{
  224.                         if (!$mime_type_check[$key]{
  225.                             //$errorList->setType(UPLOAD_ERROR_TYPE);
  226.                             $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  227.                             if (isset($typeError_ini[$key])) {
  228.                                 $message $typeError_ini[$key];
  229.                             }else if (isset($typeError_ini["default"])) {
  230.                                 $message $typeError_ini["default"];
  231.                             else {
  232.                                 $message "指定されたファイル形式ではありません。";
  233.                             }
  234.                             //$errorList->add($this->fileUpload->getName()."[".$key."]", $message);
  235.                             $this->request->addErrorMessage($message);
  236.                         }
  237.                     }
  238.                 }
  239.             }
  240.             if (isset($noFileError_whether&& count($error== $noFileError_whether{
  241.                 //$errorList->setType(UPLOAD_ERROR_TYPE);
  242.                 $this->request->setFilterError(UPLOAD_ERROR_TYPE);
  243.                 $message $noFileError_ini["whether"];
  244.                 //$errorList->add($this->fileUpload->getName(), $message);
  245.                 $this->request->addErrorMessage($message);
  246.             }
  247.         else {
  248.             $this->log->trace("フィールド名が指定されていません""Filter_FileUpload#execute");
  249.         }
  250.  
  251.         return;
  252.     }
  253.     
  254.     public function postfilter({}
  255. }
  256. ?>

Documentation generated on Mon, 26 Apr 2010 08:59:46 +0900 by phpDocumentor 1.4.3