From 49e6128951e8d8b340ea6027735c8b3566c44b6b Mon Sep 17 00:00:00 2001 From: M Date: Thu, 3 Dec 2015 22:11:13 -0500 Subject: + Started Form class definition --- app/class/form.class.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/class/form.class.php (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php new file mode 100644 index 0000000..e398690 --- /dev/null +++ b/app/class/form.class.php @@ -0,0 +1,35 @@ +textFields = array(); + + $this->errorlist = array(); + $this->warninglist = array(); + $this->noticelist = array(); + } + + /* + * Add new text field to the form + */ + function field_text($name, $req = true) + { + if ($req !== true) + $req = false; + + $this->textFields[] = array( + 'name' => $name, + 'req' => $req + ); + } +} + +?> -- cgit v1.2.3 From 59962f7c260aaa0661b0c811e6b553d1a850032b Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 15:03:48 -0500 Subject: + Added numeric and enum types to Form class --- app/class/form.class.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index e398690..ffee3d7 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -11,6 +11,8 @@ class Form function __construct() { $this->textFields = array(); + $this->numbFields = array(); + $this->enumFields = array(); $this->errorlist = array(); $this->warninglist = array(); @@ -30,6 +32,41 @@ class Form 'req' => $req ); } + + /* + * Add new numeric field to the form + */ + function field_numeric($name, $req = true, $integer = true, $min = null, $max = null) + { + if ($req !== true) + $req = false; + + if ($integer !== true) + $integer = false; + + $this->numbFields[] = array( + 'name' => $name, + 'req' => $req, + 'int' => $integer, + 'min' => $min, + 'max' => $max + ); + } + + /* + * Add new enumeration field to the form + */ + function field_enum($name, $req = true, $values) + { + if ($req !== true) + $req = false; + + $this->enumFields[] = array( + 'name' => $name, + 'req' => $req, + 'vals' => $values + ); + } } ?> -- cgit v1.2.3 From 91659b121e63735a7620663c0f43f5c5adef77d4 Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 18:54:01 -0500 Subject: + Implemented populate function in Form class + Added helper function in Form class, logError ! Finished Form class for now --- app/class/form.class.php | 100 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index ffee3d7..502e348 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -14,9 +14,15 @@ class Form $this->numbFields = array(); $this->enumFields = array(); - $this->errorlist = array(); - $this->warninglist = array(); - $this->noticelist = array(); + $this->errorlist = array(); + } + + /* + * Log an error + */ + function logError($str) + { + $this->errorlist[] = $str; } /* @@ -67,6 +73,94 @@ class Form 'vals' => $values ); } + + /* + * Populate the form with input data from web page + */ + function populate($input) + { + /* detect duplicate names */ + $names = array(); + foreach ($this->textFields as $fld) + $names[] = $fld['name']; + foreach ($this->numbFields as $fld) + $names[] = $fld['name']; + foreach ($this->enumFields as $fld) + $names[] = $fld['name']; + + if (count(array_unique($names)) != count($names)) + { + $this->logError("Internal error: Duplicate field names defined in form"); + return false; + } + + /* init text fields */ + foreach ($this->textFields as $fld) + { + if (isset($input[$fld['name']])) + $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* init numeric fields */ + foreach ($this->numbFields as $fld) + { + if (isset($input[$fld['name']])) + { + if (!is_numeric($input[$fld['name']])) + { + $this->logError($fld['name'] . " must be numeric"); + continue; + } + + if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']])) + { + $this->logError($fld['name'] . " must be an integer"); + continue; + } + + if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min'])) + { + $this->logError($fld['name'] . " must be no less than " . $fld['min']); + continue; + } + + if (!is_null($fld['max']) && ($input[$fld['name']] > $fld['max'])) + { + $this->logError($fld['name'] . " must be no more than " . $fld['max']); + continue; + } + + $this->$fld['name'] = $input[$fld['name']]; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* init enum fields */ + foreach ($this->enumFields as $fld) + { + if (isset($input[$fld['name']])) + { + if (array_search($input[$fld['name']], $fld['vals']) === false) + { + $this->logError($fld['name'] . " is not an appropriate value"); + continue; + } + + $this->$fld['name'] = $input[$fld['name']]; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* return */ + return count($this->errorlist) == 0; + } } ?> -- cgit v1.2.3 From 9bab1e5c3d7dae9603c5f2172b2a620465caab0e Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 21:37:03 -0500 Subject: * Form class fields now have the ability to set a default value. Default value is applied if the supplied $input array has no key matching the field name. --- app/class/form.class.php | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 502e348..e50876d 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -28,21 +28,22 @@ class Form /* * Add new text field to the form */ - function field_text($name, $req = true) + function field_text($name, $deflt = null, $req = true) { if ($req !== true) $req = false; $this->textFields[] = array( - 'name' => $name, - 'req' => $req + 'name' => $name, + 'deflt' => $deflt, + 'req' => $req ); } /* * Add new numeric field to the form */ - function field_numeric($name, $req = true, $integer = true, $min = null, $max = null) + function field_numeric($name, $min = null, $max = null, $deflt = null, $integer = true, $req = true) { if ($req !== true) $req = false; @@ -51,26 +52,28 @@ class Form $integer = false; $this->numbFields[] = array( - 'name' => $name, - 'req' => $req, - 'int' => $integer, - 'min' => $min, - 'max' => $max + 'name' => $name, + 'min' => $min, + 'max' => $max, + 'deflt' => $deflt, + 'int' => $integer, + 'req' => $req ); } /* * Add new enumeration field to the form */ - function field_enum($name, $req = true, $values) + function field_enum($name, $values, $deflt = null, $req = true) { if ($req !== true) $req = false; $this->enumFields[] = array( - 'name' => $name, - 'req' => $req, - 'vals' => $values + 'name' => $name, + 'vals' => $values, + 'deflt' => $deflt, + 'req' => $req ); } @@ -100,6 +103,9 @@ class Form if (isset($input[$fld['name']])) $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } @@ -136,6 +142,9 @@ class Form $this->$fld['name'] = $input[$fld['name']]; } + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } @@ -154,6 +163,9 @@ class Form $this->$fld['name'] = $input[$fld['name']]; } + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } -- cgit v1.2.3 From e6f3bf746fbb1d4c768a1d43e2a0233d0fb25f47 Mon Sep 17 00:00:00 2001 From: M Date: Sun, 6 Dec 2015 00:12:16 -0500 Subject: * Bug fix in Form class - populate function -- If a field was set in $input, but equal to "", the isset check would not behave as expected --- app/class/form.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index e50876d..d3af399 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -100,7 +100,7 @@ class Form /* init text fields */ foreach ($this->textFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); else if (!is_null($fld['deflt'])) @@ -113,7 +113,7 @@ class Form /* init numeric fields */ foreach ($this->numbFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") { if (!is_numeric($input[$fld['name']])) { @@ -152,7 +152,7 @@ class Form /* init enum fields */ foreach ($this->enumFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") { if (array_search($input[$fld['name']], $fld['vals']) === false) { -- cgit v1.2.3 From 2896ade5e1257045513f871d59e6e4eaac27e317 Mon Sep 17 00:00:00 2001 From: M Date: Tue, 8 Dec 2015 18:51:20 -0500 Subject: + Added bool field type to Form class --- app/class/form.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index d3af399..808de27 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -77,6 +77,14 @@ class Form ); } + /* + * Add new boolean field to the form + */ + function field_bool($name) + { + $this->field_enum($name, array("true", "false"), "false"); + } + /* * Populate the form with input data from web page */ -- cgit v1.2.3 From cc755e3756e43109d0db0de963b3a132039456b1 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 12 Mar 2016 15:05:12 -0500 Subject: Alter representation of form boolean values Changed how Form() objects model true and false for boolean fields. Was "true" and "false", is now "1" and "0", respectivly. This is to address how Mysql handles these values as they are pushed to the db. --- app/class/form.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 808de27..9f103ba 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -82,7 +82,7 @@ class Form */ function field_bool($name) { - $this->field_enum($name, array("true", "false"), "false"); + $this->field_enum($name, array("1", "0"), "0"); } /* -- cgit v1.2.3 From 8ad6e8f9223bd3ee214478b3e1247f9c7d8e91ec Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 27 Mar 2016 22:32:25 -0400 Subject: Add form field type 'file' Add the Form::field_file() function to allow form handlers to specify they expect to receive file from the end-user. This adds data about the file field to the form, but does not yet handle it in the populate function --- app/class/form.class.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 9f103ba..e748afc 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -13,6 +13,7 @@ class Form $this->textFields = array(); $this->numbFields = array(); $this->enumFields = array(); + $this->fileFields = array(); $this->errorlist = array(); } @@ -85,6 +86,22 @@ class Form $this->field_enum($name, array("1", "0"), "0"); } + /* + * Add new file field to the form + */ + function field_file($name, $maxsize, $allowed_mime = null, $req = false) + { + if ($req !== true) + $req = false; + + $this->fileFields[] = array( + 'name' => $name, + 'maxsize' => $maxsize, + 'mime' => $allowed_mime, + 'req' => $req + ); + } + /* * Populate the form with input data from web page */ -- cgit v1.2.3 From 1f8b53e426b8c0a1546e9d5c21573be9003cb556 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 27 Mar 2016 23:59:08 -0400 Subject: Update function Form::populate() to initialize fields added with Form::field_file() Set $form->[name] for each file field type setup on the form. --- app/class/form.class.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index e748afc..529c480 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -105,7 +105,7 @@ class Form /* * Populate the form with input data from web page */ - function populate($input) + function populate($input, $files = null) { /* detect duplicate names */ $names = array(); @@ -115,6 +115,8 @@ class Form $names[] = $fld['name']; foreach ($this->enumFields as $fld) $names[] = $fld['name']; + foreach ($this->fileFields as $fld) + $names[] = $fld['name']; if (count(array_unique($names)) != count($names)) { @@ -195,6 +197,38 @@ class Form $this->logError($fld['name'] . " is required"); } + /* init file fields */ + foreach ($this->fileFields as $fld) + { + if (!is_null($files) && isset($files[$fld['name']])) + { + $file = $files[$fld['name']]; + + if ($file['error'] > 0) + { + $this->logError("An unknown error occurred"); + continue; + } + + if ($file['size'] > $fld['maxsize']) + { + $this->logError("File must be no larger than " . $fld['maxsize'] . " bytes"); + continue; + } + + if (is_array($fld['mime']) && array_search($file['type'], $fld['mime']) === false) + { + $this->logError("File type is not supported"); + continue; + } + + $this->$fld['name'] = $file; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + /* return */ return count($this->errorlist) == 0; } -- cgit v1.2.3 From 3d493fc75dc6e3593001c2d9dfef26f4c1d79c2c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Tue, 29 Mar 2016 19:59:36 -0400 Subject: Add function Form::saveUploadedFile() Added function to form class to move tmp uploaded files to permanent storage --- app/class/form.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 529c480..3f28a36 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -232,6 +232,14 @@ class Form /* return */ return count($this->errorlist) == 0; } + + /* + * Save file uploaded through web form + */ + function saveUploadedFile($file, $filename) + { + return move_uploaded_file($file['tmp_name'], $filename); + } } ?> -- cgit v1.2.3 From dbf4512ce48c04af4a8332cff3064f29b5690446 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 27 Apr 2016 20:41:35 -0400 Subject: Revert "Add function Form::saveUploadedFile()" This reverts commit 3d493fc75dc6e3593001c2d9dfef26f4c1d79c2c. The way I was wanting to handle file uploads isn't going to fly with a semantic of PHP and POST var mgmt. -.- Rolling back relevant changes to write up something else. --- app/class/form.class.php | 8 -------- 1 file changed, 8 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 3f28a36..529c480 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -232,14 +232,6 @@ class Form /* return */ return count($this->errorlist) == 0; } - - /* - * Save file uploaded through web form - */ - function saveUploadedFile($file, $filename) - { - return move_uploaded_file($file['tmp_name'], $filename); - } } ?> -- cgit v1.2.3 From caebcb442985496b3bddaa3e7c1a752832e32e0b Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 27 Apr 2016 20:43:35 -0400 Subject: Revert "Update function Form::populate() to initialize fields added with Form::field_file()" This reverts commit 1f8b53e426b8c0a1546e9d5c21573be9003cb556. See parent commit message. --- app/class/form.class.php | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 529c480..e748afc 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -105,7 +105,7 @@ class Form /* * Populate the form with input data from web page */ - function populate($input, $files = null) + function populate($input) { /* detect duplicate names */ $names = array(); @@ -115,8 +115,6 @@ class Form $names[] = $fld['name']; foreach ($this->enumFields as $fld) $names[] = $fld['name']; - foreach ($this->fileFields as $fld) - $names[] = $fld['name']; if (count(array_unique($names)) != count($names)) { @@ -197,38 +195,6 @@ class Form $this->logError($fld['name'] . " is required"); } - /* init file fields */ - foreach ($this->fileFields as $fld) - { - if (!is_null($files) && isset($files[$fld['name']])) - { - $file = $files[$fld['name']]; - - if ($file['error'] > 0) - { - $this->logError("An unknown error occurred"); - continue; - } - - if ($file['size'] > $fld['maxsize']) - { - $this->logError("File must be no larger than " . $fld['maxsize'] . " bytes"); - continue; - } - - if (is_array($fld['mime']) && array_search($file['type'], $fld['mime']) === false) - { - $this->logError("File type is not supported"); - continue; - } - - $this->$fld['name'] = $file; - } - - else if ($fld['req']) - $this->logError($fld['name'] . " is required"); - } - /* return */ return count($this->errorlist) == 0; } -- cgit v1.2.3 From 9458874e8c194b1c5a53bd3e85f9ba7548c9dac4 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 27 Apr 2016 20:47:21 -0400 Subject: Revert "Add form field type 'file'" This reverts commit 8ad6e8f9223bd3ee214478b3e1247f9c7d8e91ec. See parent commit message --- app/class/form.class.php | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index e748afc..9f103ba 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -13,7 +13,6 @@ class Form $this->textFields = array(); $this->numbFields = array(); $this->enumFields = array(); - $this->fileFields = array(); $this->errorlist = array(); } @@ -86,22 +85,6 @@ class Form $this->field_enum($name, array("1", "0"), "0"); } - /* - * Add new file field to the form - */ - function field_file($name, $maxsize, $allowed_mime = null, $req = false) - { - if ($req !== true) - $req = false; - - $this->fileFields[] = array( - 'name' => $name, - 'maxsize' => $maxsize, - 'mime' => $allowed_mime, - 'req' => $req - ); - } - /* * Populate the form with input data from web page */ -- cgit v1.2.3 From db369507f636f9395604a5cd72fcfe5d99f97166 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 27 Apr 2016 22:11:58 -0400 Subject: Add function Form::saveFile() This is a rework of how the framework handles performing file uploads. Rather than attaching new fields to a form (of type file) and handling them during populate() then saving later, users can call what is essentially a static function and save files in isolation. Since each webform I can conceive using in Scrott at this time won't be uploading more than one file at a time, this model should work nicely moving forward; however can be easily adjusted if need be. --- app/class/form.class.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 9f103ba..907c0a2 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -181,6 +181,47 @@ class Form /* return */ return count($this->errorlist) == 0; } + + /* + * Handle an uploaded file + */ + function saveFile($file, $maxsize, $allowed_mime, $path, $req = false) + { + if (isset($file) && !is_null($file)) + { + if ($file['error'] > 0) + { + $this->logError("An unknown error occurred"); + return false; + } + + if ($file['size'] > $maxsize) + { + $this->logError("File must be no larger than " . $maxsize . " bytes"); + return false; + } + + if (is_array($allowed_mime) && array_search($file['type'], $allowed_mime) === false) + { + $this->logError("File type is not supported"); + return false; + } + + if (!move_uploaded_file($file['tmp_name'], $path)) + { + $this->logError("Error saving uploaded file"); + return false; + } + } + + else if ($req) + { + $this->logError("File upload is required"); + return false; + } + + return true; + } } ?> -- cgit v1.2.3 From ed2af3a874417b8317bb6def9b40189b1d5d05e8 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 30 Apr 2016 16:57:11 -0400 Subject: Fix to Form::saveFile() Only log an error if we get an upload error besides err code 4 (No file uploaded) --- app/class/form.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 907c0a2..fd85638 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -191,7 +191,9 @@ class Form { if ($file['error'] > 0) { - $this->logError("An unknown error occurred"); + if ($file['error'] != UPLOAD_ERR_NO_FILE) + $this->logError("An unknown error occurred"); + return false; } -- cgit v1.2.3 From ec7186ed4e1c2a41ff9052cdd1624b8cabbb047c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 26 May 2016 23:46:22 -0400 Subject: Add copyright notice to Scrott class files --- app/class/form.class.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index fd85638..f0d660a 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -1,5 +1,19 @@ Date: Tue, 13 Sep 2016 20:34:15 -0400 Subject: Changes to the handling of indirect variables, properties, and methods To maintain forward compatability with newer versions of PHP (and since my dev environment is now running PHP 7), this patch is made to address the following breaking change from PHP 5: PHP 7 now uses an abstract syntax tree when parsing source files. This has permitted many improvements to the language which were previously impossible due to limitations in the parser used in earlier versions of PHP, but has resulted in the removal of a few special cases for consistency reasons, which has resulted in backward compatibility breaks. Indirect access to variables, properties, and methods will now be evaluated strictly in left-to-right order, as opposed to the previous mix of special cases. --- app/class/form.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index f0d660a..8bb6506 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -123,10 +123,10 @@ class Form foreach ($this->textFields as $fld) { if (isset($input[$fld['name']]) && $input[$fld['name']] != "") - $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + $this->{$fld['name']} = htmlEntities($input[$fld['name']], ENT_QUOTES); else if (!is_null($fld['deflt'])) - $this->$fld['name'] = $fld['deflt']; + $this->{$fld['name']} = $fld['deflt']; else if ($fld['req']) $this->logError($fld['name'] . " is required"); @@ -161,11 +161,11 @@ class Form continue; } - $this->$fld['name'] = $input[$fld['name']]; + $this->{$fld['name']} = $input[$fld['name']]; } else if (!is_null($fld['deflt'])) - $this->$fld['name'] = $fld['deflt']; + $this->{$fld['name']} = $fld['deflt']; else if ($fld['req']) $this->logError($fld['name'] . " is required"); @@ -182,11 +182,11 @@ class Form continue; } - $this->$fld['name'] = $input[$fld['name']]; + $this->{$fld['name']} = $input[$fld['name']]; } else if (!is_null($fld['deflt'])) - $this->$fld['name'] = $fld['deflt']; + $this->{$fld['name']} = $fld['deflt']; else if ($fld['req']) $this->logError($fld['name'] . " is required"); -- cgit v1.2.3 From ed99654d2e139a847a63e9295bf976d17462ee34 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sat, 22 Oct 2016 00:29:30 -0400 Subject: Deprecate application code Setup to perform an iteration of development focused on a simpler implementation and eliminating redundancy in design. --- app/class/form.class.php | 243 ----------------------------------------------- 1 file changed, 243 deletions(-) delete mode 100644 app/class/form.class.php (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php deleted file mode 100644 index 8bb6506..0000000 --- a/app/class/form.class.php +++ /dev/null @@ -1,243 +0,0 @@ -textFields = array(); - $this->numbFields = array(); - $this->enumFields = array(); - - $this->errorlist = array(); - } - - /* - * Log an error - */ - function logError($str) - { - $this->errorlist[] = $str; - } - - /* - * Add new text field to the form - */ - function field_text($name, $deflt = null, $req = true) - { - if ($req !== true) - $req = false; - - $this->textFields[] = array( - 'name' => $name, - 'deflt' => $deflt, - 'req' => $req - ); - } - - /* - * Add new numeric field to the form - */ - function field_numeric($name, $min = null, $max = null, $deflt = null, $integer = true, $req = true) - { - if ($req !== true) - $req = false; - - if ($integer !== true) - $integer = false; - - $this->numbFields[] = array( - 'name' => $name, - 'min' => $min, - 'max' => $max, - 'deflt' => $deflt, - 'int' => $integer, - 'req' => $req - ); - } - - /* - * Add new enumeration field to the form - */ - function field_enum($name, $values, $deflt = null, $req = true) - { - if ($req !== true) - $req = false; - - $this->enumFields[] = array( - 'name' => $name, - 'vals' => $values, - 'deflt' => $deflt, - 'req' => $req - ); - } - - /* - * Add new boolean field to the form - */ - function field_bool($name) - { - $this->field_enum($name, array("1", "0"), "0"); - } - - /* - * Populate the form with input data from web page - */ - function populate($input) - { - /* detect duplicate names */ - $names = array(); - foreach ($this->textFields as $fld) - $names[] = $fld['name']; - foreach ($this->numbFields as $fld) - $names[] = $fld['name']; - foreach ($this->enumFields as $fld) - $names[] = $fld['name']; - - if (count(array_unique($names)) != count($names)) - { - $this->logError("Internal error: Duplicate field names defined in form"); - return false; - } - - /* init text fields */ - foreach ($this->textFields as $fld) - { - if (isset($input[$fld['name']]) && $input[$fld['name']] != "") - $this->{$fld['name']} = htmlEntities($input[$fld['name']], ENT_QUOTES); - - else if (!is_null($fld['deflt'])) - $this->{$fld['name']} = $fld['deflt']; - - else if ($fld['req']) - $this->logError($fld['name'] . " is required"); - } - - /* init numeric fields */ - foreach ($this->numbFields as $fld) - { - if (isset($input[$fld['name']]) && $input[$fld['name']] != "") - { - if (!is_numeric($input[$fld['name']])) - { - $this->logError($fld['name'] . " must be numeric"); - continue; - } - - if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']])) - { - $this->logError($fld['name'] . " must be an integer"); - continue; - } - - if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min'])) - { - $this->logError($fld['name'] . " must be no less than " . $fld['min']); - continue; - } - - if (!is_null($fld['max']) && ($input[$fld['name']] > $fld['max'])) - { - $this->logError($fld['name'] . " must be no more than " . $fld['max']); - continue; - } - - $this->{$fld['name']} = $input[$fld['name']]; - } - - else if (!is_null($fld['deflt'])) - $this->{$fld['name']} = $fld['deflt']; - - else if ($fld['req']) - $this->logError($fld['name'] . " is required"); - } - - /* init enum fields */ - foreach ($this->enumFields as $fld) - { - if (isset($input[$fld['name']]) && $input[$fld['name']] != "") - { - if (array_search($input[$fld['name']], $fld['vals']) === false) - { - $this->logError($fld['name'] . " is not an appropriate value"); - continue; - } - - $this->{$fld['name']} = $input[$fld['name']]; - } - - else if (!is_null($fld['deflt'])) - $this->{$fld['name']} = $fld['deflt']; - - else if ($fld['req']) - $this->logError($fld['name'] . " is required"); - } - - /* return */ - return count($this->errorlist) == 0; - } - - /* - * Handle an uploaded file - */ - function saveFile($file, $maxsize, $allowed_mime, $path, $req = false) - { - if (isset($file) && !is_null($file)) - { - if ($file['error'] > 0) - { - if ($file['error'] != UPLOAD_ERR_NO_FILE) - $this->logError("An unknown error occurred"); - - return false; - } - - if ($file['size'] > $maxsize) - { - $this->logError("File must be no larger than " . $maxsize . " bytes"); - return false; - } - - if (is_array($allowed_mime) && array_search($file['type'], $allowed_mime) === false) - { - $this->logError("File type is not supported"); - return false; - } - - if (!move_uploaded_file($file['tmp_name'], $path)) - { - $this->logError("Error saving uploaded file"); - return false; - } - } - - else if ($req) - { - $this->logError("File upload is required"); - return false; - } - - return true; - } -} - -?> -- cgit v1.2.3 From 4a9908d9aa3d0f50d4907eb25e6fc1d317061484 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Tue, 27 Jun 2017 01:35:26 -0400 Subject: Add form class --- app/class/form.class.php | 179 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 app/class/form.class.php (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php new file mode 100644 index 0000000..8f9d936 --- /dev/null +++ b/app/class/form.class.php @@ -0,0 +1,179 @@ +textFields[] = array( + 'name' => $name, + 'required' => $required, + 'default' => $deflt, + ); + } + + /* + * Add new numeric field to this form + */ + public function numeric(string $name, ?int $min = NULL, ?int $max = NULL, + int $deflt = 0, bool $isInt = true, bool $required = true) : void + { + $this->numbFields[] = array( + 'name' => $name, + 'min' => $min, + 'max' => $max, + 'default' => $deflt, + 'isInt' => $isInt, + 'required' => $required, + ); + } + + /* + * Add new enumeration field to this form + */ + public function enum(string $name, array $values, $deflt = NULL, + bool $required = true) : void + { + $this->enumFields[] = array( + 'name' => $name, + 'values' => $values, + 'default' => $deflt, + 'required' => $required, + ); + } + + /* + * Add new boolean field to this form + */ + public function flag(string $name) : void + { + $this->enum($name, array("1", "0"), "0", false); + } + + /* + * Populate this form with input data from web page. If an error + * is encountered, or a required field is missing, false is returned. + */ + public function populate(array $input) : bool + { + /* detect duplicate names */ + $names = array(); + + foreach ($this->textFields as $field) + $names[] = $field['name']; + + foreach ($this->numbFields as $field) + $names[] = $field['name']; + + foreach ($this->enumFields as $field) + $names[] = $field['name']; + + if (count(array_unique($names)) != count($names)) + throw new Exception("Internal error: Duplicate field names defined in form"); + + /* init text fields */ + foreach ($this->textFields as $field) + { + if (isset($input[$field['name']]) && $input[$field['name']] != "") + $this->{$field['name']} = htmlentities($input[$field['name']], ENT_QUOTES); + + else if ($field['required']) + logError(ERROR, $field['name'] . " is required"); + + else + $this->{$field['name']} = $field['default']; + } + + /* init numeric fields */ + foreach ($this->numbFields as $field) + { + if (isset($input[$field['name']]) && $input[$field['name']] != "") + { + if (!is_numeric($input[$field['name']])) + { + logError(ERROR, $field['name'] . " must be numeric"); + continue; + } + + if ($field['isInt'] && (floor($input[$field['name']]) != $input[$field['name']])) + { + logError(ERROR, $field['name'] . " must be an integer"); + continue; + } + + if (!is_null($field['min']) && ($input[$field['name']] < $field['min'])) + { + logError(ERROR, $field['name'] . " must be no less than " . $field['min']); + continue; + } + + if (!is_null($field['max']) && ($input[$field['name']] > $field['max'])) + { + logError(ERROR, $field['name'] . " must be no more than " . $field['max']); + continue; + } + + $this->{$field['name']} = $input[$field['name']]; + } + + else if ($field['required']) + logError(ERROR, $field['name'] . " is required"); + + else + $this->{$field['name']} = $field['default']; + } + + /* init enum fields */ + foreach ($this->enumFields as $field) + { + if (isset($input[$field['name']]) && $input[$field['name']] != "") + { + if (array_search($input[$field['name']], $field['values']) === false) + { + logError(ERROR, $field['name'] . " is not an appropriate value"); + continue; + } + + $this->{$field['name']} = $input[$field['name']]; + } + + else if ($field['required']) + logError(ERROR, $field['name'] . " is required"); + + else + $this->{$field['name']} = $field['default']; + } + + return !isError(ERROR); + } +} + +?> -- cgit v1.2.3 From fd58becfca433f125df70531f9bece88c8f14b71 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 23 Sep 2018 22:05:39 -0400 Subject: form: Fix bug in populate() There was a problem with processing enum type fields. The way all other field types are asserted to be 'defined' is via: isset($field) && $field != "" Which works perfectly fine, and is exactly what we want. However, with enums the second part of that && can bite us if "" is in the list of acceptable values. This commit removed that half of the check (only for enum values) so that the empty string may be an acceptable enum value. If "" is not in the values array, then the check is implicitly reinstated. --- app/class/form.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 8f9d936..10a68c3 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -154,7 +154,7 @@ class form /* init enum fields */ foreach ($this->enumFields as $field) { - if (isset($input[$field['name']]) && $input[$field['name']] != "") + if (isset($input[$field['name']])) { if (array_search($input[$field['name']], $field['values']) === false) { -- cgit v1.2.3