PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/system/expressionengine/modules/referrer/mcp.referrer.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 539 lines | 357 code | 117 blank | 65 comment | 49 complexity | 6fd4be6dfc00c5da5c9b5366b75a93b9 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Referrer Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Modules
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Referrer_mcp {
  24. /**
  25. * Constructor
  26. */
  27. function Referrer_mcp( $switch = TRUE )
  28. {
  29. // Make a local reference to the ExpressionEngine super object
  30. $this->EE =& get_instance();
  31. $base_url = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer';
  32. $this->EE->cp->set_right_nav(array(
  33. 'view_referrers' => $base_url.AMP.'method=view',
  34. 'clear_referrers' => $base_url.AMP.'method=clear',
  35. 'referrer_preferences' => BASE.AMP.'C=admin_system'.AMP.'M=tracking_preferences'
  36. ));
  37. }
  38. // --------------------------------------------------------------------
  39. /**
  40. * Referrer Home Page
  41. */
  42. function index()
  43. {
  44. $vars['cp_page_title'] = $this->EE->lang->line('referrers');
  45. $vars['num_referrers'] = $this->EE->db->count_all('referrers');
  46. $this->EE->load->library('javascript');
  47. $this->EE->javascript->compile();
  48. return $this->EE->load->view('index', $vars, TRUE);
  49. }
  50. // --------------------------------------------------------------------
  51. /**
  52. * View Referrers
  53. */
  54. function view()
  55. {
  56. $this->EE->load->library('pagination');
  57. $this->EE->load->library('javascript');
  58. $this->EE->load->library('table');
  59. $this->EE->load->helper('form');
  60. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer', $this->EE->lang->line('referrers'));
  61. $vars['cp_page_title'] = $this->EE->lang->line('view_referrers');
  62. $this->EE->jquery->tablesorter('.mainTable', '{
  63. headers: {5: {sorter: false}},
  64. widgets: ["zebra"]
  65. }');
  66. $this->EE->javascript->output(array(
  67. '$(".toggle_all").toggle(
  68. function(){
  69. $("input.toggle").each(function() {
  70. this.checked = true;
  71. });
  72. }, function (){
  73. var checked_status = this.checked;
  74. $("input.toggle").each(function() {
  75. this.checked = false;
  76. });
  77. }
  78. );')
  79. );
  80. $this->EE->cp->add_to_foot('<script type="text/javascript">function showHide(entryID, htmlObj, linkType) {
  81. extTextDivID = ("extText" + (entryID));
  82. extLinkDivID = ("extLink" + (entryID));
  83. if (linkType == "close")
  84. {
  85. document.getElementById(extTextDivID).style.display = "none";
  86. document.getElementById(extLinkDivID).style.display = "block";
  87. htmlObj.blur();
  88. }
  89. else
  90. {
  91. document.getElementById(extTextDivID).style.display = "block";
  92. document.getElementById(extLinkDivID).style.display = "none";
  93. htmlObj.blur();
  94. }
  95. }
  96. </script>');
  97. $vars['referrers'] = array(); // used to pass referrer info into view, but initialized here in case there are no results.
  98. $rownum = ($this->EE->input->get_post('rownum') != '') ? $this->EE->input->get_post('rownum') : 0;
  99. $perpage = 10;
  100. $search_str = '';
  101. $search_sql = '';
  102. $vars['search']['name'] = 'search';
  103. if ( isset($_GET['search']) OR isset($_POST['search']))
  104. {
  105. $search_str = (isset($_POST['search'])) ? stripslashes($_POST['search']) : base64_decode($_GET['search']);
  106. }
  107. if ($search_str != '')
  108. {
  109. // Load the search helper so we can filter the keywords
  110. $this->EE->load->helper('search');
  111. $s = preg_split("/\s+/", sanitize_search_terms($search_str));
  112. foreach($s as $part)
  113. {
  114. if (substr($part, 0, 1) == '-')
  115. {
  116. $search_sql .= "CONCAT_WS(' ', ref_from, ref_to, ref_ip, ref_agent) NOT LIKE '%".$this->EE->db->escape_like_str(substr($part, 1))."%' AND ";
  117. }
  118. else
  119. {
  120. $search_sql .= "CONCAT_WS(' ', ref_from, ref_to, ref_ip, ref_agent) LIKE '%".$this->EE->db->escape_like_str($part)."%' AND ";
  121. }
  122. }
  123. $sql = "WHERE (".substr($search_sql, 0, -4).")";
  124. $vars['search']['value'] = sanitize_search_terms($search_str);
  125. }
  126. else
  127. {
  128. $sql = "";
  129. }
  130. $query = $this->EE->db->query("SELECT COUNT(*) AS count FROM exp_referrers ".$sql);
  131. $vars['num_referrers'] = $query->row('count');
  132. if ($query->row('count') == 0)
  133. {
  134. $vars['message'] = (isset($vars['search']['value'])) ? $this->EE->lang->line('referrer_no_results') : $this->EE->lang->line('no_referrers');
  135. return $this->EE->load->view('view', $vars, TRUE);
  136. exit;
  137. }
  138. $sites_query = $this->EE->db->query("SELECT site_id, site_label FROM exp_sites");
  139. $sites = array();
  140. foreach($sites_query->result_array() as $row)
  141. {
  142. $sites[$row['site_id']] = $row['site_label'];
  143. }
  144. $query = $this->EE->db->query("SELECT * FROM exp_referrers ".$sql." ORDER BY ref_id desc LIMIT $rownum, $perpage");
  145. $site_url = $this->EE->config->item('site_url');
  146. foreach($query->result_array() as $row)
  147. {
  148. // From
  149. $row['ref_from'] = str_replace('http://','',$row['ref_from']);
  150. if (strlen($row['ref_from']) > 40)
  151. {
  152. $from_pieces = explode('/',$row['ref_from']);
  153. $new_from = $from_pieces['0'].'/';
  154. for($p=1; $p < count($from_pieces); $p++)
  155. {
  156. if (strlen($from_pieces[$p]) + strlen($new_from) <= 40)
  157. {
  158. $new_from .= ($p == (count($from_pieces) - 1)) ? $from_pieces[$p] : $from_pieces[$p].'/';
  159. }
  160. else
  161. {
  162. $new_from .= '&#8230;';
  163. break;
  164. }
  165. }
  166. }
  167. else
  168. {
  169. $new_from = $row['ref_from'];
  170. }
  171. $vars['referrers'][$row['ref_id']]['from_link'] = $this->EE->functions->fetch_site_index().QUERY_MARKER.'URL='.urlencode($row['ref_from']);
  172. $vars['referrers'][$row['ref_id']]['from_url'] = $new_from;
  173. // To
  174. $vars['referrers'][$row['ref_id']]['to_link'] = $this->EE->functions->fetch_site_index().QUERY_MARKER.'URL='.urlencode($row['ref_to']);
  175. $vars['referrers'][$row['ref_id']]['to_url'] = '/'.ltrim(str_replace($site_url, '', $row['ref_to']), '/');
  176. // Date
  177. $vars['referrers'][$row['ref_id']]['date'] = ($row['ref_date'] != '' AND $row['ref_date'] != 0) ? $this->EE->localize->set_human_time($row['ref_date']) : '-';
  178. // IP
  179. $vars['referrers'][$row['ref_id']]['referrer_ip'] = ($row['ref_ip'] != '' AND $row['ref_ip'] != 0) ? $row['ref_ip'] : '-';
  180. // Agent
  181. $agent = ($row['ref_agent'] != '') ? $row['ref_agent'] : '-';
  182. if (strlen($agent) > 11)
  183. {
  184. $agent2 = '<span class="defaultBold">'.$this->EE->lang->line('ref_user_agent').'</span>:'.NBS."<a href=\"javascript:void(0);\" name=\"ext{$row['ref_id']}\" onclick=\"showHide({$row['ref_id']},this,'close');return false;\">[-]</a>".NBS.NBS.$agent;
  185. $agent = "<div id='extLink{$row['ref_id']}'><span class='defaultBold'>". $this->EE->lang->line('ref_user_agent').'</span>:'.NBS."<a href=\"javascript:void(0);\" name=\"ext{$row['ref_id']}\" onclick=\"showHide({$row['ref_id']},this,'open');return false;\">[+]</a>".NBS.NBS.preg_replace("/(.+?)\s+.*/", "\\1", $agent)."</div>";
  186. $agent .= '<div id="extText'.$row['ref_id'].'" style="display: none; padding:0;">'.$agent2.'</div>';
  187. }
  188. $vars['referrers'][$row['ref_id']]['user_agent'] = $agent;
  189. // Site
  190. $vars['referrers'][$row['ref_id']]['site'] = $sites[$row['site_id']];
  191. // Toggle checkbox
  192. $vars['referrers'][$row['ref_id']]['toggle'] = array(
  193. 'name' => 'toggle[]',
  194. 'id' => 'delete_box_'.$row['ref_id'],
  195. 'value' => $row['ref_id'],
  196. 'class' =>'toggle'
  197. );
  198. }
  199. // Pass the relevant data to the paginate class
  200. $config['base_url'] = BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer'.AMP.'method=view';
  201. $config['total_rows'] = $vars['num_referrers'];
  202. $config['per_page'] = $perpage;
  203. $config['page_query_string'] = TRUE;
  204. $config['query_string_segment'] = 'rownum';
  205. $config['full_tag_open'] = '<p id="paginationLinks">';
  206. $config['full_tag_close'] = '</p>';
  207. $config['prev_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_prev_button.gif" width="13" height="13" alt="&lt;" />';
  208. $config['next_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_next_button.gif" width="13" height="13" alt="&gt;" />';
  209. $config['first_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_first_button.gif" width="13" height="13" alt="&lt; &lt;" />';
  210. $config['last_link'] = '<img src="'.$this->EE->cp->cp_theme_url.'images/pagination_last_button.gif" width="13" height="13" alt="&gt; &gt;" />';
  211. $this->EE->pagination->initialize($config);
  212. $vars['pagination'] = $this->EE->pagination->create_links();
  213. $this->EE->javascript->compile();
  214. return $this->EE->load->view('view', $vars, TRUE);
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Delete Confirm
  219. */
  220. function delete_confirm()
  221. {
  222. if ( ! $this->EE->input->post('toggle'))
  223. {
  224. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer');
  225. }
  226. $this->EE->load->helper('form');
  227. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer', $this->EE->lang->line('referrers'));
  228. $vars['cp_page_title'] = $this->EE->lang->line('delete_confirm');
  229. $vars['form_action'] = 'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer'.AMP.'method=delete';
  230. foreach ($_POST['toggle'] as $key => $val)
  231. {
  232. $vars['damned'][] = $val;
  233. }
  234. if ($this->EE->db->table_exists('exp_blacklisted') === TRUE)
  235. {
  236. $vars['add_ips'] = $this->EE->lang->line('add_and_blacklist_ips');
  237. $vars['add_urls'] = $this->EE->lang->line('add_and_blacklist_urls');
  238. $vars['add_agents'] = $this->EE->lang->line('add_and_blacklist_agents');
  239. }
  240. else
  241. {
  242. $vars['add_ips'] = $this->EE->lang->line('add_ips');
  243. $vars['add_urls'] = $this->EE->lang->line('add_urls');
  244. $vars['add_agents'] = $this->EE->lang->line('add_agents');
  245. }
  246. $this->EE->javascript->compile();
  247. return $this->EE->load->view('delete_confirm', $vars, TRUE);
  248. }
  249. // --------------------------------------------------------------------
  250. /**
  251. * Delete Referrers
  252. */
  253. function delete()
  254. {
  255. if ( ! $this->EE->input->post('delete'))
  256. {
  257. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer');
  258. }
  259. $ids = array();
  260. $new = array('url'=>array(),'ip' => array(), 'agent' => array());
  261. $white = array('url'=>array(),'ip' => array(), 'agent' => array());
  262. $IDS = " ref_id IN('".implode("','", $this->EE->db->escape_str($_POST['delete']))."') ";
  263. // Add To Blacklist?
  264. if (isset($_POST['add_urls']) OR isset($_POST['add_agents']) OR isset($_POST['add_ips']))
  265. {
  266. $query = $this->EE->db->query("SELECT ref_from, ref_ip, ref_agent FROM exp_referrers WHERE ".$IDS);
  267. if ($query->num_rows() == 0)
  268. {
  269. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer');
  270. }
  271. // New Values
  272. foreach($query->result_array() as $row)
  273. {
  274. if(isset($_POST['add_urls']))
  275. {
  276. $mod_url = str_replace('http://','',$row['ref_from']);
  277. $new['url'][] = str_replace('www.','',$mod_url);
  278. }
  279. if(isset($_POST['add_agents']))
  280. {
  281. $new['agent'][] = $row['ref_agent'];
  282. }
  283. if(isset($_POST['add_ips']))
  284. {
  285. $new['ip'][] = $row['ref_ip'];
  286. }
  287. }
  288. // Add Current Blacklisted - but only if installed
  289. if ($this->EE->db->table_exists('exp_blacklisted') === TRUE)
  290. {
  291. $query = $this->EE->db->get('blacklisted');
  292. $old['url'] = array();
  293. $old['agent'] = array();
  294. $old['ip'] = array();
  295. if ($query->num_rows() > 0)
  296. {
  297. foreach($query->result_array() as $row)
  298. {
  299. $old_values = explode('|',$row['blacklisted_value']);
  300. for ($i=0; $i < count($old_values); $i++)
  301. {
  302. $old[$row['blacklisted_type']][] = $old_values[$i];
  303. }
  304. }
  305. }
  306. // Check for uniqueness and sort
  307. $new['url'] = array_unique(array_merge($old['url'],$new['url']));
  308. $new['agent'] = array_unique(array_merge($old['agent'],$new['agent']));
  309. $new['ip'] = array_unique(array_merge($old['ip'],$new['ip']));
  310. sort($new['url']);
  311. sort($new['agent']);
  312. sort($new['ip']);
  313. // Put blacklist info back into database
  314. $this->EE->db->truncate('blacklisted');
  315. foreach($new as $key => $value)
  316. {
  317. $blacklisted_value = implode('|',$value);
  318. $data = array( 'blacklisted_type' => $key,
  319. 'blacklisted_value' => $blacklisted_value);
  320. $this->EE->db->insert('blacklisted', $data);
  321. }
  322. // Current Whitelisted
  323. $query = $this->EE->db->get('whitelisted');
  324. if ($query->num_rows() > 0)
  325. {
  326. foreach($query->result_array() as $row)
  327. {
  328. $white_values = explode('|',$row['whitelisted_value']);
  329. for ($i=0; $i < count($white_values); $i++)
  330. {
  331. if (trim($white_values[$i]) != '')
  332. {
  333. $white[$row['whitelisted_type']][] = $this->EE->db->escape_str($white_values[$i]);
  334. }
  335. }
  336. }
  337. }
  338. // Using new blacklist members, clean out spam
  339. $new['url'] = array_diff($new['url'], $old['url']);
  340. $new['agent'] = array_diff($new['agent'], $old['agent']);
  341. $new['ip'] = array_diff($new['ip'], $old['ip']);
  342. }
  343. $modified_channels = array();
  344. foreach($new as $key => $value)
  345. {
  346. $name = ($key == 'url') ? 'from' : $key;
  347. if (count($value) > 0 && isset($_POST['add_'.$key.'s']))
  348. {
  349. sort($value);
  350. for($i=0; $i < count($value); $i++)
  351. {
  352. if ($value[$i] != '')
  353. {
  354. $sql = "DELETE FROM exp_referrers WHERE ref_{$name} LIKE '%".$this->EE->db->escape_like_str($value[$i])."%'";
  355. if (count($white[$key]) > 1)
  356. {
  357. $sql .= " AND ref_{$name} NOT LIKE '%".implode("%' AND ref_{$name} NOT LIKE '%", $this->EE->db->escape_like_str($white[$key]))."%'";
  358. }
  359. elseif (count($white[$key]) > 0)
  360. {
  361. $sql .= "AND ref_{$name} NOT LIKE '%".$this->EE->db->escape_like_str($white[$key]['0'])."%'";
  362. }
  363. $this->EE->db->query($sql);
  364. }
  365. }
  366. }
  367. }
  368. }
  369. // Delete Referrers
  370. $this->EE->db->query("DELETE FROM exp_referrers WHERE ".$IDS);
  371. $message = (count($ids) == 1) ? $this->EE->lang->line('referrer_deleted') : $this->EE->lang->line('referrers_deleted');
  372. $this->EE->session->set_flashdata('message_success', $message);
  373. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer');
  374. }
  375. // --------------------------------------------------------------------
  376. /**
  377. * Clear Referrers
  378. */
  379. function clear()
  380. {
  381. $this->EE->load->helper('form');
  382. $this->EE->cp->set_breadcrumb(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer', $this->EE->lang->line('referrers'));
  383. $vars['cp_page_title'] = $this->EE->lang->line('clear_referrers');
  384. $total = $this->EE->db->count_all('referrers');
  385. $vars['total'] = $total;
  386. $save = ( ! isset($_POST['save'])) ? '' : $_POST['save'];
  387. if ($save < 0)
  388. {
  389. $save = 0;
  390. }
  391. if (is_numeric($save) AND $save >= 0)
  392. {
  393. if ($save == 0)
  394. {
  395. $this->EE->db->truncate('referrers');
  396. $total = 0;
  397. }
  398. else
  399. {
  400. if ($total > $save)
  401. {
  402. $this->EE->db->select_max('ref_id', 'max_id');
  403. $query = $this->EE->db->get('referrers');
  404. $max = ($query->num_rows() == 0 OR ! is_numeric($query->row('max_id') )) ? 0 : $query->row('max_id') ;
  405. $save--;
  406. $id = $max - $save;
  407. $this->EE->db->where("ref_id < {$id}");
  408. $this->EE->db->delete('referrers');
  409. }
  410. }
  411. $this->EE->session->set_flashdata('message_success', $this->EE->lang->line('referrers_deleted'));
  412. $this->EE->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=referrer'.AMP.'method=clear');
  413. }
  414. return $this->EE->load->view('clear', $vars, TRUE);
  415. }
  416. }
  417. // END CLASS
  418. /* End of file mcp.referrer.php */
  419. /* Location: ./system/expressionengine/modules/referrer/mcp.referrer.php */