PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/core/model/modx/modscript.class.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 175 lines | 115 code | 8 blank | 52 comment | 30 complexity | c66a970909d2fc3b1361e5f026e61297 MD5 | raw file
  1. <?php
  2. /**
  3. * @package modx
  4. */
  5. /**
  6. * An element representing executable PHP script content.
  7. *
  8. * {@inheritdoc}
  9. *
  10. * @property int $id
  11. * @property string $name The name of the script
  12. * @property string $description The description of the script
  13. * @property int $editor_type Deprecated
  14. * @property int $category The Category this Script resides in
  15. * @abstract Implement a derivative class that defines a table for storage.
  16. * @package modx
  17. */
  18. class modScript extends modElement {
  19. /**
  20. * The name of the script
  21. * @var string $_scriptName
  22. */
  23. public $_scriptName= null;
  24. /**
  25. * The cache key of the script
  26. * @var string $_scriptCacheKey
  27. */
  28. public $_scriptCacheKey= null;
  29. /**
  30. * Override set to properly strip invalid tags from script code
  31. *
  32. * {@inheritdoc}
  33. */
  34. public function set($k, $v= null, $vType= '') {
  35. if (in_array($k, array('snippet', 'plugincode', 'content'))) {
  36. $v= trim($v);
  37. if (strncmp($v, '<?', 2) == 0) {
  38. $v= substr($v, 2);
  39. if (strncmp($v, 'php', 3) == 0) $v= substr($v, 3);
  40. }
  41. if (substr($v, -2, 2) == '?>') $v= substr($v, 0, -2);
  42. $v= trim($v, " \n\r\0\x0B");
  43. }
  44. $set= parent::set($k, $v, $vType);
  45. return $set;
  46. }
  47. /**
  48. * Process specifically script-related functionality for modScript objects.
  49. *
  50. * {@inheritdoc}
  51. */
  52. public function process($properties= null, $content= null) {
  53. parent :: process($properties, $content);
  54. if (!$this->_processed) {
  55. $scriptName= $this->getScriptName();
  56. $this->_result= function_exists($scriptName);
  57. if (!$this->_result) {
  58. $this->_result= $this->loadScript();
  59. }
  60. if ($this->_result) {
  61. if (empty($this->xpdo->event)) $this->xpdo->event = new stdClass();
  62. $this->xpdo->event->params= $this->_properties; /* store params inside event object */
  63. ob_start();
  64. $this->_output= $scriptName($this->_properties);
  65. $this->_output= ob_get_contents() . $this->_output;
  66. ob_end_clean();
  67. if ($this->_output && is_string($this->_output)) {
  68. /* collect element tags in the evaluated content and process them */
  69. $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
  70. $this->xpdo->parser->processElementTags(
  71. $this->_tag,
  72. $this->_output,
  73. $this->xpdo->parser->isProcessingUncacheable(),
  74. $this->xpdo->parser->isRemovingUnprocessed(),
  75. '[[',
  76. ']]',
  77. array(),
  78. $maxIterations
  79. );
  80. }
  81. $this->filterOutput();
  82. unset ($this->xpdo->event->params);
  83. $this->cache();
  84. }
  85. }
  86. $this->_processed= true;
  87. /* finally, return the processed element content */
  88. return $this->_output;
  89. }
  90. /**
  91. * Get the name of the script source file, written to the cache file system
  92. *
  93. * @return string The filename containing the function generated from the
  94. * script element.
  95. */
  96. public function getScriptCacheKey() {
  97. if ($this->_scriptCacheKey === null) {
  98. $this->_scriptCacheKey= str_replace('_', '/', $this->getScriptName());
  99. }
  100. return $this->_scriptCacheKey;
  101. }
  102. /**
  103. * Get the name of the function the script has been given.
  104. *
  105. * @return string The function name representing this script element.
  106. */
  107. public function getScriptName() {
  108. if ($this->_scriptName === null) {
  109. $className= $this->_class;
  110. $this->_scriptName= 'elements_' . strtolower($className) . '_' . $this->get('id');
  111. }
  112. return $this->_scriptName;
  113. }
  114. /**
  115. * Loads and evaluates the script, returning the result.
  116. *
  117. * @return boolean True if the result of the script is not false.
  118. */
  119. public function loadScript() {
  120. $includeFilename = $this->xpdo->getCachePath() . 'includes/' . $this->getScriptCacheKey() . '.include.cache.php';
  121. $result = file_exists($includeFilename);
  122. $outdated = false;
  123. if ($result && $this->isStatic()) {
  124. $includeMTime = filemtime($includeFilename);
  125. $sourceMTime = filemtime($this->getSourceFile());
  126. $outdated = $sourceMTime > $includeMTime;
  127. }
  128. if (!$result || $outdated) {
  129. $script= false;
  130. if (!$outdated) {
  131. $script= $this->xpdo->cacheManager->get($this->getScriptCacheKey(), array(
  132. xPDO::OPT_CACHE_KEY => $this->xpdo->getOption('cache_scripts_key', null, 'scripts'),
  133. xPDO::OPT_CACHE_HANDLER => $this->xpdo->getOption('cache_scripts_handler', null, $this->xpdo->getOption(xPDO::OPT_CACHE_HANDLER)),
  134. xPDO::OPT_CACHE_FORMAT => (integer) $this->xpdo->getOption('cache_scripts_format', null, $this->xpdo->getOption(xPDO::OPT_CACHE_FORMAT, null, xPDOCacheManager::CACHE_PHP)),
  135. ));
  136. }
  137. if (!$script) {
  138. $script= $this->xpdo->cacheManager->generateScript($this);
  139. }
  140. if (!empty($script)) {
  141. $result = $this->xpdo->cacheManager->writeFile($includeFilename, "<?php\n" . $script);
  142. }
  143. }
  144. if ($result) {
  145. $result = include($includeFilename);
  146. if ($result) {
  147. $result = function_exists($this->getScriptName());
  148. }
  149. }
  150. return ($result !== false);
  151. }
  152. public function getFileContent(array $options = array()) {
  153. $content = parent::getFileContent($options);
  154. $content= trim($content);
  155. if (strncmp($content, '<?', 2) == 0) {
  156. $content= substr($content, 2);
  157. if (strncmp($content, 'php', 3) == 0) $content= substr($content, 3);
  158. }
  159. if (substr($content, -2, 2) == '?>') $content= substr($content, 0, -2);
  160. $content= trim($content, " \n\r\0\x0B");
  161. return $content;
  162. }
  163. public function setFileContent($content, array $options = array()) {
  164. $content = "<?php\n{$content}";
  165. return parent::setFileContent($content, $options);
  166. }
  167. }