ManageSearchEngines.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file contains all the screens that relate to search engines.
  15. // !!!
  16. */
  17. // Entry point for this section.
  18. function SearchEngines()
  19. {
  20. global $context, $txt, $scripturl;
  21. isAllowedTo('admin_forum');
  22. loadLanguage('Search');
  23. loadTemplate('ManageSearch');
  24. $subActions = array(
  25. 'editspiders' => 'EditSpider',
  26. 'logs' => 'SpiderLogs',
  27. 'settings' => 'ManageSearchEngineSettings',
  28. 'spiders' => 'ViewSpiders',
  29. 'stats' => 'SpiderStats',
  30. );
  31. // Ensure we have a valid subaction.
  32. $context['sub_action'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'stats';
  33. $context['page_title'] = $txt['search_engines'];
  34. // Some more tab data.
  35. $context[$context['admin_menu_name']]['tab_data'] = array(
  36. 'title' => $txt['search_engines'],
  37. 'description' => $txt['search_engines_description'],
  38. );
  39. // Call the function!
  40. $subActions[$context['sub_action']]();
  41. }
  42. // This is really just the settings page.
  43. function ManageSearchEngineSettings($return_config = false)
  44. {
  45. global $context, $txt, $modSettings, $scripturl, $sourcedir, $smcFunc;
  46. $config_vars = array(
  47. // How much detail?
  48. array('select', 'spider_mode', array($txt['spider_mode_off'], $txt['spider_mode_standard'], $txt['spider_mode_high'], $txt['spider_mode_vhigh']), 'onchange' => 'disableFields();'),
  49. 'spider_group' => array('select', 'spider_group', array($txt['spider_group_none'], $txt['membergroups_members'])),
  50. array('select', 'show_spider_online', array($txt['show_spider_online_no'], $txt['show_spider_online_summary'], $txt['show_spider_online_detail'], $txt['show_spider_online_detail_admin'])),
  51. );
  52. // Set up a message.
  53. $context['settings_message'] = '<span class="smalltext">' . sprintf($txt['spider_settings_desc'], $scripturl . '?action=admin;area=logs;sa=pruning;' . $context['session_var'] . '=' . $context['session_id']) . '</span>';
  54. // Do some javascript.
  55. $javascript_function = '
  56. function disableFields()
  57. {
  58. disabledState = document.getElementById(\'spider_mode\').value == 0;';
  59. foreach ($config_vars as $variable)
  60. if ($variable[1] != 'spider_mode')
  61. $javascript_function .= '
  62. if (document.getElementById(\'' . $variable[1] . '\'))
  63. document.getElementById(\'' . $variable[1] . '\').disabled = disabledState;';
  64. $javascript_function .= '
  65. }
  66. disableFields();';
  67. if ($return_config)
  68. return $config_vars;
  69. // We need to load the groups for the spider group thingy.
  70. $request = $smcFunc['db_query']('', '
  71. SELECT id_group, group_name
  72. FROM {db_prefix}membergroups
  73. WHERE id_group != {int:admin_group}
  74. AND id_group != {int:moderator_group}',
  75. array(
  76. 'admin_group' => 1,
  77. 'moderator_group' => 3,
  78. )
  79. );
  80. while ($row = $smcFunc['db_fetch_assoc']($request))
  81. $config_vars['spider_group'][2][$row['id_group']] = $row['group_name'];
  82. $smcFunc['db_free_result']($request);
  83. // Make sure it's valid - note that regular members are given id_group = 1 which is reversed in Load.php - no admins here!
  84. if (isset($_POST['spider_group']) && !isset($config_vars['spider_group'][2][$_POST['spider_group']]))
  85. $_POST['spider_group'] = 0;
  86. // We'll want this for our easy save.
  87. require_once($sourcedir . '/ManageServer.php');
  88. // Setup the template.
  89. $context['page_title'] = $txt['settings'];
  90. $context['sub_template'] = 'show_settings';
  91. // Are we saving them - are we??
  92. if (isset($_GET['save']))
  93. {
  94. checkSession();
  95. saveDBSettings($config_vars);
  96. recacheSpiderNames();
  97. redirectexit('action=admin;area=sengines;sa=settings');
  98. }
  99. // Final settings...
  100. $context['post_url'] = $scripturl . '?action=admin;area=sengines;save;sa=settings';
  101. $context['settings_title'] = $txt['settings'];
  102. $context['settings_insert_below'] = '
  103. <script type="text/javascript"><!-- // --><![CDATA[
  104. ' . $javascript_function . '
  105. // ]]></script>';
  106. // Prepare the settings...
  107. prepareDBSettingContext($config_vars);
  108. }
  109. // View a list of all the spiders we know about.
  110. function ViewSpiders()
  111. {
  112. global $context, $txt, $sourcedir, $scripturl, $smcFunc;
  113. if (!isset($_SESSION['spider_stat']) || $_SESSION['spider_stat'] < time() - 60)
  114. {
  115. consolidateSpiderStats();
  116. $_SESSION['spider_stat'] = time();
  117. }
  118. // Are we adding a new one?
  119. if (!empty($_POST['addSpider']))
  120. return EditSpider();
  121. // User pressed the 'remove selection button'.
  122. elseif (!empty($_POST['removeSpiders']) && !empty($_POST['remove']) && is_array($_POST['remove']))
  123. {
  124. checkSession();
  125. // Make sure every entry is a proper integer.
  126. foreach ($_POST['remove'] as $index => $spider_id)
  127. $_POST['remove'][(int) $index] = (int) $spider_id;
  128. // Delete them all!
  129. $smcFunc['db_query']('', '
  130. DELETE FROM {db_prefix}spiders
  131. WHERE id_spider IN ({array_int:remove_list})',
  132. array(
  133. 'remove_list' => $_POST['remove'],
  134. )
  135. );
  136. $smcFunc['db_query']('', '
  137. DELETE FROM {db_prefix}log_spider_hits
  138. WHERE id_spider IN ({array_int:remove_list})',
  139. array(
  140. 'remove_list' => $_POST['remove'],
  141. )
  142. );
  143. $smcFunc['db_query']('', '
  144. DELETE FROM {db_prefix}log_spider_stats
  145. WHERE id_spider IN ({array_int:remove_list})',
  146. array(
  147. 'remove_list' => $_POST['remove'],
  148. )
  149. );
  150. cache_put_data('spider_search', null, 300);
  151. recacheSpiderNames();
  152. }
  153. // Get the last seens.
  154. $request = $smcFunc['db_query']('', '
  155. SELECT id_spider, MAX(last_seen) AS last_seen_time
  156. FROM {db_prefix}log_spider_stats
  157. GROUP BY id_spider',
  158. array(
  159. )
  160. );
  161. $context['spider_last_seen'] = array();
  162. while ($row = $smcFunc['db_fetch_assoc']($request))
  163. $context['spider_last_seen'][$row['id_spider']] = $row['last_seen_time'];
  164. $smcFunc['db_free_result']($request);
  165. $listOptions = array(
  166. 'id' => 'spider_list',
  167. 'items_per_page' => 20,
  168. 'base_href' => $scripturl . '?action=admin;area=sengines;sa=spiders',
  169. 'default_sort_col' => 'name',
  170. 'get_items' => array(
  171. 'function' => 'list_getSpiders',
  172. ),
  173. 'get_count' => array(
  174. 'function' => 'list_getNumSpiders',
  175. ),
  176. 'no_items_label' => $txt['spiders_no_entries'],
  177. 'columns' => array(
  178. 'name' => array(
  179. 'header' => array(
  180. 'value' => $txt['spider_name'],
  181. ),
  182. 'data' => array(
  183. 'function' => create_function('$rowData', '
  184. global $scripturl;
  185. return sprintf(\'<a href="%1$s?action=admin;area=sengines;sa=editspiders;sid=%2$d">%3$s</a>\', $scripturl, $rowData[\'id_spider\'], htmlspecialchars($rowData[\'spider_name\']));
  186. '),
  187. ),
  188. 'sort' => array(
  189. 'default' => 'spider_name',
  190. 'reverse' => 'spider_name DESC',
  191. ),
  192. ),
  193. 'last_seen' => array(
  194. 'header' => array(
  195. 'value' => $txt['spider_last_seen'],
  196. ),
  197. 'data' => array(
  198. 'function' => create_function('$rowData', '
  199. global $context, $txt;
  200. return isset($context[\'spider_last_seen\'][$rowData[\'id_spider\']]) ? timeformat($context[\'spider_last_seen\'][$rowData[\'id_spider\']]) : $txt[\'spider_last_never\'];
  201. '),
  202. ),
  203. ),
  204. 'user_agent' => array(
  205. 'header' => array(
  206. 'value' => $txt['spider_agent'],
  207. ),
  208. 'data' => array(
  209. 'db_htmlsafe' => 'user_agent',
  210. ),
  211. 'sort' => array(
  212. 'default' => 'user_agent',
  213. 'reverse' => 'user_agent DESC',
  214. ),
  215. ),
  216. 'ip_info' => array(
  217. 'header' => array(
  218. 'value' => $txt['spider_ip_info'],
  219. ),
  220. 'data' => array(
  221. 'db_htmlsafe' => 'ip_info',
  222. 'class' => 'smalltext',
  223. ),
  224. 'sort' => array(
  225. 'default' => 'ip_info',
  226. 'reverse' => 'ip_info DESC',
  227. ),
  228. ),
  229. 'check' => array(
  230. 'header' => array(
  231. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  232. ),
  233. 'data' => array(
  234. 'sprintf' => array(
  235. 'format' => '<input type="checkbox" name="remove[]" value="%1$d" class="input_check" />',
  236. 'params' => array(
  237. 'id_spider' => false,
  238. ),
  239. ),
  240. 'style' => 'text-align: center',
  241. ),
  242. ),
  243. ),
  244. 'form' => array(
  245. 'href' => $scripturl . '?action=admin;area=sengines;sa=spiders',
  246. ),
  247. 'additional_rows' => array(
  248. array(
  249. 'position' => 'below_table_data',
  250. 'value' => '
  251. <input type="submit" name="addSpider" value="' . $txt['spiders_add'] . '" class="button_submit" />
  252. <input type="submit" name="removeSpiders" value="' . $txt['spiders_remove_selected'] . '" onclick="return confirm(\'' . $txt['spider_remove_selected_confirm'] . '\');" class="button_submit" />
  253. ',
  254. 'style' => 'text-align: right;',
  255. ),
  256. ),
  257. );
  258. require_once($sourcedir . '/Subs-List.php');
  259. createList($listOptions);
  260. $context['sub_template'] = 'show_list';
  261. $context['default_list'] = 'spider_list';
  262. }
  263. function list_getSpiders($start, $items_per_page, $sort)
  264. {
  265. global $smcFunc;
  266. $request = $smcFunc['db_query']('', '
  267. SELECT id_spider, spider_name, user_agent, ip_info
  268. FROM {db_prefix}spiders
  269. ORDER BY ' . $sort . '
  270. LIMIT ' . $start . ', ' . $items_per_page,
  271. array(
  272. )
  273. );
  274. $spiders = array();
  275. while ($row = $smcFunc['db_fetch_assoc']($request))
  276. $spiders[$row['id_spider']] = $row;
  277. $smcFunc['db_free_result']($request);
  278. return $spiders;
  279. }
  280. function list_getNumSpiders()
  281. {
  282. global $smcFunc;
  283. $request = $smcFunc['db_query']('', '
  284. SELECT COUNT(*) AS num_spiders
  285. FROM {db_prefix}spiders',
  286. array(
  287. )
  288. );
  289. list ($numSpiders) = $smcFunc['db_fetch_row']($request);
  290. $smcFunc['db_free_result']($request);
  291. return $numSpiders;
  292. }
  293. // Here we can add, and edit, spider info!
  294. function EditSpider()
  295. {
  296. global $context, $smcFunc, $txt;
  297. // Some standard stuff.
  298. $context['id_spider'] = !empty($_GET['sid']) ? (int) $_GET['sid'] : 0;
  299. $context['page_title'] = $context['id_spider'] ? $txt['spiders_edit'] : $txt['spiders_add'];
  300. $context['sub_template'] = 'spider_edit';
  301. // Are we saving?
  302. if (!empty($_POST['save']))
  303. {
  304. checkSession();
  305. $ips = array();
  306. // Check the IP range is valid.
  307. $ip_sets = explode(',', $_POST['spider_ip']);
  308. foreach ($ip_sets as $set)
  309. {
  310. $test = ip2range(trim($set));
  311. if (!empty($test))
  312. $ips[] = $set;
  313. }
  314. $ips = implode(',', $ips);
  315. // Goes in as it is...
  316. if ($context['id_spider'])
  317. $smcFunc['db_query']('', '
  318. UPDATE {db_prefix}spiders
  319. SET spider_name = {string:spider_name}, user_agent = {string:spider_agent},
  320. ip_info = {string:ip_info}
  321. WHERE id_spider = {int:current_spider}',
  322. array(
  323. 'current_spider' => $context['id_spider'],
  324. 'spider_name' => $_POST['spider_name'],
  325. 'spider_agent' => $_POST['spider_agent'],
  326. 'ip_info' => $ips,
  327. )
  328. );
  329. else
  330. $smcFunc['db_insert']('insert',
  331. '{db_prefix}spiders',
  332. array(
  333. 'spider_name' => 'string', 'user_agent' => 'string', 'ip_info' => 'string',
  334. ),
  335. array(
  336. $_POST['spider_name'], $_POST['spider_agent'], $ips,
  337. ),
  338. array('id_spider')
  339. );
  340. // Order by user agent length.
  341. sortSpiderTable();
  342. cache_put_data('spider_search', null, 300);
  343. recacheSpiderNames();
  344. redirectexit('action=admin;area=sengines;sa=spiders');
  345. }
  346. // The default is new.
  347. $context['spider'] = array(
  348. 'id' => 0,
  349. 'name' => '',
  350. 'agent' => '',
  351. 'ip_info' => '',
  352. );
  353. // An edit?
  354. if ($context['id_spider'])
  355. {
  356. $request = $smcFunc['db_query']('', '
  357. SELECT id_spider, spider_name, user_agent, ip_info
  358. FROM {db_prefix}spiders
  359. WHERE id_spider = {int:current_spider}',
  360. array(
  361. 'current_spider' => $context['id_spider'],
  362. )
  363. );
  364. if ($row = $smcFunc['db_fetch_assoc']($request))
  365. $context['spider'] = array(
  366. 'id' => $row['id_spider'],
  367. 'name' => $row['spider_name'],
  368. 'agent' => $row['user_agent'],
  369. 'ip_info' => $row['ip_info'],
  370. );
  371. $smcFunc['db_free_result']($request);
  372. }
  373. }
  374. //!!! Should this not be... you know... in a different file?
  375. // Do we think the current user is a spider?
  376. function SpiderCheck()
  377. {
  378. global $modSettings, $smcFunc;
  379. if (isset($_SESSION['id_robot']))
  380. unset($_SESSION['id_robot']);
  381. $_SESSION['robot_check'] = time();
  382. // We cache the spider data for five minutes if we can.
  383. if (!empty($modSettings['cache_enable']))
  384. $spider_data = cache_get_data('spider_search', 300);
  385. if (!isset($spider_data) || $spider_data === NULL)
  386. {
  387. $request = $smcFunc['db_query']('spider_check', '
  388. SELECT id_spider, user_agent, ip_info
  389. FROM {db_prefix}spiders',
  390. array(
  391. )
  392. );
  393. $spider_data = array();
  394. while ($row = $smcFunc['db_fetch_assoc']($request))
  395. $spider_data[] = $row;
  396. $smcFunc['db_free_result']($request);
  397. if (!empty($modSettings['cache_enable']))
  398. cache_put_data('spider_search', $spider_data, 300);
  399. }
  400. if (empty($spider_data))
  401. return false;
  402. // Only do these bits once.
  403. $ci_user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  404. preg_match('/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $_SERVER['REMOTE_ADDR'], $ip_parts);
  405. foreach ($spider_data as $spider)
  406. {
  407. // User agent is easy.
  408. if (!empty($spider['user_agent']) && strpos($ci_user_agent, strtolower($spider['user_agent'])) !== false)
  409. $_SESSION['id_robot'] = $spider['id_spider'];
  410. // IP stuff is harder.
  411. elseif (!empty($ip_parts))
  412. {
  413. $ips = explode(',', $spider['ip_info']);
  414. foreach ($ips as $ip)
  415. {
  416. $ip = ip2range($ip);
  417. if (!empty($ip))
  418. {
  419. foreach ($ip as $key => $value)
  420. {
  421. if ($value['low'] > $ip_parts[$key + 1] || $value['high'] < $ip_parts[$key + 1])
  422. break;
  423. elseif ($key == 3)
  424. $_SESSION['id_robot'] = $spider['id_spider'];
  425. }
  426. }
  427. }
  428. }
  429. if (isset($_SESSION['id_robot']))
  430. break;
  431. }
  432. // If this is low server tracking then log the spider here as oppossed to the main logging function.
  433. if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] == 1 && !empty($_SESSION['id_robot']))
  434. logSpider();
  435. return !empty($_SESSION['id_robot']) ? $_SESSION['id_robot'] : 0;
  436. }
  437. // Log the spider presence online.
  438. //!!! Different file?
  439. function logSpider()
  440. {
  441. global $smcFunc, $modSettings, $context;
  442. if (empty($modSettings['spider_mode']) || empty($_SESSION['id_robot']))
  443. return;
  444. // Attempt to update today's entry.
  445. if ($modSettings['spider_mode'] == 1)
  446. {
  447. $date = strftime('%Y-%m-%d', forum_time(false));
  448. $smcFunc['db_query']('', '
  449. UPDATE {db_prefix}log_spider_stats
  450. SET last_seen = {int:current_time}, page_hits = page_hits + 1
  451. WHERE id_spider = {int:current_spider}
  452. AND stat_date = {date:current_date}',
  453. array(
  454. 'current_date' => $date,
  455. 'current_time' => time(),
  456. 'current_spider' => $_SESSION['id_robot'],
  457. )
  458. );
  459. // Nothing updated?
  460. if ($smcFunc['db_affected_rows']() == 0)
  461. {
  462. $smcFunc['db_insert']('ignore',
  463. '{db_prefix}log_spider_stats',
  464. array(
  465. 'id_spider' => 'int', 'last_seen' => 'int', 'stat_date' => 'date', 'page_hits' => 'int',
  466. ),
  467. array(
  468. $_SESSION['id_robot'], time(), $date, 1,
  469. ),
  470. array('id_spider', 'stat_date')
  471. );
  472. }
  473. }
  474. // If we're tracking better stats than track, better stats - we sort out the today thing later.
  475. else
  476. {
  477. if ($modSettings['spider_mode'] > 2)
  478. {
  479. $url = $_GET + array('USER_AGENT' => $_SERVER['HTTP_USER_AGENT']);
  480. unset($url['sesc'], $url[$context['session_var']]);
  481. $url = serialize($url);
  482. }
  483. else
  484. $url = '';
  485. $smcFunc['db_insert']('insert',
  486. '{db_prefix}log_spider_hits',
  487. array('id_spider' => 'int', 'log_time' => 'int', 'url' => 'string'),
  488. array($_SESSION['id_robot'], time(), $url),
  489. array()
  490. );
  491. }
  492. }
  493. // This function takes any unprocessed hits and turns them into stats.
  494. function consolidateSpiderStats()
  495. {
  496. global $smcFunc;
  497. $request = $smcFunc['db_query']('consolidate_spider_stats', '
  498. SELECT id_spider, MAX(log_time) AS last_seen, COUNT(*) AS num_hits
  499. FROM {db_prefix}log_spider_hits
  500. WHERE processed = {int:not_processed}
  501. GROUP BY id_spider, MONTH(log_time), DAYOFMONTH(log_time)',
  502. array(
  503. 'not_processed' => 0,
  504. )
  505. );
  506. $spider_hits = array();
  507. while ($row = $smcFunc['db_fetch_assoc']($request))
  508. $spider_hits[] = $row;
  509. $smcFunc['db_free_result']($request);
  510. if (empty($spider_hits))
  511. return;
  512. // Attempt to update the master data.
  513. $stat_inserts = array();
  514. foreach ($spider_hits as $stat)
  515. {
  516. // We assume the max date is within the right day.
  517. $date = strftime('%Y-%m-%d', $stat['last_seen']);
  518. $smcFunc['db_query']('', '
  519. UPDATE {db_prefix}log_spider_stats
  520. SET page_hits = page_hits + ' . $stat['num_hits'] . ',
  521. last_seen = CASE WHEN last_seen > {int:last_seen} THEN last_seen ELSE {int:last_seen} END
  522. WHERE id_spider = {int:current_spider}
  523. AND stat_date = {date:last_seen_date}',
  524. array(
  525. 'last_seen_date' => $date,
  526. 'last_seen' => $stat['last_seen'],
  527. 'current_spider' => $stat['id_spider'],
  528. )
  529. );
  530. if ($smcFunc['db_affected_rows']() == 0)
  531. $stat_inserts[] = array($date, $stat['id_spider'], $stat['num_hits'], $stat['last_seen']);
  532. }
  533. // New stats?
  534. if (!empty($stat_inserts))
  535. $smcFunc['db_insert']('ignore',
  536. '{db_prefix}log_spider_stats',
  537. array('stat_date' => 'date', 'id_spider' => 'int', 'page_hits' => 'int', 'last_seen' => 'int'),
  538. $stat_inserts,
  539. array('stat_date', 'id_spider')
  540. );
  541. // All processed.
  542. $smcFunc['db_query']('', '
  543. UPDATE {db_prefix}log_spider_hits
  544. SET processed = {int:is_processed}
  545. WHERE processed = {int:not_processed}',
  546. array(
  547. 'is_processed' => 1,
  548. 'not_processed' => 0,
  549. )
  550. );
  551. }
  552. // See what spiders have been up to.
  553. function SpiderLogs()
  554. {
  555. global $context, $txt, $sourcedir, $scripturl, $smcFunc, $modSettings;
  556. // Load the template and language just incase.
  557. loadLanguage('Search');
  558. loadTemplate('ManageSearch');
  559. // Did they want to delete some entries?
  560. if (!empty($_POST['delete_entries']) && isset($_POST['older']))
  561. {
  562. checkSession();
  563. $deleteTime = time() - (((int) $_POST['older']) * 24 * 60 * 60);
  564. // Delete the entires.
  565. $smcFunc['db_query']('', '
  566. DELETE FROM {db_prefix}log_spider_hits
  567. WHERE log_time < {int:delete_period}',
  568. array(
  569. 'delete_period' => $deleteTime,
  570. )
  571. );
  572. }
  573. $listOptions = array(
  574. 'id' => 'spider_logs',
  575. 'items_per_page' => 20,
  576. 'title' => $txt['spider_logs'],
  577. 'no_items_label' => $txt['spider_logs_empty'],
  578. 'base_href' => $context['admin_area'] == 'sengines' ? $scripturl . '?action=admin;area=sengines;sa=logs' : $scripturl . '?action=admin;area=logs;sa=spiderlog',
  579. 'default_sort_col' => 'log_time',
  580. 'get_items' => array(
  581. 'function' => 'list_getSpiderLogs',
  582. ),
  583. 'get_count' => array(
  584. 'function' => 'list_getNumSpiderLogs',
  585. ),
  586. 'columns' => array(
  587. 'name' => array(
  588. 'header' => array(
  589. 'value' => $txt['spider'],
  590. ),
  591. 'data' => array(
  592. 'db' => 'spider_name',
  593. ),
  594. 'sort' => array(
  595. 'default' => 's.spider_name',
  596. 'reverse' => 's.spider_name DESC',
  597. ),
  598. ),
  599. 'log_time' => array(
  600. 'header' => array(
  601. 'value' => $txt['spider_time'],
  602. ),
  603. 'data' => array(
  604. 'function' => create_function('$rowData', '
  605. return timeformat($rowData[\'log_time\']);
  606. '),
  607. ),
  608. 'sort' => array(
  609. 'default' => 'sl.id_hit DESC',
  610. 'reverse' => 'sl.id_hit',
  611. ),
  612. ),
  613. 'viewing' => array(
  614. 'header' => array(
  615. 'value' => $txt['spider_viewing'],
  616. ),
  617. 'data' => array(
  618. 'db' => 'url',
  619. ),
  620. ),
  621. ),
  622. 'additional_rows' => array(
  623. array(
  624. 'position' => 'after_title',
  625. 'value' => $txt['spider_logs_info'],
  626. 'class' => 'smalltext',
  627. ),
  628. ),
  629. );
  630. require_once($sourcedir . '/Subs-List.php');
  631. createList($listOptions);
  632. // Now determine the actions of the URLs.
  633. if (!empty($context['spider_logs']['rows']))
  634. {
  635. $urls = array();
  636. // Grab the current /url.
  637. foreach ($context['spider_logs']['rows'] as $k => $row)
  638. {
  639. // Feature disabled?
  640. if (empty($row['viewing']['value']) && isset($modSettings['spider_mode']) && $modSettings['spider_mode'] < 3)
  641. $context['spider_logs']['rows'][$k]['viewing']['value'] = '<em>' . $txt['spider_disabled'] . '</em>';
  642. else
  643. $urls[$k] = array($row['viewing']['value'], -1);
  644. }
  645. // Now stick in the new URLs.
  646. require_once($sourcedir . '/Who.php');
  647. $urls = determineActions($urls, 'whospider_');
  648. foreach ($urls as $k => $new_url)
  649. {
  650. $context['spider_logs']['rows'][$k]['viewing']['value'] = $new_url;
  651. }
  652. }
  653. $context['page_title'] = $txt['spider_logs'];
  654. $context['sub_template'] = 'show_spider_logs';
  655. $context['default_list'] = 'spider_logs';
  656. }
  657. function list_getSpiderLogs($start, $items_per_page, $sort)
  658. {
  659. global $smcFunc;
  660. $request = $smcFunc['db_query']('', '
  661. SELECT sl.id_spider, sl.url, sl.log_time, s.spider_name
  662. FROM {db_prefix}log_spider_hits AS sl
  663. INNER JOIN {db_prefix}spiders AS s ON (s.id_spider = sl.id_spider)
  664. ORDER BY ' . $sort . '
  665. LIMIT ' . $start . ', ' . $items_per_page,
  666. array(
  667. )
  668. );
  669. $spider_logs = array();
  670. while ($row = $smcFunc['db_fetch_assoc']($request))
  671. $spider_logs[] = $row;
  672. $smcFunc['db_free_result']($request);
  673. return $spider_logs;
  674. }
  675. function list_getNumSpiderLogs()
  676. {
  677. global $smcFunc;
  678. $request = $smcFunc['db_query']('', '
  679. SELECT COUNT(*) AS num_logs
  680. FROM {db_prefix}log_spider_hits',
  681. array(
  682. )
  683. );
  684. list ($numLogs) = $smcFunc['db_fetch_row']($request);
  685. $smcFunc['db_free_result']($request);
  686. return $numLogs;
  687. }
  688. // Show the spider statistics.
  689. function SpiderStats()
  690. {
  691. global $context, $txt, $sourcedir, $scripturl, $smcFunc;
  692. // Force an update of the stats every 60 seconds.
  693. if (!isset($_SESSION['spider_stat']) || $_SESSION['spider_stat'] < time() - 60)
  694. {
  695. consolidateSpiderStats();
  696. $_SESSION['spider_stat'] = time();
  697. }
  698. // Get the earliest and latest dates.
  699. $request = $smcFunc['db_query']('', '
  700. SELECT MIN(stat_date) AS first_date, MAX(stat_date) AS last_date
  701. FROM {db_prefix}log_spider_stats',
  702. array(
  703. )
  704. );
  705. list ($min_date, $max_date) = $smcFunc['db_fetch_row']($request);
  706. $smcFunc['db_free_result']($request);
  707. $min_year = (int) substr($min_date, 0, 4);
  708. $max_year = (int) substr($max_date, 0, 4);
  709. $min_month = (int) substr($min_date, 5, 2);
  710. $max_month = (int) substr($max_date, 5, 2);
  711. // Prepare the dates for the drop down.
  712. $date_choices = array();
  713. for ($y = $min_year; $y <= $max_year; $y++)
  714. for ($m = 1; $m <= 12; $m++)
  715. {
  716. // This doesn't count?
  717. if ($y == $min_year && $m < $min_month)
  718. continue;
  719. if ($y == $max_year && $m > $max_month)
  720. break;
  721. $date_choices[$y . $m] = $txt['months_short'][$m] . ' ' . $y;
  722. }
  723. // What are we currently viewing?
  724. $current_date = isset($_REQUEST['new_date']) && isset($date_choices[$_REQUEST['new_date']]) ? $_REQUEST['new_date'] : $max_date;
  725. // Prepare the HTML.
  726. $date_select = '
  727. ' . $txt['spider_stats_select_month'] . ':
  728. <select name="new_date" onchange="document.spider_stat_list.submit();">';
  729. if (empty($date_choices))
  730. $date_select .= '
  731. <option></option>';
  732. else
  733. foreach ($date_choices as $id => $text)
  734. $date_select .= '
  735. <option value="' . $id . '"' . ($current_date == $id ? ' selected="selected"' : '') . '>' . $text . '</option>';
  736. $date_select .= '
  737. </select>
  738. <noscript>
  739. <input type="submit" name="go" value="' . $txt['go'] . '" class="button_submit" />
  740. </noscript>';
  741. // If we manually jumped to a date work out the offset.
  742. if (isset($_REQUEST['new_date']))
  743. {
  744. $date_query = sprintf('%04d-%02d-01', substr($current_date, 0, 4), substr($current_date, 4));
  745. $request = $smcFunc['db_query']('', '
  746. SELECT COUNT(*) AS offset
  747. FROM {db_prefix}log_spider_stats
  748. WHERE stat_date < {date:date_being_viewed}',
  749. array(
  750. 'date_being_viewed' => $date_query,
  751. )
  752. );
  753. list ($_REQUEST['start']) = $smcFunc['db_fetch_row']($request);
  754. $smcFunc['db_free_result']($request);
  755. }
  756. $listOptions = array(
  757. 'id' => 'spider_stat_list',
  758. 'items_per_page' => 20,
  759. 'base_href' => $scripturl . '?action=admin;area=sengines;sa=stats',
  760. 'default_sort_col' => 'stat_date',
  761. 'get_items' => array(
  762. 'function' => 'list_getSpiderStats',
  763. ),
  764. 'get_count' => array(
  765. 'function' => 'list_getNumSpiderStats',
  766. ),
  767. 'no_items_label' => $txt['spider_stats_no_entries'],
  768. 'columns' => array(
  769. 'stat_date' => array(
  770. 'header' => array(
  771. 'value' => $txt['date'],
  772. ),
  773. 'data' => array(
  774. 'db' => 'stat_date',
  775. ),
  776. 'sort' => array(
  777. 'default' => 'stat_date',
  778. 'reverse' => 'stat_date DESC',
  779. ),
  780. ),
  781. 'name' => array(
  782. 'header' => array(
  783. 'value' => $txt['spider_name'],
  784. ),
  785. 'data' => array(
  786. 'db' => 'spider_name',
  787. ),
  788. 'sort' => array(
  789. 'default' => 's.spider_name',
  790. 'reverse' => 's.spider_name DESC',
  791. ),
  792. ),
  793. 'page_hits' => array(
  794. 'header' => array(
  795. 'value' => $txt['spider_stats_page_hits'],
  796. ),
  797. 'data' => array(
  798. 'db' => 'page_hits',
  799. ),
  800. 'sort' => array(
  801. 'default' => 'ss.page_hits',
  802. 'reverse' => 'ss.page_hits DESC',
  803. ),
  804. ),
  805. ),
  806. 'form' => array(
  807. 'href' => $scripturl . '?action=admin;area=sengines;sa=stats',
  808. 'name' => 'spider_stat_list',
  809. ),
  810. 'additional_rows' => array(
  811. array(
  812. 'position' => 'below_table_data',
  813. 'value' => $date_select,
  814. 'style' => 'text-align: right;',
  815. ),
  816. ),
  817. );
  818. require_once($sourcedir . '/Subs-List.php');
  819. createList($listOptions);
  820. $context['sub_template'] = 'show_list';
  821. $context['default_list'] = 'spider_stat_list';
  822. }
  823. function list_getSpiderStats($start, $items_per_page, $sort)
  824. {
  825. global $smcFunc;
  826. $request = $smcFunc['db_query']('', '
  827. SELECT ss.id_spider, ss.stat_date, ss.page_hits, s.spider_name
  828. FROM {db_prefix}log_spider_stats AS ss
  829. INNER JOIN {db_prefix}spiders AS s ON (s.id_spider = ss.id_spider)
  830. ORDER BY ' . $sort . '
  831. LIMIT ' . $start . ', ' . $items_per_page,
  832. array(
  833. )
  834. );
  835. $spider_stats = array();
  836. while ($row = $smcFunc['db_fetch_assoc']($request))
  837. $spider_stats[] = $row;
  838. $smcFunc['db_free_result']($request);
  839. return $spider_stats;
  840. }
  841. function list_getNumSpiderStats()
  842. {
  843. global $smcFunc;
  844. $request = $smcFunc['db_query']('', '
  845. SELECT COUNT(*) AS num_stats
  846. FROM {db_prefix}log_spider_stats',
  847. array(
  848. )
  849. );
  850. list ($numStats) = $smcFunc['db_fetch_row']($request);
  851. $smcFunc['db_free_result']($request);
  852. return $numStats;
  853. }
  854. // Recache spider names?
  855. function recacheSpiderNames()
  856. {
  857. global $smcFunc;
  858. $request = $smcFunc['db_query']('', '
  859. SELECT id_spider, spider_name
  860. FROM {db_prefix}spiders',
  861. array(
  862. )
  863. );
  864. $spiders = array();
  865. while ($row = $smcFunc['db_fetch_assoc']($request))
  866. $spiders[$row['id_spider']] = $row['spider_name'];
  867. $smcFunc['db_free_result']($request);
  868. updateSettings(array('spider_name_cache' => serialize($spiders)));
  869. }
  870. // Sort the search engine table by user agent name to avoid misidentification of engine.
  871. function sortSpiderTable()
  872. {
  873. global $smcFunc;
  874. db_extend('packages');
  875. // Add a sorting column.
  876. $smcFunc['db_add_column']('{db_prefix}spiders', array('name' => 'temp_order', 'size' => 8, 'type' => 'mediumint', 'null' => false));
  877. // Set the contents of this column.
  878. $smcFunc['db_query']('set_spider_order', '
  879. UPDATE {db_prefix}spiders
  880. SET temp_order = LENGTH(user_agent)',
  881. array(
  882. )
  883. );
  884. // Order the table by this column.
  885. $smcFunc['db_query']('alter_table_spiders', '
  886. ALTER TABLE {db_prefix}spiders
  887. ORDER BY temp_order DESC',
  888. array(
  889. 'db_error_skip' => true,
  890. )
  891. );
  892. // Remove the sorting column.
  893. $smcFunc['db_remove_column']('{db_prefix}spiders', 'temp_order');
  894. }
  895. ?>