PageRenderTime 66ms CodeModel.GetById 21ms 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

Large files files are truncated, but you can click here to view the full 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_da…

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