form : update docComment
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2727 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
122
form.php
122
form.php
@@ -4,8 +4,8 @@
|
||||
@author Dominique Fournier <dominique@fournier38.fr> */
|
||||
|
||||
/** This class permit to create easily some forms to HTML (or text mode in
|
||||
future).
|
||||
Each field can be checked in AJAX or HTML. */
|
||||
* future).
|
||||
* Each field can be checked in AJAX or HTML. */
|
||||
class form
|
||||
{
|
||||
|
||||
@@ -16,9 +16,9 @@ class form
|
||||
/** Allow to debug the PHP */
|
||||
public $debug=0;
|
||||
/** CSRF protection
|
||||
By default, the CSRF protection is active if a SESSION is active too.
|
||||
It can be disabled if needed. An Exception is raised if the form is send
|
||||
back without the token */
|
||||
* By default, the CSRF protection is active if a SESSION is active too.
|
||||
* It can be disabled if needed. An Exception is raised if the form is send
|
||||
* back without the token */
|
||||
public $csrf=TRUE;
|
||||
/** Name of the CSRF hidden field in HTML page */
|
||||
public $csrfField = "CSRF_TOKEN";
|
||||
@@ -32,7 +32,7 @@ class form
|
||||
public $fieldwidth = 10;
|
||||
|
||||
/** Create a form
|
||||
@param string|null $formName The form name
|
||||
* @param string|null $formName The form name
|
||||
*/
|
||||
public function __construct ($formName = "form")
|
||||
{
|
||||
@@ -40,48 +40,48 @@ class form
|
||||
}
|
||||
|
||||
/** Save the array of fields into the structure.
|
||||
Available :
|
||||
- name : name of the field in the HTML page
|
||||
- label : label written to the describe the field
|
||||
- [titles] : text written in radio/checkboxes
|
||||
- [defaults] : default values. Must be array for checkbox/select, and
|
||||
string for others
|
||||
- [type] : text, password, hidden, checkbox, select, radio, submit,
|
||||
textarea
|
||||
text by default
|
||||
- [help] : The Help message (written below the field). Overwrited in
|
||||
case of error
|
||||
- [multiple] : Multiple selection are possible (if the type supports it)
|
||||
- [group] : define a fieldset and define the title with groupe name
|
||||
Warning : all the elements of the same group must be
|
||||
consecutive !
|
||||
- [readonly] : put a read-only flag on the field (the user see it but
|
||||
can't interract on it. The value will be sent to next
|
||||
page
|
||||
- [mandatory] : boolean to add a red star at end of label
|
||||
- [hidden] : hide the field (add a style='display:hidden' to the field)
|
||||
- [maxlength] : the maximum length of the content of the field in chars
|
||||
- [rows] : Number of rows
|
||||
- [cols] : Number of columns
|
||||
- [placeholder] : The text to be displayed in the placeholder
|
||||
|
||||
@param array $fields The fields to be displayed
|
||||
*/
|
||||
* Available :
|
||||
* - name : name of the field in the HTML page
|
||||
* - label : label written to the describe the field
|
||||
* - [titles] : text written in radio/checkboxes
|
||||
* - [defaults] : default values. Must be array for checkbox/select, and
|
||||
* string for others
|
||||
* - [type] : text, password, hidden, checkbox, select, radio, submit,
|
||||
* textarea
|
||||
* text by default
|
||||
* - [help] : The Help message (written below the field). Overwrited in
|
||||
* case of error
|
||||
* - [multiple] : Multiple selection are possible (if the type supports it)
|
||||
* - [group] : define a fieldset and define the title with groupe name
|
||||
* Warning : all the elements of the same group must be
|
||||
* consecutive !
|
||||
* - [readonly] : put a read-only flag on the field (the user see it but
|
||||
* can't interract on it. The value will be sent to next
|
||||
* page
|
||||
* - [mandatory] : boolean to add a red star at end of label
|
||||
* - [hidden] : hide the field (add a style='display:hidden' to the field)
|
||||
* - [maxlength] : the maximum length of the content of the field in chars
|
||||
* - [rows] : Number of rows
|
||||
* - [cols] : Number of columns
|
||||
* - [placeholder] : The text to be displayed in the placeholder
|
||||
*
|
||||
* @param array $fields The fields to be displayed
|
||||
*/
|
||||
public function fields ($fields)
|
||||
{
|
||||
$this->fields = $fields;
|
||||
}
|
||||
|
||||
/** Add a field to the form. For the details of a field, see the description
|
||||
in fields method */
|
||||
* in fields method */
|
||||
public function addfield ($field)
|
||||
{
|
||||
$this->fields[] = $field;
|
||||
}
|
||||
|
||||
/** Return the values provided by the user. Test the CSRF before continue
|
||||
NEVER read the values from $_POST in your codes or CSRF will not be
|
||||
checked */
|
||||
* NEVER read the values from $_POST in your codes or CSRF will not be
|
||||
* checked */
|
||||
public function values ()
|
||||
{
|
||||
$values = array ();
|
||||
@@ -120,14 +120,14 @@ class form
|
||||
}
|
||||
|
||||
/** Return the fields in HTML code. If $values is provided, use it in place
|
||||
of default values. In case of select boxes, $values are the selected
|
||||
elements
|
||||
$method is the method written in method field of <form>
|
||||
@param string|null $method The method to use to transmit the form (POST,
|
||||
GET)
|
||||
@param array|null $values The default values of the fields
|
||||
@param array|null $errors The fields to put in error with the associated
|
||||
message */
|
||||
* of default values. In case of select boxes, $values are the selected
|
||||
* elements
|
||||
* $method is the method written in method field of <form>
|
||||
* @param string|null $method The method to use to transmit the form (POST,
|
||||
* GET)
|
||||
* @param array|null $values The default values of the fields
|
||||
* @param array|null $errors The fields to put in error with the associated
|
||||
* message */
|
||||
public function printHTML ($method = 'post', $values = NULL,
|
||||
$errors = array())
|
||||
{
|
||||
@@ -214,14 +214,14 @@ class form
|
||||
return $res;
|
||||
}
|
||||
|
||||
/** Check the token from the user
|
||||
@param string $tokenFromUser The value form the user's token */
|
||||
public function checkToken ($tokenFromUser)
|
||||
{
|
||||
$csrf = new csrf ();
|
||||
$csrf->field = $this->csrfField;
|
||||
$csrf->checkToken ($tokenFromUser);
|
||||
}
|
||||
/** Check the token from the user
|
||||
* @param string $tokenFromUser The value form the user's token */
|
||||
public function checkToken ($tokenFromUser)
|
||||
{
|
||||
$csrf = new csrf ();
|
||||
$csrf->field = $this->csrfField;
|
||||
$csrf->checkToken ($tokenFromUser);
|
||||
}
|
||||
|
||||
/** Check if the parameters are correct with the defined fields
|
||||
* Need the session !
|
||||
@@ -317,7 +317,7 @@ class formfield
|
||||
/** The type of the field (text, password, checkbox, select)*/
|
||||
public $type="text";
|
||||
/** Allow a help message to be displayed below the field. In case of error,
|
||||
it is overrided by the error message */
|
||||
* it is overrided by the error message */
|
||||
public $help;
|
||||
/** The multiplicity of selection of the field (available in select only)*/
|
||||
public $multiple;
|
||||
@@ -338,8 +338,8 @@ class formfield
|
||||
/** The Bootstrap width of the column of fields */
|
||||
public $fieldwidth = 10;
|
||||
/** When adding a field, the name and the label are the minimum mandatory
|
||||
@param string $name Name of the field
|
||||
@param string $label Label of the field */
|
||||
* @param string $name Name of the field
|
||||
* @param string $label Label of the field */
|
||||
public function __construct ($name, $label)
|
||||
{
|
||||
$this->name = $name;
|
||||
@@ -893,9 +893,9 @@ class formfield
|
||||
}
|
||||
|
||||
/** CSRF protection
|
||||
By default, the CSRF protection is active if a SESSION is active too.
|
||||
It can be disabled if needed. An Exception is raised if the form is send
|
||||
back without the token */
|
||||
* By default, the CSRF protection is active if a SESSION is active too.
|
||||
* It can be disabled if needed. An Exception is raised if the form is send
|
||||
* back without the token */
|
||||
class csrf
|
||||
{
|
||||
/** Allow to disable the csrf protection */
|
||||
@@ -905,7 +905,7 @@ class csrf
|
||||
/** The created token */
|
||||
private $csrfToken = "";
|
||||
/** Timeout of the CSRF token : 3600s by default (maximum time allowed to
|
||||
enter information in form and submit) */
|
||||
* enter information in form and submit) */
|
||||
private $csrfTimeout = 3600;
|
||||
|
||||
/** This function return the token */
|
||||
@@ -923,8 +923,8 @@ class csrf
|
||||
}
|
||||
|
||||
/** Check if the provided token is the right token, defined last displayed
|
||||
page
|
||||
@param string $tokenFromUser The value form the user's token */
|
||||
* page
|
||||
* @param string $tokenFromUser The value form the user's token */
|
||||
public function checkToken ($tokenFromUser)
|
||||
{
|
||||
if ($this->csrf === FALSE )
|
||||
|
||||
Reference in New Issue
Block a user