* A static helper class
*/
class Summoner {
- /**
- * validate the given string with the given type. Optional check the string
- * length
- *
- * @param string $input The string to check
- * @param string $mode How the string should be checked
- * @param string $limit If int given the string is checked for length
- *
- * @return bool
- *
- * @see http://de.php.net/manual/en/regexp.reference.unicode.php
- * http://www.sql-und-xml.de/unicode-database/#pc
- *
- * the pattern replaces all that is allowed. the correct result after
- * the replace should be empty, otherwise are there chars which are not
- * allowed
- */
- static function validate(string $input, string $mode = 'text', string $limit = "0"): bool {
- // check if we have input
- $input = trim($input);
-
- if($input == "") return false;
-
- $ret = false;
-
- switch ($mode) {
- case 'mail':
- if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) {
- return true;
- }
- else {
- return false;
- }
- break;
-
- case 'url':
- if(filter_var($input,FILTER_VALIDATE_URL) === $input) {
- return true;
- }
- else {
- return false;
- }
+ /**
+ * validate the given string with the given type. Optional check the string
+ * length
+ *
+ * @param string $input The string to check
+ * @param string $mode How the string should be checked
+ * @param string $limit If int given the string is checked for length
+ *
+ * @return bool
+ *
+ * @see http://de.php.net/manual/en/regexp.reference.unicode.php
+ * http://www.sql-und-xml.de/unicode-database/#pc
+ *
+ * the pattern replaces all that is allowed. the correct result after
+ * the replace should be empty, otherwise are there chars which are not
+ * allowed
+ */
+ static function validate(string $input, string $mode = 'text', string $limit = "0"): bool {
+ // check if we have input
+ $input = trim($input);
+
+ if($input == "") return false;
+
+ $ret = false;
+
+ switch ($mode) {
+ case 'mail':
+ if(filter_var($input,FILTER_VALIDATE_EMAIL) === $input) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ break;
+
+ case 'url':
+ if(filter_var($input,FILTER_VALIDATE_URL) === $input) {
+ return true;
+ }
+ else {
+ return false;
+ }
break;
- case 'nospace':
- // text without any whitespace and special chars
- $pattern = '/[\p{L}\p{N}]/u';
- break;
-
- case 'nospaceP':
- // text without any whitespace and special chars
- // but with Punctuation other
- # http://www.sql-und-xml.de/unicode-database/po.html
- $pattern = '/[\p{L}\p{N}\p{Po}\-]/u';
- break;
-
- case 'digit':
- // only numbers and digit
- // warning with negative numbers...
- $pattern = '/[\p{N}\-]/';
- break;
-
- case 'pageTitle':
- // text with whitespace and without special chars
- // but with Punctuation
- $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s-]/u';
- break;
-
- # strange. the \p{M} is needed.. don't know why..
- case 'filename':
- $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u';
- break;
-
- case 'shortlink':
- // special char string based on https://www.jwz.org/base64-shortlinks/
- $pattern = '/[\p{L}\p{N}\-_]/u';
- break;
+ case 'nospace':
+ // text without any whitespace and special chars
+ $pattern = '/[\p{L}\p{N}]/u';
+ break;
+
+ case 'nospaceP':
+ // text without any whitespace and special chars
+ // but with Punctuation other
+ # http://www.sql-und-xml.de/unicode-database/po.html
+ $pattern = '/[\p{L}\p{N}\p{Po}\-]/u';
+ break;
+
+ case 'digit':
+ // only numbers and digit
+ // warning with negative numbers...
+ $pattern = '/[\p{N}\-]/';
+ break;
+
+ case 'pageTitle':
+ // text with whitespace and without special chars
+ // but with Punctuation
+ $pattern = '/[\p{L}\p{N}\p{Po}\p{Z}\s-]/u';
+ break;
+
+ # strange. the \p{M} is needed.. don't know why..
+ case 'filename':
+ $pattern = '/[\p{L}\p{N}\p{M}\-_\.\p{Zs}]/u';
+ break;
+
+ case 'shortlink':
+ // special char string based on https://www.jwz.org/base64-shortlinks/
+ $pattern = '/[\p{L}\p{N}\-_]/u';
+ break;
case 'datetime':
// based on the format eg. 2022-09-25T22:00
case 'text':
- default:
- $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
- }
-
- $value = preg_replace($pattern, '', $input);
-
- if($value === "") {
- $ret = true;
- }
-
- if(!empty($limit) && is_numeric($limit)) {
- # isset starts with 0
- if(isset($input[$limit])) {
- # too long
- $ret = false;
- }
- }
-
- return $ret;
- }
-
- /**
- * check if a string starts with a given string
- *
- * @param string $haystack
- * @param string $needle
- * @return boolean
- */
- static function startsWith(string $haystack, string $needle): bool {
- $length = strlen($needle);
- return (substr($haystack, 0, $length) === $needle);
- }
-
- /**
- * check if a string ends with a given string
- *
- * @param string $haystack
- * @param string $needle
- * @return boolean
- */
- static function endsWith(string $haystack, string $needle): bool {
- $length = strlen($needle);
- if ($length == 0) {
- return true;
- }
-
- return (substr($haystack, -$length) === $needle);
- }
-
-
- /**
- * create a short string based on a integer
- *
- * @see https://www.jwz.org/base64-shortlinks/
- * @param string $id
- * @return string
- */
- static function b64sl_pack_id(string $id): string {
- $id = intval($id);
- $ida = ($id > 0xFFFFFFFF ? $id >> 32 : 0); // 32 bit big endian, top
- $idb = ($id & 0xFFFFFFFF); // 32 bit big endian, bottom
- $id = pack ('N', $ida) . pack ('N', $idb);
- $id = preg_replace('/^\000+/', '', "$id"); // omit high-order NUL bytes
- $id = base64_encode ($id);
- $id = str_replace ('+', '-', $id); // encode URL-unsafe "+" "/"
- $id = str_replace ('/', '_', $id);
- $id = preg_replace ('/=+$/', '', $id); // omit trailing padding bytes
- return $id;
- }
-
- /**
- * Decode a base64-encoded big-endian integer of up to 64 bits.
- *
- * @see https://www.jwz.org/base64-shortlinks/
- * @param string $id
- * @return string
- */
- static function b64sl_unpack_id(string $id): string {
- $id = str_replace ('-', '+', $id); // decode URL-unsafe "+" "/"
- $id = str_replace ('_', '/', $id);
- $id = base64_decode ($id);
- while (strlen($id) < 8) { $id = "\000$id"; } // pad with leading NULs
- $a = unpack ('N*', $id); // 32 bit big endian
- $id = ($a[1] << 32) | $a[2]; // pack top and bottom word
- return $id;
- }
-
- /**
- * this only works with arrays and checking if the key is there and echo/return it.
- *
- * @param $array array
- * @param $key string
- * @return mixed
- */
- static function ifset(array $array, string $key) {
- return $array[$key] ?? '';
- }
+ default:
+ $pattern = '/[\p{L}\p{N}\p{P}\p{S}\p{Z}\p{M}\s]/u';
+ }
+
+ $value = preg_replace($pattern, '', $input);
+
+ if($value === "") {
+ $ret = true;
+ }
+
+ if(!empty($limit) && is_numeric($limit)) {
+ # isset starts with 0
+ if(isset($input[$limit])) {
+ # too long
+ $ret = false;
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * check if a string starts with a given string
+ *
+ * @param string $haystack
+ * @param string $needle
+ * @return boolean
+ */
+ static function startsWith(string $haystack, string $needle): bool {
+ $length = strlen($needle);
+ return (substr($haystack, 0, $length) === $needle);
+ }
+
+ /**
+ * check if a string ends with a given string
+ *
+ * @param string $haystack
+ * @param string $needle
+ * @return boolean
+ */
+ static function endsWith(string $haystack, string $needle): bool {
+ $length = strlen($needle);
+ if ($length == 0) {
+ return true;
+ }
+
+ return (substr($haystack, -$length) === $needle);
+ }
+
+
+ /**
+ * create a short string based on a integer
+ *
+ * @see https://www.jwz.org/base64-shortlinks/
+ * @param string $id
+ * @return string
+ */
+ static function b64sl_pack_id(string $id): string {
+ $id = intval($id);
+ $ida = ($id > 0xFFFFFFFF ? $id >> 32 : 0); // 32 bit big endian, top
+ $idb = ($id & 0xFFFFFFFF); // 32 bit big endian, bottom
+ $id = pack ('N', $ida) . pack ('N', $idb);
+ $id = preg_replace('/^\000+/', '', "$id"); // omit high-order NUL bytes
+ $id = base64_encode ($id);
+ $id = str_replace ('+', '-', $id); // encode URL-unsafe "+" "/"
+ $id = str_replace ('/', '_', $id);
+ $id = preg_replace ('/=+$/', '', $id); // omit trailing padding bytes
+ return $id;
+ }
+
+ /**
+ * Decode a base64-encoded big-endian integer of up to 64 bits.
+ *
+ * @see https://www.jwz.org/base64-shortlinks/
+ * @param string $id
+ * @return string
+ */
+ static function b64sl_unpack_id(string $id): string {
+ $id = str_replace ('-', '+', $id); // decode URL-unsafe "+" "/"
+ $id = str_replace ('_', '/', $id);
+ $id = base64_decode ($id);
+ while (strlen($id) < 8) { $id = "\000$id"; } // pad with leading NULs
+ $a = unpack ('N*', $id); // 32 bit big endian
+ $id = ($a[1] << 32) | $a[2]; // pack top and bottom word
+ return $id;
+ }
+
+ /**
+ * this only works with arrays and checking if the key is there and echo/return it.
+ *
+ * @param $array array
+ * @param $key string
+ * @return mixed
+ */
+ static function ifset(array $array, string $key) {
+ return $array[$key] ?? '';
+ }
}
<form method="post">
- <fieldset>
- <label for="receipt">Receipt <small>Just leave it blank to create a new based on date and market name.</small></label>
- <input name="fdata[receipt]" id="receipt" type="text" list="receiptList" autocomplete="off"
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'receipt'); ?>" />
- <datalist id="receiptList">
- <option>Rewe</option>
- <option>Lidl</option>
- </datalist>
-
- <label for="receiptdate">Date and time*</label>
- <input name="fdata[receiptdate]" id="receiptdate" type="datetime-local" autocomplete="off" required
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'receiptdate'); ?>" />
-
- <label for="market">Market*</label>
- <input name="fdata[market]" id="market" type="text" list="marketList" autocomplete="off" required
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'market'); ?>" />
- <datalist id="marketList">
- <option>Rewe</option>
- <option>Lidl</option>
- </datalist>
-
- <label for="product">Product*</label>
- <input name="fdata[product]" id="product" type="text" list="productList" autocomplete="off" required
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'product'); ?>"/>
- <datalist id="productList">
- <option>Rewe</option>
- <option>Lidl</option>
- </datalist>
-
- <label for="manufacturer">Manufacturer*</label>
- <input name="fdata[manufacturer]" id="manufacturer" type="text" list="manufacturerList" autocomplete="off" required
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'manufacturer'); ?>"/>
- <datalist id="manufacturerList">
- <option>Rewe</option>
- <option>Lidl</option>
- </datalist>
-
- <label for="weight">Weight(g)</label>
- <input name="fdata[weight]" id="weight" type="number" autocomplete="off" lang="<?php echo NUMBER_INPUT_LANG; ?>"
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'weight'); ?>"/>
-
- <label for="itemcount">Itemcount</label>
- <input name="fdata[itemcount]" id="itemcount" type="number" autocomplete="off" lang="<?php echo NUMBER_INPUT_LANG; ?>"
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'itemcount'); ?>"/>
-
- <label for="price">Price*</label>
- <input name="fdata[price]" id="price" type="number" autocomplete="off" required
- lang="<?php echo NUMBER_INPUT_LANG; ?>" step="0.01"
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'price'); ?>"/>
-
- <label for="catalog">Catalog</label>
- <input name="fdata[catalog]" id="catalog" type="text" list="catalogList" autocomplete="off"
- value="<?php echo Summoner::ifset($TemplateData['editData'], 'catalog'); ?>"/>
- <datalist id="catalogList">
- <option>Rewe</option>
- <option>Lidl</option>
- </datalist>
-
- <input type="submit" class="tui-button" value="Save" name="submitForm" />
- </fieldset>
+ <fieldset>
+ <label for="receipt">Receipt <small>Just leave it blank to create a new based on date and market name.</small></label>
+ <input name="fdata[receipt]" id="receipt" type="text" list="receiptList" autocomplete="off"
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'receipt'); ?>" />
+ <datalist id="receiptList">
+ <option>Rewe</option>
+ <option>Lidl</option>
+ </datalist>
+
+ <label for="receiptdate">Date and time*</label>
+ <input name="fdata[receiptdate]" id="receiptdate" type="datetime-local" autocomplete="off" required
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'receiptdate'); ?>" />
+
+ <label for="market">Market*</label>
+ <input name="fdata[market]" id="market" type="text" list="marketList" autocomplete="off" required
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'market'); ?>" />
+ <datalist id="marketList">
+ <option>Rewe</option>
+ <option>Lidl</option>
+ </datalist>
+
+ <label for="product">Product*</label>
+ <input name="fdata[product]" id="product" type="text" list="productList" autocomplete="off" required
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'product'); ?>"/>
+ <datalist id="productList">
+ <option>Rewe</option>
+ <option>Lidl</option>
+ </datalist>
+
+ <label for="manufacturer">Manufacturer*</label>
+ <input name="fdata[manufacturer]" id="manufacturer" type="text" list="manufacturerList" autocomplete="off" required
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'manufacturer'); ?>"/>
+ <datalist id="manufacturerList">
+ <option>Rewe</option>
+ <option>Lidl</option>
+ </datalist>
+
+ <label for="weight">Weight(g)</label>
+ <input name="fdata[weight]" id="weight" type="number" autocomplete="off" lang="<?php echo NUMBER_INPUT_LANG; ?>"
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'weight'); ?>"/>
+
+ <label for="itemcount">Itemcount</label>
+ <input name="fdata[itemcount]" id="itemcount" type="number" autocomplete="off" lang="<?php echo NUMBER_INPUT_LANG; ?>"
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'itemcount'); ?>"/>
+
+ <label for="price">Price*</label>
+ <input name="fdata[price]" id="price" type="number" autocomplete="off" required
+ lang="<?php echo NUMBER_INPUT_LANG; ?>" step="0.01"
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'price'); ?>"/>
+
+ <label for="catalog">Catalog</label>
+ <input name="fdata[catalog]" id="catalog" type="text" list="catalogList" autocomplete="off"
+ value="<?php echo Summoner::ifset($TemplateData['editData'], 'catalog'); ?>"/>
+ <datalist id="catalogList">
+ <option>Rewe</option>
+ <option>Lidl</option>
+ </datalist>
+
+ <input type="submit" class="tui-button" value="Save" name="submitForm" />
+ </fieldset>
</form>