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