PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/html/AppCode/expressionengine/modules/comment/mod.comment.php

https://github.com/w3bg/www.hsifin.com
PHP | 3324 lines | 2126 code | 623 blank | 575 comment | 544 complexity | b5f2cc077858179941b24ddac8141f17 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Comment Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Modules
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class Comment {
  24. // Maximum number of comments. This is a safety valve
  25. // in case the user doesn't specify a maximum
  26. var $limit = 100;
  27. // Show anchor?
  28. // TRUE/FALSE
  29. // Determines whether to show the <a name> anchor above each comment
  30. var $show_anchor = FALSE;
  31. // Comment Expiration Mode
  32. // 0 - Comments only expire if the comment expiration field in the PUBLISH page contains a value.
  33. // 1 - If the comment expiration field is blank, comments will still expire if the global preference
  34. // is set in the Channel Preferences page. Use this option only if you used EE prior to
  35. // version 1.1 and you want your old comments to expire.
  36. var $comment_expiration_mode = 0;
  37. /**
  38. * Constructor
  39. *
  40. * @access public
  41. */
  42. function Comment()
  43. {
  44. // Make a local reference to the ExpressionEngine super object
  45. $this->EE =& get_instance();
  46. $fields = array('name', 'email', 'url', 'location', 'comment');
  47. foreach ($fields as $val)
  48. {
  49. if (isset($_POST[$val] ))
  50. {
  51. $_POST[$val] = $this->EE->functions->encode_ee_tags($_POST[$val], TRUE);
  52. if ($val == 'comment')
  53. {
  54. $_POST[$val] = $this->EE->security->xss_clean($_POST[$val]);
  55. }
  56. }
  57. }
  58. }
  59. // --------------------------------------------------------------------
  60. /**
  61. * Comment Entries
  62. *
  63. * @access public
  64. * @return string
  65. */
  66. function entries()
  67. {
  68. $return = '';
  69. $current_page = '';
  70. $qstring = $this->EE->uri->query_string;
  71. $uristr = $this->EE->uri->uri_string;
  72. $switch = array();
  73. $search_link = '';
  74. // Pagination variables
  75. $paginate = FALSE;
  76. $paginate_data = '';
  77. $pagination_links = '';
  78. $page_next = '';
  79. $page_previous = '';
  80. $current_page = 0;
  81. $t_current_page = '';
  82. $total_pages = 1;
  83. if ($this->EE->TMPL->fetch_param('dynamic') == 'no')
  84. {
  85. $dynamic = FALSE;
  86. }
  87. else
  88. {
  89. $dynamic = TRUE;
  90. }
  91. $force_entry = FALSE;
  92. if ($this->EE->TMPL->fetch_param('entry_id') !== FALSE OR $this->EE->TMPL->fetch_param('url_title') !== FALSE OR $this->EE->TMPL->fetch_param('comment_id') !== FALSE)
  93. {
  94. $force_entry = TRUE;
  95. }
  96. /** ----------------------------------------------
  97. /** Do we allow dynamic POST variables to set parameters?
  98. /** ----------------------------------------------*/
  99. if ($this->EE->TMPL->fetch_param('dynamic_parameters') !== FALSE AND isset($_POST) AND count($_POST) > 0)
  100. {
  101. foreach (explode('|', $this->EE->TMPL->fetch_param('dynamic_parameters')) as $var)
  102. {
  103. if (isset($_POST[$var]) AND in_array($var, array('channel', 'limit', 'sort', 'orderby')))
  104. {
  105. $this->EE->TMPL->tagparams[$var] = $_POST[$var];
  106. }
  107. }
  108. }
  109. /** --------------------------------------
  110. /** Parse page number
  111. /** --------------------------------------*/
  112. // We need to strip the page number from the URL for two reasons:
  113. // 1. So we can create pagination links
  114. // 2. So it won't confuse the query with an improper proper ID
  115. if ( ! $dynamic)
  116. {
  117. if (preg_match("#(^|/)N(\d+)(/|$)#i", $qstring, $match))
  118. {
  119. $current_page = $match['2'];
  120. $uristr = trim($this->EE->functions->remove_double_slashes(str_replace($match['0'], '/', $uristr)), '/');
  121. }
  122. }
  123. else
  124. {
  125. if (preg_match("#(^|/)P(\d+)(/|$)#", $qstring, $match))
  126. {
  127. $current_page = $match['2'];
  128. $uristr = $this->EE->functions->remove_double_slashes(str_replace($match['0'], '/', $uristr));
  129. $qstring = trim($this->EE->functions->remove_double_slashes(str_replace($match['0'], '/', $qstring)), '/');
  130. }
  131. }
  132. if ($dynamic == TRUE OR $force_entry == TRUE)
  133. {
  134. // Fetch channel_ids if appropriate
  135. $channel_ids = array();
  136. if ($channel = $this->EE->TMPL->fetch_param('channel') OR $this->EE->TMPL->fetch_param('site'))
  137. {
  138. $this->EE->db->select('channel_id');
  139. $this->EE->db->where_in('site_id', $this->EE->TMPL->site_ids);
  140. if ($channel !== FALSE)
  141. {
  142. $this->EE->functions->ar_andor_string($channel, 'channel_name');
  143. }
  144. $channels = $this->EE->db->get('channels');
  145. if ($channels->num_rows() == 0)
  146. {
  147. return false;
  148. }
  149. else
  150. {
  151. foreach($channels->result_array() as $row)
  152. {
  153. $channel_ids[] = $row['channel_id'];
  154. }
  155. }
  156. }
  157. // Check if an entry_id or url_title was specified
  158. if ($entry_id = $this->EE->TMPL->fetch_param('entry_id'))
  159. {
  160. $sql = substr($this->EE->functions->sql_andor_string($entry_id, 'entry_id'), 4);
  161. $this->EE->db->where($sql, NULL, FALSE);
  162. }
  163. elseif ($url_title = $this->EE->TMPL->fetch_param('url_title'))
  164. {
  165. $sql = substr($this->EE->functions->sql_andor_string($url_title, 'url_title'), 4);
  166. $this->EE->db->where($sql, NULL, FALSE);
  167. }
  168. else
  169. {
  170. // If there is a slash in the entry ID we'll kill everything after it.
  171. $entry_id = trim($qstring);
  172. $entry_id = preg_replace("#/.+#", "", $entry_id);
  173. // Have to choose between id or url title
  174. if ( ! is_numeric($entry_id))
  175. {
  176. $this->EE->db->where('url_title', $entry_id);
  177. }
  178. else
  179. {
  180. $this->EE->db->where('entry_id', $entry_id);
  181. }
  182. }
  183. /** ----------------------------------------
  184. /** Do we have a valid entry ID number?
  185. /** ----------------------------------------*/
  186. $timestamp = ($this->EE->TMPL->cache_timestamp != '') ? $this->EE->localize->set_gmt($this->EE->TMPL->cache_timestamp) : $this->EE->localize->now;
  187. $this->EE->db->select('entry_id, channel_titles.channel_id');
  188. $this->EE->db->where('channel_titles.channel_id = '.$this->EE->db->dbprefix('channels').'.channel_id');
  189. $this->EE->db->where_in('channel_titles.site_id', $this->EE->TMPL->site_ids);
  190. if ($this->EE->TMPL->fetch_param('show_expired') !== 'yes')
  191. {
  192. $date_where = "(".$this->EE->db->protect_identifiers('expiration_date')." = 0 OR "
  193. .$this->EE->db->protect_identifiers('expiration_date')." > {$timestamp})";
  194. $this->EE->db->where($date_where);
  195. }
  196. if ($author_id = $this->EE->TMPL->fetch_param('author_id'))
  197. {
  198. $this->EE->db->where('author_id', $author_id);
  199. }
  200. if ($e_status = $this->EE->TMPL->fetch_param('entry_status'))
  201. {
  202. $e_status = str_replace('Open', 'open', $e_status);
  203. $e_status = str_replace('Closed', 'closed', $e_status);
  204. $sql = $this->EE->functions->sql_andor_string($e_status, 'status');
  205. if (stristr($sql, "'closed'") === FALSE)
  206. {
  207. $sql .= " AND status != 'closed' ";
  208. }
  209. // We need to drop the leading AND from the generated string
  210. $sql = substr($sql, 4);
  211. $this->EE->db->where($sql, NULL, FALSE);
  212. }
  213. else
  214. {
  215. $this->EE->db->where('status !=', 'closed');
  216. }
  217. /** ----------------------------------------------
  218. /** Limit to/exclude specific channels
  219. /** ----------------------------------------------*/
  220. if (count($channel_ids) == 1)
  221. {
  222. $this->EE->db->where('channel_titles.channel_id', $channel_ids['0']);
  223. }
  224. elseif (count($channel_ids) > 1)
  225. {
  226. $this->EE->db->where_in('channel_titles.channel_id', $channel_ids);
  227. }
  228. $this->EE->db->from('channel_titles');
  229. $this->EE->db->from('channels');
  230. $query = $this->EE->db->get();
  231. // Bad ID? See ya!
  232. if ($query->num_rows() == 0)
  233. {
  234. return FALSE;
  235. }
  236. // We'll reassign the entry IDs so they're the true numeric ID
  237. foreach($query->result_array() as $row)
  238. {
  239. $entry_ids[] = $row['entry_id'];
  240. }
  241. }
  242. // If the comment tag is being used in freeform mode
  243. // we need to fetch the channel ID numbers
  244. if ( ! $dynamic)
  245. {
  246. if ($channel = $this->EE->TMPL->fetch_param('channel') OR $this->EE->TMPL->fetch_param('site'))
  247. {
  248. $this->EE->db->select('channel_id');
  249. $this->EE->db->where_in('site_id', $this->EE->TMPL->site_ids);
  250. if ($channel !== FALSE)
  251. {
  252. $this->EE->functions->ar_andor_string($channel, 'channel_name');
  253. }
  254. $query = $this->EE->db->get('channels');
  255. if ($query->num_rows() == 0)
  256. {
  257. return $this->EE->TMPL->no_results();
  258. }
  259. else
  260. {
  261. // Store the query components in the AR cache so we don't need to
  262. // recompile them after we run count_all_results for pagination.
  263. $this->EE->db->start_cache();
  264. if ($query->num_rows() == 1)
  265. {
  266. $this->EE->db->where('channel_id', $query->row('channel_id'));
  267. }
  268. else
  269. {
  270. $ids = array();
  271. foreach ($query->result_array() as $row)
  272. {
  273. $ids[] = $row['channel_id'];
  274. }
  275. $this->EE->db->where_in('channel_id', $ids);
  276. }
  277. }
  278. }
  279. }
  280. /** ----------------------------------------
  281. /** Set sorting and limiting
  282. /** ----------------------------------------*/
  283. if ( ! $dynamic)
  284. {
  285. $limit = ( ! $this->EE->TMPL->fetch_param('limit')) ? 100 : $this->EE->TMPL->fetch_param('limit');
  286. $sort = ( ! $this->EE->TMPL->fetch_param('sort')) ? 'desc' : $this->EE->TMPL->fetch_param('sort');
  287. }
  288. else
  289. {
  290. $limit = ( ! $this->EE->TMPL->fetch_param('limit')) ? $this->limit : $this->EE->TMPL->fetch_param('limit');
  291. $sort = ( ! $this->EE->TMPL->fetch_param('sort')) ? 'asc' : $this->EE->TMPL->fetch_param('sort');
  292. }
  293. $allowed_sorts = array('date', 'email', 'location', 'name', 'url');
  294. /** ----------------------------------------
  295. /** Fetch comment ID numbers
  296. /** ----------------------------------------*/
  297. $temp = array();
  298. $i = 0;
  299. // Left this here for backward compatibility
  300. // We need to deprecate the "order_by" parameter
  301. if ($this->EE->TMPL->fetch_param('orderby') != '')
  302. {
  303. $order_by = $this->EE->TMPL->fetch_param('orderby');
  304. }
  305. else
  306. {
  307. $order_by = $this->EE->TMPL->fetch_param('order_by');
  308. }
  309. $random = ($order_by == 'random') ? TRUE : FALSE;
  310. $order_by = ($order_by == 'date' OR ! in_array($order_by, $allowed_sorts)) ? 'comment_date' : $order_by;
  311. $this->EE->db->select('comment_date, comment_id');
  312. $comment_sql = FALSE;
  313. if ($status = $this->EE->TMPL->fetch_param('status'))
  314. {
  315. $status = strtolower($status);
  316. $status = str_replace('open', 'o', $status);
  317. $status = str_replace('closed', 'c', $status);
  318. $status = str_replace('pending', 'p', $status);
  319. $comment_sql = $this->EE->functions->sql_andor_string($status, 'status');
  320. if (stristr($comment_sql, "'c'") === FALSE)
  321. {
  322. $comment_sql .= " AND status != 'c' ";
  323. }
  324. // We need to drop the leading AND from the generated string
  325. $comment_sql = substr($comment_sql, 4);
  326. $this->EE->db->where($comment_sql, NULL, FALSE);
  327. }
  328. else
  329. {
  330. $this->EE->db->where('status', 'o');
  331. }
  332. if ( ! $dynamic)
  333. {
  334. // When we are only showing comments and it is not based on an entry id or url title
  335. // in the URL, we can make the query much more efficient and save some work.
  336. if (isset($entry_ids) && count($entry_ids) > 0)
  337. {
  338. $this->EE->db->where_in('entry_id', $entry_ids);
  339. }
  340. $total_rows = $this->EE->db->count_all_results('comments');
  341. // We lose these in the counting process
  342. $this->EE->db->select('comment_date, comment_id');
  343. if ($comment_sql)
  344. {
  345. $this->EE->db->where($comment_sql, NULL, FALSE);
  346. }
  347. else
  348. {
  349. $this->EE->db->where('status', 'o');
  350. }
  351. if (isset($entry_ids) && count($entry_ids) > 0)
  352. {
  353. $this->EE->db->where_in('entry_id', $entry_ids);
  354. }
  355. $this_sort = ($random) ? 'random' : strtolower($sort);
  356. $this_page = ($current_page == '' OR ($limit > 1 AND $current_page == 1)) ? 0 : $current_page;
  357. $this->EE->db->order_by($order_by, $this_sort);
  358. $this->EE->db->limit($limit, $this_page);
  359. }
  360. else
  361. {
  362. // Force entry may result in multiple entry ids
  363. if (isset($entry_ids) && count($entry_ids) > 0)
  364. {
  365. $this->EE->db->where_in('entry_id', $entry_ids);
  366. }
  367. else
  368. {
  369. $this->EE->db->where('entry_id', $entry_id);
  370. }
  371. $this_sort = ($random) ? 'random' : strtolower($sort);
  372. $this->EE->db->order_by($order_by, $this_sort);
  373. }
  374. $query = $this->EE->db->get('comments');
  375. $result_ids = array();
  376. if ($query->num_rows() > 0)
  377. {
  378. foreach ($query->result_array() as $row)
  379. {
  380. $result_ids[] = $row['comment_id'];
  381. }
  382. }
  383. // We are done with this
  384. $this->EE->db->flush_cache();
  385. $this->EE->db->stop_cache();
  386. /** ------------------------------------
  387. /** No results? No reason to continue...
  388. /** ------------------------------------*/
  389. if (count($result_ids) == 0)
  390. {
  391. return $this->EE->TMPL->no_results();
  392. }
  393. /** ---------------------------------
  394. /** Do we need pagination?
  395. /** ---------------------------------*/
  396. // When showing only comments and no using the URL, then we already have this value
  397. if ($dynamic)
  398. {
  399. $total_rows = count($result_ids);
  400. }
  401. if (preg_match("/".LD."paginate(.*?)".RD."(.+?)".LD.'\/'."paginate".RD."/s", $this->EE->TMPL->tagdata, $match))
  402. {
  403. $paginate = TRUE;
  404. $paginate_data = $match['2'];
  405. $anchor = '';
  406. if ($match['1'] != '')
  407. {
  408. if (preg_match("/anchor.*?=[\"|\'](.+?)[\"|\']/", $match['1'], $amatch))
  409. {
  410. $anchor = '#'.$amatch['1'];
  411. }
  412. }
  413. $this->EE->TMPL->tagdata = preg_replace("/".LD."paginate.*?".RD.".+?".LD.'\/'."paginate".RD."/s", "", $this->EE->TMPL->tagdata);
  414. $current_page = ($current_page == '' OR ($limit > 1 AND $current_page == 1)) ? 0 : $current_page;
  415. if ($current_page > $total_rows)
  416. {
  417. $current_page = 0;
  418. }
  419. $t_current_page = floor(($current_page / $limit) + 1);
  420. $total_pages = intval(floor($total_rows / $limit));
  421. if ($total_rows % $limit)
  422. $total_pages++;
  423. if ($total_rows > $limit)
  424. {
  425. $this->EE->load->library('pagination');
  426. $deft_tmpl = '';
  427. if ($uristr == '')
  428. {
  429. if ($this->EE->config->item('template_group') == '')
  430. {
  431. $this->EE->db->select('group_name');
  432. $query = $this->EE->db->get_where('template_groups', array('is_site_default' => 'y'));
  433. $deft_tmpl = $query->row('group_name') .'/index';
  434. }
  435. else
  436. {
  437. $deft_tmpl = $this->EE->config->item('template_group').'/';
  438. $deft_tmpl .= ($this->EE->config->item('template') == '') ? 'index' : $this->EE->config->item('template');
  439. }
  440. }
  441. $basepath = $this->EE->functions->remove_double_slashes($this->EE->functions->create_url($uristr, FALSE).'/'.$deft_tmpl);
  442. if ($this->EE->TMPL->fetch_param('paginate_base'))
  443. {
  444. // Load the string helper
  445. $this->EE->load->helper('string');
  446. $pbase = trim_slashes($this->EE->TMPL->fetch_param('paginate_base'));
  447. $pbase = str_replace("/index", "/", $pbase);
  448. if ( ! strstr($basepath, $pbase))
  449. {
  450. $basepath = $this->EE->functions->remove_double_slashes($basepath.'/'.$pbase);
  451. }
  452. }
  453. $config['first_url'] = rtrim($basepath, '/').$anchor;
  454. $config['base_url'] = $basepath;
  455. $config['prefix'] = ( ! $dynamic) ? 'N' : 'P';
  456. $config['total_rows'] = $total_rows;
  457. $config['per_page'] = $limit;
  458. $config['cur_page'] = $current_page;
  459. $config['suffix'] = $anchor;
  460. $config['first_link'] = $this->EE->lang->line('pag_first_link');
  461. $config['last_link'] = $this->EE->lang->line('pag_last_link');
  462. // Allows $config['cur_page'] to override
  463. $config['uri_segment'] = 0;
  464. $this->EE->pagination->initialize($config);
  465. $pagination_links = $this->EE->pagination->create_links();
  466. if ((($total_pages * $limit) - $limit) > $current_page)
  467. {
  468. $page_next = $basepath.$config['prefix'].($current_page + $limit).'/';
  469. }
  470. if (($current_page - $limit ) >= 0)
  471. {
  472. $page_previous = $basepath.$config['prefix'].($current_page - $limit).'/';
  473. }
  474. }
  475. else
  476. {
  477. $current_page = '';
  478. }
  479. }
  480. // When only non-dynamic comments are shown, all results are valid as the
  481. // query is restricted with a LIMIT clause
  482. if ($dynamic)
  483. {
  484. if ($current_page == '')
  485. {
  486. $result_ids = array_slice($result_ids, 0, $limit);
  487. }
  488. else
  489. {
  490. $result_ids = array_slice($result_ids, $current_page, $limit);
  491. }
  492. }
  493. /** -----------------------------------
  494. /** Fetch Comments if necessary
  495. /** -----------------------------------*/
  496. $results = $result_ids;
  497. $mfields = array();
  498. /** ----------------------------------------
  499. /** "Search by Member" link
  500. /** ----------------------------------------*/
  501. // We use this with the {member_search_path} variable
  502. $result_path = (preg_match("/".LD."member_search_path\s*=(.*?)".RD."/s", $this->EE->TMPL->tagdata, $match)) ? $match['1'] : 'search/results';
  503. $result_path = str_replace("\"", "", $result_path);
  504. $result_path = str_replace("'", "", $result_path);
  505. $search_link = $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$this->EE->functions->fetch_action_id('Search', 'do_search').'&amp;result_path='.$result_path.'&amp;mbr=';
  506. $this->EE->db->select('comments.comment_id, comments.entry_id, comments.channel_id, comments.author_id, comments.name, comments.email, comments.url, comments.location AS c_location, comments.ip_address, comments.comment_date, comments.edit_date, comments.comment, comments.site_id AS comment_site_id,
  507. members.username, members.group_id, members.location, members.occupation, members.interests, members.aol_im, members.yahoo_im, members.msn_im, members.icq, members.group_id, members.member_id, members.signature, members.sig_img_filename, members.sig_img_width, members.sig_img_height, members.avatar_filename, members.avatar_width, members.avatar_height, members.photo_filename, members.photo_width, members.photo_height,
  508. member_data.*,
  509. channel_titles.title, channel_titles.url_title, channel_titles.author_id AS entry_author_id,
  510. channels.comment_text_formatting, channels.comment_html_formatting, channels.comment_allow_img_urls, channels.comment_auto_link_urls, channels.channel_url, channels.comment_url, channels.channel_title'
  511. );
  512. $this->EE->db->join('channels', 'comments.channel_id = channels.channel_id', 'left');
  513. $this->EE->db->join('channel_titles', 'comments.entry_id = channel_titles.entry_id', 'left');
  514. $this->EE->db->join('members', 'members.member_id = comments.author_id', 'left');
  515. $this->EE->db->join('member_data', 'member_data.member_id = members.member_id', 'left');
  516. $this->EE->db->where_in('comments.comment_id', $result_ids);
  517. $this->EE->db->order_by($order_by, $this_sort);
  518. $query = $this->EE->db->get('comments');
  519. $total_results = $query->num_rows();
  520. if ($query->num_rows() > 0)
  521. {
  522. $i = 0;
  523. foreach ($query->result_array() as $row)
  524. {
  525. $results[$row['comment_id']] = $query->result_array[$i];
  526. $i++;
  527. }
  528. // Potentially a lot of information
  529. $query->free_result();
  530. }
  531. /** ----------------------------------------
  532. /** Fetch custom member field IDs
  533. /** ----------------------------------------*/
  534. $this->EE->db->select('m_field_id, m_field_name');
  535. $query = $this->EE->db->get('member_fields');
  536. if ($query->num_rows() > 0)
  537. {
  538. foreach ($query->result_array() as $row)
  539. {
  540. $mfields[$row['m_field_name']] = $row['m_field_id'];
  541. }
  542. }
  543. /** ----------------------------------------
  544. /** Instantiate Typography class
  545. /** ----------------------------------------*/
  546. $config = ($this->EE->config->item('comment_word_censoring') == 'y') ? array('word_censor' => TRUE) : array();
  547. $this->EE->load->library('typography');
  548. $this->EE->typography->initialize($config);
  549. $this->EE->typography->parse_images = FALSE;
  550. $this->EE->typography->allow_headings = FALSE;
  551. /** ----------------------------------------
  552. /** Fetch all the date-related variables
  553. /** ----------------------------------------*/
  554. $gmt_comment_date = array();
  555. $comment_date = array();
  556. $edit_date = array();
  557. // We do this here to avoid processing cycles in the foreach loop
  558. $date_vars = array('gmt_comment_date', 'comment_date', 'edit_date');
  559. foreach ($date_vars as $val)
  560. {
  561. if (preg_match_all("/".LD.$val."\s+format=[\"'](.*?)[\"']".RD."/s", $this->EE->TMPL->tagdata, $matches))
  562. {
  563. for ($j = 0; $j < count($matches['0']); $j++)
  564. {
  565. $matches['0'][$j] = str_replace(LD, '', $matches['0'][$j]);
  566. $matches['0'][$j] = str_replace(RD, '', $matches['0'][$j]);
  567. switch ($val)
  568. {
  569. case 'comment_date' : $comment_date[$matches['0'][$j]] = $this->EE->localize->fetch_date_params($matches['1'][$j]);
  570. break;
  571. case 'gmt_comment_date' : $gmt_comment_date[$matches['0'][$j]] = $this->EE->localize->fetch_date_params($matches['1'][$j]);
  572. break;
  573. case 'edit_date' : $edit_date[$matches['0'][$j]] = $this->EE->localize->fetch_date_params($matches['1'][$j]);
  574. break;
  575. }
  576. }
  577. }
  578. }
  579. /** ----------------------------------------
  580. /** Protected Variables for Cleanup Routine
  581. /** ----------------------------------------*/
  582. // Since comments do not necessarily require registration, and since
  583. // you are allowed to put member variables in comments, we need to kill
  584. // left-over unparsed junk. The $member_vars array is all of those
  585. // member related variables that should be removed.
  586. $member_vars = array('location', 'occupation', 'interests', 'aol_im', 'yahoo_im', 'msn_im', 'icq',
  587. 'signature', 'sig_img_filename', 'sig_img_width', 'sig_img_height',
  588. 'avatar_filename', 'avatar_width', 'avatar_height',
  589. 'photo_filename', 'photo_width', 'photo_height');
  590. $member_cond_vars = array();
  591. foreach($member_vars as $var)
  592. {
  593. $member_cond_vars[$var] = '';
  594. }
  595. /** ----------------------------------------
  596. /** Start the processing loop
  597. /** ----------------------------------------*/
  598. $item_count = 0;
  599. $relative_count = 0;
  600. $absolute_count = ($current_page == '') ? 0 : $current_page;
  601. foreach ($results as $id => $row)
  602. {
  603. if ( ! is_array($row))
  604. continue;
  605. $relative_count++;
  606. $absolute_count++;
  607. $row['count'] = $relative_count;
  608. $row['absolute_count'] = $absolute_count;
  609. $row['total_comments'] = $total_rows;
  610. $row['total_results'] = $total_results;
  611. // This lets the {if location} variable work
  612. if (isset($row['author_id']))
  613. {
  614. if ($row['author_id'] == 0)
  615. $row['location'] = $row['c_location'];
  616. }
  617. $tagdata = $this->EE->TMPL->tagdata;
  618. // -------------------------------------------
  619. // 'comment_entries_tagdata' hook.
  620. // - Modify and play with the tagdata before everyone else
  621. //
  622. if ($this->EE->extensions->active_hook('comment_entries_tagdata') === TRUE)
  623. {
  624. $tagdata = $this->EE->extensions->call('comment_entries_tagdata', $tagdata, $row);
  625. if ($this->EE->extensions->end_script === TRUE) return $tagdata;
  626. }
  627. //
  628. // -------------------------------------------
  629. /** ----------------------------------------
  630. /** Conditionals
  631. /** ----------------------------------------*/
  632. $cond = array_merge($member_cond_vars, $row);
  633. $cond['comments'] = (substr($id, 0, 1) == 't') ? 'FALSE' : 'TRUE';
  634. $cond['logged_in'] = ($this->EE->session->userdata('member_id') == 0) ? 'FALSE' : 'TRUE';
  635. $cond['logged_out'] = ($this->EE->session->userdata('member_id') != 0) ? 'FALSE' : 'TRUE';
  636. $cond['allow_comments'] = (isset($row['allow_comments']) AND $row['allow_comments'] == 'n') ? 'FALSE' : 'TRUE';
  637. $cond['signature_image'] = ( ! isset($row['sig_img_filename']) OR $row['sig_img_filename'] == '' OR $this->EE->config->item('enable_signatures') == 'n' OR $this->EE->session->userdata('display_signatures') == 'n') ? 'FALSE' : 'TRUE';
  638. $cond['avatar'] = ( ! isset($row['avatar_filename']) OR $row['avatar_filename'] == '' OR $this->EE->config->item('enable_avatars') == 'n' OR $this->EE->session->userdata('display_avatars') == 'n') ? 'FALSE' : 'TRUE';
  639. $cond['photo'] = ( ! isset($row['photo_filename']) OR $row['photo_filename'] == '' OR $this->EE->config->item('enable_photos') == 'n' OR $this->EE->session->userdata('display_photos') == 'n') ? 'FALSE' : 'TRUE';
  640. $cond['is_ignored'] = ( ! isset($row['member_id']) OR ! in_array($row['member_id'], $this->EE->session->userdata['ignore_list'])) ? 'FALSE' : 'TRUE';
  641. $cond['editable'] = FALSE;
  642. $cond['can_moderate_comment'] = FALSE;
  643. if ($this->EE->session->userdata['group_id'] == 1 OR
  644. $this->EE->session->userdata['can_edit_all_comments'] == 'y' OR
  645. ($this->EE->session->userdata['can_edit_own_comments'] == 'y' && $row['entry_author_id'] == $this->EE->session->userdata['member_id']))
  646. {
  647. $cond['editable'] = TRUE;
  648. $cond['can_moderate_comment'] = TRUE;
  649. }
  650. elseif ($this->EE->session->userdata['member_id'] != '0' && $author_id == $this->EE->session->userdata['member_id'])
  651. {
  652. $cond['editable'] = TRUE;
  653. }
  654. if ( isset($mfields) && is_array($mfields) && count($mfields) > 0)
  655. {
  656. foreach($mfields as $key => $value)
  657. {
  658. if (isset($row['m_field_id_'.$value]))
  659. $cond[$key] = $row['m_field_id_'.$value];
  660. }
  661. }
  662. $tagdata = $this->EE->functions->prep_conditionals($tagdata, $cond);
  663. /** ----------------------------------------
  664. /** Parse "single" variables
  665. /** ----------------------------------------*/
  666. foreach ($this->EE->TMPL->var_single as $key => $val)
  667. {
  668. /** ----------------------------------------
  669. /** parse {switch} variable
  670. /** ----------------------------------------*/
  671. if (strncmp($key, 'switch', 6) == 0)
  672. {
  673. $sparam = $this->EE->functions->assign_parameters($key);
  674. $sw = '';
  675. if (isset($sparam['switch']))
  676. {
  677. $sopt = @explode("|", $sparam['switch']);
  678. $sw = $sopt[($relative_count + count($sopt) - 1) % count($sopt)];
  679. }
  680. $tagdata = $this->EE->TMPL->swap_var_single($key, $sw, $tagdata);
  681. }
  682. /** ----------------------------------------
  683. /** parse permalink
  684. /** ----------------------------------------*/
  685. if ($key == 'permalink' && isset($row['comment_id']))
  686. {
  687. $tagdata = $this->EE->TMPL->swap_var_single(
  688. $key,
  689. $this->EE->functions->create_url($uristr.'#'.$row['comment_id'], FALSE),
  690. $tagdata
  691. );
  692. }
  693. /** ----------------------------------------
  694. /** parse comment_path
  695. /** ----------------------------------------*/
  696. if (strncmp($key, 'comment_path', 12) == 0 OR strncmp($key, 'entry_id_path', 13) == 0)
  697. {
  698. $tagdata = $this->EE->TMPL->swap_var_single(
  699. $key,
  700. $this->EE->functions->create_url($this->EE->functions->extract_path($key).'/'.$row['entry_id']),
  701. $tagdata
  702. );
  703. }
  704. /** ----------------------------------------
  705. /** parse title permalink
  706. /** ----------------------------------------*/
  707. if (strncmp($key, 'title_permalink', 15) == 0 OR strncmp($key, 'url_title_path', 14) == 0)
  708. {
  709. $path = ($this->EE->functions->extract_path($key) != '' AND $this->EE->functions->extract_path($key) != 'SITE_INDEX') ? $this->EE->functions->extract_path($key).'/'.$row['url_title'] : $row['url_title'];
  710. $tagdata = $this->EE->TMPL->swap_var_single(
  711. $key,
  712. $this->EE->functions->create_url($path, FALSE),
  713. $tagdata
  714. );
  715. }
  716. /** ----------------------------------------
  717. /** parse comment date
  718. /** ----------------------------------------*/
  719. if (isset($comment_date[$key]) && isset($row['comment_date']))
  720. {
  721. foreach ($comment_date[$key] as $dvar)
  722. {
  723. $val = str_replace($dvar, $this->EE->localize->convert_timestamp($dvar, $row['comment_date'], TRUE), $val);
  724. }
  725. $tagdata = $this->EE->TMPL->swap_var_single($key, $val, $tagdata);
  726. }
  727. /** ----------------------------------------
  728. /** parse GMT comment date
  729. /** ----------------------------------------*/
  730. if (isset($gmt_comment_date[$key]) && isset($row['comment_date']))
  731. {
  732. foreach ($gmt_comment_date[$key] as $dvar)
  733. {
  734. $val = str_replace($dvar, $this->EE->localize->convert_timestamp($dvar, $row['comment_date'], FALSE), $val);
  735. }
  736. $tagdata = $this->EE->TMPL->swap_var_single($key, $val, $tagdata);
  737. }
  738. /** ----------------------------------------
  739. /** parse "last edit" date
  740. /** ----------------------------------------*/
  741. if (isset($edit_date[$key]))
  742. {
  743. if (isset($row['edit_date']))
  744. {
  745. foreach ($edit_date[$key] as $dvar)
  746. $val = str_replace($dvar, $this->EE->localize->convert_timestamp($dvar, $this->EE->localize->timestamp_to_gmt($row['edit_date']), TRUE), $val);
  747. $tagdata = $this->EE->TMPL->swap_var_single($key, $val, $tagdata);
  748. }
  749. }
  750. /** ----------------------------------------
  751. /** {member_search_path}
  752. /** ----------------------------------------*/
  753. if (strncmp($key, 'member_search_path', 18) == 0)
  754. {
  755. $tagdata = $this->EE->TMPL->swap_var_single($key, $search_link.$row['author_id'], $tagdata);
  756. }
  757. // Prep the URL
  758. if (isset($row['url']))
  759. {
  760. $this->EE->load->helper('url');
  761. $row['url'] = prep_url($row['url']);
  762. }
  763. /** ----------------------------------------
  764. /** {username}
  765. /** ----------------------------------------*/
  766. if ($key == "username")
  767. {
  768. $tagdata = $this->EE->TMPL->swap_var_single($val, (isset($row['username'])) ? $row['username'] : '', $tagdata);
  769. }
  770. /** ----------------------------------------
  771. /** {author}
  772. /** ----------------------------------------*/
  773. if ($key == "author")
  774. {
  775. $tagdata = $this->EE->TMPL->swap_var_single($val, (isset($row['name'])) ? $row['name'] : '', $tagdata);
  776. }
  777. /** ----------------------------------------
  778. /** {url_or_email} - Uses Raw Email Address, Like Channel Module
  779. /** ----------------------------------------*/
  780. if ($key == "url_or_email" AND isset($row['url']))
  781. {
  782. $tagdata = $this->EE->TMPL->swap_var_single($val, ($row['url'] != '') ? $row['url'] : $row['email'], $tagdata);
  783. }
  784. /** ----------------------------------------
  785. /** {url_as_author}
  786. /** ----------------------------------------*/
  787. if ($key == "url_as_author" AND isset($row['url']))
  788. {
  789. if ($row['url'] != '')
  790. {
  791. $tagdata = $this->EE->TMPL->swap_var_single($val, "<a href=\"".$row['url']."\">".$row['name']."</a>", $tagdata);
  792. }
  793. else
  794. {
  795. $tagdata = $this->EE->TMPL->swap_var_single($val, $row['name'], $tagdata);
  796. }
  797. }
  798. /** ----------------------------------------
  799. /** {url_or_email_as_author}
  800. /** ----------------------------------------*/
  801. if ($key == "url_or_email_as_author" AND isset($row['url']))
  802. {
  803. if ($row['url'] != '')
  804. {
  805. $tagdata = $this->EE->TMPL->swap_var_single($val, "<a href=\"".$row['url']."\">".$row['name']."</a>", $tagdata);
  806. }
  807. else
  808. {
  809. if ($row['email'] != '')
  810. {
  811. $tagdata = $this->EE->TMPL->swap_var_single($val, $this->EE->typography->encode_email($row['email'], $row['name']), $tagdata);
  812. }
  813. else
  814. {
  815. $tagdata = $this->EE->TMPL->swap_var_single($val, $row['name'], $tagdata);
  816. }
  817. }
  818. }
  819. /** ----------------------------------------
  820. /** {url_or_email_as_link}
  821. /** ----------------------------------------*/
  822. if ($key == "url_or_email_as_link" AND isset($row['url']))
  823. {
  824. if ($row['url'] != '')
  825. {
  826. $tagdata = $this->EE->TMPL->swap_var_single($val, "<a href=\"".$row['url']."\">".$row['url']."</a>", $tagdata);
  827. }
  828. else
  829. {
  830. if ($row['email'] != '')
  831. {
  832. $tagdata = $this->EE->TMPL->swap_var_single($val, $this->EE->typography->encode_email($row['email']), $tagdata);
  833. }
  834. else
  835. {
  836. $tagdata = $this->EE->TMPL->swap_var_single($val, $row['name'], $tagdata);
  837. }
  838. }
  839. }
  840. /** ----------------------------------------
  841. /** {comment_auto_path}
  842. /** ----------------------------------------*/
  843. if ($key == "comment_auto_path")
  844. {
  845. $path = ($row['comment_url'] == '') ? $row['channel_url'] : $row['comment_url'];
  846. $tagdata = $this->EE->TMPL->swap_var_single($key, $path, $tagdata);
  847. }
  848. /** ----------------------------------------
  849. /** {comment_url_title_auto_path}
  850. /** ----------------------------------------*/
  851. if ($key == "comment_url_title_auto_path")
  852. {
  853. $path = ($row['comment_url'] == '') ? $row['channel_url'] : $row['comment_url'];
  854. $tagdata = $this->EE->TMPL->swap_var_single(
  855. $key,
  856. $path.'/'.$row['url_title'],
  857. $tagdata
  858. );
  859. }
  860. /** ----------------------------------------
  861. /** {comment_entry_id_auto_path}
  862. /** ----------------------------------------*/
  863. if ($key == "comment_entry_id_auto_path")
  864. {
  865. $path = ($row['comment_url'] == '') ? $row['channel_url'] : $row['comment_url'];
  866. $tagdata = $this->EE->TMPL->swap_var_single(
  867. $key,
  868. $path.'/'.$row['entry_id'],
  869. $tagdata
  870. );
  871. }
  872. /** ----------------------------------------
  873. /** parse comment_stripped field
  874. /** ----------------------------------------*/
  875. if ($key == "comment_stripped" AND isset($row['comment']))
  876. {
  877. $tagdata = $this->EE->TMPL->swap_var_single(
  878. $key,
  879. $this->EE->functions->encode_ee_tags($row['comment'], TRUE),
  880. $tagdata
  881. );
  882. }
  883. /** ----------------------------------------
  884. /** parse comment field
  885. /** ----------------------------------------*/
  886. if ($key == 'comment' AND isset($row['comment']))
  887. {
  888. // -------------------------------------------
  889. // 'comment_entries_comment_format' hook.
  890. // - Play with the tagdata contents of the comment entries
  891. //
  892. if ($this->EE->extensions->active_hook('comment_entries_comment_format') === TRUE)
  893. {
  894. $comment = $this->EE->extensions->call('comment_entries_comment_format', $row);
  895. if ($this->EE->extensions->end_script === TRUE) return;
  896. }
  897. else
  898. {
  899. $comment = $this->EE->typography->parse_type( $row['comment'],
  900. array(
  901. 'text_format' => $row['comment_text_formatting'],
  902. 'html_format' => $row['comment_html_formatting'],
  903. 'auto_links' => $row['comment_auto_link_urls'],
  904. 'allow_img_url' => $row['comment_allow_img_urls']
  905. )
  906. );
  907. }
  908. $tagdata = $this->EE->TMPL->swap_var_single($key, $comment, $tagdata);
  909. }
  910. // {location}
  911. if ($key == 'location' AND (isset($row['location']) OR isset($row['c_location'])))
  912. {
  913. $tagdata = $this->EE->TMPL->swap_var_single($key, (empty($row['location'])) ? $row['c_location'] : $row['location'], $tagdata);
  914. }
  915. /** ----------------------------------------
  916. /** {signature}
  917. /** ----------------------------------------*/
  918. if ($key == "signature")
  919. {
  920. if ($this->EE->session->userdata('display_signatures') == 'n' OR ! isset($row['signature']) OR $row['signature'] == '' OR $this->EE->session->userdata('display_signatures') == 'n')
  921. {
  922. $tagdata = $this->EE->TMPL->swap_var_single($key, '', $tagdata);
  923. }
  924. else
  925. {
  926. $tagdata = $this->EE->TMPL->swap_var_single($key,
  927. $this->EE->typography->parse_type($row['signature'], array(
  928. 'text_format' => 'xhtml',
  929. 'html_format' => 'safe',
  930. 'auto_links' => 'y',
  931. 'allow_img_url' => $this->EE->config->item('sig_allow_img_hotlink')
  932. )
  933. ), $tagdata);
  934. }
  935. }
  936. if ($key == "signature_image_url")
  937. {
  938. if ($this->EE->session->userdata('display_signatures') == 'n' OR $row['sig_img_filename'] == '' OR $this->EE->session->userdata('display_signatures') == 'n')
  939. {
  940. $tagdata = $this->EE->TMPL->swap_var_single($key, '', $tagdata);
  941. $tagdata = $this->EE->TMPL->swap_var_single('signature_image_width', '', $tagdata);
  942. $tagdata = $this->EE->TMPL->swap_var_single('signature_image_height', '', $tagdata);
  943. }
  944. else
  945. {
  946. $tagdata = $this->EE->TMPL->swap_var_single($key, $this->EE->config->slash_item('sig_img_url').$row['sig_img_filename'], $tagdata);
  947. $tagdata = $this->EE->TMPL->swap_var_single('signature_image_width', $row['sig_img_width'], $tagdata);
  948. $tagdata = $this->EE->TMPL->swap_var_single('signature_image_height', $row['sig_img_height'], $tagdata);
  949. }
  950. }
  951. if ($key == "avatar_url")
  952. {
  953. if ( ! isset($row['avatar_filename']))
  954. $row['avatar_filename'] = '';
  955. if ($this->EE->session->userdata('display_avatars') == 'n' OR $row['avatar_filename'] == '' OR $this->EE->session->userdata('display_avatars') == 'n')
  956. {
  957. $tagdata = $this->EE->TMPL->swap_var_single($key, '', $tagdata);
  958. $tagdata = $this->EE->TMPL->swap_var_single('avatar_image_width', '', $tagdata);
  959. $tagdata = $this->EE->TMPL->swap_var_single('avatar_image_height', '', $tagdata);
  960. }
  961. else
  962. {
  963. $tagdata = $this->EE->TMPL->swap_var_single($key, $this->EE->config->slash_item('avatar_url').$row['avatar_filename'], $tagdata);
  964. $tagdata = $this->EE->TMPL->swap_var_single('avatar_image_width', $row['avatar_width'], $tagdata);
  965. $tagdata = $this->EE->TMPL->swap_var_single('avatar_image_height', $row['avatar_height'], $tagdata);
  966. }
  967. }
  968. if ($key == "photo_url")
  969. {
  970. if ( ! isset($row['photo_filename']))
  971. $row['photo_filename'] = '';
  972. if ($this->EE->session->userdata('display_photos') == 'n' OR $row['photo_filename'] == '' OR $this->EE->session->userdata('display_photos') == 'n')
  973. {
  974. $tagdata = $this->EE->TMPL->swap_var_single($key, '', $tagdata);
  975. $tagdata = $this->EE->TMPL->swap_var_single('photo_image_width', '', $tagdata);
  976. $tagdata = $this->EE->TMPL->swap_var_single('photo_image_height', '', $tagdata);
  977. }
  978. else
  979. {
  980. $tagdata = $this->EE->TMPL->swap_var_single($key, $this->EE->config->slash_item('photo_url').$row['photo_filename'], $tagdata);
  981. $tagdata = $this->EE->TMPL->swap_var_single('photo_image_width', $row['photo_width'], $tagdata);
  982. $tagdata = $this->EE->TMPL->swap_var_single('photo_image_height', $row['photo_height'], $tagdata);
  983. }
  984. }
  985. /** ----------------------------------------
  986. /** parse basic fields
  987. /** ----------------------------------------*/
  988. if (isset($row[$val]) && $val != 'member_id')
  989. {
  990. $tagdata = $this->EE->TMPL->swap_var_single($val, $row[$val], $tagdata);
  991. }
  992. /** ----------------------------------------
  993. /** parse custom member fields
  994. /** ----------------------------------------*/
  995. if ( isset($mfields[$val]))
  996. {
  997. // Since comments do not necessarily require registration, and since
  998. // you are allowed to put custom member variables in comments,
  999. // we delete them if no such row exists
  1000. $return_val = (isset($row['m_field_id_'.$mfields[$val]])) ? $row['m_field_id_'.$mfields[$val]] : '';
  1001. $tagdata = $this->EE->TMPL->swap_var_single(
  1002. $val,
  1003. $return_val,
  1004. $tagdata
  1005. );
  1006. }
  1007. /** ----------------------------------------
  1008. /** Clean up left over member variables
  1009. /** ----------------------------------------*/
  1010. if (in_array($val, $member_vars))
  1011. {
  1012. $tagdata = str_replace(LD.$val.RD, '', $tagdata);
  1013. }
  1014. }
  1015. if ($this->show_anchor == TRUE)
  1016. {
  1017. $return .= "<a name=\"".$item_count."\"></a>\n";
  1018. }
  1019. $return .= $tagdata;
  1020. $item_count++;
  1021. }
  1022. /** ----------------------------------------
  1023. /** Parse path variable
  1024. /** ----------------------------------------*/
  1025. $return = preg_replace_callback("/".LD."\s*path=(.+?)".RD."/", array(&$this->EE->functions, 'create_url'), $return);
  1026. /** ----------------------------------------
  1027. /** Add pagination to result
  1028. /** ----------------------------------------*/
  1029. if ($paginate == TRUE)
  1030. {
  1031. $paginate_data = str_replace(LD.'current_page'.RD, $t_current_page, $paginate_data);
  1032. $paginate_data = str_replace(LD.'total_pages'.RD, $total_pages, $paginate_data);
  1033. $paginate_data = str_replace(LD.'pagination_links'.RD, $pagination_links, $paginate_data);
  1034. if (preg_match("/".LD."if previous_page".RD."(.+?)".LD.'\/'."if".RD."/s", $paginate_data, $match))
  1035. {
  1036. if ($page_previous == '')
  1037. {
  1038. $paginate_data = preg_replace("/".LD."if previous_page".RD.".+?".LD.'\/'."if".RD."/s", '', $paginate_data);
  1039. }
  1040. else
  1041. {
  1042. $match['1'] = str_replace(array(LD.'path'.RD, LD.'auto_path'.RD), $page_previous, $match['1']);
  1043. $paginate_data = str_replace($match['0'], $match['1'], $paginate_data);
  1044. }
  1045. }
  1046. if (preg_match("/".LD."if next_page".RD."(.+?)".LD.'\/'."if".RD."/s", $paginate_data, $match))
  1047. {
  1048. if ($page_next == '')
  1049. {
  1050. $paginate_data = preg_replace("/".LD."if next_page".RD.".+?".LD.'\/'."if".RD."/s", '', $paginate_data);
  1051. }
  1052. else
  1053. {
  1054. $match['1'] = str_replace(array(LD.'path'.RD, LD.'auto_path'.RD), $page_next, $match['1']);
  1055. $paginate_data = str_replace($match['0'], $match['1'], $paginate_data);
  1056. }
  1057. }
  1058. $position = ( ! $this->EE->TMPL->fetch_param('paginate')) ? '' : $this->EE->TMPL->fetch_param('paginate');
  1059. switch ($position)
  1060. {
  1061. case "top" : $return = $paginate_data.$return;
  1062. break;
  1063. case "both" : $return = $paginate_data.$return.$paginate_data;
  1064. break;
  1065. default : $return .= $paginate_data;
  1066. break;
  1067. }
  1068. }
  1069. return $return;
  1070. }
  1071. // --------------------------------------------------------------------
  1072. /**
  1073. * Comment Submission Form
  1074. *
  1075. * @access public
  1076. * @return string
  1077. */
  1078. function form($return_form = FALSE, $captcha = '')
  1079. {
  1080. $qstring = $this->EE->uri->query_string;
  1081. $entry_where = array();
  1082. $halt_processing = FALSE;
  1083. /** --------------------------------------
  1084. /** Remove page number
  1085. /** --------------------------------------*/
  1086. if (preg_match("#(^|/)P(\d+)(/|$)#", $qstring, $match))
  1087. {
  1088. $qstring = trim($this->EE->functions->remove_double_slashes(str_replace($match['0'], '/', $qstring)), '/');
  1089. }
  1090. // Figure out the right entry ID
  1091. // Order of precedence: POST, entry_id=, url_title=, $qstring
  1092. if (isset($_POST['entry_id']))
  1093. {
  1094. $entry_where = array('entry_id' => $_POST['entry_id']);
  1095. }
  1096. elseif ($entry_id = $this->EE->TMPL->fetch_param('entry_id'))
  1097. {
  1098. $entry_where = array('entry_id' => $entry_id);
  1099. }
  1100. elseif ($url_title = $this->EE->TMPL->fetch_param('url_title'))
  1101. {
  1102. $entry_where = array('url_title' => $url_title);
  1103. }
  1104. else
  1105. {
  1106. // If there is a slash in the entry ID we'll kill everything after it.
  1107. $entry_id = trim($qstring);
  1108. $entry_id = preg_replace("#/.+#", "", $entry_id);
  1109. if ( ! is_numeric($entry_id))
  1110. {
  1111. $entry_where = array('url_title' => $entry_id);
  1112. }
  1113. else
  1114. {
  1115. $entry_where = array('entry_id' => $entry_id);
  1116. }
  1117. }
  1118. /** ----------------------------------------
  1119. /** Are comments allowed?
  1120. /** ----------------------------------------*/
  1121. if ($channel = $this->EE->TMPL->fetch_param('channel'))
  1122. {
  1123. $this->EE->db->select('channel_id');
  1124. $this->EE->functions->ar_andor_string($channel, 'channel_name');
  1125. $this->EE->db->where_in('site_id', $this->EE->TMPL->site_ids);
  1126. $query = $this->EE->db->get('channels');
  1127. if ($query->num_rows() == 0)
  1128. {
  1129. return FALSE;
  1130. }
  1131. elseif ($query->num_rows() == 1)
  1132. {
  1133. $this->EE->db->where('channel_titles.channel_id', $query->row('channel_id'));
  1134. }
  1135. else
  1136. {
  1137. $ids = array();
  1138. foreach ($query->result_array() as $row)
  1139. {
  1140. $ids[] = $row['channel_id'];
  1141. }
  1142. $this->EE->db->where_in('channel_titles.channel_id', $ids);
  1143. }
  1144. }
  1145. // The where clauses above will affect this query - it's below the conditional
  1146. // because AR cannot keep track of two queries at once
  1147. $this->EE->db->select('channel_titles.entry_id, channel_titles.entry_date, channel_titles.comment_expiration_date, channel_titles.allow_comments, channels.comment_system_enabled, channels.comment_use_captcha, channels.comment_expiration');
  1148. $this->EE->db->from(array('channel_titles', 'channels'));
  1149. $this->EE->db->where_in('channel_titles.site_id', $this->EE->TMPL->site_ids);
  1150. $this->EE->db->where('channel_titles.channel_id = '.$this->EE->db->dbprefix('channels').'.channel_id');
  1151. if ($e_status = $this->EE->TMPL->fetch_param('entry_status'))
  1152. {
  1153. $e_status = str_replace('Open', 'open', $e_status);
  1154. $e_status = str_replace('Closed', 'closed', $e_status);
  1155. $sql = $this->EE->functions->sql_andor_string($e_status, 'status');
  1156. if (stristr($sql, "'closed'") === FALSE)
  1157. {
  1158. $sql .= " AND status != 'closed' ";
  1159. }
  1160. // We need to drop the leading AND from the generated string
  1161. $sql = substr($sql, 4);
  1162. $this->EE->db->where($sql, NULL, FALSE);
  1163. }
  1164. else
  1165. {
  1166. $this->EE->db->where('status !=', 'closed');
  1167. }
  1168. $this->EE->db->where($entry_where);
  1169. $query = $this->EE->db->get();
  1170. if ($query->num_rows() == 0)
  1171. {
  1172. return FALSE;
  1173. }
  1174. if ($query->row('allow_comments') == 'n' OR $query->row('comment_system_enabled') == 'n')
  1175. {
  1176. $halt_processing = 'disabled';
  1177. }
  1178. /** ----------------------------------------
  1179. /** Smart Notifications? Mark comments as read.
  1180. /** ----------------------------------------*/
  1181. if ($this->EE->session->userdata('smart_notifications') == 'y')
  1182. {
  1183. $this->EE->load->library('subscription');
  1184. $this->EE->subscription->init('comment', array('entry_id' => $query->row('entry_id')), TRUE);
  1185. $this->EE->subscription->mark_as_read();
  1186. }
  1187. /** ----------------------------------------
  1188. /** Return the "no cache" version of the form
  1189. /** ----------------------------------------*/
  1190. if ($return_form == FALSE)
  1191. {
  1192. if ($query->row('comment_use_captcha') == 'n')
  1193. {
  1194. $this->EE->TMPL->tagdata = str_replace(LD.'captcha'.RD, '', $this->EE->TMPL->tagdata);
  1195. }
  1196. $nc = '';
  1197. if (is_array($this->EE->TMPL->tagparams) AND count($this->EE->TMPL->tagparams) > 0)
  1198. {
  1199. foreach ($this->EE->TMPL->tagparams as $key => $val)
  1200. {
  1201. switch ($key)
  1202. {
  1203. case 'form_class':
  1204. $nc .= 'class="'.$val.'" ';
  1205. break;
  1206. case 'form_id':
  1207. $nc .= 'id="'.$val.'" ';
  1208. break;
  1209. default:
  1210. $nc .= ' '.$key.'="'.$val.'" ';
  1211. }
  1212. }
  1213. }
  1214. return '{NOCACHE_COMMENT_FORM="'.$nc.'"}'.$this->EE->TMPL->tagdata.'{/NOCACHE_FORM}';
  1215. }
  1216. /** ----------------------------------------
  1217. /** Has commenting expired?
  1218. /** ----------------------------------------*/
  1219. $mode = ( ! isset($this->comment_expiration_mode)) ? 0 : $this->comment_expiration_mode;
  1220. // First check whether expiration is overriden
  1221. if ($this->EE->config->item('comment_moderation_override') !== 'y')
  1222. {
  1223. if ($mode == 0)
  1224. {
  1225. if ($query->row('comment_expiration_date') > 0)
  1226. {
  1227. if ($this->EE->localize->now > $query->row('comment_expiration_date') )
  1228. {
  1229. $halt_processing = 'expired';
  1230. }
  1231. }
  1232. }
  1233. else
  1234. {
  1235. if ($query->row('comment_expiration') > 0)
  1236. {
  1237. $days = $query->row('entry_date') + ($query->row('comment_expiration') * 86400);
  1238. if ($this->EE->localize->now > $days)
  1239. {
  1240. $halt_processing = 'expired';
  1241. }
  1242. }
  1243. }
  1244. }
  1245. $tagdata = $this->EE->TMPL->tagdata;
  1246. if ($halt_processing != FALSE)
  1247. {
  1248. foreach ($this->EE->TMPL->var_cond as $key => $val)
  1249. {
  1250. if ($halt_processing == 'expired')
  1251. {
  1252. if (isset($val['3']) && $val['3'] == 'comments_expired')
  1253. {
  1254. return $val['2'];
  1255. }
  1256. }
  1257. elseif ($halt_processing == 'disabled')
  1258. {
  1259. if (isset($val['3']) && $val['3'] == 'comments_disabled')
  1260. {
  1261. return $val['2'];
  1262. }
  1263. }
  1264. }
  1265. // If there is no conditional- just return the message
  1266. $this->EE->lang->loadfile('comment');
  1267. return $this->EE->lang->line('cmt_commenting_has_expired');
  1268. }
  1269. // -------------------------------------------
  1270. // 'comment_form_tagdata' hook.
  1271. // - Modify, add, etc. something to the comment form
  1272. //
  1273. if ($this->EE->extensions->active_hook('comment_form_tagdata') === TRUE)
  1274. {
  1275. $tagdata = $this->EE->extensions->call('comment_form_tagdata', $tagdata);
  1276. if ($this->EE->extensions->end_script === TRUE) return;
  1277. }
  1278. //
  1279. // -------------------------------------------
  1280. /** ----------------------------------------
  1281. /** Conditionals
  1282. /** ----------------------------------------*/
  1283. $cond = array();
  1284. $cond['logged_in'] = ($this->EE->session->userdata('member_id') == 0) ? 'FALSE' : 'TRUE';
  1285. $cond['logged_out'] = ($this->EE->session->userdata('member_id') != 0) ? 'FALSE' : 'TRUE';
  1286. if ($query->row('comment_use_captcha') == 'n')
  1287. {
  1288. $cond['captcha'] = 'FALSE';
  1289. }
  1290. elseif ($query->row('comment_use_captcha') == 'y')
  1291. {
  1292. $cond['captcha'] = ($this->EE->config->item('captcha_require_members') == 'y' OR
  1293. ($this->EE->config->item('captcha_require_members') == 'n' AND $this->EE->session->userdata('member_id') == 0)) ? 'TRUE' : 'FALSE';
  1294. }
  1295. $tagdata = $this->EE->functions->prep_conditionals($tagdata, $cond);
  1296. /** ----------------------------------------
  1297. /** Single Variables
  1298. /** ----------------------------------------*/
  1299. // Load the form helper
  1300. $this->EE->load->helper('form');
  1301. foreach ($this->EE->TMPL->var_single as $key => $val)
  1302. {
  1303. /** ----------------------------------------
  1304. /** parse {name}
  1305. /** ----------------------------------------*/
  1306. if ($key == 'name')
  1307. {
  1308. $name = ($this->EE->session->userdata['screen_name'] != '') ? $this->EE->session->userdata['screen_name'] : $this->EE->session->userdata['username'];
  1309. $name = ( ! isset($_POST['name'])) ? $name : $_POST['name'];
  1310. $tagdata = $this->EE->TMPL->swap_var_single($key, form_prep($name), $tagdata);
  1311. }
  1312. /** ----------------------------------------
  1313. /** parse {email}
  1314. /** ----------------------------------------*/
  1315. if ($key == 'email')
  1316. {
  1317. $email = ( ! isset($_POST['email'])) ? $this->EE->session->userdata['email'] : $_POST['email'];
  1318. $tagdata = $this->EE->TMPL->swap_var_single($key, form_prep($email), $tagdata);
  1319. }
  1320. /** ----------------------------------------
  1321. /** parse {url}
  1322. /** ----------------------------------------*/
  1323. if ($key == 'url')
  1324. {
  1325. $url = ( ! isset($_POST['url'])) ? $this->EE->session->userdata['url'] : $_POST['url'];
  1326. if ($url == '')
  1327. $url = 'http://';
  1328. $tagdata = $this->EE->TMPL->swap_var_single($key, form_prep($url), $tagdata);
  1329. }
  1330. /** ----------------------------------------
  1331. /** parse {location}
  1332. /** ----------------------------------------*/
  1333. if ($key == 'location')
  1334. {
  1335. $location = ( ! isset($_POST['location'])) ? $this->EE->session->userdata['location'] : $_POST['location'];
  1336. $tagdata = $this->EE->TMPL->swap_var_single($key, form_prep($location), $tagdata);
  1337. }
  1338. /** ----------------------------------------
  1339. /** parse {comment}
  1340. /** ----------------------------------------*/
  1341. if ($key == 'comment')
  1342. {
  1343. $comment = ( ! isset($_POST['comment'])) ? '' : $_POST['comment'];
  1344. $tagdata = $this->EE->TMPL->swap_var_single($key, $comment, $tagdata);
  1345. }
  1346. /** ----------------------------------------
  1347. /** parse {captcha_word}
  1348. /** ----------------------------------------*/
  1349. if ($key == 'captcha_word')
  1350. {
  1351. $tagdata = $this->EE->TMPL->swap_var_single($key, '', $tagdata);
  1352. }
  1353. /** ----------------------------------------
  1354. /** parse {save_info}
  1355. /** ----------------------------------------*/
  1356. if ($key == 'save_info')
  1357. {
  1358. $save_info = ( ! isset($_POST['save_info'])) ? '' : $_POST['save_info'];
  1359. $notify = ( ! isset($this->EE->session->userdata['notify_by_default'])) ? $this->EE->input->cookie('save_info') : $this->EE->session->userdata['notify_by_default'];
  1360. $checked = ( ! isset($_POST['PRV'])) ? $notify : $save_info;
  1361. $tagdata = $this->EE->TMPL->swap_var_single($key, ($checked == 'yes') ? "checked=\"checked\"" : '', $tagdata);
  1362. }
  1363. /** ----------------------------------------
  1364. /** parse {notify_me}
  1365. /** ----------------------------------------*/
  1366. if ($key == 'notify_me')
  1367. {
  1368. $checked = '';
  1369. if ( ! isset($_POST['PRV']))
  1370. {
  1371. if ($this->EE->input->cookie('notify_me'))
  1372. {
  1373. $checked = $this->EE->input->cookie('notify_me');
  1374. }
  1375. if (isset($this->EE->session->userdata['notify_by_default']))
  1376. {
  1377. $checked = ($this->EE->session->userdata['notify_by_default'] == 'y') ? 'yes' : '';
  1378. }
  1379. }
  1380. if (isset($_POST['notify_me']))
  1381. {
  1382. $checked = $_POST['notify_me'];
  1383. }
  1384. $tagdata = $this->EE->TMPL->swap_var_single($key, ($checked == 'yes') ? "checked=\"checked\"" : '', $tagdata);
  1385. }
  1386. }
  1387. /** ----------------------------------------
  1388. /** Create form
  1389. /** ----------------------------------------*/
  1390. $RET = $this->EE->functions->fetch_current_uri();
  1391. if (isset($_POST['RET']))
  1392. {
  1393. $RET = $_POST['RET'];
  1394. }
  1395. elseif ($this->EE->TMPL->fetch_param('return') && $this->EE->TMPL->fetch_param('return') != "")
  1396. {
  1397. $RET = $this->EE->TMPL->fetch_param('return');
  1398. }
  1399. $PRV = (isset($_POST['PRV'])) ? $_POST['PRV'] : $this->EE->TMPL->fetch_param('preview');
  1400. $XID = (isset($_POST['XID'])) ? $_POST['XID'] : '';
  1401. $hidden_fields = array(
  1402. 'ACT' => $this->EE->functions->fetch_action_id('Comment', 'insert_new_comment'),
  1403. 'RET' => $RET,
  1404. 'URI' => ($this->EE->uri->uri_string == '') ? 'index' : $this->EE->uri->uri_string,
  1405. 'PRV' => $PRV,
  1406. 'XID' => $XID,
  1407. 'entry_id' => $query->row('entry_id')
  1408. );
  1409. if ($query->row('comment_use_captcha') == 'y')
  1410. {
  1411. if (preg_match("/({captcha})/", $tagdata))
  1412. {
  1413. $tagdata = preg_replace("/{captcha}/", $this->EE->functions->create_captcha(), $tagdata);
  1414. }
  1415. }
  1416. // -------------------------------------------
  1417. // 'comment_form_hidden_fields' hook.
  1418. // - Add/Remove Hidden Fields for Comment Form
  1419. //
  1420. if ($this->EE->extensions->active_hook('comment_form_hidden_fields') === TRUE)
  1421. {
  1422. $hidden_fields = $this->EE->extensions->call('comment_form_hidden_fields', $hidden_fields);
  1423. if ($this->EE->extensions->end_script === TRUE) return;
  1424. }
  1425. //
  1426. // -------------------------------------------
  1427. $data = array(
  1428. 'hidden_fields' => $hidden_fields,
  1429. 'id' => ( ! isset($this->EE->TMPL->tagparams['id'])) ? 'comment_form' : $this->EE->TMPL->tagparams['id'],
  1430. 'class' => ( ! isset($this->EE->TMPL->tagparams['class'])) ? NULL : $this->EE->TMPL->tagparams['class']
  1431. );
  1432. if ($this->EE->TMPL->fetch_param('name') !== FALSE &&
  1433. preg_match("#^[a-zA-Z0-9_\-]+$#i", $this->EE->TMPL->fetch_param('name'), $match))
  1434. {
  1435. $data['name'] = $this->EE->TMPL->fetch_param('name');
  1436. }
  1437. $res = $this->EE->functions->form_declaration($data);
  1438. $res .= stripslashes($tagdata);
  1439. $res .= "</form>";
  1440. // -------------------------------------------
  1441. // 'comment_form_end' hook.
  1442. // - Modify, add, etc. something to the comment form at end of processing
  1443. //
  1444. if ($this->EE->extensions->active_hook('comment_form_end') === TRUE)
  1445. {
  1446. $res = $this->EE->extensions->call('comment_form_end', $res);
  1447. if ($this->EE->extensions->end_script === TRUE) return $res;
  1448. }
  1449. //
  1450. // -------------------------------------------
  1451. return $res;
  1452. }
  1453. // --------------------------------------------------------------------
  1454. /**
  1455. * Preview
  1456. *
  1457. * @access public
  1458. * @return void
  1459. */
  1460. function preview()
  1461. {
  1462. $entry_id = (isset($_POST['entry_id'])) ? $_POST['entry_id'] : $this->EE->uri->query_string;
  1463. if ( ! is_numeric($entry_id) OR empty($_POST['comment']))
  1464. {
  1465. return FALSE;
  1466. }
  1467. /** ----------------------------------------
  1468. /** Instantiate Typography class
  1469. /** ----------------------------------------*/
  1470. $config = ($this->EE->config->item('comment_word_censoring') == 'y') ? array('word_censor' => TRUE) : array();
  1471. $this->EE->load->library('typography');
  1472. $this->EE->typography->initialize($config);
  1473. $this->EE->typography->parse_images = FALSE;
  1474. $this->EE->typography->allow_headings = FALSE;
  1475. $this->EE->typography->encode_email = FALSE;
  1476. $this->EE->db->select('channels.comment_text_formatting, channels.comment_html_formatting, channels.comment_allow_img_urls, channels.comment_auto_link_urls, channels.comment_max_chars');
  1477. $this->EE->db->where('channel_titles.channel_id = '.$this->EE->db->dbprefix('channels').'.channel_id');
  1478. $this->EE->db->where('channel_titles.entry_id', $entry_id);
  1479. $this->EE->db->from(array('channels', 'channel_titles'));
  1480. $query = $this->EE->db->get();
  1481. if ($query->num_rows() == 0)
  1482. {
  1483. return '';
  1484. }
  1485. /** -------------------------------------
  1486. /** Check size of comment
  1487. /** -------------------------------------*/
  1488. if ($query->row('comment_max_chars') != '' AND $query->row('comment_max_chars') != 0)
  1489. {
  1490. if (strlen($_POST['comment']) > $query->row('comment_max_chars') )
  1491. {
  1492. $str = str_replace("%n", strlen($_POST['comment']), $this->EE->lang->line('cmt_too_large'));
  1493. $str = str_replace("%x", $query->row('comment_max_chars') , $str);
  1494. return $this->EE->output->show_user_error('submission', $str);
  1495. }
  1496. }
  1497. if ($query->num_rows() == '')
  1498. {
  1499. $formatting = 'none';
  1500. }
  1501. else
  1502. {
  1503. $formatting = $query->row('comment_text_formatting') ;
  1504. }
  1505. $tagdata = $this->EE->TMPL->tagdata;
  1506. // -------------------------------------------
  1507. // 'comment_preview_tagdata' hook.
  1508. // - Play with the tagdata contents of the comment preview
  1509. //
  1510. if ($this->EE->extensions->active_hook('comment_preview_tagdata') === TRUE)
  1511. {
  1512. $tagdata = $this->EE->extensions->call('comment_preview_tagdata', $tagdata);
  1513. if ($this->EE->extensions->end_script === TRUE) return;
  1514. }
  1515. //
  1516. // -------------------------------------------
  1517. /** ----------------------------------------
  1518. /** Fetch all the date-related variables
  1519. /** ----------------------------------------*/
  1520. $comment_date = array();
  1521. if (preg_match_all("/".LD."comment_date\s+format=[\"'](.*?)[\"']".RD."/s", $tagdata, $matches))
  1522. {
  1523. for ($j = 0; $j < count($matches['0']); $j++)
  1524. {
  1525. $matches['0'][$j] = str_replace(LD, '', $matches['0'][$j]);
  1526. $matches['0'][$j] = str_replace(RD, '', $matches['0'][$j]);
  1527. $comment_date[$matches['0'][$j]] = $this->EE->localize->fetch_date_params($matches['1'][$j]);
  1528. }
  1529. }
  1530. /** ----------------------------------------
  1531. /** Set defaults based on member data as needed
  1532. /** ----------------------------------------*/
  1533. if (isset($_POST['name']) AND $_POST['name'] != '')
  1534. {
  1535. $name = stripslashes($this->EE->input->post('name'));
  1536. }
  1537. elseif ($this->EE->session->userdata['screen_name'] != '')
  1538. {
  1539. $name = $this->EE->session->userdata['screen_name'];
  1540. }
  1541. else
  1542. {
  1543. $name = '';
  1544. }
  1545. foreach (array('email', 'url', 'location') as $v)
  1546. {
  1547. if (isset($_POST[$v]) AND $_POST[$v] != '')
  1548. {
  1549. ${$v} = stripslashes($this->EE->input->post($v));
  1550. }
  1551. elseif ($this->EE->session->userdata[$v] != '')
  1552. {
  1553. ${$v} = $this->EE->session->userdata[$v];
  1554. }
  1555. else
  1556. {
  1557. ${$v} = '';
  1558. }
  1559. }
  1560. /** ----------------------------------------
  1561. /** Conditionals
  1562. /** ----------------------------------------*/
  1563. $cond = $_POST; // Sanitized on input and also in prep_conditionals, so no real worries here
  1564. $cond['logged_in'] = ($this->EE->session->userdata('member_id') == 0) ? 'FALSE' : 'TRUE';
  1565. $cond['logged_out'] = ($this->EE->session->userdata('member_id') != 0) ? 'FALSE' : 'TRUE';
  1566. $cond['name'] = $name;
  1567. $cond['email'] = $email;
  1568. $cond['url'] = ($url == 'http://') ? '' : $url;
  1569. $cond['location'] = $location;
  1570. $tagdata = $this->EE->functions->prep_conditionals($tagdata, $cond);
  1571. /** ----------------------------------------
  1572. /** Single Variables
  1573. /** ----------------------------------------*/
  1574. foreach ($this->EE->TMPL->var_single as $key => $val)
  1575. {
  1576. // {name}
  1577. if ($key == 'name')
  1578. {
  1579. $tagdata = $this->EE->TMPL->swap_var_single($key, $name, $tagdata);
  1580. }
  1581. // {email}
  1582. if ($key == 'email')
  1583. {
  1584. $tagdata = $this->EE->TMPL->swap_var_single($key, $email, $tagdata);
  1585. }
  1586. // {url}
  1587. if ($key == 'url')
  1588. {
  1589. $tagdata = $this->EE->TMPL->swap_var_single($key, $url, $tagdata);
  1590. }
  1591. // {location}
  1592. if ($key == 'location')
  1593. {
  1594. $tagdata = $this->EE->TMPL->swap_var_single($key, $location, $tagdata);
  1595. }
  1596. // Prep the URL
  1597. if ($url != '')
  1598. {
  1599. $this->EE->load->helper('url');
  1600. $url = prep_url($url);
  1601. }
  1602. // {url_or_email}
  1603. if ($key == "url_or_email")
  1604. {
  1605. $temp = $url;
  1606. if ($temp == '' AND $email != '')
  1607. {
  1608. $temp = $this->EE->typography->encode_email($email, '', 0);
  1609. }
  1610. $tagdata = $this->EE->TMPL->swap_var_single($val, $temp, $tagdata);
  1611. }
  1612. // {url_or_email_as_author}
  1613. if ($key == "url_or_email_as_author")
  1614. {
  1615. if ($url != '')
  1616. {
  1617. $tagdata = $this->EE->TMPL->swap_var_single($val, "<a href=\"".$url."\">".$name."</a>", $tagdata);
  1618. }
  1619. else
  1620. {
  1621. if ($email != '')
  1622. {
  1623. $tagdata = $this->EE->TMPL->swap_var_single($val, $this->EE->typography->encode_email($email, $name), $tagdata);
  1624. }
  1625. else
  1626. {
  1627. $tagdata = $this->EE->TMPL->swap_var_single($val, $name, $tagdata);
  1628. }
  1629. }
  1630. }
  1631. // {url_or_email_as_link}
  1632. if ($key == "url_or_email_as_link")
  1633. {
  1634. if ($url != '')
  1635. {
  1636. $tagdata = $this->EE->TMPL->swap_var_single($val, "<a href=\"".$url."\">".$url."</a>", $tagdata);
  1637. }
  1638. else
  1639. {
  1640. if ($email != '')
  1641. {
  1642. $tagdata = $this->EE->TMPL->swap_var_single($val, $this->EE->typography->encode_email($email), $tagdata);
  1643. }
  1644. else
  1645. {
  1646. $tagdata = $this->EE->TMPL->swap_var_single($val, $name, $tagdata);
  1647. }
  1648. }
  1649. }
  1650. // {url_as_author}
  1651. if ($key == 'url_as_author')
  1652. {
  1653. if ($url != '')
  1654. {
  1655. $tagdata = $this->EE->TMPL->swap_var_single($val, '<a href="'.$url.'">'.$name.'</a>', $tagdata);
  1656. }
  1657. else
  1658. {
  1659. $tagdata = $this->EE->TMPL->swap_var_single($val, $name, $tagdata);
  1660. }
  1661. }
  1662. /** ----------------------------------------
  1663. /** parse comment field
  1664. /** ----------------------------------------*/
  1665. if ($key == 'comment')
  1666. {
  1667. // -------------------------------------------
  1668. // 'comment_preview_comment_format' hook.
  1669. // - Play with the tagdata contents of the comment preview
  1670. //
  1671. if ($this->EE->extensions->active_hook('comment_preview_comment_format') === TRUE)
  1672. {
  1673. $data = $this->EE->extensions->call('comment_preview_comment_format', $query->row());
  1674. if ($this->EE->extensions->end_script === TRUE) return;
  1675. }
  1676. else
  1677. {
  1678. $data = $this->EE->typography->parse_type( stripslashes($this->EE->input->post('comment')),
  1679. array(
  1680. 'text_format' => $query->row('comment_text_formatting') ,
  1681. 'html_format' => $query->row('comment_html_formatting') ,
  1682. 'auto_links' => $query->row('comment_auto_link_urls') ,
  1683. 'allow_img_url' => $query->row('comment_allow_img_urls')
  1684. )
  1685. );
  1686. }
  1687. // -------------------------------------------
  1688. $tagdata = $this->EE->TMPL->swap_var_single($key, $data, $tagdata);
  1689. }
  1690. /** ----------------------------------------
  1691. /** parse comment date
  1692. /** ----------------------------------------*/
  1693. if (isset($comment_date[$key]))
  1694. {
  1695. foreach ($comment_date[$key] as $dvar)
  1696. {
  1697. $val = str_replace($dvar, $this->EE->localize->convert_timestamp($dvar, $this->EE->localize->now, TRUE), $val);
  1698. }
  1699. $tagdata = $this->EE->TMPL->swap_var_single($key, $val, $tagdata);
  1700. }
  1701. }
  1702. return $tagdata;
  1703. }
  1704. // --------------------------------------------------------------------
  1705. /**
  1706. * Preview Handler
  1707. *
  1708. * @access public
  1709. * @return void
  1710. */
  1711. function preview_handler()
  1712. {
  1713. if ($this->EE->input->post('PRV') == '')
  1714. {
  1715. $error[] = $this->EE->lang->line('cmt_no_preview_template_specified');
  1716. return $this->EE->output->show_user_error('general', $error);
  1717. }
  1718. if ( ! isset($_POST['PRV']) or $_POST['PRV'] == '')
  1719. {
  1720. exit('Preview template not specified in your comment form tag');
  1721. }
  1722. // Clean return value- segments only
  1723. $clean_return = str_replace($this->EE->functions->fetch_site_index(), '', $_POST['RET']);
  1724. // Load the string helper
  1725. $this->EE->load->helper('string');
  1726. $_POST['PRV'] = trim_slashes($this->EE->security->xss_clean($_POST['PRV']));
  1727. $this->EE->functions->clear_caching('all', $_POST['PRV']);
  1728. $this->EE->functions->clear_caching('all', $clean_return);
  1729. require APPPATH.'libraries/Template'.EXT;
  1730. $this->EE->TMPL = new EE_Template();
  1731. $preview = ( ! $this->EE->input->post('PRV')) ? '' : $this->EE->input->get_post('PRV');
  1732. if (strpos($preview, '/') === FALSE)
  1733. {
  1734. $preview = '';
  1735. }
  1736. else
  1737. {
  1738. $ex = explode("/", $preview);
  1739. if (count($ex) != 2)
  1740. {
  1741. $preview = '';
  1742. }
  1743. }
  1744. if ($preview == '')
  1745. {
  1746. $group = 'channel';
  1747. $templ = 'preview';
  1748. }
  1749. else
  1750. {
  1751. $group = $ex['0'];
  1752. $templ = $ex['1'];
  1753. }
  1754. // this makes sure the query string is seen correctly by tags on the template
  1755. $this->EE->TMPL->parse_template_uri();
  1756. $this->EE->TMPL->run_template_engine($group, $templ);
  1757. }
  1758. // --------------------------------------------------------------------
  1759. /**
  1760. * Insert New Comment
  1761. *
  1762. * @access public
  1763. * @return string
  1764. */
  1765. function insert_new_comment()
  1766. {
  1767. $default = array('name', 'email', 'url', 'comment', 'location', 'entry_id');
  1768. foreach ($default as $val)
  1769. {
  1770. if ( ! isset($_POST[$val]))
  1771. {
  1772. $_POST[$val] = '';
  1773. }
  1774. }
  1775. // No entry ID? What the heck are they doing?
  1776. if ( ! is_numeric($_POST['entry_id']))
  1777. {
  1778. return FALSE;
  1779. }
  1780. /** ----------------------------------------
  1781. /** Fetch the comment language pack
  1782. /** ----------------------------------------*/
  1783. $this->EE->lang->loadfile('comment');
  1784. // No comment- let's end it here
  1785. if ($_POST['comment'] == '')
  1786. {
  1787. $error = $this->EE->lang->line('cmt_missing_comment');
  1788. return $this->EE->output->show_user_error('submission', $error);
  1789. }
  1790. /** ----------------------------------------
  1791. /** Is the user banned?
  1792. /** ----------------------------------------*/
  1793. if ($this->EE->session->userdata['is_banned'] == TRUE)
  1794. {
  1795. return $this->EE->output->show_user_error('general', array($this->EE->lang->line('not_authorized')));
  1796. }
  1797. /** ----------------------------------------
  1798. /** Is the IP address and User Agent required?
  1799. /** ----------------------------------------*/
  1800. if ($this->EE->config->item('require_ip_for_posting') == 'y')
  1801. {
  1802. if ($this->EE->input->ip_address() == '0.0.0.0' OR $this->EE->session->userdata['user_agent'] == "")
  1803. {
  1804. return $this->EE->output->show_user_error('general', array($this->EE->lang->line('not_authorized')));
  1805. }
  1806. }
  1807. /** ----------------------------------------
  1808. /** Is the nation of the user banend?
  1809. /** ----------------------------------------*/
  1810. $this->EE->session->nation_ban_check();
  1811. /** ----------------------------------------
  1812. /** Can the user post comments?
  1813. /** ----------------------------------------*/
  1814. if ($this->EE->session->userdata['can_post_comments'] == 'n')
  1815. {
  1816. $error[] = $this->EE->lang->line('cmt_no_authorized_for_comments');
  1817. return $this->EE->output->show_user_error('general', $error);
  1818. }
  1819. /** ----------------------------------------
  1820. /** Blacklist/Whitelist Check
  1821. /** ----------------------------------------*/
  1822. if ($this->EE->blacklist->blacklisted == 'y' && $this->EE->blacklist->whitelisted == 'n')
  1823. {
  1824. return $this->EE->output->show_user_error('general', array($this->EE->lang->line('not_authorized')));
  1825. }
  1826. /** ----------------------------------------
  1827. /** Is this a preview request?
  1828. /** ----------------------------------------*/
  1829. if (isset($_POST['preview']))
  1830. {
  1831. return $this->preview_handler();
  1832. }
  1833. // -------------------------------------------
  1834. // 'insert_comment_start' hook.
  1835. // - Allows complete rewrite of comment submission routine.
  1836. // - Or could be used to modify the POST data before processing
  1837. //
  1838. $edata = $this->EE->extensions->call('insert_comment_start');
  1839. if ($this->EE->extensions->end_script === TRUE) return;
  1840. //
  1841. // -------------------------------------------
  1842. /** ----------------------------------------
  1843. /** Fetch channel preferences
  1844. /** ----------------------------------------*/
  1845. // Bummer, saw the hook after converting the query
  1846. /*
  1847. $this->EE->db->select('channel_titles.title, channel_titles.url_title, channel_titles.channel_id, channel_titles.author_id,
  1848. channel_titles.comment_total, channel_titles.allow_comments, channel_titles.entry_date, channel_titles.comment_expiration_date,
  1849. channels.channel_title, channels.comment_system_enabled, channels.comment_max_chars, channels.comment_use_captcha,
  1850. channels.comment_timelock, channels.comment_require_membership, channels.comment_moderate, channels.comment_require_email,
  1851. channels.comment_notify, channels.comment_notify_authors, channels.comment_notify_emails, channels.comment_expiration'
  1852. );
  1853. $this->EE->db->from(array('channel_titles', 'channels'));
  1854. $this->EE->db->where('channel_titles.channel_id = channels.channel_id');
  1855. $this->EE->db->where('channel_titles.entry_id', $_POST['entry_id']);
  1856. $this->EE->db->where('channel_titles.status', 'closed');
  1857. */
  1858. $sql = "SELECT exp_channel_titles.title,
  1859. exp_channel_titles.url_title,
  1860. exp_channel_titles.entry_id,
  1861. exp_channel_titles.channel_id,
  1862. exp_channel_titles.author_id,
  1863. exp_channel_titles.comment_total,
  1864. exp_channel_titles.allow_comments,
  1865. exp_channel_titles.entry_date,
  1866. exp_channel_titles.comment_expiration_date,
  1867. exp_channels.channel_title,
  1868. exp_channels.comment_system_enabled,
  1869. exp_channels.comment_max_chars,
  1870. exp_channels.comment_use_captcha,
  1871. exp_channels.comment_timelock,
  1872. exp_channels.comment_require_membership,
  1873. exp_channels.comment_moderate,
  1874. exp_channels.comment_require_email,
  1875. exp_channels.comment_notify,
  1876. exp_channels.comment_notify_authors,
  1877. exp_channels.comment_notify_emails,
  1878. exp_channels.comment_expiration,
  1879. exp_channels.channel_url,
  1880. exp_channels.comment_url
  1881. FROM exp_channel_titles, exp_channels
  1882. WHERE exp_channel_titles.channel_id = exp_channels.channel_id
  1883. AND exp_channel_titles.entry_id = '".$this->EE->db->escape_str($_POST['entry_id'])."'";
  1884. // Added entry_status param, so it is possible to post to closed title
  1885. //AND exp_channel_titles.status != 'closed' ";
  1886. // -------------------------------------------
  1887. // 'insert_comment_preferences_sql' hook.
  1888. // - Rewrite or add to the comment preference sql query
  1889. // - Could be handy for comment/channel restrictions
  1890. //
  1891. if ($this->EE->extensions->active_hook('insert_comment_preferences_sql') === TRUE)
  1892. {
  1893. $sql = $this->EE->extensions->call('insert_comment_preferences_sql', $sql);
  1894. if ($this->EE->extensions->end_script === TRUE) return;
  1895. }
  1896. //
  1897. // -------------------------------------------
  1898. $query = $this->EE->db->query($sql);
  1899. unset($sql);
  1900. if ($query->num_rows() == 0)
  1901. {
  1902. return FALSE;
  1903. }
  1904. /** ----------------------------------------
  1905. /** Are comments allowed?
  1906. /** ----------------------------------------*/
  1907. if ($query->row('allow_comments') == 'n' OR $query->row('comment_system_enabled') == 'n')
  1908. {
  1909. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('cmt_comments_not_allowed'));
  1910. }
  1911. /** ----------------------------------------
  1912. /** Has commenting expired?
  1913. /** ----------------------------------------*/
  1914. $force_moderation = $query->row('comment_moderate');
  1915. if ($this->comment_expiration_mode == 0)
  1916. {
  1917. if ($query->row('comment_expiration_date') > 0)
  1918. {
  1919. if ($this->EE->localize->now > $query->row('comment_expiration_date') )
  1920. {
  1921. if ($this->EE->config->item('comment_moderation_override') == 'y')
  1922. {
  1923. $force_moderation = 'y';
  1924. }
  1925. else
  1926. {
  1927. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('cmt_commenting_has_expired'));
  1928. }
  1929. }
  1930. }
  1931. }
  1932. else
  1933. {
  1934. if ($query->row('comment_expiration') > 0)
  1935. {
  1936. $days = $query->row('entry_date') + ($query->row('comment_expiration') * 86400);
  1937. if ($this->EE->localize->now > $days)
  1938. {
  1939. if ($this->EE->config->item('comment_moderation_override') == 'y')
  1940. {
  1941. $force_moderation = 'y';
  1942. }
  1943. else
  1944. {
  1945. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('cmt_commenting_has_expired'));
  1946. }
  1947. }
  1948. }
  1949. }
  1950. /** ----------------------------------------
  1951. /** Is there a comment timelock?
  1952. /** ----------------------------------------*/
  1953. if ($query->row('comment_timelock') != '' AND $query->row('comment_timelock') > 0)
  1954. {
  1955. if ($this->EE->session->userdata['group_id'] != 1)
  1956. {
  1957. $time = $this->EE->localize->now - $query->row('comment_timelock') ;
  1958. $this->EE->db->where('comment_date >', $time);
  1959. $this->EE->db->where('ip_address', $this->EE->input->ip_address());
  1960. $result = $this->EE->db->count_all_results('comments');
  1961. if ($result > 0)
  1962. {
  1963. return $this->EE->output->show_user_error('submission', str_replace("%s", $query->row('comment_timelock') , $this->EE->lang->line('cmt_comments_timelock')));
  1964. }
  1965. }
  1966. }
  1967. /** ----------------------------------------
  1968. /** Do we allow duplicate data?
  1969. /** ----------------------------------------*/
  1970. if ($this->EE->config->item('deny_duplicate_data') == 'y')
  1971. {
  1972. if ($this->EE->session->userdata['group_id'] != 1)
  1973. {
  1974. $this->EE->db->where('comment', $_POST['comment']);
  1975. $result = $this->EE->db->count_all_results('comments');
  1976. if ($result > 0)
  1977. {
  1978. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('cmt_duplicate_comment_warning'));
  1979. }
  1980. }
  1981. }
  1982. /** ----------------------------------------
  1983. /** Assign data
  1984. /** ----------------------------------------*/
  1985. $author_id = $query->row('author_id') ;
  1986. $entry_title = $query->row('title') ;
  1987. $url_title = $query->row('url_title') ;
  1988. $channel_title = $query->row('channel_title') ;
  1989. $channel_id = $query->row('channel_id') ;
  1990. $comment_total = $query->row('comment_total') + 1;
  1991. $require_membership = $query->row('comment_require_membership') ;
  1992. $comment_moderate = ($this->EE->session->userdata['group_id'] == 1 OR $this->EE->session->userdata['exclude_from_moderation'] == 'y') ? 'n' : $force_moderation;
  1993. $author_notify = $query->row('comment_notify_authors') ;
  1994. $comment_url = $query->row('comment_url');
  1995. $channel_url = $query->row('channel_url');
  1996. $entry_id = $query->row('entry_id');
  1997. $notify_address = ($query->row('comment_notify') == 'y' AND $query->row('comment_notify_emails') != '') ? $query->row('comment_notify_emails') : '';
  1998. /** ----------------------------------------
  1999. /** Start error trapping
  2000. /** ----------------------------------------*/
  2001. $error = array();
  2002. if ($this->EE->session->userdata('member_id') != 0)
  2003. {
  2004. // If the user is logged in we'll reassign the POST variables with the user data
  2005. $_POST['name'] = ($this->EE->session->userdata['screen_name'] != '') ? $this->EE->session->userdata['screen_name'] : $this->EE->session->userdata['username'];
  2006. $_POST['email'] = $this->EE->session->userdata['email'];
  2007. $_POST['url'] = $this->EE->session->userdata['url'];
  2008. $_POST['location'] = $this->EE->session->userdata['location'];
  2009. }
  2010. /** ----------------------------------------
  2011. /** Is membership is required to post...
  2012. /** ----------------------------------------*/
  2013. if ($require_membership == 'y')
  2014. {
  2015. // Not logged in
  2016. if ($this->EE->session->userdata('member_id') == 0)
  2017. {
  2018. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('cmt_must_be_member'));
  2019. }
  2020. // Membership is pending
  2021. if ($this->EE->session->userdata['group_id'] == 4)
  2022. {
  2023. return $this->EE->output->show_user_error('general', $this->EE->lang->line('cmt_account_not_active'));
  2024. }
  2025. }
  2026. else
  2027. {
  2028. /** ----------------------------------------
  2029. /** Missing name?
  2030. /** ----------------------------------------*/
  2031. if ($_POST['name'] == '')
  2032. {
  2033. $error[] = $this->EE->lang->line('cmt_missing_name');
  2034. }
  2035. /** -------------------------------------
  2036. /** Is name banned?
  2037. /** -------------------------------------*/
  2038. if ($this->EE->session->ban_check('screen_name', $_POST['name']))
  2039. {
  2040. $error[] = $this->EE->lang->line('cmt_name_not_allowed');
  2041. }
  2042. /** ----------------------------------------
  2043. /** Missing or invalid email address
  2044. /** ----------------------------------------*/
  2045. if ($query->row('comment_require_email') == 'y')
  2046. {
  2047. $this->EE->load->helper('email');
  2048. if ($_POST['email'] == '')
  2049. {
  2050. $error[] = $this->EE->lang->line('cmt_missing_email');
  2051. }
  2052. elseif ( ! valid_email($_POST['email']))
  2053. {
  2054. $error[] = $this->EE->lang->line('cmt_invalid_email');
  2055. }
  2056. }
  2057. }
  2058. /** -------------------------------------
  2059. /** Is email banned?
  2060. /** -------------------------------------*/
  2061. if ($_POST['email'] != '')
  2062. {
  2063. if ($this->EE->session->ban_check('email', $_POST['email']))
  2064. {
  2065. $error[] = $this->EE->lang->line('cmt_banned_email');
  2066. }
  2067. }
  2068. /** ----------------------------------------
  2069. /** Is comment too big?
  2070. /** ----------------------------------------*/
  2071. if ($query->row('comment_max_chars') != '' AND $query->row('comment_max_chars') != 0)
  2072. {
  2073. if (strlen($_POST['comment']) > $query->row('comment_max_chars') )
  2074. {
  2075. $str = str_replace("%n", strlen($_POST['comment']), $this->EE->lang->line('cmt_too_large'));
  2076. $str = str_replace("%x", $query->row('comment_max_chars') , $str);
  2077. $error[] = $str;
  2078. }
  2079. }
  2080. /** ----------------------------------------
  2081. /** Do we have errors to display?
  2082. /** ----------------------------------------*/
  2083. if (count($error) > 0)
  2084. {
  2085. return $this->EE->output->show_user_error('submission', $error);
  2086. }
  2087. /** ----------------------------------------
  2088. /** Do we require CAPTCHA?
  2089. /** ----------------------------------------*/
  2090. if ($query->row('comment_use_captcha') == 'y')
  2091. {
  2092. if ($this->EE->config->item('captcha_require_members') == 'y' OR ($this->EE->config->item('captcha_require_members') == 'n' AND $this->EE->session->userdata('member_id') == 0))
  2093. {
  2094. if ( ! isset($_POST['captcha']) OR $_POST['captcha'] == '')
  2095. {
  2096. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('captcha_required'));
  2097. }
  2098. else
  2099. {
  2100. $this->EE->db->where('word', $_POST['captcha']);
  2101. $this->EE->db->where('ip_address', $this->EE->input->ip_address());
  2102. $this->EE->db->where('date > UNIX_TIMESTAMP()-7200', NULL, FALSE);
  2103. $result = $this->EE->db->count_all_results('captcha');
  2104. if ($result == 0)
  2105. {
  2106. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('captcha_incorrect'));
  2107. }
  2108. // @TODO: AR
  2109. $this->EE->db->query("DELETE FROM exp_captcha WHERE (word='".$this->EE->db->escape_str($_POST['captcha'])."' AND ip_address = '".$this->EE->input->ip_address()."') OR date < UNIX_TIMESTAMP()-7200");
  2110. }
  2111. }
  2112. }
  2113. /** ----------------------------------------
  2114. /** Build the data array
  2115. /** ----------------------------------------*/
  2116. $this->EE->load->helper('url');
  2117. $notify = ($this->EE->input->post('notify_me')) ? 'y' : 'n';
  2118. $cmtr_name = $this->EE->input->post('name', TRUE);
  2119. $cmtr_email = $this->EE->input->post('email');
  2120. $cmtr_loc = $this->EE->input->post('location', TRUE);
  2121. $cmtr_url = $this->EE->input->post('url', TRUE);
  2122. $cmtr_url = prep_url($cmtr_url);
  2123. $data = array(
  2124. 'channel_id' => $channel_id,
  2125. 'entry_id' => $_POST['entry_id'],
  2126. 'author_id' => $this->EE->session->userdata('member_id'),
  2127. 'name' => $cmtr_name,
  2128. 'email' => $cmtr_email,
  2129. 'url' => $cmtr_url,
  2130. 'location' => $cmtr_loc,
  2131. 'comment' => $this->EE->security->xss_clean($_POST['comment']),
  2132. 'comment_date' => $this->EE->localize->now,
  2133. 'ip_address' => $this->EE->input->ip_address(),
  2134. 'status' => ($comment_moderate == 'y') ? 'p' : 'o',
  2135. 'site_id' => $this->EE->config->item('site_id')
  2136. );
  2137. // -------------------------------------------
  2138. // 'insert_comment_insert_array' hook.
  2139. // - Modify any of the soon to be inserted values
  2140. //
  2141. if ($this->EE->extensions->active_hook('insert_comment_insert_array') === TRUE)
  2142. {
  2143. $data = $this->EE->extensions->call('insert_comment_insert_array', $data);
  2144. if ($this->EE->extensions->end_script === TRUE) return;
  2145. }
  2146. //
  2147. // -------------------------------------------
  2148. $return_link = ( ! stristr($_POST['RET'],'http://') && ! stristr($_POST['RET'],'https://')) ? $this->EE->functions->create_url($_POST['RET']) : $_POST['RET'];
  2149. /** ----------------------------------------
  2150. /** Insert data
  2151. /** ----------------------------------------*/
  2152. if ($this->EE->config->item('secure_forms') == 'y')
  2153. {
  2154. $query = $this->EE->db->query("SELECT COUNT(*) AS count FROM exp_security_hashes WHERE hash='".$this->EE->db->escape_str($_POST['XID'])."' AND ip_address = '".$this->EE->input->ip_address()."' AND date > UNIX_TIMESTAMP()-7200");
  2155. if ($query->row('count') > 0)
  2156. {
  2157. $sql = $this->EE->db->insert_string('exp_comments', $data);
  2158. $this->EE->db->query($sql);
  2159. $comment_id = $this->EE->db->insert_id();
  2160. $this->EE->db->query("DELETE FROM exp_security_hashes WHERE (hash='".$this->EE->db->escape_str($_POST['XID'])."' AND ip_address = '".$this->EE->input->ip_address()."') OR date < UNIX_TIMESTAMP()-7200");
  2161. }
  2162. else
  2163. {
  2164. $this->EE->functions->redirect(stripslashes($return_link));
  2165. }
  2166. }
  2167. else
  2168. {
  2169. $sql = $this->EE->db->insert_string('exp_comments', $data);
  2170. $this->EE->db->query($sql);
  2171. $comment_id = $this->EE->db->insert_id();
  2172. }
  2173. if ($notify == 'y')
  2174. {
  2175. $this->EE->load->library('subscription');
  2176. $this->EE->subscription->init('comment', array('entry_id' => $entry_id), TRUE);
  2177. if ($cmtr_id = $this->EE->session->userdata('member_id'))
  2178. {
  2179. $this->EE->subscription->subscribe($cmtr_id);
  2180. }
  2181. else
  2182. {
  2183. $this->EE->subscription->subscribe($cmtr_email);
  2184. }
  2185. }
  2186. if ($comment_moderate == 'n')
  2187. {
  2188. /** ------------------------------------------------
  2189. /** Update comment total and "recent comment" date
  2190. /** ------------------------------------------------*/
  2191. $this->EE->db->set('comment_total', $comment_total);
  2192. $this->EE->db->set('recent_comment_date', $this->EE->localize->now);
  2193. $this->EE->db->where('entry_id', $_POST['entry_id']);
  2194. $this->EE->db->update('channel_titles');
  2195. /** ----------------------------------------
  2196. /** Update member comment total and date
  2197. /** ----------------------------------------*/
  2198. if ($this->EE->session->userdata('member_id') != 0)
  2199. {
  2200. $this->EE->db->select('total_comments');
  2201. $this->EE->db->where('member_id', $this->EE->session->userdata('member_id'));
  2202. $query = $this->EE->db->get('members');
  2203. $this->EE->db->set('total_comments', $query->row('total_comments') + 1);
  2204. $this->EE->db->set('last_comment_date', $this->EE->localize->now);
  2205. $this->EE->db->where('member_id', $this->EE->session->userdata('member_id'));
  2206. $this->EE->db->update('members');
  2207. }
  2208. /** ----------------------------------------
  2209. /** Update comment stats
  2210. /** ----------------------------------------*/
  2211. $this->EE->stats->update_comment_stats($channel_id, $this->EE->localize->now);
  2212. /** ----------------------------------------
  2213. /** Fetch email notification addresses
  2214. /** ----------------------------------------*/
  2215. $this->EE->load->library('subscription');
  2216. $this->EE->subscription->init('comment', array('entry_id' => $entry_id), TRUE);
  2217. // Remove the current user
  2218. $ignore = ($this->EE->session->userdata('member_id') != 0) ? $this->EE->session->userdata('member_id') : $this->EE->input->post('email');
  2219. // Grab them all
  2220. $subscriptions = $this->EE->subscription->get_subscriptions($ignore);
  2221. $this->EE->load->model('comment_model');
  2222. $recipients = $this->EE->comment_model->fetch_email_recipients($_POST['entry_id'], $subscriptions);
  2223. }
  2224. /** ----------------------------------------
  2225. /** Fetch Author Notification
  2226. /** ----------------------------------------*/
  2227. if ($author_notify == 'y')
  2228. {
  2229. $this->EE->db->select('email');
  2230. $this->EE->db->where('member_id', $author_id);
  2231. $result = $this->EE->db->get('members');
  2232. $notify_address .= ','.$result->row('email');
  2233. }
  2234. /** ----------------------------------------
  2235. /** Instantiate Typography class
  2236. /** ----------------------------------------*/
  2237. $config = ($this->EE->config->item('comment_word_censoring') == 'y') ? array('word_censor' => TRUE) : array();
  2238. $this->EE->load->library('typography');
  2239. $this->EE->typography->initialize($config);
  2240. $this->EE->typography->parse_images = FALSE;
  2241. $this->EE->typography->allow_headings = FALSE;
  2242. $this->EE->typography->smileys = FALSE;
  2243. $comment = $this->EE->security->xss_clean($_POST['comment']);
  2244. $comment = $this->EE->typography->parse_type( $comment,
  2245. array(
  2246. 'text_format' => 'none',
  2247. 'html_format' => 'none',
  2248. 'auto_links' => 'n',
  2249. 'allow_img_url' => 'n'
  2250. )
  2251. );
  2252. $path = ($comment_url == '') ? $channel_url : $comment_url;
  2253. $comment_url_title_auto_path = reduce_double_slashes($path.'/'.$url_title);
  2254. /** ----------------------------
  2255. /** Send admin notification
  2256. /** ----------------------------*/
  2257. if ($notify_address != '')
  2258. {
  2259. $cp_url = $this->EE->config->item('cp_url').'?S=0&D=cp&C=addons_modules&M=show_module_cp&module=comment';
  2260. $swap = array(
  2261. 'name' => $cmtr_name,
  2262. 'name_of_commenter' => $cmtr_name,
  2263. 'email' => $cmtr_email,
  2264. 'url' => $cmtr_url,
  2265. 'location' => $cmtr_loc,
  2266. 'channel_name' => $channel_title,
  2267. 'entry_title' => $entry_title,
  2268. 'comment_id' => $comment_id,
  2269. 'comment' => $comment,
  2270. 'comment_url' => $this->remove_session_id($this->EE->functions->fetch_site_index().$_POST['URI']),
  2271. 'delete_link' => $cp_url.'&method=delete_comment_confirm&comment_id='.$comment_id,
  2272. 'approve_link' => $cp_url.'&method=change_comment_status&comment_id='.$comment_id.'&status=o',
  2273. 'close_link' => $cp_url.'&method=change_comment_status&comment_id='.$comment_id.'&status=c',
  2274. 'channel_id' => $channel_id,
  2275. 'entry_id' => $entry_id,
  2276. 'url_title' => $url_title,
  2277. 'comment_url_title_auto_path' => $comment_url_title_auto_path
  2278. );
  2279. $template = $this->EE->functions->fetch_email_template('admin_notify_comment');
  2280. $email_tit = $this->EE->functions->var_swap($template['title'], $swap);
  2281. $email_msg = $this->EE->functions->var_swap($template['data'], $swap);
  2282. // We don't want to send an admin notification if the person
  2283. // leaving the comment is an admin in the notification list
  2284. if ($_POST['email'] != '')
  2285. {
  2286. if (strpos($notify_address, $_POST['email']) !== FALSE)
  2287. {
  2288. $notify_address = str_replace($_POST['email'], '', $notify_address);
  2289. }
  2290. }
  2291. $this->EE->load->helper('string');
  2292. // Remove multiple commas
  2293. $notify_address = reduce_multiples($notify_address, ',', TRUE);
  2294. if ($notify_address != '')
  2295. {
  2296. /** ----------------------------
  2297. /** Send email
  2298. /** ----------------------------*/
  2299. $this->EE->load->library('email');
  2300. $replyto = ($data['email'] == '') ? $this->EE->config->item('webmaster_email') : $data['email'];
  2301. $sent = array();
  2302. // Load the text helper
  2303. $this->EE->load->helper('text');
  2304. foreach (explode(',', $notify_address) as $addy)
  2305. {
  2306. if (in_array($addy, $sent)) continue;
  2307. $this->EE->email->EE_initialize();
  2308. $this->EE->email->wordwrap = false;
  2309. $this->EE->email->from($this->EE->config->item('webmaster_email'), $this->EE->config->item('webmaster_name'));
  2310. $this->EE->email->to($addy);
  2311. $this->EE->email->reply_to($replyto);
  2312. $this->EE->email->subject($email_tit);
  2313. $this->EE->email->message(entities_to_ascii($email_msg));
  2314. $this->EE->email->send();
  2315. $sent[] = $addy;
  2316. }
  2317. }
  2318. }
  2319. /** ----------------------------------------
  2320. /** Send user notifications
  2321. /** ----------------------------------------*/
  2322. if ($comment_moderate == 'n')
  2323. {
  2324. $email_msg = '';
  2325. if (count($recipients) > 0)
  2326. {
  2327. $action_id = $this->EE->functions->fetch_action_id('Comment_mcp', 'delete_comment_notification');
  2328. $swap = array(
  2329. 'name_of_commenter' => $cmtr_name,
  2330. 'channel_name' => $channel_title,
  2331. 'entry_title' => $entry_title,
  2332. 'site_name' => stripslashes($this->EE->config->item('site_name')),
  2333. 'site_url' => $this->EE->config->item('site_url'),
  2334. 'comment_url' => $this->remove_session_id($this->EE->functions->fetch_site_index().$_POST['URI']),
  2335. 'comment_id' => $comment_id,
  2336. 'comment' => $comment,
  2337. 'channel_id' => $channel_id,
  2338. 'entry_id' => $entry_id,
  2339. 'url_title' => $url_title,
  2340. 'comment_url_title_auto_path' => $comment_url_title_auto_path
  2341. );
  2342. $template = $this->EE->functions->fetch_email_template('comment_notification');
  2343. $email_tit = $this->EE->functions->var_swap($template['title'], $swap);
  2344. $email_msg = $this->EE->functions->var_swap($template['data'], $swap);
  2345. /** ----------------------------
  2346. /** Send email
  2347. /** ----------------------------*/
  2348. $this->EE->load->library('email');
  2349. $this->EE->email->wordwrap = true;
  2350. $cur_email = ($_POST['email'] == '') ? FALSE : $_POST['email'];
  2351. if ( ! isset($sent)) $sent = array();
  2352. // Load the text helper
  2353. $this->EE->load->helper('text');
  2354. foreach ($recipients as $val)
  2355. {
  2356. // We don't notify the person currently commenting. That would be silly.
  2357. if ( ! in_array($val['0'], $sent))
  2358. {
  2359. $title = $email_tit;
  2360. $message = $email_msg;
  2361. $sub = $subscriptions[$val['1']];
  2362. $sub_qs = 'id='.$sub['subscription_id'].'&hash='.$sub['hash'];
  2363. // Deprecate the {name} variable at some point
  2364. $title = str_replace('{name}', $val['2'], $title);
  2365. $message = str_replace('{name}', $val['2'], $message);
  2366. $title = str_replace('{name_of_recipient}', $val['2'], $title);
  2367. $message = str_replace('{name_of_recipient}', $val['2'], $message);
  2368. $title = str_replace('{notification_removal_url}', $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$action_id.'&'.$sub_qs, $title);
  2369. $message = str_replace('{notification_removal_url}', $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$action_id.'&'.$sub_qs, $message);
  2370. $this->EE->email->EE_initialize();
  2371. $this->EE->email->from($this->EE->config->item('webmaster_email'), $this->EE->config->item('webmaster_name'));
  2372. $this->EE->email->to($val['0']);
  2373. $this->EE->email->subject($title);
  2374. $this->EE->email->message(entities_to_ascii($message));
  2375. $this->EE->email->send();
  2376. $sent[] = $val['0'];
  2377. }
  2378. }
  2379. }
  2380. /** ----------------------------------------
  2381. /** Clear cache files
  2382. /** ----------------------------------------*/
  2383. $this->EE->functions->clear_caching('all', $this->EE->functions->fetch_site_index().$_POST['URI']);
  2384. // clear out the entry_id version if the url_title is in the URI, and vice versa
  2385. if (preg_match("#\/".preg_quote($url_title)."\/#", $_POST['URI'], $matches))
  2386. {
  2387. $this->EE->functions->clear_caching('all', $this->EE->functions->fetch_site_index().preg_replace("#".preg_quote($matches['0'])."#", "/{$data['entry_id']}/", $_POST['URI']));
  2388. }
  2389. else
  2390. {
  2391. $this->EE->functions->clear_caching('all', $this->EE->functions->fetch_site_index().preg_replace("#{$data['entry_id']}#", $url_title, $_POST['URI']));
  2392. }
  2393. }
  2394. /** ----------------------------------------
  2395. /** Set cookies
  2396. /** ----------------------------------------*/
  2397. if ($notify == 'y')
  2398. {
  2399. $this->EE->functions->set_cookie('notify_me', 'yes', 60*60*24*365);
  2400. }
  2401. else
  2402. {
  2403. $this->EE->functions->set_cookie('notify_me', 'no', 60*60*24*365);
  2404. }
  2405. if ($this->EE->input->post('save_info'))
  2406. {
  2407. $this->EE->functions->set_cookie('save_info', 'yes', 60*60*24*365);
  2408. $this->EE->functions->set_cookie('my_name', $_POST['name'], 60*60*24*365);
  2409. $this->EE->functions->set_cookie('my_email', $_POST['email'], 60*60*24*365);
  2410. $this->EE->functions->set_cookie('my_url', $_POST['url'], 60*60*24*365);
  2411. $this->EE->functions->set_cookie('my_location', $_POST['location'], 60*60*24*365);
  2412. }
  2413. else
  2414. {
  2415. $this->EE->functions->set_cookie('save_info', 'no', 60*60*24*365);
  2416. $this->EE->functions->set_cookie('my_name', '');
  2417. $this->EE->functions->set_cookie('my_email', '');
  2418. $this->EE->functions->set_cookie('my_url', '');
  2419. $this->EE->functions->set_cookie('my_location', '');
  2420. }
  2421. // -------------------------------------------
  2422. // 'insert_comment_end' hook.
  2423. // - More emails, more processing, different redirect
  2424. // - $comment_id added in 1.6.1
  2425. //
  2426. $edata = $this->EE->extensions->call('insert_comment_end', $data, $comment_moderate, $comment_id);
  2427. if ($this->EE->extensions->end_script === TRUE) return;
  2428. //
  2429. // -------------------------------------------
  2430. /** -------------------------------------------
  2431. /** Bounce user back to the comment page
  2432. /** -------------------------------------------*/
  2433. if ($comment_moderate == 'y')
  2434. {
  2435. $data = array( 'title' => $this->EE->lang->line('cmt_comment_accepted'),
  2436. 'heading' => $this->EE->lang->line('thank_you'),
  2437. 'content' => $this->EE->lang->line('cmt_will_be_reviewed'),
  2438. 'redirect' => $return_link,
  2439. 'link' => array($return_link, $this->EE->lang->line('cmt_return_to_comments')),
  2440. 'rate' => 3
  2441. );
  2442. $this->EE->output->show_message($data);
  2443. }
  2444. else
  2445. {
  2446. $this->EE->functions->redirect($return_link);
  2447. }
  2448. }
  2449. // --------------------------------------------------------------------
  2450. /**
  2451. * Comment subscription tag
  2452. *
  2453. *
  2454. * @access public
  2455. * @return string
  2456. */
  2457. function notification_links()
  2458. {
  2459. // Membership is required
  2460. if ($this->EE->session->userdata('member_id') == 0)
  2461. {
  2462. return;
  2463. }
  2464. $entry_id = FALSE;
  2465. $qstring = $this->EE->uri->query_string;
  2466. if (preg_match("#(^|/)P(\d+)(/|$)#", $qstring, $match))
  2467. {
  2468. $qstring = trim($this->EE->functions->remove_double_slashes(str_replace($match['0'], '/', $qstring)), '/');
  2469. }
  2470. // Figure out the right entry ID
  2471. // If there is a slash in the entry ID we'll kill everything after it.
  2472. $entry_seg = trim($qstring);
  2473. $entry_seg= preg_replace("#/.+#", "", $entry_seg);
  2474. if (is_numeric($entry_seg))
  2475. {
  2476. $entry_id = $entry_seg;
  2477. }
  2478. else
  2479. {
  2480. $this->EE->db->select('entry_id');
  2481. $query = $this->EE->db->get_where('channel_titles', array('url_title' => $entry_seg));
  2482. if ($query->num_rows() == 1)
  2483. {
  2484. $row = $query->row();
  2485. $entry_id = $row->entry_id;
  2486. }
  2487. }
  2488. // entry_id is required
  2489. if ( ! $entry_id)
  2490. {
  2491. return;
  2492. }
  2493. $this->EE->load->library('subscription');
  2494. $this->EE->subscription->init('comment', array('entry_id' => $entry_id), TRUE);
  2495. $subscribed = $this->EE->subscription->is_subscribed(FALSE);
  2496. $action_id = $this->EE->functions->fetch_action_id('Comment', 'comment_subscribe');
  2497. // Bleh- really need a conditional for if they are subscribed
  2498. $sub_link = $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$action_id.'&entry_id='.$entry_id.'&ret='.$this->EE->uri->uri_string();
  2499. $unsub_link = $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$action_id.'&entry_id='.$entry_id.'&type=unsubscribe'.'&ret='. $this->EE->uri->uri_string();
  2500. $data[] = array('subscribe_link' => $sub_link, 'unsubscribe_link' => $unsub_link, 'subscribed' => $subscribed);
  2501. $tagdata = $this->EE->TMPL->tagdata;
  2502. return $this->EE->TMPL->parse_variables($tagdata, $data);
  2503. }
  2504. // --------------------------------------------------------------------
  2505. /**
  2506. * Comment subscription w/out commenting
  2507. *
  2508. *
  2509. * @access public
  2510. * @return string
  2511. */
  2512. function comment_subscribe()
  2513. {
  2514. // Membership is required
  2515. if ($this->EE->session->userdata('member_id') == 0)
  2516. {
  2517. return;
  2518. }
  2519. $id = $this->EE->input->get('entry_id');
  2520. $type = ($this->EE->input->get('type')) ? 'unsubscribe' : 'subscribe';
  2521. $ret = $this->EE->input->get('ret');
  2522. if ( ! $id)
  2523. {
  2524. return;
  2525. }
  2526. $this->EE->lang->loadfile('comment');
  2527. // Does entry exist?
  2528. $this->EE->db->select('title');
  2529. $query = $this->EE->db->get_where('channel_titles', array('entry_id' => $id));
  2530. if ($query->num_rows() != 1)
  2531. {
  2532. return $this->EE->output->show_user_error('submission', 'invalid_subscription');
  2533. }
  2534. $row = $query->row();
  2535. $entry_title = $row->title;
  2536. // Are they currently subscribed
  2537. $this->EE->load->library('subscription');
  2538. $this->EE->subscription->init('comment', array('entry_id' => $id), TRUE);
  2539. $subscribed = $this->EE->subscription->is_subscribed(FALSE);
  2540. if ($type == 'subscribe' && $subscribed == TRUE)
  2541. {
  2542. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('already_subscribed'));
  2543. }
  2544. if ($type == 'unsubscribe' && $subscribed == FALSE)
  2545. {
  2546. return $this->EE->output->show_user_error('submission', $this->EE->lang->line('not_currently_subscribed'));
  2547. }
  2548. // They check out- let them through
  2549. $this->EE->subscription->$type();
  2550. // Show success message
  2551. $this->EE->lang->loadfile('comment');
  2552. $title = ($type == 'unsubscribe') ? 'cmt_unsubscribe' : 'cmt_subscribe';
  2553. $content = ($type == 'unsubscribe') ? 'you_have_been_unsubscribed' : 'you_have_been_subscribed';
  2554. $return_link = $this->EE->functions->create_url($ret);
  2555. $data = array( 'title' => $this->EE->lang->line($title),
  2556. 'heading' => $this->EE->lang->line('thank_you'),
  2557. 'content' => $this->EE->lang->line($content).' '.$entry_title,
  2558. 'redirect' => $return_link,
  2559. 'link' => array($return_link, $this->EE->lang->line('cmt_return_to_comments')),
  2560. 'rate' => 3
  2561. );
  2562. $this->EE->output->show_message($data);
  2563. }
  2564. // --------------------------------------------------------------------
  2565. /**
  2566. * Remove session ID from string
  2567. *
  2568. * This function is used mainly by the Input class to strip
  2569. * session IDs if they are used in public pages.
  2570. *
  2571. * @access public
  2572. * @param string
  2573. * @return string
  2574. */
  2575. function remove_session_id($str)
  2576. {
  2577. return preg_replace("#S=.+?/#", "", $str);
  2578. }
  2579. // --------------------------------------------------------------------
  2580. /**
  2581. * Frontend comment editing
  2582. *
  2583. *
  2584. * @access public
  2585. * @param string
  2586. * @return string
  2587. */
  2588. function edit_comment($ajax_request = TRUE)
  2589. {
  2590. @header("Content-type: text/html; charset=UTF-8");
  2591. if ($this->EE->input->get_post('comment_id') === FALSE OR (($this->EE->input->get_post('comment') === FALSE OR $this->EE->input->get_post('comment') == '') && $this->EE->input->get_post('status') != 'close'))
  2592. {
  2593. exit('null');
  2594. }
  2595. // Not logged in member- eject
  2596. if ($this->EE->session->userdata['member_id'] == '0')
  2597. {
  2598. exit('null');
  2599. }
  2600. $edited_status = ($this->EE->input->get_post('status') != 'close') ? FALSE : 'c';
  2601. $edited_comment = $this->EE->input->get_post('comment');
  2602. $can_edit = FALSE;
  2603. $can_moderate = FALSE;
  2604. $this->EE->db->from('comments');
  2605. $this->EE->db->from('channels');
  2606. $this->EE->db->from('channel_titles');
  2607. $this->EE->db->select('comments.author_id, comments.comment_date, channel_titles.author_id AS entry_author_id, channels.comment_text_formatting, channels.comment_html_formatting, channels.comment_allow_img_urls, channels.comment_auto_link_urls');
  2608. $this->EE->db->where('comment_id', $this->EE->input->get_post('comment_id'));
  2609. $this->EE->db->where('comments.channel_id = '.$this->EE->db->dbprefix('channels').'.channel_id');
  2610. $this->EE->db->where('comments.entry_id = '.$this->EE->db->dbprefix('channel_titles').'.entry_id');
  2611. $query = $this->EE->db->get();
  2612. if ($query->num_rows() > 0)
  2613. {
  2614. if ($this->EE->session->userdata['group_id'] == 1 OR
  2615. $this->EE->session->userdata['can_edit_all_comments'] == 'y' OR
  2616. ($this->EE->session->userdata['can_edit_own_comments'] == 'y' && $query->row('entry_author_id') == $this->EE->session->userdata['member_id']))
  2617. {
  2618. $can_edit = TRUE;
  2619. $can_moderate = TRUE;
  2620. }
  2621. elseif ($this->EE->session->userdata['member_id'] != '0' && $query->row('author_id') == $this->EE->session->userdata['member_id'])
  2622. {
  2623. // Check for time limit
  2624. if ($this->EE->config->item('comment_edit_time_limit') > 0)
  2625. {
  2626. if ($query->row('comment_date') > $this->EE->localize->now - 60*$this->EE->config->item('edit_time_limit'))
  2627. {
  2628. $can_edit = TRUE;
  2629. }
  2630. }
  2631. else
  2632. {
  2633. $can_edit = TRUE;
  2634. }
  2635. }
  2636. $data = array();
  2637. if ($edited_status != FALSE & $can_moderate != FALSE)
  2638. {
  2639. $data['status'] = 'c';
  2640. }
  2641. if ($edited_comment != FALSE & $can_edit != FALSE)
  2642. {
  2643. $data['comment'] = $edited_comment;
  2644. }
  2645. if (count($data) > 0)
  2646. {
  2647. $this->EE->db->where('comment_id', $this->EE->input->get_post('comment_id'));
  2648. $this->EE->db->update('comments', $data);
  2649. if ($edited_status != FALSE & $can_moderate != FALSE)
  2650. {
  2651. exit('Comment Closed');
  2652. }
  2653. $this->EE->load->library('typography');
  2654. exit( $this->EE->typography->parse_type( stripslashes($this->EE->input->get_post('comment')),
  2655. array(
  2656. 'text_format' => $query->row('comment_text_formatting'),
  2657. 'html_format' => $query->row('comment_html_formatting'),
  2658. 'auto_links' => $query->row('comment_auto_link_urls'),
  2659. 'allow_img_url' => $query->row('comment_allow_img_urls')
  2660. )
  2661. ));
  2662. }
  2663. }
  2664. exit('null');
  2665. }
  2666. function ajax_edit_url()
  2667. {
  2668. $url = $this->EE->functions->fetch_site_index(0, 0).QUERY_MARKER.'ACT='.$this->EE->functions->fetch_action_id('Comment', 'edit_comment');
  2669. return $url;
  2670. }
  2671. }
  2672. // END CLASS
  2673. /* End of file mod.comment.php */
  2674. /* Location: ./system/expressionengine/modules/comment/mod.comment.php */