Fix file rules. Needs to reload autoload.

1.10.x
Julio Montoya 11 years ago
parent 0c71327813
commit dfdc842b8f
  1. 7
      main/exercice/hotspot.class.php
  2. 26
      main/inc/lib/formvalidator/Rule/FileName.php
  3. 28
      main/inc/lib/formvalidator/Rule/MaxFileSize.php
  4. 29
      main/inc/lib/formvalidator/Rule/MimeType.php
  5. 27
      main/inc/lib/formvalidator/Rule/UploadFile.php
  6. 6
      main/inc/lib/pear/HTML/QuickForm/RuleRegistry.php
  7. 73
      main/inc/lib/pear/HTML/QuickForm/file.php

@ -25,10 +25,13 @@ class HotSpot extends Question
{
}
function createForm (&$form, $fck_config=0)
/**
* @param FormValidator $form
* @param int $fck_config
*/
public function createForm (&$form, $fck_config=0)
{
parent::createForm($form, $fck_config);
global $text, $class;
if (!isset($_GET['editQuestion'])) {
$form->addElement('file','imageUpload',array('<img src="../img/hotspots.png" />', get_lang('UploadJpgPicture')) );

@ -0,0 +1,26 @@
<?php
/* For licensing terms, see /license.txt */
/** @author Julio Montoya */
/**
* Class HTML_QuickForm_Rule_FileName
*/
class HTML_QuickForm_Rule_FileName extends HTML_QuickForm_Rule
{
/**
* @param $value array Uploaded file info (from $_FILES)
* @param null $options
* @return bool
*/
public function validate($value, $options = null)
{
if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
(!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) {
return is_uploaded_file($elementValue['tmp_name']);
} else {
return false;
}
}
}

@ -0,0 +1,28 @@
<?php
/* For licensing terms, see /license.txt */
/** @author Julio Montoya */
/**
* Class HTML_QuickForm_Rule_MaxFileSize
*/
class HTML_QuickForm_Rule_MaxFileSize extends HTML_QuickForm_Rule
{
/**
* @param $value array Uploaded file info (from $_FILES)
* @param null $options
* @return bool
*/
public function validate($elementValue, $maxSize)
{
if (!empty($elementValue['error']) &&
(UPLOAD_ERR_FORM_SIZE == $elementValue['error'] || UPLOAD_ERR_INI_SIZE == $elementValue['error'])
) {
return false;
}
if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
return true;
}
return ($maxSize >= @filesize($elementValue['tmp_name']));
}
}

@ -0,0 +1,29 @@
<?php
/* For licensing terms, see /license.txt */
/** @author Julio Montoya */
/**
* Class HTML_QuickForm_Rule_MimeType
*/
class HTML_QuickForm_Rule_MimeType extends HTML_QuickForm_Rule
{
/**
* Checks if the given element contains an uploaded file of the right mime type
*
* @param array Uploaded file info (from $_FILES)
* @param mixed Mime Type (can be an array of allowed types)
* @access private
* @return bool true if mimetype is correct, false otherwise
*/
public function validate($elementValue, $mimeType)
{
if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
return true;
}
if (is_array($mimeType)) {
return in_array($elementValue['type'], $mimeType);
}
return $elementValue['type'] == $mimeType;
} // end func _ruleCheckMimeType
}

@ -0,0 +1,27 @@
<?php
/* For licensing terms, see /license.txt */
/** @author Julio Montoya */
/**
* Class HTML_QuickForm_Rule_UploadFile
*/
class HTML_QuickForm_Rule_UploadFile extends HTML_QuickForm_Rule
{
/**
* Checks if the given element contains an uploaded file of the filename regex
*
* @param array Uploaded file info (from $_FILES)
* @param string Regular expression
* @access private
* @return bool true if name matches regex, false otherwise
*/
public function validate($elementValue, $regex)
{
if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
(!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')) {
return is_uploaded_file($elementValue['tmp_name']);
} else {
return false;
}
} // end func _ruleCheckFileName
}

