PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/manager/includes/tmplvars.commands.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 150 lines | 119 code | 15 blank | 16 comment | 25 complexity | c54ebc12e688a63f6c5195d98c8e190a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /*
  3. * Template Variable Data Source @Bindings
  4. * Created by Raymond Irving Feb, 2005
  5. */
  6. global $BINDINGS; // Array of supported bindings. must be upper case
  7. $BINDINGS = array (
  8. 'FILE',
  9. 'CHUNK',
  10. 'DOCUMENT',
  11. 'SELECT',
  12. 'EVAL',
  13. 'INHERIT',
  14. 'DIRECTORY'
  15. );
  16. function ProcessTVCommand($value, $name = '', $docid = '') {
  17. global $modx;
  18. $etomite = & $modx;
  19. $docid = intval($docid) ? intval($docid) : $modx->documentIdentifier;
  20. $nvalue = trim($value);
  21. if (substr($nvalue, 0, 1) != '@')
  22. return $value;
  23. else {
  24. list ($cmd, $param) = ParseCommand($nvalue);
  25. $cmd = trim($cmd);
  26. switch ($cmd) {
  27. case "FILE" :
  28. $output = ProcessFile(trim($param));
  29. $output = str_replace('@FILE ' . $param, $output, $nvalue);
  30. break;
  31. case "CHUNK" : // retrieve a chunk and process it's content
  32. $chunk = $modx->getChunk($param);
  33. $output = $chunk;
  34. break;
  35. case "DOCUMENT" : // retrieve a document and process it's content
  36. $rs = $modx->getDocument($param);
  37. if (is_array($rs))
  38. $output = $rs['content'];
  39. else
  40. $output = "Unable to locate document $param";
  41. break;
  42. case "SELECT" : // selects a record from the cms database
  43. $rt = array ();
  44. $replacementVars = array (
  45. 'DBASE' => $modx->db->config['dbase'],
  46. 'PREFIX' => $modx->db->config['table_prefix']
  47. );
  48. foreach ($replacementVars as $rvKey => $rvValue) {
  49. $modx->setPlaceholder($rvKey, $rvValue);
  50. }
  51. $param = $modx->mergePlaceholderContent($param);
  52. $rs = $modx->db->query("SELECT $param;");
  53. $output = $rs;
  54. break;
  55. case "EVAL" : // evaluates text as php codes return the results
  56. $output = eval ($param);
  57. break;
  58. case "INHERIT" :
  59. $output = $param; // Default to param value if no content from parents
  60. $doc = $modx->getPageInfo($docid, 0, 'id,parent');
  61. while ($doc['parent'] != 0) {
  62. $parent_id = $doc['parent'];
  63. // Grab document regardless of publish status
  64. $doc = $modx->getPageInfo($parent_id, 0, 'id,parent,published');
  65. if ($doc['parent'] != 0 && !$doc['published'])
  66. continue; // hide unpublished docs if we're not at the top
  67. $tv = $modx->getTemplateVar($name, '*', $doc['id'], $doc['published']);
  68. // inheritance allows other @ bindings to be inherited
  69. // if no value is inherited and there is content following the @INHERIT binding,
  70. // that content will be used as the output
  71. // @todo consider reimplementing *appending* the output the follows an @INHERIT as opposed
  72. // to using it as a default/fallback value; perhaps allow choice in behavior with
  73. // system setting
  74. if ((string) $tv['value'] !== '' && !preg_match('%^@INHERIT[\s\n\r]*$%im', $tv['value'])) {
  75. $output = (string) $tv['value'];
  76. //$output = str_replace('@INHERIT', $output, $nvalue);
  77. break 2;
  78. }
  79. }
  80. break;
  81. case 'DIRECTORY' :
  82. $files = array ();
  83. $path = $modx->config['base_path'] . $param;
  84. if (substr($path, -1, 1) != '/') {
  85. $path .= '/';
  86. }
  87. if (!is_dir($path)) {
  88. die($path);
  89. break;
  90. }
  91. $dir = dir($path);
  92. while (($file = $dir->read()) !== false) {
  93. if (substr($file, 0, 1) != '.') {
  94. $files[] = "{$file}=={$param}{$file}";
  95. }
  96. }
  97. asort($files);
  98. $output = implode('||', $files);
  99. break;
  100. default :
  101. $output = $value;
  102. break;
  103. }
  104. // support for nested bindings
  105. return is_string($output) && ($output != $value) ? ProcessTVCommand($output, $name, $docid) : $output;
  106. }
  107. }
  108. function ProcessFile($file) {
  109. // get the file
  110. if (file_exists($file) && @ $handle = fopen($file, "r")) {
  111. $buffer = "";
  112. while (!feof($handle)) {
  113. $buffer .= fgets($handle, 4096);
  114. }
  115. fclose($handle);
  116. } else {
  117. $buffer = " Could not retrieve document '$file'.";
  118. }
  119. return $buffer;
  120. }
  121. // ParseCommand - separate @ cmd from params
  122. function ParseCommand($binding_string) {
  123. global $BINDINGS;
  124. $match = array ();
  125. $regexp = '/@(' . implode('|', $BINDINGS) . ')\s*(.*)/im'; // Split binding on whitespace
  126. if (preg_match($regexp, $binding_string, $match)) {
  127. // We can't return the match array directly because the first element is the whole string
  128. $binding_array = array (
  129. strtoupper($match[1]),
  130. $match[2]
  131. ); // Make command uppercase
  132. return $binding_array;
  133. }
  134. }
  135. ?>