Automatic pass to convert with php-cs-fixer

This commit is contained in:
2023-04-13 21:33:52 +02:00
parent 63b150a493
commit 0111c96f1d
105 changed files with 10478 additions and 8273 deletions

View File

@@ -1,161 +1,187 @@
<?php
/** DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
/**
* DomFramework
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework;
/** This class permit to create easily some forms to HTML (or text mode in
* future).
* Each field can be checked in AJAX or HTML.
*/
/**
* This class permit to create easily some forms to HTML (or text mode in
* future).
* Each field can be checked in AJAX or HTML.
*/
class Form
{
/** All the fields
*/
/**
* All the fields
*/
private $fields = null;
/** The name of the form
*/
/**
* The name of the form
*/
private $formName;
/** Allow to debug the PHP
*/
/**
* 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
*/
/**
* 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
*/
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";
/** The CSRF token value
*/
/**
* The CSRF token value
*/
private $csrfToken = "";
/** The method used to send the values
*/
/**
* The method used to send the values
*/
private $method = "post";
/** The Bootstrap width of the column of titles
*/
/**
* The Bootstrap width of the column of titles
*/
public $titlewidth = 2;
/** The Bootstrap width of the column of fields
*/
/**
* The Bootstrap width of the column of fields
*/
public $fieldwidth = 10;
/** Define a class for form object
*/
/**
* Define a class for form object
*/
public $formClass = "form-horizontal";
/** The logging callable method
*/
/**
* The logging callable method
*/
private $loggingCallable = null;
/** The logging basemsg
*/
/**
* The logging basemsg
*/
private $loggingBasemsg = "";
/** Form template (Bootstrap3 by default)
*/
/**
* Form template (Bootstrap3 by default)
*/
private $formTemplate = "Bootstrap3";
/** Create a form
* @param string|null $formName The form name
*/
/**
* Create a form
* @param string|null $formName The form name
*/
public function __construct($formName = "form")
{
$this->formName = $formName;
}
// The setters of the properties
/** Set the debug level
* @param integer $val The debug value
*/
/**
* Set the debug level
* @param integer $val The debug value
*/
public function debug($val)
{
$this->debug = $val;
return $this;
}
/** Set the csrf enable
* @param integer $val The csrf check
*/
/**
* Set the csrf enable
* @param integer $val The csrf check
*/
public function csrf($val)
{
$this->csrf = !! $val;
return $this;
}
/** Set the method
* @param string $val The method to use
*/
/**
* Set the method
* @param string $val The method to use
*/
public function method($val)
{
$this->method = strtolower($val);
return $this;
}
/** Set the csrf token name
* @param integer $val The csrf token name
*/
/**
* Set the csrf token name
* @param integer $val The csrf token name
*/
public function csrfField($val)
{
$this->csrfField = $val;
return $this;
}
/** Set the titlewidth
* @param integer $val The titlewidth
*/
/**
* Set the titlewidth
* @param integer $val The titlewidth
*/
public function titlewidth($val)
{
$this->titlewidth = $val;
return $this;
}
/** Set the fieldwidth
* @param integer $val The fieldwidth
*/
/**
* Set the fieldwidth
* @param integer $val The fieldwidth
*/
public function fieldwidth($val)
{
$this->fieldwidth = $val;
return $this;
}
/** Set the formClass
* @param integer $val The formClass
*/
/**
* Set the formClass
* @param integer $val The formClass
*/
public function formClass($val)
{
$this->formClass = $val;
return $this;
}
/** Set logging class an method
* @param callable $loggingCallable The callable function. This method will
* receive two params : the LOG level (LOG_ERROR...) and the message
* @param string|null $loggingBasemsg The basemsg added at the beginning of
* the log
*/
/**
* Set logging class an method
* @param callable $loggingCallable The callable function. This method will
* receive two params : the LOG level (LOG_ERROR...) and the message
* @param string|null $loggingBasemsg The basemsg added at the beginning of
* the log
*/
public function logging($loggingCallable, $loggingBasemsg = "")
{
$this->loggingCallable = $loggingCallable;
$this->loggingBasemsg = $loggingBasemsg;
}
/** Set the Form Templating to use.
* Can be : Bootstrap3, Bootstrap4 (later Bulma)
* @param string $formTemplate The template to use
*/
/**
* Set the Form Templating to use.
* Can be : Bootstrap3, Bootstrap4 (later Bulma)
* @param string $formTemplate The template to use
*/
public function formTemplate($formTemplate)
{
if (
! in_array(
$formTemplate,
array("Bootstrap3", "Bootstrap4")
["Bootstrap3", "Bootstrap4"],
true
)
) {
throw new \Exception("Unknown formTemplate provided", 500);
@@ -164,10 +190,11 @@ class Form
return $this;
}
/** The private method to log if the $this->loggingCallable is defined
* @param integer $prio The priority of the message
* @param string $msg The message to store
*/
/**
* The private method to log if the $this->loggingCallable is defined
* @param integer $prio The priority of the message
* @param string $msg The message to store
*/
private function loggingCallable($prio, $msg)
{
if (! is_callable($this->loggingCallable)) {
@@ -180,55 +207,58 @@ class Form
call_user_func($this->loggingCallable, $prio, $base . $msg);
}
/** 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
*/
/**
* 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
*/
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
* @param object $field The field to add
*/
/**
* Add a field to the form. For the details of a field, see the description
* in fields method
* @param object $field The field to add
*/
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
*/
/**
* 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
*/
public function values()
{
$values = array();
$values = [];
if ($this->method === "post") {
if (isset($_POST[$this->formName])) {
$values = $_POST[$this->formName];
@@ -283,20 +313,21 @@ class Form
return $values;
}
/** 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
*/
/**
* 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
*/
public function printHTML(
$method = 'post',
$values = null,
$errors = array()
$errors = []
) {
if (count($this->fields) === 0) {
$this->loggingCallable(
@@ -360,7 +391,7 @@ class Form
if (is_array($errors[$field->name])) {
$field->errors = $errors[$field->name];
} else {
$field->errors = array("error", $errors[$field->name]);
$field->errors = ["error", $errors[$field->name]];
}
if ($field->type === "hidden") {
$field->type = "text";
@@ -421,9 +452,10 @@ class Form
return $res;
}
/** Check the token from the user
* @param string $tokenFromUser The value form the user's token
*/
/**
* Check the token from the user
* @param string $tokenFromUser The value form the user's token
*/
public function checkToken($tokenFromUser)
{
$csrf = new Csrf();
@@ -433,8 +465,9 @@ class Form
$csrf->checkThenDeleteToken($tokenFromUser);
}
/** Return the token generated in form
*/
/**
* Return the token generated in form
*/
public function getToken()
{
if ($this->csrfToken === "") {
@@ -443,22 +476,23 @@ class Form
return $this->csrfToken;
}
/** Check if the parameters are correct with the defined fields
* Need the session !
* @param array $values The values to check
* @param array|null $fields The fields definition (or use the session
* stored one if the value is null)
* @return array containing the errors
*/
public function verify($values, $fields = array())
/**
* Check if the parameters are correct with the defined fields
* Need the session !
* @param array $values The values to check
* @param array|null $fields The fields definition (or use the session
* stored one if the value is null)
* @return array containing the errors
*/
public function verify($values, $fields = [])
{
if (count($fields) === 0) {
if (! isset($_SESSION["domframework"]["form"]["fields"])) {
return array();
return [];
}
$fields = $_SESSION["domframework"]["form"]["fields"];
}
$errors = array();
$errors = [];
foreach ($fields as $field) {
if (
$field->mandatory !== null &&
@@ -474,25 +508,26 @@ class Form
return $errors;
}
/** If there is at least one error reported in $errors, save the old values
* and the errors in the session, and redirect to the provided url.
* If there is no error, do nothing
* @param array $values The values of the fields filled by the user
* @param array $errors The errors detected by a verify
* @param object $route the route object
* @param string|null $url The URL to redirect. If not provided, use the
* $route->requestURL () method to found the calling page
*
* Example :
$form = new \Domframework\form ();
$form->logging (array ('\apps\general\controllers\logging', 'log'),
$authHTML["email"]);
$values = $form->values ();
$errors = $spaceObj->verify ($values);
$form->redirectIfError ($values, $errors, $route, "/admin/space/");
$spaceuuid = $spaceObj->spaceCreateConceal ($values["spacename"]);
$route->redirect ("/admin/space/");
*/
/**
* If there is at least one error reported in $errors, save the old values
* and the errors in the session, and redirect to the provided url.
* If there is no error, do nothing
* @param array $values The values of the fields filled by the user
* @param array $errors The errors detected by a verify
* @param object $route the route object
* @param string|null $url The URL to redirect. If not provided, use the
* $route->requestURL () method to found the calling page
*
* Example :
* $form = new \Domframework\form ();
* $form->logging (array ('\apps\general\controllers\logging', 'log'),
* $authHTML["email"]);
* $values = $form->values ();
* $errors = $spaceObj->verify ($values);
* $form->redirectIfError ($values, $errors, $route, "/admin/space/");
* $spaceuuid = $spaceObj->spaceCreateConceal ($values["spacename"]);
* $route->redirect ("/admin/space/");
*/
public function redirectIfError($values, $errors, $route, $url = "")
{
$this->saveValuesErrors($values, $errors);
@@ -505,13 +540,14 @@ class Form
$this->saveValuesErrorsReset();
}
/** Save the values and errors to be displayed in the next page if the session
* is available
* Need the session to work
* @param array $values The values of the fields filled by the user
* @param array|null $errors The errors detected by a verify
*/
public function saveValuesErrors($values, $errors = array())
/**
* Save the values and errors to be displayed in the next page if the session
* is available
* Need the session to work
* @param array $values The values of the fields filled by the user
* @param array|null $errors The errors detected by a verify
*/
public function saveValuesErrors($values, $errors = [])
{
if (isset($_SESSION)) {
$_SESSION["domframework"]["form"][$this->formName]["values"] = $values;
@@ -519,20 +555,22 @@ class Form
}
}
/** Reset the saved values to provide a clean form next page
* Need the session to work
*/
/**
* Reset the saved values to provide a clean form next page
* Need the session to work
*/
public function saveValuesErrorsReset()
{
unset($_SESSION["domframework"]["form"][$this->formName]["values"]);
unset($_SESSION["domframework"]["form"][$this->formName]["errors"]);
}
/** Get the stored values if there is one. If there is no stored values,
* return the values provided as parameter
* @param array $values The values returned if there is no stored values
* @return array The values to use
*/
/**
* Get the stored values if there is one. If there is no stored values,
* return the values provided as parameter
* @param array $values The values returned if there is no stored values
* @return array The values to use
*/
public function getOldValues($values)
{
if (isset($_SESSION["domframework"]["form"][$this->formName]["values"])) {
@@ -542,11 +580,12 @@ class Form
return $values;
}
/** Get the stored errors if there is one. If there is no sorted errors,
* return the errors provided as parameter
* @param array $errors The values returned if there is no stored values
* @return array The errors to use
*/
/**
* Get the stored errors if there is one. If there is no sorted errors,
* return the errors provided as parameter
* @param array $errors The values returned if there is no stored values
* @return array The errors to use
*/
public function getOldErrors($errors)
{
if (isset($_SESSION["domframework"]["form"][$this->formName]["errors"])) {
@@ -556,15 +595,16 @@ class Form
return $errors;
}
/** Convert Date received in one format to another.
* If the provided string is not corresponding to the format, don't change
* anything.
* Format used http://php.net/manual/en/datetime.createfromformat.php
* @param string $inputDate The date to modify
* @param string $inputFormat The input format of the date
* @param string $outputFormat The output format of the date
* @return string
*/
/**
* Convert Date received in one format to another.
* If the provided string is not corresponding to the format, don't change
* anything.
* Format used http://php.net/manual/en/datetime.createfromformat.php
* @param string $inputDate The date to modify
* @param string $inputFormat The input format of the date
* @param string $outputFormat The output format of the date
* @return string
*/
public function convertDate($inputDate, $inputFormat, $outputFormat)
{
$date = \DateTime::CreateFromFormat($inputFormat, $inputDate);