PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/html/AppCode/expressionengine/libraries/Template.php

https://github.com/w3bg/www.hsifin.com
PHP | 4169 lines | 3936 code | 96 blank | 137 comment | 51 complexity | d7fe52d7b39c7ca8ab2fd9373f5c906e MD5 | raw file
Possible License(s): AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Template Parser Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class EE_Template {
  24. var $loop_count = 0; // Main loop counter.
  25. var $depth = 0; // Sub-template loop depth
  26. var $in_point = ''; // String position of matched opening tag
  27. var $template = ''; // The requested template (page)
  28. var $final_template = ''; // The finalized template
  29. var $fl_tmpl = ''; // 'Floating' copy of the template. Used as a temporary "work area".
  30. var $cache_hash = ''; // md5 checksum of the template name. Used as title of cache file.
  31. var $cache_status = ''; // Status of page cache (NO_CACHE, CURRENT, EXPIRED)
  32. var $tag_cache_status = ''; // Status of tag cache (NO_CACHE, CURRENT, EXPIRED)
  33. var $cache_timestamp = '';
  34. var $template_type = ''; // Type of template (webpage, rss)
  35. var $embed_type = ''; // Type of template for embedded template
  36. var $template_hits = 0;
  37. var $php_parse_location = 'output'; // Where in the chain the PHP gets parsed
  38. var $template_edit_date = ''; // Template edit date
  39. var $templates_sofar = ''; // Templates processed so far, subtemplate tracker
  40. var $encode_email = TRUE; // Whether to use the email encoder. This is set automatically
  41. var $hit_lock_override = FALSE; // Set to TRUE if you want hits tracked on sub-templates
  42. var $hit_lock = FALSE; // Lets us lock the hit counter if sub-templates are contained in a template
  43. var $parse_php = FALSE; // Whether to parse PHP or not
  44. var $protect_javascript = TRUE; // Protect javascript in conditionals
  45. var $tag_data = array(); // Data contained in tags
  46. var $modules = array(); // List of installed modules
  47. var $module_data = array(); // Data for modules from exp_channels
  48. var $plugins = array(); // List of installed plug-ins
  49. var $var_single = array(); // "Single" variables
  50. var $var_cond = array(); // "Conditional" variables
  51. var $var_pair = array(); // "Paired" variables
  52. var $global_vars = array(); // This array can be set via the path.php file
  53. var $embed_vars = array(); // This array can be set via the {embed} tag
  54. var $segment_vars = array(); // Array of segment variables
  55. var $tagparts = array(); // The parts of the tag: {exp:comment:form}
  56. var $tagdata = ''; // The chunk between tag pairs. This is what modules will utilize
  57. var $tagproper = ''; // The full opening tag
  58. var $no_results = ''; // The contents of the {if no_results}{/if} conditionals
  59. var $no_results_block = ''; // The {if no_results}{/if} chunk
  60. var $search_fields = array(); // Special array of tag parameters that begin with 'search:'
  61. var $date_vars = array(); // Date variables found in the tagdata (FALSE if date variables do not exist in tagdata)
  62. var $unfound_vars = array(); // These are variables that have not been found in the tagdata and can be ignored
  63. var $conditional_vars = array(); // Used by the template variable parser to prep conditionals
  64. var $TYPE = FALSE; // FALSE if Typography has not been instantiated, Typography Class object otherwise
  65. var $related_data = array(); // A multi-dimensional array containing any related tags
  66. var $related_id = ''; // Used temporarily for the related ID number
  67. var $related_markers = array(); // Used temporarily
  68. var $site_ids = array(); // Site IDs for the Sites Request for a Tag
  69. var $sites = array(); // Array of sites with site_id as key and site_name as value, used to determine site_ids for tag, above.
  70. var $site_prefs_cache = array(); // Array of cached site prefs, to allow fetching of another site's template files
  71. var $reverse_related_data = array(); // A multi-dimensional array containing any reverse related tags
  72. var $t_cache_path = 'tag_cache/'; // Location of the tag cache file
  73. var $p_cache_path = 'page_cache/'; // Location of the page cache file
  74. var $disable_caching = FALSE;
  75. var $debugging = FALSE; // Template parser debugging on?
  76. var $cease_processing = FALSE; // Used with no_results() method.
  77. var $log = array(); // Log of Template processing
  78. var $start_microtime = 0; // For Logging (= microtime())
  79. var $strict_urls = FALSE; // Whether to make URLs operate strictly or not. This is set via a template global pref
  80. var $realm = 'ExpressionEngine Template'; // Localize?
  81. var $marker = '0o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr'; // Temporary marker used as a place-holder for template data
  82. var $form_id = ''; // Form Id
  83. var $form_class = ''; // Form Class
  84. // --------------------------------------------------------------------
  85. /**
  86. * Constructor
  87. *
  88. * @access public
  89. * @return void
  90. */
  91. function EE_Template()
  92. {
  93. // Make a local reference to the ExpressionEngine super object
  94. $this->EE =& get_instance();
  95. if ($this->EE->config->item('multiple_sites_enabled') != 'y')
  96. {
  97. $this->sites[$this->EE->config->item('site_id')] = $this->EE->config->item('site_short_name');
  98. }
  99. if ($this->EE->config->item('template_debugging') === 'y' && $this->EE->session->userdata['group_id'] == 1)
  100. {
  101. $this->debugging = TRUE;
  102. if (phpversion() < 5)
  103. {
  104. list($usec, $sec) = explode(" ", microtime());
  105. $this->start_microtime = ((float)$usec + (float)$sec);
  106. }
  107. else
  108. {
  109. $this->start_microtime = microtime(TRUE);
  110. }
  111. }
  112. }
  113. // --------------------------------------------------------------------
  114. /**
  115. * Run Template Engine
  116. *
  117. * Upon a Page or a Preview, it Runs the Processing of a Template baed on URI request or method arguments
  118. *
  119. * @access public
  120. * @param string
  121. * @param string
  122. * @return void
  123. */
  124. function run_template_engine($template_group = '', $template = '')
  125. {
  126. $this->log_item(" - Begin Template Processing - ");
  127. // Set the name of the cache folder for both tag and page caching
  128. if ($this->EE->uri->uri_string != '')
  129. {
  130. $this->t_cache_path .= md5($this->EE->functions->fetch_site_index().$this->EE->uri->uri_string).'/';
  131. $this->p_cache_path .= md5($this->EE->functions->fetch_site_index().$this->EE->uri->uri_string).'/';
  132. }
  133. else
  134. {
  135. $this->t_cache_path .= md5($this->EE->config->item('site_url').'index'.$this->EE->uri->query_string).'/';
  136. $this->p_cache_path .= md5($this->EE->config->item('site_url').'index'.$this->EE->uri->query_string).'/';
  137. }
  138. // We limit the total number of cache files in order to
  139. // keep some sanity with large sites or ones that get
  140. // hit by over-ambitious crawlers.
  141. if ($this->disable_caching == FALSE)
  142. {
  143. if ($dh = @opendir(APPPATH.'cache/page_cache'))
  144. {
  145. $i = 0;
  146. while (FALSE !== (readdir($dh)))
  147. {
  148. $i++;
  149. }
  150. $max = ( ! $this->EE->config->item('max_caches') OR ! is_numeric($this->EE->config->item('max_caches')) OR $this->EE->config->item('max_caches') > 1000) ? 1000 : $this->EE->config->item('max_caches');
  151. if ($i > $max)
  152. {
  153. $this->EE->functions->clear_caching('page');
  154. }
  155. }
  156. }
  157. $this->log_item("URI: ".$this->EE->uri->uri_string);
  158. $this->log_item("Path.php Template: {$template_group}/{$template}");
  159. $this->fetch_and_parse($template_group, $template, FALSE);
  160. $this->log_item(" - End Template Processing - ");
  161. $this->log_item("Parse Global Variables");
  162. if ($this->template_type == 'static')
  163. {
  164. $this->final_template = $this->restore_xml_declaration($this->final_template);
  165. }
  166. else
  167. {
  168. $this->final_template = $this->parse_globals($this->final_template);
  169. }
  170. $this->log_item("Template Parsing Finished");
  171. $this->EE->output->out_type = $this->template_type;
  172. $this->EE->output->set_output($this->final_template);
  173. }
  174. // --------------------------------------------------------------------
  175. /**
  176. * Fetch and Process Template
  177. *
  178. * Determines what template to process, fetches the template and its preferences, and then processes all of it
  179. *
  180. * @access public
  181. * @param string
  182. * @param string
  183. * @param bool
  184. * @param int
  185. * @return void
  186. */
  187. function fetch_and_parse($template_group = '', $template = '', $sub = FALSE, $site_id = '')
  188. {
  189. // add this template to our subtemplate tracker
  190. $this->templates_sofar = $this->templates_sofar.'|'.$site_id.':'.$template_group.'/'.$template.'|';
  191. /** -------------------------------------
  192. /** Fetch the requested template
  193. /** -------------------------------------*/
  194. // The template can either come from the DB or a cache file
  195. // Do not use a reference!
  196. $this->cache_status = 'NO_CACHE';
  197. $this->log_item("Retrieving Template");
  198. $this->template = ($template_group != '' AND $template != '') ? $this->fetch_template($template_group, $template, FALSE, $site_id) : $this->parse_template_uri();
  199. $this->log_item("Template Type: ".$this->template_type);
  200. $this->parse($this->template, $sub, $site_id);
  201. }
  202. // --------------------------------------------------------------------
  203. /**
  204. * Parse a string as a template
  205. *
  206. * @access public
  207. * @param string
  208. * @param string
  209. * @return void
  210. */
  211. function parse(&$str, $sub = FALSE, $site_id = '')
  212. {
  213. if ($str != '')
  214. {
  215. $this->template =& $str;
  216. }
  217. /** -------------------------------------
  218. /** Static Content, No Parsing
  219. /** -------------------------------------*/
  220. if ($this->template_type == 'static' OR $this->embed_type == 'static')
  221. {
  222. if ($sub == FALSE)
  223. {
  224. $this->final_template = $this->template;
  225. }
  226. return;
  227. }
  228. /* -------------------------------------
  229. /* "Smart" Static Parsing
  230. /*
  231. /* Performed on embedded webpage templates only that do not have
  232. /* ExpressionEngine tags or PHP in them.
  233. /*
  234. /* Hidden Configuration Variable
  235. /* - smart_static_parsing => Bypass parsing of templates that could be
  236. /* of the type 'static' but aren't? (y/n)
  237. /* -------------------------------------*/
  238. if ($this->EE->config->item('smart_static_parsing') !== 'n' && $this->embed_type == 'webpage' && ! stristr($this->template, LD) && ! stristr($this->template, '<?'))
  239. {
  240. $this->log_item("Smart Static Parsing Triggered");
  241. if ($sub == FALSE)
  242. {
  243. $this->final_template = $this->template;
  244. }
  245. return;
  246. }
  247. /** --------------------------------------------------
  248. /** Parse 'Site' variables
  249. /** --------------------------------------------------*/
  250. $this->log_item("Parsing Site Variables");
  251. // load site variables into the global_vars array
  252. foreach (array('site_id', 'site_label', 'site_short_name') as $site_var)
  253. {
  254. $this->EE->config->_global_vars[$site_var] = stripslashes($this->EE->config->item($site_var));
  255. }
  256. // Parse {last_segment} variable
  257. $seg_array = $this->EE->uri->segment_array();
  258. $this->EE->config->_global_vars['last_segment'] = end($seg_array);
  259. /** -------------------------------------
  260. /** Parse manual variables and Snippets
  261. /** -------------------------------------*/
  262. // These are variables that can be set in the path.php file
  263. if (count($this->EE->config->_global_vars) > 0)
  264. {
  265. $this->log_item("Snippets (Keys): ".implode('|', array_keys($this->EE->config->_global_vars)));
  266. $this->log_item("Snippets (Values): ".trim(implode('|', $this->EE->config->_global_vars)));
  267. foreach ($this->EE->config->_global_vars as $key => $val)
  268. {
  269. $this->template = str_replace(LD.$key.RD, $val, $this->template);
  270. }
  271. // in case any of these variables have EE comments of their own
  272. $this->template = $this->remove_ee_comments($this->template);
  273. }
  274. /** -------------------------------------
  275. /** Parse URI segments
  276. /** -------------------------------------*/
  277. // This code lets admins fetch URI segments which become
  278. // available as: {segment_1} {segment_2}
  279. for ($i = 1; $i < 10; $i++)
  280. {
  281. $this->template = str_replace(LD.'segment_'.$i.RD, $this->EE->uri->segment($i), $this->template);
  282. $this->segment_vars['segment_'.$i] = $this->EE->uri->segment($i);
  283. }
  284. /** -------------------------------------
  285. /** Parse {embed} tag variables
  286. /** -------------------------------------*/
  287. if ($sub === TRUE && count($this->embed_vars) > 0)
  288. {
  289. $this->log_item("Embed Variables (Keys): ".implode('|', array_keys($this->embed_vars)));
  290. $this->log_item("Embed Variables (Values): ".trim(implode('|', $this->embed_vars)));
  291. foreach ($this->embed_vars as $key => $val)
  292. {
  293. // add 'embed:' to the key for replacement and so these variables work in conditionals
  294. $this->embed_vars['embed:'.$key] = $val;
  295. unset($this->embed_vars[$key]);
  296. $this->template = str_replace(LD.'embed:'.$key.RD, $val, $this->template);
  297. }
  298. }
  299. // cleanup of leftover/undeclared embed variables
  300. // don't worry with undeclared embed: vars in conditionals as the conditionals processor will handle that adequately
  301. if (strpos($this->template, LD.'embed:') !== FALSE)
  302. {
  303. $this->template = preg_replace('/'.LD.'embed:(.+?)'.RD.'/', '', $this->template);
  304. }
  305. /** -------------------------------------
  306. /** Parse date format string "constants"
  307. /** -------------------------------------*/
  308. $date_constants = array('DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
  309. 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
  310. 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
  311. 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
  312. 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
  313. 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
  314. 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
  315. 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
  316. 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
  317. 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
  318. );
  319. foreach ($date_constants as $key => $val)
  320. {
  321. $this->template = str_replace(LD.$key.RD, $val, $this->template);
  322. }
  323. $this->log_item("Parse Date Format String Constants");
  324. /** --------------------------------------------------
  325. /** Template's Last Edit time {template_edit_date format="%Y %m %d %H:%i:%s"}
  326. /** --------------------------------------------------*/
  327. if (strpos($this->template, LD.'template_edit_date') !== FALSE && preg_match_all("/".LD."template_edit_date\s+format=([\"\'])([^\\1]*?)\\1".RD."/", $this->template, $matches))
  328. {
  329. for ($j = 0; $j < count($matches[0]); $j++)
  330. {
  331. $this->template = str_replace($matches[0][$j], $this->EE->localize->decode_date($matches[2][$j], $this->template_edit_date), $this->template);
  332. }
  333. }
  334. /** --------------------------------------------------
  335. /** Current time {current_time format="%Y %m %d %H:%i:%s"}
  336. /** --------------------------------------------------*/
  337. if (strpos($this->template, LD.'current_time') !== FALSE && preg_match_all("/".LD."current_time\s+format=([\"\'])([^\\1]*?)\\1".RD."/", $this->template, $matches))
  338. {
  339. for ($j = 0; $j < count($matches[0]); $j++)
  340. {
  341. $this->template = str_replace($matches[0][$j], $this->EE->localize->decode_date($matches[2][$j], $this->EE->localize->now), $this->template);
  342. }
  343. }
  344. $this->template = str_replace(LD.'current_time'.RD, $this->EE->localize->now, $this->template);
  345. $this->log_item("Parse Current Time Variables");
  346. /** -------------------------------------
  347. /** Is the main template cached?
  348. /** -------------------------------------*/
  349. // If a cache file exists for the primary template
  350. // there is no reason to go further.
  351. // However we do need to fetch any subtemplates
  352. if ($this->cache_status == 'CURRENT' AND $sub == FALSE)
  353. {
  354. $this->log_item("Cached Template Used");
  355. $this->template = $this->parse_nocache($this->template);
  356. /** -------------------------------------
  357. /** Smite Our Enemies: Advanced Conditionals
  358. /** -------------------------------------*/
  359. if (stristr($this->template, LD.'if'))
  360. {
  361. $this->template = $this->advanced_conditionals($this->template);
  362. }
  363. $this->log_item("Conditionals Parsed, Processing Sub Templates");
  364. $this->final_template = $this->template;
  365. $this->process_sub_templates($this->template);
  366. return;
  367. }
  368. // Remove whitespace from variables.
  369. // This helps prevent errors, particularly if PHP is used in a template
  370. $this->template = preg_replace("/".LD."\s*(\S+)\s*".RD."/U", LD."\\1".RD, $this->template);
  371. /** -------------------------------------
  372. /** Parse Input Stage PHP
  373. /** -------------------------------------*/
  374. if ($this->parse_php == TRUE AND $this->php_parse_location == 'input' AND $this->cache_status != 'CURRENT')
  375. {
  376. $this->log_item("Parsing PHP on Input");
  377. $this->template = $this->parse_template_php($this->template);
  378. }
  379. /** -------------------------------------
  380. /** Smite Our Enemies: Conditionals
  381. /** -------------------------------------*/
  382. $this->log_item("Parsing Segment, Embed, and Global Vars Conditionals");
  383. $this->template = $this->parse_simple_segment_conditionals($this->template);
  384. $this->template = $this->simple_conditionals($this->template, $this->embed_vars);
  385. $this->template = $this->simple_conditionals($this->template, $this->EE->config->_global_vars);
  386. /** -------------------------------------
  387. /** Assign Variables
  388. /** -------------------------------------*/
  389. if (strpos($this->template, 'preload_replace') !== FALSE)
  390. {
  391. if (preg_match_all("/".LD."preload_replace:(.+?)=([\"\'])([^\\2]*?)\\2".RD."/i", $this->template, $matches))
  392. {
  393. $this->log_item("Processing Preload Text Replacements: ".trim(implode('|', $matches[1])));
  394. for ($j = 0; $j < count($matches[0]); $j++)
  395. {
  396. $this->template = str_replace($matches[0][$j], "", $this->template);
  397. $this->template = str_replace(LD.$matches[1][$j].RD, $matches[3][$j], $this->template);
  398. }
  399. }
  400. }
  401. /** -------------------------------------
  402. /** Parse Plugin and Module Tags
  403. /** -------------------------------------*/
  404. $this->tags();
  405. if ($this->cease_processing === TRUE)
  406. {
  407. return;
  408. }
  409. /** -------------------------------------
  410. /** Parse Output Stage PHP
  411. /** -------------------------------------*/
  412. if ($this->parse_php == TRUE AND $this->php_parse_location == 'output' AND $this->cache_status != 'CURRENT')
  413. {
  414. $this->log_item("Parsing PHP on Output");
  415. $this->template = $this->parse_template_php($this->template);
  416. }
  417. /** -------------------------------------
  418. /** Write the cache file if needed
  419. /** -------------------------------------*/
  420. if ($this->cache_status == 'EXPIRED')
  421. {
  422. $this->template = $this->EE->functions->insert_action_ids($this->template);
  423. $this->write_cache_file($this->cache_hash, $this->template, 'template');
  424. }
  425. /** -------------------------------------
  426. /** Parse Our Uncacheable Forms
  427. /** -------------------------------------*/
  428. $this->template = $this->parse_nocache($this->template);
  429. /** -------------------------------------
  430. /** Smite Our Enemies: Advanced Conditionals
  431. /** -------------------------------------*/
  432. if (strpos($this->template, LD.'if') !== FALSE)
  433. {
  434. $this->log_item("Processing Advanced Conditionals");
  435. $this->template = $this->advanced_conditionals($this->template);
  436. }
  437. /** -------------------------------------
  438. /** Build finalized template
  439. /** -------------------------------------*/
  440. // We only do this on the first pass.
  441. // The sub-template routine will insert embedded
  442. // templates into the master template
  443. if ($sub == FALSE)
  444. {
  445. $this->final_template = $this->template;
  446. $this->process_sub_templates($this->template);
  447. }
  448. }
  449. // --------------------------------------------------------------------
  450. /**
  451. * Processes Any Embedded Templates in String
  452. *
  453. * If any {embed=} tags are found, it processes those templates and does a replacement.
  454. *
  455. * @access public
  456. * @param string
  457. * @return void
  458. */
  459. function process_sub_templates($template)
  460. {
  461. /** -------------------------------------
  462. /** Match all {embed=bla/bla} tags
  463. /** -------------------------------------*/
  464. $matches = array();
  465. if ( ! preg_match_all("/(".LD."embed\s*=)(.*?)".RD."/s", $template, $matches))
  466. {
  467. return;
  468. }
  469. /** -------------------------------------
  470. /** Loop until we have parsed all sub-templates
  471. /** -------------------------------------*/
  472. // For each embedded tag we encounter we'll run the template parsing
  473. // function - AND - through the beauty of recursive functions we
  474. // will also call THIS function as well, allowing us to parse
  475. // infinitely nested sub-templates in one giant loop o' love
  476. $this->log_item(" - Processing Sub Templates (Depth: ".($this->depth+1).") - ");
  477. $i = 0;
  478. $this->depth++;
  479. $this->log_item("List of Embeds: ".str_replace(array('"', "'"), '', trim(implode(',', $matches[2]))));
  480. // re-match the full tag of each if necessary before we start processing
  481. // necessary evil in case template globals are used inside the embed tag,
  482. // doing this within the processing loop will result in leaving unparsed
  483. // embed tags e.g. {embed="foo/bar" var="{global_var}/{custom_field}"}
  484. $temp = $template;
  485. foreach ($matches[2] as $key => $val)
  486. {
  487. if (strpos($val, LD) !== FALSE)
  488. {
  489. $matches[0][$key] = $this->EE->functions->full_tag($matches[0][$key], $temp);
  490. $matches[2][$key] = substr(str_replace($matches[1][$key], '', $matches[0][$key]), 0, -1);
  491. $temp = str_replace($matches[0][$key], '', $temp);
  492. }
  493. }
  494. // Load the string helper
  495. $this->EE->load->helper('string');
  496. foreach($matches[2] as $key => $val)
  497. {
  498. $parts = preg_split("/\s+/", $val, 2);
  499. $this->embed_vars = (isset($parts[1])) ? $this->EE->functions->assign_parameters($parts[1]) : array();
  500. if ($this->embed_vars === FALSE)
  501. {
  502. $this->embed_vars = array();
  503. }
  504. $val = trim_slashes(strip_quotes($parts[0]));
  505. if (strpos($val, '/') === FALSE)
  506. {
  507. continue;
  508. }
  509. $ex = explode("/", trim($val));
  510. if (count($ex) != 2)
  511. {
  512. continue;
  513. }
  514. /** ----------------------------------
  515. /** Determine Site
  516. /** ----------------------------------*/
  517. $site_id = $this->EE->config->item('site_id');
  518. if (stristr($ex[0], ':'))
  519. {
  520. $name = substr($ex[0], 0, strpos($ex[0], ':'));
  521. if ($this->EE->config->item('multiple_sites_enabled') == 'y' && ! IS_FREELANCER)
  522. {
  523. if (count($this->sites) == 0)
  524. {
  525. // This should really be cached somewhere
  526. $this->EE->db->select('site_id, site_name');
  527. $sites_query = $this->EE->db->get('sites');
  528. foreach($sites_query->result_array() as $row)
  529. {
  530. $this->sites[$row['site_id']] = $row['site_name'];
  531. }
  532. }
  533. $site_id = array_search($name, $this->sites);
  534. if (empty($site_id))
  535. {
  536. $site_id = $this->EE->config->item('site_id');
  537. }
  538. }
  539. $ex[0] = str_replace($name.':', '', $ex[0]);
  540. }
  541. /** ----------------------------------
  542. /** Loop Prevention
  543. /** ----------------------------------*/
  544. /* -------------------------------------------
  545. /* Hidden Configuration Variable
  546. /* - template_loop_prevention => 'n'
  547. Whether or not loop prevention is enabled - y/n
  548. /* -------------------------------------------*/
  549. if (substr_count($this->templates_sofar, '|'.$site_id.':'.$ex['0'].'/'.$ex['1'].'|') > 1 && $this->EE->config->item('template_loop_prevention') != 'n')
  550. {
  551. $this->final_template = ($this->EE->config->item('debug') >= 1) ? str_replace('%s', $ex['0'].'/'.$ex['1'], $this->EE->lang->line('template_loop')) : "";
  552. return;
  553. }
  554. /** ----------------------------------
  555. /** Process Subtemplate
  556. /** ----------------------------------*/
  557. $this->log_item("Processing Sub Template: ".$ex[0]."/".$ex[1]);
  558. $this->fetch_and_parse($ex[0], $ex[1], TRUE, $site_id);
  559. $this->final_template = str_replace($matches[0][$key], $this->template, $this->final_template);
  560. $this->embed_type = '';
  561. // Here we go again! Wheeeeeee.....
  562. $this->process_sub_templates($this->template);
  563. // pull the subtemplate tracker back a level to the parent template
  564. $this->templates_sofar = substr($this->templates_sofar, 0, - strlen('|'.$site_id.':'.$ex[0].'/'.$ex[1].'|'));
  565. }
  566. $this->depth--;
  567. if ($this->depth == 0)
  568. {
  569. $this->templates_sofar = '';
  570. }
  571. }
  572. // --------------------------------------------------------------------
  573. /**
  574. * Finds Tags, Parses Them
  575. *
  576. * Goes Through the Template, Finds the Beginning and End of Tags, and Stores Tag Data in a Class Array
  577. *
  578. * @access public
  579. * @return void
  580. */
  581. function parse_tags()
  582. {
  583. while (TRUE)
  584. {
  585. // Make a "floating" copy of the template which we'll progressively slice into pieces with each loop
  586. $this->fl_tmpl = $this->template;
  587. // Identify the string position of the first occurence of a matched tag
  588. $this->in_point = strpos($this->fl_tmpl, LD.'exp:');
  589. // If the above variable returns FALSE we are done looking for tags
  590. // This single conditional keeps the template engine from spiraling
  591. // out of control in an infinite loop.
  592. if (FALSE === $this->in_point)
  593. {
  594. break;
  595. }
  596. else
  597. {
  598. /** ------------------------------------------
  599. /** Process the tag data
  600. /** ------------------------------------------*/
  601. // These REGEXs parse out the various components contained in any given tag.
  602. // Grab the opening portion of the tag: {exp:some:tag param="value" param="value"}
  603. if ( ! preg_match("/".LD.'exp:'.".*?".RD."/s", $this->fl_tmpl, $matches))
  604. {
  605. $this->template = preg_replace("/".LD.'exp:'.".*?$/", '', $this->template);
  606. break;
  607. }
  608. // Checking for variables/tags embedded within tags
  609. // {exp:channel:entries channel="{master_channel_name}"}
  610. if (stristr(substr($matches[0], 1), LD) !== FALSE)
  611. {
  612. $matches[0] = $this->EE->functions->full_tag($matches[0]);
  613. }
  614. $this->log_item("Tag: ".$matches[0]);
  615. $raw_tag = str_replace(array("\r\n", "\r", "\n", "\t"), " ", $matches[0]);
  616. $tag_length = strlen($raw_tag);
  617. $data_start = $this->in_point + $tag_length;
  618. $tag = trim(substr($raw_tag, 1, -1));
  619. $args = trim((preg_match("/\s+.*/", $tag, $matches))) ? $matches[0] : '';
  620. $tag = trim(str_replace($args, '', $tag));
  621. $cur_tag_close = LD.'/'.$tag.RD;
  622. // Deprecate "weblog" tags, but allow them to work until 2.1, then remove this.
  623. if (strpos($tag, ':weblog:') !== FALSE OR strpos($tag, ' weblog=') !== FALSE)
  624. {
  625. $tag = str_replace(array(':weblog:', ' weblog='), array(':channel:', ' channel='), $tag);
  626. $this->log_item("WARNING: Deprecated 'weblog' tag used, please change to 'channel'");
  627. }
  628. // -----------------------------------------
  629. // Grab the class name and method names contained in the tag
  630. $class = explode(':', substr($tag, strlen('exp') + 1));
  631. // Tags can either have one segment or two:
  632. // {exp:first_segment}
  633. // {exp:first_segment:second_segment}
  634. //
  635. // These two segments represent either a "class:constructor"
  636. // or a "class:method". We need to determine which one it is.
  637. if (count($class) == 1)
  638. {
  639. $class[1] = $class[0];
  640. }
  641. foreach($class as $key => $value)
  642. {
  643. $class[$key] = trim($value);
  644. }
  645. // -----------------------------------------
  646. // Assign parameters based on the arguments from the tag
  647. $args = $this->EE->functions->assign_parameters($args);
  648. // standardized mechanism for "search" type parameters get some extra lovin'
  649. $search_fields = array();
  650. if ($args !== FALSE)
  651. {
  652. foreach ($args as $key => $val)
  653. {
  654. if (strncmp($key, 'search:', 7) == 0)
  655. {
  656. $search_fields[substr($key, 7)] = $val;
  657. }
  658. }
  659. }
  660. // Trim the floating template, removing the tag we just parsed.
  661. $this->fl_tmpl = substr($this->fl_tmpl, $this->in_point + $tag_length);
  662. $out_point = strpos($this->fl_tmpl, $cur_tag_close);
  663. // Do we have a tag pair?
  664. if (FALSE !== $out_point)
  665. {
  666. // Assign the data contained between the opening/closing tag pair
  667. $this->log_item("Closing Tag Found");
  668. $block = substr($this->template, $data_start, $out_point);
  669. // Fetch the "no_results" data
  670. $no_results = '';
  671. $no_results_block = '';
  672. if (strpos($block, 'if no_results') !== FALSE && preg_match("/".LD."if no_results".RD."(.*?)".LD.'\/'."if".RD."/s", $block, $match))
  673. {
  674. // Match the entirety of the conditional, dude. Bad Rick!
  675. if (stristr($match[1], LD.'if'))
  676. {
  677. $match[0] = $this->EE->functions->full_tag($match[0], $block, LD.'if', LD.'\/'."if".RD);
  678. }
  679. $no_results = substr($match[0], strlen(LD."if no_results".RD), -strlen(LD.'/'."if".RD));
  680. $no_results_block = $match[0];
  681. }
  682. // Define the entire "chunk" - from the left edge of the opening tag
  683. // to the right edge of closing tag.
  684. $out_point = $out_point + $tag_length + strlen($cur_tag_close);
  685. $chunk = substr($this->template, $this->in_point, $out_point);
  686. }
  687. else
  688. {
  689. // Single tag...
  690. $this->log_item("No Closing Tag");
  691. $block = ''; // Single tags don't contain data blocks
  692. $no_results = '';
  693. $no_results_block = '';
  694. // Define the entire opening tag as a "chunk"
  695. $chunk = substr($this->template, $this->in_point, $tag_length);
  696. }
  697. // Strip the "chunk" from the template, replacing it with a unique marker.
  698. if (stristr($raw_tag, 'random'))
  699. {
  700. $this->template = preg_replace("|".preg_quote($chunk)."|s", 'M'.$this->loop_count.$this->marker, $this->template, 1);
  701. }
  702. else
  703. {
  704. $this->template = str_replace($chunk, 'M'.$this->loop_count.$this->marker, $this->template);
  705. }
  706. $cfile = md5($chunk); // This becomes the name of the cache file
  707. // Build a multi-dimensional array containing all of the tag data we've assembled
  708. $this->tag_data[$this->loop_count]['tag'] = $raw_tag;
  709. $this->tag_data[$this->loop_count]['class'] = $class[0];
  710. $this->tag_data[$this->loop_count]['method'] = $class[1];
  711. $this->tag_data[$this->loop_count]['tagparts'] = $class;
  712. $this->tag_data[$this->loop_count]['params'] = $args;
  713. $this->tag_data[$this->loop_count]['chunk'] = $chunk; // Matched data block - including opening/closing tags
  714. $this->tag_data[$this->loop_count]['block'] = $block; // Matched data block - no tags
  715. $this->tag_data[$this->loop_count]['cache'] = $args;
  716. $this->tag_data[$this->loop_count]['cfile'] = $cfile;
  717. $this->tag_data[$this->loop_count]['no_results'] = $no_results;
  718. $this->tag_data[$this->loop_count]['no_results_block'] = $no_results_block;
  719. $this->tag_data[$this->loop_count]['search_fields'] = $search_fields;
  720. } // END IF
  721. // Increment counter
  722. $this->loop_count++;
  723. } // END WHILE
  724. }
  725. // --------------------------------------------------------------------
  726. /**
  727. * Looks Through Template Looking for Tags
  728. *
  729. * Goes Through the Template, Finds the Beginning and End of Tags, and Stores Tag Data in a Class Array
  730. *
  731. * @access public
  732. * @return void
  733. */
  734. function tags()
  735. {
  736. // Fetch installed modules and plugins if needed
  737. if (count($this->modules) == 0)
  738. {
  739. $this->fetch_addons();
  740. }
  741. // Parse the template.
  742. $this->log_item(" - Beginning Tag Processing - ");
  743. while (is_int(strpos($this->template, LD.'exp:')))
  744. {
  745. // Initialize values between loops
  746. $this->tag_data = array();
  747. $this->var_single = array();
  748. $this->var_cond = array();
  749. $this->var_pair = array();
  750. $this->loop_count = 0;
  751. $this->log_item("Parsing Tags in Template");
  752. // Run the template parser
  753. $this->parse_tags();
  754. $this->log_item("Processing Tags");
  755. // Run the class/method handler
  756. $this->process_tags();
  757. if ($this->cease_processing === TRUE)
  758. {
  759. return;
  760. }
  761. }
  762. $this->log_item(" - End Tag Processing - ");
  763. }
  764. // --------------------------------------------------------------------
  765. /**
  766. * Process Tags
  767. *
  768. * Takes the Class Array Full of Tag Data and Processes the Tags One by One. Class class, feeds
  769. * data to class, takes results, and puts it back into the Template.
  770. *
  771. * @access public
  772. * @return void
  773. */
  774. function process_tags()
  775. {
  776. $plugins = array();
  777. $modules = array();
  778. // Fill an array with the names of all the classes that we previously extracted from the tags
  779. for ($i = 0, $ctd = count($this->tag_data); $i < $ctd; $i++)
  780. {
  781. // Check the tag cache file
  782. $cache_contents = $this->fetch_cache_file($this->tag_data[$i]['cfile'], 'tag', $this->tag_data[$i]['cache']);
  783. // Set cache status for final processing
  784. $this->tag_data[$i]['cache'] = $this->tag_cache_status;
  785. if ($this->tag_cache_status == 'CURRENT')
  786. {
  787. // If so, replace the marker in the tag with the cache data
  788. $this->log_item("Tag Cached and Cache is Current");
  789. $this->template = str_replace('M'.$i.$this->marker, $cache_contents, $this->template);
  790. }
  791. else
  792. {
  793. // Is a module or plug-in being requested?
  794. if ( ! in_array($this->tag_data[$i]['class'] , $this->modules))
  795. {
  796. if ( ! in_array($this->tag_data[$i]['class'] , $this->plugins))
  797. {
  798. $this->log_item("Invalid Tag");
  799. if ($this->EE->config->item('debug') >= 1)
  800. {
  801. if ($this->tag_data[$i]['tagparts'][0] == $this->tag_data[$i]['tagparts'][1] &&
  802. ! isset($this->tag_data[$i]['tagparts'][2]))
  803. {
  804. unset($this->tag_data[$i]['tagparts'][1]);
  805. }
  806. $error = $this->EE->lang->line('error_tag_syntax');
  807. $error .= '<br /><br />';
  808. $error .= htmlspecialchars(LD);
  809. $error .= 'exp:'.implode(':', $this->tag_data[$i]['tagparts']);
  810. $error .= htmlspecialchars(RD);
  811. $error .= '<br /><br />';
  812. $error .= $this->EE->lang->line('error_fix_syntax');
  813. $this->EE->output->fatal_error($error);
  814. }
  815. else
  816. return FALSE;
  817. }
  818. else
  819. {
  820. $plugins[] = $this->tag_data[$i]['class'];
  821. $this->log_item("Plugin Tag: ".ucfirst($this->tag_data[$i]['class']).'/'.$this->tag_data[$i]['method']);
  822. }
  823. }
  824. else
  825. {
  826. $modules[] = $this->tag_data[$i]['class'];
  827. $this->log_item("Module Tag: ".ucfirst($this->tag_data[$i]['class']).'/'.$this->tag_data[$i]['method']);
  828. }
  829. }
  830. }
  831. // Remove duplicate class names and re-order the array
  832. $plugins = array_values(array_unique($plugins));
  833. $modules = array_values(array_unique($modules));
  834. // Dynamically require the file that contains each class
  835. $this->log_item("Including Files for Plugins and Modules");
  836. foreach ($plugins as $plugin)
  837. {
  838. // make sure it's not already included just in case
  839. if ( ! class_exists($plugin))
  840. {
  841. if (in_array($plugin ,$this->EE->core->native_plugins))
  842. {
  843. require_once PATH_PI."pi.{$plugin}".EXT;
  844. }
  845. else
  846. {
  847. require_once PATH_THIRD."{$plugin}/pi.{$plugin}".EXT;
  848. }
  849. }
  850. }
  851. foreach ($modules as $module)
  852. {
  853. // make sure it's not already included just in case
  854. if ( ! class_exists($module))
  855. {
  856. if (in_array($module, $this->EE->core->native_modules))
  857. {
  858. require_once PATH_MOD."{$module}/mod.{$module}".EXT;
  859. }
  860. else
  861. {
  862. require_once PATH_THIRD."{$module}/mod.{$module}".EXT;
  863. }
  864. }
  865. }
  866. $this->log_item("Files for Plugins and Modules All Included");
  867. /** -----------------------------------
  868. /** Only Retrieve Data if Not Done Before and Modules Being Called
  869. /** -----------------------------------*/
  870. if (count($this->module_data) == 0 && count(array_intersect($this->modules, $modules)) > 0)
  871. {
  872. $this->EE->db->select('module_version, module_name');
  873. $query = $this->EE->db->get('modules');
  874. foreach($query->result_array() as $row)
  875. {
  876. $this->module_data[$row['module_name']] = array('version' => $row['module_version']);
  877. }
  878. }
  879. // Final data processing
  880. // Loop through the master array containing our extracted template data
  881. $this->log_item("Beginning Final Tag Data Processing");
  882. reset($this->tag_data);
  883. for ($i = 0; $i < count($this->tag_data); $i++)
  884. {
  885. if ($this->tag_data[$i]['cache'] != 'CURRENT')
  886. {
  887. $this->log_item("Calling Class/Method: ".ucfirst($this->tag_data[$i]['class'])."/".$this->tag_data[$i]['method']);
  888. /* ---------------------------------
  889. /* Plugin as Parameter
  890. /*
  891. /* - Example: channel="{exp:some_plugin}"
  892. /* - A bit of a hidden feature. Has been tested but not quite
  893. /* ready to say it is ready for prime time as I might want to
  894. /* move it to earlier in processing so that if there are
  895. /* multiple plugins being used as parameters it is only called
  896. /* once instead of for every single parameter. - Paul
  897. /* ---------------------------------*/
  898. if (substr_count($this->tag_data[$i]['tag'], LD.'exp') > 1 && isset($this->tag_data[$i]['params']['parse']) && $this->tag_data[$i]['params']['parse'] == 'inward')
  899. {
  900. foreach($this->tag_data[$i]['params'] as $name => $param)
  901. {
  902. if (stristr($this->tag_data[$i]['params'][$name], LD.'exp'))
  903. {
  904. $this->log_item("Plugin in Parameter, Processing Plugin First");
  905. $TMPL2 = $this->EE->functions->clone_object($this);
  906. while (is_int(strpos($TMPL2->tag_data[$i]['params'][$name], LD.'exp:')))
  907. {
  908. unset($this->EE->TMPL);
  909. $this->EE->TMPL = new EE_Template();
  910. $this->EE->TMPL->start_microtime = $this->start_microtime;
  911. $this->EE->TMPL->template = $TMPL2->tag_data[$i]['params'][$name];
  912. $this->EE->TMPL->tag_data = array();
  913. $this->EE->TMPL->var_single = array();
  914. $this->EE->TMPL->var_cond = array();
  915. $this->EE->TMPL->var_pair = array();
  916. $this->EE->TMPL->plugins = $TMPL2->plugins;
  917. $this->EE->TMPL->modules = $TMPL2->modules;
  918. $this->EE->TMPL->parse_tags();
  919. $this->EE->TMPL->process_tags();
  920. $this->EE->TMPL->loop_count = 0;
  921. $TMPL2->tag_data[$i]['params'][$name] = $this->EE->TMPL->template;
  922. $TMPL2->log = array_merge($TMPL2->log, $this->EE->TMPL->log);
  923. }
  924. foreach (get_object_vars($TMPL2) as $key => $value)
  925. {
  926. $this->$key = $value;
  927. }
  928. unset($TMPL2);
  929. $this->EE->TMPL = $this;
  930. }
  931. }
  932. }
  933. /** ---------------------------------
  934. /** Nested Plugins...
  935. /** ---------------------------------*/
  936. if (in_array($this->tag_data[$i]['class'] , $this->plugins) && strpos($this->tag_data[$i]['block'], LD.'exp:') !== FALSE)
  937. {
  938. if ( ! isset($this->tag_data[$i]['params']['parse']) OR $this->tag_data[$i]['params']['parse'] != 'inward')
  939. {
  940. $this->log_item("Nested Plugins in Tag, Parsing Outward First");
  941. $TMPL2 = $this->EE->functions->clone_object($this);
  942. while (is_int(strpos($TMPL2->tag_data[$i]['block'], LD.'exp:')))
  943. {
  944. unset($this->EE->TMPL);
  945. $this->EE->TMPL = new EE_Template();
  946. $this->EE->TMPL->start_microtime = $this->start_microtime;
  947. $this->EE->TMPL->template = $TMPL2->tag_data[$i]['block'];
  948. $this->EE->TMPL->tag_data = array();
  949. $this->EE->TMPL->var_single = array();
  950. $this->EE->TMPL->var_cond = array();
  951. $this->EE->TMPL->var_pair = array();
  952. $this->EE->TMPL->plugins = $TMPL2->plugins;
  953. $this->EE->TMPL->modules = $TMPL2->modules;
  954. $this->EE->TMPL->parse_tags();
  955. $this->EE->TMPL->process_tags();
  956. $this->EE->TMPL->loop_count = 0;
  957. $TMPL2->tag_data[$i]['block'] = $this->EE->TMPL->template;
  958. $TMPL2->log = array_merge($TMPL2->log, $this->EE->TMPL->log);
  959. }
  960. foreach (get_object_vars($TMPL2) as $key => $value)
  961. {
  962. $this->$key = $value;
  963. }
  964. unset($TMPL2);
  965. $this->EE->TMPL = $this;
  966. }
  967. }
  968. // Assign the data chunk, parameters
  969. // We moved the no_results_block here because of nested tags. The first
  970. // parsed tag has priority for that conditional.
  971. $this->tagdata = str_replace($this->tag_data[$i]['no_results_block'], '', $this->tag_data[$i]['block']);
  972. $this->tagparams = $this->tag_data[$i]['params'];
  973. $this->tagchunk = $this->tag_data[$i]['chunk'];
  974. $this->tagproper = $this->tag_data[$i]['tag'];
  975. $this->tagparts = $this->tag_data[$i]['tagparts'];
  976. $this->no_results = $this->tag_data[$i]['no_results'];
  977. $this->search_fields = $this->tag_data[$i]['search_fields'];
  978. /** -------------------------------------
  979. /** Assign Sites for Tag
  980. /** -------------------------------------*/
  981. $this->_fetch_site_ids();
  982. /** -------------------------------------
  983. /** Fetch Form Class/Id Attributes
  984. /** -------------------------------------*/
  985. $this->tag_data[$i] = $this->_assign_form_params($this->tag_data[$i]);
  986. /** -------------------------------------
  987. /** Relationship Data Pulled Out
  988. /** -------------------------------------*/
  989. // If the channel:entries tag or search:search_results is being called
  990. // we need to extract any relationship data that might be present.
  991. // Note: This needs to happen before extracting the variables
  992. // in the tag so it doesn't get confused as to which entry the
  993. // variables belong to.
  994. if (($this->tag_data[$i]['class'] == 'channel' AND $this->tag_data[$i]['method'] == 'entries')
  995. OR ($this->tag_data[$i]['class'] == 'search' AND $this->tag_data[$i]['method'] == 'search_results'))
  996. {
  997. $this->tagdata = $this->assign_relationship_data($this->tagdata);
  998. }
  999. // LEGACY CODE
  1000. // Fetch the variables for this particular tag
  1001. // Hopefully, with Jones' new parsing code we should be able to stop using the
  1002. // assign_variables and assign_conditional_variables() methods entirely. -Paul
  1003. $vars = $this->EE->functions->assign_variables($this->tag_data[$i]['block']);
  1004. if (count($this->related_markers) > 0)
  1005. {
  1006. foreach ($this->related_markers as $mkr)
  1007. {
  1008. if ( ! isset($vars['var_single'][$mkr]))
  1009. {
  1010. $vars['var_single'][$mkr] = $mkr;
  1011. }
  1012. }
  1013. }
  1014. $this->var_single = $vars['var_single'];
  1015. $this->var_pair = $vars['var_pair'];
  1016. if ($this->related_id != '')
  1017. {
  1018. $this->var_single[$this->related_id] = $this->related_id;
  1019. $this->related_id = '';
  1020. }
  1021. // Assign the class name and method name
  1022. $class_name = ucfirst($this->tag_data[$i]['class']);
  1023. $meth_name = $this->tag_data[$i]['method'];
  1024. // If it's a third party class or a first party module,
  1025. // add the root folder to the loader paths so we can use
  1026. // libraries, models, and helpers
  1027. $package_path = '';
  1028. if ( ! in_array($this->tag_data[$i]['class'], $this->EE->core->native_plugins))
  1029. {
  1030. $package_path = in_array($this->tag_data[$i]['class'], $this->EE->core->native_modules) ? PATH_MOD : PATH_THIRD;
  1031. $package_path .= strtolower($this->tag_data[$i]['class'].'/');
  1032. $this->EE->load->add_package_path($package_path);
  1033. }
  1034. // Dynamically instantiate the class.
  1035. // If module, only if it is installed...
  1036. if (in_array($this->tag_data[$i]['class'], $this->modules) && ! isset($this->module_data[$class_name]))
  1037. {
  1038. $this->log_item("Problem Processing Module: Module Not Installed");
  1039. }
  1040. else
  1041. {
  1042. $this->log_item(" -> Class Called: ".$class_name);
  1043. $EE = new $class_name();
  1044. }
  1045. /** ----------------------------------
  1046. /** Does method exist? Is This A Module and Is It Installed?
  1047. /** ----------------------------------*/
  1048. if ((in_array($this->tag_data[$i]['class'], $this->modules) && ! isset($this->module_data[$class_name])) OR ! method_exists($EE, $meth_name))
  1049. {
  1050. $this->log_item("Tag Not Processed: Method Inexistent or Module Not Installed");
  1051. if ($this->EE->config->item('debug') >= 1)
  1052. {
  1053. if ($this->tag_data[$i]['tagparts'][0] == $this->tag_data[$i]['tagparts'][1] &&
  1054. ! isset($this->tag_data[$i]['tagparts'][2]))
  1055. {
  1056. unset($this->tag_data[$i]['tagparts'][1]);
  1057. }
  1058. $error = $this->EE->lang->line('error_tag_module_processing');
  1059. $error .= '<br /><br />';
  1060. $error .= htmlspecialchars(LD);
  1061. $error .= 'exp:'.implode(':', $this->tag_data[$i]['tagparts']);
  1062. $error .= htmlspecialchars(RD);
  1063. $error .= '<br /><br />';
  1064. $error .= str_replace('%x', $this->tag_data[$i]['class'], str_replace('%y', $meth_name, $this->EE->lang->line('error_fix_module_processing')));
  1065. $this->EE->output->fatal_error($error);
  1066. }
  1067. else
  1068. {
  1069. return;
  1070. }
  1071. }
  1072. /*
  1073. OK, lets grab the data returned from the class.
  1074. First, however, lets determine if the tag has one or two segments.
  1075. If it only has one, we don't want to call the constructor again since
  1076. it was already called during instantiation.
  1077. Note: If it only has one segment, only the object constructor will be called.
  1078. Since constructors can't return a value just by initialializing the object
  1079. the output of the class must be assigned to a variable called $this->return_data
  1080. */
  1081. $this->log_item(" -> Method Called: ".$meth_name);
  1082. if (strtolower($class_name) == $meth_name)
  1083. {
  1084. $return_data = (isset($EE->return_data)) ? $EE->return_data : '';
  1085. }
  1086. else
  1087. {
  1088. $return_data = $EE->$meth_name();
  1089. }
  1090. // if it's a third party add-on or module, remove the temporarily added path for local libraries, models, etc.
  1091. // if a "no results" template is returned, $this->tag_data will be reset inside of the scope
  1092. // of the tag being processed. So let's use the locally scoped variable for the class name
  1093. if ($package_path)
  1094. {
  1095. $this->EE->load->remove_package_path();
  1096. }
  1097. /** ----------------------------------
  1098. /** 404 Page Triggered, Cease All Processing of Tags From Now On
  1099. /** ----------------------------------*/
  1100. if ($this->cease_processing === TRUE)
  1101. {
  1102. return;
  1103. }
  1104. $this->log_item(" -> Data Returned");
  1105. // Write cache file if needed
  1106. if ($this->tag_data[$i]['cache'] == 'EXPIRED')
  1107. {
  1108. $this->write_cache_file($this->tag_data[$i]['cfile'], $return_data);
  1109. }
  1110. // Replace the temporary markers we added earlier with the fully parsed data
  1111. $this->template = str_replace('M'.$i.$this->marker, $return_data, $this->template);
  1112. // Initialize data in case there are susequent loops
  1113. $this->var_single = array();
  1114. $this->var_cond = array();
  1115. $this->var_pair = array();
  1116. unset($return_data);
  1117. unset($class_name);
  1118. unset($meth_name);
  1119. unset($EE);
  1120. }
  1121. }
  1122. }
  1123. // --------------------------------------------------------------------
  1124. /**
  1125. * Process Tags
  1126. *
  1127. * Channel entries can have related entries embedded within them.
  1128. * We'll extract the related tag data, stash it away in an array, and
  1129. * replace it with a marker string so that the template parser
  1130. * doesn't see it. In the channel class we'll check to see if the
  1131. * $this->EE->TMPL->related_data array contains anything. If so, we'll celebrate
  1132. * wildly.
  1133. *
  1134. * @access public
  1135. * @param string
  1136. * @return string
  1137. */
  1138. function assign_relationship_data($chunk)
  1139. {
  1140. $this->related_markers = array();
  1141. if (preg_match_all("/".LD."related_entries\s+id\s*=\s*[\"\'](.+?)[\"\']".RD."(.+?)".LD.'\/'."related_entries".RD."/is", $chunk, $matches))
  1142. {
  1143. $this->log_item("Assigning Related Entry Data");
  1144. $no_rel_content = '';
  1145. for ($j = 0; $j < count($matches[0]); $j++)
  1146. {
  1147. $rand = $this->EE->functions->random('alnum', 8);
  1148. $marker = LD.'REL['.$matches[1][$j].']'.$rand.'REL'.RD;
  1149. if (preg_match("/".LD."if no_related_entries".RD."(.*?)".LD.'\/'."if".RD."/s", $matches[2][$j], $no_rel_match))
  1150. {
  1151. // Match the entirety of the conditional
  1152. if (stristr($no_rel_match[1], LD.'if'))
  1153. {
  1154. $match[0] = $this->EE->functions->full_tag($no_rel_match[0], $matches[2][$j], LD.'if', LD.'\/'."if".RD);
  1155. }
  1156. $no_rel_content = substr($no_rel_match[0], strlen(LD."if no_related_entries".RD), -strlen(LD.'/'."if".RD));
  1157. }
  1158. $this->related_markers[] = $matches[1][$j];
  1159. $vars = $this->EE->functions->assign_variables($matches[2][$j]);
  1160. $this->related_id = $matches[1][$j];
  1161. $this->related_data[$rand] = array(
  1162. 'marker' => $rand,
  1163. 'field_name' => $matches[1][$j],
  1164. 'tagdata' => $matches[2][$j],
  1165. 'var_single' => $vars['var_single'],
  1166. 'var_pair' => $vars['var_pair'],
  1167. 'var_cond' => $this->EE->functions->assign_conditional_variables($matches[2][$j], '\/', LD, RD),
  1168. 'no_rel_content' => $no_rel_content
  1169. );
  1170. $chunk = str_replace($matches[0][$j], $marker, $chunk);
  1171. }
  1172. }
  1173. if (preg_match_all("/".LD."reverse_related_entries\s*(.*?)".RD."(.+?)".LD.'\…

Large files files are truncated, but you can click here to view the full file