PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://modx-ja.googlecode.com/
PHP | 150 lines | 124 code | 15 blank | 11 comment | 25 complexity | e9820863ee3fc0872cee7f88ea372a36 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, 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. elseif(empty($modx->config['enable_bindings'])) // jp-edition only
  24. {
  25. return '@Bindings is disabled.';
  26. }
  27. else {
  28. list ($cmd, $param) = ParseCommand($nvalue);
  29. $cmd = trim($cmd);
  30. switch ($cmd) {
  31. case "FILE" :
  32. $output = ProcessFile(trim($param));
  33. $output = str_replace('@FILE ' . $param, $output, $nvalue);
  34. break;
  35. case "CHUNK" : // retrieve a chunk and process it's content
  36. $chunk = $modx->getChunk($param);
  37. $output = $chunk;
  38. break;
  39. case "DOCUMENT" : // retrieve a document and process it's content
  40. $rs = $modx->getDocument($param);
  41. if (is_array($rs))
  42. $output = $rs['content'];
  43. else
  44. $output = "Unable to locate document $param";
  45. break;
  46. case "SELECT" : // selects a record from the cms database
  47. $rt = array ();
  48. $replacementVars = array (
  49. 'DBASE' => $modx->db->config['dbase'],
  50. 'PREFIX' => $modx->db->config['table_prefix']
  51. );
  52. foreach ($replacementVars as $rvKey => $rvValue) {
  53. $modx->setPlaceholder($rvKey, $rvValue);
  54. }
  55. $param = $modx->mergePlaceholderContent($param);
  56. $rs = $modx->db->query("SELECT $param;");
  57. $output = $rs;
  58. break;
  59. case "EVAL" : // evaluates text as php codes return the results
  60. $output = eval ($param);
  61. break;
  62. case "INHERIT" :
  63. $output = $param; // Default to param value if no content from parents
  64. $doc = $modx->getPageInfo($docid, 0, 'id,parent');
  65. while ($doc['parent'] != 0) {
  66. $parent_id = $doc['parent'];
  67. // Grab document regardless of publish status
  68. $doc = $modx->getPageInfo($parent_id, 0, 'id,parent,published');
  69. if ($doc['parent'] != 0 && !$doc['published'])
  70. continue; // hide unpublished docs if we're not at the top
  71. $tv = $modx->getTemplateVar($name, '*', $doc['id'], $doc['published']);
  72. // Modified @INHERIT binding to allow for @FILE bindings (and others) to be inherited,
  73. // as well as allowing content after the @INHERITed values
  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. ?>