PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/includes/functions_report_admin.php

http://torrentpier2.googlecode.com/
PHP | 569 lines | 388 code | 80 blank | 101 comment | 56 complexity | 90d8900c253a3ff524d5b35f3af871dc MD5 | raw file
  1. <?php
  2. if (!defined('BB_ROOT'))
  3. {
  4. die(basename(__FILE__));
  5. }
  6. //
  7. // Obtains report count for each report module
  8. //
  9. function report_counts_obtain()
  10. {
  11. $sql = 'SELECT rm.report_module_id, COUNT(r.report_id) AS report_count
  12. FROM ' . BB_REPORTS_MODULES . ' rm
  13. LEFT JOIN ' . BB_REPORTS . ' r
  14. ON r.report_module_id = rm.report_module_id
  15. GROUP BY rm.report_module_id';
  16. if (!$result = DB()->sql_query($sql))
  17. {
  18. message_die(GENERAL_ERROR, 'Could not obtain report counts', '', __LINE__, __FILE__, $sql);
  19. }
  20. $report_counts = array();
  21. while ($row = DB()->sql_fetchrow($result))
  22. {
  23. $report_counts[$row['report_module_id']] = $row['report_count'];
  24. }
  25. DB()->sql_freeresult($result);
  26. return $report_counts;
  27. }
  28. //
  29. // Obtains report reason count for each report module
  30. //
  31. function report_reason_counts_obtain()
  32. {
  33. $sql = 'SELECT rm.report_module_id, COUNT(rr.report_reason_id) AS reason_count
  34. FROM ' . BB_REPORTS_MODULES . ' rm
  35. LEFT JOIN ' . BB_REPORTS_REASONS . ' rr
  36. ON rr.report_module_id = rm.report_module_id
  37. GROUP BY rm.report_module_id';
  38. if (!$result = DB()->sql_query($sql))
  39. {
  40. message_die(GENERAL_ERROR, 'Could not obtain report reason counts', '', __LINE__, __FILE__, $sql);
  41. }
  42. $report_reason_counts = array();
  43. while ($row = DB()->sql_fetchrow($result))
  44. {
  45. $report_reason_counts[$row['report_module_id']] = $row['reason_count'];
  46. }
  47. DB()->sql_freeresult($result);
  48. return $report_reason_counts;
  49. }
  50. //
  51. // Obtains inactive report modules, includes modules and stores module objects
  52. //
  53. function report_modules_inactive($mode = 'all', $module = null)
  54. {
  55. global $bb_cfg;
  56. static $modules;
  57. if (!isset($modules))
  58. {
  59. if (!class_exists("report_module"))
  60. include(INC_DIR . "report_module.php");
  61. $installed_modules = report_modules('names');
  62. $deny_modes = array('open', 'process', 'clear', 'delete', 'reported');
  63. $dir = @opendir(INC_DIR .'report_hack');
  64. $modules = array();
  65. while ($file = @readdir($dir))
  66. {
  67. if (!preg_match('#(.*)\.' . bb_preg_quote('php', '#') . '$#', $file, $matches))
  68. {
  69. continue;
  70. }
  71. // exclude installed modules
  72. $module_name = $matches[1];
  73. if (isset($installed_modules[$module_name]))
  74. {
  75. continue;
  76. }
  77. // include module file
  78. include(INC_DIR . "report_hack/$file");
  79. // Include language file
  80. $lang = array();
  81. $lang_file = LANG_ROOT_DIR ."lang_{$bb_cfg['default_lang']}/report_hack/lang_$module_name.php";
  82. if (file_exists($lang_file))
  83. {
  84. include($lang_file);
  85. }
  86. // Create module object
  87. $modules[$module_name] = new $module_name(0, array('module_name' => $module_name), $lang);
  88. //
  89. // Check validity of the module
  90. //
  91. if (!empty($modules[$module_name]->mode) && in_array($modules[$module_name]->mode, $deny_modes))
  92. {
  93. unset($modules[$module_name]);
  94. }
  95. if (!isset($modules[$module_name]->id) || !isset($modules[$module_name]->data) || !isset($modules[$module_name]->lang) || !isset($modules[$module_name]->duplicates))
  96. {
  97. unset($modules[$module_name]);
  98. }
  99. }
  100. @closedir($dir);
  101. }
  102. switch ($mode)
  103. {
  104. case 'all':
  105. return $modules;
  106. break;
  107. case 'name':
  108. if (!isset($module))
  109. {
  110. return false;
  111. }
  112. return (isset($modules[$module])) ? $modules[$module] : false;
  113. break;
  114. default:
  115. return false;
  116. break;
  117. }
  118. }
  119. //
  120. // Generates the auth select box
  121. //
  122. function report_auth_select($block_name, $default, $select_items = array(REPORT_AUTH_MOD, REPORT_AUTH_ADMIN))
  123. {
  124. global $lang, $template;
  125. foreach ($select_items as $value)
  126. {
  127. $template->assign_block_vars($block_name, array(
  128. 'VALUE' => $value,
  129. 'TITLE' => $lang['REPORT_AUTH'][$value],
  130. 'SELECTED' => ($value == $default) ? ' selected="selected"' : '')
  131. );
  132. }
  133. }
  134. //
  135. // Installs a report module
  136. //
  137. function report_module_install($module_notify, $module_prune, $module_name, $auth_write, $auth_view, $auth_notify, $auth_delete, $check = true)
  138. {
  139. global $bb_cfg;
  140. //
  141. // Check module
  142. //
  143. if ($check)
  144. {
  145. if (!$report_module = report_modules_inactive('name', $module_name))
  146. {
  147. return false;
  148. }
  149. }
  150. //
  151. // Get module order
  152. //
  153. $sql = 'SELECT MAX(report_module_order) AS max_order
  154. FROM ' . BB_REPORTS_MODULES;
  155. if (!$result = DB()->sql_query($sql))
  156. {
  157. message_die(GENERAL_ERROR, 'Could not obtain max order', '', __LINE__, __FILE__, $sql);
  158. }
  159. $max_order = DB()->sql_fetchfield('max_order', 0, $result);
  160. DB()->sql_freeresult($result);
  161. //
  162. // Insert module
  163. //
  164. $sql = 'INSERT INTO ' . BB_REPORTS_MODULES . ' (report_module_order, report_module_notify, report_module_prune,
  165. report_module_name, auth_write, auth_view, auth_notify, auth_delete)
  166. VALUES(' . ($max_order + 1) . ', ' . (int) $module_notify . ', ' . (int) $module_prune . ",
  167. '" . DB()->escape($module_name) . "', " . (int) $auth_write . ', ' . (int) $auth_view . ',
  168. ' . (int) $auth_notify . ', ' . (int) $auth_delete . ')';
  169. if (!DB()->sql_query($sql))
  170. {
  171. message_die(GENERAL_ERROR, 'Could not install report module', '', __LINE__, __FILE__, $sql);
  172. }
  173. $module_id = DB()->sql_nextid();
  174. //
  175. // Clean modules cache
  176. //
  177. if ($bb_cfg['report_modules_cache'])
  178. {
  179. report_modules_cache_clean();
  180. }
  181. return $module_id;
  182. }
  183. //
  184. // Edits a module
  185. //
  186. function report_module_edit($module_id, $module_notify, $module_prune, $auth_write, $auth_view, $auth_notify, $auth_delete)
  187. {
  188. global $bb_cfg;
  189. $sql = 'UPDATE ' . BB_REPORTS_MODULES . '
  190. SET
  191. report_module_notify = ' . (int) $module_notify . ',
  192. report_module_prune = ' . (int) $module_prune . ',
  193. auth_write = ' . (int) $auth_write . ',
  194. auth_view = ' . (int) $auth_view . ',
  195. auth_notify = ' . (int) $auth_notify . ',
  196. auth_delete = ' . (int) $auth_delete . '
  197. WHERE report_module_id = ' . (int) $module_id;
  198. if (!DB()->sql_query($sql))
  199. {
  200. message_die(GENERAL_ERROR, 'Could not edit report module', '', __LINE__, __FILE__, $sql);
  201. }
  202. //
  203. // Clean modules cache
  204. //
  205. if ($bb_cfg['report_modules_cache'])
  206. {
  207. report_modules_cache_clean();
  208. }
  209. }
  210. //
  211. // Moves a module to another position (up or down), reorders other modules
  212. //
  213. function report_module_move($mode, $module_id, $steps = 1)
  214. {
  215. global $bb_cfg;
  216. if (!$report_module = report_modules('id', $module_id))
  217. {
  218. return false;
  219. }
  220. switch ($mode)
  221. {
  222. case 'up':
  223. $sql = 'UPDATE ' . BB_REPORTS_MODULES . "
  224. SET report_module_order = report_module_order + 1
  225. WHERE report_module_order >= " . ($report_module->data['report_module_order'] - (int) $steps) . '
  226. AND report_module_order < ' . $report_module->data['report_module_order'];
  227. break;
  228. case 'down':
  229. $sql = 'UPDATE ' . BB_REPORTS_MODULES . "
  230. SET report_module_order = report_module_order - 1
  231. WHERE report_module_order <= " . ($report_module->data['report_module_order'] + (int) $steps) . '
  232. AND report_module_order > ' . $report_module->data['report_module_order'];
  233. break;
  234. default:
  235. return false;
  236. break;
  237. }
  238. if (!DB()->sql_query($sql))
  239. {
  240. message_die(GENERAL_ERROR, 'Could not update module order', '', __LINE__, __FILE__, $sql);
  241. }
  242. if (DB()->affected_rows())
  243. {
  244. $op = ($mode == 'up') ? '-' : '+';
  245. $sql = 'UPDATE ' . BB_REPORTS_MODULES . "
  246. SET report_module_order = report_module_order $op 1
  247. WHERE report_module_id = " . (int) $module_id;
  248. if (!DB()->sql_query($sql))
  249. {
  250. message_die(GENERAL_ERROR, 'Could not update module order', '', __LINE__, __FILE__, $sql);
  251. }
  252. }
  253. DB()->sql_query('');
  254. //
  255. // Clean modules cache
  256. //
  257. if ($bb_cfg['report_modules_cache'])
  258. {
  259. report_modules_cache_clean();
  260. }
  261. return true;
  262. }
  263. //
  264. // Uninstalls a report module
  265. //
  266. function report_module_uninstall($module_id)
  267. {
  268. global $bb_cfg;
  269. //
  270. // Obtain reports in this module
  271. //
  272. $sql = 'SELECT report_id
  273. FROM ' . BB_REPORTS . '
  274. WHERE report_module_id = ' . (int) $module_id;
  275. if (!$result = DB()->sql_query($sql))
  276. {
  277. message_die(GENERAL_ERROR, 'Could not obtain report ids', '', __LINE__, __FILE__, $sql);
  278. }
  279. $report_ids = array();
  280. while ($row = DB()->sql_fetchrow($result))
  281. {
  282. $report_ids = $row['report_id'];
  283. }
  284. DB()->sql_freeresult($result);
  285. // delete reports
  286. reports_delete($report_ids, false, false);
  287. //
  288. // Sync module
  289. //
  290. $report_module = report_modules('id', $module_id);
  291. if (method_exists($report_module, 'sync'))
  292. {
  293. $report_module->sync(true);
  294. }
  295. //
  296. // Update module order
  297. //
  298. $sql = 'UPDATE ' . BB_REPORTS_MODULES . '
  299. SET report_module_order = report_module_order - 1
  300. WHERE report_module_order > ' . $report_module->data['report_module_order'];
  301. if (!DB()->sql_query($sql))
  302. {
  303. message_die(GENERAL_ERROR, 'Could not update module order', '', __LINE__, __FILE__, $sql);
  304. }
  305. //
  306. // Delete report reasons
  307. //
  308. $sql = 'DELETE FROM ' . BB_REPORTS_REASONS . '
  309. WHERE report_module_id = ' . (int) $module_id;
  310. if (!DB()->sql_query($sql))
  311. {
  312. message_die(GENERAL_ERROR, 'Could not delete report reasons', '', __LINE__, __FILE__, $sql);
  313. }
  314. //
  315. // Delete module
  316. //
  317. $sql = 'DELETE FROM ' . BB_REPORTS_MODULES . '
  318. WHERE report_module_id = ' . (int) $module_id;
  319. if (!DB()->sql_query($sql))
  320. {
  321. message_die(GENERAL_ERROR, 'Could not delete report module', '', __LINE__, __FILE__, $sql);
  322. }
  323. //
  324. // Clean modules cache
  325. //
  326. if ($bb_cfg['report_modules_cache'])
  327. {
  328. report_modules_cache_clean();
  329. }
  330. }
  331. //
  332. // Obtains a report reason
  333. //
  334. function report_reason_obtain($reason_id)
  335. {
  336. $sql = 'SELECT report_reason_desc
  337. FROM ' . BB_REPORTS_REASONS . '
  338. WHERE report_reason_id = ' . (int) $reason_id;
  339. if (!$result = DB()->sql_query($sql))
  340. {
  341. message_die(GENERAL_ERROR, 'Could not obtain report reason', '', __LINE__, __FILE__, $sql);
  342. }
  343. $row = DB()->sql_fetchrow($result);
  344. DB()->sql_freeresult($result);
  345. return $row;
  346. }
  347. //
  348. // Inserts a report reason
  349. //
  350. function report_reason_insert($module_id, $reason_desc)
  351. {
  352. //
  353. // Get reason order
  354. //
  355. $sql = 'SELECT MAX(report_reason_order) AS max_order
  356. FROM ' . BB_REPORTS_REASONS;
  357. if (!$result = DB()->sql_query($sql))
  358. {
  359. message_die(GENERAL_ERROR, 'Could not obtain max order', '', __LINE__, __FILE__, $sql);
  360. }
  361. $max_order = DB()->sql_fetchfield('max_order', 0, $result);
  362. DB()->sql_freeresult($result);
  363. //
  364. // Insert reason
  365. //
  366. $sql = 'INSERT INTO ' . BB_REPORTS_REASONS . ' (report_module_id, report_reason_order, report_reason_desc)
  367. VALUES(' . (int) $module_id . ', ' . ($max_order + 1) . ", '" . DB()->escape($reason_desc) . "')";
  368. if (!DB()->sql_query($sql))
  369. {
  370. message_die(GENERAL_ERROR, 'Could not insert report reason', '', __LINE__, __FILE__, $sql);
  371. }
  372. return DB()->sql_nextid();
  373. }
  374. //
  375. // Edits a report reason
  376. //
  377. function report_reason_edit($reason_id, $module_id, $reason_desc)
  378. {
  379. $sql = 'UPDATE ' . BB_REPORTS_REASONS . '
  380. SET
  381. report_module_id = ' . (int) $module_id . ",
  382. report_reason_desc = '" . DB()->escape($reason_desc) . "'
  383. WHERE report_reason_id = " . (int) $reason_id;
  384. if (!DB()->sql_query($sql))
  385. {
  386. message_die(GENERAL_ERROR, 'Could not update report reason', '', __LINE__, __FILE__, $sql);
  387. }
  388. }
  389. //
  390. // Moves a report reason to another position (up or down), reorders other report reasons
  391. //
  392. function report_reason_move($mode, $reason_id, $steps = 1)
  393. {
  394. //
  395. // Obtain report reason information
  396. //
  397. $sql = 'SELECT report_module_id, report_reason_order
  398. FROM ' . BB_REPORTS_REASONS . '
  399. WHERE report_reason_id = ' . (int) $reason_id;
  400. if (!$result = DB()->sql_query($sql))
  401. {
  402. message_die(GENERAL_ERROR, 'Could not obtain report reason order', '', __LINE__, __FILE__, $sql);
  403. }
  404. $row = DB()->sql_fetchrow($result);
  405. DB()->sql_freeresult($result);
  406. if (!$row)
  407. {
  408. return false;
  409. }
  410. switch ($mode)
  411. {
  412. case 'up':
  413. $sql = 'UPDATE ' . BB_REPORTS_REASONS . '
  414. SET report_reason_order = report_reason_order + 1
  415. WHERE report_module_id = ' . $row['report_module_id'] . '
  416. AND report_reason_order >= ' . ($row['report_reason_order'] - (int) $steps) . '
  417. AND report_reason_order < ' . $row['report_reason_order'];
  418. break;
  419. case 'down':
  420. $sql = 'UPDATE ' . BB_REPORTS_REASONS . '
  421. SET report_reason_order = report_reason_order - 1
  422. WHERE report_module_id = ' . $row['report_module_id'] . '
  423. AND report_reason_order <= ' . ($row['report_reason_order'] + (int) $steps) . '
  424. AND report_reason_order > ' . $row['report_reason_order'];
  425. break;
  426. default:
  427. return false;
  428. break;
  429. }
  430. if (!DB()->sql_query($sql))
  431. {
  432. message_die(GENERAL_ERROR, 'Could not update report reason order', '', __LINE__, __FILE__, $sql);
  433. }
  434. if (DB()->affected_rows())
  435. {
  436. $op = ($mode == 'up') ? '-' : '+';
  437. $sql = 'UPDATE ' . BB_REPORTS_REASONS . "
  438. SET report_reason_order = report_reason_order $op 1
  439. WHERE report_reason_id = " . (int) $reason_id;
  440. if (!DB()->sql_query($sql))
  441. {
  442. message_die(GENERAL_ERROR, 'Could not update report reason order', '', __LINE__, __FILE__, $sql);
  443. }
  444. }
  445. DB()->sql_query('');
  446. return true;
  447. }
  448. //
  449. // Deletes a report reason
  450. //
  451. function report_reason_delete($reason_id)
  452. {
  453. //
  454. // Obtain report reason information
  455. //
  456. $sql = 'SELECT report_module_id, report_reason_order
  457. FROM ' . BB_REPORTS_REASONS . '
  458. WHERE report_reason_id = ' . (int) $reason_id;
  459. if (!$result = DB()->sql_query($sql))
  460. {
  461. message_die(GENERAL_ERROR, 'Could not obtain report reason', '', __LINE__, __FILE__, $sql);
  462. }
  463. $row = DB()->sql_fetchrow($result);
  464. DB()->sql_freeresult($result);
  465. if (!$row)
  466. {
  467. return;
  468. }
  469. //
  470. // Update report reason order
  471. //
  472. $sql = 'UPDATE ' . BB_REPORTS_REASONS . '
  473. SET report_reason_order = report_reason_order - 1
  474. WHERE report_module_id = ' . $row['report_module_id'] . '
  475. AND report_reason_order > ' . $row['report_reason_order'];
  476. if (!DB()->sql_query($sql))
  477. {
  478. message_die(GENERAL_ERROR, 'Could not update report reason order', '', __LINE__, __FILE__, $sql);
  479. }
  480. //
  481. // Delete report reason
  482. //
  483. $sql = 'DELETE FROM ' . BB_REPORTS_REASONS . '
  484. WHERE report_reason_id = ' . (int) $reason_id;
  485. if (!DB()->sql_query($sql))
  486. {
  487. message_die(GENERAL_ERROR, 'Could not delete report reason', '', __LINE__, __FILE__, $sql);
  488. }
  489. }