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