LEPTON CMS 7.0.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
lepton_request.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
26{
27
28 public static $instance;
29
38 public string $strict_looking_inside = "post";
39
48 public bool $error_mode = false;
49
56 public string $logFileName = "LEPTON_request.log";
57
64 public string $logFilePath = "/temp/secure/";
65
74 public array $errors = [];
75
76 public function initialize()
77 {
78 // comes from parent
79 }
80
102 public function testPostValues(array &$aValueList ): array
103 {
104 $this->strict_looking_inside = "post";
105
106 $aReturnList = [];
107 foreach($aValueList as $term => $options)
108 {
109 $aReturnList[ $term ] = $this->get_request(
110 $term,
111 $options['default'],
112 $options['type'],
113 (isset($options['range']) ? $options['range'] : "" )
114 );
115 }
116
117 return $aReturnList;
118 }
119
142 public function testGetValues(array &$aValueList): array
143 {
144 $this->strict_looking_inside = "get";
145
146 $aReturnList = [];
147 foreach($aValueList as $term => $options)
148 {
149 $aReturnList[ $term ] = $this->get_request(
150 $term,
151 $options['default'],
152 $options['type'],
153 (isset($options['range']) ? $options['range'] : "" )
154 );
155 }
156
157 return $aReturnList;
158 }
159
160
219 public function get_request(string $aName="", string|int|array|float|null $aDefault=null, string $type="", string|array $range="")
220 {
221
222 if ($aName == "")
223 {
224 return NULL;
225 }
226
227 if (false === $this->strict_looking_inside)
228 {
229
230 if (strtoupper($_SERVER['REQUEST_METHOD']) == "POST")
231 {
232 $return_value = (true === array_key_exists ($aName , $_POST) ) ? $_POST[$aName] : $aDefault;
233 }
234 else
235 {
236 $return_value = (true === array_key_exists ($aName , $_GET) ) ? $_GET[$aName] : $aDefault;
237 }
238 }
239 else
240 {
241 switch (strtolower($this->strict_looking_inside))
242 {
243 case 'post':
244 $return_value = (true === array_key_exists($aName , $_POST) ) ? $_POST[$aName] : $aDefault;
245 break;
246
247 case 'get':
248 $return_value = (true === array_key_exists($aName , $_GET) ) ? $_GET[$aName] : $aDefault;
249 break;
250
251 case 'request':
252 $return_value = (true === array_key_exists($aName , $_REQUEST) ) ? $_REQUEST[$aName] : $aDefault;
253 break;
254
255 default:
256 $return_value = NULL;
257 break;
258 }
259 }
260
261 if ($type != "")
262 {
263 $this->testType($aName, $type, gettype($return_value));
264
265 switch (strtolower($type))
266 {
267 // [1] Integers
268 // 1.1
269 case 'integer':
270 case 'number':
271 $return_value = intval($return_value);
272 if (!is_int($return_value)) {
273 $return_value = $aDefault;
274 } else {
275 if ( true === is_array($range) )
276 {
277 $this->check_range($type, $return_value, $aDefault, $range);
278 }
279 }
280 break;
281
282 // 1.2 Only positive integers (without 0)
283 case 'integer+':
284 $return_value = intval($return_value);
285 if (!is_int($return_value))
286 {
287 $return_value = $aDefault;
288 }
289
290 if ($return_value <= 0)
291 {
292 $return_value = $aDefault;
293 }
294
295 if (true === is_array($range) )
296 {
297 $this->check_range($type, $return_value, $aDefault, $range);
298 }
299 break;
300
301 // 1.3 Only negative integers (without 0)
302 case 'integer-':
303 $return_value = intval($return_value);
304 if (!is_int($return_value))
305 {
306 $return_value = $aDefault;
307 }
308
309 if ( $return_value >= 0)
310 {
311 $return_value = $aDefault;
312 }
313
314 if ( true === is_array($range) )
315 {
316 $this->check_range($type, $return_value, $aDefault, $range);
317 }
318 break;
319
320 /* 1.4 decimal
321 * Keep in mind that we only replace the "," (Comma) with "." (decimal-dot)!
322 * to save a valid value in the database.
323 *
324 * e.g. "Win 1.234.300 Euro" will become "1234300"
325 * "1.300.000,34" will become "1300000.34"
326 * "1.300.000.27" will become "1300000.27"
327 * "-23,99" will become "-23.99"
328 */
329 case 'float':
330 case 'double':
331 case 'decimal': // -12.350,78
332 // 1.4.1 remove all NONE numbers (but keep '.', ',' and '-')
333 $sPattern = "/[^0-9-,\.]/i";
334 $return_value = preg_replace (
335 $sPattern,
336 "",
337 (string)$return_value
338 );
339
340 // 1.4.2 replace existing "," with "."
341 $return_value = str_replace(",", ".", $return_value);
342
343 // 1.4.3 force to keep at last ONE dot (.)
344 $aTemp = explode(".", $return_value);
345
346 // 1.4.3.1 more than one dot found!
347 if(count($aTemp) > 2)
348 {
349 $sPrefix = array_pop($aTemp);
350 $return_value = implode("", $aTemp).".".$sPrefix;
351 }
352
353 // 1.4.4 the range
354 if ( true === is_array($range) )
355 {
356 $this->check_range($type, $return_value, $aDefault, $range);
357 }
358 break;
359
360
361 // [2] Strings
362 // 2.1
363 case 'string':
364 // keep in mind that pdo add slashes automatically via prepare and execute
365 if (!is_string((string)$return_value))
366 {
367 $return_value = $aDefault;
368 }
369
370 if (true === is_array($range))
371 {
372 $this->check_range($type, $return_value, $aDefault, $range);
373 }
374 break;
375
376 // 2.2 string without any html tags
377 case 'string_clean':
378 if (!is_string((string)$return_value))
379 {
380 $return_value = $aDefault;
381 }
382 else
383 {
384 $return_value = htmlspecialchars(strip_tags($return_value));
385 }
386 break;
387
388 // 2.3 string with all html-tags converted to htmlspecialchars
389 case 'string_chars':
390
391 if (!is_string((string)$return_value))
392 {
393 $return_value = $aDefault;
394 }
395 else
396 {
397 $return_value = lib_lepton::getToolInstance("htmlpurifier")->purify($return_value);
398 $return_value = htmlspecialchars($return_value);
399 }
400 break;
401
402 // 2.4 string without tags but allowed html-tags
403 case 'string_allowed':
404
405 if (!is_string((string)$return_value))
406 {
407 $return_value = $aDefault;
408 }
409 else
410 {
411 $return_value = strip_tags($return_value, $range);
412 }
413 break;
414
415
416
417 // [4] E-Mail
418 case 'email':
419 if (false === LEPTON_handle::checkEmailChars($return_value))
420 {
421 $return_value = '';
422 }
423 break;
424
425 // [5] Date(-s)
426 // [5.1]
427 case 'date':
428 $format = 'Y-m-d';
429 $d = DateTime::createFromFormat($format, $return_value);
430 $result = $d && $d->format($format) == $return_value;
431 if ($result === false) {
432 $return_value = $aDefault;
433 }
434 break;
435
436 // [5.2]
437 case 'datetime':
438 $format = 'Y-m-d H:i:s';
439 $d = DateTime::createFromFormat($format, $return_value);
440 $result = $d && $d->format($format) == $return_value;
441 if ($result === false) {
442 $return_value = $aDefault;
443 }
444 break;
445
446 // [5.3]
447 case 'datetime_local':
448 $format = 'Y-m-dTH:i:s';
449 $d = DateTime::createFromFormat($format, $return_value);
450 $result = $d && $d->format($format) == $return_value;
451 if ($result === false) {
452 $return_value = $aDefault;
453 }
454 break;
455
456 // [5.4]
457 case 'month':
458 $format = 'Y-m';
459 $d = DateTime::createFromFormat($format, $return_value);
460 $result = $d && $d->format($format) == $return_value;
461 if ($result === false) {
462 $return_value = $aDefault;
463 }
464 break;
465
466 // [5.5]
467 case 'time': // hour 00:00
468 $format = 'H:i';
469 $d = DateTime::createFromFormat($format, $return_value);
470 $result = $d && $d->format($format) == $return_value;
471 if ($result === false) {
472 $return_value = $aDefault;
473 }
474 break;
475
476 // [5.6]
477 case 'week':
478 // $format = 'Y-W[0-9]{2}';
479 $result = preg_match("/^[1-9][0-9]{3}[-][W][0-9]{2}$/", $return_value);
480 if ($result != 1) {
481 $return_value = $aDefault;
482 }
483 break;
484
485 // [6] Arrays
486 case 'array':
487 if (!is_array($return_value))
488 {
489 $return_value = $aDefault;
490 }
491 break;
492
493 // [7] RegExp
494 case 'regexp':
495 if ((true === is_array($range)) && (isset($range['pattern'])))
496 {
497 $test = preg_match($range['pattern'], $return_value);
498 if ($test !== 1)
499 {
500 $return_value = $range['default'] ?? $aDefault;
501 }
502 }
503 break;
504
505 // [8]
506 default:
507 // nothing above match.
508 break;
509 }
510 }
511 return $return_value;
512 }
513
514 static public function add_slash(string &$sText="" ): void
515 {
516 if (substr($sText, 0,1) != "/")
517 {
518 $sText = "/".$sText;
519 }
520
521 if (substr($sText, -1) != "/")
522 {
523 $sText .= "/";
524 }
525 }
526
527 private function check_range(string $type, int|string|array|float &$value, int|string|array|float &$default, string|array|float|int &$range)
528 {
529
530 if ($value === NULL)
531 {
532 return true;
533 }
534
535 if (!array_key_exists('use', $range))
536 {
537 $range['use'] = 'default';
538 }
539
540 if (!array_key_exists('min', $range))
541 {
542 $range['min'] = 0;
543 }
544
545 if (!array_key_exists('max', $range))
546 {
547 $range['max'] = 255;
548 }
549
550 if (!array_key_exists('char', $range))
551 {
552 $range['char'] = " ";
553 }
554
555 switch (strtolower($type) )
556 {
557 case 'integer':
558 case 'integer+':
559 case 'integer-':
560 case 'float':
561 if (($value < $range['min']) || ($value > $range['max']))
562 {
563 switch (strtolower($range['use']))
564 {
565 case 'default':
566 $value = $default;
567 break;
568
569 case 'min':
570 $value = $range['min'];
571 break;
572
573 case 'max':
574 $value = $range['max'];
575 break;
576
577 case 'near':
578 if ($value <= $range['min'])
579 {
580 $value = $range['min'];
581 }
582
583 if ($value >= $range['max'])
584 {
585 $value = $range['max'];
586 }
587 break;
588
589 default:
590 // nothing
591 break;
592 }
593 }
594 break;
595
596 case 'string':
597 $nc = strlen($value);
598
599 if (($nc < $range['min']) || ($nc > $range['max']))
600 {
601
602 switch(strtolower($range['use']))
603 {
604 case 'default':
605 $value = $default;
606 break;
607
608 case 'fill':
609 for ($i=$nc; $i<=$range['min'];$i++)
610 {
611 $value .= $range['char'];
612 }
613 break;
614
615 case 'cut':
616 $value = substr(
617 $value,
618 0,
619 ( isset($range['max'])
620 ? intval($range['max'])
621 : $nc
622 )
623 );
624 break;
625
626 default:
627 // nothing - keep value as it is
628 break;
629 }
630 }
631 break;
632
633 default:
634 // nothing
635 break;
636 }
637 return true;
638 }
639
640
641 static public function getRequest(string $sField = "")
642 {
643 $sReturnValue = filter_input(
644 INPUT_GET,
645 $sField,
646 FILTER_VALIDATE_REGEXP,
647 [ "options" => [
648 "regexp" => "~^[a-z0-9-_]{2,}$~i",
649 "default" => NULL
650 ]]
651 );
652
653 if (NULL !== $sReturnValue)
654 {
655 return $sReturnValue;
656 }
657 else
658 {
659 return filter_input(
660 INPUT_POST,
661 $sField,
662 FILTER_VALIDATE_REGEXP,
663 [ "options" => [
664 "regexp" => "~^[a-z0-9-_]{2,}$~i",
665 "default" => NULL
666 ]
667 ]
668 );
669 }
670 }
671
672 static public function getPageID()
673 {
674 if (defined("PAGE_ID"))
675 {
676 return PAGE_ID;
677 }
678
679 $sField="page_id" ;
680
681 $sReturnValue = filter_input(
682 INPUT_GET,
683 $sField,
684 FILTER_VALIDATE_REGEXP,
685 [ "options" => [
686 "regexp" => "~^[1-9-]?[0-9]*$~",
687 "default" => NULL
688 ]
689 ]
690 );
691
692 if (NULL !== $sReturnValue)
693 {
694 return $sReturnValue;
695 }
696 else
697 {
698 return filter_input(
699 INPUT_POST,
700 $sField,
701 FILTER_VALIDATE_REGEXP,
702 [ "options" => [
703 "regexp" => "~^[1-9-]?[0-9]*$~",
704 "default" => NULL
705 ]
706 ]
707 );
708 }
709 }
710
711 protected function testType(string $name, string $expected, string $getTypeResult): void
712 {
713 if (true === $this->error_mode)
714 {
715 $types = [
716 'string' => ['string', 'str'],
717 'integer' => ['integer', 'integer+', 'integer-', 'int', 'int+', 'int-']
718 ];
719
720 if (isset($types[$getTypeResult]) && (in_array($expected, $types[$getTypeResult])))
721 {
722
723 }
724 else
725 {
726 $strTemp = "Type does not match! '%s' is not %s. [%s].";
727 $this->writeInfoToLogfile(
728 sprintf(
729 $strTemp,
730 $name,
731 $expected,
732 $getTypeResult
733 )
734 );
735 }
736 }
737 }
738
739 protected function writeInfoToLogfile(string $sMessage=""): void
740 {
741 $sOutputStrg = "[".date("Y-m-d H:i:s")."] ".$sMessage."\n";
742 $sFullPath = dirname(__DIR__, 2).$this->logFilePath.$this->logFileName;
743 file_put_contents($sFullPath, $sOutputStrg, FILE_APPEND);
744 }
745}
static checkEmailChars(string $sEmail)
static add_slash(string &$sText="")
string $strict_looking_inside
static getRequest(string $sField="")
get_request(string $aName="", string|int|array|float|null $aDefault=null, string $type="", string|array $range="")
writeInfoToLogfile(string $sMessage="")
testType(string $name, string $expected, string $getTypeResult)
testGetValues(array &$aValueList)
testPostValues(array &$aValueList)