PageRenderTime 79ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/system/modules/weblog/mod.weblog.php

https://github.com/danboy/Croissierd
PHP | 7538 lines | 5257 code | 1360 blank | 921 comment | 1104 complexity | 9e9dbfb8c6d68a5c7e56286895847a9a MD5 | raw file
  1. <?php
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2003 - 2010 EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/docs/license.html
  13. =====================================================
  14. File: mod.weblog.php
  15. -----------------------------------------------------
  16. Purpose: Weblog class
  17. =====================================================
  18. */
  19. if ( ! defined('EXT'))
  20. {
  21. exit('Invalid file request');
  22. }
  23. class Weblog {
  24. var $limit = '100'; // Default maximum query results if not specified.
  25. // These variable are all set dynamically
  26. var $query;
  27. var $TYPE;
  28. var $entry_id = '';
  29. var $uri = '';
  30. var $uristr = '';
  31. var $return_data = ''; // Final data
  32. var $tb_action_id = '';
  33. var $basepath = '';
  34. var $hit_tracking_id = FALSE;
  35. var $sql = FALSE;
  36. var $display_tb_rdf = FALSE;
  37. var $cfields = array();
  38. var $dfields = array();
  39. var $rfields = array();
  40. var $mfields = array();
  41. var $categories = array();
  42. var $catfields = array();
  43. var $weblog_name = array();
  44. var $weblogs_array = array();
  45. var $related_entries = array();
  46. var $reverse_related_entries= array();
  47. var $reserved_cat_segment = '';
  48. var $use_category_names = FALSE;
  49. var $dynamic_sql = FALSE;
  50. var $tb_captcha_hash = '';
  51. var $cat_request = FALSE;
  52. var $enable = array(); // modified by various tags with disable= parameter
  53. // These are used with the nested category trees
  54. var $category_list = array();
  55. var $cat_full_array = array();
  56. var $cat_array = array();
  57. var $temp_array = array();
  58. var $category_count = 0;
  59. // Pagination variables
  60. var $paginate = FALSE;
  61. var $field_pagination = FALSE;
  62. var $paginate_data = '';
  63. var $pagination_links = '';
  64. var $page_next = '';
  65. var $page_previous = '';
  66. var $current_page = 1;
  67. var $total_pages = 1;
  68. var $multi_fields = array();
  69. var $display_by = '';
  70. var $total_rows = 0;
  71. var $pager_sql = '';
  72. var $p_limit = '';
  73. var $p_page = '';
  74. // SQL Caching
  75. var $sql_cache_dir = 'sql_cache/';
  76. // Misc. - Class variable usable by extensions
  77. var $misc = FALSE;
  78. /** ----------------------------------------
  79. /** Constructor
  80. /** ----------------------------------------*/
  81. function Weblog()
  82. {
  83. global $PREFS, $IN, $TMPL;
  84. $this->p_limit = $this->limit;
  85. $this->QSTR = ($IN->Pages_QSTR != '') ? $IN->Pages_QSTR : $IN->QSTR;
  86. if ($PREFS->ini("use_category_name") == 'y' AND $PREFS->ini("reserved_category_word") != '')
  87. {
  88. $this->use_category_names = $PREFS->ini("use_category_name");
  89. $this->reserved_cat_segment = $PREFS->ini("reserved_category_word");
  90. }
  91. // a number tags utilize the disable= parameter, set it here
  92. if (is_object($TMPL))
  93. {
  94. $this->_fetch_disable_param();
  95. }
  96. }
  97. /* END */
  98. /** ----------------------------------------
  99. /** Initialize values
  100. /** ----------------------------------------*/
  101. function initialize()
  102. {
  103. $this->sql = '';
  104. $this->return_data = '';
  105. }
  106. /* END */
  107. /** ----------------------------------------
  108. /** Fetch Cache
  109. /** ----------------------------------------*/
  110. function fetch_cache($identifier = '')
  111. {
  112. global $IN, $TMPL;
  113. $tag = ($identifier == '') ? $TMPL->tagproper : $TMPL->tagproper.$identifier;
  114. if ($TMPL->fetch_param('dynamic_parameters') !== FALSE AND isset($_POST) AND count($_POST) > 0)
  115. {
  116. foreach (explode('|', $TMPL->fetch_param('dynamic_parameters')) as $var)
  117. {
  118. if (isset($_POST[$var]) AND in_array($var, array('weblog', 'entry_id', 'category', 'orderby', 'sort', 'sticky', 'show_future_entries', 'show_expired', 'entry_id_from', 'entry_id_to', 'not_entry_id', 'start_on', 'stop_before', 'year', 'month', 'day', 'display_by', 'limit', 'username', 'status', 'group_id', 'cat_limit', 'month_limit', 'offset', 'author_id')))
  119. {
  120. $tag .= $var.'="'.$_POST[$var].'"';
  121. }
  122. if (isset($_POST[$var]) && strncmp($var, 'search:', 7) == 0)
  123. {
  124. $tag .= $var.'="'.substr($_POST[$var], 7).'"';
  125. }
  126. }
  127. }
  128. $cache_file = PATH_CACHE.$this->sql_cache_dir.md5($tag.$this->uri);
  129. if ( ! $fp = @fopen($cache_file, 'rb'))
  130. {
  131. return FALSE;
  132. }
  133. flock($fp, LOCK_SH);
  134. $sql = @fread($fp, filesize($cache_file));
  135. flock($fp, LOCK_UN);
  136. fclose($fp);
  137. return $sql;
  138. }
  139. /* END */
  140. /** ----------------------------------------
  141. /** Save Cache
  142. /** ----------------------------------------*/
  143. function save_cache($sql, $identifier = '')
  144. {
  145. global $IN, $TMPL;
  146. $tag = ($identifier == '') ? $TMPL->tagproper : $TMPL->tagproper.$identifier;
  147. $cache_dir = PATH_CACHE.$this->sql_cache_dir;
  148. $cache_file = $cache_dir.md5($tag.$this->uri);
  149. if ( ! @is_dir($cache_dir))
  150. {
  151. if ( ! @mkdir($cache_dir, 0777))
  152. {
  153. return FALSE;
  154. }
  155. if ($fp = @fopen($cache_dir.'/index.html', 'wb'))
  156. {
  157. fclose($fp);
  158. }
  159. @chmod($cache_dir, 0777);
  160. }
  161. if ( ! $fp = @fopen($cache_file, 'wb'))
  162. {
  163. return FALSE;
  164. }
  165. flock($fp, LOCK_EX);
  166. fwrite($fp, $sql);
  167. flock($fp, LOCK_UN);
  168. fclose($fp);
  169. @chmod($cache_file, 0777);
  170. return TRUE;
  171. }
  172. /* END */
  173. /** ----------------------------------------
  174. /** Weblog entries
  175. /** ----------------------------------------*/
  176. function entries()
  177. {
  178. global $IN, $PREFS, $DB, $TMPL, $FNS;
  179. // If the "related_categories" mode is enabled
  180. // we'll call the "related_categories" function
  181. // and bail out.
  182. if ($TMPL->fetch_param('related_categories_mode') == 'on')
  183. {
  184. return $this->related_entries();
  185. }
  186. // Onward...
  187. $this->initialize();
  188. $this->uri = ($this->QSTR != '') ? $this->QSTR : 'index.php';
  189. if ($this->enable['custom_fields'] == TRUE)
  190. {
  191. $this->fetch_custom_weblog_fields();
  192. }
  193. if ($this->enable['member_data'] == TRUE)
  194. {
  195. $this->fetch_custom_member_fields();
  196. }
  197. if ($this->enable['pagination'] == TRUE)
  198. {
  199. $this->fetch_pagination_data();
  200. }
  201. $save_cache = FALSE;
  202. if ($PREFS->ini('enable_sql_caching') == 'y')
  203. {
  204. if (FALSE == ($this->sql = $this->fetch_cache()))
  205. {
  206. $save_cache = TRUE;
  207. }
  208. else
  209. {
  210. if ($TMPL->fetch_param('dynamic') != 'off')
  211. {
  212. if (preg_match("#(^|\/)C(\d+)#", $this->QSTR, $match) OR in_array($this->reserved_cat_segment, explode("/", $this->QSTR)))
  213. {
  214. $this->cat_request = TRUE;
  215. }
  216. }
  217. }
  218. if (FALSE !== ($cache = $this->fetch_cache('pagination_count')))
  219. {
  220. if (FALSE !== ($this->fetch_cache('field_pagination')))
  221. {
  222. if (FALSE !== ($pg_query = $this->fetch_cache('pagination_query')))
  223. {
  224. $this->paginate = TRUE;
  225. $this->field_pagination = TRUE;
  226. $this->create_pagination(trim($cache), $DB->query(trim($pg_query)));
  227. }
  228. }
  229. else
  230. {
  231. $this->create_pagination(trim($cache));
  232. }
  233. }
  234. }
  235. if ($this->sql == '')
  236. {
  237. $this->build_sql_query();
  238. }
  239. if ($this->sql == '')
  240. {
  241. return $TMPL->no_results();
  242. }
  243. if ($save_cache == TRUE)
  244. {
  245. $this->save_cache($this->sql);
  246. }
  247. $this->query = $DB->query($this->sql);
  248. if ($this->query->num_rows == 0)
  249. {
  250. return $TMPL->no_results();
  251. }
  252. /* -------------------------------------
  253. /* "Relaxed" View Tracking
  254. /*
  255. /* Some people have tags that are used to mimic a single-entry
  256. /* page without it being dynamic. This allows Entry View Tracking
  257. /* to work for ANY combination that results in only one entry
  258. /* being returned by the tag, including weblog query caching.
  259. /*
  260. /* Hidden Configuration Variable
  261. /* - relaxed_track_views => Allow view tracking on non-dynamic
  262. /* single entries (y/n)
  263. /* -------------------------------------*/
  264. if ($PREFS->ini('relaxed_track_views') === 'y' AND $this->query->num_rows == 1)
  265. {
  266. $this->hit_tracking_id = $this->query->row['entry_id'];
  267. }
  268. $this->track_views();
  269. if ( ! class_exists('Typography'))
  270. {
  271. require PATH_CORE.'core.typography'.EXT;
  272. }
  273. $this->TYPE = new Typography;
  274. $this->TYPE->convert_curly = FALSE;
  275. if ($this->enable['categories'] == TRUE)
  276. {
  277. $this->fetch_categories();
  278. }
  279. if ($this->enable['trackbacks'] == TRUE)
  280. {
  281. $this->tb_action_id = $FNS->fetch_action_id('Trackback_CP', 'receive_trackback');
  282. }
  283. $this->parse_weblog_entries();
  284. if ($this->enable['pagination'] == TRUE)
  285. {
  286. $this->add_pagination_data();
  287. }
  288. // Does the tag contain "related entries" that we need to parse out?
  289. if (count($TMPL->related_data) > 0 AND count($this->related_entries) > 0)
  290. {
  291. $this->parse_related_entries();
  292. }
  293. if (count($TMPL->reverse_related_data) > 0 AND count($this->reverse_related_entries) > 0)
  294. {
  295. $this->parse_reverse_related_entries();
  296. }
  297. return $this->return_data;
  298. }
  299. /* END */
  300. /** ----------------------------------------
  301. /** Process related entries
  302. /** ----------------------------------------*/
  303. function parse_related_entries()
  304. {
  305. global $TMPL, $DB, $REGX, $FNS;
  306. $sql = "SELECT rel_id, rel_parent_id, rel_child_id, rel_type, rel_data
  307. FROM exp_relationships
  308. WHERE rel_id IN (";
  309. $templates = array();
  310. foreach ($this->related_entries as $val)
  311. {
  312. $x = explode('_', $val);
  313. $sql .= "'".$x['0']."',";
  314. $templates[] = array($x['0'], $x['1'], $TMPL->related_data[$x['1']]);
  315. }
  316. $sql = substr($sql, 0, -1).')';
  317. $query = $DB->query($sql);
  318. if ($query->num_rows == 0)
  319. return;
  320. /* --------------------------------
  321. /* Without this the Related Entries were inheriting the parameters of
  322. /* the enclosing Weblog Entries tag. Sometime in the future we will
  323. /* likely allow Related Entries to have their own parameters
  324. /* --------------------------------*/
  325. $TMPL->tagparams = array('rdf'=> "off");
  326. $return_data = $this->return_data;
  327. foreach ($templates as $temp)
  328. {
  329. foreach ($query->result as $row)
  330. {
  331. if ($row['rel_id'] != $temp['0'])
  332. continue;
  333. /* --------------------------------------
  334. /* If the data is emptied (cache cleared), then we
  335. /* rebuild it with fresh data so processing can continue.
  336. /* --------------------------------------*/
  337. if (trim($row['rel_data']) == '')
  338. {
  339. $rewrite = array(
  340. 'type' => $row['rel_type'],
  341. 'parent_id' => $row['rel_parent_id'],
  342. 'child_id' => $row['rel_child_id'],
  343. 'related_id' => $row['rel_id']
  344. );
  345. $FNS->compile_relationship($rewrite, FALSE);
  346. $results = $DB->query("SELECT rel_data FROM exp_relationships WHERE rel_id = '".$row['rel_id']."'");
  347. $row['rel_data'] = $results->row['rel_data'];
  348. }
  349. /** --------------------------------------
  350. /** Begin Processing
  351. /** --------------------------------------*/
  352. $this->initialize();
  353. if ($reldata = @unserialize($row['rel_data']))
  354. {
  355. $TMPL->var_single = $temp['2']['var_single'];
  356. $TMPL->var_pair = $temp['2']['var_pair'];
  357. $TMPL->var_cond = $temp['2']['var_cond'];
  358. $TMPL->tagdata = $temp['2']['tagdata'];
  359. if ($row['rel_type'] == 'blog')
  360. {
  361. // Bug fix for when categories were not being inserted
  362. // correctly for related weblog entries. Bummer.
  363. if (sizeof($reldata['categories'] == 0) && ! isset($reldata['cats_fixed']))
  364. {
  365. $fixdata = array(
  366. 'type' => $row['rel_type'],
  367. 'parent_id' => $row['rel_parent_id'],
  368. 'child_id' => $row['rel_child_id'],
  369. 'related_id' => $row['rel_id']
  370. );
  371. $FNS->compile_relationship($fixdata, FALSE);
  372. $reldata['categories'] = $FNS->cat_array;
  373. }
  374. $this->query = $reldata['query'];
  375. $this->categories = array($this->query->row['entry_id'] => $reldata['categories']);
  376. $this->parse_weblog_entries();
  377. $marker = LD."REL[".$row['rel_id']."][".$temp['2']['field_name']."]".$temp['1']."REL".RD;
  378. $return_data = str_replace($marker, $this->return_data, $return_data);
  379. }
  380. elseif ($row['rel_type'] == 'gallery')
  381. {
  382. if ( ! class_exists('Gallery'))
  383. {
  384. include_once PATH_MOD.'gallery/mod.gallery'.EXT;
  385. }
  386. $GAL = new Gallery;
  387. $GAL->one_entry = TRUE;
  388. $GAL->query = $reldata['query'];
  389. $GAL->TYPE = $this->TYPE;
  390. $GAL->parse_gallery_tag();
  391. $GAL->parse_gallery_entries();
  392. $marker = LD."REL[".$row['rel_id']."][".$temp['2']['field_name']."]".$temp['1']."REL".RD;
  393. $return_data = str_replace($marker, $GAL->return_data, $return_data);
  394. }
  395. }
  396. }
  397. }
  398. $this->return_data = $return_data;
  399. }
  400. /* END */
  401. /** ----------------------------------------
  402. /** Process reverse related entries
  403. /** ----------------------------------------*/
  404. function parse_reverse_related_entries()
  405. {
  406. global $TMPL, $DB, $REGX, $FNS;
  407. $sql = "SELECT rel_id, rel_parent_id, rel_child_id, rel_type, reverse_rel_data
  408. FROM exp_relationships
  409. WHERE rel_child_id IN ('".implode("','", array_keys($this->reverse_related_entries))."')
  410. AND rel_type = 'blog'";
  411. $query = $DB->query($sql);
  412. if ($query->num_rows == 0)
  413. {
  414. // remove Reverse Related tags for these entries
  415. foreach ($this->reverse_related_entries as $entry_id => $templates)
  416. {
  417. foreach($templates as $tkey => $template)
  418. {
  419. $this->return_data = str_replace(LD."REV_REL[".$TMPL->reverse_related_data[$template]['marker']."][".$entry_id."]REV_REL".RD, $TMPL->reverse_related_data[$template]['no_rev_content'], $this->return_data);
  420. }
  421. }
  422. return;
  423. }
  424. /** --------------------------------
  425. /** Data Processing Time
  426. /** --------------------------------*/
  427. $entry_data = array();
  428. foreach($query->result as $row)
  429. {
  430. /* --------------------------------------
  431. /* If the data is emptied (cache cleared or first process), then we
  432. /* rebuild it with fresh data so processing can continue.
  433. /* --------------------------------------*/
  434. if (trim($row['reverse_rel_data']) == '')
  435. {
  436. $rewrite = array(
  437. 'type' => $row['rel_type'],
  438. 'parent_id' => $row['rel_parent_id'],
  439. 'child_id' => $row['rel_child_id'],
  440. 'related_id' => $row['rel_id']
  441. );
  442. $FNS->compile_relationship($rewrite, FALSE, TRUE);
  443. $results = $DB->query("SELECT reverse_rel_data FROM exp_relationships WHERE rel_parent_id = '".$row['rel_parent_id']."'");
  444. $row['reverse_rel_data'] = $results->row['reverse_rel_data'];
  445. }
  446. /** --------------------------------------
  447. /** Unserialize the entries data, please
  448. /** --------------------------------------*/
  449. if ($revreldata = @unserialize($row['reverse_rel_data']))
  450. {
  451. $entry_data[$row['rel_child_id']][$row['rel_parent_id']] = $revreldata;
  452. }
  453. }
  454. /* --------------------------------
  455. /* Without this the Reverse Related Entries were inheriting the parameters of
  456. /* the enclosing Weblog Entries tag, which is not appropriate.
  457. /* --------------------------------*/
  458. $TMPL->tagparams = array('rdf'=> "off");
  459. $return_data = $this->return_data;
  460. foreach ($this->reverse_related_entries as $entry_id => $templates)
  461. {
  462. /** --------------------------------------
  463. /** No Entries? Remove Reverse Related Tags and Continue to Next Entry
  464. /** --------------------------------------*/
  465. if ( ! isset($entry_data[$entry_id]))
  466. {
  467. foreach($templates as $tkey => $template)
  468. {
  469. $return_data = str_replace(LD."REV_REL[".$TMPL->reverse_related_data[$template]['marker']."][".$entry_id."]REV_REL".RD, $TMPL->reverse_related_data[$template]['no_rev_content'], $return_data);
  470. }
  471. continue;
  472. }
  473. /** --------------------------------------
  474. /** Process Our Reverse Related Templates
  475. /** --------------------------------------*/
  476. foreach($templates as $tkey => $template)
  477. {
  478. $i = 0;
  479. $cats = array();
  480. $params = $TMPL->reverse_related_data[$template]['params'];
  481. if ( ! is_array($params))
  482. {
  483. $params = array('open');
  484. }
  485. elseif( ! isset($params['status']))
  486. {
  487. $params['status'] = 'open';
  488. }
  489. /** --------------------------------------
  490. /** Entries have to be ordered, sorted and other stuff
  491. /** --------------------------------------*/
  492. $new = array();
  493. $order = ( ! isset($params['orderby'])) ? 'date' : $params['orderby'];
  494. $offset = ( ! isset($params['offset']) OR ! is_numeric($params['offset'])) ? 0 : $params['offset'];
  495. $limit = ( ! isset($params['limit']) OR ! is_numeric($params['limit'])) ? 100 : $params['limit'];
  496. $sort = ( ! isset($params['sort'])) ? 'asc' : $params['sort'];
  497. $random = ($order == 'random') ? TRUE : FALSE;
  498. $base_orders = array('random', 'date', 'title', 'url_title', 'edit_date', 'comment_total', 'username', 'screen_name', 'most_recent_comment', 'expiration_date',
  499. 'view_count_one', 'view_count_two', 'view_count_three', 'view_count_four');
  500. if ( ! in_array($order, $base_orders))
  501. {
  502. $set = 'n';
  503. foreach($this->cfields as $site_id => $cfields)
  504. {
  505. if ( isset($cfields[$order]))
  506. {
  507. $order = 'field_id_'.$cfields[$order];
  508. $set = 'y';
  509. break;
  510. }
  511. }
  512. if ( $set == 'n' )
  513. {
  514. $order = 'date';
  515. }
  516. }
  517. if ($order == 'date' OR $order == 'random')
  518. {
  519. $order = 'entry_date';
  520. }
  521. if (isset($params['weblog']) && trim($params['weblog']) != '')
  522. {
  523. if (sizeof($this->weblogs_array) == 0)
  524. {
  525. $results = $DB->query("SELECT weblog_id, blog_name FROM exp_weblogs WHERE site_id IN ('".implode("','", $TMPL->site_ids)."') AND is_user_blog = 'n'");
  526. foreach($results->result as $row)
  527. {
  528. $this->weblogs_array[$row['weblog_id']] = $row['blog_name'];
  529. }
  530. }
  531. $weblogs = explode('|', trim($params['weblog']));
  532. $allowed = array();
  533. if (strncmp($weblogs[0], 'not ', 4) == 0)
  534. {
  535. $weblogs['0'] = trim(substr($weblogs['0'], 3));
  536. $allowed = $this->weblogs_array;
  537. foreach($weblogs as $name)
  538. {
  539. if (in_array($name, $allowed))
  540. {
  541. foreach (array_keys($allowed, $name) AS $k)
  542. {
  543. unset($allowed[$k]);
  544. }
  545. }
  546. }
  547. }
  548. else
  549. {
  550. foreach($weblogs as $name)
  551. {
  552. if (in_array($name, $this->weblogs_array))
  553. {
  554. foreach (array_keys($this->weblogs_array, $name) AS $k)
  555. {
  556. $allowed[$k] = $name;
  557. }
  558. }
  559. }
  560. }
  561. }
  562. $stati = array();
  563. if (isset($params['status']) && trim($params['status']) != '')
  564. {
  565. $stati = explode('|', trim($params['status']));
  566. $status_state = 'positive';
  567. if (substr($stati['0'], 0, 4) == 'not ')
  568. {
  569. $stati['0'] = trim(substr($stati['0'], 3));
  570. $status_state = 'negative';
  571. $stati[] = 'closed';
  572. }
  573. }
  574. // lower case to match MySQL's case-insensitivity
  575. $stati = array_map('strtolower', $stati);
  576. $r = 1; // Fixes a problem when a sorting key occurs twice
  577. foreach($entry_data[$entry_id] as $relating_data)
  578. {
  579. if ( ! isset($params['weblog']) OR array_key_exists($relating_data['query']->row['weblog_id'], $allowed))
  580. {
  581. if (isset($stati) && ! empty($stati) && isset($relating_data['query']->row[$order]))
  582. {
  583. if ($status_state == 'negative' && ! in_array(strtolower($relating_data['query']->row['status']), $stati))
  584. {
  585. $new[$relating_data['query']->row[$order].'_'.$r] = $relating_data;
  586. }
  587. elseif($status_state == 'positive' && in_array(strtolower($relating_data['query']->row['status']), $stati))
  588. {
  589. $new[$relating_data['query']->row[$order].'_'.$r] = $relating_data;
  590. }
  591. }
  592. elseif (strtolower($relating_data['query']->row['status']) == 'open')
  593. {
  594. $new[$relating_data['query']->row[$order].'_'.$r] = $relating_data;
  595. }
  596. ++$r;
  597. }
  598. }
  599. if ($random === TRUE)
  600. {
  601. shuffle($new);
  602. }
  603. elseif ($sort == 'asc') // 1 to 10, A to Z
  604. {
  605. uksort($new, 'strnatcasecmp');
  606. }
  607. else
  608. {
  609. uksort($new, 'strnatcasecmp');
  610. $new = array_reverse($new, TRUE);
  611. }
  612. $output_data[$entry_id] = array_slice($new, $offset, $limit);
  613. if (sizeof($output_data[$entry_id]) == 0)
  614. {
  615. $return_data = str_replace(LD."REV_REL[".$TMPL->reverse_related_data[$template]['marker']."][".$entry_id."]REV_REL".RD, $TMPL->reverse_related_data[$template]['no_rev_content'], $return_data);
  616. continue;
  617. }
  618. /** --------------------------------------
  619. /** Finally! We get to process our parents
  620. /** --------------------------------------*/
  621. foreach($output_data[$entry_id] as $relating_data)
  622. {
  623. if ($i == 0)
  624. {
  625. $query = $FNS->clone_object($relating_data['query']);
  626. }
  627. else
  628. {
  629. $query->result[] = $relating_data['query']->row;
  630. }
  631. $cats[$relating_data['query']->row['entry_id']] = $relating_data['categories'];
  632. ++$i;
  633. }
  634. $query->num_rows = $i;
  635. $this->initialize();
  636. $TMPL->var_single = $TMPL->reverse_related_data[$template]['var_single'];
  637. $TMPL->var_pair = $TMPL->reverse_related_data[$template]['var_pair'];
  638. $TMPL->var_cond = $TMPL->reverse_related_data[$template]['var_cond'];
  639. $TMPL->tagdata = $TMPL->reverse_related_data[$template]['tagdata'];
  640. $this->query = $query;
  641. $this->categories = $cats;
  642. $this->parse_weblog_entries();
  643. $return_data = str_replace( LD."REV_REL[".$TMPL->reverse_related_data[$template]['marker']."][".$entry_id."]REV_REL".RD,
  644. $this->return_data,
  645. $return_data);
  646. }
  647. }
  648. $this->return_data = $return_data;
  649. }
  650. /* END */
  651. /** ----------------------------------------
  652. /** Track Views
  653. /** ----------------------------------------*/
  654. function track_views()
  655. {
  656. global $DB, $TMPL, $PREFS;
  657. if ($PREFS->ini('enable_entry_view_tracking') == 'n')
  658. {
  659. return;
  660. }
  661. if ( ! $TMPL->fetch_param('track_views') OR $this->hit_tracking_id === FALSE OR ! in_array($TMPL->fetch_param('track_views'), array("one", "two", "three", "four")))
  662. {
  663. return;
  664. }
  665. if ($this->field_pagination == TRUE AND $this->p_page > 0)
  666. {
  667. return;
  668. }
  669. $column = "view_count_".$TMPL->fetch_param('track_views');
  670. $sql = "UPDATE exp_weblog_titles SET {$column} = ({$column} + 1) WHERE ";
  671. $sql .= (is_numeric($this->hit_tracking_id)) ? "entry_id = {$this->hit_tracking_id}" : "url_title = '".$DB->escape_str($this->hit_tracking_id)."'";
  672. $DB->query($sql);
  673. }
  674. /* END */
  675. /** ----------------------------------------
  676. /** Fetch pagination data
  677. /** ----------------------------------------*/
  678. function fetch_pagination_data()
  679. {
  680. global $TMPL, $FNS, $EXT;
  681. if (preg_match("/".LD."paginate".RD."(.+?)".LD.SLASH."paginate".RD."/s", $TMPL->tagdata, $match))
  682. {
  683. if ($TMPL->fetch_param('paginate_type') == 'field')
  684. {
  685. if (preg_match("/".LD."multi_field\=[\"'](.+?)[\"']".RD."/s", $TMPL->tagdata, $mmatch))
  686. {
  687. $this->multi_fields = $FNS->fetch_simple_conditions($mmatch['1']);
  688. $this->field_pagination = TRUE;
  689. }
  690. }
  691. // -------------------------------------------
  692. // 'weblog_module_fetch_pagination_data' hook.
  693. // - Works with the 'weblog_module_create_pagination' hook
  694. // - Developers, if you want to modify the $this object remember
  695. // to use a reference on function call.
  696. //
  697. if ($EXT->active_hook('weblog_module_fetch_pagination_data') === TRUE)
  698. {
  699. $edata = $EXT->universal_call_extension('weblog_module_fetch_pagination_data', $this);
  700. if ($EXT->end_script === TRUE) return;
  701. }
  702. //
  703. // -------------------------------------------
  704. $this->paginate = TRUE;
  705. $this->paginate_data = $match['1'];
  706. $TMPL->tagdata = preg_replace("/".LD."paginate".RD.".+?".LD.SLASH."paginate".RD."/s", "", $TMPL->tagdata);
  707. }
  708. }
  709. /* END */
  710. /** ----------------------------------------
  711. /** Add pagination data to result
  712. /** ----------------------------------------*/
  713. function add_pagination_data()
  714. {
  715. global $TMPL;
  716. if ($this->pagination_links == '')
  717. {
  718. // return;
  719. }
  720. if ($this->paginate == TRUE)
  721. {
  722. $this->paginate_data = str_replace(LD.'current_page'.RD, $this->current_page, $this->paginate_data);
  723. $this->paginate_data = str_replace(LD.'total_pages'.RD, $this->total_pages, $this->paginate_data);
  724. $this->paginate_data = str_replace(LD.'pagination_links'.RD, $this->pagination_links, $this->paginate_data);
  725. if (preg_match("/".LD."if previous_page".RD."(.+?)".LD.SLASH."if".RD."/s", $this->paginate_data, $match))
  726. {
  727. if ($this->page_previous == '')
  728. {
  729. $this->paginate_data = preg_replace("/".LD."if previous_page".RD.".+?".LD.SLASH."if".RD."/s", '', $this->paginate_data);
  730. }
  731. else
  732. {
  733. $match['1'] = preg_replace("/".LD.'path.*?'.RD."/", $this->page_previous, $match['1']);
  734. $match['1'] = preg_replace("/".LD.'auto_path'.RD."/", $this->page_previous, $match['1']);
  735. $this->paginate_data = str_replace($match['0'], $match['1'], $this->paginate_data);
  736. }
  737. }
  738. if (preg_match("/".LD."if next_page".RD."(.+?)".LD.SLASH."if".RD."/s", $this->paginate_data, $match))
  739. {
  740. if ($this->page_next == '')
  741. {
  742. $this->paginate_data = preg_replace("/".LD."if next_page".RD.".+?".LD.SLASH."if".RD."/s", '', $this->paginate_data);
  743. }
  744. else
  745. {
  746. $match['1'] = preg_replace("/".LD.'path.*?'.RD."/", $this->page_next, $match['1']);
  747. $match['1'] = preg_replace("/".LD.'auto_path'.RD."/", $this->page_next, $match['1']);
  748. $this->paginate_data = str_replace($match['0'], $match['1'], $this->paginate_data);
  749. }
  750. }
  751. $position = ( ! $TMPL->fetch_param('paginate')) ? '' : $TMPL->fetch_param('paginate');
  752. switch ($position)
  753. {
  754. case "top" : $this->return_data = $this->paginate_data.$this->return_data;
  755. break;
  756. case "both" : $this->return_data = $this->paginate_data.$this->return_data.$this->paginate_data;
  757. break;
  758. default : $this->return_data .= $this->paginate_data;
  759. break;
  760. }
  761. }
  762. }
  763. /* END */
  764. /** ----------------------------------------
  765. /** Fetch custom weblog field IDs
  766. /** ----------------------------------------*/
  767. function fetch_custom_weblog_fields()
  768. {
  769. global $DB, $SESS, $TMPL;
  770. if (isset($SESS->cache['weblog']['custom_weblog_fields']) && isset($SESS->cache['weblog']['date_fields']) && isset($SESS->cache['weblog']['relationship_fields']))
  771. {
  772. $this->cfields = $SESS->cache['weblog']['custom_weblog_fields'];
  773. $this->dfields = $SESS->cache['weblog']['date_fields'];
  774. $this->rfields = $SESS->cache['weblog']['relationship_fields'];
  775. return;
  776. }
  777. // Gotta catch 'em all!
  778. $sql = "SELECT field_id, field_type, field_name, site_id
  779. FROM exp_weblog_fields";
  780. $query = $DB->query($sql);
  781. foreach ($query->result as $row)
  782. {
  783. // Assign date fields
  784. if ($row['field_type'] == 'date')
  785. {
  786. $this->dfields[$row['site_id']][$row['field_name']] = $row['field_id'];
  787. }
  788. // Assign relationship fields
  789. if ($row['field_type'] == 'rel')
  790. {
  791. $this->rfields[$row['site_id']][$row['field_name']] = $row['field_id'];
  792. }
  793. // Assign standard fields
  794. $this->cfields[$row['site_id']][$row['field_name']] = $row['field_id'];
  795. }
  796. $SESS->cache['weblog']['custom_weblog_fields'] = $this->cfields;
  797. $SESS->cache['weblog']['date_fields'] = $this->dfields;
  798. $SESS->cache['weblog']['relationship_fields'] = $this->rfields;
  799. }
  800. /* END */
  801. /** ----------------------------------------
  802. /** Fetch custom member field IDs
  803. /** ----------------------------------------*/
  804. function fetch_custom_member_fields()
  805. {
  806. global $DB;
  807. $query = $DB->query("SELECT m_field_id, m_field_name, m_field_fmt FROM exp_member_fields");
  808. foreach ($query->result as $row)
  809. {
  810. $this->mfields[$row['m_field_name']] = array($row['m_field_id'], $row['m_field_fmt']);
  811. }
  812. }
  813. /* END */
  814. /** ----------------------------------------
  815. /** Fetch categories
  816. /** ----------------------------------------*/
  817. function fetch_categories()
  818. {
  819. global $DB, $TMPL;
  820. if ($this->enable['category_fields'] === TRUE)
  821. {
  822. $query = $DB->query("SELECT field_id, field_name FROM exp_category_fields WHERE site_id IN ('".implode("','", $TMPL->site_ids)."')");
  823. if ($query->num_rows > 0)
  824. {
  825. foreach ($query->result as $row)
  826. {
  827. $this->catfields[] = array('field_name' => $row['field_name'], 'field_id' => $row['field_id']);
  828. }
  829. }
  830. $field_sqla = ", cg.field_html_formatting, fd.* ";
  831. $field_sqlb = " LEFT JOIN exp_category_field_data AS fd ON fd.cat_id = c.cat_id
  832. LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id";
  833. }
  834. else
  835. {
  836. $field_sqla = '';
  837. $field_sqlb = '';
  838. }
  839. $sql = "SELECT c.cat_name, c.cat_url_title, c.cat_id, c.cat_image, c.cat_description, c.parent_id,
  840. p.cat_id, p.entry_id, c.group_id {$field_sqla}
  841. FROM (exp_categories AS c, exp_category_posts AS p)
  842. {$field_sqlb}
  843. WHERE c.cat_id = p.cat_id
  844. AND p.entry_id IN (";
  845. $categories = array();
  846. foreach ($this->query->result as $row)
  847. {
  848. $sql .= "'".$row['entry_id']."',";
  849. $categories[] = $row['entry_id'];
  850. }
  851. $sql = substr($sql, 0, -1).')';
  852. $sql .= " ORDER BY c.group_id, c.parent_id, c.cat_order";
  853. $query = $DB->query($sql);
  854. if ($query->num_rows == 0)
  855. {
  856. return;
  857. }
  858. foreach ($categories as $val)
  859. {
  860. $this->temp_array = array();
  861. $this->cat_array = array();
  862. $parents = array();
  863. foreach ($query->result as $row)
  864. {
  865. if ($val == $row['entry_id'])
  866. {
  867. $this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['group_id'], $row['cat_url_title']);
  868. foreach ($row as $k => $v)
  869. {
  870. if (strpos($k, 'field') !== FALSE)
  871. {
  872. $this->temp_array[$row['cat_id']][$k] = $v;
  873. }
  874. }
  875. if ($row['parent_id'] > 0 && ! isset($this->temp_array[$row['parent_id']])) $parents[$row['parent_id']] = '';
  876. unset($parents[$row['cat_id']]);
  877. }
  878. }
  879. if (count($this->temp_array) == 0)
  880. {
  881. $temp = FALSE;
  882. }
  883. else
  884. {
  885. foreach($this->temp_array as $k => $v)
  886. {
  887. if (isset($parents[$v['1']])) $v['1'] = 0;
  888. if (0 == $v['1'])
  889. {
  890. $this->cat_array[] = $v;
  891. $this->process_subcategories($k);
  892. }
  893. }
  894. }
  895. $this->categories[$val] = $this->cat_array;
  896. }
  897. unset($this->temp_array);
  898. unset($this->cat_array);
  899. }
  900. /* END */
  901. /** ----------------------------------------
  902. /** Build SQL query
  903. /** ----------------------------------------*/
  904. function build_sql_query($qstring = '')
  905. {
  906. global $IN, $DB, $TMPL, $SESS, $LOC, $FNS, $REGX, $PREFS;
  907. $entry_id = '';
  908. $year = '';
  909. $month = '';
  910. $day = '';
  911. $qtitle = '';
  912. $cat_id = '';
  913. $corder = array();
  914. $offset = 0;
  915. $page_marker = FALSE;
  916. $dynamic = TRUE;
  917. $this->dynamic_sql = TRUE;
  918. /** ----------------------------------------------
  919. /** Is dynamic='off' set?
  920. /** ----------------------------------------------*/
  921. // If so, we'll override all dynamically set variables
  922. if ($TMPL->fetch_param('dynamic') == 'off')
  923. {
  924. $dynamic = FALSE;
  925. }
  926. /** ----------------------------------------------
  927. /** Do we allow dynamic POST variables to set parameters?
  928. /** ----------------------------------------------*/
  929. if ($TMPL->fetch_param('dynamic_parameters') !== FALSE AND isset($_POST) AND count($_POST) > 0)
  930. {
  931. foreach (explode('|', $TMPL->fetch_param('dynamic_parameters')) as $var)
  932. {
  933. if (isset($_POST[$var]) AND in_array($var, array('weblog', 'entry_id', 'category', 'orderby', 'sort', 'sticky', 'show_future_entries', 'show_expired', 'entry_id_from', 'entry_id_to', 'not_entry_id', 'start_on', 'stop_before', 'year', 'month', 'day', 'display_by', 'limit', 'username', 'status', 'group_id', 'cat_limit', 'month_limit', 'offset', 'author_id')))
  934. {
  935. $TMPL->tagparams[$var] = $_POST[$var];
  936. }
  937. if (isset($_POST[$var]) && strncmp($var, 'search:', 7) == 0)
  938. {
  939. $TMPL->search_fields[substr($var, 7)] = $_POST[$var];
  940. }
  941. }
  942. }
  943. /** ----------------------------------------------
  944. /** Parse the URL query string
  945. /** ----------------------------------------------*/
  946. $this->uristr = $IN->URI;
  947. if ($qstring == '')
  948. $qstring = $this->QSTR;
  949. $this->basepath = $FNS->create_url($this->uristr, 1);
  950. if ($qstring == '')
  951. {
  952. if ($TMPL->fetch_param('require_entry') == 'yes')
  953. {
  954. return '';
  955. }
  956. }
  957. else
  958. {
  959. /** --------------------------------------
  960. /** Do we have a pure ID number?
  961. /** --------------------------------------*/
  962. if (is_numeric($qstring) AND $dynamic)
  963. {
  964. $entry_id = $qstring;
  965. }
  966. else
  967. {
  968. /** --------------------------------------
  969. /** Parse day
  970. /** --------------------------------------*/
  971. if (preg_match("#\d{4}/\d{2}/(\d{2})#", $qstring, $match) AND $dynamic)
  972. {
  973. $partial = substr($match['0'], 0, -3);
  974. if (preg_match("#(\d{4}/\d{2})#", $partial, $pmatch))
  975. {
  976. $ex = explode('/', $pmatch['1']);
  977. $year = $ex['0'];
  978. $month = $ex['1'];
  979. }
  980. $day = $match['1'];
  981. $qstring = $REGX->trim_slashes(str_replace($match['0'], $partial, $qstring));
  982. }
  983. /** --------------------------------------
  984. /** Parse /year/month/
  985. /** --------------------------------------*/
  986. // added (^|\/) to make sure this doesn't trigger with url titles like big_party_2006
  987. if (preg_match("#(^|\/)(\d{4}/\d{2})(\/|$)#", $qstring, $match) AND $dynamic)
  988. {
  989. $ex = explode('/', $match['2']);
  990. $year = $ex['0'];
  991. $month = $ex['1'];
  992. $qstring = $REGX->trim_slashes(str_replace($match['2'], '', $qstring));
  993. // Removed this in order to allow archive pagination
  994. // $this->paginate = FALSE;
  995. }
  996. /** --------------------------------------
  997. /** Parse ID indicator
  998. /** --------------------------------------*/
  999. if (preg_match("#^(\d+)(.*)#", $qstring, $match) AND $dynamic)
  1000. {
  1001. $seg = ( ! isset($match['2'])) ? '' : $match['2'];
  1002. if (substr($seg, 0, 1) == "/" OR $seg == '')
  1003. {
  1004. $entry_id = $match['1'];
  1005. $qstring = $REGX->trim_slashes(preg_replace("#^".$match['1']."#", '', $qstring));
  1006. }
  1007. }
  1008. /** --------------------------------------
  1009. /** Parse page number
  1010. /** --------------------------------------*/
  1011. if (preg_match("#^P(\d+)|/P(\d+)#", $qstring, $match) AND ($dynamic OR $TMPL->fetch_param('paginate')))
  1012. {
  1013. $this->p_page = (isset($match['2'])) ? $match['2'] : $match['1'];
  1014. $this->basepath = $FNS->remove_double_slashes(str_replace($match['0'], '', $this->basepath));
  1015. $this->uristr = $FNS->remove_double_slashes(str_replace($match['0'], '', $this->uristr));
  1016. $qstring = $REGX->trim_slashes(str_replace($match['0'], '', $qstring));
  1017. $page_marker = TRUE;
  1018. }
  1019. /** --------------------------------------
  1020. /** Parse category indicator
  1021. /** --------------------------------------*/
  1022. // Text version of the category
  1023. if ($qstring != '' AND $this->reserved_cat_segment != '' AND in_array($this->reserved_cat_segment, explode("/", $qstring)) AND $dynamic AND $TMPL->fetch_param('weblog'))
  1024. {
  1025. $qstring = preg_replace("/(.*?)".preg_quote($this->reserved_cat_segment)."\//i", '', $qstring);
  1026. $sql = "SELECT DISTINCT cat_group FROM exp_weblogs WHERE site_id IN ('".implode("','", $TMPL->site_ids)."') AND ";
  1027. if (USER_BLOG !== FALSE)
  1028. {
  1029. $sql .= " weblog_id='".UB_BLOG_ID."'";
  1030. }
  1031. else
  1032. {
  1033. $xsql = $FNS->sql_andor_string($TMPL->fetch_param('weblog'), 'blog_name');
  1034. if (substr($xsql, 0, 3) == 'AND') $xsql = substr($xsql, 3);
  1035. $sql .= ' '.$xsql;
  1036. }
  1037. $query = $DB->query($sql);
  1038. if ($query->num_rows > 0)
  1039. {
  1040. $valid = 'y';
  1041. $last = explode('|', $query->row['cat_group']);
  1042. $valid_cats = array();
  1043. foreach($query->result as $row)
  1044. {
  1045. if ($TMPL->fetch_param('relaxed_categories') == 'yes')
  1046. {
  1047. $valid_cats = array_merge($valid_cats, explode('|', $row['cat_group']));
  1048. }
  1049. else
  1050. {
  1051. $valid_cats = array_intersect($last, explode('|', $row['cat_group']));
  1052. }
  1053. $valid_cats = array_unique($valid_cats);
  1054. if (sizeof($valid_cats) == 0)
  1055. {
  1056. $valid = 'n';
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. else
  1062. {
  1063. $valid = 'n';
  1064. }
  1065. if ($valid == 'y')
  1066. {
  1067. // the category URL title should be the first segment left at this point in $qstring,
  1068. // but because prior to this feature being added, category names were used in URLs,
  1069. // and '/' is a valid character for category names. If they have not updated their
  1070. // category url titles since updating to 1.6, their category URL title could still
  1071. // contain a '/'. So we'll try to get the category the correct way first, and if
  1072. // it fails, we'll try the whole $qstring
  1073. $cut_qstring = array_shift($temp = explode('/', $qstring));
  1074. $result = $DB->query("SELECT cat_id FROM exp_categories
  1075. WHERE cat_url_title='".$DB->escape_str($cut_qstring)."'
  1076. AND group_id IN ('".implode("','", $valid_cats)."')");
  1077. if ($result->num_rows == 1)
  1078. {
  1079. $qstring = str_replace($cut_qstring, 'C'.$result->row['cat_id'], $qstring);
  1080. }
  1081. else
  1082. {
  1083. // give it one more try using the whole $qstring
  1084. $result = $DB->query("SELECT cat_id FROM exp_categories
  1085. WHERE cat_url_title='".$DB->escape_str($qstring)."'
  1086. AND group_id IN ('".implode("','", $valid_cats)."')");
  1087. if ($result->num_rows == 1)
  1088. {
  1089. $qstring = 'C'.$result->row['cat_id'];
  1090. }
  1091. }
  1092. }
  1093. }
  1094. // Numeric version of the category
  1095. if (preg_match("#(^|\/)C(\d+)#", $qstring, $match) AND $dynamic)
  1096. {
  1097. $this->cat_request = TRUE;
  1098. $cat_id = $match['2'];
  1099. $qstring = $REGX->trim_slashes(str_replace($match['0'], '', $qstring));
  1100. }
  1101. /** --------------------------------------
  1102. /** Remove "N"
  1103. /** --------------------------------------*/
  1104. // The recent comments feature uses "N" as the URL indicator
  1105. // It needs to be removed if presenst
  1106. if (preg_match("#^N(\d+)|/N(\d+)#", $qstring, $match))
  1107. {
  1108. $this->uristr = $FNS->remove_double_slashes(str_replace($match['0'], '', $this->uristr));
  1109. $qstring = $REGX->trim_slashes(str_replace($match['0'], '', $qstring));
  1110. }
  1111. /** --------------------------------------
  1112. /** Parse URL title
  1113. /** --------------------------------------*/
  1114. if (($cat_id == '' AND $year == '') OR $TMPL->fetch_param('require_entry') == 'yes')
  1115. {
  1116. if (strstr($qstring, '/'))
  1117. {
  1118. $xe = explode('/', $qstring);
  1119. $qstring = current($xe);
  1120. }
  1121. if ($dynamic == TRUE)
  1122. {
  1123. $sql = "SELECT count(*) AS count
  1124. FROM exp_weblog_titles, exp_weblogs
  1125. WHERE exp_weblog_titles.weblog_id = exp_weblogs.weblog_id
  1126. AND exp_weblog_titles.url_title = '".$DB->escape_str($qstring)."'";
  1127. if (USER_BLOG !== FALSE)
  1128. {
  1129. $sql .= " AND exp_weblogs.weblog_id = '".UB_BLOG_ID."'";
  1130. }
  1131. else
  1132. {
  1133. $sql .= " AND exp_weblogs.is_user_blog = 'n' AND exp_weblogs.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  1134. }
  1135. $query = $DB->query($sql);
  1136. if ($query->row['count'] == 0)
  1137. {
  1138. if ($TMPL->fetch_param('require_entry') == 'yes')
  1139. {
  1140. return '';
  1141. }
  1142. $qtitle = '';
  1143. }
  1144. else
  1145. {
  1146. $qtitle = $qstring;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. /** ----------------------------------------------
  1153. /** Entry ID number
  1154. /** ----------------------------------------------*/
  1155. // If the "entry ID" was hard-coded, use it instead of
  1156. // using the dynamically set one above
  1157. if ($TMPL->fetch_param('entry_id'))
  1158. {
  1159. $entry_id = $TMPL->fetch_param('entry_id');
  1160. }
  1161. /** ----------------------------------------------
  1162. /** Only Entries with Pages
  1163. /** ----------------------------------------------*/
  1164. if ($TMPL->fetch_param('show_pages') !== FALSE && in_array($TMPL->fetch_param('show_pages'), array('only', 'no')) && ($pages = $PREFS->ini('site_pages')) !== FALSE)
  1165. {
  1166. // consider entry_id
  1167. if ($TMPL->fetch_param('entry_id') !== FALSE)
  1168. {
  1169. $not = FALSE;
  1170. if (strncmp($entry_id, 'not', 3) == 0)
  1171. {
  1172. $not = TRUE;
  1173. $entry_id = trim(substr($entry_id, 3));
  1174. }
  1175. $ids = explode('|', $entry_id);
  1176. if ($TMPL->fetch_param('show_pages') == 'only')
  1177. {
  1178. if ($not === TRUE)
  1179. {
  1180. $entry_id = implode('|', array_diff(array_flip($pages['uris']), explode('|', $ids)));
  1181. }
  1182. else
  1183. {
  1184. $entry_id = implode('|',array_diff($ids, array_diff($ids, array_flip($pages['uris']))));
  1185. }
  1186. }
  1187. else
  1188. {
  1189. if ($not === TRUE)
  1190. {
  1191. $entry_id = "not {$entry_id}|".implode('|', array_flip($pages['uris']));
  1192. }
  1193. else
  1194. {
  1195. $entry_id = implode('|',array_diff($ids, array_flip($pages['uris'])));
  1196. }
  1197. }
  1198. echo $entry_id;
  1199. }
  1200. else
  1201. {
  1202. $entry_id = (($TMPL->fetch_param('show_pages') == 'no') ? 'not ' : '').implode('|', array_flip($pages['uris']));
  1203. }
  1204. }
  1205. /** ----------------------------------------------
  1206. /** Assing the order variables
  1207. /** ----------------------------------------------*/
  1208. $order = $TMPL->fetch_param('orderby');
  1209. $sort = $TMPL->fetch_param('sort');
  1210. $sticky = $TMPL->fetch_param('sticky');
  1211. /** -------------------------------------
  1212. /** Multiple Orders and Sorts...
  1213. /** -------------------------------------*/
  1214. if ($order !== FALSE && stristr($order, '|'))
  1215. {
  1216. $order_array = explode('|', $order);
  1217. if ($order_array['0'] == 'random')
  1218. {
  1219. $order_array = array('random');
  1220. }
  1221. }
  1222. else
  1223. {
  1224. $order_array = array($order);
  1225. }
  1226. if ($sort !== FALSE && stristr($sort, '|'))
  1227. {
  1228. $sort_array = explode('|', $sort);
  1229. }
  1230. else
  1231. {
  1232. $sort_array = array($sort);
  1233. }
  1234. /** -------------------------------------
  1235. /** Validate Results for Later Processing
  1236. /** -------------------------------------*/
  1237. $base_orders = array('random', 'entry_id', 'date', 'title', 'url_title', 'edit_date', 'comment_total', 'username', 'screen_name', 'most_recent_comment', 'expiration_date',
  1238. 'view_count_one', 'view_count_two', 'view_count_three', 'view_count_four');
  1239. foreach($order_array as $key => $order)
  1240. {
  1241. if ( ! in_array($order, $base_orders))
  1242. {
  1243. if (FALSE !== $order)
  1244. {
  1245. $set = 'n';
  1246. /** -------------------------------------
  1247. /** Site Namespace is Being Used, Parse Out
  1248. /** -------------------------------------*/
  1249. if (strpos($order, ':') !== FALSE)
  1250. {
  1251. $order_parts = explode(':', $order, 2);
  1252. if (isset($TMPL->site_ids[$order_parts[0]]) && isset($this->cfields[$TMPL->site_ids[$order_parts[0]]][$order_parts[1]]))
  1253. {
  1254. $corder[$key] = $this->cfields[$TMPL->site_ids[$order_parts[0]]][$order_parts[1]];
  1255. $order_array[$key] = 'custom_field';
  1256. $set = 'y';
  1257. }
  1258. }
  1259. /** -------------------------------------
  1260. /** Find the Custom Field, Cycle Through All Sites for Tag
  1261. /** - If multiple sites have the same short_name for a field, we do a CONCAT ORDERBY in query
  1262. /** -------------------------------------*/
  1263. if ($set == 'n')
  1264. {
  1265. foreach($this->cfields as $site_id => $cfields)
  1266. {
  1267. // Only those sites specified
  1268. if ( ! in_array($site_id, $TMPL->site_ids))
  1269. {
  1270. continue;
  1271. }
  1272. if (isset($cfields[$order]))
  1273. {
  1274. if ($set == 'y')
  1275. {
  1276. $corder[$key] .= '|'.$cfields[$order];
  1277. }
  1278. else
  1279. {
  1280. $corder[$key] = $cfields[$order];
  1281. $order_array[$key] = 'custom_field';
  1282. $set = 'y';
  1283. }
  1284. }
  1285. }
  1286. }
  1287. if ($set == 'n')
  1288. {
  1289. $order_array[$key] = FALSE;
  1290. }
  1291. }
  1292. }
  1293. if ( ! isset($sort_array[$key]))
  1294. {
  1295. $sort_array[$key] = 'desc';
  1296. }
  1297. }
  1298. foreach($sort_array as $key => $sort)
  1299. {
  1300. if ($sort == FALSE || ($sort != 'asc' AND $sort != 'desc'))
  1301. {
  1302. $sort_array[$key] = "desc";
  1303. }
  1304. }
  1305. // fixed entry id ordering
  1306. if (($fixed_order = $TMPL->fetch_param('fixed_order')) === FALSE OR preg_match('/[^0-9\|]/', $fixed_order))
  1307. {
  1308. $fixed_order = FALSE;
  1309. }
  1310. else
  1311. {
  1312. // MySQL will not order the entries correctly unless the results are constrained
  1313. // to matching rows only, so we force the entry_id as well
  1314. $entry_id = $fixed_order;
  1315. $fixed_order = preg_split('/\|/', $fixed_order, -1, PREG_SPLIT_NO_EMPTY);
  1316. // some peeps might want to be able to 'flip' it
  1317. // the default sort order is 'desc' but in this context 'desc' has a stronger "reversing"
  1318. // connotation, so we look not at the sort array, but the tag parameter itself, to see the user's intent
  1319. if ($sort == 'desc')
  1320. {
  1321. $fixed_order = array_reverse($fixed_order);
  1322. }
  1323. }
  1324. /** ----------------------------------------------
  1325. /** Build the master SQL query
  1326. /** ----------------------------------------------*/
  1327. $sql_a = "SELECT ";
  1328. $sql_b = ($TMPL->fetch_param('category') || $TMPL->fetch_param('category_group') || $cat_id != '' || $order_array['0'] == 'random') ? "DISTINCT(t.entry_id) " : "t.entry_id ";
  1329. if ($this->field_pagination == TRUE)
  1330. {
  1331. $sql_b .= ",wd.* ";
  1332. }
  1333. $sql_c = "COUNT(t.entry_id) AS count ";
  1334. $sql = "FROM exp_weblog_titles AS t
  1335. LEFT JOIN exp_weblogs ON t.weblog_id = exp_weblogs.weblog_id ";
  1336. if ($this->field_pagination == TRUE)
  1337. {
  1338. $sql .= "LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id ";
  1339. }
  1340. elseif (in_array('custom_field', $order_array))
  1341. {
  1342. $sql .= "LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id ";
  1343. }
  1344. $sql .= "LEFT JOIN exp_members AS m ON m.member_id = t.author_id ";
  1345. if ($TMPL->fetch_param('category') || $TMPL->fetch_param('category_group') || $cat_id != '')
  1346. {
  1347. /* --------------------------------
  1348. /* We use LEFT JOIN when there is a 'not' so that we get
  1349. /* entries that are not assigned to a category.
  1350. /* --------------------------------*/
  1351. if ((substr($TMPL->fetch_param('category_group'), 0, 3) == 'not' OR substr($TMPL->fetch_param('category'), 0, 3) == 'not') && $TMPL->fetch_param('uncategorized_entries') !== 'n')
  1352. {
  1353. $sql .= "LEFT JOIN exp_category_posts ON t.entry_id = exp_category_posts.entry_id
  1354. LEFT JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id ";
  1355. }
  1356. else
  1357. {
  1358. $sql .= "INNER JOIN exp_category_posts ON t.entry_id = exp_category_posts.entry_id
  1359. INNER JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id ";
  1360. }
  1361. }
  1362. // join data table if we're searching fields
  1363. if (! empty($TMPL->search_fields) && strpos($sql, 'exp_weblog_data AS wd') === FALSE)
  1364. {
  1365. $sql .= "LEFT JOIN exp_weblog_data AS wd ON wd.entry_id = t.entry_id ";
  1366. }
  1367. $sql .= "WHERE t.entry_id !='' AND t.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  1368. /** ----------------------------------------------
  1369. /** We only select entries that have not expired
  1370. /** ----------------------------------------------*/
  1371. $timestamp = ($TMPL->cache_timestamp != '') ? $LOC->set_gmt($TMPL->cache_timestamp) : $LOC->now;
  1372. if ($TMPL->fetch_param('show_future_entries') != 'yes')
  1373. {
  1374. $sql .= " AND t.entry_date < ".$timestamp." ";
  1375. }
  1376. if ($TMPL->fetch_param('show_expired') != 'yes')
  1377. {
  1378. $sql .= " AND (t.expiration_date = 0 OR t.expiration_date > ".$timestamp.") ";
  1379. }
  1380. /** ----------------------------------------------
  1381. /** Limit query by post ID for individual entries
  1382. /** ----------------------------------------------*/
  1383. if ($entry_id != '')
  1384. {
  1385. $sql .= $FNS->sql_andor_string($entry_id, 't.entry_id').' ';
  1386. }
  1387. /** ----------------------------------------------
  1388. /** Limit query by post url_title for individual entries
  1389. /** ----------------------------------------------*/
  1390. if ($url_title = $TMPL->fetch_param('url_title'))
  1391. {
  1392. $sql .= $FNS->sql_andor_string($url_title, 't.url_title').' ';
  1393. }
  1394. /** ----------------------------------------------
  1395. /** Limit query by entry_id range
  1396. /** ----------------------------------------------*/
  1397. if ($entry_id_from = $TMPL->fetch_param('entry_id_from'))
  1398. {
  1399. $sql .= "AND t.entry_id >= '$entry_id_from' ";
  1400. }
  1401. if ($entry_id_to = $TMPL->fetch_param('entry_id_to'))
  1402. {
  1403. $sql .= "AND t.entry_id <= '$entry_id_to' ";
  1404. }
  1405. /** ----------------------------------------------
  1406. /** Exclude an individual entry
  1407. /** ----------------------------------------------*/
  1408. if ($not_entry_id = $TMPL->fetch_param('not_entry_id'))
  1409. {
  1410. $sql .= ( ! is_numeric($not_entry_id))
  1411. ? "AND t.url_title != '{$not_entry_id}' "
  1412. : "AND t.entry_id != '{$not_entry_id}' ";
  1413. }
  1414. /** ----------------------------------------------
  1415. /** Limit to/exclude specific weblogs
  1416. /** ----------------------------------------------*/
  1417. if (USER_BLOG !== FALSE)
  1418. {
  1419. // If it's a "user blog" we limit to only their assigned blog
  1420. $sql .= "AND exp_weblogs.weblog_id = '".UB_BLOG_ID."' ";
  1421. }
  1422. else
  1423. {
  1424. $sql .= "AND exp_weblogs.is_user_blog = 'n' ";
  1425. if ($weblog = $TMPL->fetch_param('weblog'))
  1426. {
  1427. $xql = "SELECT weblog_id FROM exp_weblogs WHERE ";
  1428. $str = $FNS->sql_andor_string($weblog, 'blog_name');
  1429. if (substr($str, 0, 3) == 'AND')
  1430. $str = substr($str, 3);
  1431. $xql .= $str;
  1432. $query = $DB->query($xql);
  1433. if ($query->num_rows == 0)
  1434. {
  1435. return '';
  1436. }
  1437. else
  1438. {
  1439. if ($query->num_rows == 1)
  1440. {
  1441. $sql .= "AND t.weblog_id = '".$query->row['weblog_id']."' ";
  1442. }
  1443. else
  1444. {
  1445. $sql .= "AND (";
  1446. foreach ($query->result as $row)
  1447. {
  1448. $sql .= "t.weblog_id = '".$row['weblog_id']."' OR ";
  1449. }
  1450. $sql = substr($sql, 0, - 3);
  1451. $sql .= ") ";
  1452. }
  1453. }
  1454. }
  1455. }
  1456. /** ----------------------------------------------------
  1457. /** Limit query by date range given in tag parameters
  1458. /** ----------------------------------------------------*/
  1459. if ($TMPL->fetch_param('start_on'))
  1460. $sql .= "AND t.entry_date >= '".$LOC->convert_human_date_to_gmt($TMPL->fetch_param('start_on'))."' ";
  1461. if ($TMPL->fetch_param('stop_before'))
  1462. $sql .= "AND t.entry_date < '".$LOC->convert_human_date_to_gmt($TMPL->fetch_param('stop_before'))."' ";
  1463. /** -----------------------------------------------------
  1464. /** Limit query by date contained in tag parameters
  1465. /** -----------------------------------------------------*/
  1466. if ($TMPL->fetch_param('year') || $TMPL->fetch_param('month') || $TMPL->fetch_param('day'))
  1467. {
  1468. $year = ( ! is_numeric($TMPL->fetch_param('year'))) ? date('Y') : $TMPL->fetch_param('year');
  1469. $smonth = ( ! is_numeric($TMPL->fetch_param('month'))) ? '01' : $TMPL->fetch_param('month');
  1470. $emonth = ( ! is_numeric($TMPL->fetch_param('month'))) ? '12': $TMPL->fetch_param('month');
  1471. $day = ( ! is_numeric($TMPL->fetch_param('day'))) ? '' : $TMPL->fetch_param('day');
  1472. if ($day != '' && ! is_numeric($TMPL->fetch_param('month')))
  1473. {
  1474. $smonth = date('m');
  1475. $emonth = date('m');
  1476. }
  1477. if (strlen($smonth) == 1) $smonth = '0'.$smonth;
  1478. if (strlen($emonth) == 1) $emonth = '0'.$emonth;
  1479. if ($day == '')
  1480. {
  1481. $sday = 1;
  1482. $eday = $LOC->fetch_days_in_month($emonth, $year);
  1483. }
  1484. else
  1485. {
  1486. $sday = $day;
  1487. $eday = $day;
  1488. }
  1489. $stime = $LOC->set_gmt(mktime(0, 0, 0, $smonth, $sday, $year));
  1490. $etime = $LOC->set_gmt(mktime(23, 59, 59, $emonth, $eday, $year));
  1491. $sql .= " AND t.entry_date >= ".$stime." AND t.entry_date <= ".$etime." ";
  1492. }
  1493. else
  1494. {
  1495. /** ------------------------------------------------
  1496. /** Limit query by date in URI: /2003/12/14/
  1497. /** -------------------------------------------------*/
  1498. if ($year != '' AND $month != '' AND $dynamic == TRUE)
  1499. {
  1500. if ($day == '')
  1501. {
  1502. $sday = 1;
  1503. $eday = $LOC->fetch_days_in_month($month, $year);
  1504. }
  1505. else
  1506. {
  1507. $sday = $day;
  1508. $eday = $day;
  1509. }
  1510. $stime = $LOC->set_gmt(mktime(0, 0, 0, $month, $sday, $year));
  1511. $etime = $LOC->set_gmt(mktime(23, 59, 59, $month, $eday, $year));
  1512. if (date("I", $LOC->now) AND ! date("I", $stime))
  1513. {
  1514. $stime -= 3600;
  1515. }
  1516. elseif ( ! date("I", $LOC->now) AND date("I", $stime))
  1517. {
  1518. $stime += 3600;
  1519. }
  1520. $stime += $LOC->set_localized_offset();
  1521. if (date("I", $LOC->now) AND ! date("I", $etime))
  1522. {
  1523. $etime -= 3600;
  1524. }
  1525. elseif ( ! date("I", $LOC->now) AND date("I", $etime))
  1526. {
  1527. $etime += 3600;
  1528. }
  1529. $etime += $LOC->set_localized_offset();
  1530. $sql .= " AND t.entry_date >= ".$stime." AND t.entry_date <= ".$etime." ";
  1531. }
  1532. else
  1533. {
  1534. $this->display_by = $TMPL->fetch_param('display_by');
  1535. $lim = ( ! is_numeric($TMPL->fetch_param('limit'))) ? '1' : $TMPL->fetch_param('limit');
  1536. /** -------------------------------------------
  1537. /** If display_by = "month"
  1538. /** -------------------------------------------*/
  1539. if ($this->display_by == 'month')
  1540. {
  1541. // We need to run a query and fetch the distinct months in which there are entries
  1542. $dql = "SELECT t.year, t.month ".$sql;
  1543. /** ----------------------------------------------
  1544. /** Add status declaration
  1545. /** ----------------------------------------------*/
  1546. if ($status = $TMPL->fetch_param('status'))
  1547. {
  1548. $status = str_replace('Open', 'open', $status);
  1549. $status = str_replace('Closed', 'closed', $status);
  1550. $sstr = $FNS->sql_andor_string($status, 't.status');
  1551. if ( ! preg_match("#\'closed\'#i", $sstr))
  1552. {
  1553. $sstr .= " AND t.status != 'closed' ";
  1554. }
  1555. $dql .= $sstr;
  1556. }
  1557. else
  1558. {
  1559. $dql .= "AND t.status = 'open' ";
  1560. }
  1561. /** --------------------
  1562. /** Add Category Limit
  1563. /** --------------------*/
  1564. if ($cat_id != '')
  1565. {
  1566. $dql .= "AND exp_category_posts.cat_id = '".$DB->escape_str($cat_id)."' ";
  1567. }
  1568. $query = $DB->query($dql);
  1569. $distinct = array();
  1570. if ($query->num_rows > 0)
  1571. {
  1572. foreach ($query->result as $row)
  1573. {
  1574. $distinct[] = $row['year'].$row['month'];
  1575. }
  1576. $distinct = array_unique($distinct);
  1577. sort($distinct);
  1578. if ($sort_array['0'] == 'desc')
  1579. {
  1580. $distinct = array_reverse($distinct);
  1581. }
  1582. $this->total_rows = count($distinct);
  1583. $cur = ($this->p_page == '') ? 0 : $this->p_page;
  1584. $distinct = array_slice($distinct, $cur, $lim);
  1585. if ($distinct != FALSE)
  1586. {
  1587. $sql .= "AND (";
  1588. foreach ($distinct as $val)
  1589. {
  1590. $sql .= "(t.year = '".substr($val, 0, 4)."' AND t.month = '".substr($val, 4, 2)."') OR";
  1591. }
  1592. $sql = substr($sql, 0, -2).')';
  1593. }
  1594. }
  1595. }
  1596. /** -------------------------------------------
  1597. /** If display_by = "day"
  1598. /** -------------------------------------------*/
  1599. elseif ($this->display_by == 'day')
  1600. {
  1601. // We need to run a query and fetch the distinct days in which there are entries
  1602. $dql = "SELECT t.year, t.month, t.day ".$sql;
  1603. /** ----------------------------------------------
  1604. /** Add status declaration
  1605. /** ----------------------------------------------*/
  1606. if ($status = $TMPL->fetch_param('status'))
  1607. {
  1608. $status = str_replace('Open', 'open', $status);
  1609. $status = str_replace('Closed', 'closed', $status);
  1610. $sstr = $FNS->sql_andor_string($status, 't.status');
  1611. if ( ! preg_match("#\'closed\'#i", $sstr))
  1612. {
  1613. $sstr .= " AND t.status != 'closed' ";
  1614. }
  1615. $dql .= $sstr;
  1616. }
  1617. else
  1618. {
  1619. $dql .= "AND t.status = 'open' ";
  1620. }
  1621. /** --------------------
  1622. /** Add Category Limit
  1623. /** --------------------*/
  1624. if ($cat_id != '')
  1625. {
  1626. $dql .= "AND exp_category_posts.cat_id = '".$DB->escape_str($cat_id)."' ";
  1627. }
  1628. $query = $DB->query($dql);
  1629. $distinct = array();
  1630. if ($query->num_rows > 0)
  1631. {
  1632. foreach ($query->result as $row)
  1633. {
  1634. $distinct[] = $row['year'].$row['month'].$row['day'];
  1635. }
  1636. $distinct = array_unique($distinct);
  1637. sort($distinct);
  1638. if ($sort_array['0'] == 'desc')
  1639. {
  1640. $distinct = array_reverse($distinct);
  1641. }
  1642. $this->total_rows = count($distinct);
  1643. $cur = ($this->p_page == '') ? 0 : $this->p_page;
  1644. $distinct = array_slice($distinct, $cur, $lim);
  1645. if ($distinct != FALSE)
  1646. {
  1647. $sql .= "AND (";
  1648. foreach ($distinct as $val)
  1649. {
  1650. $sql .= "(t.year = '".substr($val, 0, 4)."' AND t.month = '".substr($val, 4, 2)."' AND t.day = '".substr($val, 6)."' ) OR";
  1651. }
  1652. $sql = substr($sql, 0, -2).')';
  1653. }
  1654. }
  1655. }
  1656. /** -------------------------------------------
  1657. /** If display_by = "week"
  1658. /** -------------------------------------------*/
  1659. elseif ($this->display_by == 'week')
  1660. {
  1661. /** ---------------------------------
  1662. /* Run a Query to get a combined Year and Week value. There is a downside
  1663. /* to this approach and that is the lack of localization and use of DST for
  1664. /* dates. Unfortunately, without making a complex and ultimately fubar'ed
  1665. /* PHP script this is the best approach possible.
  1666. /* ---------------------------------*/
  1667. $loc_offset = $LOC->set_localized_offset();
  1668. if ($TMPL->fetch_param('start_day') === 'Monday')
  1669. {
  1670. $yearweek = "DATE_FORMAT(FROM_UNIXTIME(entry_date + {$loc_offset}), '%x%v') AS yearweek ";
  1671. $dql = 'SELECT '.$yearweek.$sql;
  1672. }
  1673. else
  1674. {
  1675. $yearweek = "DATE_FORMAT(FROM_UNIXTIME(entry_date + {$loc_offset}), '%X%V') AS yearweek ";
  1676. $dql = 'SELECT '.$yearweek.$sql;
  1677. }
  1678. /** ----------------------------------------------
  1679. /** Add status declaration
  1680. /** ----------------------------------------------*/
  1681. if ($status = $TMPL->fetch_param('status'))
  1682. {
  1683. $status = str_replace('Open', 'open', $status);
  1684. $status = str_replace('Closed', 'closed', $status);
  1685. $sstr = $FNS->sql_andor_string($status, 't.status');
  1686. if ( ! preg_match("#\'closed\'#i", $sstr))
  1687. {
  1688. $sstr .= " AND t.status != 'closed' ";
  1689. }
  1690. $dql .= $sstr;
  1691. }
  1692. else
  1693. {
  1694. $dql .= "AND t.status = 'open' ";
  1695. }
  1696. /** --------------------
  1697. /** Add Category Limit
  1698. /** --------------------*/
  1699. if ($cat_id != '')
  1700. {
  1701. $dql .= "AND exp_category_posts.cat_id = '".$DB->escape_str($cat_id)."' ";
  1702. }
  1703. $query = $DB->query($dql);
  1704. $distinct = array();
  1705. if ($query->num_rows > 0)
  1706. {
  1707. /** ---------------------------------
  1708. /* Sort Default is ASC for Display By Week so that entries are displayed
  1709. /* oldest to newest in the week, which is how you would expect.
  1710. /* ---------------------------------*/
  1711. if ($TMPL->fetch_param('sort') === FALSE)
  1712. {
  1713. $sort_array['0'] = 'asc';
  1714. }
  1715. foreach ($query->result as $row)
  1716. {
  1717. $distinct[] = $row['yearweek'];
  1718. }
  1719. $distinct = array_unique($distinct);
  1720. rsort($distinct);
  1721. /* Old code, didn't really do anything....
  1722. *
  1723. if ($TMPL->fetch_param('week_sort') == 'desc')
  1724. {
  1725. $distinct = array_reverse($distinct);
  1726. }
  1727. *
  1728. */
  1729. $this->total_rows = count($distinct);
  1730. $cur = ($this->p_page == '') ? 0 : $this->p_page;
  1731. /** ---------------------------------
  1732. /* If no pagination, then the Current Week is shown by default with
  1733. /* all pagination correctly set and ready to roll, if used.
  1734. /* ---------------------------------*/
  1735. if ($TMPL->fetch_param('show_current_week') === 'yes' && $this->p_page == '')
  1736. {
  1737. if ($TMPL->fetch_param('start_day') === 'Monday')
  1738. {
  1739. $query = $DB->query("SELECT DATE_FORMAT(CURDATE(), '%x%v') AS thisWeek");
  1740. }
  1741. else
  1742. {
  1743. $query = $DB->query("SELECT DATE_FORMAT(CURDATE(), '%X%V') AS thisWeek");
  1744. }
  1745. foreach($distinct as $key => $week)
  1746. {
  1747. if ($week == $query->row['thisWeek'])
  1748. {
  1749. $cur = $key;
  1750. $this->p_page = $key;
  1751. break;
  1752. }
  1753. }
  1754. }
  1755. $distinct = array_slice($distinct, $cur, $lim);
  1756. /** ---------------------------------
  1757. /* Finally, we add the display by week SQL to the query
  1758. /* ---------------------------------*/
  1759. if ($distinct != FALSE)
  1760. {
  1761. // A Rough Attempt to Get the Localized Offset Added On
  1762. $offset = $LOC->set_localized_offset();
  1763. $dst_on = (date("I", $LOC->now) == 1) ? TRUE : FALSE;
  1764. $sql .= "AND (";
  1765. foreach ($distinct as $val)
  1766. {
  1767. if ($dst_on === TRUE AND (substr($val, 4) < 13 OR substr($val, 4) >= 43))
  1768. {
  1769. $sql_offset = "- 3600";
  1770. }
  1771. elseif ($dst_on === FALSE AND (substr($val, 4) >= 13 AND substr($val, 4) < 43))
  1772. {
  1773. $sql_offset = "+ 3600";
  1774. }
  1775. else
  1776. {
  1777. $sql_offset = '';
  1778. }
  1779. if ($TMPL->fetch_param('start_day') === 'Monday')
  1780. {
  1781. $sql .= " DATE_FORMAT(FROM_UNIXTIME(entry_date {$sql_offset}), '%x%v') = '".$val."' OR";
  1782. }
  1783. else
  1784. {
  1785. $sql .= " DATE_FORMAT(FROM_UNIXTIME(entry_date {$sql_offset}), '%X%V') = '".$val."' OR";
  1786. }
  1787. }
  1788. $sql = substr($sql, 0, -2).')';
  1789. }
  1790. }
  1791. }
  1792. }
  1793. }
  1794. /** ----------------------------------------------
  1795. /** Limit query "URL title"
  1796. /** ----------------------------------------------*/
  1797. if ($qtitle != '' AND $dynamic)
  1798. {
  1799. $sql .= "AND t.url_title = '".$DB->escape_str($qtitle)."' ";
  1800. // We use this with hit tracking....
  1801. $this->hit_tracking_id = $qtitle;
  1802. }
  1803. // We set a global variable which we use with entry hit tracking
  1804. if ($entry_id != '' AND $this->entry_id !== FALSE)
  1805. {
  1806. $this->hit_tracking_id = $entry_id;
  1807. }
  1808. /** ----------------------------------------------
  1809. /** Limit query by category
  1810. /** ----------------------------------------------*/
  1811. if ($TMPL->fetch_param('category'))
  1812. {
  1813. if (stristr($TMPL->fetch_param('category'), '&'))
  1814. {
  1815. /** --------------------------------------
  1816. /** First, we find all entries with these categories
  1817. /** --------------------------------------*/
  1818. $for_sql = (substr($TMPL->fetch_param('category'), 0, 3) == 'not') ? trim(substr($TMPL->fetch_param('category'), 3)) : $TMPL->fetch_param('category');
  1819. $csql = "SELECT exp_category_posts.entry_id, exp_category_posts.cat_id ".
  1820. $sql.
  1821. $FNS->sql_andor_string(str_replace('&', '|', $for_sql), 'exp_categories.cat_id');
  1822. //exit($csql);
  1823. $results = $DB->query($csql);
  1824. if ($results->num_rows == 0)
  1825. {
  1826. return;
  1827. }
  1828. $type = 'IN';
  1829. $categories = explode('&', $TMPL->fetch_param('category'));
  1830. $entry_array = array();
  1831. if (substr($categories['0'], 0, 3) == 'not')
  1832. {
  1833. $type = 'NOT IN';
  1834. $categories['0'] = trim(substr($categories['0'], 3));
  1835. }
  1836. foreach($results->result as $row)
  1837. {
  1838. $entry_array[$row['cat_id']][] = $row['entry_id'];
  1839. }
  1840. if (sizeof($entry_array) < 2 OR sizeof(array_diff($categories, array_keys($entry_array))) > 0)
  1841. {
  1842. return;
  1843. }
  1844. $chosen = call_user_func_array('array_intersect', $entry_array);
  1845. if (sizeof($chosen) == 0)
  1846. {
  1847. return;
  1848. }
  1849. $sql .= "AND t.entry_id ".$type." ('".implode("','", $chosen)."') ";
  1850. }
  1851. else
  1852. {
  1853. if (substr($TMPL->fetch_param('category'), 0, 3) == 'not' && $TMPL->fetch_param('uncategorized_entries') !== 'n')
  1854. {
  1855. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category'), 'exp_categories.cat_id', '', TRUE)." ";
  1856. }
  1857. else
  1858. {
  1859. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category'), 'exp_categories.cat_id')." ";
  1860. }
  1861. }
  1862. }
  1863. if ($TMPL->fetch_param('category_group'))
  1864. {
  1865. if (substr($TMPL->fetch_param('category_group'), 0, 3) == 'not' && $TMPL->fetch_param('uncategorized_entries') !== 'n')
  1866. {
  1867. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category_group'), 'exp_categories.group_id', '', TRUE)." ";
  1868. }
  1869. else
  1870. {
  1871. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category_group'), 'exp_categories.group_id')." ";
  1872. }
  1873. }
  1874. if ($TMPL->fetch_param('category') === FALSE && $TMPL->fetch_param('category_group') === FALSE)
  1875. {
  1876. if ($cat_id != '' AND $dynamic)
  1877. {
  1878. $sql .= " AND exp_categories.cat_id = '".$DB->escape_str($cat_id)."' ";
  1879. }
  1880. }
  1881. /** ----------------------------------------------
  1882. /** Limit to (or exclude) specific users
  1883. /** ----------------------------------------------*/
  1884. if ($username = $TMPL->fetch_param('username'))
  1885. {
  1886. // Shows entries ONLY for currently logged in user
  1887. if ($username == 'CURRENT_USER')
  1888. {
  1889. $sql .= "AND m.member_id = '".$SESS->userdata('member_id')."' ";
  1890. }
  1891. elseif ($username == 'NOT_CURRENT_USER')
  1892. {
  1893. $sql .= "AND m.member_id != '".$SESS->userdata('member_id')."' ";
  1894. }
  1895. else
  1896. {
  1897. $sql .= $FNS->sql_andor_string($username, 'm.username');
  1898. }
  1899. }
  1900. /** ----------------------------------------------
  1901. /** Limit to (or exclude) specific author id(s)
  1902. /** ----------------------------------------------*/
  1903. if ($author_id = $TMPL->fetch_param('author_id'))
  1904. {
  1905. // Shows entries ONLY for currently logged in user
  1906. if ($author_id == 'CURRENT_USER')
  1907. {
  1908. $sql .= "AND m.member_id = '".$SESS->userdata('member_id')."' ";
  1909. }
  1910. elseif ($author_id == 'NOT_CURRENT_USER')
  1911. {
  1912. $sql .= "AND m.member_id != '".$SESS->userdata('member_id')."' ";
  1913. }
  1914. else
  1915. {
  1916. $sql .= $FNS->sql_andor_string($author_id, 'm.member_id');
  1917. }
  1918. }
  1919. /** ----------------------------------------------
  1920. /** Add status declaration
  1921. /** ----------------------------------------------*/
  1922. if ($status = $TMPL->fetch_param('status'))
  1923. {
  1924. $status = str_replace('Open', 'open', $status);
  1925. $status = str_replace('Closed', 'closed', $status);
  1926. $sstr = $FNS->sql_andor_string($status, 't.status');
  1927. if ( ! preg_match("#\'closed\'#i", $sstr))
  1928. {
  1929. $sstr .= " AND t.status != 'closed' ";
  1930. }
  1931. $sql .= $sstr;
  1932. }
  1933. else
  1934. {
  1935. $sql .= "AND t.status = 'open' ";
  1936. }
  1937. /** ----------------------------------------------
  1938. /** Add Group ID clause
  1939. /** ----------------------------------------------*/
  1940. if ($group_id = $TMPL->fetch_param('group_id'))
  1941. {
  1942. $sql .= $FNS->sql_andor_string($group_id, 'm.group_id');
  1943. }
  1944. /** ---------------------------------------
  1945. /** Field searching
  1946. /** ---------------------------------------*/
  1947. if (! empty($TMPL->search_fields))
  1948. {
  1949. foreach ($TMPL->search_fields as $field_name => $terms)
  1950. {
  1951. if (isset($this->cfields[$PREFS->ini('site_id')][$field_name]))
  1952. {
  1953. if (strncmp($terms, '=', 1) == 0)
  1954. {
  1955. /** ---------------------------------------
  1956. /** Exact Match e.g.: search:body="=pickle"
  1957. /** ---------------------------------------*/
  1958. $terms = substr($terms, 1);
  1959. // special handling for IS_EMPTY
  1960. if (strpos($terms, 'IS_EMPTY') !== FALSE)
  1961. {
  1962. $terms = str_replace('IS_EMPTY', '', $terms);
  1963. $add_search = $FNS->sql_andor_string($terms, 'wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name]);
  1964. // remove the first AND output by $FNS->sql_andor_string() so we can parenthesize this clause
  1965. $add_search = substr($add_search, 3);
  1966. $conj = ($add_search == '') ? '' : ((strncmp($terms, 'not ', 4) != 0) ? 'OR' : 'AND');
  1967. if (strncmp($terms, 'not ', 4) == 0)
  1968. {
  1969. $sql .= 'AND ('.$add_search.' '.$conj.' wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name].' != "") ';
  1970. }
  1971. else
  1972. {
  1973. $sql .= 'AND ('.$add_search.' '.$conj.' wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name].' = "") ';
  1974. }
  1975. }
  1976. else
  1977. {
  1978. $sql .= $FNS->sql_andor_string($terms, 'wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name]).' ';
  1979. }
  1980. }
  1981. else
  1982. {
  1983. /** ---------------------------------------
  1984. /** "Contains" e.g.: search:body="pickle"
  1985. /** ---------------------------------------*/
  1986. if (strncmp($terms, 'not ', 4) == 0)
  1987. {
  1988. $terms = substr($terms, 4);
  1989. $like = 'NOT LIKE';
  1990. }
  1991. else
  1992. {
  1993. $like = 'LIKE';
  1994. }
  1995. if (strpos($terms, '&&') !== FALSE)
  1996. {
  1997. $terms = explode('&&', $terms);
  1998. $andor = (strncmp($like, 'NOT', 3) == 0) ? 'OR' : 'AND';
  1999. }
  2000. else
  2001. {
  2002. $terms = explode('|', $terms);
  2003. $andor = (strncmp($like, 'NOT', 3) == 0) ? 'AND' : 'OR';
  2004. }
  2005. $sql .= ' AND (';
  2006. foreach ($terms as $term)
  2007. {
  2008. if ($term == 'IS_EMPTY')
  2009. {
  2010. $sql .= ' wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name].' '.$like.' "" '.$andor;
  2011. }
  2012. elseif (strpos($term, '\W') !== FALSE) // full word only, no partial matches
  2013. {
  2014. $not = ($like == 'LIKE') ? ' ' : ' NOT ';
  2015. // Note: MySQL's nutty POSIX regex word boundary is [[:>:]]
  2016. // we add slashes because $DB->escape_str() strips slashes before adding, and
  2017. // we need them to remain intact or MySQL will not parse the regular expression properly
  2018. $term = '([[:<:]]|^)'.addslashes(preg_quote(str_replace('\W', '', $term))).'([[:>:]]|$)';
  2019. $sql .= ' wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name].$not.'REGEXP "'.$DB->escape_str($term).'" '.$andor;
  2020. }
  2021. else
  2022. {
  2023. $sql .= ' wd.field_id_'.$this->cfields[$PREFS->ini('site_id')][$field_name].' '.$like.' "%'.$DB->escape_like_str($term).'%" '.$andor;
  2024. }
  2025. }
  2026. $sql = substr($sql, 0, -strlen($andor)).') ';
  2027. }
  2028. }
  2029. }
  2030. }
  2031. /** --------------------------------------------------
  2032. /** Build sorting clause
  2033. /** --------------------------------------------------*/
  2034. // We'll assign this to a different variable since we
  2035. // need to use this in two places
  2036. $end = 'ORDER BY ';
  2037. if ($fixed_order !== FALSE)
  2038. {
  2039. $end .= 'FIELD(t.entry_id, '.implode(',', $fixed_order).') ';
  2040. }
  2041. else
  2042. {
  2043. // Used to eliminate sort issues with duplicated fields below
  2044. $entry_id_sort = $sort_array[0];
  2045. if (FALSE === $order_array['0'])
  2046. {
  2047. if ($sticky == 'off')
  2048. {
  2049. $end .= "t.entry_date";
  2050. }
  2051. else
  2052. {
  2053. $end .= "t.sticky desc, t.entry_date";
  2054. }
  2055. if ($sort_array['0'] == 'asc' || $sort_array['0'] == 'desc')
  2056. {
  2057. $end .= " ".$sort_array['0'];
  2058. }
  2059. }
  2060. else
  2061. {
  2062. if ($sticky != 'off')
  2063. {
  2064. $end .= "t.sticky desc, ";
  2065. }
  2066. foreach($order_array as $key => $order)
  2067. {
  2068. if (in_array($order, array('view_count_one', 'view_count_two', 'view_count_three', 'view_count_four')))
  2069. {
  2070. $view_ct = substr($order, 10);
  2071. $order = "view_count";
  2072. }
  2073. if ($key > 0) $end .= ", ";
  2074. switch ($order)
  2075. {
  2076. case 'entry_id' :
  2077. $end .= "t.entry_id";
  2078. break;
  2079. case 'date' :
  2080. $end .= "t.entry_date";
  2081. break;
  2082. case 'edit_date' :
  2083. $end .= "t.edit_date";
  2084. break;
  2085. case 'expiration_date' :
  2086. $end .= "t.expiration_date";
  2087. break;
  2088. case 'title' :
  2089. $end .= "t.title";
  2090. break;
  2091. case 'url_title' :
  2092. $end .= "t.url_title";
  2093. break;
  2094. case 'view_count' :
  2095. $vc = $order.$view_ct;
  2096. $end .= " t.{$vc} ".$sort_array[$key];
  2097. if (sizeof($order_array)-1 == $key)
  2098. {
  2099. $end .= ", t.entry_date ".$sort_array[$key];
  2100. }
  2101. $sort_array[$key] = FALSE;
  2102. break;
  2103. case 'comment_total' :
  2104. $end .= "t.comment_total ".$sort_array[$key];
  2105. if (sizeof($order_array)-1 == $key)
  2106. {
  2107. $end .= ", t.entry_date ".$sort_array[$key];
  2108. }
  2109. $sort_array[$key] = FALSE;
  2110. break;
  2111. case 'most_recent_comment' :
  2112. $end .= "t.recent_comment_date ".$sort_array[$key];
  2113. if (sizeof($order_array)-1 == $key)
  2114. {
  2115. $end .= ", t.entry_date ".$sort_array[$key];
  2116. }
  2117. $sort_array[$key] = FALSE;
  2118. break;
  2119. case 'username' :
  2120. $end .= "m.username";
  2121. break;
  2122. case 'screen_name' :
  2123. $end .= "m.screen_name";
  2124. break;
  2125. case 'custom_field' :
  2126. if (strpos($corder[$key], '|'))
  2127. {
  2128. $end .= "CONCAT(wd.field_id_".implode(", wd.field_id_", explode('|', $corder[$key])).")";
  2129. }
  2130. else
  2131. {
  2132. $end .= "wd.field_id_".$corder[$key];
  2133. }
  2134. break;
  2135. case 'random' :
  2136. $end = "ORDER BY rand()";
  2137. $sort_array[$key] = FALSE;
  2138. break;
  2139. default :
  2140. $end .= "t.entry_date";
  2141. break;
  2142. }
  2143. if ($sort_array[$key] == 'asc' || $sort_array[$key] == 'desc')
  2144. {
  2145. // keep entries with the same timestamp in the correct order
  2146. $end .= " {$sort_array[$key]}";
  2147. }
  2148. }
  2149. }
  2150. // In the event of a sorted field containing identical information as another
  2151. // entry (title, entry_date, etc), they will sort on the order they were entered
  2152. // into ExpressionEngine, with the first "sort" parameter taking precedence.
  2153. // If no sort parameter is set, entries will descend by entry id.
  2154. if ( ! in_array('entry_id', $order_array))
  2155. {
  2156. $end .= ", t.entry_id ".$entry_id_sort;
  2157. }
  2158. }
  2159. /** ----------------------------------------
  2160. /** Determine the row limits
  2161. /** ----------------------------------------*/
  2162. // Even thouth we don't use the LIMIT clause until the end,
  2163. // we need it to help create our pagination links so we'll
  2164. // set it here
  2165. if ($cat_id != '' && is_numeric($TMPL->fetch_param('cat_limit')))
  2166. {
  2167. $this->p_limit = $TMPL->fetch_param('cat_limit');
  2168. }
  2169. elseif ($month != '' && is_numeric($TMPL->fetch_param('month_limit')))
  2170. {
  2171. $this->p_limit = $TMPL->fetch_param('month_limit');
  2172. }
  2173. else
  2174. {
  2175. $this->p_limit = ( ! is_numeric($TMPL->fetch_param('limit'))) ? $this->limit : $TMPL->fetch_param('limit');
  2176. }
  2177. /** ----------------------------------------------
  2178. /** Is there an offset?
  2179. /** ----------------------------------------------*/
  2180. // We do this hear so we can use the offset into next, then later one as well
  2181. $offset = ( ! $TMPL->fetch_param('offset') OR ! is_numeric($TMPL->fetch_param('offset'))) ? '0' : $TMPL->fetch_param('offset');
  2182. /** ----------------------------------------
  2183. /** Do we need pagination?
  2184. /** ----------------------------------------*/
  2185. // We'll run the query to find out
  2186. if ($this->paginate == TRUE)
  2187. {
  2188. if ($this->field_pagination == FALSE)
  2189. {
  2190. $this->pager_sql = $sql_a.$sql_b.$sql;
  2191. $query = $DB->query($this->pager_sql);
  2192. $total = $query->num_rows;
  2193. // Adjust for offset
  2194. if ($total >= $offset)
  2195. $total = $total - $offset;
  2196. $this->create_pagination($total);
  2197. }
  2198. else
  2199. {
  2200. $this->pager_sql = $sql_a.$sql_b.$sql;
  2201. $query = $DB->query($this->pager_sql);
  2202. $total = $query->num_rows;
  2203. $this->create_pagination($total, $query);
  2204. if ($PREFS->ini('enable_sql_caching') == 'y')
  2205. {
  2206. $this->save_cache($this->pager_sql, 'pagination_query');
  2207. $this->save_cache('1', 'field_pagination');
  2208. }
  2209. }
  2210. if ($PREFS->ini('enable_sql_caching') == 'y')
  2211. {
  2212. $this->save_cache($total, 'pagination_count');
  2213. }
  2214. }
  2215. /** ----------------------------------------------
  2216. /** Add Limits to query
  2217. /** ----------------------------------------------*/
  2218. $sql .= $end;
  2219. if ($this->paginate == FALSE)
  2220. $this->p_page = 0;
  2221. // Adjust for offset
  2222. $this->p_page += $offset;
  2223. if ($this->display_by == '')
  2224. {
  2225. if (($page_marker == FALSE AND $this->p_limit != '') || ($page_marker == TRUE AND $this->field_pagination != TRUE))
  2226. {
  2227. $sql .= ($this->p_page == '') ? " LIMIT ".$offset.', '.$this->p_limit : " LIMIT ".$this->p_page.', '.$this->p_limit;
  2228. }
  2229. elseif ($entry_id == '' AND $qtitle == '')
  2230. {
  2231. $sql .= ($this->p_page == '') ? " LIMIT ".$this->limit : " LIMIT ".$this->p_page.', '.$this->limit;
  2232. }
  2233. }
  2234. else
  2235. {
  2236. if ($offset != 0)
  2237. {
  2238. $sql .= ($this->p_page == '') ? " LIMIT ".$offset.', '.$this->p_limit : " LIMIT ".$this->p_page.', '.$this->p_limit;
  2239. }
  2240. }
  2241. /** ----------------------------------------------
  2242. /** Fetch the entry_id numbers
  2243. /** ----------------------------------------------*/
  2244. $query = $DB->query($sql_a.$sql_b.$sql);
  2245. //exit($sql_a.$sql_b.$sql);
  2246. if ($query->num_rows == 0)
  2247. {
  2248. $this->sql = '';
  2249. return;
  2250. }
  2251. /** ----------------------------------------------
  2252. /** Build the full SQL query
  2253. /** ----------------------------------------------*/
  2254. $this->sql = "SELECT ";
  2255. if ($TMPL->fetch_param('category') || $TMPL->fetch_param('category_group') || $cat_id != '')
  2256. {
  2257. // Using DISTINCT like this is bogus but since
  2258. // FULL OUTER JOINs are not supported in older versions
  2259. // of MySQL it's our only choice
  2260. $this->sql .= " DISTINCT(t.entry_id), ";
  2261. }
  2262. if ($this->display_by == 'week' && isset($yearweek))
  2263. {
  2264. $this->sql .= $yearweek.', ';
  2265. }
  2266. // DO NOT CHANGE THE ORDER
  2267. // The exp_member_data table needs to be called before the exp_members table.
  2268. $this->sql .= " t.entry_id, t.weblog_id, t.forum_topic_id, t.author_id, t.ip_address, t.title, t.url_title, t.status, t.dst_enabled, t.view_count_one, t.view_count_two, t.view_count_three, t.view_count_four, t.allow_comments, t.comment_expiration_date, t.allow_trackbacks, t.sticky, t.entry_date, t.year, t.month, t.day, t.edit_date, t.expiration_date, t.recent_comment_date, t.comment_total, t.trackback_total, t.sent_trackbacks, t.recent_trackback_date, t.site_id as entry_site_id,
  2269. w.blog_title, w.blog_name, w.blog_url, w.comment_url, w.tb_return_url, w.comment_moderate, w.weblog_html_formatting, w.weblog_allow_img_urls, w.weblog_auto_link_urls, w.enable_trackbacks, w.trackback_use_url_title, w.trackback_field, w.trackback_use_captcha, w.trackback_system_enabled,
  2270. m.username, m.email, m.url, m.screen_name, m.location, m.occupation, m.interests, m.aol_im, m.yahoo_im, m.msn_im, m.icq, m.signature, m.sig_img_filename, m.sig_img_width, m.sig_img_height, m.avatar_filename, m.avatar_width, m.avatar_height, m.photo_filename, m.photo_width, m.photo_height, m.group_id, m.member_id, m.bday_d, m.bday_m, m.bday_y, m.bio,
  2271. md.*,
  2272. wd.*
  2273. FROM exp_weblog_titles AS t
  2274. LEFT JOIN exp_weblogs AS w ON t.weblog_id = w.weblog_id
  2275. LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id
  2276. LEFT JOIN exp_members AS m ON m.member_id = t.author_id
  2277. LEFT JOIN exp_member_data AS md ON md.member_id = m.member_id ";
  2278. $this->sql .= "WHERE t.entry_id IN (";
  2279. $entries = array();
  2280. // Build ID numbers (checking for duplicates)
  2281. foreach ($query->result as $row)
  2282. {
  2283. if ( ! isset($entries[$row['entry_id']]))
  2284. {
  2285. $entries[$row['entry_id']] = 'y';
  2286. }
  2287. else
  2288. {
  2289. continue;
  2290. }
  2291. $this->sql .= $row['entry_id'].',';
  2292. }
  2293. unset($query);
  2294. unset($entries);
  2295. $this->sql = substr($this->sql, 0, -1).') ';
  2296. // modify the ORDER BY if displaying by week
  2297. if ($this->display_by == 'week' && isset($yearweek))
  2298. {
  2299. $weeksort = ($TMPL->fetch_param('week_sort') == 'desc') ? 'DESC' : 'ASC';
  2300. $end = str_replace('ORDER BY ', 'ORDER BY yearweek '.$weeksort.', ', $end);
  2301. }
  2302. $this->sql .= $end;
  2303. }
  2304. /* END */
  2305. /** ----------------------------------------
  2306. /** Create pagination
  2307. /** ----------------------------------------*/
  2308. function create_pagination($count = 0, $query = '')
  2309. {
  2310. global $FNS, $TMPL, $IN, $REGX, $EXT, $PREFS, $SESS;
  2311. // -------------------------------------------
  2312. // 'weblog_module_create_pagination' hook.
  2313. // - Rewrite the pagination function in the Weblog module
  2314. // - Could be used to expand the kind of pagination available
  2315. // - Paginate via field length, for example
  2316. //
  2317. if ($EXT->active_hook('weblog_module_create_pagination') === TRUE)
  2318. {
  2319. $edata = $EXT->universal_call_extension('weblog_module_create_pagination', $this);
  2320. if ($EXT->end_script === TRUE) return;
  2321. }
  2322. //
  2323. // -------------------------------------------
  2324. if ($this->paginate == TRUE)
  2325. {
  2326. /* --------------------------------------
  2327. /* For subdomain's or domains using $template_group and $template
  2328. /* in path.php, the pagination for the main index page requires
  2329. /* that the template group and template are specified.
  2330. /* --------------------------------------*/
  2331. if (($IN->URI == '' OR $IN->URI == '/') && $PREFS->ini('template_group') != '' && $PREFS->ini('template') != '')
  2332. {
  2333. $this->basepath = $FNS->create_url($PREFS->ini('template_group').'/'.$PREFS->ini('template'), 1);
  2334. }
  2335. if ($this->basepath == '')
  2336. {
  2337. $this->basepath = $FNS->create_url($IN->URI, 1);
  2338. if (preg_match("#^P(\d+)|/P(\d+)#", $this->QSTR, $match))
  2339. {
  2340. $this->p_page = (isset($match['2'])) ? $match['2'] : $match['1'];
  2341. $this->basepath = $FNS->remove_double_slashes(str_replace($match['0'], '', $this->basepath));
  2342. }
  2343. }
  2344. /** ----------------------------------------
  2345. /** Standard pagination - base values
  2346. /** ----------------------------------------*/
  2347. if ($this->field_pagination == FALSE)
  2348. {
  2349. if ($this->display_by == '')
  2350. {
  2351. if ($count == 0)
  2352. {
  2353. $this->sql = '';
  2354. return;
  2355. }
  2356. $this->total_rows = $count;
  2357. }
  2358. if ($this->dynamic_sql == FALSE)
  2359. {
  2360. $cat_limit = FALSE;
  2361. if ((in_array($this->reserved_cat_segment, explode("/", $IN->URI))
  2362. AND $TMPL->fetch_param('dynamic') != 'off'
  2363. AND $TMPL->fetch_param('weblog'))
  2364. || (preg_match("#(^|\/)C(\d+)#", $IN->URI, $match) AND $TMPL->fetch_param('dynamic') != 'off'))
  2365. {
  2366. $cat_limit = TRUE;
  2367. }
  2368. if ($cat_limit && is_numeric($TMPL->fetch_param('cat_limit')))
  2369. {
  2370. $this->p_limit = $TMPL->fetch_param('cat_limit');
  2371. }
  2372. else
  2373. {
  2374. $this->p_limit = ( ! is_numeric($TMPL->fetch_param('limit'))) ? $this->limit : $TMPL->fetch_param('limit');
  2375. }
  2376. }
  2377. $this->p_page = ($this->p_page == '' || ($this->p_limit > 1 AND $this->p_page == 1)) ? 0 : $this->p_page;
  2378. if ($this->p_page > $this->total_rows)
  2379. {
  2380. $this->p_page = 0;
  2381. }
  2382. $this->current_page = floor(($this->p_page / $this->p_limit) + 1);
  2383. $this->total_pages = intval(floor($this->total_rows / $this->p_limit));
  2384. }
  2385. else
  2386. {
  2387. /** ----------------------------------------
  2388. /** Field pagination - base values
  2389. /** ----------------------------------------*/
  2390. if ($count == 0)
  2391. {
  2392. $this->sql = '';
  2393. return;
  2394. }
  2395. $m_fields = array();
  2396. foreach ($this->multi_fields as $val)
  2397. {
  2398. foreach($this->cfields as $site_id => $cfields)
  2399. {
  2400. if (isset($cfields[$val]))
  2401. {
  2402. if (isset($query->row['field_id_'.$cfields[$val]]) AND $query->row['field_id_'.$cfields[$val]] != '')
  2403. {
  2404. $m_fields[] = $val;
  2405. }
  2406. }
  2407. }
  2408. }
  2409. $this->p_limit = 1;
  2410. $this->total_rows = count($m_fields);
  2411. $this->total_pages = $this->total_rows;
  2412. if ($this->total_pages == 0)
  2413. $this->total_pages = 1;
  2414. $this->p_page = ($this->p_page == '') ? 0 : $this->p_page;
  2415. if ($this->p_page > $this->total_rows)
  2416. {
  2417. $this->p_page = 0;
  2418. }
  2419. $this->current_page = floor(($this->p_page / $this->p_limit) + 1);
  2420. if (isset($m_fields[$this->p_page]))
  2421. {
  2422. $TMPL->tagdata = preg_replace("/".LD."multi_field\=[\"'].+?[\"']".RD."/s", LD.$m_fields[$this->p_page].RD, $TMPL->tagdata);
  2423. $TMPL->var_single[$m_fields[$this->p_page]] = $m_fields[$this->p_page];
  2424. }
  2425. }
  2426. /** ----------------------------------------
  2427. /** Create the pagination
  2428. /** ----------------------------------------*/
  2429. if ($this->total_rows % $this->p_limit)
  2430. {
  2431. $this->total_pages++;
  2432. }
  2433. if ($this->total_rows > $this->p_limit)
  2434. {
  2435. if ( ! class_exists('Paginate'))
  2436. {
  2437. require PATH_CORE.'core.paginate'.EXT;
  2438. }
  2439. $PGR = new Paginate();
  2440. if ( ! stristr($this->basepath, SELF) AND $PREFS->ini('site_index') != '')
  2441. {
  2442. $this->basepath .= SELF.'/';
  2443. }
  2444. if ($TMPL->fetch_param('paginate_base'))
  2445. {
  2446. $this->basepath = $FNS->create_url($REGX->trim_slashes($TMPL->fetch_param('paginate_base')));
  2447. }
  2448. $first_url = (preg_match("#\.php/$#", $this->basepath)) ? substr($this->basepath, 0, -1) : $this->basepath;
  2449. $PGR->first_url = $first_url;
  2450. $PGR->path = $this->basepath;
  2451. $PGR->prefix = 'P';
  2452. $PGR->total_count = $this->total_rows;
  2453. $PGR->per_page = $this->p_limit;
  2454. $PGR->cur_page = $this->p_page;
  2455. $this->pagination_links = $PGR->show_links();
  2456. if ((($this->total_pages * $this->p_limit) - $this->p_limit) > $this->p_page)
  2457. {
  2458. $this->page_next = $this->basepath.'P'.($this->p_page + $this->p_limit).'/';
  2459. }
  2460. if (($this->p_page - $this->p_limit ) >= 0)
  2461. {
  2462. $this->page_previous = $this->basepath.'P'.($this->p_page - $this->p_limit).'/';
  2463. }
  2464. }
  2465. else
  2466. {
  2467. $this->p_page = '';
  2468. }
  2469. }
  2470. }
  2471. /* END */
  2472. /** ----------------------------------------
  2473. /** Parse weblog entries
  2474. /** ----------------------------------------*/
  2475. function parse_weblog_entries()
  2476. {
  2477. global $IN, $DB, $TMPL, $FNS, $SESS, $LOC, $PREFS, $REGX, $EXT;
  2478. $switch = array();
  2479. /** ----------------------------------------
  2480. /** Set default date header variables
  2481. /** ----------------------------------------*/
  2482. $heading_date_hourly = 0;
  2483. $heading_flag_hourly = 0;
  2484. $heading_flag_weekly = 1;
  2485. $heading_date_daily = 0;
  2486. $heading_flag_daily = 0;
  2487. $heading_date_monthly = 0;
  2488. $heading_flag_monthly = 0;
  2489. $heading_date_yearly = 0;
  2490. $heading_flag_yearly = 0;
  2491. /** ----------------------------------------
  2492. /** Fetch the "category chunk"
  2493. /** ----------------------------------------*/
  2494. // We'll grab the category data now to avoid processing cycles in the foreach loop below
  2495. $cat_chunk = array();
  2496. if (preg_match_all("/".LD."categories(.*?)".RD."(.*?)".LD.SLASH.'categories'.RD."/s", $TMPL->tagdata, $matches))
  2497. {
  2498. for ($j = 0; $j < count($matches['0']); $j++)
  2499. {
  2500. $cat_chunk[] = array($matches['2'][$j], $FNS->assign_parameters($matches['1'][$j]), $matches['0'][$j]);
  2501. }
  2502. }
  2503. /** ----------------------------------------
  2504. /** Fetch all the date-related variables
  2505. /** ----------------------------------------*/
  2506. $entry_date = array();
  2507. $gmt_date = array();
  2508. $gmt_entry_date = array();
  2509. $edit_date = array();
  2510. $gmt_edit_date = array();
  2511. $expiration_date = array();
  2512. $week_date = array();
  2513. // We do this here to avoid processing cycles in the foreach loop
  2514. $date_vars = array('entry_date', 'gmt_date', 'gmt_entry_date', 'edit_date', 'gmt_edit_date', 'expiration_date', 'recent_comment_date', 'week_date');
  2515. foreach ($date_vars as $val)
  2516. {
  2517. if (preg_match_all("/".LD.$val."\s+format=[\"'](.*?)[\"']".RD."/s", $TMPL->tagdata, $matches))
  2518. {
  2519. for ($j = 0; $j < count($matches['0']); $j++)
  2520. {
  2521. $matches['0'][$j] = str_replace(array(LD,RD), '', $matches['0'][$j]);
  2522. switch ($val)
  2523. {
  2524. case 'entry_date' : $entry_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2525. break;
  2526. case 'gmt_date' : $gmt_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2527. break;
  2528. case 'gmt_entry_date' : $gmt_entry_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2529. break;
  2530. case 'edit_date' : $edit_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2531. break;
  2532. case 'gmt_edit_date' : $gmt_edit_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2533. break;
  2534. case 'expiration_date' : $expiration_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2535. break;
  2536. case 'recent_comment_date' : $recent_comment_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2537. break;
  2538. case 'week_date' : $week_date[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2539. break;
  2540. }
  2541. }
  2542. }
  2543. }
  2544. // Are any of the custom fields dates?
  2545. $custom_date_fields = array();
  2546. if (count($this->dfields) > 0)
  2547. {
  2548. foreach ($this->dfields as $site_id => $dfields)
  2549. {
  2550. foreach($dfields as $key => $value)
  2551. {
  2552. if (preg_match_all("/".LD.$key."\s+format=[\"'](.*?)[\"']".RD."/s", $TMPL->tagdata, $matches))
  2553. {
  2554. for ($j = 0; $j < count($matches['0']); $j++)
  2555. {
  2556. $matches['0'][$j] = str_replace(array(LD,RD), '', $matches['0'][$j]);
  2557. $custom_date_fields[$matches['0'][$j]] = $LOC->fetch_date_params($matches['1'][$j]);
  2558. }
  2559. }
  2560. }
  2561. }
  2562. }
  2563. // And the same again for reverse related entries
  2564. $reverse_markers = array();
  2565. if (preg_match_all("/".LD."REV_REL\[([^\]]+)\]REV_REL".RD."/", $TMPL->tagdata, $matches))
  2566. {
  2567. for ($j = 0; $j < count($matches['0']); $j++)
  2568. {
  2569. $reverse_markers[$matches['1'][$j]] = '';
  2570. }
  2571. }
  2572. /** ----------------------------------------
  2573. /** "Search by Member" link
  2574. /** ----------------------------------------*/
  2575. // We use this with the {member_search_path} variable
  2576. $result_path = (preg_match("/".LD."member_search_path\s*=(.*?)".RD."/s", $TMPL->tagdata, $match)) ? $match['1'] : 'search/results';
  2577. $result_path = str_replace(array("\"","'"), "", $result_path);
  2578. $qs = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
  2579. $search_link = $FNS->fetch_site_index(0, 0).$qs.'ACT='.$FNS->fetch_action_id('Search', 'do_search').'&amp;result_path='.$result_path.'&amp;mbr=';
  2580. /** ----------------------------------------
  2581. /** Start the main processing loop
  2582. /** ----------------------------------------*/
  2583. // -------------------------------------------
  2584. // 'weblog_entries_query_result' hook.
  2585. // - Take the whole query object, do what you wish
  2586. // - added 1.6.7
  2587. //
  2588. if ($EXT->active_hook('weblog_entries_query_result') === TRUE)
  2589. {
  2590. $this->query = $EXT->call_extension('weblog_entries_query_result', $this, $this->query);
  2591. if ($EXT->end_script === TRUE) return $TMPL->tagdata;
  2592. }
  2593. //
  2594. // -------------------------------------------
  2595. $tb_captcha = TRUE;
  2596. $total_results = count($this->query->result);
  2597. $site_pages = $PREFS->ini('site_pages');
  2598. foreach ($this->query->result as $count => $row)
  2599. {
  2600. // Fetch the tag block containing the variables that need to be parsed
  2601. $tagdata = $TMPL->tagdata;
  2602. $row['count'] = $count+1;
  2603. $row['page_uri'] = '';
  2604. $row['page_url'] = '';
  2605. $row['total_results'] = $total_results;
  2606. $row['absolute_count'] = $this->p_page + $row['count'];
  2607. if ($site_pages !== FALSE && isset($site_pages['uris'][$row['entry_id']]))
  2608. {
  2609. $row['page_uri'] = $site_pages['uris'][$row['entry_id']];
  2610. $row['page_url'] = $FNS->create_url($site_pages['uris'][$row['entry_id']]);
  2611. }
  2612. // -------------------------------------------
  2613. // 'weblog_entries_tagdata' hook.
  2614. // - Take the entry data and tag data, do what you wish
  2615. //
  2616. if ($EXT->active_hook('weblog_entries_tagdata') === TRUE)
  2617. {
  2618. $tagdata = $EXT->call_extension('weblog_entries_tagdata', $tagdata, $row, $this);
  2619. if ($EXT->end_script === TRUE) return $tagdata;
  2620. }
  2621. //
  2622. // -------------------------------------------
  2623. // -------------------------------------------
  2624. // 'weblog_entries_row' hook.
  2625. // - Take the entry data, do what you wish
  2626. // - added 1.6.7
  2627. //
  2628. if ($EXT->active_hook('weblog_entries_row') === TRUE)
  2629. {
  2630. $row = $EXT->call_extension('weblog_entries_row', $this, $row);
  2631. if ($EXT->end_script === TRUE) return $tagdata;
  2632. }
  2633. //
  2634. // -------------------------------------------
  2635. /** ----------------------------------------
  2636. /** Trackback RDF?
  2637. /** ----------------------------------------*/
  2638. if ($TMPL->fetch_param('rdf') == "on")
  2639. {
  2640. $this->display_tb_rdf = TRUE;
  2641. }
  2642. elseif ($TMPL->fetch_param('rdf') == "off")
  2643. {
  2644. $this->display_tb_rdf = FALSE;
  2645. }
  2646. elseif ($row['enable_trackbacks'] == 'y')
  2647. {
  2648. $this->display_tb_rdf = TRUE;
  2649. }
  2650. else
  2651. {
  2652. $this->display_tb_rdf = FALSE;
  2653. }
  2654. /** ----------------------------------------
  2655. /** Adjust dates if needed
  2656. /** ----------------------------------------*/
  2657. // If the "dst_enabled" item is set in any given entry
  2658. // we need to offset to the timestamp by an hour
  2659. if ( ! isset($row['dst_enabled']))
  2660. $row['dst_enabled'] = 'n';
  2661. if ($row['entry_date'] != '')
  2662. $row['entry_date'] = $LOC->offset_entry_dst($row['entry_date'], $row['dst_enabled'], FALSE);
  2663. if ($row['expiration_date'] != '' AND $row['expiration_date'] != 0)
  2664. $row['expiration_date'] = $LOC->offset_entry_dst($row['expiration_date'], $row['dst_enabled'], FALSE);
  2665. if ($row['comment_expiration_date'] != '' AND $row['comment_expiration_date'] != 0)
  2666. $row['comment_expiration_date'] = $LOC->offset_entry_dst($row['comment_expiration_date'], $row['dst_enabled'], FALSE);
  2667. /** ------------------------------------------
  2668. /** Reset custom date fields
  2669. /** ------------------------------------------*/
  2670. // Since custom date fields columns are integer types by default, if they
  2671. // don't contain any data they return a zero.
  2672. // This creates a problem if conditionals are used with those fields.
  2673. // For example, if an admin has this in a template: {if mydate == ''}
  2674. // Since the field contains a zero it would never evaluate true.
  2675. // Therefore we'll reset any zero dates to nothing.
  2676. if (isset($this->dfields[$row['site_id']]) && count($this->dfields[$row['site_id']]) > 0)
  2677. {
  2678. foreach ($this->dfields[$row['site_id']] as $dkey => $dval)
  2679. {
  2680. // While we're at it, kill any formatting
  2681. $row['field_ft_'.$dval] = 'none';
  2682. if (isset($row['field_id_'.$dval]) AND $row['field_id_'.$dval] == 0)
  2683. {
  2684. $row['field_id_'.$dval] = '';
  2685. }
  2686. }
  2687. }
  2688. // While we're at it, do the same for related entries.
  2689. if (isset($this->rfields[$row['site_id']]) && count($this->rfields[$row['site_id']]) > 0)
  2690. {
  2691. foreach ($this->rfields[$row['site_id']] as $rkey => $rval)
  2692. {
  2693. $row['field_ft_'.$rval] = 'none';
  2694. }
  2695. }
  2696. // Reverse related markers
  2697. $j = 0;
  2698. foreach ($reverse_markers as $k => $v)
  2699. {
  2700. $this->reverse_related_entries[$row['entry_id']][$j] = $k;
  2701. $tagdata = str_replace( LD."REV_REL[".$k."]REV_REL".RD, LD."REV_REL[".$k."][".$row['entry_id']."]REV_REL".RD, $tagdata);
  2702. $j++;
  2703. }
  2704. /** ----------------------------------------
  2705. /** Conditionals
  2706. /** ----------------------------------------*/
  2707. $cond = $row;
  2708. $cond['logged_in'] = ($SESS->userdata('member_id') == 0) ? 'FALSE' : 'TRUE';
  2709. $cond['logged_out'] = ($SESS->userdata('member_id') != 0) ? 'FALSE' : 'TRUE';
  2710. if (($row['comment_expiration_date'] > 0 && $LOC->now > $row['comment_expiration_date']) OR $row['allow_comments'] == 'n')
  2711. {
  2712. $cond['allow_comments'] = 'FALSE';
  2713. }
  2714. else
  2715. {
  2716. $cond['allow_comments'] = 'TRUE';
  2717. }
  2718. foreach (array('avatar_filename', 'photo_filename', 'sig_img_filename') as $pv)
  2719. {
  2720. if ( ! isset($row[$pv]))
  2721. $row[$pv] = '';
  2722. }
  2723. $cond['allow_trackbacks'] = ($row['allow_trackbacks'] == 'n' OR $row['trackback_system_enabled'] == '') ? 'FALSE' : 'TRUE';
  2724. $cond['signature_image'] = ($row['sig_img_filename'] == '' OR $PREFS->ini('enable_signatures') == 'n' OR $SESS->userdata('display_signatures') == 'n') ? 'FALSE' : 'TRUE';
  2725. $cond['avatar'] = ($row['avatar_filename'] == '' OR $PREFS->ini('enable_avatars') == 'n' OR $SESS->userdata('display_avatars') == 'n') ? 'FALSE' : 'TRUE';
  2726. $cond['photo'] = ($row['photo_filename'] == '' OR $PREFS->ini('enable_photos') == 'n' OR $SESS->userdata('display_photos') == 'n') ? 'FALSE' : 'TRUE';
  2727. $cond['forum_topic'] = ($row['forum_topic_id'] == 0) ? 'FALSE' : 'TRUE';
  2728. $cond['not_forum_topic'] = ($row['forum_topic_id'] != 0) ? 'FALSE' : 'TRUE';
  2729. $cond['comment_tb_total'] = $row['comment_total'] + $row['trackback_total'];
  2730. $cond['category_request'] = ($this->cat_request === FALSE) ? 'FALSE' : 'TRUE';
  2731. $cond['not_category_request'] = ($this->cat_request !== FALSE) ? 'FALSE' : 'TRUE';
  2732. $cond['weblog'] = $row['blog_title'];
  2733. $cond['weblog_short_name'] = $row['blog_name'];
  2734. $cond['author'] = ($row['screen_name'] != '') ? $row['screen_name'] : $row['username'];
  2735. $cond['photo_url'] = $PREFS->ini('photo_url', 1).$row['photo_filename'];
  2736. $cond['photo_image_width'] = $row['photo_width'];
  2737. $cond['photo_image_height'] = $row['photo_height'];
  2738. $cond['avatar_url'] = $PREFS->ini('avatar_url', 1).$row['avatar_filename'];
  2739. $cond['avatar_image_width'] = $row['avatar_width'];
  2740. $cond['avatar_image_height'] = $row['avatar_height'];
  2741. $cond['signature_image_url'] = $PREFS->ini('sig_img_url', 1).$row['sig_img_filename'];
  2742. $cond['signature_image_width'] = $row['sig_img_width'];
  2743. $cond['signature_image_height'] = $row['sig_img_height'];
  2744. $cond['relative_date'] = $LOC->format_timespan($LOC->now - $row['entry_date']);
  2745. if (isset($this->cfields[$row['site_id']]))
  2746. {
  2747. foreach($this->cfields[$row['site_id']] as $key => $value)
  2748. {
  2749. $cond[$key] = ( ! isset($row['field_id_'.$value])) ? '' : $row['field_id_'.$value];
  2750. }
  2751. }
  2752. foreach($this->mfields as $key => $value)
  2753. {
  2754. if (isset($row['m_field_id_'.$value['0']]))
  2755. {
  2756. $cond[$key] = $this->TYPE->parse_type($row['m_field_id_'.$value['0']],
  2757. array(
  2758. 'text_format' => $value['1'],
  2759. 'html_format' => 'safe',
  2760. 'auto_links' => 'y',
  2761. 'allow_img_url' => 'n'
  2762. )
  2763. );
  2764. }
  2765. }
  2766. $tagdata = $FNS->prep_conditionals($tagdata, $cond);
  2767. /** ----------------------------------------
  2768. /** Parse Variable Pairs
  2769. /** ----------------------------------------*/
  2770. foreach ($TMPL->var_pair as $key => $val)
  2771. {
  2772. /** ----------------------------------------
  2773. /** parse categories
  2774. /** ----------------------------------------*/
  2775. if (strncmp('categories', $key, 10) == 0)
  2776. {
  2777. if (isset($this->categories[$row['entry_id']]) AND is_array($this->categories[$row['entry_id']]) AND count($cat_chunk) > 0)
  2778. {
  2779. foreach ($cat_chunk as $catkey => $catval)
  2780. {
  2781. $i = 0;
  2782. $cats = '';
  2783. if (isset($catval['1']['limit']))
  2784. {
  2785. $cat_limit = $catval['1']['limit'];
  2786. }
  2787. else
  2788. {
  2789. $cat_limit = FALSE;
  2790. }
  2791. // We do the pulling out of categories before the "prepping" of conditionals
  2792. // So, we have to do it here again too. How annoying...
  2793. $catval[0] = $FNS->prep_conditionals($catval[0], $cond);
  2794. $catval[2] = $FNS->prep_conditionals($catval[2], $cond);
  2795. $not_these = array();
  2796. $these = array();
  2797. $not_these_groups = array();
  2798. $these_groups = array();
  2799. if (isset($catval['1']['show']))
  2800. {
  2801. if (strncmp('not ', $catval[1]['show'], 4) == 0)
  2802. {
  2803. $not_these = explode('|', trim(substr($catval['1']['show'], 3)));
  2804. }
  2805. else
  2806. {
  2807. $these = explode('|', trim($catval['1']['show']));
  2808. }
  2809. }
  2810. if (isset($catval['1']['show_group']))
  2811. {
  2812. if (strncmp('not ', $catval[1]['show_group'], 4) == 0)
  2813. {
  2814. $not_these_groups = explode('|', trim(substr($catval['1']['show_group'], 3)));
  2815. }
  2816. else
  2817. {
  2818. $these_groups = explode('|', trim($catval['1']['show_group']));
  2819. }
  2820. }
  2821. foreach ($this->categories[$row['entry_id']] as $k => $v)
  2822. {
  2823. if (in_array($v['0'], $not_these) OR (isset($v['5']) && in_array($v['5'], $not_these_groups)))
  2824. {
  2825. continue;
  2826. }
  2827. elseif( (sizeof($these) > 0 && ! in_array($v['0'], $these)) OR
  2828. (sizeof($these_groups) > 0 && isset($v['5']) && ! in_array($v['5'], $these_groups)))
  2829. {
  2830. continue;
  2831. }
  2832. $temp = $catval['0'];
  2833. if (preg_match_all("#".LD."path=(.+?)".RD."#", $temp, $matches))
  2834. {
  2835. foreach ($matches['1'] as $match)
  2836. {
  2837. if ($this->use_category_names == TRUE)
  2838. {
  2839. $temp = preg_replace("#".LD."path=.+?".RD."#", $FNS->remove_double_slashes($FNS->create_url($match).'/'.$this->reserved_cat_segment.'/'.$v['6'].'/'), $temp, 1);
  2840. }
  2841. else
  2842. {
  2843. $temp = preg_replace("#".LD."path=.+?".RD."#", $FNS->remove_double_slashes($FNS->create_url($match).'/C'.$v['0'].'/'), $temp, 1);
  2844. }
  2845. }
  2846. }
  2847. else
  2848. {
  2849. $temp = preg_replace("#".LD."path=.+?".RD."#", $FNS->create_url("SITE_INDEX"), $temp);
  2850. }
  2851. $cat_vars = array('category_name' => $v['2'],
  2852. 'category_url_title' => $v['6'],
  2853. 'category_description' => (isset($v['4'])) ? $v['4'] : '',
  2854. 'category_group' => (isset($v['5'])) ? $v['5'] : '',
  2855. 'category_image' => $v['3'],
  2856. 'category_id' => $v['0'],
  2857. 'parent_id' => $v['1']);
  2858. // add custom fields for conditionals prep
  2859. foreach ($this->catfields as $cv)
  2860. {
  2861. $cat_vars[$cv['field_name']] = ( ! isset($v['field_id_'.$cv['field_id']])) ? '' : $v['field_id_'.$cv['field_id']];
  2862. }
  2863. $temp = $FNS->prep_conditionals($temp, $cat_vars);
  2864. $temp = str_replace(array(LD."category_id".RD,
  2865. LD."category_name".RD,
  2866. LD."category_url_title".RD,
  2867. LD."category_image".RD,
  2868. LD."category_group".RD,
  2869. LD.'category_description'.RD,
  2870. LD.'parent_id'.RD),
  2871. array($v['0'],
  2872. $v['2'],
  2873. $v['6'],
  2874. $v['3'],
  2875. (isset($v['5'])) ? $v['5'] : '',
  2876. (isset($v['4'])) ? $v['4'] : '',
  2877. $v['1']
  2878. ),
  2879. $temp);
  2880. foreach($this->catfields as $cv2)
  2881. {
  2882. if (isset($v['field_id_'.$cv2['field_id']]) AND $v['field_id_'.$cv2['field_id']] != '')
  2883. {
  2884. $field_content = $this->TYPE->parse_type($v['field_id_'.$cv2['field_id']],
  2885. array(
  2886. 'text_format' => $v['field_ft_'.$cv2['field_id']],
  2887. 'html_format' => $v['field_html_formatting'],
  2888. 'auto_links' => 'n',
  2889. 'allow_img_url' => 'y'
  2890. )
  2891. );
  2892. $temp = str_replace(LD.$cv2['field_name'].RD, $field_content, $temp);
  2893. }
  2894. else
  2895. {
  2896. // garbage collection
  2897. $temp = str_replace(LD.$cv2['field_name'].RD, '', $temp);
  2898. }
  2899. $temp = $FNS->remove_double_slashes($temp);
  2900. }
  2901. $cats .= $temp;
  2902. if ($cat_limit !== FALSE && $cat_limit == ++$i)
  2903. {
  2904. break;
  2905. }
  2906. }
  2907. $cats = rtrim(str_replace("&#47;", "/", $cats));
  2908. if (is_array($catval['1']) AND isset($catval['1']['backspace']))
  2909. {
  2910. $cats = substr($cats, 0, - $catval['1']['backspace']);
  2911. }
  2912. $cats = str_replace("/", "&#47;", $cats);
  2913. $tagdata = str_replace($catval['2'], $cats, $tagdata);
  2914. }
  2915. }
  2916. else
  2917. {
  2918. $tagdata = $TMPL->delete_var_pairs($key, 'categories', $tagdata);
  2919. }
  2920. }
  2921. /** ----------------------------------------
  2922. /** parse date heading
  2923. /** ----------------------------------------*/
  2924. if (strncmp('date_heading', $key, 12) == 0)
  2925. {
  2926. // Set the display preference
  2927. $display = (is_array($val) AND isset($val['display'])) ? $val['display'] : 'daily';
  2928. /** ----------------------------------------
  2929. /** Hourly header
  2930. /** ----------------------------------------*/
  2931. if ($display == 'hourly')
  2932. {
  2933. $heading_date_hourly = date('YmdH', $LOC->set_localized_time($row['entry_date']));
  2934. if ($heading_date_hourly == $heading_flag_hourly)
  2935. {
  2936. $tagdata = $TMPL->delete_var_pairs($key, 'date_heading', $tagdata);
  2937. }
  2938. else
  2939. {
  2940. $tagdata = $TMPL->swap_var_pairs($key, 'date_heading', $tagdata);
  2941. $heading_flag_hourly = $heading_date_hourly;
  2942. }
  2943. }
  2944. /** ----------------------------------------
  2945. /** Weekly header
  2946. /** ----------------------------------------*/
  2947. elseif ($display == 'weekly')
  2948. {
  2949. $temp_date = $LOC->set_localized_time($row['entry_date']);
  2950. // date()'s week variable 'W' starts weeks on Monday per ISO-8601.
  2951. // By default we start weeks on Sunday, so we need to do a little dance for
  2952. // entries made on Sundays to make sure they get placed in the right week heading
  2953. if (strtolower($TMPL->fetch_param('start_day')) != 'monday' && date('w', $LOC->set_localized_time($row['entry_date'])) == 0)
  2954. {
  2955. // add 7 days to toss us into the next ISO-8601 week
  2956. $heading_date_weekly = date('YW', $temp_date + 604800);
  2957. }
  2958. else
  2959. {
  2960. $heading_date_weekly = date('YW', $temp_date);
  2961. }
  2962. if ($heading_date_weekly == $heading_flag_weekly)
  2963. {
  2964. $tagdata = $TMPL->delete_var_pairs($key, 'date_heading', $tagdata);
  2965. }
  2966. else
  2967. {
  2968. $tagdata = $TMPL->swap_var_pairs($key, 'date_heading', $tagdata);
  2969. $heading_flag_weekly = $heading_date_weekly;
  2970. }
  2971. }
  2972. /** ----------------------------------------
  2973. /** Monthly header
  2974. /** ----------------------------------------*/
  2975. elseif ($display == 'monthly')
  2976. {
  2977. $heading_date_monthly = date('Ym', $LOC->set_localized_time($row['entry_date']));
  2978. if ($heading_date_monthly == $heading_flag_monthly)
  2979. {
  2980. $tagdata = $TMPL->delete_var_pairs($key, 'date_heading', $tagdata);
  2981. }
  2982. else
  2983. {
  2984. $tagdata = $TMPL->swap_var_pairs($key, 'date_heading', $tagdata);
  2985. $heading_flag_monthly = $heading_date_monthly;
  2986. }
  2987. }
  2988. /** ----------------------------------------
  2989. /** Yearly header
  2990. /** ----------------------------------------*/
  2991. elseif ($display == 'yearly')
  2992. {
  2993. $heading_date_yearly = date('Y', $LOC->set_localized_time($row['entry_date']));
  2994. if ($heading_date_yearly == $heading_flag_yearly)
  2995. {
  2996. $tagdata = $TMPL->delete_var_pairs($key, 'date_heading', $tagdata);
  2997. }
  2998. else
  2999. {
  3000. $tagdata = $TMPL->swap_var_pairs($key, 'date_heading', $tagdata);
  3001. $heading_flag_yearly = $heading_date_yearly;
  3002. }
  3003. }
  3004. /** ----------------------------------------
  3005. /** Default (daily) header
  3006. /** ----------------------------------------*/
  3007. else
  3008. {
  3009. $heading_date_daily = date('Ymd', $LOC->set_localized_time($row['entry_date']));
  3010. if ($heading_date_daily == $heading_flag_daily)
  3011. {
  3012. $tagdata = $TMPL->delete_var_pairs($key, 'date_heading', $tagdata);
  3013. }
  3014. else
  3015. {
  3016. $tagdata = $TMPL->swap_var_pairs($key, 'date_heading', $tagdata);
  3017. $heading_flag_daily = $heading_date_daily;
  3018. }
  3019. }
  3020. }
  3021. // END DATE HEADING
  3022. /** ----------------------------------------
  3023. /** parse date footer
  3024. /** ----------------------------------------*/
  3025. if (strncmp('date_footer', $key, 11) == 0)
  3026. {
  3027. // Set the display preference
  3028. $display = (is_array($val) AND isset($val['display'])) ? $val['display'] : 'daily';
  3029. /** ----------------------------------------
  3030. /** Hourly footer
  3031. /** ----------------------------------------*/
  3032. if ($display == 'hourly')
  3033. {
  3034. if ( ! isset($this->query->result[$row['count']]) OR
  3035. date('YmdH', $LOC->set_localized_time($row['entry_date'])) != date('YmdH', $LOC->set_localized_time($this->query->result[$row['count']]['entry_date'])))
  3036. {
  3037. $tagdata = $TMPL->swap_var_pairs($key, 'date_footer', $tagdata);
  3038. }
  3039. else
  3040. {
  3041. $tagdata = $TMPL->delete_var_pairs($key, 'date_footer', $tagdata);
  3042. }
  3043. }
  3044. /** ----------------------------------------
  3045. /** Weekly footer
  3046. /** ----------------------------------------*/
  3047. elseif ($display == 'weekly')
  3048. {
  3049. if ( ! isset($this->query->result[$row['count']]) OR
  3050. date('YW', $LOC->set_localized_time($row['entry_date'])) != date('YW', $LOC->set_localized_time($this->query->result[$row['count']]['entry_date'])))
  3051. {
  3052. $tagdata = $TMPL->swap_var_pairs($key, 'date_footer', $tagdata);
  3053. }
  3054. else
  3055. {
  3056. $tagdata = $TMPL->delete_var_pairs($key, 'date_footer', $tagdata);
  3057. }
  3058. }
  3059. /** ----------------------------------------
  3060. /** Monthly footer
  3061. /** ----------------------------------------*/
  3062. elseif ($display == 'monthly')
  3063. {
  3064. if ( ! isset($this->query->result[$row['count']]) OR
  3065. date('Ym', $LOC->set_localized_time($row['entry_date'])) != date('Ym', $LOC->set_localized_time($this->query->result[$row['count']]['entry_date'])))
  3066. {
  3067. $tagdata = $TMPL->swap_var_pairs($key, 'date_footer', $tagdata);
  3068. }
  3069. else
  3070. {
  3071. $tagdata = $TMPL->delete_var_pairs($key, 'date_footer', $tagdata);
  3072. }
  3073. }
  3074. /** ----------------------------------------
  3075. /** Yearly footer
  3076. /** ----------------------------------------*/
  3077. elseif ($display == 'yearly')
  3078. {
  3079. if ( ! isset($this->query->result[$row['count']]) OR
  3080. date('Y', $LOC->set_localized_time($row['entry_date'])) != date('Y', $LOC->set_localized_time($this->query->result[$row['count']]['entry_date'])))
  3081. {
  3082. $tagdata = $TMPL->swap_var_pairs($key, 'date_footer', $tagdata);
  3083. }
  3084. else
  3085. {
  3086. $tagdata = $TMPL->delete_var_pairs($key, 'date_footer', $tagdata);
  3087. }
  3088. }
  3089. /** ----------------------------------------
  3090. /** Default (daily) footer
  3091. /** ----------------------------------------*/
  3092. else
  3093. {
  3094. if ( ! isset($this->query->result[$row['count']]) OR
  3095. date('Ymd', $LOC->set_localized_time($row['entry_date'])) != date('Ymd', $LOC->set_localized_time($this->query->result[$row['count']]['entry_date'])))
  3096. {
  3097. $tagdata = $TMPL->swap_var_pairs($key, 'date_footer', $tagdata);
  3098. }
  3099. else
  3100. {
  3101. $tagdata = $TMPL->delete_var_pairs($key, 'date_footer', $tagdata);
  3102. }
  3103. }
  3104. }
  3105. // END DATE FOOTER
  3106. }
  3107. // END VARIABLE PAIRS
  3108. /** ----------------------------------------
  3109. /** Parse "single" variables
  3110. /** ----------------------------------------*/
  3111. foreach ($TMPL->var_single as $key => $val)
  3112. {
  3113. /** ------------------------------------------------
  3114. /** parse simple conditionals: {body|more|summary}
  3115. /** ------------------------------------------------*/
  3116. // Note: This must happen first.
  3117. if (stristr($key, '|') && is_array($val))
  3118. {
  3119. foreach($val as $item)
  3120. {
  3121. // Basic fields
  3122. if (isset($row[$item]) AND $row[$item] != "")
  3123. {
  3124. $tagdata = $TMPL->swap_var_single($key, $row[$item], $tagdata);
  3125. continue;
  3126. }
  3127. // Custom weblog fields
  3128. if ( isset( $this->cfields[$row['site_id']][$item] ) AND isset( $row['field_id_'.$this->cfields[$row['site_id']][$item]] ) AND $row['field_id_'.$this->cfields[$row['site_id']][$item]] != "")
  3129. {
  3130. $entry = $this->TYPE->parse_type(
  3131. $row['field_id_'.$this->cfields[$row['site_id']][$item]],
  3132. array(
  3133. 'text_format' => $row['field_ft_'.$this->cfields[$row['site_id']][$item]],
  3134. 'html_format' => $row['weblog_html_formatting'],
  3135. 'auto_links' => $row['weblog_auto_link_urls'],
  3136. 'allow_img_url' => $row['weblog_allow_img_urls']
  3137. )
  3138. );
  3139. $tagdata = $TMPL->swap_var_single($key, $entry, $tagdata);
  3140. continue;
  3141. }
  3142. }
  3143. // Garbage collection
  3144. $val = '';
  3145. $tagdata = $TMPL->swap_var_single($key, "", $tagdata);
  3146. }
  3147. /** ----------------------------------------
  3148. /** parse {switch} variable
  3149. /** ----------------------------------------*/
  3150. if (preg_match("/^switch\s*=.+/i", $key))
  3151. {
  3152. $sparam = $FNS->assign_parameters($key);
  3153. $sw = '';
  3154. if (isset($sparam['switch']))
  3155. {
  3156. $sopt = explode("|", $sparam['switch']);
  3157. $sw = $sopt[($count + count($sopt)) % count($sopt)];
  3158. /* Old style switch parsing
  3159. /*
  3160. if (count($sopt) == 2)
  3161. {
  3162. if (isset($switch[$sparam['switch']]) AND $switch[$sparam['switch']] == $sopt['0'])
  3163. {
  3164. $switch[$sparam['switch']] = $sopt['1'];
  3165. $sw = $sopt['1'];
  3166. }
  3167. else
  3168. {
  3169. $switch[$sparam['switch']] = $sopt['0'];
  3170. $sw = $sopt['0'];
  3171. }
  3172. }
  3173. */
  3174. }
  3175. $tagdata = $TMPL->swap_var_single($key, $sw, $tagdata);
  3176. }
  3177. /** ----------------------------------------
  3178. /** parse entry date
  3179. /** ----------------------------------------*/
  3180. if (isset($entry_date[$key]))
  3181. {
  3182. foreach ($entry_date[$key] as $dvar)
  3183. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $row['entry_date'], TRUE), $val);
  3184. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3185. }
  3186. /** ----------------------------------------
  3187. /** Recent Comment Date
  3188. /** ----------------------------------------*/
  3189. if (isset($recent_comment_date[$key]))
  3190. {
  3191. if ($row['recent_comment_date'] != 0)
  3192. {
  3193. foreach ($recent_comment_date[$key] as $dvar)
  3194. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $row['recent_comment_date'], TRUE), $val);
  3195. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3196. }
  3197. else
  3198. {
  3199. $tagdata = str_replace(LD.$key.RD, "", $tagdata);
  3200. }
  3201. }
  3202. /** ----------------------------------------
  3203. /** GMT date - entry date in GMT
  3204. /** ----------------------------------------*/
  3205. if (isset($gmt_entry_date[$key]))
  3206. {
  3207. foreach ($gmt_entry_date[$key] as $dvar)
  3208. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $row['entry_date'], FALSE), $val);
  3209. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3210. }
  3211. if (isset($gmt_date[$key]))
  3212. {
  3213. foreach ($gmt_date[$key] as $dvar)
  3214. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $row['entry_date'], FALSE), $val);
  3215. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3216. }
  3217. /** ----------------------------------------
  3218. /** parse "last edit" date
  3219. /** ----------------------------------------*/
  3220. if (isset($edit_date[$key]))
  3221. {
  3222. foreach ($edit_date[$key] as $dvar)
  3223. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $LOC->timestamp_to_gmt($row['edit_date']), TRUE), $val);
  3224. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3225. }
  3226. /** ----------------------------------------
  3227. /** "last edit" date as GMT
  3228. /** ----------------------------------------*/
  3229. if (isset($gmt_edit_date[$key]))
  3230. {
  3231. foreach ($gmt_edit_date[$key] as $dvar)
  3232. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $LOC->timestamp_to_gmt($row['edit_date']), FALSE), $val);
  3233. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3234. }
  3235. /** ----------------------------------------
  3236. /** parse expiration date
  3237. /** ----------------------------------------*/
  3238. if (isset($expiration_date[$key]))
  3239. {
  3240. if ($row['expiration_date'] != 0)
  3241. {
  3242. foreach ($expiration_date[$key] as $dvar)
  3243. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $row['expiration_date'], TRUE), $val);
  3244. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3245. }
  3246. else
  3247. {
  3248. $tagdata = str_replace(LD.$key.RD, "", $tagdata);
  3249. }
  3250. }
  3251. /** ----------------------------------------
  3252. /** "week_date"
  3253. /** ----------------------------------------*/
  3254. if (isset($week_date[$key]))
  3255. {
  3256. // Subtract the number of days the entry is "into" the week to get zero (Sunday)
  3257. // If the entry date is for Sunday, and Monday is being used as the week's start day,
  3258. // then we must back things up by six days
  3259. $offset = 0;
  3260. if (strtolower($TMPL->fetch_param('start_day')) == 'monday')
  3261. {
  3262. $day_of_week = $LOC->convert_timestamp('%w', $row['entry_date'], TRUE);
  3263. if ($day_of_week == '0')
  3264. {
  3265. $offset = -518400; // back six days
  3266. }
  3267. else
  3268. {
  3269. $offset = 86400; // plus one day
  3270. }
  3271. }
  3272. $week_start_date = $row['entry_date'] - ($LOC->convert_timestamp('%w', $row['entry_date'], TRUE) * 60 * 60 * 24) + $offset;
  3273. foreach ($week_date[$key] as $dvar)
  3274. {
  3275. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $week_start_date, TRUE), $val);
  3276. }
  3277. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3278. }
  3279. /** ----------------------------------------
  3280. /** parse profile path
  3281. /** ----------------------------------------*/
  3282. if (strncmp('profile_path', $key, 12) == 0)
  3283. {
  3284. $tagdata = $TMPL->swap_var_single(
  3285. $key,
  3286. $FNS->create_url($FNS->extract_path($key).'/'.$row['member_id']),
  3287. $tagdata
  3288. );
  3289. }
  3290. /** ----------------------------------------
  3291. /** {member_search_path}
  3292. /** ----------------------------------------*/
  3293. if (strncmp('member_search_path', $key, 18) == 0)
  3294. {
  3295. $tagdata = $TMPL->swap_var_single(
  3296. $key,
  3297. $search_link.$row['member_id'],
  3298. $tagdata
  3299. );
  3300. }
  3301. /** ----------------------------------------
  3302. /** parse comment_path or trackback_path
  3303. /** ----------------------------------------*/
  3304. if (preg_match("#^(comment_path|trackback_path|entry_id_path)#", $key))
  3305. {
  3306. $path = ($FNS->extract_path($key) != '' AND $FNS->extract_path($key) != 'SITE_INDEX') ? $FNS->extract_path($key).'/'.$row['entry_id'] : $row['entry_id'];
  3307. $tagdata = $TMPL->swap_var_single(
  3308. $key,
  3309. $FNS->create_url($path, 1),
  3310. $tagdata
  3311. );
  3312. }
  3313. /** ----------------------------------------
  3314. /** parse URL title path
  3315. /** ----------------------------------------*/
  3316. if (strncmp('url_title_path', $key, 14) == 0)
  3317. {
  3318. $path = ($FNS->extract_path($key) != '' AND $FNS->extract_path($key) != 'SITE_INDEX') ? $FNS->extract_path($key).'/'.$row['url_title'] : $row['url_title'];
  3319. $tagdata = $TMPL->swap_var_single(
  3320. $key,
  3321. $FNS->create_url($path, 1),
  3322. $tagdata
  3323. );
  3324. }
  3325. /** ----------------------------------------
  3326. /** parse title permalink
  3327. /** ----------------------------------------*/
  3328. if (strncmp('title_permalink', $key, 15) == 0)
  3329. {
  3330. $path = ($FNS->extract_path($key) != '' AND $FNS->extract_path($key) != 'SITE_INDEX') ? $FNS->extract_path($key).'/'.$row['url_title'] : $row['url_title'];
  3331. $tagdata = $TMPL->swap_var_single(
  3332. $key,
  3333. $FNS->create_url($path, 1, 0),
  3334. $tagdata
  3335. );
  3336. }
  3337. /** ----------------------------------------
  3338. /** parse permalink
  3339. /** ----------------------------------------*/
  3340. if (strncmp('permalink', $key, 9) == 0)
  3341. {
  3342. $path = ($FNS->extract_path($key) != '' AND $FNS->extract_path($key) != 'SITE_INDEX') ? $FNS->extract_path($key).'/'.$row['entry_id'] : $row['entry_id'];
  3343. $tagdata = $TMPL->swap_var_single(
  3344. $key,
  3345. $FNS->create_url($path, 1, 0),
  3346. $tagdata
  3347. );
  3348. }
  3349. /** ----------------------------------------
  3350. /** {comment_auto_path}
  3351. /** ----------------------------------------*/
  3352. if ($key == "comment_auto_path")
  3353. {
  3354. $path = ($row['comment_url'] == '') ? $row['blog_url'] : $row['comment_url'];
  3355. $tagdata = $TMPL->swap_var_single($key, $path, $tagdata);
  3356. }
  3357. /** ----------------------------------------
  3358. /** {comment_url_title_auto_path}
  3359. /** ----------------------------------------*/
  3360. if ($key == "comment_url_title_auto_path")
  3361. {
  3362. $path = ($row['comment_url'] == '') ? $row['blog_url'] : $row['comment_url'];
  3363. $tagdata = $TMPL->swap_var_single(
  3364. $key,
  3365. $path.$row['url_title'].'/',
  3366. $tagdata
  3367. );
  3368. }
  3369. /** ----------------------------------------
  3370. /** {comment_entry_id_auto_path}
  3371. /** ----------------------------------------*/
  3372. if ($key == "comment_entry_id_auto_path")
  3373. {
  3374. $path = ($row['comment_url'] == '') ? $row['blog_url'] : $row['comment_url'];
  3375. $tagdata = $TMPL->swap_var_single(
  3376. $key,
  3377. $path.$row['entry_id'].'/',
  3378. $tagdata
  3379. );
  3380. }
  3381. /** ----------------------------------------
  3382. /** {author}
  3383. /** ----------------------------------------*/
  3384. if ($key == "author")
  3385. {
  3386. $tagdata = $TMPL->swap_var_single($val, ($row['screen_name'] != '') ? $row['screen_name'] : $row['username'], $tagdata);
  3387. }
  3388. /** ----------------------------------------
  3389. /** {weblog}
  3390. /** ----------------------------------------*/
  3391. if ($key == "weblog")
  3392. {
  3393. $tagdata = $TMPL->swap_var_single($val, $row['blog_title'], $tagdata);
  3394. }
  3395. /** ----------------------------------------
  3396. /** {weblog_short_name}
  3397. /** ----------------------------------------*/
  3398. if ($key == "weblog_short_name")
  3399. {
  3400. $tagdata = $TMPL->swap_var_single($val, $row['blog_name'], $tagdata);
  3401. }
  3402. /** ----------------------------------------
  3403. /** {relative_date}
  3404. /** ----------------------------------------*/
  3405. if ($key == "relative_date")
  3406. {
  3407. $tagdata = $TMPL->swap_var_single($val, $LOC->format_timespan($LOC->now - $row['entry_date']), $tagdata);
  3408. }
  3409. /** ----------------------------------------
  3410. /** {trimmed_url} - used by Atom feeds
  3411. /** ----------------------------------------*/
  3412. if ($key == "trimmed_url")
  3413. {
  3414. $blog_url = (isset($row['blog_url']) AND $row['blog_url'] != '') ? $row['blog_url'] : '';
  3415. $blog_url = str_replace(array('http://','www.'), '', $blog_url);
  3416. $xe = explode("/", $blog_url);
  3417. $blog_url = current($xe);
  3418. $tagdata = $TMPL->swap_var_single($val, $blog_url, $tagdata);
  3419. }
  3420. /** ----------------------------------------
  3421. /** {relative_url} - used by Atom feeds
  3422. /** ----------------------------------------*/
  3423. if ($key == "relative_url")
  3424. {
  3425. $blog_url = (isset($row['blog_url']) AND $row['blog_url'] != '') ? $row['blog_url'] : '';
  3426. $blog_url = str_replace('http://', '', $blog_url);
  3427. if ($x = strpos($blog_url, "/"))
  3428. {
  3429. $blog_url = substr($blog_url, $x + 1);
  3430. }
  3431. $blog_url = rtrim($blog_url, '/');
  3432. $tagdata = $TMPL->swap_var_single($val, $blog_url, $tagdata);
  3433. }
  3434. /** ----------------------------------------
  3435. /** {url_or_email}
  3436. /** ----------------------------------------*/
  3437. if ($key == "url_or_email")
  3438. {
  3439. $tagdata = $TMPL->swap_var_single($val, ($row['url'] != '') ? $row['url'] : $row['email'], $tagdata);
  3440. }
  3441. /** ----------------------------------------
  3442. /** {url_or_email_as_author}
  3443. /** ----------------------------------------*/
  3444. if ($key == "url_or_email_as_author")
  3445. {
  3446. $name = ($row['screen_name'] != '') ? $row['screen_name'] : $row['username'];
  3447. if ($row['url'] != '')
  3448. {
  3449. $tagdata = $TMPL->swap_var_single($val, "<a href=\"".$row['url']."\">".$name."</a>", $tagdata);
  3450. }
  3451. else
  3452. {
  3453. $tagdata = $TMPL->swap_var_single($val, $this->TYPE->encode_email($row['email'], $name), $tagdata);
  3454. }
  3455. }
  3456. /** ----------------------------------------
  3457. /** {url_or_email_as_link}
  3458. /** ----------------------------------------*/
  3459. if ($key == "url_or_email_as_link")
  3460. {
  3461. if ($row['url'] != '')
  3462. {
  3463. $tagdata = $TMPL->swap_var_single($val, "<a href=\"".$row['url']."\">".$row['url']."</a>", $tagdata);
  3464. }
  3465. else
  3466. {
  3467. $tagdata = $TMPL->swap_var_single($val, $this->TYPE->encode_email($row['email']), $tagdata);
  3468. }
  3469. }
  3470. /** ----------------------------------------
  3471. /** parse {comment_tb_total}
  3472. /** ----------------------------------------*/
  3473. if ($key == 'comment_tb_total')
  3474. {
  3475. $tagdata = $TMPL->swap_var_single($val, ($row['comment_total'] + $row['trackback_total']), $tagdata);
  3476. }
  3477. /** ----------------------------------------
  3478. /** {signature}
  3479. /** ----------------------------------------*/
  3480. if ($key == "signature")
  3481. {
  3482. if ($SESS->userdata('display_signatures') == 'n' OR $row['signature'] == '' OR $SESS->userdata('display_signatures') == 'n')
  3483. {
  3484. $tagdata = $TMPL->swap_var_single($key, '', $tagdata);
  3485. }
  3486. else
  3487. {
  3488. $tagdata = $TMPL->swap_var_single($key,
  3489. $this->TYPE->parse_type($row['signature'], array(
  3490. 'text_format' => 'xhtml',
  3491. 'html_format' => 'safe',
  3492. 'auto_links' => 'y',
  3493. 'allow_img_url' => $PREFS->ini('sig_allow_img_hotlink')
  3494. )
  3495. ), $tagdata);
  3496. }
  3497. }
  3498. if ($key == "signature_image_url")
  3499. {
  3500. if ($SESS->userdata('display_signatures') == 'n' OR $row['sig_img_filename'] == '' OR $SESS->userdata('display_signatures') == 'n')
  3501. {
  3502. $tagdata = $TMPL->swap_var_single($key, '', $tagdata);
  3503. $tagdata = $TMPL->swap_var_single('signature_image_width', '', $tagdata);
  3504. $tagdata = $TMPL->swap_var_single('signature_image_height', '', $tagdata);
  3505. }
  3506. else
  3507. {
  3508. $tagdata = $TMPL->swap_var_single($key, $PREFS->ini('sig_img_url', TRUE).$row['sig_img_filename'], $tagdata);
  3509. $tagdata = $TMPL->swap_var_single('signature_image_width', $row['sig_img_width'], $tagdata);
  3510. $tagdata = $TMPL->swap_var_single('signature_image_height', $row['sig_img_height'], $tagdata);
  3511. }
  3512. }
  3513. if ($key == "avatar_url")
  3514. {
  3515. if ($SESS->userdata('display_avatars') == 'n' OR $row['avatar_filename'] == '' OR $SESS->userdata('display_avatars') == 'n')
  3516. {
  3517. $tagdata = $TMPL->swap_var_single($key, '', $tagdata);
  3518. $tagdata = $TMPL->swap_var_single('avatar_image_width', '', $tagdata);
  3519. $tagdata = $TMPL->swap_var_single('avatar_image_height', '', $tagdata);
  3520. }
  3521. else
  3522. {
  3523. $tagdata = $TMPL->swap_var_single($key, $PREFS->ini('avatar_url', 1).$row['avatar_filename'], $tagdata);
  3524. $tagdata = $TMPL->swap_var_single('avatar_image_width', $row['avatar_width'], $tagdata);
  3525. $tagdata = $TMPL->swap_var_single('avatar_image_height', $row['avatar_height'], $tagdata);
  3526. }
  3527. }
  3528. if ($key == "photo_url")
  3529. {
  3530. if ($SESS->userdata('display_photos') == 'n' OR $row['photo_filename'] == '' OR $SESS->userdata('display_photos') == 'n')
  3531. {
  3532. $tagdata = $TMPL->swap_var_single($key, '', $tagdata);
  3533. $tagdata = $TMPL->swap_var_single('photo_image_width', '', $tagdata);
  3534. $tagdata = $TMPL->swap_var_single('photo_image_height', '', $tagdata);
  3535. }
  3536. else
  3537. {
  3538. $tagdata = $TMPL->swap_var_single($key, $PREFS->ini('photo_url', 1).$row['photo_filename'], $tagdata);
  3539. $tagdata = $TMPL->swap_var_single('photo_image_width', $row['photo_width'], $tagdata);
  3540. $tagdata = $TMPL->swap_var_single('photo_image_height', $row['photo_height'], $tagdata);
  3541. }
  3542. }
  3543. /** ----------------------------------------
  3544. /** parse {title}
  3545. /** ----------------------------------------*/
  3546. if ($key == 'title')
  3547. {
  3548. $row['title'] = str_replace(array('{', '}'), array('&#123;', '&#125;'), $row['title']);
  3549. $tagdata = $TMPL->swap_var_single($val, $this->TYPE->format_characters($row['title']), $tagdata);
  3550. }
  3551. /** ----------------------------------------
  3552. /** parse basic fields (username, screen_name, etc.)
  3553. /** ----------------------------------------*/
  3554. if (isset($row[$val]))
  3555. {
  3556. $tagdata = $TMPL->swap_var_single($val, $row[$val], $tagdata);
  3557. }
  3558. /** ----------------------------------------
  3559. /** parse custom date fields
  3560. /** ----------------------------------------*/
  3561. if (isset($custom_date_fields[$key]) && isset($this->dfields[$row['site_id']]))
  3562. {
  3563. foreach ($this->dfields[$row['site_id']] as $dkey => $dval)
  3564. {
  3565. if (! preg_match("/^".preg_quote($dkey, '/')."\s/", $key))
  3566. continue;
  3567. if ($row['field_id_'.$dval] == 0 OR $row['field_id_'.$dval] == '')
  3568. {
  3569. $tagdata = $TMPL->swap_var_single($key, '', $tagdata);
  3570. continue;
  3571. }
  3572. // use a temporary variable in case the custom date variable is used
  3573. // multiple times with different formats; prevents localization from
  3574. // occurring multiple times on the same value
  3575. $temp_val = $row['field_id_'.$dval];
  3576. $localize = TRUE;
  3577. if (isset($row['field_dt_'.$dval]) AND $row['field_dt_'.$dval] != '')
  3578. {
  3579. $localize = TRUE;
  3580. if ($row['field_dt_'.$dval] != '')
  3581. {
  3582. $temp_val = $LOC->offset_entry_dst($temp_val, $row['dst_enabled']);
  3583. $temp_val = $LOC->simpl_offset($temp_val, $row['field_dt_'.$dval]);
  3584. $localize = FALSE;
  3585. }
  3586. }
  3587. foreach ($custom_date_fields[$key] as $dvar)
  3588. $val = str_replace($dvar, $LOC->convert_timestamp($dvar, $temp_val, $localize), $val);
  3589. $tagdata = $TMPL->swap_var_single($key, $val, $tagdata);
  3590. }
  3591. }
  3592. /** ----------------------------------------
  3593. /** Assign Related Entry IDs
  3594. /** ----------------------------------------*/
  3595. // When an entry has related entries within it, since the related entry ID
  3596. // is stored in the custom field itself we need to pull it out and set it
  3597. // aside so that when the related stuff is parsed out we'll have it.
  3598. // We also need to modify the marker in the template so that we can replace
  3599. // it with the right entry
  3600. if (isset($this->rfields[$row['site_id']][$val]))
  3601. {
  3602. // No relationship? Ditch the marker
  3603. if ( ! isset($row['field_id_'.$this->cfields[$row['site_id']][$val]]) OR
  3604. $row['field_id_'.$this->cfields[$row['site_id']][$val]] == 0 OR
  3605. ! preg_match_all("/".LD."REL\[".$val."\](.+?)REL".RD."/", $tagdata, $match)
  3606. )
  3607. {
  3608. // replace the marker with the {if no_related_entries} content
  3609. preg_match_all("/".LD."REL\[".$val."\](.+?)REL".RD."/", $tagdata, $matches);
  3610. foreach ($matches['1'] as $match)
  3611. {
  3612. $tagdata = preg_replace("/".LD."REL\[".$val."\](.+?)REL".RD."/", $TMPL->related_data[$match]['no_rel_content'], $tagdata);
  3613. }
  3614. }
  3615. else
  3616. {
  3617. for ($j = 0; $j < count($match['1']); $j++)
  3618. {
  3619. $this->related_entries[] = $row['field_id_'.$this->cfields[$row['site_id']][$val]].'_'.$match['1'][$j];
  3620. $tagdata = preg_replace("/".LD."REL\[".$val."\](.+?)REL".RD."/", LD."REL[".$row['field_id_'.$this->cfields[$row['site_id']][$val]]."][".$val."]\\1REL".RD, $tagdata);
  3621. }
  3622. $tagdata = $TMPL->swap_var_single($val, '', $tagdata);
  3623. }
  3624. }
  3625. // Clean up any unparsed relationship fields
  3626. if (isset($this->rfields[$row['site_id']]) && sizeof($this->rfields[$row['site_id']]) > 0)
  3627. {
  3628. $tagdata = preg_replace("/".LD."REL\[".preg_quote($val,'/')."\](.+?)REL".RD."/", "", $tagdata);
  3629. }
  3630. /** ----------------------------------------
  3631. /** parse custom weblog fields
  3632. /** ----------------------------------------*/
  3633. if (isset($this->cfields[$row['site_id']][$val]))
  3634. {
  3635. if ( ! isset($row['field_id_'.$this->cfields[$row['site_id']][$val]]) OR $row['field_id_'.$this->cfields[$row['site_id']][$val]] == '')
  3636. {
  3637. $entry = '';
  3638. }
  3639. else
  3640. {
  3641. // This line of code fixes a very odd bug that happens when you place EE tags in weblog entries
  3642. // For some inexplicable reason, we have to convert the tag to entities before sending it to
  3643. // the typography class below or else it tries to get parsed as a tag. What is totally baffling
  3644. // is that the typography class converts tags, so we shouldn't have to do it here. I can't
  3645. // figure out a solution, however.
  3646. $entry = $this->TYPE->parse_type(
  3647. $REGX->encode_ee_tags($row['field_id_'.$this->cfields[$row['site_id']][$val]]),
  3648. array(
  3649. 'text_format' => $row['field_ft_'.$this->cfields[$row['site_id']][$val]],
  3650. 'html_format' => $row['weblog_html_formatting'],
  3651. 'auto_links' => $row['weblog_auto_link_urls'],
  3652. 'allow_img_url' => $row['weblog_allow_img_urls']
  3653. )
  3654. );
  3655. }
  3656. // prevent accidental parsing of other weblog variables in custom field data
  3657. if (strpos($entry, '{') !== FALSE)
  3658. {
  3659. $tagdata = $TMPL->swap_var_single($val, str_replace(array('{', '}'), array('60ba4b2daa4ed4', 'c2b7df6201fdd3'), $entry), $tagdata);
  3660. }
  3661. else
  3662. {
  3663. $tagdata = $TMPL->swap_var_single($val, $entry, $tagdata);
  3664. }
  3665. }
  3666. /** ----------------------------------------
  3667. /** parse custom member fields
  3668. /** ----------------------------------------*/
  3669. if ( isset( $this->mfields[$val]) AND isset($row['m_field_id_'.$this->mfields[$val]['0']]))
  3670. {
  3671. $tagdata = $TMPL->swap_var_single(
  3672. $val,
  3673. $this->TYPE->parse_type(
  3674. $row['m_field_id_'.$this->mfields[$val]['0']],
  3675. array(
  3676. 'text_format' => $this->mfields[$val]['1'],
  3677. 'html_format' => 'safe',
  3678. 'auto_links' => 'y',
  3679. 'allow_img_url' => 'n'
  3680. )
  3681. ),
  3682. $tagdata
  3683. );
  3684. }
  3685. }
  3686. // END SINGLE VARIABLES
  3687. // do we need to replace any curly braces that we protected in custom fields?
  3688. if (strpos($tagdata, '60ba4b2daa4ed4') !== FALSE)
  3689. {
  3690. $tagdata = str_replace(array('60ba4b2daa4ed4', 'c2b7df6201fdd3'), array('{', '}'), $tagdata);
  3691. }
  3692. /** ----------------------------------------
  3693. /** Compile trackback data
  3694. /** ----------------------------------------*/
  3695. if ($this->display_tb_rdf == TRUE)
  3696. {
  3697. $categories = '';
  3698. if (isset($this->categories[$row['entry_id']]))
  3699. {
  3700. if (is_array($this->categories[$row['entry_id']]))
  3701. {
  3702. foreach ($this->categories[$row['entry_id']] as $k => $v)
  3703. {
  3704. $categories .= $REGX->xml_convert($v['2']).',';
  3705. }
  3706. $categories = substr($categories, 0, -1);
  3707. }
  3708. }
  3709. /** ----------------------------------------
  3710. /** Build Trackback RDF
  3711. /** ----------------------------------------*/
  3712. if ($row['trackback_use_captcha'] == 'y' AND $tb_captcha == TRUE)
  3713. {
  3714. $this->tb_captcha_hash = $FNS->random('alpha', 8);
  3715. $DB->query("INSERT INTO exp_captcha (date, ip_address, word) VALUES (UNIX_TIMESTAMP(), '".$IN->IP."', '".$this->tb_captcha_hash."')");
  3716. $this->tb_captcha_hash .= '/';
  3717. $tb_captcha = FALSE;
  3718. }
  3719. $ret_url = ($row['tb_return_url'] == '') ? $row['blog_url'] : $row['tb_return_url'];
  3720. $this->TYPE->encode_email = FALSE;
  3721. $tb_desc = $this->TYPE->parse_type(
  3722. $FNS->char_limiter((isset($row['field_id_'.$row['trackback_field']])) ? $row['field_id_'.$row['trackback_field']] : ''),
  3723. array(
  3724. 'text_format' => 'none',
  3725. 'html_format' => 'none',
  3726. 'auto_links' => 'n',
  3727. 'allow_img_url' => 'y'
  3728. )
  3729. );
  3730. $row['title'] = str_replace(array('{', '}'), array('&#123;', '&#125;'), $row['title']);
  3731. $identifier = ($row['trackback_use_url_title'] == 'y') ? $row['url_title'] : $row['entry_id'];
  3732. $TB = array(
  3733. 'about' => $FNS->remove_double_slashes($ret_url.'/'.$identifier.'/'),
  3734. 'ping' => $FNS->fetch_site_index(1, 0).'trackback/'.$row['entry_id'].'/'.$this->tb_captcha_hash,
  3735. 'title' => $REGX->xml_convert($row['title']),
  3736. 'identifier' => $FNS->remove_double_slashes($ret_url.'/'.$identifier.'/'),
  3737. 'subject' => $REGX->xml_convert($categories),
  3738. 'description' => $REGX->xml_convert($tb_desc),
  3739. 'creator' => $REGX->xml_convert(($row['screen_name'] != '') ? $row['screen_name'] : $row['username']),
  3740. 'date' => $LOC->set_human_time($row['entry_date'], 0, 1).' GMT'
  3741. );
  3742. $tagdata .= $this->trackback_rdf($TB);
  3743. $this->display_tb_rdf = FALSE;
  3744. }
  3745. // -------------------------------------------
  3746. // 'weblog_entries_tagdata_end' hook.
  3747. // - Take the final results of an entry's parsing and do what you wish
  3748. //
  3749. if ($EXT->active_hook('weblog_entries_tagdata_end') === TRUE)
  3750. {
  3751. $tagdata = $EXT->call_extension('weblog_entries_tagdata_end', $tagdata, $row, $this);
  3752. if ($EXT->end_script === TRUE) return $tagdata;
  3753. }
  3754. //
  3755. // -------------------------------------------
  3756. $this->return_data .= $tagdata;
  3757. }
  3758. // END FOREACH LOOP
  3759. // Kill multi_field variable
  3760. $this->return_data = preg_replace("/".LD."multi_field\=[\"'](.+?)[\"']".RD."/s", "", $this->return_data);
  3761. // Do we have backspacing?
  3762. // This can only be used when RDF data is not present.
  3763. if ($back = $TMPL->fetch_param('backspace') AND $this->display_tb_rdf != TRUE)
  3764. {
  3765. if (is_numeric($back))
  3766. {
  3767. $this->return_data = rtrim(str_replace("&#47;", "/", $this->return_data));
  3768. $this->return_data = substr($this->return_data, 0, - $back);
  3769. $this->return_data = str_replace("/", "&#47;", $this->return_data);
  3770. }
  3771. }
  3772. }
  3773. /* END */
  3774. /** ----------------------------------------
  3775. /** Weblog Info Tag
  3776. /** ----------------------------------------*/
  3777. function info()
  3778. {
  3779. global $TMPL, $DB, $LANG;
  3780. if ( ! $blog_name = $TMPL->fetch_param('weblog'))
  3781. {
  3782. return '';
  3783. }
  3784. if (count($TMPL->var_single) == 0)
  3785. {
  3786. return '';
  3787. }
  3788. $params = array(
  3789. 'blog_title',
  3790. 'blog_url',
  3791. 'blog_description',
  3792. 'blog_lang',
  3793. 'blog_encoding'
  3794. );
  3795. $q = '';
  3796. foreach ($TMPL->var_single as $val)
  3797. {
  3798. if (in_array($val, $params))
  3799. {
  3800. $q .= $val.',';
  3801. }
  3802. }
  3803. $q = substr($q, 0, -1);
  3804. if ($q == '')
  3805. return '';
  3806. $sql = "SELECT ".$q." FROM exp_weblogs ";
  3807. if (USER_BLOG !== FALSE)
  3808. {
  3809. $sql .= " WHERE exp_weblogs.weblog_id = '".UB_BLOG_ID."'";
  3810. }
  3811. else
  3812. {
  3813. $sql .= " WHERE exp_weblogs.is_user_blog = 'n' AND site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  3814. if ($blog_name != '')
  3815. {
  3816. $sql .= " AND blog_name = '".$DB->escape_str($blog_name)."'";
  3817. }
  3818. }
  3819. $query = $DB->query($sql);
  3820. if ($query->num_rows != 1)
  3821. {
  3822. return '';
  3823. }
  3824. foreach ($query->row as $key => $val)
  3825. {
  3826. $TMPL->tagdata = str_replace(LD.$key.RD, $val, $TMPL->tagdata);
  3827. }
  3828. return $TMPL->tagdata;
  3829. }
  3830. /* END */
  3831. /** ----------------------------------------
  3832. /** Weblog Name
  3833. /** ----------------------------------------*/
  3834. function weblog_name()
  3835. {
  3836. global $TMPL, $DB, $LANG;
  3837. $blog_name = $TMPL->fetch_param('weblog');
  3838. if (isset($this->weblog_name[$blog_name]))
  3839. {
  3840. return $this->weblog_name[$blog_name];
  3841. }
  3842. $sql = "SELECT blog_title FROM exp_weblogs ";
  3843. if (USER_BLOG !== FALSE)
  3844. {
  3845. $sql .= " WHERE exp_weblogs.weblog_id = '".UB_BLOG_ID."'";
  3846. }
  3847. else
  3848. {
  3849. $sql .= " WHERE exp_weblogs.is_user_blog = 'n' AND site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  3850. if ($blog_name != '')
  3851. {
  3852. $sql .= " AND blog_name = '".$DB->escape_str($blog_name)."'";
  3853. }
  3854. }
  3855. $query = $DB->query($sql);
  3856. if ($query->num_rows == 1)
  3857. {
  3858. $this->weblog_name[$blog_name] = $query->row['blog_title'];
  3859. return $query->row['blog_title'];
  3860. }
  3861. else
  3862. {
  3863. return '';
  3864. }
  3865. }
  3866. /* END */
  3867. /** ----------------------------------------
  3868. /** Weblog Category Totals
  3869. /** ----------------------------------------*/
  3870. // Need to finish this function. It lets a simple list of cagegories
  3871. // appear along with the post total.
  3872. function category_totals()
  3873. {
  3874. $sql = "SELECT count( exp_category_posts.entry_id ) AS count,
  3875. exp_categories.cat_id,
  3876. exp_categories.cat_name
  3877. FROM exp_categories
  3878. LEFT JOIN exp_category_posts ON exp_category_posts.cat_id = exp_categories.cat_id
  3879. GROUP BY exp_categories.cat_id
  3880. ORDER BY group_id, parent_id, cat_order";
  3881. }
  3882. /* END */
  3883. /** ----------------------------------------
  3884. /** Weblog Categories
  3885. /** ----------------------------------------*/
  3886. function categories()
  3887. {
  3888. global $TMPL, $LOC, $FNS, $REGX, $DB, $LANG, $EXT;
  3889. // -------------------------------------------
  3890. // 'weblog_module_categories_start' hook.
  3891. // - Rewrite the displaying of categories, if you dare!
  3892. //
  3893. if ($EXT->active_hook('weblog_module_categories_start') === TRUE)
  3894. {
  3895. return $EXT->call_extension('weblog_module_categories_start');
  3896. }
  3897. //
  3898. // -------------------------------------------
  3899. if (USER_BLOG !== FALSE)
  3900. {
  3901. $group_id = $DB->escape_str(UB_CAT_GRP);
  3902. }
  3903. else
  3904. {
  3905. $sql = "SELECT DISTINCT cat_group FROM exp_weblogs WHERE site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  3906. if ($weblog = $TMPL->fetch_param('weblog'))
  3907. {
  3908. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('weblog'), 'blog_name');
  3909. }
  3910. $query = $DB->query($sql);
  3911. if ($query->num_rows != 1)
  3912. {
  3913. return '';
  3914. }
  3915. $group_id = $query->row['cat_group'];
  3916. if ($category_group = $TMPL->fetch_param('category_group'))
  3917. {
  3918. if (substr($category_group, 0, 4) == 'not ')
  3919. {
  3920. $x = explode('|', substr($category_group, 4));
  3921. $groups = array_diff(explode('|', $group_id), $x);
  3922. }
  3923. else
  3924. {
  3925. $x = explode('|', $category_group);
  3926. $groups = array_intersect(explode('|', $group_id), $x);
  3927. }
  3928. if (sizeof($groups) == 0)
  3929. {
  3930. return '';
  3931. }
  3932. else
  3933. {
  3934. $group_id = implode('|', $groups);
  3935. }
  3936. }
  3937. }
  3938. $parent_only = ($TMPL->fetch_param('parent_only') == 'yes') ? TRUE : FALSE;
  3939. $path = array();
  3940. if (preg_match_all("#".LD."path(=.+?)".RD."#", $TMPL->tagdata, $matches))
  3941. {
  3942. for ($i = 0; $i < count($matches['0']); $i++)
  3943. {
  3944. if ( ! isset($path[$matches['0'][$i]]))
  3945. {
  3946. $path[$matches['0'][$i]] = $FNS->create_url($FNS->extract_path($matches['1'][$i]));
  3947. }
  3948. }
  3949. }
  3950. $str = '';
  3951. if ($TMPL->fetch_param('style') == '' OR $TMPL->fetch_param('style') == 'nested')
  3952. {
  3953. $this->category_tree(
  3954. array(
  3955. 'group_id' => $group_id,
  3956. 'template' => $TMPL->tagdata,
  3957. 'path' => $path,
  3958. 'blog_array' => '',
  3959. 'parent_only' => $parent_only,
  3960. 'show_empty' => $TMPL->fetch_param('show_empty')
  3961. )
  3962. );
  3963. if (count($this->category_list) > 0)
  3964. {
  3965. $i = 0;
  3966. $id_name = ( ! $TMPL->fetch_param('id')) ? 'nav_categories' : $TMPL->fetch_param('id');
  3967. $class_name = ( ! $TMPL->fetch_param('class')) ? 'nav_categories' : $TMPL->fetch_param('class');
  3968. $this->category_list['0'] = '<ul id="'.$id_name.'" class="'.$class_name.'">'."\n";
  3969. foreach ($this->category_list as $val)
  3970. {
  3971. $str .= $val;
  3972. }
  3973. }
  3974. }
  3975. else
  3976. {
  3977. // fetch category field names and id's
  3978. if ($this->enable['category_fields'] === TRUE)
  3979. {
  3980. $query = $DB->query("SELECT field_id, field_name FROM exp_category_fields
  3981. WHERE site_id IN ('".implode("','", $TMPL->site_ids)."')
  3982. AND group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."')");
  3983. if ($query->num_rows > 0)
  3984. {
  3985. foreach ($query->result as $row)
  3986. {
  3987. $this->catfields[] = array('field_name' => $row['field_name'], 'field_id' => $row['field_id']);
  3988. }
  3989. }
  3990. $field_sqla = ", cg.field_html_formatting, fd.* ";
  3991. $field_sqlb = " LEFT JOIN exp_category_field_data AS fd ON fd.cat_id = c.cat_id
  3992. LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id";
  3993. }
  3994. else
  3995. {
  3996. $field_sqla = '';
  3997. $field_sqlb = '';
  3998. }
  3999. $show_empty = $TMPL->fetch_param('show_empty');
  4000. if ($show_empty == 'no')
  4001. {
  4002. // First we'll grab all category ID numbers
  4003. $query = $DB->query("SELECT cat_id, parent_id
  4004. FROM exp_categories
  4005. WHERE group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."')
  4006. ORDER BY group_id, parent_id, cat_order");
  4007. $all = array();
  4008. // No categories exist? Let's go home..
  4009. if ($query->num_rows == 0)
  4010. return false;
  4011. foreach($query->result as $row)
  4012. {
  4013. $all[$row['cat_id']] = $row['parent_id'];
  4014. }
  4015. // Next we'l grab only the assigned categories
  4016. $sql = "SELECT DISTINCT(exp_categories.cat_id), parent_id FROM exp_categories
  4017. LEFT JOIN exp_category_posts ON exp_categories.cat_id = exp_category_posts.cat_id
  4018. LEFT JOIN exp_weblog_titles ON exp_category_posts.entry_id = exp_weblog_titles.entry_id
  4019. WHERE group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."')
  4020. AND exp_weblog_titles.site_id IN ('".implode("','", $TMPL->site_ids)."')
  4021. AND exp_category_posts.cat_id IS NOT NULL ";
  4022. if (($status = $TMPL->fetch_param('status')) !== FALSE)
  4023. {
  4024. $status = str_replace(array('Open', 'Closed'), array('open', 'closed'), $status);
  4025. $sql .= $FNS->sql_andor_string($status, 'exp_weblog_titles.status');
  4026. }
  4027. else
  4028. {
  4029. $sql .= "AND exp_weblog_titles.status != 'closed' ";
  4030. }
  4031. /** ----------------------------------------------
  4032. /** We only select entries that have not expired
  4033. /** ----------------------------------------------*/
  4034. $timestamp = ($TMPL->cache_timestamp != '') ? $LOC->set_gmt($TMPL->cache_timestamp) : $LOC->now;
  4035. if ($TMPL->fetch_param('show_future_entries') != 'yes')
  4036. {
  4037. $sql .= " AND exp_weblog_titles.entry_date < ".$timestamp." ";
  4038. }
  4039. if ($TMPL->fetch_param('show_expired') != 'yes')
  4040. {
  4041. $sql .= " AND (exp_weblog_titles.expiration_date = 0 OR exp_weblog_titles.expiration_date > ".$timestamp.") ";
  4042. }
  4043. if ($parent_only === TRUE)
  4044. {
  4045. $sql .= " AND parent_id = 0";
  4046. }
  4047. $sql .= " ORDER BY group_id, parent_id, cat_order";
  4048. $query = $DB->query($sql);
  4049. if ($query->num_rows == 0)
  4050. return false;
  4051. // All the magic happens here, baby!!
  4052. foreach($query->result as $row)
  4053. {
  4054. if ($row['parent_id'] != 0)
  4055. {
  4056. $this->find_parent($row['parent_id'], $all);
  4057. }
  4058. $this->cat_full_array[] = $row['cat_id'];
  4059. }
  4060. $this->cat_full_array = array_unique($this->cat_full_array);
  4061. $sql = "SELECT c.cat_id, c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description {$field_sqla}
  4062. FROM exp_categories AS c
  4063. {$field_sqlb}
  4064. WHERE c.cat_id IN (";
  4065. foreach ($this->cat_full_array as $val)
  4066. {
  4067. $sql .= $val.',';
  4068. }
  4069. $sql = substr($sql, 0, -1).')';
  4070. $sql .= " ORDER BY c.group_id, c.parent_id, c.cat_order";
  4071. $query = $DB->query($sql);
  4072. if ($query->num_rows == 0)
  4073. return false;
  4074. }
  4075. else
  4076. {
  4077. $sql = "SELECT c.cat_name, c.cat_url_title, c.cat_image, c.cat_description, c.cat_id, c.parent_id {$field_sqla}
  4078. FROM exp_categories AS c
  4079. {$field_sqlb}
  4080. WHERE c.group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."') ";
  4081. if ($parent_only === TRUE)
  4082. {
  4083. $sql .= " AND c.parent_id = 0";
  4084. }
  4085. $sql .= " ORDER BY c.group_id, c.parent_id, c.cat_order";
  4086. $query = $DB->query($sql);
  4087. if ($query->num_rows == 0)
  4088. {
  4089. return '';
  4090. }
  4091. }
  4092. // Here we check the show parameter to see if we have any
  4093. // categories we should be ignoring or only a certain group of
  4094. // categories that we should be showing. By doing this here before
  4095. // all of the nested processing we should keep out all but the
  4096. // request categories while also not having a problem with having a
  4097. // child but not a parent. As we all know, categories are not asexual.
  4098. if ($TMPL->fetch_param('show') !== FALSE)
  4099. {
  4100. if (strncmp('not ', $TMPL->fetch_param('show'), 4) == 0)
  4101. {
  4102. $not_these = explode('|', trim(substr($TMPL->fetch_param('show'), 3)));
  4103. }
  4104. else
  4105. {
  4106. $these = explode('|', trim($TMPL->fetch_param('show')));
  4107. }
  4108. }
  4109. foreach($query->result as $row)
  4110. {
  4111. if (isset($not_these) && in_array($row['cat_id'], $not_these))
  4112. {
  4113. continue;
  4114. }
  4115. elseif(isset($these) && ! in_array($row['cat_id'], $these))
  4116. {
  4117. continue;
  4118. }
  4119. $this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], '1', $row['cat_name'], $row['cat_description'], $row['cat_image'], $row['cat_url_title']);
  4120. foreach ($row as $key => $val)
  4121. {
  4122. if (strpos($key, 'field') !== FALSE)
  4123. {
  4124. $this->temp_array[$row['cat_id']][$key] = $val;
  4125. }
  4126. }
  4127. }
  4128. foreach($this->temp_array as $key => $val)
  4129. {
  4130. if (0 == $val['1'])
  4131. {
  4132. $this->cat_array[] = $val;
  4133. $this->process_subcategories($key);
  4134. }
  4135. }
  4136. unset($this->temp_array);
  4137. if ( ! class_exists('Typography'))
  4138. {
  4139. require PATH_CORE.'core.typography'.EXT;
  4140. }
  4141. $this->TYPE = new Typography;
  4142. $this->TYPE->convert_curly = FALSE;
  4143. $this->category_count = 0;
  4144. $total_results = count($this->cat_array);
  4145. foreach ($this->cat_array as $key => $val)
  4146. {
  4147. $chunk = $TMPL->tagdata;
  4148. $cat_vars = array('category_name' => $val['3'],
  4149. 'category_url_title' => $val['6'],
  4150. 'category_description' => $val['4'],
  4151. 'category_image' => $val['5'],
  4152. 'category_id' => $val['0'],
  4153. 'parent_id' => $val['1']
  4154. );
  4155. // add custom fields for conditionals prep
  4156. foreach ($this->catfields as $v)
  4157. {
  4158. $cat_vars[$v['field_name']] = ( ! isset($val['field_id_'.$v['field_id']])) ? '' : $val['field_id_'.$v['field_id']];
  4159. }
  4160. $cat_vars['count'] = ++$this->category_count;
  4161. $cat_vars['total_results'] = $total_results;
  4162. $chunk = $FNS->prep_conditionals($chunk, $cat_vars);
  4163. $chunk = str_replace(array(LD.'category_name'.RD,
  4164. LD.'category_url_title'.RD,
  4165. LD.'category_description'.RD,
  4166. LD.'category_image'.RD,
  4167. LD.'category_id'.RD,
  4168. LD.'parent_id'.RD),
  4169. array($val['3'],
  4170. $val['6'],
  4171. $val['4'],
  4172. $val['5'],
  4173. $val['0'],
  4174. $val['1']),
  4175. $chunk);
  4176. foreach($path as $k => $v)
  4177. {
  4178. if ($this->use_category_names == TRUE)
  4179. {
  4180. $chunk = str_replace($k, $FNS->remove_double_slashes($v.'/'.$this->reserved_cat_segment.'/'.$val['6'].'/'), $chunk);
  4181. }
  4182. else
  4183. {
  4184. $chunk = str_replace($k, $FNS->remove_double_slashes($v.'/C'.$val['0'].'/'), $chunk);
  4185. }
  4186. }
  4187. // parse custom fields
  4188. foreach($this->catfields as $cv)
  4189. {
  4190. if (isset($val['field_id_'.$cv['field_id']]) AND $val['field_id_'.$cv['field_id']] != '')
  4191. {
  4192. $field_content = $this->TYPE->parse_type($val['field_id_'.$cv['field_id']],
  4193. array(
  4194. 'text_format' => $val['field_ft_'.$cv['field_id']],
  4195. 'html_format' => $val['field_html_formatting'],
  4196. 'auto_links' => 'n',
  4197. 'allow_img_url' => 'y'
  4198. )
  4199. );
  4200. $chunk = str_replace(LD.$cv['field_name'].RD, $field_content, $chunk);
  4201. }
  4202. else
  4203. {
  4204. // garbage collection
  4205. $chunk = str_replace(LD.$cv['field_name'].RD, '', $chunk);
  4206. }
  4207. }
  4208. /** --------------------------------
  4209. /** {count}
  4210. /** --------------------------------*/
  4211. if (strpos($chunk, LD.'count'.RD) !== FALSE)
  4212. {
  4213. $chunk = str_replace(LD.'count'.RD, $this->category_count, $chunk);
  4214. }
  4215. /** --------------------------------
  4216. /** {total_results}
  4217. /** --------------------------------*/
  4218. if (strpos($chunk, LD.'total_results'.RD) !== FALSE)
  4219. {
  4220. $chunk = str_replace(LD.'total_results'.RD, $total_results, $chunk);
  4221. }
  4222. $str .= $chunk;
  4223. }
  4224. if ($TMPL->fetch_param('backspace'))
  4225. {
  4226. $str = rtrim(str_replace("&#47;", "/", $str));
  4227. $str = substr($str, 0, - $TMPL->fetch_param('backspace'));
  4228. $str = str_replace("/", "&#47;", $str);
  4229. }
  4230. }
  4231. return $str;
  4232. }
  4233. /* END */
  4234. /** --------------------------------
  4235. /** Process Subcategories
  4236. /** --------------------------------*/
  4237. function process_subcategories($parent_id)
  4238. {
  4239. foreach($this->temp_array as $key => $val)
  4240. {
  4241. if ($parent_id == $val['1'])
  4242. {
  4243. $this->cat_array[] = $val;
  4244. $this->process_subcategories($key);
  4245. }
  4246. }
  4247. }
  4248. /* END */
  4249. /** ----------------------------------------
  4250. /** Category archives
  4251. /** ----------------------------------------*/
  4252. function category_archive()
  4253. {
  4254. global $TMPL, $LOC, $FNS, $REGX, $DB, $LANG;
  4255. if (USER_BLOG !== FALSE)
  4256. {
  4257. $group_id = $DB->escape_str(UB_CAT_GRP);
  4258. $weblog_id = $DB->escape_str(UB_BLOG_ID);
  4259. }
  4260. else
  4261. {
  4262. $sql = "SELECT DISTINCT cat_group, weblog_id FROM exp_weblogs WHERE site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  4263. if ($weblog = $TMPL->fetch_param('weblog'))
  4264. {
  4265. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('weblog'), 'blog_name');
  4266. }
  4267. $query = $DB->query($sql);
  4268. if ($query->num_rows != 1)
  4269. {
  4270. return '';
  4271. }
  4272. $group_id = $query->row['cat_group'];
  4273. $weblog_id = $query->row['weblog_id'];
  4274. }
  4275. $sql = "SELECT exp_category_posts.cat_id, exp_weblog_titles.entry_id, exp_weblog_titles.title, exp_weblog_titles.url_title, exp_weblog_titles.entry_date
  4276. FROM exp_weblog_titles, exp_category_posts
  4277. WHERE weblog_id = '$weblog_id'
  4278. AND exp_weblog_titles.entry_id = exp_category_posts.entry_id ";
  4279. $timestamp = ($TMPL->cache_timestamp != '') ? $LOC->set_gmt($TMPL->cache_timestamp) : $LOC->now;
  4280. if ($TMPL->fetch_param('show_future_entries') != 'yes')
  4281. {
  4282. $sql .= "AND exp_weblog_titles.entry_date < ".$timestamp." ";
  4283. }
  4284. if ($TMPL->fetch_param('show_expired') != 'yes')
  4285. {
  4286. $sql .= "AND (exp_weblog_titles.expiration_date = 0 OR exp_weblog_titles.expiration_date > ".$timestamp.") ";
  4287. }
  4288. $sql .= "AND exp_weblog_titles.status != 'closed' ";
  4289. if ($status = $TMPL->fetch_param('status'))
  4290. {
  4291. $status = str_replace('Open', 'open', $status);
  4292. $status = str_replace('Closed', 'closed', $status);
  4293. $sql .= $FNS->sql_andor_string($status, 'exp_weblog_titles.status');
  4294. }
  4295. else
  4296. {
  4297. $sql .= "AND exp_weblog_titles.status = 'open' ";
  4298. }
  4299. if ($TMPL->fetch_param('show') !== FALSE)
  4300. {
  4301. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('show'), 'exp_category_posts.cat_id').' ';
  4302. }
  4303. $orderby = $TMPL->fetch_param('orderby');
  4304. switch ($orderby)
  4305. {
  4306. case 'date' : $sql .= "ORDER BY exp_weblog_titles.entry_date";
  4307. break;
  4308. case 'expiration_date' : $sql .= "ORDER BY exp_weblog_titles.expiration_date";
  4309. break;
  4310. case 'title' : $sql .= "ORDER BY exp_weblog_titles.title";
  4311. break;
  4312. case 'comment_total' : $sql .= "ORDER BY exp_weblog_titles.entry_date";
  4313. break;
  4314. case 'most_recent_comment' : $sql .= "ORDER BY exp_weblog_titles.recent_comment_date desc, exp_weblog_titles.entry_date";
  4315. break;
  4316. default : $sql .= "ORDER BY exp_weblog_titles.title";
  4317. break;
  4318. }
  4319. $sort = $TMPL->fetch_param('sort');
  4320. switch ($sort)
  4321. {
  4322. case 'asc' : $sql .= " asc";
  4323. break;
  4324. case 'desc' : $sql .= " desc";
  4325. break;
  4326. default : $sql .= " asc";
  4327. break;
  4328. }
  4329. $result = $DB->query($sql);
  4330. $blog_array = array();
  4331. $parent_only = ($TMPL->fetch_param('parent_only') == 'yes') ? TRUE : FALSE;
  4332. $cat_chunk = (preg_match("/".LD."categories\s*".RD."(.*?)".LD.SLASH."categories\s*".RD."/s", $TMPL->tagdata, $match)) ? $match['1'] : '';
  4333. $c_path = array();
  4334. if (preg_match_all("#".LD."path(=.+?)".RD."#", $cat_chunk, $matches))
  4335. {
  4336. for ($i = 0; $i < count($matches['0']); $i++)
  4337. {
  4338. $c_path[$matches['0'][$i]] = $FNS->create_url($FNS->extract_path($matches['1'][$i]));
  4339. }
  4340. }
  4341. $tit_chunk = (preg_match("/".LD."entry_titles\s*".RD."(.*?)".LD.SLASH."entry_titles\s*".RD."/s", $TMPL->tagdata, $match)) ? $match['1'] : '';
  4342. $t_path = array();
  4343. if (preg_match_all("#".LD."path(=.+?)".RD."#", $tit_chunk, $matches))
  4344. {
  4345. for ($i = 0; $i < count($matches['0']); $i++)
  4346. {
  4347. $t_path[$matches['0'][$i]] = $FNS->create_url($FNS->extract_path($matches['1'][$i]));
  4348. }
  4349. }
  4350. $id_path = array();
  4351. if (preg_match_all("#".LD."entry_id_path(=.+?)".RD."#", $tit_chunk, $matches))
  4352. {
  4353. for ($i = 0; $i < count($matches['0']); $i++)
  4354. {
  4355. $id_path[$matches['0'][$i]] = $FNS->create_url($FNS->extract_path($matches['1'][$i]));
  4356. }
  4357. }
  4358. $entry_date = array();
  4359. preg_match_all("/".LD."entry_date\s+format\s*=\s*(\042|\047)([^\\1]*?)\\1".RD."/s", $tit_chunk, $matches);
  4360. {
  4361. $j = count($matches['0']);
  4362. for ($i = 0; $i < $j; $i++)
  4363. {
  4364. $matches['0'][$i] = str_replace(array(LD,RD), '', $matches['0'][$i]);
  4365. $entry_date[$matches['0'][$i]] = $LOC->fetch_date_params($matches['2'][$i]);
  4366. }
  4367. }
  4368. $str = '';
  4369. if ($TMPL->fetch_param('style') == '' OR $TMPL->fetch_param('style') == 'nested')
  4370. {
  4371. if ($result->num_rows > 0 && $tit_chunk != '')
  4372. {
  4373. $i = 0;
  4374. foreach($result->result as $row)
  4375. {
  4376. $chunk = "<li>".str_replace(LD.'category_name'.RD, '', $tit_chunk)."</li>";
  4377. foreach($t_path as $tkey => $tval)
  4378. {
  4379. $chunk = str_replace($tkey, $FNS->remove_double_slashes($tval.'/'.$row['url_title'].'/'), $chunk);
  4380. }
  4381. foreach($id_path as $tkey => $tval)
  4382. {
  4383. $chunk = str_replace($tkey, $FNS->remove_double_slashes($tval.'/'.$row['entry_id'].'/'), $chunk);
  4384. }
  4385. foreach($TMPL->var_single as $key => $val)
  4386. {
  4387. if (isset($entry_date[$key]))
  4388. {
  4389. foreach ($entry_date[$key] as $dval)
  4390. {
  4391. $val = str_replace($dval, $LOC->convert_timestamp($dval, $row['entry_date'], TRUE), $val);
  4392. }
  4393. $chunk = $TMPL->swap_var_single($key, $val, $chunk);
  4394. }
  4395. }
  4396. $blog_array[$i.'_'.$row['cat_id']] = str_replace(LD.'title'.RD, $row['title'], $chunk);
  4397. $i++;
  4398. }
  4399. }
  4400. $this->category_tree(
  4401. array(
  4402. 'group_id' => $group_id,
  4403. 'weblog_id' => $weblog_id,
  4404. 'path' => $c_path,
  4405. 'template' => $cat_chunk,
  4406. 'blog_array' => $blog_array,
  4407. 'parent_only' => $parent_only,
  4408. 'show_empty' => $TMPL->fetch_param('show_empty')
  4409. )
  4410. );
  4411. if (count($this->category_list) > 0)
  4412. {
  4413. $id_name = ($TMPL->fetch_param('id') === FALSE) ? 'nav_cat_archive' : $TMPL->fetch_param('id');
  4414. $class_name = ($TMPL->fetch_param('class') === FALSE) ? 'nav_cat_archive' : $TMPL->fetch_param('class');
  4415. $this->category_list['0'] = '<ul id="'.$id_name.'" class="'.$class_name.'">'."\n";
  4416. foreach ($this->category_list as $val)
  4417. {
  4418. $str .= $val;
  4419. }
  4420. }
  4421. }
  4422. else
  4423. {
  4424. // fetch category field names and id's
  4425. if ($this->enable['category_fields'] === TRUE)
  4426. {
  4427. $query = $DB->query("SELECT field_id, field_name FROM exp_category_fields
  4428. WHERE site_id IN ('".implode("','", $TMPL->site_ids)."')
  4429. AND group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."')");
  4430. if ($query->num_rows > 0)
  4431. {
  4432. foreach ($query->result as $row)
  4433. {
  4434. $this->catfields[] = array('field_name' => $row['field_name'], 'field_id' => $row['field_id']);
  4435. }
  4436. }
  4437. $field_sqla = ", cg.field_html_formatting, fd.* ";
  4438. $field_sqlb = " LEFT JOIN exp_category_field_data AS fd ON fd.cat_id = c.cat_id
  4439. LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id ";
  4440. }
  4441. else
  4442. {
  4443. $field_sqla = '';
  4444. $field_sqlb = '';
  4445. }
  4446. $sql = "SELECT DISTINCT (c.cat_id), c.cat_name, c.cat_url_title, c.cat_description, c.cat_image, c.parent_id {$field_sqla}
  4447. FROM (exp_categories AS c";
  4448. $sql .= ") {$field_sqlb}";
  4449. if ($TMPL->fetch_param('show_empty') == 'no')
  4450. {
  4451. $sql .= " LEFT JOIN exp_category_posts ON c.cat_id = exp_category_posts.cat_id ";
  4452. if ($weblog_id != '')
  4453. {
  4454. $sql .= " LEFT JOIN exp_weblog_titles ON exp_category_posts.entry_id = exp_weblog_titles.entry_id ";
  4455. }
  4456. }
  4457. $sql .= " WHERE c.group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."') ";
  4458. if ($TMPL->fetch_param('show_empty') == 'no')
  4459. {
  4460. if ($weblog_id != '')
  4461. {
  4462. $sql .= "AND exp_weblog_titles.weblog_id = '".$weblog_id."' ";
  4463. }
  4464. else
  4465. {
  4466. $sql .= " AND exp_weblog_titles.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  4467. }
  4468. if ($status = $TMPL->fetch_param('status'))
  4469. {
  4470. $status = str_replace('Open', 'open', $status);
  4471. $status = str_replace('Closed', 'closed', $status);
  4472. $sql .= $FNS->sql_andor_string($status, 'exp_weblog_titles.status');
  4473. }
  4474. else
  4475. {
  4476. $sql .= "AND exp_weblog_titles.status = 'open' ";
  4477. }
  4478. if ($TMPL->fetch_param('show_empty') == 'no')
  4479. {
  4480. $sql .= "AND exp_category_posts.cat_id IS NOT NULL ";
  4481. }
  4482. }
  4483. if ($TMPL->fetch_param('show') !== FALSE)
  4484. {
  4485. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('show'), 'c.cat_id').' ';
  4486. }
  4487. if ($parent_only == TRUE)
  4488. {
  4489. $sql .= " AND c.parent_id = 0";
  4490. }
  4491. $sql .= " ORDER BY c.group_id, c.parent_id, c.cat_order";
  4492. $query = $DB->query($sql);
  4493. if ($query->num_rows > 0)
  4494. {
  4495. if ( ! class_exists('Typography'))
  4496. {
  4497. require PATH_CORE.'core.typography'.EXT;
  4498. }
  4499. $this->TYPE = new Typography;
  4500. $this->TYPE->convert_curly = FALSE;
  4501. $used = array();
  4502. foreach($query->result as $row)
  4503. {
  4504. if ( ! isset($used[$row['cat_name']]))
  4505. {
  4506. $chunk = $cat_chunk;
  4507. $cat_vars = array('category_name' => $row['cat_name'],
  4508. 'category_url_title' => $row['cat_url_title'],
  4509. 'category_description' => $row['cat_description'],
  4510. 'category_image' => $row['cat_image'],
  4511. 'category_id' => $row['cat_id'],
  4512. 'parent_id' => $row['parent_id']
  4513. );
  4514. foreach ($this->catfields as $v)
  4515. {
  4516. $cat_vars[$v['field_name']] = ( ! isset($row['field_id_'.$v['field_id']])) ? '' : $row['field_id_'.$v['field_id']];
  4517. }
  4518. $chunk = $FNS->prep_conditionals($chunk, $cat_vars);
  4519. $chunk = str_replace( array(LD.'category_id'.RD,
  4520. LD.'category_name'.RD,
  4521. LD.'category_url_title'.RD,
  4522. LD.'category_image'.RD,
  4523. LD.'category_description'.RD,
  4524. LD.'parent_id'.RD),
  4525. array($row['cat_id'],
  4526. $row['cat_name'],
  4527. $row['cat_url_title'],
  4528. $row['cat_image'],
  4529. $row['cat_description'],
  4530. $row['parent_id']),
  4531. $chunk);
  4532. foreach($c_path as $ckey => $cval)
  4533. {
  4534. $cat_seg = ($this->use_category_names == TRUE) ? $this->reserved_cat_segment.'/'.$row['cat_url_title'] : 'C'.$row['cat_id'];
  4535. $chunk = str_replace($ckey, $FNS->remove_double_slashes($cval.'/'.$cat_seg.'/'), $chunk);
  4536. }
  4537. // parse custom fields
  4538. foreach($this->catfields as $cfv)
  4539. {
  4540. if (isset($row['field_id_'.$cfv['field_id']]) AND $row['field_id_'.$cfv['field_id']] != '')
  4541. {
  4542. $field_content = $this->TYPE->parse_type($row['field_id_'.$cfv['field_id']],
  4543. array(
  4544. 'text_format' => $row['field_ft_'.$cfv['field_id']],
  4545. 'html_format' => $row['field_html_formatting'],
  4546. 'auto_links' => 'n',
  4547. 'allow_img_url' => 'y'
  4548. )
  4549. );
  4550. $chunk = str_replace(LD.$cfv['field_name'].RD, $field_content, $chunk);
  4551. }
  4552. else
  4553. {
  4554. // garbage collection
  4555. $chunk = str_replace(LD.$cfv['field_name'].RD, '', $chunk);
  4556. }
  4557. }
  4558. $str .= $chunk;
  4559. $used[$row['cat_name']] = TRUE;
  4560. }
  4561. foreach($result->result as $trow)
  4562. {
  4563. if ($trow['cat_id'] == $row['cat_id'])
  4564. {
  4565. $chunk = str_replace(array(LD.'title'.RD, LD.'category_name'.RD),
  4566. array($trow['title'],$row['cat_name']),
  4567. $tit_chunk);
  4568. foreach($t_path as $tkey => $tval)
  4569. {
  4570. $chunk = str_replace($tkey, $FNS->remove_double_slashes($tval.'/'.$trow['url_title'].'/'), $chunk);
  4571. }
  4572. foreach($id_path as $tkey => $tval)
  4573. {
  4574. $chunk = str_replace($tkey, $FNS->remove_double_slashes($tval.'/'.$trow['entry_id'].'/'), $chunk);
  4575. }
  4576. foreach($TMPL->var_single as $key => $val)
  4577. {
  4578. if (isset($entry_date[$key]))
  4579. {
  4580. foreach ($entry_date[$key] as $dval)
  4581. {
  4582. $val = str_replace($dval, $LOC->convert_timestamp($dval, $trow['entry_date'], TRUE), $val);
  4583. }
  4584. $chunk = $TMPL->swap_var_single($key, $val, $chunk);
  4585. }
  4586. }
  4587. $str .= $chunk;
  4588. }
  4589. }
  4590. }
  4591. }
  4592. if ($TMPL->fetch_param('backspace'))
  4593. {
  4594. $str = rtrim(str_replace("&#47;", "/", $str));
  4595. $str = substr($str, 0, - $TMPL->fetch_param('backspace'));
  4596. $str = str_replace("/", "&#47;", $str);
  4597. }
  4598. }
  4599. return $str;
  4600. }
  4601. /* END */
  4602. /** --------------------------------
  4603. /** Locate category parent
  4604. /** --------------------------------*/
  4605. // This little recursive gem will travel up the
  4606. // category tree until it finds the category ID
  4607. // number of any parents. It's used by the function
  4608. // below
  4609. function find_parent($parent, $all)
  4610. {
  4611. foreach ($all as $cat_id => $parent_id)
  4612. {
  4613. if ($parent == $cat_id)
  4614. {
  4615. $this->cat_full_array[] = $cat_id;
  4616. if ($parent_id != 0)
  4617. $this->find_parent($parent_id, $all);
  4618. }
  4619. }
  4620. }
  4621. /* END */
  4622. /** --------------------------------
  4623. /** Category Tree
  4624. /** --------------------------------*/
  4625. // This function and the next create a nested, hierarchical category tree
  4626. function category_tree($cdata = array())
  4627. {
  4628. global $FNS, $REGX, $DB, $TMPL, $FNS, $LOC;
  4629. $default = array('group_id', 'weblog_id', 'path', 'template', 'depth', 'blog_array', 'parent_only', 'show_empty');
  4630. foreach ($default as $val)
  4631. {
  4632. $$val = ( ! isset($cdata[$val])) ? '' : $cdata[$val];
  4633. }
  4634. if ($group_id == '')
  4635. {
  4636. return false;
  4637. }
  4638. if ($this->enable['category_fields'] === TRUE)
  4639. {
  4640. $query = $DB->query("SELECT field_id, field_name
  4641. FROM exp_category_fields
  4642. WHERE site_id IN ('".implode("','", $TMPL->site_ids)."')
  4643. AND group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."')");
  4644. if ($query->num_rows > 0)
  4645. {
  4646. foreach ($query->result as $row)
  4647. {
  4648. $this->catfields[] = array('field_name' => $row['field_name'], 'field_id' => $row['field_id']);
  4649. }
  4650. }
  4651. $field_sqla = ", cg.field_html_formatting, fd.* ";
  4652. $field_sqlb = " LEFT JOIN exp_category_field_data AS fd ON fd.cat_id = c.cat_id
  4653. LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id";
  4654. }
  4655. else
  4656. {
  4657. $field_sqla = '';
  4658. $field_sqlb = '';
  4659. }
  4660. /** -----------------------------------
  4661. /** Are we showing empty categories
  4662. /** -----------------------------------*/
  4663. // If we are only showing categories that have been assigned to entries
  4664. // we need to run a couple queries and run a recursive function that
  4665. // figures out whether any given category has a parent.
  4666. // If we don't do this we will run into a problem in which parent categories
  4667. // that are not assigned to a blog will be supressed, and therefore, any of its
  4668. // children will be supressed also - even if they are assigned to entries.
  4669. // So... we will first fetch all the category IDs, then only the ones that are assigned
  4670. // to entries, and lastly we'll recursively run up the tree and fetch all parents.
  4671. // Follow that? No? Me neither...
  4672. if ($show_empty == 'no')
  4673. {
  4674. // First we'll grab all category ID numbers
  4675. $query = $DB->query("SELECT cat_id, parent_id FROM exp_categories
  4676. WHERE group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."')
  4677. ORDER BY group_id, parent_id, cat_order");
  4678. $all = array();
  4679. // No categories exist? Back to the barn for the night..
  4680. if ($query->num_rows == 0)
  4681. return false;
  4682. foreach($query->result as $row)
  4683. {
  4684. $all[$row['cat_id']] = $row['parent_id'];
  4685. }
  4686. // Next we'l grab only the assigned categories
  4687. $sql = "SELECT DISTINCT(exp_categories.cat_id), parent_id
  4688. FROM exp_categories
  4689. LEFT JOIN exp_category_posts ON exp_categories.cat_id = exp_category_posts.cat_id
  4690. LEFT JOIN exp_weblog_titles ON exp_category_posts.entry_id = exp_weblog_titles.entry_id ";
  4691. $sql .= "WHERE group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."') ";
  4692. $sql .= "AND exp_category_posts.cat_id IS NOT NULL ";
  4693. if ($weblog_id != '')
  4694. {
  4695. $sql .= "AND exp_weblog_titles.weblog_id = '".$weblog_id."' ";
  4696. }
  4697. else
  4698. {
  4699. $sql .= "AND exp_weblog_titles.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  4700. }
  4701. if (($status = $TMPL->fetch_param('status')) !== FALSE)
  4702. {
  4703. $status = str_replace(array('Open', 'Closed'), array('open', 'closed'), $status);
  4704. $sql .= $FNS->sql_andor_string($status, 'exp_weblog_titles.status');
  4705. }
  4706. else
  4707. {
  4708. $sql .= "AND exp_weblog_titles.status != 'closed' ";
  4709. }
  4710. /** ----------------------------------------------
  4711. /** We only select entries that have not expired
  4712. /** ----------------------------------------------*/
  4713. $timestamp = ($TMPL->cache_timestamp != '') ? $LOC->set_gmt($TMPL->cache_timestamp) : $LOC->now;
  4714. if ($TMPL->fetch_param('show_future_entries') != 'yes')
  4715. {
  4716. $sql .= " AND exp_weblog_titles.entry_date < ".$timestamp." ";
  4717. }
  4718. if ($TMPL->fetch_param('show_expired') != 'yes')
  4719. {
  4720. $sql .= " AND (exp_weblog_titles.expiration_date = 0 OR exp_weblog_titles.expiration_date > ".$timestamp.") ";
  4721. }
  4722. if ($parent_only === TRUE)
  4723. {
  4724. $sql .= " AND parent_id = 0";
  4725. }
  4726. $sql .= " ORDER BY group_id, parent_id, cat_order";
  4727. $query = $DB->query($sql);
  4728. if ($query->num_rows == 0)
  4729. return false;
  4730. // All the magic happens here, baby!!
  4731. foreach($query->result as $row)
  4732. {
  4733. if ($row['parent_id'] != 0)
  4734. {
  4735. $this->find_parent($row['parent_id'], $all);
  4736. }
  4737. $this->cat_full_array[] = $row['cat_id'];
  4738. }
  4739. $this->cat_full_array = array_unique($this->cat_full_array);
  4740. $sql = "SELECT c.cat_id, c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description {$field_sqla}
  4741. FROM exp_categories AS c
  4742. {$field_sqlb}
  4743. WHERE c.cat_id IN (";
  4744. foreach ($this->cat_full_array as $val)
  4745. {
  4746. $sql .= $val.',';
  4747. }
  4748. $sql = substr($sql, 0, -1).')';
  4749. $sql .= " ORDER BY c.group_id, c.parent_id, c.cat_order";
  4750. $query = $DB->query($sql);
  4751. if ($query->num_rows == 0)
  4752. return false;
  4753. }
  4754. else
  4755. {
  4756. $sql = "SELECT DISTINCT(c.cat_id), c.parent_id, c.cat_name, c.cat_url_title, c.cat_image, c.cat_description {$field_sqla}
  4757. FROM exp_categories AS c
  4758. {$field_sqlb}
  4759. WHERE c.group_id IN ('".str_replace('|', "','", $DB->escape_str($group_id))."') ";
  4760. if ($parent_only === TRUE)
  4761. {
  4762. $sql .= " AND c.parent_id = 0";
  4763. }
  4764. $sql .= " ORDER BY c.group_id, c.parent_id, c.cat_order";
  4765. $query = $DB->query($sql);
  4766. if ($query->num_rows == 0)
  4767. return false;
  4768. }
  4769. // Here we check the show parameter to see if we have any
  4770. // categories we should be ignoring or only a certain group of
  4771. // categories that we should be showing. By doing this here before
  4772. // all of the nested processing we should keep out all but the
  4773. // request categories while also not having a problem with having a
  4774. // child but not a parent. As we all know, categories are not asexual
  4775. if ($TMPL->fetch_param('show') !== FALSE)
  4776. {
  4777. if (strncmp('not ', $TMPL->fetch_param('show'), 4) == 0)
  4778. {
  4779. $not_these = explode('|', trim(substr($TMPL->fetch_param('show'), 3)));
  4780. }
  4781. else
  4782. {
  4783. $these = explode('|', trim($TMPL->fetch_param('show')));
  4784. }
  4785. }
  4786. foreach($query->result as $row)
  4787. {
  4788. if (isset($not_these) && in_array($row['cat_id'], $not_these))
  4789. {
  4790. continue;
  4791. }
  4792. elseif(isset($these) && ! in_array($row['cat_id'], $these))
  4793. {
  4794. continue;
  4795. }
  4796. $this->cat_array[$row['cat_id']] = array($row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['cat_url_title']);
  4797. foreach ($row as $key => $val)
  4798. {
  4799. if (strpos($key, 'field') !== FALSE)
  4800. {
  4801. $this->cat_array[$row['cat_id']][$key] = $val;
  4802. }
  4803. }
  4804. }
  4805. $this->temp_array = $this->cat_array;
  4806. $open = 0;
  4807. if ( ! class_exists('Typography'))
  4808. {
  4809. require PATH_CORE.'core.typography'.EXT;
  4810. }
  4811. $this->TYPE = new Typography;
  4812. $this->TYPE->convert_curly = FALSE;
  4813. $this->category_count = 0;
  4814. $total_results = count($this->cat_array);
  4815. foreach($this->cat_array as $key => $val)
  4816. {
  4817. if (0 == $val['0'])
  4818. {
  4819. if ($open == 0)
  4820. {
  4821. $open = 1;
  4822. $this->category_list[] = "<ul>\n";
  4823. }
  4824. $chunk = $template;
  4825. $cat_vars = array('category_name' => $val['1'],
  4826. 'category_url_title' => $val['4'],
  4827. 'category_description' => $val['3'],
  4828. 'category_image' => $val['2'],
  4829. 'category_id' => $key,
  4830. 'parent_id' => $val['0']
  4831. );
  4832. // add custom fields for conditionals prep
  4833. foreach ($this->catfields as $v)
  4834. {
  4835. $cat_vars[$v['field_name']] = ( ! isset($val['field_id_'.$v['field_id']])) ? '' : $val['field_id_'.$v['field_id']];
  4836. }
  4837. $cat_vars['count'] = ++$this->category_count;
  4838. $cat_vars['total_results'] = $total_results;
  4839. $chunk = $FNS->prep_conditionals($chunk, $cat_vars);
  4840. $chunk = str_replace( array(LD.'category_id'.RD,
  4841. LD.'category_name'.RD,
  4842. LD.'category_url_title'.RD,
  4843. LD.'category_image'.RD,
  4844. LD.'category_description'.RD,
  4845. LD.'parent_id'.RD),
  4846. array($key,
  4847. $val['1'],
  4848. $val['4'],
  4849. $val['2'],
  4850. $val['3'],
  4851. $val['0']),
  4852. $chunk);
  4853. foreach($path as $pkey => $pval)
  4854. {
  4855. if ($this->use_category_names == TRUE)
  4856. {
  4857. $chunk = str_replace($pkey, $FNS->remove_double_slashes($pval.'/'.$this->reserved_cat_segment.'/'.$val['4'].'/'), $chunk);
  4858. }
  4859. else
  4860. {
  4861. $chunk = str_replace($pkey, $FNS->remove_double_slashes($pval.'/C'.$key.'/'), $chunk);
  4862. }
  4863. }
  4864. // parse custom fields
  4865. foreach($this->catfields as $cval)
  4866. {
  4867. if (isset($val['field_id_'.$cval['field_id']]) AND $val['field_id_'.$cval['field_id']] != '')
  4868. {
  4869. $field_content = $this->TYPE->parse_type($val['field_id_'.$cval['field_id']],
  4870. array(
  4871. 'text_format' => $val['field_ft_'.$cval['field_id']],
  4872. 'html_format' => $val['field_html_formatting'],
  4873. 'auto_links' => 'n',
  4874. 'allow_img_url' => 'y'
  4875. )
  4876. );
  4877. $chunk = str_replace(LD.$cval['field_name'].RD, $field_content, $chunk);
  4878. }
  4879. else
  4880. {
  4881. // garbage collection
  4882. $chunk = str_replace(LD.$cval['field_name'].RD, '', $chunk);
  4883. }
  4884. }
  4885. /** --------------------------------
  4886. /** {count}
  4887. /** --------------------------------*/
  4888. if (strpos($chunk, LD.'count'.RD) !== FALSE)
  4889. {
  4890. $chunk = str_replace(LD.'count'.RD, $this->category_count, $chunk);
  4891. }
  4892. /** --------------------------------
  4893. /** {total_results}
  4894. /** --------------------------------*/
  4895. if (strpos($chunk, LD.'total_results'.RD) !== FALSE)
  4896. {
  4897. $chunk = str_replace(LD.'total_results'.RD, $total_results, $chunk);
  4898. }
  4899. $this->category_list[] = "\t<li>".$chunk;
  4900. if (is_array($blog_array))
  4901. {
  4902. $fillable_entries = 'n';
  4903. foreach($blog_array as $k => $v)
  4904. {
  4905. $k = substr($k, strpos($k, '_') + 1);
  4906. if ($key == $k)
  4907. {
  4908. if ($fillable_entries == 'n')
  4909. {
  4910. $this->category_list[] = "\n\t\t<ul>\n";
  4911. $fillable_entries = 'y';
  4912. }
  4913. $this->category_list[] = "\t\t\t$v";
  4914. }
  4915. }
  4916. }
  4917. if (isset($fillable_entries) && $fillable_entries == 'y')
  4918. {
  4919. $this->category_list[] = "\t\t</ul>\n";
  4920. }
  4921. $this->category_subtree(
  4922. array(
  4923. 'parent_id' => $key,
  4924. 'path' => $path,
  4925. 'template' => $template,
  4926. 'blog_array' => $blog_array
  4927. )
  4928. );
  4929. $t = '';
  4930. if (isset($fillable_entries) && $fillable_entries == 'y')
  4931. {
  4932. $t .= "\t";
  4933. }
  4934. $this->category_list[] = $t."</li>\n";
  4935. unset($this->temp_array[$key]);
  4936. $this->close_ul(0);
  4937. }
  4938. }
  4939. }
  4940. /* END */
  4941. /** --------------------------------
  4942. /** Category Sub-tree
  4943. /** --------------------------------*/
  4944. function category_subtree($cdata = array())
  4945. {
  4946. global $TMPL, $FNS;
  4947. $default = array('parent_id', 'path', 'template', 'depth', 'blog_array', 'show_empty');
  4948. foreach ($default as $val)
  4949. {
  4950. $$val = ( ! isset($cdata[$val])) ? '' : $cdata[$val];
  4951. }
  4952. $open = 0;
  4953. if ($depth == '')
  4954. $depth = 1;
  4955. $tab = '';
  4956. for ($i = 0; $i <= $depth; $i++)
  4957. $tab .= "\t";
  4958. $total_results = count($this->cat_array);
  4959. foreach($this->cat_array as $key => $val)
  4960. {
  4961. if ($parent_id == $val['0'])
  4962. {
  4963. if ($open == 0)
  4964. {
  4965. $open = 1;
  4966. $this->category_list[] = "\n".$tab."<ul>\n";
  4967. }
  4968. $chunk = $template;
  4969. $cat_vars = array('category_name' => $val['1'],
  4970. 'category_url_title' => $val['4'],
  4971. 'category_description' => $val['3'],
  4972. 'category_image' => $val['2'],
  4973. 'category_id' => $key,
  4974. 'parent_id' => $val['0']);
  4975. // add custom fields for conditionals prep
  4976. foreach ($this->catfields as $v)
  4977. {
  4978. $cat_vars[$v['field_name']] = ( ! isset($val['field_id_'.$v['field_id']])) ? '' : $val['field_id_'.$v['field_id']];
  4979. }
  4980. $cat_vars['count'] = ++$this->category_count;
  4981. $cat_vars['total_results'] = $total_results;
  4982. $chunk = $FNS->prep_conditionals($chunk, $cat_vars);
  4983. $chunk = str_replace( array(LD.'category_id'.RD,
  4984. LD.'category_name'.RD,
  4985. LD.'category_url_title'.RD,
  4986. LD.'category_image'.RD,
  4987. LD.'category_description'.RD,
  4988. LD.'parent_id'.RD),
  4989. array($key,
  4990. $val['1'],
  4991. $val['4'],
  4992. $val['2'],
  4993. $val['3'],
  4994. $val['0']),
  4995. $chunk);
  4996. foreach($path as $pkey => $pval)
  4997. {
  4998. if ($this->use_category_names == TRUE)
  4999. {
  5000. $chunk = str_replace($pkey, $FNS->remove_double_slashes($pval.'/'.$this->reserved_cat_segment.'/'.$val['4'].'/'), $chunk);
  5001. }
  5002. else
  5003. {
  5004. $chunk = str_replace($pkey, $FNS->remove_double_slashes($pval.'/C'.$key.'/'), $chunk);
  5005. }
  5006. }
  5007. // parse custom fields
  5008. foreach($this->catfields as $ccv)
  5009. {
  5010. if (isset($val['field_id_'.$ccv['field_id']]) AND $val['field_id_'.$ccv['field_id']] != '')
  5011. {
  5012. $field_content = $this->TYPE->parse_type($val['field_id_'.$ccv['field_id']],
  5013. array(
  5014. 'text_format' => $val['field_ft_'.$ccv['field_id']],
  5015. 'html_format' => $val['field_html_formatting'],
  5016. 'auto_links' => 'n',
  5017. 'allow_img_url' => 'y'
  5018. )
  5019. );
  5020. $chunk = str_replace(LD.$ccv['field_name'].RD, $field_content, $chunk);
  5021. }
  5022. else
  5023. {
  5024. // garbage collection
  5025. $chunk = str_replace(LD.$ccv['field_name'].RD, '', $chunk);
  5026. }
  5027. }
  5028. /** --------------------------------
  5029. /** {count}
  5030. /** --------------------------------*/
  5031. if (strpos($chunk, LD.'count'.RD) !== FALSE)
  5032. {
  5033. $chunk = str_replace(LD.'count'.RD, $this->category_count, $chunk);
  5034. }
  5035. /** --------------------------------
  5036. /** {total_results}
  5037. /** --------------------------------*/
  5038. if (strpos($chunk, LD.'total_results'.RD) !== FALSE)
  5039. {
  5040. $chunk = str_replace(LD.'total_results'.RD, $total_results, $chunk);
  5041. }
  5042. $this->category_list[] = $tab."\t<li>".$chunk;
  5043. if (is_array($blog_array))
  5044. {
  5045. $fillable_entries = 'n';
  5046. foreach($blog_array as $k => $v)
  5047. {
  5048. $k = substr($k, strpos($k, '_') + 1);
  5049. if ($key == $k)
  5050. {
  5051. if ( ! isset($fillable_entries) || $fillable_entries == 'n')
  5052. {
  5053. $this->category_list[] = "\n{$tab}\t\t<ul>\n";
  5054. $fillable_entries = 'y';
  5055. }
  5056. $this->category_list[] = "{$tab}\t\t\t$v";
  5057. }
  5058. }
  5059. }
  5060. if (isset($fillable_entries) && $fillable_entries == 'y')
  5061. {
  5062. $this->category_list[] = "{$tab}\t\t</ul>\n";
  5063. }
  5064. $t = '';
  5065. if ($this->category_subtree(
  5066. array(
  5067. 'parent_id' => $key,
  5068. 'path' => $path,
  5069. 'template' => $template,
  5070. 'depth' => $depth + 2,
  5071. 'blog_array' => $blog_array
  5072. )
  5073. ) != 0 );
  5074. if (isset($fillable_entries) && $fillable_entries == 'y')
  5075. {
  5076. $t .= "$tab\t";
  5077. }
  5078. $this->category_list[] = $t."</li>\n";
  5079. unset($this->temp_array[$key]);
  5080. $this->close_ul($parent_id, $depth + 1);
  5081. }
  5082. }
  5083. return $open;
  5084. }
  5085. /* END */
  5086. /** --------------------------------
  5087. /** Close </ul> tags
  5088. /** --------------------------------*/
  5089. // This is a helper function to the above
  5090. function close_ul($parent_id, $depth = 0)
  5091. {
  5092. $count = 0;
  5093. $tab = "";
  5094. for ($i = 0; $i < $depth; $i++)
  5095. {
  5096. $tab .= "\t";
  5097. }
  5098. foreach ($this->temp_array as $val)
  5099. {
  5100. if ($parent_id == $val['0'])
  5101. $count++;
  5102. }
  5103. if ($count == 0)
  5104. $this->category_list[] = $tab."</ul>\n";
  5105. }
  5106. /* END */
  5107. /** ----------------------------------------
  5108. /** Weblog "category_heading" tag
  5109. /** ----------------------------------------*/
  5110. function category_heading()
  5111. {
  5112. global $IN, $TMPL, $FNS, $DB, $EXT;
  5113. if ($this->QSTR == '')
  5114. {
  5115. return;
  5116. }
  5117. // -------------------------------------------
  5118. // 'weblog_module_category_heading_start' hook.
  5119. // - Rewrite the displaying of category headings, if you dare!
  5120. //
  5121. if ($EXT->active_hook('weblog_module_category_heading_start') === TRUE)
  5122. {
  5123. $TMPL->tagdata = $EXT->call_extension('weblog_module_category_heading_start');
  5124. if ($EXT->end_script === TRUE) return $TMPL->tagdata;
  5125. }
  5126. //
  5127. // -------------------------------------------
  5128. $qstring = $this->QSTR;
  5129. /** --------------------------------------
  5130. /** Remove page number
  5131. /** --------------------------------------*/
  5132. if (preg_match("#/P\d+#", $qstring, $match))
  5133. {
  5134. $qstring = $FNS->remove_double_slashes(str_replace($match['0'], '', $qstring));
  5135. }
  5136. /** --------------------------------------
  5137. /** Remove "N"
  5138. /** --------------------------------------*/
  5139. if (preg_match("#/N(\d+)#", $qstring, $match))
  5140. {
  5141. $qstring = $FNS->remove_double_slashes(str_replace($match['0'], '', $qstring));
  5142. }
  5143. // Is the category being specified by name?
  5144. if ($qstring != '' AND $this->reserved_cat_segment != '' AND in_array($this->reserved_cat_segment, explode("/", $qstring)) AND $TMPL->fetch_param('weblog'))
  5145. {
  5146. $qstring = preg_replace("/(.*?)".preg_quote($this->reserved_cat_segment)."\//i", '', $qstring);
  5147. $sql = "SELECT DISTINCT cat_group FROM exp_weblogs WHERE site_id IN ('".implode("','", $TMPL->site_ids)."') AND ";
  5148. if (USER_BLOG !== FALSE)
  5149. {
  5150. $sql .= " weblog_id='".UB_BLOG_ID."'";
  5151. }
  5152. else
  5153. {
  5154. $xsql = $FNS->sql_andor_string($TMPL->fetch_param('weblog'), 'blog_name');
  5155. if (substr($xsql, 0, 3) == 'AND') $xsql = substr($xsql, 3);
  5156. $sql .= ' '.$xsql;
  5157. }
  5158. $query = $DB->query($sql);
  5159. if ($query->num_rows > 0)
  5160. {
  5161. $valid = 'y';
  5162. $last = explode('|', $query->row['cat_group']);
  5163. $valid_cats = array();
  5164. foreach($query->result as $row)
  5165. {
  5166. if ($TMPL->fetch_param('relaxed_categories') == 'yes')
  5167. {
  5168. $valid_cats = array_merge($valid_cats, explode('|', $row['cat_group']));
  5169. }
  5170. else
  5171. {
  5172. $valid_cats = array_intersect($last, explode('|', $row['cat_group']));
  5173. }
  5174. $valid_cats = array_unique($valid_cats);
  5175. if (sizeof($valid_cats) == 0)
  5176. {
  5177. $valid = 'n';
  5178. break;
  5179. }
  5180. }
  5181. }
  5182. else
  5183. {
  5184. $valid = 'n';
  5185. }
  5186. if ($valid == 'y')
  5187. {
  5188. // the category URL title should be the first segment left at this point in $qstring,
  5189. // but because prior to this feature being added, category names were used in URLs,
  5190. // and '/' is a valid character for category names. If they have not updated their
  5191. // category url titles since updating to 1.6, their category URL title could still
  5192. // contain a '/'. So we'll try to get the category the correct way first, and if
  5193. // it fails, we'll try the whole $qstring
  5194. $arr = explode('/', $qstring);
  5195. $cut_qstring = array_shift($arr);
  5196. $result = $DB->query("SELECT cat_id FROM exp_categories
  5197. WHERE cat_url_title='".$DB->escape_str($cut_qstring)."'
  5198. AND group_id IN ('".implode("','", $valid_cats)."')");
  5199. if ($result->num_rows == 1)
  5200. {
  5201. $qstring = str_replace($cut_qstring, 'C'.$result->row['cat_id'], $qstring);
  5202. }
  5203. else
  5204. {
  5205. // give it one more try using the whole $qstring
  5206. $result = $DB->query("SELECT cat_id FROM exp_categories
  5207. WHERE cat_url_title='".$DB->escape_str($qstring)."'
  5208. AND group_id IN ('".implode("','", $valid_cats)."')");
  5209. if ($result->num_rows == 1)
  5210. {
  5211. $qstring = 'C'.$result->row['cat_id'];
  5212. }
  5213. }
  5214. }
  5215. }
  5216. // Is the category being specified by ID?
  5217. if ( ! preg_match("#(^|\/)C(\d+)#", $qstring, $match))
  5218. {
  5219. return $TMPL->no_results();
  5220. }
  5221. // fetch category field names and id's
  5222. if ($this->enable['category_fields'] === TRUE)
  5223. {
  5224. // limit to correct category group
  5225. $gquery = $DB->query("SELECT group_id FROM exp_categories WHERE cat_id = '".$DB->escape_str($match['2'])."'");
  5226. if ($gquery->num_rows == 0)
  5227. {
  5228. return $TMPL->no_results();
  5229. }
  5230. $query = $DB->query("SELECT field_id, field_name
  5231. FROM exp_category_fields
  5232. WHERE site_id IN ('".implode("','", $TMPL->site_ids)."')
  5233. AND group_id = '".$gquery->row['group_id']."'");
  5234. if ($query->num_rows > 0)
  5235. {
  5236. foreach ($query->result as $row)
  5237. {
  5238. $this->catfields[] = array('field_name' => $row['field_name'], 'field_id' => $row['field_id']);
  5239. }
  5240. }
  5241. $field_sqla = ", cg.field_html_formatting, fd.* ";
  5242. $field_sqlb = " LEFT JOIN exp_category_field_data AS fd ON fd.cat_id = c.cat_id
  5243. LEFT JOIN exp_category_groups AS cg ON cg.group_id = c.group_id ";
  5244. }
  5245. else
  5246. {
  5247. $field_sqla = '';
  5248. $field_sqlb = '';
  5249. }
  5250. $query = $DB->query("SELECT c.cat_name, c.parent_id, c.cat_url_title, c.cat_description, c.cat_image {$field_sqla}
  5251. FROM exp_categories AS c
  5252. {$field_sqlb}
  5253. WHERE c.cat_id = '".$DB->escape_str($match['2'])."'");
  5254. if ($query->num_rows == 0)
  5255. {
  5256. return $TMPL->no_results();
  5257. }
  5258. $cat_vars = array('category_name' => $query->row['cat_name'],
  5259. 'category_description' => $query->row['cat_description'],
  5260. 'category_image' => $query->row['cat_image'],
  5261. 'category_id' => $match['2'],
  5262. 'parent_id' => $query->row['parent_id']);
  5263. // add custom fields for conditionals prep
  5264. foreach ($this->catfields as $v)
  5265. {
  5266. $cat_vars[$v['field_name']] = ( ! isset($query->row['field_id_'.$v['field_id']])) ? '' : $query->row['field_id_'.$v['field_id']];
  5267. }
  5268. $TMPL->tagdata = $FNS->prep_conditionals($TMPL->tagdata, $cat_vars);
  5269. $TMPL->tagdata = str_replace( array(LD.'category_id'.RD,
  5270. LD.'category_name'.RD,
  5271. LD.'category_url_title'.RD,
  5272. LD.'category_image'.RD,
  5273. LD.'category_description'.RD,
  5274. LD.'parent_id'.RD),
  5275. array($match['2'],
  5276. $query->row['cat_name'],
  5277. $query->row['cat_url_title'],
  5278. $query->row['cat_image'],
  5279. $query->row['cat_description'],
  5280. $query->row['parent_id']),
  5281. $TMPL->tagdata);
  5282. // parse custom fields
  5283. if ( ! class_exists('Typography'))
  5284. {
  5285. require PATH_CORE.'core.typography'.EXT;
  5286. }
  5287. $this->TYPE = new Typography;
  5288. $this->TYPE->convert_curly = FALSE;
  5289. // parse custom fields
  5290. foreach($this->catfields as $ccv)
  5291. {
  5292. if (isset($query->row['field_id_'.$ccv['field_id']]) AND $query->row['field_id_'.$ccv['field_id']] != '')
  5293. {
  5294. $field_content = $this->TYPE->parse_type($query->row['field_id_'.$ccv['field_id']],
  5295. array(
  5296. 'text_format' => $query->row['field_ft_'.$ccv['field_id']],
  5297. 'html_format' => $query->row['field_html_formatting'],
  5298. 'auto_links' => 'n',
  5299. 'allow_img_url' => 'y'
  5300. )
  5301. );
  5302. $TMPL->tagdata = str_replace(LD.$ccv['field_name'].RD, $field_content, $TMPL->tagdata);
  5303. }
  5304. else
  5305. {
  5306. // garbage collection
  5307. $TMPL->tagdata = str_replace(LD.$ccv['field_name'].RD, '', $TMPL->tagdata);
  5308. }
  5309. }
  5310. return $TMPL->tagdata;
  5311. }
  5312. /* END */
  5313. /** ---------------------------------------
  5314. /** Next / Prev entry tags
  5315. /** ---------------------------------------*/
  5316. function next_entry()
  5317. {
  5318. return $this->next_prev_entry('next');
  5319. }
  5320. function prev_entry()
  5321. {
  5322. return $this->next_prev_entry('prev');
  5323. }
  5324. function next_prev_entry($which = 'next')
  5325. {
  5326. global $DB, $FNS, $IN, $LOC, $SESS, $TMPL;
  5327. $which = ($which != 'next' AND $which != 'prev') ? 'next' : $which;
  5328. $sort = ($which == 'next') ? 'ASC' : 'DESC';
  5329. // Don't repeat our work if we already know the single entry page details
  5330. if (! isset($SESS->cache['weblog']['single_entry_id']) OR ! isset($SESS->cache['weblog']['single_entry_date']))
  5331. {
  5332. // no query string? Nothing to do...
  5333. if (($qstring = $this->QSTR) == '')
  5334. {
  5335. return;
  5336. }
  5337. /** --------------------------------------
  5338. /** Remove page number
  5339. /** --------------------------------------*/
  5340. if (preg_match("#/P\d+#", $qstring, $match))
  5341. {
  5342. $qstring = $FNS->remove_double_slashes(str_replace($match['0'], '', $qstring));
  5343. }
  5344. /** --------------------------------------
  5345. /** Remove "N"
  5346. /** --------------------------------------*/
  5347. if (preg_match("#/N(\d+)#", $qstring, $match))
  5348. {
  5349. $qstring = $FNS->remove_double_slashes(str_replace($match['0'], '', $qstring));
  5350. }
  5351. if (strpos($qstring, '/') !== FALSE)
  5352. {
  5353. $qstring = substr($qstring, 0, strpos($qstring, '/'));
  5354. }
  5355. /** ---------------------------------------
  5356. /** Query for the entry id and date
  5357. /** ---------------------------------------*/
  5358. $sql = 'SELECT t.entry_id, t.entry_date
  5359. FROM (exp_weblog_titles AS t)
  5360. LEFT JOIN exp_weblogs AS w ON w.weblog_id = t.weblog_id ';
  5361. if (is_numeric($qstring))
  5362. {
  5363. $sql .= " WHERE t.entry_id = '".$DB->escape_str($qstring)."' ";
  5364. }
  5365. else
  5366. {
  5367. $sql .= " WHERE t.url_title = '".$DB->escape_str($qstring)."' ";
  5368. }
  5369. if (USER_BLOG === FALSE)
  5370. {
  5371. $sql .= " AND w.is_user_blog = 'n' AND w.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  5372. if ($blog_name = $TMPL->fetch_param('weblog'))
  5373. {
  5374. $sql .= $FNS->sql_andor_string($blog_name, 'blog_name', 'w');
  5375. }
  5376. }
  5377. else
  5378. {
  5379. $sql .= " AND t.weblog_id = '".UB_BLOG_ID."' ";
  5380. }
  5381. $query = $DB->query($sql);
  5382. // no results or more than one result? Buh bye!
  5383. if ($query->num_rows != 1)
  5384. {
  5385. $TMPL->log_item('Weblog Next/Prev Entry tag error: Could not resolve single entry page id.');
  5386. return;
  5387. }
  5388. $SESS->cache['weblog']['single_entry_id'] = $query->row['entry_id'];
  5389. $SESS->cache['weblog']['single_entry_date'] = $query->row['entry_date'];
  5390. }
  5391. /** ---------------------------------------
  5392. /** Find the next / prev entry
  5393. /** ---------------------------------------*/
  5394. $ids = '';
  5395. // Get included or excluded entry ids from entry_id parameter
  5396. if (($entry_id = $TMPL->fetch_param('entry_id')) != FALSE)
  5397. {
  5398. $ids = $FNS->sql_andor_string($entry_id, 't.entry_id').' ';
  5399. }
  5400. $sql = 'SELECT t.entry_id, t.title, t.url_title
  5401. FROM (exp_weblog_titles AS t)
  5402. LEFT JOIN exp_weblogs AS w ON w.weblog_id = t.weblog_id ';
  5403. /* --------------------------------
  5404. /* We use LEFT JOIN when there is a 'not' so that we get
  5405. /* entries that are not assigned to a category.
  5406. /* --------------------------------*/
  5407. if ((substr($TMPL->fetch_param('category_group'), 0, 3) == 'not' OR substr($TMPL->fetch_param('category'), 0, 3) == 'not') && $TMPL->fetch_param('uncategorized_entries') !== 'n')
  5408. {
  5409. $sql .= 'LEFT JOIN exp_category_posts ON t.entry_id = exp_category_posts.entry_id
  5410. LEFT JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id ';
  5411. }
  5412. elseif($TMPL->fetch_param('category_group') OR $TMPL->fetch_param('category'))
  5413. {
  5414. $sql .= 'INNER JOIN exp_category_posts ON t.entry_id = exp_category_posts.entry_id
  5415. INNER JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id ';
  5416. }
  5417. $sql .= ' WHERE t.entry_id != '.$SESS->cache['weblog']['single_entry_id'].' '.$ids;
  5418. $timestamp = ($TMPL->cache_timestamp != '') ? $LOC->set_gmt($TMPL->cache_timestamp) : $LOC->now;
  5419. if ($TMPL->fetch_param('show_future_entries') != 'yes')
  5420. {
  5421. $sql .= " AND t.entry_date < {$timestamp} ";
  5422. }
  5423. // constrain by date depending on whether this is a 'next' or 'prev' tag
  5424. if ($which == 'next')
  5425. {
  5426. $sql .= ' AND t.entry_date >= '.$SESS->cache['weblog']['single_entry_date'].' ';
  5427. $sql .= ' AND IF (t.entry_date = '.$SESS->cache['weblog']['single_entry_date'].', t.entry_id > '.$SESS->cache['weblog']['single_entry_id'].', 1) ';
  5428. }
  5429. else
  5430. {
  5431. $sql .= ' AND t.entry_date <= '.$SESS->cache['weblog']['single_entry_date'].' ';
  5432. $sql .= ' AND IF (t.entry_date = '.$SESS->cache['weblog']['single_entry_date'].', t.entry_id < '.$SESS->cache['weblog']['single_entry_id'].', 1) ';
  5433. }
  5434. if ($TMPL->fetch_param('show_expired') != 'yes')
  5435. {
  5436. $sql .= " AND (t.expiration_date = 0 OR t.expiration_date > {$timestamp}) ";
  5437. }
  5438. if (USER_BLOG === FALSE)
  5439. {
  5440. $sql .= " AND w.is_user_blog = 'n' AND w.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  5441. if ($blog_name = $TMPL->fetch_param('weblog'))
  5442. {
  5443. $sql .= $FNS->sql_andor_string($blog_name, 'blog_name', 'w')." ";
  5444. }
  5445. }
  5446. else
  5447. {
  5448. $sql .= " AND t.weblog_id = '".UB_BLOG_ID."' ";
  5449. }
  5450. if ($status = $TMPL->fetch_param('status'))
  5451. {
  5452. $status = str_replace('Open', 'open', $status);
  5453. $status = str_replace('Closed', 'closed', $status);
  5454. $sql .= $FNS->sql_andor_string($status, 't.status')." ";
  5455. }
  5456. else
  5457. {
  5458. $sql .= "AND t.status = 'open' ";
  5459. }
  5460. /** ----------------------------------------------
  5461. /** Limit query by category
  5462. /** ----------------------------------------------*/
  5463. if ($TMPL->fetch_param('category'))
  5464. {
  5465. if (stristr($TMPL->fetch_param('category'), '&'))
  5466. {
  5467. /** --------------------------------------
  5468. /** First, we find all entries with these categories
  5469. /** --------------------------------------*/
  5470. $for_sql = (substr($TMPL->fetch_param('category'), 0, 3) == 'not') ? trim(substr($TMPL->fetch_param('category'), 3)) : $TMPL->fetch_param('category');
  5471. $csql = "SELECT exp_category_posts.entry_id, exp_category_posts.cat_id, ".
  5472. str_replace('SELECT', '', $sql).
  5473. $FNS->sql_andor_string(str_replace('&', '|', $for_sql), 'exp_categories.cat_id');
  5474. $results = $DB->query($csql);
  5475. if ($results->num_rows == 0)
  5476. {
  5477. return;
  5478. }
  5479. $type = 'IN';
  5480. $categories = explode('&', $TMPL->fetch_param('category'));
  5481. $entry_array = array();
  5482. if (substr($categories['0'], 0, 3) == 'not')
  5483. {
  5484. $type = 'NOT IN';
  5485. $categories['0'] = trim(substr($categories['0'], 3));
  5486. }
  5487. foreach($results->result as $row)
  5488. {
  5489. $entry_array[$row['cat_id']][] = $row['entry_id'];
  5490. }
  5491. if (sizeof($entry_array) < 2 OR sizeof(array_diff($categories, array_keys($entry_array))) > 0)
  5492. {
  5493. return;
  5494. }
  5495. $chosen = call_user_func_array('array_intersect', $entry_array);
  5496. if (sizeof($chosen) == 0)
  5497. {
  5498. return;
  5499. }
  5500. $sql .= "AND t.entry_id ".$type." ('".implode("','", $chosen)."') ";
  5501. }
  5502. else
  5503. {
  5504. if (substr($TMPL->fetch_param('category'), 0, 3) == 'not' && $TMPL->fetch_param('uncategorized_entries') !== 'n')
  5505. {
  5506. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category'), 'exp_categories.cat_id', '', TRUE)." ";
  5507. }
  5508. else
  5509. {
  5510. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category'), 'exp_categories.cat_id')." ";
  5511. }
  5512. }
  5513. }
  5514. if ($TMPL->fetch_param('category_group'))
  5515. {
  5516. if (substr($TMPL->fetch_param('category_group'), 0, 3) == 'not' && $TMPL->fetch_param('uncategorized_entries') !== 'n')
  5517. {
  5518. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category_group'), 'exp_categories.group_id', '', TRUE)." ";
  5519. }
  5520. else
  5521. {
  5522. $sql .= $FNS->sql_andor_string($TMPL->fetch_param('category_group'), 'exp_categories.group_id')." ";
  5523. }
  5524. }
  5525. $sql .= " ORDER BY t.entry_date {$sort}, t.entry_id {$sort} LIMIT 1";
  5526. $query = $DB->query($sql);
  5527. if ($query->num_rows == 0)
  5528. {
  5529. return;
  5530. }
  5531. /** ---------------------------------------
  5532. /** Replace variables
  5533. /** ---------------------------------------*/
  5534. if (strpos($TMPL->tagdata, LD.'path=') !== FALSE)
  5535. {
  5536. $path = (preg_match("#".LD."path=(.+?)".RD."#", $TMPL->tagdata, $match)) ? $FNS->create_url($match['1']) : $FNS->create_url("SITE_INDEX");
  5537. $path .= '/'.$query->row['url_title'].'/';
  5538. $TMPL->tagdata = preg_replace("#".LD."path=.+?".RD."#", $path, $TMPL->tagdata);
  5539. }
  5540. if (strpos($TMPL->tagdata, LD.'id_path=') !== FALSE)
  5541. {
  5542. $id_path = (preg_match("#".LD."id_path=(.+?)".RD."#", $TMPL->tagdata, $match)) ? $FNS->create_url($match['1']) : $FNS->create_url("SITE_INDEX");
  5543. $id_path .= '/'.$query->row['entry_id'].'/';
  5544. $TMPL->tagdata = preg_replace("#".LD."id_path=.+?".RD."#", $id_path, $TMPL->tagdata);
  5545. }
  5546. if (strpos($TMPL->tagdata, LD.'url_title') !== FALSE)
  5547. {
  5548. $TMPL->tagdata = str_replace(LD.'url_title'.RD, $query->row['url_title'], $TMPL->tagdata);
  5549. }
  5550. if (strpos($TMPL->tagdata, LD.'entry_id') !== FALSE)
  5551. {
  5552. $TMPL->tagdata = str_replace(LD.'entry_id'.RD, $query->row['entry_id'], $TMPL->tagdata);
  5553. }
  5554. if (strpos($TMPL->tagdata, LD.'title') !== FALSE)
  5555. {
  5556. $TMPL->tagdata = str_replace(LD.'title'.RD, $query->row['title'], $TMPL->tagdata);
  5557. }
  5558. if (strpos($TMPL->tagdata, '_entry->title') !== FALSE)
  5559. {
  5560. $TMPL->tagdata = preg_replace('/'.LD.'(?:next|prev)_entry->title'.RD.'/', $query->row['title'], $TMPL->tagdata);
  5561. }
  5562. return $FNS->remove_double_slashes(stripslashes($TMPL->tagdata));
  5563. }
  5564. /* END */
  5565. /** ----------------------------------------
  5566. /** Weblog "month links"
  5567. /** ----------------------------------------*/
  5568. function month_links()
  5569. {
  5570. global $TMPL, $LOC, $FNS, $REGX, $DB, $LANG, $SESS;
  5571. $return = '';
  5572. /** ----------------------------------------
  5573. /** Build query
  5574. /** ----------------------------------------*/
  5575. // Fetch the timezone array and calculate the offset so we can localize the month/year
  5576. $zones = $LOC->zones();
  5577. $offset = ( ! isset($zones[$SESS->userdata['timezone']]) || $zones[$SESS->userdata['timezone']] == '') ? 0 : ($zones[$SESS->userdata['timezone']]*60*60);
  5578. if (substr($offset, 0, 1) == '-')
  5579. {
  5580. $calc = 'entry_date - '.substr($offset, 1);
  5581. }
  5582. elseif (substr($offset, 0, 1) == '+')
  5583. {
  5584. $calc = 'entry_date + '.substr($offset, 1);
  5585. }
  5586. else
  5587. {
  5588. $calc = 'entry_date + '.$offset;
  5589. }
  5590. $sql = "SELECT DISTINCT year(FROM_UNIXTIME(".$calc.")) AS year,
  5591. MONTH(FROM_UNIXTIME(".$calc.")) AS month
  5592. FROM exp_weblog_titles
  5593. WHERE entry_id != ''
  5594. AND site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  5595. $timestamp = ($TMPL->cache_timestamp != '') ? $LOC->set_gmt($TMPL->cache_timestamp) : $LOC->now;
  5596. if ($TMPL->fetch_param('show_future_entries') != 'yes')
  5597. {
  5598. $sql .= " AND exp_weblog_titles.entry_date < ".$timestamp." ";
  5599. }
  5600. if ($TMPL->fetch_param('show_expired') != 'yes')
  5601. {
  5602. $sql .= " AND (exp_weblog_titles.expiration_date = 0 OR exp_weblog_titles.expiration_date > ".$timestamp.") ";
  5603. }
  5604. /** ----------------------------------------------
  5605. /** Limit to/exclude specific weblogs
  5606. /** ----------------------------------------------*/
  5607. if (USER_BLOG !== FALSE)
  5608. {
  5609. $sql .= "AND weblog_id = '".UB_BLOG_ID."' ";
  5610. }
  5611. else
  5612. {
  5613. if ($weblog = $TMPL->fetch_param('weblog'))
  5614. {
  5615. $wsql = "SELECT weblog_id FROM exp_weblogs WHERE is_user_blog = 'n' AND site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  5616. $wsql .= $FNS->sql_andor_string($weblog, 'blog_name');
  5617. $query = $DB->query($wsql);
  5618. if ($query->num_rows > 0)
  5619. {
  5620. $sql .= " AND ";
  5621. if ($query->num_rows == 1)
  5622. {
  5623. $sql .= "weblog_id = '".$query->row['weblog_id']."' ";
  5624. }
  5625. else
  5626. {
  5627. $sql .= "(";
  5628. foreach ($query->result as $row)
  5629. {
  5630. $sql .= "weblog_id = '".$row['weblog_id']."' OR ";
  5631. }
  5632. $sql = substr($sql, 0, - 3);
  5633. $sql .= ") ";
  5634. }
  5635. }
  5636. }
  5637. }
  5638. /** ----------------------------------------------
  5639. /** Add status declaration
  5640. /** ----------------------------------------------*/
  5641. if ($status = $TMPL->fetch_param('status'))
  5642. {
  5643. $status = str_replace('Open', 'open', $status);
  5644. $status = str_replace('Closed', 'closed', $status);
  5645. $sstr = $FNS->sql_andor_string($status, 'status');
  5646. if ( ! preg_match("#\'closed\'#i", $sstr))
  5647. {
  5648. $sstr .= " AND status != 'closed' ";
  5649. }
  5650. $sql .= $sstr;
  5651. }
  5652. else
  5653. {
  5654. $sql .= "AND status = 'open' ";
  5655. }
  5656. $sql .= " ORDER BY entry_date";
  5657. switch ($TMPL->fetch_param('sort'))
  5658. {
  5659. case 'asc' : $sql .= " asc";
  5660. break;
  5661. case 'desc' : $sql .= " desc";
  5662. break;
  5663. default : $sql .= " desc";
  5664. break;
  5665. }
  5666. if (is_numeric($TMPL->fetch_param('limit')))
  5667. {
  5668. $sql .= " LIMIT ".$TMPL->fetch_param('limit');
  5669. }
  5670. $query = $DB->query($sql);
  5671. if ($query->num_rows == 0)
  5672. {
  5673. return '';
  5674. }
  5675. $year_limit = (is_numeric($TMPL->fetch_param('year_limit'))) ? $TMPL->fetch_param('year_limit') : 50;
  5676. $total_years = 0;
  5677. $current_year = '';
  5678. foreach ($query->result as $row)
  5679. {
  5680. $tagdata = $TMPL->tagdata;
  5681. $month = (strlen($row['month']) == 1) ? '0'.$row['month'] : $row['month'];
  5682. $year = $row['year'];
  5683. $month_name = $LOC->localize_month($month);
  5684. /** ----------------------------------------
  5685. /** Dealing with {year_heading}
  5686. /** ----------------------------------------*/
  5687. if (isset($TMPL->var_pair['year_heading']))
  5688. {
  5689. if ($year == $current_year)
  5690. {
  5691. $tagdata = $TMPL->delete_var_pairs('year_heading', 'year_heading', $tagdata);
  5692. }
  5693. else
  5694. {
  5695. $tagdata = $TMPL->swap_var_pairs('year_heading', 'year_heading', $tagdata);
  5696. $total_years++;
  5697. if ($total_years > $year_limit)
  5698. {
  5699. break;
  5700. }
  5701. }
  5702. $current_year = $year;
  5703. }
  5704. /** ---------------------------------------
  5705. /** prep conditionals
  5706. /** ---------------------------------------*/
  5707. $cond = array();
  5708. $cond['month'] = $LANG->line($month_name['1']);
  5709. $cond['month_short'] = $LANG->line($month_name['0']);
  5710. $cond['month_num'] = $month;
  5711. $cond['year'] = $year;
  5712. $cond['year_short'] = substr($year, 2);
  5713. $tagdata = $FNS->prep_conditionals($tagdata, $cond);
  5714. /** ----------------------------------------
  5715. /** parse path
  5716. /** ----------------------------------------*/
  5717. foreach ($TMPL->var_single as $key => $val)
  5718. {
  5719. if (strncmp('path', $key, 4) == 0)
  5720. {
  5721. $tagdata = $TMPL->swap_var_single(
  5722. $val,
  5723. $FNS->create_url($FNS->extract_path($key).'/'.$year.'/'.$month),
  5724. $tagdata
  5725. );
  5726. }
  5727. /** ----------------------------------------
  5728. /** parse month (long)
  5729. /** ----------------------------------------*/
  5730. if ($key == 'month')
  5731. {
  5732. $tagdata = $TMPL->swap_var_single($key, $LANG->line($month_name['1']), $tagdata);
  5733. }
  5734. /** ----------------------------------------
  5735. /** parse month (short)
  5736. /** ----------------------------------------*/
  5737. if ($key == 'month_short')
  5738. {
  5739. $tagdata = $TMPL->swap_var_single($key, $LANG->line($month_name['0']), $tagdata);
  5740. }
  5741. /** ----------------------------------------
  5742. /** parse month (numeric)
  5743. /** ----------------------------------------*/
  5744. if ($key == 'month_num')
  5745. {
  5746. $tagdata = $TMPL->swap_var_single($key, $month, $tagdata);
  5747. }
  5748. /** ----------------------------------------
  5749. /** parse year
  5750. /** ----------------------------------------*/
  5751. if ($key == 'year')
  5752. {
  5753. $tagdata = $TMPL->swap_var_single($key, $year, $tagdata);
  5754. }
  5755. /** ----------------------------------------
  5756. /** parse year (short)
  5757. /** ----------------------------------------*/
  5758. if ($key == 'year_short')
  5759. {
  5760. $tagdata = $TMPL->swap_var_single($key, substr($year, 2), $tagdata);
  5761. }
  5762. }
  5763. $return .= trim($tagdata)."\n";
  5764. }
  5765. return $return;
  5766. }
  5767. /* END */
  5768. /** ----------------------------------------
  5769. /** Related Categories Mode
  5770. /** ----------------------------------------*/
  5771. // This function shows entries that are in the same category as
  5772. // the primary entry being shown. It calls the main "weblog entries"
  5773. // function after setting some variables to control the content.
  5774. //
  5775. // Note: We have deprecated the calling of this tag directly via its own tag.
  5776. // Related entries are now shown using the standard {exp:weblog:entries} tag.
  5777. // The reason we're deprecating it is to avoid confusion since the weblog tag
  5778. // now supports relational capability via a pair of {related_entries} tags.
  5779. //
  5780. // To show "related entries" the following parameter is added to the {exp:weblog:entries} tag:
  5781. //
  5782. // related_categories_mode="on"
  5783. function related_entries()
  5784. {
  5785. global $DB, $IN, $TMPL, $LOC, $FNS;
  5786. if ($this->QSTR == '')
  5787. {
  5788. return false;
  5789. }
  5790. $qstring = $this->QSTR;
  5791. /** --------------------------------------
  5792. /** Remove page number
  5793. /** --------------------------------------*/
  5794. if (preg_match("#/P\d+#", $qstring, $match))
  5795. {
  5796. $qstring = $FNS->remove_double_slashes(str_replace($match['0'], '', $qstring));
  5797. }
  5798. /** --------------------------------------
  5799. /** Remove "N"
  5800. /** --------------------------------------*/
  5801. if (preg_match("#/N(\d+)#", $qstring, $match))
  5802. {
  5803. $qstring = $FNS->remove_double_slashes(str_replace($match['0'], '', $qstring));
  5804. }
  5805. /** --------------------------------------
  5806. /** Make sure to only get one segment
  5807. /** --------------------------------------*/
  5808. if (strstr($qstring, '/'))
  5809. {
  5810. $qstring = substr($qstring, 0, strpos($qstring, '/'));
  5811. }
  5812. /** ----------------------------------
  5813. /** Find Categories for Entry
  5814. /** ----------------------------------*/
  5815. $sql = "SELECT exp_categories.cat_id, exp_categories.cat_name
  5816. FROM exp_weblog_titles
  5817. INNER JOIN exp_category_posts ON exp_weblog_titles.entry_id = exp_category_posts.entry_id
  5818. INNER JOIN exp_categories ON exp_category_posts.cat_id = exp_categories.cat_id
  5819. WHERE exp_categories.cat_id IS NOT NULL
  5820. AND exp_weblog_titles.site_id IN ('".implode("','", $TMPL->site_ids)."') ";
  5821. $sql .= ( ! is_numeric($qstring)) ? "AND exp_weblog_titles.url_title = '".$DB->escape_str($qstring)."' " : "AND exp_weblog_titles.entry_id = '".$DB->escape_str($qstring)."' ";
  5822. $query = $DB->query($sql);
  5823. if ($query->num_rows == 0)
  5824. {
  5825. return $TMPL->no_results();
  5826. }
  5827. /** ----------------------------------
  5828. /** Build category array
  5829. /** ----------------------------------*/
  5830. $cat_array = array();
  5831. // We allow the option of adding or subtracting cat_id's
  5832. $categories = ( ! $TMPL->fetch_param('category')) ? '' : $TMPL->fetch_param('category');
  5833. if (strncmp('not ', $categories, 4) == 0)
  5834. {
  5835. $categories = substr($categories, 4);
  5836. $not_categories = explode('|',$categories);
  5837. }
  5838. else
  5839. {
  5840. $add_categories = explode('|',$categories);
  5841. }
  5842. foreach($query->result as $row)
  5843. {
  5844. if ( ! isset($not_categories) || array_search($row['cat_id'], $not_categories) === false)
  5845. {
  5846. $cat_array[] = $row['cat_id'];
  5847. }
  5848. }
  5849. // User wants some categories added, so we add these cat_id's
  5850. if (isset($add_categories) && sizeof($add_categories) > 0)
  5851. {
  5852. foreach($add_categories as $cat_id)
  5853. {
  5854. $cat_array[] = $cat_id;
  5855. }
  5856. }
  5857. // Just in case
  5858. $cat_array = array_unique($cat_array);
  5859. if (sizeof($cat_array) == 0)
  5860. {
  5861. return $TMPL->no_results();
  5862. }
  5863. /** ----------------------------------
  5864. /** Build category string
  5865. /** ----------------------------------*/
  5866. $cats = '';
  5867. foreach($cat_array as $cat_id)
  5868. {
  5869. if ($cat_id != '')
  5870. {
  5871. $cats .= $cat_id.'|';
  5872. }
  5873. }
  5874. $cats = substr($cats, 0, -1);
  5875. /** ----------------------------------
  5876. /** Manually set paramters
  5877. /** ----------------------------------*/
  5878. $TMPL->tagparams['category'] = $cats;
  5879. $TMPL->tagparams['dynamic'] = 'off';
  5880. $TMPL->tagparams['rdf'] = 'off';
  5881. $TMPL->tagparams['not_entry_id'] = $qstring; // Exclude the current entry
  5882. // Set user submitted paramters
  5883. $params = array('weblog', 'username', 'status', 'orderby', 'sort');
  5884. foreach ($params as $val)
  5885. {
  5886. if ($TMPL->fetch_param($val) != FALSE)
  5887. {
  5888. $TMPL->tagparams[$val] = $TMPL->fetch_param($val);
  5889. }
  5890. }
  5891. if ( ! is_numeric($TMPL->fetch_param('limit')))
  5892. {
  5893. $TMPL->tagparams['limit'] = 10;
  5894. }
  5895. /** ----------------------------------
  5896. /** Run the weblog parser
  5897. /** ----------------------------------*/
  5898. $this->initialize();
  5899. $this->entry_id = '';
  5900. $qstring = '';
  5901. if ($this->enable['custom_fields'] == TRUE && $TMPL->fetch_param('custom_fields') == 'on')
  5902. {
  5903. $this->fetch_custom_weblog_fields();
  5904. }
  5905. $this->build_sql_query();
  5906. if ($this->sql == '')
  5907. {
  5908. return $TMPL->no_results();
  5909. }
  5910. $this->query = $DB->query($this->sql);
  5911. if ($this->query->num_rows == 0)
  5912. {
  5913. return $TMPL->no_results();
  5914. }
  5915. if ( ! class_exists('Typography'))
  5916. {
  5917. require PATH_CORE.'core.typography'.EXT;
  5918. }
  5919. $this->TYPE = new Typography;
  5920. $this->TYPE->convert_curly = FALSE;
  5921. if ($TMPL->fetch_param('member_data') !== FALSE && $TMPL->fetch_param('member_data') == 'on')
  5922. {
  5923. $this->fetch_custom_member_fields();
  5924. }
  5925. $this->parse_weblog_entries();
  5926. return $this->return_data;
  5927. }
  5928. /* END */
  5929. /** ---------------------------------------
  5930. /** Fetch Disable Parameter
  5931. /** ---------------------------------------*/
  5932. function _fetch_disable_param()
  5933. {
  5934. global $TMPL;
  5935. $this->enable = array(
  5936. 'categories' => TRUE,
  5937. 'category_fields' => TRUE,
  5938. 'custom_fields' => TRUE,
  5939. 'member_data' => TRUE,
  5940. 'pagination' => TRUE,
  5941. 'trackbacks' => TRUE
  5942. );
  5943. if ($disable = $TMPL->fetch_param('disable'))
  5944. {
  5945. if (strpos($disable, '|') !== FALSE)
  5946. {
  5947. foreach (explode("|", $disable) as $val)
  5948. {
  5949. if (isset($this->enable[$val]))
  5950. {
  5951. $this->enable[$val] = FALSE;
  5952. }
  5953. }
  5954. }
  5955. elseif (isset($this->enable[$disable]))
  5956. {
  5957. $this->enable[$disable] = FALSE;
  5958. }
  5959. }
  5960. }
  5961. /* END */
  5962. /** ----------------------------------------
  5963. /** Weblog Calendar
  5964. /** ----------------------------------------*/
  5965. function calendar()
  5966. {
  5967. global $EXT;
  5968. // -------------------------------------------
  5969. // 'weblog_module_calendar_start' hook.
  5970. // - Rewrite the displaying of the calendar tag
  5971. //
  5972. if ($EXT->active_hook('weblog_module_calendar_start') === TRUE)
  5973. {
  5974. $edata = $EXT->call_extension('weblog_module_calendar_start');
  5975. if ($EXT->end_script === TRUE) return $edata;
  5976. }
  5977. //
  5978. // -------------------------------------------
  5979. if ( ! class_exists('Weblog_calendar'))
  5980. {
  5981. require PATH_MOD.'weblog/mod.weblog_calendar.php';
  5982. }
  5983. $WC = new Weblog_calendar();
  5984. return $WC->calendar();
  5985. }
  5986. /* END */
  5987. /** ----------------------------------------
  5988. /** Trackback RDF
  5989. /** ----------------------------------------*/
  5990. function trackback_rdf($TB)
  5991. {
  5992. return "<!--
  5993. <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"
  5994. xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\"
  5995. xmlns:dc=\"http://purl.org/dc/elements/1.1/\">
  5996. <rdf:Description
  5997. rdf:about=\"".$TB['about']."\"
  5998. trackback:ping=\"".$TB['ping']."\"
  5999. dc:title=\"".$TB['title']."\"
  6000. dc:identifier=\"".$TB['identifier']."\"
  6001. dc:subject=\"".$TB['subject']."\"
  6002. dc:description=\"".$TB['description']."\"
  6003. dc:creator=\"".$TB['creator']."\"
  6004. dc:date=\"".$TB['date']."\" />
  6005. </rdf:RDF>
  6006. -->";
  6007. }
  6008. /* END */
  6009. /** ----------------------------------------
  6010. /** Insert a new weblog entry
  6011. /** ----------------------------------------*/
  6012. // This function serves dual purpose:
  6013. // 1. It allows submitted data to be previewed
  6014. // 2. It allows submitted data to be inserted
  6015. function insert_new_entry()
  6016. {
  6017. if ( ! class_exists('Weblog_standalone'))
  6018. {
  6019. require PATH_MOD.'weblog/mod.weblog_standalone.php';
  6020. }
  6021. $WS = new Weblog_standalone();
  6022. $WS->insert_new_entry();
  6023. }
  6024. /* END */
  6025. /** ----------------------------------------
  6026. /** Stand-alone version of the entry form
  6027. /** ----------------------------------------*/
  6028. function entry_form($return_form = FALSE, $captcha = '')
  6029. {
  6030. if ( ! class_exists('Weblog_standalone'))
  6031. {
  6032. require PATH_MOD.'weblog/mod.weblog_standalone.php';
  6033. }
  6034. $WS = new Weblog_standalone();
  6035. return $WS->entry_form($return_form, $captcha);
  6036. }
  6037. /* END */
  6038. }
  6039. // END CLASS
  6040. ?>