Conditions | 394 |
Paths | 891 |
Total Lines | 943 |
Code Lines | 771 |
Lines | 33 |
Ratio | 3.5 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace EvolutionCMS\Legacy; |
||
343 | public function getValueFromPreset($key, $value, $cmd, $opt) |
||
344 | { |
||
345 | $modx = evolutionCMS(); |
||
346 | |||
347 | if ($this->isEmpty($cmd, $value)) { |
||
348 | return ''; |
||
349 | } |
||
350 | |||
351 | $this->key = $key; |
||
352 | $this->value = $value; |
||
353 | $this->opt = $opt; |
||
354 | |||
355 | switch ($cmd) { |
||
356 | ##### Conditional Modifiers |
||
357 | case 'input': |
||
358 | case 'if': |
||
359 | if (!$opt) { |
||
360 | return $value; |
||
361 | } |
||
362 | |||
363 | return $opt; |
||
364 | case '=': |
||
365 | case 'eq': |
||
366 | case 'is': |
||
367 | case 'equals': |
||
368 | $this->condition[] = (int)($value == $opt); |
||
369 | break; |
||
370 | case 'neq': |
||
371 | case 'ne': |
||
372 | case 'notequals': |
||
373 | case 'isnot': |
||
374 | case 'isnt': |
||
375 | case 'not': |
||
376 | $this->condition[] = (int)($value != $opt); |
||
377 | break; |
||
378 | case '%': |
||
379 | $this->condition[] = (int)($value % $opt == 0); |
||
380 | break; |
||
381 | case 'isempty': |
||
382 | $this->condition[] = (int)(empty($value)); |
||
383 | break; |
||
384 | case 'isntempty': |
||
385 | case 'isnotempty': |
||
386 | $this->condition[] = (int)(!empty($value)); |
||
387 | break; |
||
388 | case '>=': |
||
389 | case 'gte': |
||
390 | case 'eg': |
||
391 | case 'isgte': |
||
392 | $this->condition[] = (int)($value >= $opt); |
||
393 | break; |
||
394 | case '<=': |
||
395 | case 'lte': |
||
396 | case 'el': |
||
397 | case 'islte': |
||
398 | $this->condition[] = (int)($value <= $opt); |
||
399 | break; |
||
400 | case '>': |
||
401 | case 'gt': |
||
402 | case 'greaterthan': |
||
403 | case 'isgreaterthan': |
||
404 | case 'isgt': |
||
405 | $this->condition[] = (int)($value > $opt); |
||
406 | break; |
||
407 | case '<': |
||
408 | case 'lt': |
||
409 | case 'lowerthan': |
||
410 | case 'islowerthan': |
||
411 | case 'islt': |
||
412 | $this->condition[] = (int)($value < $opt); |
||
413 | break; |
||
414 | case 'find': |
||
415 | $this->condition[] = (int)(strpos($value, $opt) !== false); |
||
416 | break; |
||
417 | case 'inarray': |
||
418 | case 'in_array': |
||
419 | case 'in': |
||
420 | $opt = explode(',', $opt); |
||
421 | $this->condition[] = (int)(in_array($value, $opt) !== false); |
||
422 | break; |
||
423 | case 'wildcard_match': |
||
424 | case 'wcard_match': |
||
425 | case 'wildcard': |
||
426 | case 'wcard': |
||
427 | case 'fnmatch': |
||
428 | $this->condition[] = (int)(fnmatch($opt, $value) !== false); |
||
429 | break; |
||
430 | case 'is_file': |
||
431 | case 'is_dir': |
||
432 | case 'file_exists': |
||
433 | case 'is_readable': |
||
434 | case 'is_writable': |
||
435 | if (!$opt) { |
||
436 | $path = $value; |
||
437 | } else { |
||
438 | $path = $opt; |
||
439 | } |
||
440 | if (strpos($path, MODX_MANAGER_PATH) !== false) { |
||
441 | exit('Can not read core path'); |
||
442 | } |
||
443 | if (strpos($path, $modx->config['base_path']) === false) { |
||
444 | $path = ltrim($path, '/'); |
||
445 | } |
||
446 | $this->condition[] = (int)($cmd($path) !== false); |
||
447 | break; |
||
448 | case 'is_image': |
||
449 | if (!$opt) { |
||
450 | $path = $value; |
||
451 | } else { |
||
452 | $path = $opt; |
||
453 | } |
||
454 | if (!is_file($path)) { |
||
455 | $this->condition[] = '0'; |
||
456 | break; |
||
457 | } |
||
458 | $_ = getimagesize($path); |
||
459 | $this->condition[] = (int)($_[0]); |
||
460 | break; |
||
461 | case 'regex': |
||
462 | case 'preg': |
||
463 | case 'preg_match': |
||
464 | case 'isinrole': |
||
465 | $this->condition[] = (int)(preg_match($opt, $value)); |
||
466 | break; |
||
467 | case 'ir': |
||
468 | case 'memberof': |
||
469 | case 'mo': |
||
470 | // Is Member Of (same as inrole but this one can be stringed as a conditional) |
||
471 | $this->condition[] = $this->includeMdfFile('memberof'); |
||
472 | break; |
||
473 | case 'or': |
||
474 | $this->condition[] = '||'; |
||
475 | break; |
||
476 | case 'and': |
||
477 | $this->condition[] = '&&'; |
||
478 | break; |
||
479 | case 'show': |
||
480 | View Code Duplication | case 'this': |
|
481 | $conditional = implode(' ', $this->condition); |
||
482 | $isvalid = (int)(eval("return ({$conditional});")); |
||
483 | if ($isvalid) { |
||
484 | return $this->srcValue; |
||
485 | } |
||
486 | |||
487 | return null; |
||
488 | View Code Duplication | case 'then': |
|
489 | $conditional = implode(' ', $this->condition); |
||
490 | $isvalid = (int)eval("return ({$conditional});"); |
||
491 | if ($isvalid) { |
||
492 | return $opt; |
||
493 | } |
||
494 | |||
495 | return null; |
||
496 | View Code Duplication | case 'else': |
|
497 | $conditional = implode(' ', $this->condition); |
||
498 | $isvalid = (int)eval("return ({$conditional});"); |
||
499 | if (!$isvalid) { |
||
500 | return $opt; |
||
501 | } |
||
502 | break; |
||
503 | case 'select': |
||
504 | case 'switch': |
||
505 | $raw = explode('&', $opt); |
||
506 | $map = array(); |
||
507 | $c = count($raw); |
||
508 | for ($m = 0; $m < $c; $m++) { |
||
509 | $mi = explode('=', $raw[$m], 2); |
||
510 | $map[$mi[0]] = $mi[1]; |
||
511 | } |
||
512 | if (isset($map[$value])) { |
||
513 | return $map[$value]; |
||
514 | } else { |
||
515 | return ''; |
||
516 | } |
||
517 | ##### End of Conditional Modifiers |
||
518 | |||
519 | ##### Encode / Decode / Hash / Escape |
||
520 | case 'htmlent': |
||
521 | case 'htmlentities': |
||
522 | return htmlentities($value, ENT_QUOTES, $modx->config['modx_charset']); |
||
523 | case 'html_entity_decode': |
||
524 | case 'decode_html': |
||
525 | case 'html_decode': |
||
526 | return html_entity_decode($value, ENT_QUOTES, $modx->config['modx_charset']); |
||
527 | case 'esc': |
||
528 | case 'escape': |
||
529 | $value = preg_replace('/&(#[0-9]+|[a-z]+);/i', '&$1;', |
||
530 | htmlspecialchars($value, ENT_QUOTES, $modx->config['modx_charset'])); |
||
531 | |||
532 | return str_replace(array('[', ']', '`'), array('[', ']', '`'), $value); |
||
533 | case 'sql_escape': |
||
534 | case 'encode_js': |
||
535 | return $modx->getDatabase()->escape($value); |
||
536 | case 'htmlspecialchars': |
||
537 | case 'hsc': |
||
538 | case 'encode_html': |
||
539 | case 'html_encode': |
||
540 | return preg_replace('/&(#[0-9]+|[a-z]+);/i', '&$1;', |
||
541 | htmlspecialchars($value, ENT_QUOTES, $modx->config['modx_charset'])); |
||
542 | case 'spam_protect': |
||
543 | return str_replace(array('@', '.'), array('@', '.'), $value); |
||
544 | case 'strip': |
||
545 | if ($opt === '') { |
||
546 | $opt = ' '; |
||
547 | } |
||
548 | |||
549 | return preg_replace('/[\n\r\t\s]+/', $opt, $value); |
||
550 | case 'strip_linefeeds': |
||
551 | return str_replace(array("\n", "\r"), '', $value); |
||
552 | case 'notags': |
||
553 | case 'strip_tags': |
||
554 | case 'remove_html': |
||
555 | if ($opt !== '') { |
||
556 | $param = array(); |
||
557 | foreach (explode(',', $opt) as $v) { |
||
558 | $v = trim($v, '</> '); |
||
559 | $param[] = "<{$v}>"; |
||
560 | } |
||
561 | $params = implode(',', $param); |
||
562 | } else { |
||
563 | $params = ''; |
||
564 | } |
||
565 | View Code Duplication | if (!strpos($params, '<br>') === false) { |
|
566 | $value = preg_replace('@(<br[ /]*>)\n@', '$1', $value); |
||
567 | $value = preg_replace('@<br[ /]*>@', "\n", $value); |
||
568 | } |
||
569 | |||
570 | return $this->strip_tags($value, $params); |
||
571 | case 'urlencode': |
||
572 | case 'url_encode': |
||
573 | case 'encode_url': |
||
574 | return urlencode($value); |
||
575 | case 'base64_decode': |
||
576 | if ($opt !== 'false') { |
||
577 | $opt = true; |
||
578 | } else { |
||
579 | $opt = false; |
||
580 | } |
||
581 | |||
582 | return base64_decode($value, $opt); |
||
583 | case 'encode_sha1': |
||
584 | $cmd = 'sha1'; |
||
585 | case 'addslashes': |
||
586 | case 'urldecode': |
||
587 | case 'url_decode': |
||
588 | case 'rawurlencode': |
||
589 | case 'rawurldecode': |
||
590 | case 'base64_encode': |
||
591 | case 'md5': |
||
592 | case 'sha1': |
||
593 | case 'json_encode': |
||
594 | case 'json_decode': |
||
595 | return $cmd($value); |
||
596 | |||
597 | ##### String Modifiers |
||
598 | case 'lcase': |
||
599 | case 'strtolower': |
||
600 | case 'lower_case': |
||
601 | return $this->strtolower($value); |
||
602 | case 'ucase': |
||
603 | case 'strtoupper': |
||
604 | case 'upper_case': |
||
605 | return $this->strtoupper($value); |
||
606 | case 'capitalize': |
||
607 | $_ = explode(' ', $value); |
||
608 | foreach ($_ as $i => $v) { |
||
609 | $_[$i] = ucfirst($v); |
||
610 | } |
||
611 | |||
612 | return implode(' ', $_); |
||
613 | View Code Duplication | case 'zenhan': |
|
614 | if (empty($opt)) { |
||
615 | $opt = 'VKas'; |
||
616 | } |
||
617 | |||
618 | return mb_convert_kana($value, $opt, $modx->config['modx_charset']); |
||
619 | View Code Duplication | case 'hanzen': |
|
620 | if (empty($opt)) { |
||
621 | $opt = 'VKAS'; |
||
622 | } |
||
623 | |||
624 | return mb_convert_kana($value, $opt, $modx->config['modx_charset']); |
||
625 | case 'str_shuffle': |
||
626 | case 'shuffle': |
||
627 | return $this->str_shuffle($value); |
||
628 | case 'reverse': |
||
629 | case 'strrev': |
||
630 | return $this->strrev($value); |
||
631 | case 'length': |
||
632 | case 'len': |
||
633 | case 'strlen': |
||
634 | case 'count_characters': |
||
635 | return $this->strlen($value); |
||
636 | case 'count_words': |
||
637 | $value = trim($value); |
||
638 | |||
639 | return count(preg_split('/\s+/', $value)); |
||
640 | case 'str_word_count': |
||
641 | case 'word_count': |
||
642 | case 'wordcount': |
||
643 | return $this->str_word_count($value); |
||
644 | case 'count_paragraphs': |
||
645 | $value = trim($value); |
||
646 | $value = preg_replace('/\r/', '', $value); |
||
647 | |||
648 | return count(preg_split('/\n+/', $value)); |
||
649 | case 'strpos': |
||
650 | if ($opt != 0 && empty($opt)) { |
||
651 | return $value; |
||
652 | } |
||
653 | |||
654 | return $this->strpos($value, $opt); |
||
655 | case 'wordwrap': |
||
656 | // default: 70 |
||
657 | $wrapat = (int)$opt > 0 ? (int)$opt : 70; |
||
658 | if (version_compare(PHP_VERSION, '5.3.0') >= 0) { |
||
659 | return $this->includeMdfFile('wordwrap'); |
||
660 | } else { |
||
661 | return preg_replace("@(\b\w+\b)@e", "wordwrap('\\1',\$wrapat,' ',1)", $value); |
||
662 | } |
||
663 | case 'wrap_text': |
||
664 | $width = preg_match('/^[1-9][0-9]*$/', $opt) ? $opt : 70; |
||
665 | if ($modx->config['manager_language'] === 'japanese-utf8') { |
||
666 | $chunk = array(); |
||
667 | $bt = ''; |
||
668 | while ($bt != $value) { |
||
669 | $bt = $value; |
||
670 | if ($this->strlen($value) < $width) { |
||
671 | $chunk[] = $value; |
||
672 | break; |
||
673 | } |
||
674 | $chunk[] = $this->substr($value, 0, $width); |
||
675 | $value = $this->substr($value, $width); |
||
676 | } |
||
677 | |||
678 | return implode("\n", $chunk); |
||
679 | } else { |
||
680 | return wordwrap($value, $width, "\n", true); |
||
681 | } |
||
682 | case 'substr': |
||
683 | if (empty($opt)) { |
||
684 | break; |
||
685 | } |
||
686 | if (strpos($opt, ',') !== false) { |
||
687 | list($b, $e) = explode(',', $opt, 2); |
||
688 | |||
689 | return $this->substr($value, $b, (int)$e); |
||
690 | } else { |
||
691 | return $this->substr($value, $opt); |
||
692 | } |
||
693 | case 'limit': |
||
694 | case 'trim_to': // http://www.movabletype.jp/documentation/appendices/modifiers/trim_to.html |
||
695 | View Code Duplication | if (strpos($opt, '+') !== false) { |
|
696 | list($len, $str) = explode('+', $opt, 2); |
||
697 | } else { |
||
698 | $len = $opt; |
||
699 | $str = ''; |
||
700 | } |
||
701 | if ($len === '') { |
||
702 | $len = 100; |
||
703 | } |
||
704 | if (abs($len) > $this->strlen($value)) { |
||
705 | $str = ''; |
||
706 | } |
||
707 | if (preg_match('/^[1-9][0-9]*$/', $len)) { |
||
708 | return $this->substr($value, 0, $len) . $str; |
||
709 | } elseif (preg_match('/^\-[1-9][0-9]*$/', $len)) { |
||
710 | return $str . $this->substr($value, $len); |
||
711 | } |
||
712 | break; |
||
713 | case 'summary': |
||
714 | case 'smart_description': |
||
715 | case 'smart_desc': |
||
716 | return $this->includeMdfFile('summary'); |
||
717 | case 'replace': |
||
718 | case 'str_replace': |
||
719 | if (empty($opt) || strpos($opt, ',') === false) { |
||
720 | break; |
||
721 | } |
||
722 | if (substr_count($opt, ',') == 1) { |
||
723 | $delim = ','; |
||
724 | } elseif (substr_count($opt, '|') == 1) { |
||
725 | $delim = '|'; |
||
726 | } elseif (substr_count($opt, '=>') == 1) { |
||
727 | $delim = '=>'; |
||
728 | } elseif (substr_count($opt, '/') == 1) { |
||
729 | $delim = '/'; |
||
730 | } else { |
||
731 | break; |
||
732 | } |
||
733 | list($s, $r) = explode($delim, $opt); |
||
734 | if ($value !== '') { |
||
735 | return str_replace($s, $r, $value); |
||
736 | } |
||
737 | break; |
||
738 | case 'replace_to': |
||
739 | case 'tpl': |
||
740 | View Code Duplication | if ($value !== '') { |
|
741 | return str_replace(array('[+value+]', '[+output+]', '{value}', '%s'), $value, $opt); |
||
742 | } |
||
743 | break; |
||
744 | case 'eachtpl': |
||
745 | $value = explode('||', $value); |
||
746 | $_ = array(); |
||
747 | foreach ($value as $v) { |
||
748 | $_[] = str_replace(array('[+value+]', '[+output+]', '{value}', '%s'), $v, $opt); |
||
749 | } |
||
750 | |||
751 | return implode("\n", $_); |
||
752 | case 'array_pop': |
||
753 | case 'array_shift': |
||
754 | if (strpos($value, '||') !== false) { |
||
755 | $delim = '||'; |
||
756 | } else { |
||
757 | $delim = ','; |
||
758 | } |
||
759 | |||
760 | return $cmd(explode($delim, $value)); |
||
761 | case 'preg_replace': |
||
762 | case 'regex_replace': |
||
763 | if (empty($opt) || strpos($opt, ',') === false) { |
||
764 | break; |
||
765 | } |
||
766 | list($s, $r) = explode(',', $opt, 2); |
||
767 | if ($value !== '') { |
||
768 | return preg_replace($s, $r, $value); |
||
769 | } |
||
770 | break; |
||
771 | case 'cat': |
||
772 | case 'concatenate': |
||
773 | case '.': |
||
774 | if ($value !== '') { |
||
775 | return $value . $opt; |
||
776 | } |
||
777 | break; |
||
778 | case 'sprintf': |
||
779 | case 'string_format': |
||
780 | if ($value !== '') { |
||
781 | return sprintf($opt, $value); |
||
782 | } |
||
783 | break; |
||
784 | case 'number_format': |
||
785 | if ($opt == '') { |
||
786 | $opt = 0; |
||
787 | } |
||
788 | |||
789 | return number_format($value, $opt); |
||
790 | case 'money_format': |
||
791 | setlocale(LC_MONETARY, setlocale(LC_TIME, 0)); |
||
792 | if ($value !== '') { |
||
793 | return money_format($opt, (double)$value); |
||
794 | } |
||
795 | break; |
||
796 | case 'tobool': |
||
797 | return boolval($value); |
||
798 | case 'nl2lf': |
||
799 | View Code Duplication | if ($value !== '') { |
|
800 | return str_replace(array("\r\n", "\n", "\r"), '\n', $value); |
||
801 | } |
||
802 | break; |
||
803 | case 'br2nl': |
||
804 | return preg_replace('@<br[\s/]*>@i', "\n", $value); |
||
805 | case 'nl2br': |
||
806 | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
||
807 | return nl2br($value); |
||
808 | } |
||
809 | if ($opt !== '') { |
||
810 | $opt = trim($opt); |
||
811 | $opt = strtolower($opt); |
||
812 | if ($opt === 'false') { |
||
813 | $opt = false; |
||
814 | } elseif ($opt === '0') { |
||
815 | $opt = false; |
||
816 | } else { |
||
817 | $opt = true; |
||
818 | } |
||
819 | } elseif (isset($modx->config['mce_element_format']) && $modx->config['mce_element_format'] === 'html') { |
||
820 | $opt = false; |
||
821 | } else { |
||
822 | $opt = true; |
||
823 | } |
||
824 | |||
825 | return nl2br($value, $opt); |
||
826 | case 'ltrim': |
||
827 | case 'rtrim': |
||
828 | case 'trim': // ref http://mblo.info/modifiers/custom-modifiers/rtrim_opt.html |
||
829 | if ($opt === '') { |
||
830 | return $cmd($value); |
||
831 | } else { |
||
832 | return $cmd($value, $opt); |
||
833 | } |
||
834 | // These are all straight wrappers for PHP functions |
||
835 | case 'ucfirst': |
||
836 | case 'lcfirst': |
||
837 | case 'ucwords': |
||
838 | return $cmd($value); |
||
839 | |||
840 | ##### Date time format |
||
841 | case 'strftime': |
||
842 | case 'date': |
||
843 | case 'dateformat': |
||
844 | if (empty($opt)) { |
||
845 | $opt = $modx->toDateFormat(null, 'formatOnly'); |
||
846 | } |
||
847 | if (!preg_match('@^[0-9]+$@', $value)) { |
||
848 | $value = strtotime($value); |
||
849 | } |
||
850 | if (strpos($opt, '%') !== false) { |
||
851 | return strftime($opt, 0 + $value); |
||
852 | } else { |
||
853 | return date($opt, 0 + $value); |
||
854 | } |
||
855 | case 'time': |
||
856 | if (empty($opt)) { |
||
857 | $opt = '%H:%M'; |
||
858 | } |
||
859 | if (!preg_match('@^[0-9]+$@', $value)) { |
||
860 | $value = strtotime($value); |
||
861 | } |
||
862 | |||
863 | return strftime($opt, 0 + $value); |
||
864 | case 'strtotime': |
||
865 | return strtotime($value); |
||
866 | ##### mathematical function |
||
867 | case 'toint': |
||
868 | return (int)$value; |
||
869 | case 'tofloat': |
||
870 | return floatval($value); |
||
871 | case 'round': |
||
872 | if (!$opt) { |
||
873 | $opt = 0; |
||
874 | } |
||
875 | |||
876 | return $cmd($value, $opt); |
||
877 | case 'max': |
||
878 | case 'min': |
||
879 | return $cmd(explode(',', $value)); |
||
880 | case 'floor': |
||
881 | case 'ceil': |
||
882 | case 'abs': |
||
883 | return $cmd($value); |
||
884 | case 'math': |
||
885 | case 'calc': |
||
886 | $value = (int)$value; |
||
887 | if (empty($value)) { |
||
888 | $value = '0'; |
||
889 | } |
||
890 | $filter = str_replace(array('[+value+]', '[+output+]', '{value}', '%s'), '?', $opt); |
||
891 | $filter = preg_replace('@([a-zA-Z\n\r\t\s])@', '', $filter); |
||
892 | if (strpos($filter, '?') === false) { |
||
893 | $filter = "?{$filter}"; |
||
894 | } |
||
895 | $filter = str_replace('?', $value, $filter); |
||
896 | |||
897 | return eval("return {$filter};"); |
||
898 | case 'count': |
||
899 | if ($value == '') { |
||
900 | return 0; |
||
901 | } |
||
902 | $value = explode(',', $value); |
||
903 | |||
904 | return count($value); |
||
905 | case 'sort': |
||
906 | case 'rsort': |
||
907 | if (strpos($value, "\n") !== false) { |
||
908 | $delim = "\n"; |
||
909 | } else { |
||
910 | $delim = ','; |
||
911 | } |
||
912 | $swap = explode($delim, $value); |
||
913 | if (!$opt) { |
||
914 | $opt = SORT_REGULAR; |
||
915 | } else { |
||
916 | $opt = constant($opt); |
||
917 | } |
||
918 | $cmd($swap, $opt); |
||
919 | |||
920 | return implode($delim, $swap); |
||
921 | ##### Resource fields |
||
922 | case 'id': |
||
923 | if ($opt) { |
||
924 | return $this->getDocumentObject($opt, $key); |
||
925 | } |
||
926 | break; |
||
927 | case 'type': |
||
928 | case 'contenttype': |
||
929 | case 'pagetitle': |
||
930 | case 'longtitle': |
||
931 | case 'description': |
||
932 | case 'alias': |
||
933 | case 'introtext': |
||
934 | case 'link_attributes': |
||
935 | case 'published': |
||
936 | case 'pub_date': |
||
937 | case 'unpub_date': |
||
938 | case 'parent': |
||
939 | case 'isfolder': |
||
940 | case 'content': |
||
941 | case 'richtext': |
||
942 | case 'template': |
||
943 | case 'menuindex': |
||
944 | case 'searchable': |
||
945 | case 'cacheable': |
||
946 | case 'createdby': |
||
947 | case 'createdon': |
||
948 | case 'editedby': |
||
949 | case 'editedon': |
||
950 | case 'deleted': |
||
951 | case 'deletedon': |
||
952 | case 'deletedby': |
||
953 | case 'publishedon': |
||
954 | case 'publishedby': |
||
955 | case 'menutitle': |
||
956 | case 'donthit': |
||
957 | case 'haskeywords': |
||
958 | case 'privateweb': |
||
959 | case 'privatemgr': |
||
960 | case 'content_dispo': |
||
961 | case 'hidemenu': |
||
962 | if ($cmd === 'contenttype') { |
||
963 | $cmd = 'contentType'; |
||
964 | } |
||
965 | |||
966 | return $this->getDocumentObject($value, $cmd); |
||
967 | case 'title': |
||
968 | $pagetitle = $this->getDocumentObject($value, 'pagetitle'); |
||
969 | $longtitle = $this->getDocumentObject($value, 'longtitle'); |
||
970 | |||
971 | return $longtitle ? $longtitle : $pagetitle; |
||
972 | case 'shorttitle': |
||
973 | $pagetitle = $this->getDocumentObject($value, 'pagetitle'); |
||
974 | $menutitle = $this->getDocumentObject($value, 'menutitle'); |
||
975 | |||
976 | return $menutitle ? $menutitle : $pagetitle; |
||
977 | case 'templatename': |
||
978 | $rs = $modx->getDatabase()->select('templatename', '[+prefix+]site_templates', "id='{$value}'"); |
||
979 | $templateName = $modx->getDatabase()->getValue($rs); |
||
980 | |||
981 | return !$templateName ? '(blank)' : $templateName; |
||
982 | case 'getfield': |
||
983 | if (!$opt) { |
||
984 | $opt = 'content'; |
||
985 | } |
||
986 | |||
987 | return $modx->getField($opt, $value); |
||
988 | case 'children': |
||
989 | case 'childids': |
||
990 | if ($value == '') { |
||
991 | $value = 0; |
||
992 | } // 値がない場合はルートと見なす |
||
993 | $published = 1; |
||
994 | if ($opt == '') { |
||
995 | $opt = 'page'; |
||
996 | } |
||
997 | $_ = explode(',', $opt); |
||
998 | $where = array(); |
||
999 | foreach ($_ as $opt) { |
||
1000 | switch (trim($opt)) { |
||
1001 | case 'page'; |
||
1002 | case '!folder'; |
||
1003 | case '!isfolder': |
||
1004 | $where[] = 'sc.isfolder=0'; |
||
1005 | break; |
||
1006 | case 'folder'; |
||
1007 | case 'isfolder': |
||
1008 | $where[] = 'sc.isfolder=1'; |
||
1009 | break; |
||
1010 | case 'menu'; |
||
1011 | case 'show_menu': |
||
1012 | $where[] = 'sc.hidemenu=0'; |
||
1013 | break; |
||
1014 | case '!menu'; |
||
1015 | case '!show_menu': |
||
1016 | $where[] = 'sc.hidemenu=1'; |
||
1017 | break; |
||
1018 | case 'published': |
||
1019 | $published = 1; |
||
1020 | break; |
||
1021 | case '!published': |
||
1022 | $published = 0; |
||
1023 | break; |
||
1024 | } |
||
1025 | } |
||
1026 | $where = implode(' AND ', $where); |
||
1027 | $children = $modx->getDocumentChildren($value, $published, '0', 'id', $where); |
||
1028 | $result = array(); |
||
1029 | foreach ((array)$children as $child) { |
||
1030 | $result[] = $child['id']; |
||
1031 | } |
||
1032 | |||
1033 | return implode(',', $result); |
||
1034 | case 'fullurl': |
||
1035 | if (!is_numeric($value)) { |
||
1036 | return $value; |
||
1037 | } |
||
1038 | |||
1039 | return $modx->makeUrl($value); |
||
1040 | case 'makeurl': |
||
1041 | if (!is_numeric($value)) { |
||
1042 | return $value; |
||
1043 | } |
||
1044 | if (!$opt) { |
||
1045 | $opt = 'full'; |
||
1046 | } |
||
1047 | |||
1048 | return $modx->makeUrl($value, '', '', $opt); |
||
1049 | |||
1050 | ##### File system |
||
1051 | case 'getimageinfo': |
||
1052 | case 'imageinfo': |
||
1053 | if (!is_file($value)) { |
||
1054 | return ''; |
||
1055 | } |
||
1056 | $_ = getimagesize($value); |
||
1057 | if (!$_[0]) { |
||
1058 | return ''; |
||
1059 | } |
||
1060 | $info['width'] = $_[0]; |
||
1061 | $info['height'] = $_[1]; |
||
1062 | if ($_[0] > $_[1]) { |
||
1063 | $info['aspect'] = 'landscape'; |
||
1064 | } elseif ($_[0] < $_[1]) { |
||
1065 | $info['aspect'] = 'portrait'; |
||
1066 | } else { |
||
1067 | $info['aspect'] = 'square'; |
||
1068 | } |
||
1069 | switch ($_[2]) { |
||
1070 | case IMAGETYPE_GIF : |
||
1071 | $info['type'] = 'gif'; |
||
1072 | break; |
||
1073 | case IMAGETYPE_JPEG : |
||
1074 | $info['type'] = 'jpg'; |
||
1075 | break; |
||
1076 | case IMAGETYPE_PNG : |
||
1077 | $info['type'] = 'png'; |
||
1078 | break; |
||
1079 | default : |
||
1080 | $info['type'] = 'unknown'; |
||
1081 | } |
||
1082 | $info['attrib'] = $_[3]; |
||
1083 | switch ($opt) { |
||
1084 | case 'width' : |
||
1085 | return $info['width']; |
||
1086 | case 'height': |
||
1087 | return $info['height']; |
||
1088 | case 'aspect': |
||
1089 | return $info['aspect']; |
||
1090 | case 'type' : |
||
1091 | return $info['type']; |
||
1092 | case 'attrib': |
||
1093 | return $info['attrib']; |
||
1094 | default : |
||
1095 | return print_r($info, true); |
||
1096 | } |
||
1097 | |||
1098 | case 'file_get_contents': |
||
1099 | case 'readfile': |
||
1100 | if (!is_file($value)) { |
||
1101 | return $value; |
||
1102 | } |
||
1103 | $value = realpath($value); |
||
1104 | if (strpos($value, MODX_MANAGER_PATH) !== false) { |
||
1105 | exit('Can not read core file'); |
||
1106 | } |
||
1107 | $ext = strtolower(substr($value, -4)); |
||
1108 | if ($ext === '.php') { |
||
1109 | exit('Can not read php file'); |
||
1110 | } |
||
1111 | if ($ext === '.cgi') { |
||
1112 | exit('Can not read cgi file'); |
||
1113 | } |
||
1114 | |||
1115 | return file_get_contents($value); |
||
1116 | case 'filesize': |
||
1117 | if ($value == '') { |
||
1118 | return ''; |
||
1119 | } |
||
1120 | $filename = $value; |
||
1121 | |||
1122 | $site_url = $modx->config['site_url']; |
||
1123 | if (strpos($filename, $site_url) === 0) { |
||
1124 | $filename = substr($filename, 0, strlen($site_url)); |
||
1125 | } |
||
1126 | $filename = trim($filename, '/'); |
||
1127 | |||
1128 | $opt = trim($opt, '/'); |
||
1129 | if ($opt !== '') { |
||
1130 | $opt .= '/'; |
||
1131 | } |
||
1132 | |||
1133 | $filename = MODX_BASE_PATH . $opt . $filename; |
||
1134 | |||
1135 | if (is_file($filename)) { |
||
1136 | clearstatcache(); |
||
1137 | $size = filesize($filename); |
||
1138 | |||
1139 | return $size; |
||
1140 | } else { |
||
1141 | return ''; |
||
1142 | } |
||
1143 | ##### User info |
||
1144 | case 'username': |
||
1145 | case 'fullname': |
||
1146 | case 'role': |
||
1147 | case 'email': |
||
1148 | case 'phone': |
||
1149 | case 'mobilephone': |
||
1150 | case 'blocked': |
||
1151 | case 'blockeduntil': |
||
1152 | case 'blockedafter': |
||
1153 | case 'logincount': |
||
1154 | case 'lastlogin': |
||
1155 | case 'thislogin': |
||
1156 | case 'failedlogincount': |
||
1157 | case 'dob': |
||
1158 | case 'gender': |
||
1159 | case 'country': |
||
1160 | case 'street': |
||
1161 | case 'city': |
||
1162 | case 'state': |
||
1163 | case 'zip': |
||
1164 | case 'fax': |
||
1165 | case 'photo': |
||
1166 | case 'comment': |
||
1167 | $this->opt = $cmd; |
||
1168 | |||
1169 | return $this->includeMdfFile('moduser'); |
||
1170 | case 'userinfo': |
||
1171 | if (empty($opt)) { |
||
1172 | $this->opt = 'username'; |
||
1173 | } |
||
1174 | |||
1175 | return $this->includeMdfFile('moduser'); |
||
1176 | case 'webuserinfo': |
||
1177 | if (empty($opt)) { |
||
1178 | $this->opt = 'username'; |
||
1179 | } |
||
1180 | $this->value = -$value; |
||
1181 | |||
1182 | return $this->includeMdfFile('moduser'); |
||
1183 | ##### Special functions |
||
1184 | case 'ifempty': |
||
1185 | case '_default': |
||
1186 | case 'default': |
||
1187 | if (empty($value)) { |
||
1188 | return $opt; |
||
1189 | } |
||
1190 | break; |
||
1191 | case 'ifnotempty': |
||
1192 | if (!empty($value)) { |
||
1193 | return $opt; |
||
1194 | } |
||
1195 | break; |
||
1196 | case 'datagrid': |
||
1197 | $grd = new DataGrid(null, trim($value)); |
||
1198 | $grd->itemStyle = ''; |
||
1199 | $grd->altItemStyle = ''; |
||
1200 | $pos = strpos($value, "\n"); |
||
1201 | if ($pos) { |
||
1202 | $_ = substr($value, 0, $pos); |
||
1203 | } else { |
||
1204 | $_ = $pos; |
||
1205 | } |
||
1206 | $grd->cdelim = strpos($_, "\t") !== false ? 'tab' : ','; |
||
1207 | |||
1208 | return $grd->render(); |
||
1209 | case 'rotate': |
||
1210 | case 'evenodd': |
||
1211 | if (strpos($opt, ',') === false) { |
||
1212 | $opt = 'odd,even'; |
||
1213 | } |
||
1214 | $_ = explode(',', $opt); |
||
1215 | $c = count($_); |
||
1216 | $i = $value + $c; |
||
1217 | $i = $i % $c; |
||
1218 | |||
1219 | return $_[$i]; |
||
1220 | case 'takeval': |
||
1221 | $arr = explode(",", $opt); |
||
1222 | $idx = $value; |
||
1223 | if (!is_numeric($idx)) { |
||
1224 | return $value; |
||
1225 | } |
||
1226 | |||
1227 | return $arr[$idx]; |
||
1228 | case 'getimage': |
||
1229 | return $this->includeMdfFile('getimage'); |
||
1230 | case 'nicesize': |
||
1231 | return $modx->nicesize($value); |
||
1232 | case 'googlemap': |
||
1233 | case 'googlemaps': |
||
1234 | if (empty($opt)) { |
||
1235 | $opt = 'border:none;width:500px;height:350px;'; |
||
1236 | } |
||
1237 | $tpl = '<iframe style="[+style+]" src="https://maps.google.co.jp/maps?ll=[+value+]&output=embed&z=15"></iframe>'; |
||
1238 | $ph['style'] = $opt; |
||
1239 | $ph['value'] = $value; |
||
1240 | |||
1241 | return $modx->parseText($tpl, $ph); |
||
1242 | case 'youtube': |
||
1243 | case 'youtube16x9': |
||
1244 | if (empty($opt)) { |
||
1245 | $opt = 560; |
||
1246 | } |
||
1247 | $h = round($opt * 0.5625); |
||
1248 | $tpl = '<iframe width="%s" height="%s" src="https://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>'; |
||
1249 | |||
1250 | return sprintf($tpl, $opt, $h, $value); |
||
1251 | //case 'youtube4x3':%s*0.75+25 |
||
1252 | case 'setvar': |
||
1253 | $modx->placeholders[$opt] = $value; |
||
1254 | |||
1255 | return ''; |
||
1256 | case 'csstohead': |
||
1257 | $modx->regClientCSS($value); |
||
1258 | |||
1259 | return ''; |
||
1260 | case 'htmltohead': |
||
1261 | $modx->regClientStartupHTMLBlock($value); |
||
1262 | |||
1263 | return ''; |
||
1264 | case 'htmltobottom': |
||
1265 | $modx->regClientHTMLBlock($value); |
||
1266 | |||
1267 | return ''; |
||
1268 | case 'jstohead': |
||
1269 | $modx->regClientStartupScript($value); |
||
1270 | |||
1271 | return ''; |
||
1272 | case 'jstobottom': |
||
1273 | $modx->regClientScript($value); |
||
1274 | |||
1275 | return ''; |
||
1276 | case 'dummy': |
||
1277 | return $value; |
||
1278 | |||
1279 | // If we haven't yet found the modifier, let's look elsewhere |
||
1280 | default: |
||
1281 | $value = $this->getValueFromElement($key, $value, $cmd, $opt); |
||
1282 | } |
||
1283 | |||
1284 | return $value; |
||
1285 | } |
||
1286 | |||
1603 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.