PageRenderTime 114ms CodeModel.GetById 25ms RepoModel.GetById 7ms app.codeStats 0ms

/lib/fields/class.UploadField.php

https://github.com/reshadf/Library
PHP | 800 lines | 579 code | 50 blank | 171 comment | 57 complexity | 932bb44eac20e4c85b1254f2ef8ca19f MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * class UploadField
  4. *
  5. * File uploads handler class
  6. *
  7. * @author Teye Heimans
  8. * @package FormHandler
  9. * @subpackage Fields
  10. * @since 19-03-2008 J.Wiegel Fixed CHMOD bug
  11. */
  12. class UploadField extends Field
  13. {
  14. var $_aConfig; // array: which contains the default upload config
  15. var $_bAlertOverwrite; // boolean: display a message when the file already exists
  16. var $_sFilename; // string: filename of the file
  17. /**
  18. * UploadField::UploadField()
  19. *
  20. * Constructor: Create a new UploadField object
  21. *
  22. * @param object &$oForm: The form where this field is located on
  23. * @param string $sName: The name of the field
  24. * @param array $aConfig: The config array
  25. * @return UploadField
  26. * @access public
  27. * @author Teye Heimans
  28. */
  29. function UploadField( &$oForm, $sName, $aConfig )
  30. {
  31. require_once(FH_INCLUDE_DIR.'includes/mimeTypes.php');
  32. static $bSetJS = false;
  33. // needed javascript included yet ?
  34. if(!$bSetJS)
  35. {
  36. // include the needed javascript
  37. $bSetJS = true;
  38. $oForm->_setJS(FH_FHTML_DIR."js/extension_check.js", true);
  39. }
  40. $this->_bAlertOverwrite = true;
  41. // check if there are spaces in the fieldname
  42. if(strpos($sName,' ') !== false)
  43. {
  44. trigger_error('Warning: There are spaces in the field name "'.$sName.'"!', E_USER_WARNING );
  45. }
  46. // set the config file
  47. $aDefault = unserialize( FH_DEFAULT_UPLOAD_CONFIG );
  48. $this->_aConfig = array_merge( $aDefault, $aConfig );
  49. // add slash to the end of the pathname if it does not have already
  50. $l = substr($this->_aConfig['path'], -1);
  51. if($l != '\\' && $l != '/')
  52. {
  53. $this->_aConfig['path'] .= '/';
  54. }
  55. // if no size is given, get the max uploadsize
  56. if( empty($this->_aConfig['size']) )
  57. {
  58. $this->_aConfig['size'] = $this->_getMaxUploadSize();
  59. }
  60. // make the mime types given by the user useable
  61. if( is_string( $this->_aConfig['mime'] ) )
  62. {
  63. $mime = explode(' ', $this->_aConfig['mime'] );
  64. $this->_aConfig['mime'] = array(
  65. '*' => $mime
  66. );
  67. unset( $mime );
  68. }
  69. // mime types are given as an array
  70. else
  71. {
  72. foreach ( $this->_aConfig['mime'] as $key => $value )
  73. {
  74. if( is_numeric( $key ) )
  75. {
  76. $this->_aConfig['mime']['*'][] = $value;
  77. unset( $this->_aConfig['mime'][$key] );
  78. }
  79. else
  80. {
  81. if( is_string( $value ) )
  82. {
  83. $this->_aConfig['mime'][$key] = explode(' ', $value);
  84. }
  85. }
  86. }
  87. }
  88. $this->_oForm = $oForm;
  89. $this->_sName = $sName;
  90. // get the value of the field
  91. if( $oForm->isPosted() )
  92. {
  93. // make sure that the $_FILES and $_POST array are global
  94. if(!_global) global $_FILES, $_POST;
  95. // get the value if it exists in the $_FILES array
  96. if( isset( $_FILES[$sName] ) )
  97. {
  98. // detect error type if php version is older then 4.2.0 (make the errors the same)
  99. if($_FILES[$sName]['tmp_name'] == 'none' && empty($_FILES[$sName]['name']))
  100. {
  101. // nothing uploaded
  102. $_FILES[$sName]['error'] = 4;
  103. }
  104. elseif($_FILES[$sName]['tmp_name'] == 'none' && !empty($_FILES[$sName]['name']))
  105. {
  106. // file is bigger then given in MAX_FILE_SIZE.
  107. $_FILES[$sName]['error'] = 2;
  108. }
  109. elseif(!isset($_FILES[$sName]['error']))
  110. {
  111. // no error occured
  112. $_FILES[$sName]['error'] = 0;
  113. }
  114. // save the uploaded file data
  115. $this->_mValue = $_FILES[$sName];
  116. // check if there is a file uploaded...
  117. if( $this->isUploaded() )
  118. {
  119. $this->setValue( $this->_getFilename( ) );
  120. }
  121. else
  122. {
  123. // if this is an edit form and no value is given to the field,
  124. // keep the existing value thats in the database
  125. if( isset( $oForm->edit) && $oForm->edit && isset( $oForm->_dbData[$sName] ) )
  126. {
  127. $this->setValue( $oForm->_dbData[$sName] );
  128. }
  129. }
  130. }
  131. // posted value known? (when using multiple pages)
  132. elseif ( isset( $_POST[$sName] ) )
  133. {
  134. $this->setValue( get_magic_quotes_gpc() ? stripslashes($_POST[$sName]) : $_POST[$sName] );
  135. }
  136. // edit form?
  137. elseif( (isset($oForm->edit) && $oForm->edit) )
  138. {
  139. // value known from the database?
  140. if( isset( $oForm->_dbData[$sName] ) )
  141. {
  142. $this->setValue( $oForm->_dbData[$sName] );
  143. }
  144. }
  145. }
  146. // load database value if exists
  147. elseif( (isset($oForm->edit) && $oForm->edit) )
  148. {
  149. if( isset( $oForm->_dbData[$sName] ) )
  150. {
  151. $this->setValue( $oForm->_dbData[$sName] );
  152. }
  153. }
  154. // if no file is uploaded and it is a edit form, dont overwrite the current value
  155. if(!$this->isUploaded() && (isset($oForm->edit) && $oForm->edit) && $oForm->isPosted() )
  156. {
  157. $this->_oForm->_dontSave[] = $sName;
  158. }
  159. // check if the user got another value for this field.
  160. if( isset($oForm ->_buffer[ $sName ] ) )
  161. {
  162. list( $bOverwrite, $sValue ) = $oForm->_buffer[ $sName ];
  163. // if the field does not exists in the database
  164. if($bOverwrite || !isset($oForm->_dbData[$sName]) )
  165. {
  166. $this->setValue( $sValue );
  167. }
  168. // remove the value from the buffer..
  169. unset( $oForm->_buffer[ $sName ] );
  170. }
  171. }
  172. /**
  173. * UploadField::getFileInfo()
  174. *
  175. * Return the data of an uploaded file or an empty array when no file is uploaded
  176. *
  177. * @return array
  178. * @access public
  179. * @author Teye Heimans
  180. */
  181. function getFileInfo()
  182. {
  183. // file uploaded! return the data!
  184. if( $this -> isUploaded() )
  185. {
  186. return $this -> _mValue;
  187. }
  188. // no file uploaded, return empty array
  189. return array();
  190. }
  191. /**
  192. * UploadField::setValue()
  193. *
  194. * Set the value of the field (the filename of the uploaded file)
  195. *
  196. * @param string $sFilename: The filename of the value
  197. * @return void
  198. * @access public
  199. * @author Teye Heimans
  200. */
  201. function setValue( $sFilename )
  202. {
  203. $this->_sFilename = $sFilename;
  204. }
  205. /**
  206. * UploadField::getSavePath()
  207. *
  208. * Return the path were we are going to save the file
  209. *
  210. * @return string: the path where the file is saved
  211. * @access public
  212. * @author Teye Heimans
  213. */
  214. function getSavePath()
  215. {
  216. return $this->_aConfig['path'];
  217. }
  218. /**
  219. * UploadField::getValue()
  220. *
  221. * Return the current value
  222. *
  223. * @return string: the current file
  224. * @access public
  225. * @author Teye Heimans
  226. */
  227. function getValue()
  228. {
  229. return isset($this->_sFilename) ? $this->_sFilename : '';
  230. }
  231. /**
  232. * UploadField::_getViewValue()
  233. *
  234. * Return the value as link to the file
  235. *
  236. * @return string
  237. * @access public
  238. * @author Remco van Arkelen
  239. * @since 19-11-2008
  240. */
  241. function _getViewValue( )
  242. {
  243. return '<a href="'. $this->getSavePath(). $this->getValue() .'" target="_blank">'. $this->getValue() .'</a>';
  244. }
  245. /**
  246. * UploadField::isUploaded()
  247. *
  248. * Check if there is a file uploaded or not
  249. *
  250. * @return boolean
  251. * @access public
  252. * @author Teye Heimans
  253. */
  254. function isUploaded()
  255. {
  256. if(!_global) global $_FILES;
  257. return (
  258. $this->_oForm->isPosted() && # form is posted
  259. isset( $_FILES[$this->_sName] ) && # file known in the $_FILES array
  260. $this->_mValue['error'] != 4 # is there a file uploaded ?
  261. );
  262. }
  263. /**
  264. * UploadField::getField()
  265. *
  266. * Return the HTML of the field
  267. *
  268. * @return string: the html of the field
  269. * @access public
  270. * @author Teye Heimans
  271. */
  272. function getField()
  273. {
  274. // view mode enabled ?
  275. if( $this -> getViewMode() )
  276. {
  277. // get the view value..
  278. return $this -> _getViewValue();
  279. }
  280. // alert the user if they have to upload the file again or when they are going to overwrite a file
  281. if($this->_bAlertOverwrite && !empty($this->_sFilename) && empty($this->_sError))
  282. {
  283. // edit form and the field got a value ?
  284. if( (isset($this->_oForm->edit) && $this->_oForm->edit) && !empty($this->_sFilename) )
  285. //isset( $this->_oForm->_dbData[$this->_sName] ) )
  286. {
  287. // display "overwrite" message
  288. $sMsg = str_replace(
  289. array('%path%', '%filename%'),
  290. array(
  291. str_replace((isset($_SERVER['DOCUMENT_ROOT'])?$_SERVER['DOCUMENT_ROOT']:''), '', $this->_aConfig['path']),
  292. (!empty($this->_mValue['name']) ? $this->_mValue['name'] :$this->_sFilename)
  293. ),
  294. $this->_oForm->_text( 19 )
  295. );
  296. }
  297. // Correct ?
  298. else if( $this->_oForm->isPosted() && !$this->_oForm->isCorrect() )
  299. {
  300. $sMsg = str_replace('%filename%', (!empty($this->_mValue['name']) ? $this->_mValue['name'] :$this->_sFilename), $this->_oForm->_text( 33 ));
  301. }
  302. // set the massage as it is an error message
  303. if( !empty($sMsg) )
  304. {
  305. $this->_sError = $sMsg;
  306. }
  307. }
  308. // set the javascript upload checker if wanted
  309. if(FH_UPLOAD_JS_CHECK && $this->_aConfig['type'] != '*')
  310. {
  311. // check if the user has also set an onchange event
  312. $aMatch = array();
  313. if(isset($this->_sExtra) && preg_match("/onchange *= *('|\")(.*)$/i", $this->_sExtra, $aMatch))
  314. {
  315. // put the function into a onchange tag if set
  316. $sStr = str_replace($aMatch[1], ( ($aMatch[1]=="'") ? '"' : "'" ), "fh_checkUpload(this, '".$this->_aConfig['type']."', '".$this->_oForm->_text( 20 )."');");
  317. $this->_sExtra = preg_replace("/onchange *= *('|\")(.*)$/i", "onchange=\\1$sStr\\2", $this->_sExtra);
  318. }
  319. // the user did not define an onchange event, just add it to the extra argument
  320. else
  321. {
  322. $this->_sExtra = "onchange=\"fh_checkUpload(this, '".$this->_aConfig['type']."', '".$this->_oForm->_text( 20 )."')\" " . ( !empty($this->_sExtra) ? $this->_sExtra : "");
  323. }
  324. }
  325. // return the field
  326. return sprintf(
  327. '<input type="file" name="%s" id="%1$s" %s'. FH_XHTML_CLOSE .'>%s',
  328. $this->_sName,
  329. (isset($this->_iTabIndex) ? 'tabindex="'.$this->_iTabIndex.'" ' : '').
  330. (isset($this->_sExtra) ? $this->_sExtra.' ':''),
  331. (isset($this->_sExtraAfter) ? $this->_sExtraAfter :'')
  332. );
  333. }
  334. /**
  335. * UploadField::setAlertOverwrite()
  336. *
  337. * Set if we have to alert when the file already exists
  338. *
  339. * @param boolean $bStatus: The status to set
  340. * @return void
  341. * @access public
  342. * @author Teye Heimans
  343. */
  344. function setAlertOverwrite( $bStatus )
  345. {
  346. $this->_bAlertOverwrite = $bStatus;
  347. }
  348. /**
  349. * UploadField::isValid()
  350. *
  351. * Check if the value is valid
  352. *
  353. * @return bool: check if the value is valid
  354. * @access public
  355. * @author Teye Heimans
  356. */
  357. function isValid()
  358. {
  359. // make the files array global if they are not
  360. if(!_global) global $_FILES;
  361. /**
  362. * Removed this part in order to get the required parameter working.
  363. * @since 02-04-2008
  364. * @author Johan Wiegel
  365. *
  366. * reactivated this part seems to work why was this removed?? 29-04-2009 JW
  367. */
  368. // when no uploadfield was submitted (on multi-paged forms)
  369. if( !isset( $_FILES[$this->_sName] ) )
  370. {
  371. return true;
  372. }
  373. // check if we have validated this field before
  374. if( isset( $this->_isValid ) )
  375. {
  376. return $this->_isValid;
  377. }
  378. // is a own error handler used?
  379. if(isset($this->_sValidator) && !empty($this->_sValidator) )
  380. {
  381. // check the field with the users validator
  382. $this->_isValid = Field::isValid();
  383. return $this->_isValid;
  384. }
  385. // easy name to work with (this is the $_FILES['xxx'] array )
  386. $aFile = $this->_mValue;
  387. // alert when file exists ? (and is a file uploaded ?)
  388. if( strtolower( $this->_aConfig['exists'] ) == 'alert' && # do we have to alert the user ?
  389. $aFile['error'] == 0 && # check if the file is succesfully uploaded
  390. file_exists( $this->_aConfig['path'] . $this->_getFilename(true) ) ) # check if the file exists
  391. {
  392. $this->_sError = $this->_oForm->_text( 21 );
  393. }
  394. // check if the field is required and if it is uploaded.
  395. if(( ( is_bool( $this->_aConfig['required'] ) && $this->_aConfig['required'] ) ||
  396. strtolower(trim($this->_aConfig['required'])) == 'true'
  397. ) &&
  398. $aFile['error'] == 4 &&
  399. empty($this->_sFilename) )
  400. {
  401. // no file uploaded
  402. $this->_sError = $this->_oForm->_text( 22 );
  403. }
  404. // when a file is uploaded...
  405. elseif($aFile['error'] != 4)
  406. {
  407. // file size to big?
  408. if( isset($aFile['error']) &&
  409. ($aFile['error'] == 1 ||
  410. $aFile['error'] == 2 ||
  411. $aFile['size'] > $this->_aConfig['size']))
  412. {
  413. $this->_sError = sprintf( $this->_oForm->_text( 23 ), round($this->_aConfig['size'] / 1024, 2) );
  414. $this->_isValid = false;
  415. return false;
  416. }
  417. // is the extension correct ?
  418. $sExt = $this->_getExtension( $this->_mValue['name'] );
  419. if( !$sExt )
  420. {
  421. // no extension found!
  422. $this->_sError = $this->_oForm->_text( 37 );
  423. }
  424. // extension is known..
  425. else
  426. {
  427. // check if the extension is allowed
  428. if($this->_aConfig['type'] != '*' && !in_array( $sExt , explode(' ', strtolower($this->_aConfig['type']))))
  429. {
  430. $this->_sError = sprintf( $this->_oForm->_text( 20 ), $this->_aConfig['type'] );
  431. }
  432. // does build in function exists for retrieving the mime type of the file?
  433. // if so, get the type of the mime from that function (more secure)
  434. // otherwise, use the one from the browser
  435. // as of php 5.3 mime_content_type is deprecated, use finfo_open instead
  436. if( function_exists( 'finfo_open' ) )
  437. {
  438. $finfo = finfo_open( FILEINFO_MIME );
  439. $sTypeRaw = finfo_file( $finfo,$aFile['tmp_name'] );
  440. finfo_close( $finfo );
  441. list( $sType ) = preg_split( '/;/', $sTypeRaw );
  442. }
  443. else
  444. {
  445. $sType = function_exists('mime_content_type') ? mime_content_type( $aFile['tmp_name'] ) : $aFile['type'];
  446. }
  447. // get the mime data
  448. $aMimeData = unserialize( FH_MIME_DATA );
  449. // the allowed mime types given by the user
  450. $aUsrMimeData = $this->_aConfig['mime'];
  451. # Debug message
  452. /*
  453. echo
  454. "<fieldset><legend><b>Debug Mime data</b></legend>\n".
  455. "Extension of the uploaded file: ".$sExt ."<br '. FH_XHTML_CLOSE .'>\n".
  456. "Mime type of uploaded file: ". $sType ."<br '. FH_XHTML_CLOSE .'>\n".
  457. "Extension known: ".(isset( $aMimeData[$sExt] ) ? "true":"false")."<br '. FH_XHTML_CLOSE .'>\n".
  458. (isset( $aMimeData[$sExt] ) ? "Mime type(s) expected: " . implode(", ", $aMimeData[$sExt]) : '')."<br '. FH_XHTML_CLOSE .'>\n".
  459. "User allowed mime types:<br'. FH_XHTML_CLOSE .'>\n<pre>";
  460. print_r( $aUsrMimeData );
  461. echo
  462. "</pre>\n".
  463. "</fieldset>\n";
  464. */
  465. // check if the mime type is allowed
  466. // wo 8 feb 2006: added "!isset( $aUsrMimeData['*'] ) || "
  467. if( ( !isset( $aUsrMimeData[$sExt] ) || !in_array( $sType, $aUsrMimeData[$sExt] ) ) &&
  468. ( !isset( $aUsrMimeData['*'] ) || !in_array( $sType, $aUsrMimeData['*'] ) ) &&
  469. ( !isset( $aMimeData[$sExt] ) || !in_array($sType, $aMimeData[$sExt] ) ) )
  470. {
  471. // mime type is not allowed!
  472. $this->_sError = $this->_oForm->_text( 31 );
  473. }
  474. else
  475. {
  476. // is it an image and is a max width/height given? Get the proportions
  477. if(preg_match('/^image\//', $sType) &&
  478. !empty($this->_aConfig['height']) ||
  479. !empty($this->_aConfig['width']))
  480. {
  481. // size is incorrect.. give a size error message
  482. list($iWidth, $iHeight) = getimagesize( $aFile['tmp_name'] );
  483. if(( (int) $this->_aConfig['height'] > 0 && $iHeight > (int) $this->_aConfig['height'] ) ||
  484. ( (int) $this->_aConfig['width'] > 0 && $iWidth > (int) $this->_aConfig['width']))
  485. {
  486. $this->_sError = sprintf(
  487. $this->_oForm->_text( 32 ),
  488. (int) $this->_aConfig['width'],
  489. (int) $this->_aConfig['height'],
  490. $iWidth,
  491. $iHeight
  492. );
  493. }
  494. }
  495. }
  496. // if an error occured..
  497. if($aFile['error'] == 3)
  498. {
  499. $this->_sError = $this->_oForm->_text( 24 );
  500. }
  501. }
  502. }
  503. // when no error ocoured, the file is valid
  504. $this->_isValid = empty($this->_sError);
  505. return $this->_isValid;
  506. }
  507. /**
  508. * UploadField::doUpload()
  509. *
  510. * Upload the file
  511. *
  512. * @return string | bool: the filename of the uploaded file, or false on an error
  513. * @access public
  514. * @author Teye Heimans
  515. */
  516. function doUpload()
  517. {
  518. // alias for the file data
  519. $aFile = $this->_mValue;
  520. // is a file uploaded ?
  521. if( !is_null($aFile['error']) && $aFile['error'] != 4)
  522. {
  523. if( is_uploaded_file($aFile['tmp_name']) )
  524. {
  525. // make the dir if not exists
  526. if(!is_dir($this->_aConfig['path']))
  527. {
  528. if(!$this->_forceDir($this->_aConfig['path'], FH_DEFAULT_CHMOD))
  529. {
  530. trigger_error(
  531. "Could not upload file to dir: ".$this->_aConfig['path'].". ".
  532. "The dir does not exists and trying to make the dir failed...",
  533. E_USER_WARNING
  534. );
  535. return false;
  536. }
  537. }
  538. // the file like where going to upload it
  539. $sFilename = $this->_getFilename(false);
  540. $sUpload = $this->_aConfig['path'].$sFilename;
  541. // uploading
  542. if(!move_uploaded_file($aFile['tmp_name'], $sUpload))
  543. {
  544. trigger_error(
  545. sprintf(
  546. 'Unable to move uploaded file %s to location %s',
  547. $aFile['tmp_name'],
  548. $sUpload
  549. ),
  550. E_USER_ERROR
  551. );
  552. return false;
  553. }
  554. @chmod( $sUpload, FH_DEFAULT_CHMOD );
  555. $this->_sFilename = $sFilename;
  556. return $sUpload;
  557. }
  558. else
  559. {
  560. trigger_error(
  561. sprintf(
  562. 'Possible file upload attack: filename %s',
  563. $aFile['name']
  564. ),
  565. E_USER_WARNING
  566. );
  567. return false;
  568. }
  569. }
  570. return false;
  571. }
  572. /******* PRIVATE! *************/
  573. /**
  574. * UploadField::_forceDir()
  575. *
  576. * Create the given dir
  577. *
  578. * @param string $sPath: the path to create
  579. * @param int $mode: the chmode which should be used to create the dir
  580. * @return boolean
  581. * @access private
  582. * @author Teye Heimans
  583. */
  584. function _forceDir( $sPath, $iMode)
  585. {
  586. if ( strlen( $sPath) == 0)
  587. {
  588. return 0;
  589. }
  590. if ( strlen( $sPath) < 3)
  591. {
  592. return 1; // avoid 'xyz:\' problem.
  593. }
  594. elseif ( is_dir( $sPath ))
  595. {
  596. return 1; // avoid 'xyz:\' problem.
  597. }
  598. elseif ( dirname( $sPath) == $sPath )
  599. {
  600. return 1; // avoid 'xyz:\' problem.
  601. }
  602. return ( $this->_forceDir( dirname($sPath), $sPath) and mkdir( $sPath, $iMode));
  603. }
  604. /**
  605. * UploadField::_getFilename()
  606. *
  607. * Get the filename like we are going to save it
  608. *
  609. * @param boolean $bIgnoreRename: Ignore the rename option ?
  610. * @return string: the filename
  611. * @access private
  612. * @author Teye Heimans
  613. */
  614. function _getFilename( $bIgnoreRename = false )
  615. {
  616. // easy name to work with
  617. $sFile = $this->_mValue['name'];
  618. // get the extension of the uploaded file
  619. $sExt = $this->_getExtension( $sFile );
  620. if( !$sExt )
  621. {
  622. return null;
  623. }
  624. // use the given filename if wanted
  625. if(!empty($this->_aConfig['name']))
  626. {
  627. $sFile = $this->_aConfig['name'].'.'.$sExt;
  628. }
  629. // replace not wanted caracters
  630. $sFile = preg_replace('/\.'.$sExt.'$/i', '', $sFile); // remove extension
  631. $sFile = preg_replace("{^[\\/\*\?\:\,]+$}", '', $sFile ); // remove dangerous characters
  632. // rename when wanted
  633. if(strtolower($this->_aConfig['exists']) == 'rename' && !$bIgnoreRename)
  634. {
  635. $sPath = $this->_aConfig['path'];
  636. $sCopy = '';
  637. $i = 1;
  638. while (
  639. // file exists or ...
  640. file_exists($sPath.$sFile.$sCopy.'.'.$sExt) ||
  641. // other uploadfield has registered this filename ...
  642. !$this->_oForm->_registerFileName( $sPath.$sFile.$sCopy.'.'.$sExt, $this->_sName))
  643. {
  644. // then get a new filename
  645. $sCopy = '('.$i++ .')';
  646. }
  647. return $sFile.$sCopy.'.'.$sExt;
  648. }
  649. // no renaming wanted..
  650. else
  651. {
  652. return $sFile.'.'.$sExt;
  653. }
  654. }
  655. /**
  656. * UploadField::_getExtension()
  657. *
  658. * Retrieve the extension of the given filename
  659. *
  660. * @param string $sFilename: The filename where we have to retrieve the extension from
  661. * @return string: the extension
  662. * @access private
  663. * @author Teye Heimans
  664. */
  665. function _getExtension( $sFilename )
  666. {
  667. $sExt = substr(strrchr( $sFilename, '.'), 1);
  668. return ($sExt !== false) ? strtolower($sExt) : false;
  669. }
  670. /**
  671. * UploadField::_getMaxUploadSize()
  672. *
  673. * Get the max uploadsize
  674. *
  675. * @return integer: the max upload size
  676. * @access private
  677. * @author Teye Heimans
  678. */
  679. function _getMaxUploadSize()
  680. {
  681. static $iIniSize = false;
  682. if(!$iIniSize)
  683. {
  684. $iPost = intval($this->_iniSizeToBytes(ini_get('post_max_size')));
  685. $iUpl = intval($this->_iniSizeToBytes(ini_get('upload_max_filesize')));
  686. $iIniSize = floor(($iPost < $iUpl) ? $iPost : $iUpl);
  687. }
  688. return $iIniSize;
  689. }
  690. /**
  691. * UploadField::_iniSizeToBytes()
  692. *
  693. * Get the given size in bytes
  694. *
  695. * @param string $sIniSize: The size we have to make to bytes
  696. * @return integer: the size in bytes
  697. * @access private
  698. * @author Teye Heimans
  699. */
  700. function _iniSizeToBytes( $sIniSize )
  701. {
  702. $aIniParts = array();
  703. if (!is_string($sIniSize))
  704. {
  705. trigger_error('Argument A is not a string! dump: '.$sIniSize, E_USER_NOTICE);
  706. return false;
  707. }
  708. if (!preg_match ('/^(\d+)([bkm]*)$/i', $sIniSize, $aIniParts))
  709. {
  710. trigger_error('Argument A is not a valid php.ini size! dump: '.$sIniSize, E_USER_NOTICE);
  711. return false;
  712. }
  713. $iSize = $aIniParts[1];
  714. $sUnit = strtolower($aIniParts[2]);
  715. switch($sUnit)
  716. {
  717. case 'm':
  718. return (int)($iSize * 1048576);
  719. case 'k':
  720. return (int)($iSize * 1024);
  721. case 'b':
  722. default:
  723. return (int)$iSize;
  724. }
  725. }
  726. }
  727. ?>