modparser.class.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Represents the MODX parser responsible for processing MODX tags.
  12. *
  13. * This class encapsulates all of the functions for collecting and evaluating
  14. * element tags embedded in text content.
  15. *
  16. * @package modx
  17. */
  18. class modParser {
  19. /**
  20. * A reference to the modX instance
  21. * @var modX $modx
  22. */
  23. public $modx= null;
  24. /**
  25. * If the parser is currently processing a tag
  26. * @var bool $_processingTag
  27. */
  28. protected $_processingTag = false;
  29. /**
  30. * If the parser is currently processing an element
  31. * @var bool $_processingElement
  32. */
  33. protected $_processingElement = false;
  34. /**
  35. * If the parser is currently processing an uncacheable tag
  36. * @var bool $_processingUncacheable
  37. */
  38. protected $_processingUncacheable = false;
  39. /**
  40. * If the parser is currently removing all unprocessed tags
  41. * @var bool $_removingUnprocessed
  42. */
  43. protected $_removingUnprocessed = false;
  44. /**
  45. * If the parser has ever processed uncacheable
  46. *
  47. * @var bool $_startedProcessingUncacheable
  48. */
  49. protected $_startedProcessingUncacheable = false;
  50. /**
  51. * @param xPDO $modx A reference to the modX|xPDO instance
  52. */
  53. function __construct(xPDO &$modx) {
  54. $this->modx =& $modx;
  55. }
  56. /**
  57. * Returns true if the parser is currently processing an uncacheable tag
  58. * @return bool
  59. */
  60. public function isProcessingUncacheable() {
  61. $result = false;
  62. if ($this->isProcessingTag() || $this->isProcessingElement()) $result = (boolean) $this->_processingUncacheable;
  63. return $result;
  64. }
  65. /**
  66. * Returns true if the parser has ever processed an uncacheable tag
  67. * @return bool
  68. */
  69. public function startedProcessingUncacheable() {
  70. return $this->_startedProcessingUncacheable;
  71. }
  72. /**
  73. * Returns true if the parser is currently removing any unprocessed tags
  74. * @return bool
  75. */
  76. public function isRemovingUnprocessed() {
  77. $result = false;
  78. if ($this->isProcessingTag() || $this->isProcessingElement()) $result = (boolean) $this->_removingUnprocessed;
  79. return $result;
  80. }
  81. /**
  82. * Returns true if the parser is currently processing a tag
  83. * @return bool
  84. */
  85. public function isProcessingTag() {
  86. return (boolean) $this->_processingTag;
  87. }
  88. /**
  89. * Returns true if the parser is currently processing an element
  90. * @return bool
  91. */
  92. public function isProcessingElement() {
  93. return (boolean) $this->_processingElement;
  94. }
  95. public function setProcessingElement($arg = null) {
  96. if (is_bool($arg)) {
  97. $this->_processingElement = $arg;
  98. } elseif ($arg === null) {
  99. $this->_processingElement = !$this->_processingElement ? true : false;
  100. } else {
  101. $this->_processingElement = (boolean)$arg;
  102. }
  103. }
  104. /**
  105. * Collects element tags in a string and injects them into an array.
  106. *
  107. * @param string $origContent The content to collect tags from.
  108. * @param array &$matches An array in which the collected tags will be
  109. * stored (by reference)
  110. * @param string $prefix The characters that define the start of a tag
  111. * (default= "[[").
  112. * @param string $suffix The characters that define the end of a tag
  113. * (default= "]]").
  114. * @return integer The number of tags collected from the content.
  115. */
  116. public function collectElementTags($origContent, array &$matches, $prefix= '[[', $suffix= ']]') {
  117. $matchCount= 0;
  118. if (!empty ($origContent) && is_string($origContent) && strpos($origContent, $prefix) !== false) {
  119. $openCount= 0;
  120. $offset= 0;
  121. $openPos= 0;
  122. $closePos= 0;
  123. if (($startPos= strpos($origContent, $prefix)) === false) {
  124. return $matchCount;
  125. }
  126. $offset= $startPos +strlen($prefix);
  127. if (($stopPos= strrpos($origContent, $suffix)) === false) {
  128. return $matchCount;
  129. }
  130. $stopPos= $stopPos + strlen($suffix);
  131. $length= $stopPos - $startPos;
  132. $content= $origContent;
  133. while ($length > 0) {
  134. $openCount= 0;
  135. $content= substr($content, $startPos);
  136. $openPos= 0;
  137. $offset= strlen($prefix);
  138. if (($closePos= strpos($content, $suffix, $offset)) === false) {
  139. break;
  140. }
  141. $nextOpenPos= strpos($content, $prefix, $offset);
  142. while ($nextOpenPos !== false && $nextOpenPos < $closePos) {
  143. $openCount++;
  144. $offset= $nextOpenPos + strlen($prefix);
  145. $nextOpenPos= strpos($content, $prefix, $offset);
  146. }
  147. $nextClosePos= strpos($content, $suffix, $closePos + strlen($suffix));
  148. while ($openCount > 0 && $nextClosePos !== false) {
  149. $openCount--;
  150. $closePos= $nextClosePos;
  151. $nextOpenPos= strpos($content, $prefix, $offset);
  152. while ($nextOpenPos !== false && $nextOpenPos < $closePos) {
  153. $openCount++;
  154. $offset= $nextOpenPos + strlen($prefix);
  155. $nextOpenPos= strpos($content, $prefix, $offset);
  156. }
  157. $nextClosePos= strpos($content, $suffix, $closePos + strlen($suffix));
  158. }
  159. $closePos= $closePos +strlen($suffix);
  160. $outerTagLength= $closePos - $openPos;
  161. $innerTagLength= ($closePos -strlen($suffix)) - ($openPos +strlen($prefix));
  162. $matches[$matchCount][0]= substr($content, $openPos, $outerTagLength);
  163. $matches[$matchCount][1]= substr($content, ($openPos +strlen($prefix)), $innerTagLength);
  164. $matchCount++;
  165. if ($nextOpenPos === false) {
  166. $nextOpenPos= strpos($content, $prefix, $closePos);
  167. }
  168. if ($nextOpenPos !== false) {
  169. $startPos= $nextOpenPos;
  170. $length= $length - $nextOpenPos;
  171. } else {
  172. $length= 0;
  173. }
  174. }
  175. }
  176. if ($this->modx->getDebug() === true && !empty($matches)) {
  177. $this->modx->log(modX::LOG_LEVEL_DEBUG, "modParser::collectElementTags \$matches = " . print_r($matches, 1) . "\n");
  178. /* $this->modx->cacheManager->writeFile(MODX_CORE_PATH . 'logs/parser.log', print_r($matches, 1) . "\n", 'a'); */
  179. }
  180. return $matchCount;
  181. }
  182. /**
  183. * Collects and processes any set of tags as defined by a prefix and suffix.
  184. *
  185. * @param string $parentTag The tag representing the element processing this
  186. * tag. Pass an empty string to allow parsing without this recursion check.
  187. * @param string $content The content to process and act on (by reference).
  188. * @param boolean $processUncacheable Determines if noncacheable tags are to
  189. * be processed (default= false).
  190. * @param boolean $removeUnprocessed Determines if unprocessed tags should
  191. * be left in place in the content, or stripped out (default= false).
  192. * @param string $prefix The characters that define the start of a tag
  193. * (default= "[[").
  194. * @param string $suffix The characters that define the end of a tag
  195. * (default= "]]").
  196. * @param array $tokens Indicates that the parser should only process tags
  197. * with the tokens included in this array.
  198. * @param integer $depth The maximum iterations to recursively process tags
  199. * returned by prior passes, 0 by default.
  200. * @return int The number of processed tags
  201. */
  202. public function processElementTags($parentTag, & $content, $processUncacheable= false, $removeUnprocessed= false, $prefix= "[[", $suffix= "]]", $tokens= array (), $depth= 0) {
  203. if ($processUncacheable) {
  204. $this->_startedProcessingUncacheable = true;
  205. }
  206. $_processingTag = $this->_processingTag;
  207. $_processingUncacheable = $this->_processingUncacheable;
  208. $_removingUnprocessed = $this->_removingUnprocessed;
  209. $this->_processingTag = true;
  210. $this->_processingUncacheable = (boolean) $processUncacheable;
  211. $this->_removingUnprocessed = (boolean) $removeUnprocessed;
  212. $depth = $depth > 0 ? $depth - 1 : 0;
  213. $processed= 0;
  214. $tags= array ();
  215. /* invoke OnParseDocument event */
  216. $this->modx->documentOutput = $content;
  217. $this->modx->invokeEvent('OnParseDocument', array('content' => &$content));
  218. $content = $this->modx->documentOutput;
  219. unset($this->modx->documentOutput);
  220. if ($collected= $this->collectElementTags($content, $tags, $prefix, $suffix, $tokens)) {
  221. $tagMap= array ();
  222. foreach ($tags as $tag) {
  223. $token= substr($tag[1], 0, 1);
  224. if (!$processUncacheable && $token === '!') {
  225. if ($removeUnprocessed) {
  226. $tagMap[$tag[0]]= '';
  227. }
  228. }
  229. elseif (!empty ($tokens) && !in_array($token, $tokens)) {
  230. $collected--;
  231. continue;
  232. }
  233. if ($tag[0] === $parentTag) {
  234. $tagMap[$tag[0]]= '';
  235. $processed++;
  236. continue;
  237. }
  238. $tagOutput= $this->processTag($tag, $processUncacheable);
  239. if (($tagOutput === null || $tagOutput === false) && $removeUnprocessed) {
  240. $tagMap[$tag[0]]= '';
  241. $processed++;
  242. }
  243. elseif ($tagOutput !== null && $tagOutput !== false) {
  244. $tagMap[$tag[0]]= $tagOutput;
  245. if ($tag[0] !== $tagOutput) $processed++;
  246. }
  247. }
  248. $this->mergeTagOutput($tagMap, $content);
  249. if ($processed > 0 && $depth > 0) {
  250. $processed+= $this->processElementTags($parentTag, $content, $processUncacheable, $removeUnprocessed, $prefix, $suffix, $tokens, $depth);
  251. }
  252. }
  253. $this->_removingUnprocessed = $_removingUnprocessed;
  254. $this->_processingUncacheable = $_processingUncacheable;
  255. $this->_processingTag = $_processingTag;
  256. return $processed;
  257. }
  258. /**
  259. * Merges processed tag output into provided content string.
  260. *
  261. * @param array $tagMap An array with full tags as keys and processed output
  262. * as the values.
  263. * @param string $content The content to merge the tag output with (passed by
  264. * reference).
  265. */
  266. public function mergeTagOutput(array $tagMap, & $content) {
  267. if (!empty ($content) && is_array($tagMap) && !empty ($tagMap)) {
  268. $content= str_replace(array_keys($tagMap), array_values($tagMap), $content);
  269. }
  270. }
  271. /**
  272. * Parses an element/tag property string or array definition.
  273. *
  274. * @param string $propSource A valid property string or array source to
  275. * parse.
  276. * @return array An associative array of property values parsed from
  277. * the property string or array definition.
  278. */
  279. public function parseProperties($propSource) {
  280. $properties= array ();
  281. if (!empty ($propSource)) {
  282. if (is_string($propSource)) {
  283. $properties = $this->parsePropertyString($propSource, true);
  284. } elseif (is_array($propSource)) {
  285. foreach ($propSource as $propName => &$property) {
  286. if (is_array($property) && array_key_exists('value', $property)) {
  287. $properties[$propName]= $property['value'];
  288. } else {
  289. $properties[$propName]= &$property;
  290. }
  291. }
  292. }
  293. }
  294. return $properties;
  295. }
  296. /**
  297. * Parses an element/tag property string and returns an array of properties.
  298. *
  299. * @param string $string The property string to parse.
  300. * @param boolean $valuesOnly Indicates only the property value should be
  301. * returned.
  302. * @return array The processed properties in array format
  303. */
  304. public function parsePropertyString($string, $valuesOnly = false) {
  305. $properties = array();
  306. $tagProps= xPDO :: escSplit("&", $string);
  307. foreach ($tagProps as $prop) {
  308. $property= xPDO :: escSplit('=', $prop);
  309. if (count($property) == 2) {
  310. $propName= $property[0];
  311. if (substr($propName, 0, 4) == "amp;") {
  312. $propName= substr($propName, 4);
  313. }
  314. $propValue= $property[1];
  315. $propType= 'textfield';
  316. $propDesc= '';
  317. $propOptions= array();
  318. $pvTmp= xPDO :: escSplit(';', $propValue);
  319. if ($pvTmp && isset ($pvTmp[1])) {
  320. $propDesc= $pvTmp[0];
  321. if (($pvTmp[1]=='list' || $pvTmp[1]=='combo') && isset($pvTmp[3]) && $pvTmp[3]) {
  322. if (!$valuesOnly) {
  323. $propType = modParser::_XType($pvTmp[1]);
  324. $options = explode(',', $pvTmp[2]);
  325. if ($options) {
  326. foreach ($options as $option) $propOptions[] = array('name' => ucfirst($option), 'value' => $option);
  327. }
  328. }
  329. $propValue = $pvTmp[3];
  330. }
  331. elseif ($pvTmp[1]!='list' && $pvTmp[1]!='combo' && isset($pvTmp[2]) && $pvTmp[2]) {
  332. if (!$valuesOnly) {
  333. $propType = modParser::_XType($pvTmp[1]);
  334. }
  335. $propValue = $pvTmp[2];
  336. } else {
  337. $propValue = $pvTmp[0];
  338. }
  339. }
  340. if ($propValue[0] == '`' && $propValue[strlen($propValue) - 1] == '`') {
  341. $propValue= substr($propValue, 1, strlen($propValue) - 2);
  342. }
  343. $propValue= str_replace("``", "`", $propValue);
  344. if ($valuesOnly) {
  345. $properties[$propName]= $propValue;
  346. } else {
  347. $properties[$propName]= array(
  348. 'name' => $propName,
  349. 'desc' => $propDesc,
  350. 'type' => $propType,
  351. 'options' => $propOptions,
  352. 'value' => $propValue
  353. );
  354. }
  355. }
  356. }
  357. return $properties;
  358. }
  359. /**
  360. * Converts legacy property string types to xtypes.
  361. *
  362. * @access protected
  363. * @param string $type A property type string.
  364. * @return string A valid xtype.
  365. */
  366. protected function _XType($type) {
  367. $xtype = $type;
  368. switch ($type) {
  369. case 'string':
  370. $xtype = 'textfield';
  371. break;
  372. case 'int':
  373. case 'integer':
  374. case 'float':
  375. $xtype = 'numberfield';
  376. break;
  377. case 'bool':
  378. case 'boolean':
  379. $xtype = 'checkbox';
  380. break;
  381. case 'list':
  382. break;
  383. default:
  384. if (!in_array($xtype, array('checkbox', 'combo', 'datefield', 'numberfield', 'radio', 'textarea', 'textfield', 'timefield'))) {
  385. $xtype = 'textfield';
  386. }
  387. break;
  388. }
  389. return $xtype;
  390. }
  391. /**
  392. * Processes a modElement tag and returns the result.
  393. *
  394. * @param string $tag A full tag string parsed from content.
  395. * @param boolean $processUncacheable
  396. * @return mixed The output of the processed element represented by the
  397. * specified tag.
  398. */
  399. public function processTag($tag, $processUncacheable = true) {
  400. $this->_processingTag = true;
  401. $element= null;
  402. $elementOutput= null;
  403. $outerTag= $tag[0];
  404. $innerTag= $tag[1];
  405. /* Avoid all processing for comment tags, e.g. [[- comments here]] */
  406. if (substr($innerTag, 0, 1) === '-') {
  407. return "";
  408. }
  409. /* collect any nested element tags in the innerTag and process them */
  410. $this->processElementTags($outerTag, $innerTag, $processUncacheable);
  411. $this->_processingTag = true;
  412. $outerTag= '[[' . $innerTag . ']]';
  413. $tagParts= xPDO :: escSplit('?', $innerTag, '`', 2);
  414. $tagName= trim($tagParts[0]);
  415. $tagPropString= null;
  416. if (isset ($tagParts[1])) {
  417. $tagPropString= trim($tagParts[1]);
  418. }
  419. $token= substr($tagName, 0, 1);
  420. $tokenOffset= 0;
  421. $cacheable= true;
  422. if ($token === '!') {
  423. if (!$processUncacheable) {
  424. $this->_processingTag = false;
  425. return $outerTag;
  426. }
  427. $cacheable= false;
  428. $tokenOffset++;
  429. $token= substr($tagName, $tokenOffset, 1);
  430. } elseif (!$processUncacheable && strpos($tagPropString, '[[!') !== false) {
  431. $this->modx->log(xPDO::LOG_LEVEL_WARN, "You should not call uncached elements inside cached!\nOuter tag: {$tag[0]}\nInner tag {$innerTag}");
  432. $this->_processingTag = false;
  433. return $outerTag;
  434. }
  435. if ($cacheable && $token !== '+') {
  436. $elementOutput= $this->loadFromCache($outerTag);
  437. }
  438. $_restoreProcessingUncacheable = $this->_processingUncacheable;
  439. /* stop processing uncacheable tags so they are not cached in the cacheable content */
  440. if ($this->_processingUncacheable && $cacheable && $this->modx->getOption('parser_recurse_uncacheable', null, true)) {
  441. $this->_processingUncacheable = false;
  442. }
  443. if ($elementOutput === null) {
  444. switch ($token) {
  445. case '-':
  446. $elementOutput = '';
  447. break;
  448. case '+':
  449. $tagName= substr($tagName, 1 + $tokenOffset);
  450. $element= new modPlaceholderTag($this->modx);
  451. $element->set('name', $tagName);
  452. $element->setTag($outerTag);
  453. $elementOutput= $element->process($tagPropString);
  454. break;
  455. case '%':
  456. $tagName= substr($tagName, 1 + $tokenOffset);
  457. $element= new modLexiconTag($this->modx);
  458. $element->set('name', $tagName);
  459. $element->setTag($outerTag);
  460. $element->setCacheable($cacheable);
  461. $elementOutput= $element->process($tagPropString);
  462. break;
  463. case '~':
  464. $tagName= substr($tagName, 1 + $tokenOffset);
  465. $element= new modLinkTag($this->modx);
  466. $element->set('name', $tagName);
  467. $element->setTag($outerTag);
  468. $element->setCacheable($cacheable);
  469. $elementOutput= $element->process($tagPropString);
  470. break;
  471. case '$':
  472. $tagName= substr($tagName, 1 + $tokenOffset);
  473. if ($element= $this->getElement('modChunk', $tagName)) {
  474. $element->set('name', $tagName);
  475. $element->setTag($outerTag);
  476. $element->setCacheable($cacheable);
  477. $elementOutput= $element->process($tagPropString);
  478. }
  479. break;
  480. case '*':
  481. $tagName= substr($tagName, 1 + $tokenOffset);
  482. $nextToken= substr($tagName, 0, 1);
  483. if ($nextToken === '#') {
  484. $tagName= substr($tagName, 1);
  485. }
  486. if (is_array($this->modx->resource->_fieldMeta) && in_array($this->realname($tagName), array_keys($this->modx->resource->_fieldMeta))) {
  487. $element= new modFieldTag($this->modx);
  488. $element->set('name', $tagName);
  489. $element->setTag($outerTag);
  490. $element->setCacheable($cacheable);
  491. $elementOutput= $element->process($tagPropString);
  492. }
  493. else {
  494. $element = $this->getElement('modTemplateVar', $tagName);
  495. // If our element tag was not found (e.i. not an existing TV), create a new instance of
  496. // modFieldTag. We do this to make it possible to use output modifiers such as default. This
  497. // mirrors the behavior of placeholders.
  498. if ($element === false) {
  499. $element = new modFieldTag($this->modx);
  500. }
  501. $element->set('name', $tagName);
  502. $element->setTag($outerTag);
  503. $element->setCacheable($cacheable);
  504. $elementOutput= $element->process($tagPropString);
  505. }
  506. break;
  507. default:
  508. $tagName= substr($tagName, $tokenOffset);
  509. if ($element= $this->getElement('modSnippet', $tagName)) {
  510. $element->set('name', $tagName);
  511. $element->setTag($outerTag);
  512. $element->setCacheable($cacheable);
  513. $elementOutput= $element->process($tagPropString);
  514. }
  515. elseif(!empty($tagName)) {
  516. if ($this->modx->getOption('log_snippet_not_found', null, false)) {
  517. $this->modx->log(xPDO::LOG_LEVEL_ERROR, "Could not find snippet with name {$tagName}.");
  518. }
  519. }
  520. }
  521. }
  522. if (($elementOutput === null || $elementOutput === false) && $outerTag !== $tag[0]) {
  523. $elementOutput = $outerTag;
  524. }
  525. if ($this->modx->getDebug() === true) {
  526. $this->modx->log(xPDO::LOG_LEVEL_DEBUG, "Processing {$outerTag} as {$innerTag} using tagname {$tagName}:\n" . print_r($elementOutput, 1) . "\n\n");
  527. /* $this->modx->cacheManager->writeFile(MODX_BASE_PATH . 'parser.log', "Processing {$outerTag} as {$innerTag}:\n" . print_r($elementOutput, 1) . "\n\n", 'a'); */
  528. }
  529. $this->_processingTag = false;
  530. $this->_processingUncacheable = $_restoreProcessingUncacheable;
  531. return $elementOutput;
  532. }
  533. /**
  534. * Get a modElement instance taking advantage of the modX::$sourceCache.
  535. *
  536. * @param string $class The modElement derivative class to load.
  537. * @param string $name An element name or raw tagName to identify the modElement instance.
  538. * @return modElement|null An instance of the specified modElement derivative class.
  539. */
  540. public function getElement($class, $name) {
  541. $realname = $this->realname($name);
  542. if (array_key_exists($class, $this->modx->sourceCache) && array_key_exists($realname, $this->modx->sourceCache[$class])) {
  543. /** @var modElement $element */
  544. $element = $this->modx->newObject($class);
  545. $element->fromArray($this->modx->sourceCache[$class][$realname]['fields'], '', true, true);
  546. $element->setPolicies($this->modx->sourceCache[$class][$realname]['policies']);
  547. if (!empty($this->modx->sourceCache[$class][$realname]['source'])) {
  548. if (!empty($this->modx->sourceCache[$class][$realname]['source']['class_key'])) {
  549. $sourceClassKey = $this->modx->sourceCache[$class][$realname]['source']['class_key'];
  550. $this->modx->loadClass('sources.modMediaSource');
  551. /* @var modMediaSource $source */
  552. $source = $this->modx->newObject($sourceClassKey);
  553. $source->fromArray($this->modx->sourceCache[$class][$realname]['source'],'',true,true);
  554. $element->addOne($source,'Source');
  555. }
  556. }
  557. } else {
  558. /** @var modElement $element */
  559. $element = $this->modx->getObjectGraph($class,array('Source' => array()),array('name' => $realname), true);
  560. if ($element && array_key_exists($class, $this->modx->sourceCache)) {
  561. $this->modx->sourceCache[$class][$realname] = array(
  562. 'fields' => $element->toArray(),
  563. 'policies' => $element->getPolicies(),
  564. 'source' => $element->Source ? $element->Source->toArray() : array(),
  565. );
  566. }
  567. elseif(!$element) {
  568. $evtOutput = $this->modx->invokeEvent('OnElementNotFound', array('class' => $class, 'name' => $realname));
  569. $element = false;
  570. if ($evtOutput != false) {
  571. foreach ((array) $evtOutput as $elm) {
  572. if (!empty($elm) && is_string($elm)) {
  573. $element = $this->modx->newObject($class, array(
  574. 'name' => $realname,
  575. 'snippet' => $elm
  576. ));
  577. }
  578. elseif ($elm instanceof modElement ) {
  579. $element = $elm;
  580. }
  581. if ($element) {
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. if ($element instanceof modElement) {
  589. $element->set('name', $name);
  590. }
  591. return $element;
  592. }
  593. /**
  594. * Gets the real name of an element containing filter modifiers.
  595. *
  596. * @param string $unfiltered The unfiltered name of a {@link modElement}.
  597. * @return string The name minus any filter modifiers.
  598. */
  599. public function realname($unfiltered) {
  600. $filtered= $unfiltered;
  601. $split= xPDO :: escSplit(':', $filtered);
  602. if ($split && isset($split[0])) {
  603. $filtered= $split[0];
  604. $propsetSplit = xPDO :: escSplit('@', $filtered);
  605. if ($propsetSplit && isset($propsetSplit[0])) {
  606. $filtered= $propsetSplit[0];
  607. }
  608. }
  609. return $filtered;
  610. }
  611. /**
  612. * Loads output cached by complete tag signature from the elementCache.
  613. *
  614. * @uses modX::$_elementCache Stores all cacheable content from processed
  615. * elements.
  616. * @param string $tag The tag signature representing the element instance.
  617. * @return string The cached output from the element instance.
  618. */
  619. public function loadFromCache($tag) {
  620. $elementOutput= null;
  621. if (isset ($this->modx->elementCache[$tag])) {
  622. $elementOutput= (string) $this->modx->elementCache[$tag];
  623. }
  624. return $elementOutput;
  625. }
  626. }
  627. /**
  628. * Abstract class representing a pseudo-element that can be parsed.
  629. *
  630. * @abstract You must implement the process() method on derivatives to implement
  631. * a parseable element tag. All element tags are identified by a unique single
  632. * character token at the beginning of the tag string.
  633. * @package modx
  634. */
  635. abstract class modTag {
  636. /**
  637. * A reference to the modX instance
  638. * @var modX $modx
  639. */
  640. public $modx= null;
  641. /**
  642. * The name of the tag
  643. * @var string $name
  644. */
  645. public $name;
  646. /**
  647. * The properties on the tag
  648. * @var array $properties
  649. */
  650. public $properties;
  651. /**
  652. * The content of the tag
  653. * @var string $_content
  654. */
  655. public $_content= null;
  656. /**
  657. * The processed output of the tag
  658. * @var string $_output
  659. */
  660. public $_output= '';
  661. /**
  662. * The result of processing the tag
  663. * @var bool $_result
  664. */
  665. public $_result= true;
  666. /**
  667. * Just the isolated properties part of the tag string
  668. * @var string $_propertyString
  669. */
  670. public $_propertyString= '';
  671. /**
  672. * The arranged properties array for this tag
  673. * @var array $_properties
  674. */
  675. public $_properties= array();
  676. /**
  677. * Whether or not the tag has been processed
  678. * @var boolean $_processed
  679. */
  680. public $_processed= false;
  681. /**
  682. * The tag string
  683. * @var string $_tag
  684. */
  685. public $_tag= '';
  686. /**
  687. * The tag initial token ($,%,*,etc)
  688. * @var string $_token
  689. */
  690. public $_token= '';
  691. /**
  692. * Fields on the tag
  693. * @var array $_fields
  694. */
  695. public $_fields= array(
  696. 'name' => '',
  697. 'properties' => ''
  698. );
  699. /**
  700. * Whether or not this tag is marked as cacheable
  701. * @var boolean $_cacheable
  702. */
  703. public $_cacheable= true;
  704. /**
  705. * Any output/input filters on this tag
  706. * @var array $_filters
  707. */
  708. public $_filters= array('input' => null, 'output' => null);
  709. /**
  710. * Set a reference to the modX object, load the name and properties, and instantiate the tag class instance.
  711. * @param modX $modx A reference to the modX object
  712. */
  713. function __construct(modX &$modx) {
  714. $this->modx =& $modx;
  715. $this->name =& $this->_fields['name'];
  716. $this->properties =& $this->_fields['properties'];
  717. }
  718. /**
  719. * Generic getter method for modTag attributes.
  720. *
  721. * @see xPDOObject::get()
  722. * @param string $k The field key.
  723. * @return mixed The value of the field or null if it is not set.
  724. */
  725. public function get($k) {
  726. $value = null;
  727. if (array_key_exists($k, $this->_fields)) {
  728. if ($k == 'properties') {
  729. $value = is_string($this->_fields[$k]) && !empty($this->_fields[$k])
  730. ? unserialize($this->_fields[$k])
  731. : array();
  732. } else {
  733. $value = $this->_fields[$k];
  734. }
  735. }
  736. return $value;
  737. }
  738. /**
  739. * Generic setter method for modTag attributes.
  740. *
  741. * @see xPDOObject::set()
  742. * @param string $k The field key.
  743. * @param mixed $v The value to assign to the field.
  744. */
  745. public function set($k, $v) {
  746. if ($k == 'properties') {
  747. $v = is_array($v) ? serialize($v) : $v;
  748. }
  749. $this->_fields[$k]= $v;
  750. }
  751. /**
  752. * Cache the element into the elementCache by tag signature.
  753. * @see modElement::cache()
  754. */
  755. public function cache() {
  756. if ($this->isCacheable()) {
  757. $this->modx->elementCache[$this->_tag]= $this->_output;
  758. }
  759. }
  760. /**
  761. * Returns the current token for the tag
  762. *
  763. * @return string The token for the tag
  764. */
  765. public function getToken() {
  766. return $this->_token;
  767. }
  768. /**
  769. * Setter method for the token class var.
  770. *
  771. * @param string $token The token to use for this element tag.
  772. */
  773. public function setToken($token) {
  774. $this->_token = $token;
  775. }
  776. /**
  777. * Setter method for the tag class var.
  778. *
  779. * @param string $tag The tag to use for this element.
  780. */
  781. public function setTag($tag) {
  782. $this->_tag = $tag;
  783. }
  784. /**
  785. * Gets a tag representation of the modTag instance.
  786. *
  787. * @return string
  788. */
  789. public function getTag() {
  790. if (empty($this->_tag) && ($name = $this->get('name'))) {
  791. $propTemp = array();
  792. if (empty($this->_propertyString) && !empty($this->_properties)) {
  793. foreach ($this->_properties as $key => $value) {
  794. $propTemp[] = trim($key) . '=`' . $value . '`';
  795. }
  796. if (!empty($propTemp)) {
  797. $this->_propertyString = '?' . implode('&', $propTemp);
  798. }
  799. }
  800. $tag = '[[';
  801. $tag.= $this->getToken();
  802. $tag.= $name;
  803. if (!empty($this->_propertyString)) {
  804. $tag.= $this->_propertyString;
  805. }
  806. $tag.= ']]';
  807. $this->_tag = $tag;
  808. }
  809. if (empty($this->_tag)) {
  810. $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Instance of ' . get_class($this) . ' produced an empty tag!');
  811. }
  812. return $this->_tag;
  813. }
  814. /**
  815. * Process the tag and return the result.
  816. *
  817. * @see modElement::process()
  818. * @param array|string $properties An array of properties or a formatted
  819. * property string.
  820. * @param string $content Optional content to use for the element
  821. * processing.
  822. * @return mixed The result of processing the tag.
  823. */
  824. public function process($properties= null, $content= null) {
  825. $this->modx->getParser();
  826. $this->modx->parser->setProcessingElement(true);
  827. $this->getProperties($properties);
  828. $this->getTag();
  829. $this->filterInput();
  830. if ($this->modx->getDebug() === true) $this->modx->log(xPDO::LOG_LEVEL_DEBUG, "Processing Element: " . $this->get('name') . ($this->_tag ? "\nTag: {$this->_tag}" : "\n") . "\nProperties: " . print_r($this->_properties, true));
  831. if ($this->isCacheable() && isset ($this->modx->elementCache[$this->_tag])) {
  832. $this->_output = $this->modx->elementCache[$this->_tag];
  833. $this->_processed = true;
  834. } else {
  835. $this->getContent(is_string($content) ? array('content' => $content) : array());
  836. }
  837. return $this->_result;
  838. }
  839. /**
  840. * Get an input filter instance configured for this Element.
  841. *
  842. * @return modInputFilter|null An input filter instance (or null if one cannot be loaded).
  843. */
  844. public function & getInputFilter() {
  845. if (!isset ($this->_filters['input']) || !($this->_filters['input'] instanceof modInputFilter)) {
  846. if (!$inputFilterClass= $this->get('input_filter')) {
  847. $inputFilterClass = $this->modx->getOption('input_filter',null,'filters.modInputFilter');
  848. }
  849. if ($filterClass= $this->modx->loadClass($inputFilterClass, '', false, true)) {
  850. if ($filter= new $filterClass($this->modx)) {
  851. $this->_filters['input']= $filter;
  852. }
  853. }
  854. }
  855. return $this->_filters['input'];
  856. }
  857. /**
  858. * Get an output filter instance configured for this Element.
  859. *
  860. * @return modOutputFilter|null An output filter instance (or null if one cannot be loaded).
  861. */
  862. public function & getOutputFilter() {
  863. if (!isset ($this->_filters['output']) || !($this->_filters['output'] instanceof modOutputFilter)) {
  864. if (!$outputFilterClass= $this->get('output_filter')) {
  865. $outputFilterClass = $this->modx->getOption('output_filter',null,'filters.modOutputFilter');
  866. }
  867. if ($filterClass= $this->modx->loadClass($outputFilterClass, '', false, true)) {
  868. if ($filter= new $filterClass($this->modx)) {
  869. $this->_filters['output']= $filter;
  870. }
  871. }
  872. }
  873. return $this->_filters['output'];
  874. }
  875. /**
  876. * Apply an input filter to a tag.
  877. *
  878. * This is called by default in {@link modTag::process()} after the tag
  879. * properties have been parsed.
  880. *
  881. * @see modElement::filterInput()
  882. */
  883. public function filterInput() {
  884. $filter = $this->getInputFilter();
  885. if ($filter !== null && $filter instanceof modInputFilter) {
  886. $filter->filter($this);
  887. }
  888. }
  889. /**
  890. * Apply an output filter to a tag.
  891. *
  892. * Call this method in your {modTag::process()} implementation when it is
  893. * appropriate, typically once all processing has been completed, but before
  894. * any caching takes place.
  895. *
  896. * @see modElement::filterOutput()
  897. */
  898. public function filterOutput() {
  899. $filter = $this->getOutputFilter();
  900. if ($filter !== null && $filter instanceof modOutputFilter) {
  901. $filter->filter($this);
  902. }
  903. }
  904. /**
  905. * Get the raw source content of the tag element.
  906. *
  907. * @param array $options An array of options implementations can use to
  908. * accept language, revision identifiers, or other information to alter the
  909. * behavior of the method.
  910. * @return string The raw source content for the element.
  911. */
  912. public function getContent(array $options = array()) {
  913. if (!$this->isCacheable() || !is_string($this->_content) || $this->_content === '') {
  914. if (isset($options['content'])) {
  915. $this->_content = $options['content'];
  916. } else {
  917. $this->_content = $this->get('name');
  918. }
  919. }
  920. return $this->_content;
  921. }
  922. /**
  923. * Set the raw source content for the tag element.
  924. *
  925. * @param string $content The content to set
  926. * @param array $options Ignored.
  927. * @return boolean
  928. */
  929. public function setContent($content, array $options = array()) {
  930. return $this->set('name', $content);
  931. }
  932. /**
  933. * Get the properties for this element instance for processing.
  934. *
  935. * @param array|string $properties An array or string of properties to apply.
  936. * @return array A simple array of properties ready to use for processing.
  937. */
  938. public function getProperties($properties = null) {
  939. $this->_properties= $this->modx->parser->parseProperties($this->get('properties'));
  940. $set= $this->getPropertySet();
  941. if (!empty($set)) {
  942. $this->_properties= array_merge($this->_properties, $set);
  943. }
  944. if (!empty($properties)) {
  945. $this->_properties= array_merge($this->_properties, $this->modx->parser->parseProperties($properties));
  946. }
  947. return $this->_properties;
  948. }
  949. /**
  950. * Set default properties for this element instance.
  951. *
  952. * @param array|string $properties A property array or property string.
  953. * @param boolean $merge Indicates if properties should be merged with
  954. * existing ones.
  955. * @return boolean true if the properties are set.
  956. */
  957. public function setProperties($properties, $merge = false) {
  958. $set = false;
  959. $propertyArray = array();
  960. if (is_string($properties)) {
  961. $properties = $this->modx->parser->parsePropertyString($properties);
  962. }
  963. if (is_array($properties)) {
  964. foreach ($properties as $propKey => $property) {
  965. if (is_array($property) && isset($property[5])) {
  966. $propertyArray[$property[0]] = array(
  967. 'name' => $property[0],
  968. 'desc' => $property[1],
  969. 'type' => $property[2],
  970. 'options' => $property[3],
  971. 'value' => $property[4],
  972. );
  973. } elseif (is_array($property) && isset($property['value'])) {
  974. $propertyArray[$property['name']] = array(
  975. 'name' => $property['name'],
  976. 'desc' => isset($property['description']) ? $property['description'] : (isset($property['desc']) ? $property['desc'] : ''),
  977. 'type' => isset($property['xtype']) ? $property['xtype'] : (isset($property['type']) ? $property['type'] : 'textfield'),
  978. 'options' => isset($property['options']) ? $property['options'] : array(),
  979. 'value' => $property['value'],
  980. );
  981. } else {
  982. $propertyArray[$propKey] = array(
  983. 'name' => $propKey,
  984. 'desc' => '',
  985. 'type' => 'textfield',
  986. 'options' => array(),
  987. 'value' => $property
  988. );
  989. }
  990. }
  991. if ($merge && !empty($propertyArray)) {
  992. $existing = $this->get('properties');
  993. if (is_array($existing) && !empty($existing)) {
  994. $propertyArray = array_merge($existing, $propertyArray);
  995. }
  996. }
  997. $set = $this->set('properties', $propertyArray);
  998. }
  999. return $set;
  1000. }
  1001. /**
  1002. * Indicates if the element is cacheable.
  1003. *
  1004. * @return boolean True if the element can be stored to or retrieved from
  1005. * the element cache.
  1006. */
  1007. public function isCacheable() {
  1008. return $this->_cacheable;
  1009. }
  1010. /**
  1011. * Sets the runtime cacheability of the element.
  1012. *
  1013. * @param boolean $cacheable Indicates the value to set for cacheability of
  1014. * this element.
  1015. */
  1016. public function setCacheable($cacheable = true) {
  1017. $this->_cacheable = (boolean) $cacheable;
  1018. }
  1019. /**
  1020. * Gets a named property set to use with this modTag instance.
  1021. *
  1022. * This function will attempt to extract a setName from the tag name using the
  1023. * @ symbol to delimit the name of the property set. If a setName parameter is provided,
  1024. * the function will override any property set specified in the name by merging both
  1025. * property sets.
  1026. *
  1027. * Here is an example of an tag using the @ modifier to specify a property set name:
  1028. * [[~TagName@PropertySetName:FilterCommand=`FilterModifier`?
  1029. * &PropertyKey1=`PropertyValue1`
  1030. * &PropertyKey2=`PropertyValue2`
  1031. * ]]
  1032. *
  1033. * @param string|null $setName An explicit property set name to search for.
  1034. * @return array|null An array of properties or null if no set is found.
  1035. */
  1036. public function getPropertySet($setName = null) {
  1037. $propertySet= null;
  1038. $name = $this->get('name');
  1039. if (strpos($name, '@') !== false) {
  1040. $psName= '';
  1041. $split= xPDO :: escSplit('@', $name);
  1042. if ($split && isset($split[1])) {
  1043. $name= $split[0];
  1044. $psName= $split[1];
  1045. $filters= xPDO :: escSplit(':', $setName);
  1046. if ($filters && isset($filters[1]) && !empty($filters[1])) {
  1047. $psName= $filters[0];
  1048. $name.= ':' . $filters[1];
  1049. }
  1050. $this->set('name', $name);
  1051. }
  1052. if (!empty($psName)) {
  1053. $psObj= $this->modx->getObject('modPropertySet', array('name' => $psName));
  1054. if ($psObj) {
  1055. $propertySet= $this->modx->parser->parseProperties($psObj->get('properties'));
  1056. }
  1057. }
  1058. }
  1059. if (!empty($setName)) {
  1060. $propertySetObj= $this->modx->getObject('modPropertySet', array('name' => $setName));
  1061. if ($propertySetObj) {
  1062. if (is_array($propertySet)) {
  1063. $propertySet= array_merge($propertySet, $this->modx->parser->parseProperties($propertySetObj->get('properties')));
  1064. } else {
  1065. $propertySet= $this->modx->parser->parseProperties($propertySetObj->get('properties'));
  1066. }
  1067. }
  1068. }
  1069. return $propertySet;
  1070. }
  1071. }
  1072. /**
  1073. * Tag representing a modResource field from the current MODX resource.
  1074. *
  1075. * [[*content]] Represents the content field from modResource.
  1076. *
  1077. * @uses modX::$resource The modResource instance being processed by modX.
  1078. * @package modx
  1079. */
  1080. class modFieldTag extends modTag {
  1081. /**
  1082. * Overrides modTag::__construct to set the Field Tag token
  1083. * {@inheritdoc}
  1084. */
  1085. function __construct(modX & $modx) {
  1086. parent :: __construct($modx);
  1087. $this->setToken('*');
  1088. }
  1089. /**
  1090. * Process the modFieldTag and return the output.
  1091. *
  1092. * {@inheritdoc}
  1093. */
  1094. public function process($properties= null, $content= null) {
  1095. if ($this->get('name') === 'content') $this->setCacheable(false);
  1096. parent :: process($properties, $content);
  1097. if (!$this->_processed) {
  1098. $this->_output= $this->_content;
  1099. if (is_string($this->_output) && !empty ($this->_output)) {
  1100. /* collect element tags in the content and process them */
  1101. $maxIterations= intval($this->modx->getOption('parser_max_iterations',null,10));
  1102. $this->modx->parser->processElementTags(
  1103. $this->_tag,
  1104. $this->_output,
  1105. $this->modx->parser->isProcessingUncacheable(),
  1106. $this->modx->parser->isRemovingUnprocessed(),
  1107. '[[',
  1108. ']]',
  1109. array(),
  1110. $maxIterations
  1111. );
  1112. }
  1113. $this->filterOutput();
  1114. $this->cache();
  1115. $this->_processed= true;
  1116. }
  1117. /* finally, return the processed element content */
  1118. return $this->_output;
  1119. }
  1120. /**
  1121. * Get the raw source content of the field.
  1122. *
  1123. * {@inheritdoc}
  1124. */
  1125. public function getContent(array $options = array()) {
  1126. if (!$this->isCacheable() || !is_string($this->_content) || $this->_content === '') {
  1127. if (isset($options['content']) && !empty($options['content'])) {
  1128. $this->_content = $options['content'];
  1129. } elseif ($this->modx->resource instanceof modResource) {
  1130. if ($this->get('name') == 'content') {
  1131. $this->_content = $this->modx->resource->getContent($options);
  1132. } else {
  1133. $this->_content = $this->modx->resource->get($this->get('name'));
  1134. }
  1135. }
  1136. }
  1137. return $this->_content;
  1138. }
  1139. }
  1140. /**
  1141. * Represents placeholder tags.
  1142. *
  1143. * [[+placeholder_key]] Represents a placeholder with name placeholder_key.
  1144. *
  1145. * @uses modX::getPlaceholder() To retrieve the placeholder value.
  1146. * @package modx
  1147. */
  1148. class modPlaceholderTag extends modTag {
  1149. /**
  1150. * Overrides modTag::__construct to set the Placeholder Tag token
  1151. * {@inheritdoc}
  1152. */
  1153. function __construct(modX & $modx) {
  1154. parent :: __construct($modx);
  1155. $this->setCacheable(false);
  1156. $this->setToken('+');
  1157. }
  1158. /**
  1159. * Processes the modPlaceholderTag, recursively processing nested tags.
  1160. *
  1161. * Tags in the properties of the tag itself, or the content returned by the
  1162. * tag element are processed. Non-cacheable nested tags are only processed
  1163. * if this tag element is also non-cacheable.
  1164. *
  1165. * {@inheritdoc}
  1166. */
  1167. public function process($properties= null, $content= null) {
  1168. parent :: process($properties, $content);
  1169. if (!$this->_processed) {
  1170. $this->_output= $this->_content;
  1171. if ($this->_output !== null && is_string($this->_output) && !empty($this->_output)) {
  1172. /* collect element tags in the content and process them */
  1173. $maxIterations= intval($this->modx->getOption('parser_max_iterations',null,10));
  1174. $this->modx->parser->processElementTags(
  1175. $this->_tag,
  1176. $this->_output,
  1177. $this->modx->parser->isProcessingUncacheable(),
  1178. $this->modx->parser->isRemovingUnprocessed(),
  1179. '[[',
  1180. ']]',
  1181. array(),
  1182. $maxIterations
  1183. );
  1184. }
  1185. if ($this->_output !== null || $this->modx->parser->startedProcessingUncacheable()) {
  1186. $this->filterOutput();
  1187. $this->_processed = true;
  1188. }
  1189. }
  1190. /* finally, return the processed element content */
  1191. return $this->_output;
  1192. }
  1193. /**
  1194. * Get the raw source content of the field.
  1195. *
  1196. * {@inheritdoc}
  1197. */
  1198. public function getContent(array $options = array()) {
  1199. if (!is_string($this->_content)) {
  1200. if (isset($options['content'])) {
  1201. $this->_content = $options['content'];
  1202. } else {
  1203. $this->_content = $this->modx->getPlaceholder($this->get('name'));
  1204. }
  1205. }
  1206. return $this->_content;
  1207. }
  1208. /**
  1209. * modPlaceholderTag instances cannot be cacheable.
  1210. *
  1211. * @return boolean Always returns false.
  1212. */
  1213. public function isCacheable() {
  1214. return false;
  1215. }
  1216. /**
  1217. * modPlaceholderTag instances cannot be cacheable.
  1218. *
  1219. * {@inheritdoc}
  1220. */
  1221. public function setCacheable($cacheable = true) {}
  1222. }
  1223. /**
  1224. * Represents link tags.
  1225. *
  1226. * [[~12]] Creates a URL from the specified resource identifier.
  1227. *
  1228. * @package modx
  1229. */
  1230. class modLinkTag extends modTag {
  1231. /**
  1232. * Overrides modTag::__construct to set the Link Tag token
  1233. * {@inheritdoc}
  1234. */
  1235. function __constructor(modX & $modx) {
  1236. parent :: __construct($modx);
  1237. $this->setToken('~');
  1238. }
  1239. /**
  1240. * Processes the modLinkTag, recursively processing nested tags.
  1241. *
  1242. * {@inheritdoc}
  1243. */
  1244. public function process($properties= null, $content= null) {
  1245. parent :: process($properties, $content);
  1246. if (!$this->_processed) {
  1247. $this->_output= $this->_content;
  1248. if (is_string($this->_output) && !empty ($this->_output)) {
  1249. /* collect element tags in the content and process them */
  1250. $maxIterations= intval($this->modx->getOption('parser_max_iterations',null,10));
  1251. $this->modx->parser->processElementTags(
  1252. $this->_tag,
  1253. $this->_output,
  1254. $this->modx->parser->isProcessingUncacheable(),
  1255. $this->modx->parser->isRemovingUnprocessed(),
  1256. '[[',
  1257. ']]',
  1258. array(),
  1259. $maxIterations
  1260. );
  1261. $context = '';
  1262. if ($this->modx->getOption('friendly_urls', null, false)) {
  1263. if (array_key_exists('context', $this->_properties)) {
  1264. $context = $this->_properties['context'];
  1265. }
  1266. if ($context) {
  1267. $resource = $this->modx->findResource($this->_output, $context);
  1268. if ($resource) {
  1269. $this->_output = $resource;
  1270. }
  1271. }
  1272. }
  1273. if (!empty($this->_output)) {
  1274. $qs = '';
  1275. $scheme = $this->modx->getOption('link_tag_scheme',null,-1);
  1276. $options = array();
  1277. if (is_array($this->_properties) && !empty($this->_properties)) {
  1278. $qs = array();
  1279. if (array_key_exists('context', $this->_properties)) {
  1280. $context = $this->_properties['context'];
  1281. unset($this->_properties['context']);
  1282. }
  1283. if (array_key_exists('scheme', $this->_properties)) {
  1284. $scheme = $this->_properties['scheme'];
  1285. unset($this->_properties['scheme']);
  1286. if (is_numeric($scheme)) $scheme = (integer) $scheme;
  1287. }
  1288. if (array_key_exists('use_weblink_target', $this->_properties)) {
  1289. $options['use_weblink_target'] = $this->_properties['use_weblink_target'];
  1290. unset($this->_properties['use_weblink_target']);
  1291. }
  1292. foreach ($this->_properties as $propertyKey => $propertyValue) {
  1293. if (in_array($propertyKey, array('context', 'scheme', 'use_weblink_target'))) continue;
  1294. $qs[]= "{$propertyKey}={$propertyValue}";
  1295. }
  1296. if ($qs= implode('&', $qs)) {
  1297. $qs= rawurlencode($qs);
  1298. $qs= str_replace(array('%26','%3D'),array('&amp;','='),$qs);
  1299. }
  1300. }
  1301. $this->_output= $this->modx->makeUrl($this->_output, $context, $qs, $scheme, $options);
  1302. }
  1303. }
  1304. if (!empty($this->_output)) {
  1305. $this->filterOutput();
  1306. $this->cache();
  1307. $this->_processed= true;
  1308. }
  1309. if (empty($this->_output)) {
  1310. $this->modx->log(
  1311. modX::LOG_LEVEL_ERROR,
  1312. 'Bad link tag `' . $this->_tag . '` encountered',
  1313. '',
  1314. $this->modx->resource
  1315. ? "resource {$this->modx->resource->id}"
  1316. : ($_SERVER['REQUEST_URI'] ? "uri {$_SERVER['REQUEST_URI']}" : '')
  1317. );
  1318. }
  1319. }
  1320. /* finally, return the processed element content */
  1321. return $this->_output;
  1322. }
  1323. }
  1324. /**
  1325. * Represents Lexicon tags, for localized strings.
  1326. *
  1327. * [[%word_or_phase]] Returns the lexicon representation of 'word_or_phrase' for
  1328. * the currently loaded language.
  1329. *
  1330. * @package modx
  1331. */
  1332. class modLexiconTag extends modTag {
  1333. /**
  1334. * Overrides modTag::__construct to set the Lexicon Tag token
  1335. * {@inheritdoc}
  1336. */
  1337. function __construct(modX & $modx) {
  1338. parent :: __construct($modx);
  1339. $this->setToken('%');
  1340. }
  1341. /**
  1342. * Processes a modLexiconTag, recursively processing nested tags.
  1343. *
  1344. * {@inheritdoc}
  1345. */
  1346. public function process($properties= null, $content= null) {
  1347. parent :: process($properties, $content);
  1348. if (!$this->_processed) {
  1349. $this->_output= $this->_content;
  1350. if (is_string($this->_output) && !empty ($this->_output)) {
  1351. /* collect element tags in the content and process them */
  1352. $maxIterations= intval($this->modx->getOption('parser_max_iterations',null,10));
  1353. $this->modx->parser->processElementTags(
  1354. $this->_tag,
  1355. $this->_output,
  1356. $this->modx->parser->isProcessingUncacheable(),
  1357. $this->modx->parser->isRemovingUnprocessed(),
  1358. '[[',
  1359. ']]',
  1360. array(),
  1361. $maxIterations
  1362. );
  1363. }
  1364. $this->filterOutput();
  1365. $this->cache();
  1366. $this->_processed= true;
  1367. }
  1368. /* finally, return the processed element content */
  1369. return $this->_output;
  1370. }
  1371. /**
  1372. * Get the raw source content of the link.
  1373. *
  1374. * {@inheritdoc}
  1375. */
  1376. public function getContent(array $options = array()) {
  1377. if (!is_string($this->_content) || $this->_content === '') {
  1378. if (isset($options['content'])) {
  1379. $this->_content = $options['content'];
  1380. } else {
  1381. if (!is_object($this->modx->lexicon)) {
  1382. $this->modx->getService('lexicon','modLexicon');
  1383. }
  1384. $topic = !empty($this->_properties['topic']) ? $this->_properties['topic'] : 'default';
  1385. $namespace = !empty($this->_properties['namespace']) ? $this->_properties['namespace'] : 'core';
  1386. $language = !empty($this->_properties['language']) ? $this->_properties['language'] : $this->modx->getOption('cultureKey',null,'en');
  1387. $this->modx->lexicon->load($language.':'.$namespace.':'.$topic);
  1388. $this->_content= $this->modx->lexicon($this->get('name'), $this->_properties,$language);
  1389. }
  1390. }
  1391. return $this->_content;
  1392. }
  1393. }