@ -152,7 +152,11 @@ class HTML_QuickForm_RuleRegistry
'html' => 'HTML_QuickForm_Rule_HTML',
'CAPTCHA' => 'HTML_QuickForm_Rule_CAPTCHA',
'date' => 'HTML_QuickForm_Rule_Date',
'compare_datetime_text' => 'HTML_QuickForm_Rule_CompareDateTimeText'
'compare_datetime_text' => 'HTML_QuickForm_Rule_CompareDateTimeText',
'uploadedfile' => 'HTML_QuickForm_Rule_UploadFile',
'maxfilesize', 'HTML_QuickForm_Rule_MaxFileSize',
'mimetype', 'HTML_QuickForm_Rule_MimeType',
'filename', 'HTML_QuickForm_Rule_FileName'
);
$class = $rules[$ruleName];

@ -23,14 +23,6 @@
* @link http://pear.php.net/package/HTML_QuickForm
*/
// register file-related rules
if (class_exists('HTML_QuickForm')) {
HTML_QuickForm::registerRule('uploadedfile', 'callback', '_ruleIsUploadedFile', 'HTML_QuickForm_file');
HTML_QuickForm::registerRule('maxfilesize', 'callback', '_ruleCheckMaxFileSize', 'HTML_QuickForm_file');
HTML_QuickForm::registerRule('mimetype', 'callback', '_ruleCheckMimeType', 'HTML_QuickForm_file');
HTML_QuickForm::registerRule('filename', 'callback', '_ruleCheckFileName', 'HTML_QuickForm_file');
}
/**
* HTML class for a file upload field
*
@ -221,6 +213,7 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
return HTML_QuickForm_file::_ruleIsUploadedFile($this->_value);
} // end func isUploadedFile
// }}}
// {{{ _ruleIsUploadedFile()
@ -244,70 +237,6 @@ class HTML_QuickForm_file extends HTML_QuickForm_input
// }}}
// {{{ _ruleCheckMaxFileSize()
/**
* Checks that the file does not exceed the max file size
*
* @param array Uploaded file info (from $_FILES)
* @param int Max file size
* @access private
* @return bool true if filesize is lower than maxsize, false otherwise
*/
function _ruleCheckMaxFileSize($elementValue, $maxSize)
{
if (!empty($elementValue['error']) &&
(UPLOAD_ERR_FORM_SIZE == $elementValue['error'] || UPLOAD_ERR_INI_SIZE == $elementValue['error'])) {
return false;
}
if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
return true;
}
return ($maxSize >= @filesize($elementValue['tmp_name']));
} // end func _ruleCheckMaxFileSize
// }}}
// {{{ _ruleCheckMimeType()
/**
* Checks if the given element contains an uploaded file of the right mime type
*
* @param array Uploaded file info (from $_FILES)
* @param mixed Mime Type (can be an array of allowed types)
* @access private
* @return bool true if mimetype is correct, false otherwise
*/
function _ruleCheckMimeType($elementValue, $mimeType)
{
if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
return true;
}
if (is_array($mimeType)) {
return in_array($elementValue['type'], $mimeType);
}
return $elementValue['type'] == $mimeType;
} // end func _ruleCheckMimeType
// }}}
// {{{ _ruleCheckFileName()
/**
* Checks if the given element contains an uploaded file of the filename regex
*
* @param array Uploaded file info (from $_FILES)
* @param string Regular expression
* @access private
* @return bool true if name matches regex, false otherwise
*/
function _ruleCheckFileName($elementValue, $regex)
{
if (!HTML_QuickForm_file::_ruleIsUploadedFile($elementValue)) {
return true;
}
return (bool)preg_match($regex, $elementValue['name']);
} // end func _ruleCheckFileName
// }}}
// {{{ _findValue()
/**
* Tries to find the element value from the values array
*

Loading…
Cancel
Save