Changeset 354 for tags

Show
Ignore:
Timestamp:
02/26/06 20:11:16 (3 years ago)
Author:
gerd
Message:

Fixed Bugs #208 and #209

  • implemented "maxsize" attribute
  • fixed validation check for non required files
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tags/RELEASE_0_9_0B2/examples/example_element_file.php

    r332 r354  
    4949                'uploaddir'     =>  './fileupload', 
    5050                'overwrite'     =>  'no', 
    51                 'usesession'    =>  'no', 
    52                 //'maxsize'        =>  20000, 
     51                'usesession'    =>  'yes', 
     52                'maxsize'      =>  900000, 
    5353                //'tempdir'     =>  '/tmp_does:not:exist' 
    5454                //'permissions' =>  0666, 
    5555                //'replacename' =>  'i/[^a-z0-9]\./_', 
    56                 //'mimetype'      =>  array( 'image/*', 'image/png' ), 
     56                'mimetype'        =>  array( 'image/*', 'image/png' ), 
    5757            ), 
    5858        ), 
     
    6868                'default'       =>  'x', 
    6969                'maxlength'     =>  '15', 
    70                 'minlength'     =>  '4', 
     70                'minlength'     =>  '2', 
    7171            ), 
    7272        ), 
     
    8787    // serialize the elements 
    8888    $elements   =   $form->renderForm(); 
    89      
    90      
     89 
    9190    // ERROR DISPLAY ------------------------------------------------------ 
    9291    // ask the form if it has been submitted and display errors. For 
     
    102101    displayForm( $form, $elements ); // see patExampleGen/customFunctions.php 
    103102 
    104  
    105103     
    106104     
  • tags/RELEASE_0_9_0B2/patForms/Element/File.php

    r332 r354  
    1313 *    Though the user may not upload a file twice of validation of other form elements faile 
    1414 *  - tempdir: The place to store uploaded file until finalization 
     15 *  - maxsize: maximum size of upload file in bytes (consider your web server's and PHP settings!) 
    1516 *  - mimetype: Defines as many allowed filetype as you want. Supports asterisk for groups like: 'image/*' 
    1617 *  - replacechars: use Perl-Regular expression for filename-translation 
     
    205206                                        "outputFormats" =>  array(), 
    206207                                    ), 
     208            "maxsize"       =>  array(  "required"      =>  false, 
     209                                        "format"        =>  "int", 
     210                                        "outputFormats" =>  array(), 
     211                                    ), 
    207212            "mimetype"      =>  array(  "required"      =>  false, 
    208213                                        "format"        =>  "array", 
     
    324329            } 
    325330             
    326             if( !is_dir( $tempdir ) && !is_writeable( $tempdir ) ) 
    327             { 
     331            if( !is_dir( $tempdir ) && !is_writeable( $tempdir ) ) { 
    328332                return patErrorManager::raiseError( PATFORMS_FILE_ERROR_TEMPDIR_NOT_VALID, 
    329333                                                'Session support is on but cannot work', 
     
    362366 
    363367        // editable or not? 
    364         if( isset( $this->attributes['edit'] ) && $this->attributes['edit'] == 'no' ) 
    365         { 
     368        if( isset( $this->attributes['edit'] ) && $this->attributes['edit'] == 'no' ) { 
    366369            return $this->serializeHtmlReadonly( $value ); 
    367370        } 
     
    369372        // create element 
    370373        $this->attributes['name']   =   $nameUpload; 
     374        $this->attributes['id']     =   $nameUpload; 
    371375        $element = $this->toHtml(); 
    372         if( patErrorManager::isError( $element ) ) 
    373         { 
     376        if( patErrorManager::isError( $element ) ) { 
    374377            return $element; 
    375378        } 
    376379 
    377         $this->attributes["name"]   =   $name; 
    378         $hidden =   $this->createHiddenTag( $this->attributes["value"] ); 
     380        $this->attributes["name"]   =   $name;       
     381        $valueElement =   $this->createHiddenTag( $this->attributes["value"] ); 
     382        /* 
     383        if( $this->useSession() && $this->getSessionValue( 'state' ) == 'valid' ) { 
     384            $atts   =   $this->attributes; 
     385            $atts   =   array( 
     386                        'value' =>  $this->attributes["value"], 
     387                        'type'  =>  'checkbox', 
     388                        'title' =>  'keep uploaded file: "'. $this->attributes["value"] .'"', 
     389                        'checked'   =>  'checked' 
     390                        ); 
     391            $valueElement   =   $this->createTag( 'input', 'full', $atts ); 
     392        } 
     393        */ 
    379394 
    380395        // and return to sender... 
    381         return $hidden . "\n" . $element; 
     396        return $valueElement . "\n" . $element; 
    382397    } 
    383398 
     
    413428        $empty      =   false; 
    414429        $mime       =   false; 
     430        $uploade    =   false;       
    415431 
    416432        $error      =   UPLOAD_ERR_NO_FILE; 
    417433        $restored   =   false; 
    418434 
    419         $files = $_FILES; 
    420         if (($namespace = $this->getNamespace()) && isset($files[$namespace])) { 
    421             $files = $files[$namespace]; 
    422         } 
    423  
     435        $files      =   false; 
     436        if( isset( $_FILES ) ) { 
     437            $files = $_FILES; 
     438             
     439            if( ( $namespace = $this->getNamespace() ) && isset( $files[$namespace] ) ) { 
     440                $files = $files[$namespace]; 
     441            } 
     442        } 
     443         
    424444        // file was just uploaded 
    425         if( isset( $files ) && (isset($files[$nameUpload]) || isset($files['name'][$nameUpload]))) 
    426         { 
    427             if (!$namespace) { 
     445        if( !empty( $files ) && ( isset($files[$nameUpload] ) || isset( $files['name'][$nameUpload] ) ) ) { 
     446            if( !$namespace ) { 
    428447                $uploadFile =   $files[$nameUpload]['tmp_name']; 
    429448                $error      =   $files[$nameUpload]['error']; 
     
    440459 
    441460            // see if the value comes from "setValue()" instead 
    442             if( empty( $value ) && !empty( $this->value ) ) 
    443             { 
    444                 if( $this->useSession() ) 
    445                 { 
     461            if( empty( $value ) && !empty( $this->value ) ) { 
     462                if( $this->useSession() ) { 
    446463                    // already stored in session 
    447464                    $state      =   $this->getSessionValue( 'state' ); 
    448                     if( $state == 'valid' ) 
    449                     { 
     465                    if( $state == 'valid' ) { 
    450466                        return true; 
    451467                    } 
     468                    $this->setSessionValue( 'state', 'finalized' ); 
     469                    return true; 
    452470                } 
    453  
    454                 /* 
    455                 $dir    =   $this->attributes['uploaddir']; 
    456                 $mime   =   mime_content_type( $dir . '/' . $this->value ); 
    457                 if( !$this->_checkMimeType( $mime ) ) 
    458                 { 
    459                     return false; 
    460                 } 
    461                 */ 
    462  
    463                 if( $this->useSession() ) 
    464                 { 
    465                     $this->setSessionValue( 'state', 'finalized' ); 
    466                 } 
    467                 return true; 
    468471            } 
    469472 
    470473            // don't allow empty files 
    471             if( !$error && !$size ) 
    472             { 
     474            if( !$error && !$size ) { 
    473475                $this->addValidationError( 6 ); 
    474476                return false; 
     
    476478 
    477479            // new file -> must be valid 
    478             if( $this->useSession() ) 
    479             { 
     480            if( $this->useSession() ) { 
    480481                $this->setSessionValue( 'state', 'invalid' ); 
    481             } 
    482  
    483             // remove previous uploaded file 
    484             if( $oldfile = $this->getSessionValue( 'uploadfile' ) ) 
    485             { 
    486                 unlink( $oldfile ); 
    487             } 
    488  
    489             if( !$this->_checkErrorCode( $error ) ) 
    490             { 
     482 
     483                // remove previous uploaded file 
     484                if( $oldfile = $this->getSessionValue( 'uploadfile' ) ) { 
     485                    unlink( $oldfile ); 
     486                }                 
     487            } 
     488 
     489            if( !$this->_checkErrorCode( $error ) ) { 
    491490                return false; 
    492491            } 
    493492 
    494493            // validate file-type 
    495             if( !$this->_checkMimeType( $mime ) ) 
    496             { 
     494            if( !$this->_checkMimeType( $mime ) ) { 
    497495                return false; 
    498496            } 
     497             
     498            if( $size > $this->attributes['maxsize'] ) { 
     499                $this->addValidationError( 3, array( 'maxsize' => $this->attributes['maxsize'] ) ); 
     500                return false; 
     501            } 
    499502        } 
    500503        // see if file was uploaded during this session 
     
    505508            $mime       =   $this->getSessionValue( 'mime' ); 
    506509 
    507             if( $sessValue = $this->getSessionValue( 'name' ) ) 
    508             { 
     510            if( $sessValue = $this->getSessionValue( 'name' ) ) { 
    509511                $value  =   $sessValue; 
    510512            } 
    511513 
    512514            // all done, skip the rest 
    513             if( $state == 'finalized' ) 
    514             { 
     515            if( $state == 'finalized' ) { 
    515516                $this->value        =   $value; 
    516517                return true; 
     
    518519 
    519520            $exists =   $this->_fileExists( $value ); 
    520             if( patErrorManager::isError( $exists ) ) 
    521             { 
     521            if( patErrorManager::isError( $exists ) ) { 
    522522                return $exists; 
    523523            } 
    524524 
    525             if( $exists ) 
    526             { 
     525            if( $exists ) { 
    527526                return true; 
    528527            } 
    529528 
    530             if( $state == 'valid' || file_exists( $uploadFile ) ) 
    531             { 
     529            if( $state == 'valid' || file_exists( $uploadFile ) ) { 
    532530                $restored   =   true; 
    533531                $error      =   UPLOAD_ERR_OK; 
    534532            } 
    535533 
    536             if( !$this->_checkErrorCode( $error ) ) 
    537             { 
     534            if( !$this->_checkErrorCode( $error ) ) { 
    538535                return false; 
    539536            } 
     
    543540        { 
    544541            $exists =   $this->_fileExists( $value ); 
    545             if( patErrorManager::isError( $exists ) ) 
    546             { 
     542            if( patErrorManager::isError( $exists ) ) { 
    547543                return $exists; 
    548544            } 
    549             else if( $exists ) 
    550             { 
     545            else if( $exists ) { 
    551546                return true; 
    552547            } 
     
    554549 
    555550        // not required 
    556         if( empty( $value ) && $this->attributes['required'] != 'yes' ) 
    557         { 
     551        if( empty( $value ) && $this->attributes['required'] != 'yes' ) { 
    558552            return true; 
    559553        } 
    560554 
    561555        $checkDir   =   $this->_checkDestination(); 
    562         if( patErrorManager::isError( $checkDir ) ) 
    563         { 
     556        if( patErrorManager::isError( $checkDir ) ) { 
    564557            return $checkDir; 
    565558        } 
     
    567560        // check if file exists 
    568561        $dir    =   $this->attributes['uploaddir']; 
    569         if( $this->attributes['overwrite'] != 'yes' ) 
    570         { 
    571             if( file_exists( $dir . '/' . $value ) ) 
    572             { 
     562        if( $this->attributes['overwrite'] != 'yes' ) { 
     563            if( file_exists( $dir . '/' . $value ) ) { 
    573564                $this->addValidationError( 2 ); 
    574565                return false; 
     
    577568 
    578569        // keep uploaded file 
    579         if( $this->useSession() && !$restored ) 
    580         { 
     570        if( $this->useSession() && !$restored ) { 
    581571            $tempfile   =   tempnam( $this->tempdir, $name . '_' ); 
    582             if( !move_uploaded_file( $uploadFile, $tempfile ) ) 
    583            
     572             
     573            if( !move_uploaded_file( $uploadFile, $tempfile ) )
    584574                return patErrorManager::raiseError( PATFORMS_FILE_ERROR_CANNOT_MOVE_UPLOAD_FILE, 
    585575                            "Cannot upload file", 
     
    613603        $dir        =   $this->attributes["uploaddir"]; 
    614604 
    615         $files = $_FILES; 
    616         if (($namespace = $this->getNamespace()) && isset($files[$namespace])) { 
    617             $files = $files[$namespace]; 
    618         } 
    619  
    620605        $uploadFile =   false; 
    621         if( $this->useSession() ) 
    622         { 
     606        if( $this->useSession() ) { 
    623607            // check if this upload was finalized 
    624608            $state  =   $this->getSessionValue( "state" ); 
    625             if( $state == "finalized" ) 
    626             { 
     609            if( $state == "finalized" ) { 
    627610                return true; 
    628611            } 
     
    633616            $this->setSessionValue( "state", "finalized" ); 
    634617        } 
    635         else if( isset( $files[$nameUpload]["tmp_name"] ) ) 
    636         { 
    637             $uploadFile =   $files[$nameUpload]["tmp_name"]; 
    638         } 
    639         else if( isset( $files["tmp_name"][$nameUpload] ) ) 
    640         { 
    641             $uploadFile =   $files["tmp_name"][$nameUpload]; 
     618        else { 
     619            $files = $_FILES; 
     620            if (($namespace = $this->getNamespace()) && isset($files[$namespace])) { 
     621                $files = $files[$namespace]; 
     622            } 
     623         
     624            if( isset( $files[$nameUpload]["tmp_name"] ) ) { 
     625                $uploadFile =   $files[$nameUpload]["tmp_name"]; 
     626            }       
     627            else if( isset( $files["tmp_name"][$nameUpload] ) ) { 
     628                $uploadFile =   $files["tmp_name"][$nameUpload]; 
     629            } 
    642630        } 
    643631 
    644632        // if file does not exist and is not required, function will just return 
    645         if( $this->attributes["required"] != "yes" && !$uploadFile ) 
    646         { 
     633        if( $this->attributes["required"] != "yes" && !$uploadFile ) { 
    647634            return true; 
    648635        } 
    649636 
    650637        // cannot use 'move_upload_file()' in case of usage of sessions 
    651         if( !rename( $uploadFile, "$dir/$value" ) ) 
    652         { 
     638        if( !rename( $uploadFile, "$dir/$value" ) ) { 
    653639            return patErrorManager::raiseError( PATFORMS_FILE_ERROR_CANNOT_MOVE_UPLOAD_FILE, 
    654640                        "Cannot upload file", 
     
    688674            // no file send 
    689675            case    UPLOAD_ERR_NO_FILE: 
    690                 if( $this->attributes['required'] == 'yes' ) 
    691                 { 
     676                if( $this->attributes['required'] == 'yes' ) { 
    692677                    $this->addValidationError( 1 ); 
    693678                    return false; 
     
    714699    function _checkMimeType( $type ) 
    715700    { 
    716         if( empty( $this->attributes["mimetype"] ) ) 
    717         { 
     701        // an empty type means there was no file uploaded - don't bother about that 
     702        if( empty( $type ) ) { 
     703            return true; 
     704        } 
     705     
     706        if( empty( $this->attributes["mimetype"] ) ) { 
    718707            return true; 
    719708        } 
    720709 
    721710        // check for exact match 
    722         if( in_array( $type, $this->attributes["mimetype"] ) ) 
    723         { 
     711        if( in_array( $type, $this->attributes["mimetype"] ) ) { 
    724712            return true; 
    725713        } 
     
    727715        // check for *-pattern like image/* 
    728716        list( $major )  =   explode( "/", $type ); 
    729         foreach( $this->attributes["mimetype"] as $m ) 
    730         { 
     717        foreach( $this->attributes["mimetype"] as $m ) { 
    731718            $m =    explode( "/", $m ); 
    732             if( $m[1] == "*" && $m[0] == $major ) 
    733             { 
     719            if( $m[1] == "*" && $m[0] == $major ) { 
    734720                return true; 
    735721            } 
     
    751737        $preg   =   explode( "/", $this->attributes["replacename"] ); 
    752738 
    753         if( count( $preg ) != 3 ) 
    754         { 
     739        if( count( $preg ) != 3 ) { 
    755740            return patErrorManager::raiseError( PATFORMS_FILE_ERROR_REPLACE_PREG_INVALID, 
    756741                        "Attribute 'replace' is not a valid PREG", 
     
    770755    { 
    771756        // check destination 
    772         if( !strlen( $this->attributes["uploaddir"] ) ) 
    773         { 
     757        if( !strlen( $this->attributes["uploaddir"] ) ) { 
    774758                return patErrorManager::raiseError(  PATFORMS_FILE_ERROR_NO_FILE_UPLOAD_DIR, 
    775759                                "Cannot upload file", 
     
    778762 
    779763        $dir    =   $this->attributes["uploaddir"]; 
    780         if( !is_dir( $dir ) ) 
    781         { 
     764        if( !is_dir( $dir ) ) { 
    782765            return patErrorManager::raiseError(  PATFORMS_FILE_ERROR_NO_FILE_UPLOAD_DIR, 
    783766                            "Cannot upload file", 
     
    785768        } 
    786769 
    787         if( !is_writeable( $dir ) ) 
    788         { 
     770        if( !is_writeable( $dir ) ) { 
    789771            return patErrorManager::raiseError( PATFORMS_FILE_ERROR_UPLOAD_DIR_NOT_WRITEABLE, 
    790772                            "Cannot upload file", 
     
    804786    function _fileExists( $file ) 
    805787    { 
    806         if( !$file && !strlen( $file ) ) 
    807         { 
     788        if( !$file && !strlen( $file ) ) { 
    808789            return false; 
    809790        } 
    810791 
    811792        $result =   $this->_checkDestination(); 
    812         if( patErrorManager::isError( $result ) ) 
    813         { 
     793        if( patErrorManager::isError( $result ) ) { 
    814794            return $result; 
    815795        } 
    816796 
    817797        $dir    =   $this->attributes["uploaddir"]; 
    818         if( file_exists( $dir . "/" . $file ) ) 
    819         { 
     798        if( file_exists( $dir . "/" . $file ) ) { 
    820799            return true; 
    821800        } 
     
    831810    function _getPermission() 
    832811    { 
    833         if( $this->permission != null ) 
    834         { 
     812        if( $this->permission != null ) { 
    835813            return $this->permission; 
    836814        } 
     
    839817 
    840818        // permission string must be contain four letters, e.g. 0644 
    841         if( strlen( $perm ) != 4 ) 
    842         { 
     819        if( strlen( $perm ) != 4 ) { 
    843820            patErrorManager::raiseError(    PATFORMS_FILE_ERROR_PERMISSION_ATTRIBUTE_NOT_VALID, 
    844821                                            "Wrong format of attribute 'permissions'", 
     
    847824        } 
    848825 
    849         if( !is_numeric( $perm ) ) 
    850         { 
     826        if( !is_numeric( $perm ) ) { 
    851827            patErrorManager::raiseError(    PATFORMS_FILE_ERROR_PERMISSION_ATTRIBUTE_NOT_VALID, 
    852828                                            "Wrong format of attribute 'permissions'", 
     
    855831        } 
    856832 
    857         if( $perm[0] != '0' ) 
    858         { 
     833        if( $perm[0] != '0' ) { 
    859834            patErrorManager::raiseError(    PATFORMS_FILE_ERROR_PERMISSION_ATTRIBUTE_NOT_VALID, 
    860835                                            "Wrong format of attribute 'permissions'",