Просмотр файла attach_mod/includes/functions_admin.php

Размер файла: 11.96Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. if ( !defined('IN_PHPBB') )
  7. {
  8. die("ERROR!!! THIS FILE PROTECTED. IF YOU SAW THIS REPORT, MEANS HACKERS HERE IS NOTHING TO DO ");
  9. }
  10.  
  11. function process_quota_settings($mode, $id, $quota_type, $quota_limit_id = 0)
  12. {
  13. global $db;
  14.  
  15. $id = (int) $id;
  16. $quota_type = (int) $quota_type;
  17. $quota_limit_id = (int) $quota_limit_id;
  18.  
  19. if ($mode == 'user')
  20. {
  21. if (!$quota_limit_id)
  22. {
  23. $sql = 'DELETE FROM ' . QUOTA_TABLE . "
  24. WHERE user_id = $id
  25. AND quota_type = $quota_type";
  26. }
  27. else
  28. {
  29. $sql = 'SELECT user_id
  30. FROM ' . QUOTA_TABLE . "
  31. WHERE user_id = $id
  32. AND quota_type = $quota_type";
  33.  
  34. if (!($result = $db->sql_query($sql)))
  35. {
  36. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  37. }
  38.  
  39. if ($db->sql_numrows($result) == 0)
  40. {
  41. $sql_ary = array(
  42. 'user_id' => (int) $id,
  43. 'group_id' => 0,
  44. 'quota_type' => (int) $quota_type,
  45. 'quota_limit_id'=> (int) $quota_limit_id
  46. );
  47.  
  48. $sql = 'INSERT INTO ' . QUOTA_TABLE . ' ' . attach_mod_sql_build_array('INSERT', $sql_ary);
  49. }
  50. else
  51. {
  52. $sql = 'UPDATE ' . QUOTA_TABLE . "
  53. SET quota_limit_id = $quota_limit_id
  54. WHERE user_id = $id
  55. AND quota_type = $quota_type";
  56. }
  57. $db->sql_freeresult($result);
  58. }
  59. if (!($result = $db->sql_query($sql)))
  60. {
  61. message_die(GENERAL_ERROR, 'Unable to update quota Settings', '', __LINE__, __FILE__, $sql);
  62. }
  63. }
  64. else if ($mode == 'group')
  65. {
  66. if (!$quota_limit_id)
  67. {
  68. $sql = 'DELETE FROM ' . QUOTA_TABLE . "
  69. WHERE group_id = $id
  70. AND quota_type = $quota_type";
  71.  
  72. if (!($result = $db->sql_query($sql)))
  73. {
  74. message_die(GENERAL_ERROR, 'Unable to delete quota Settings', '', __LINE__, __FILE__, $sql);
  75. }
  76. }
  77. else
  78. {
  79. $sql = 'SELECT group_id
  80. FROM ' . QUOTA_TABLE . "
  81. WHERE group_id = $id
  82. AND quota_type = $quota_type";
  83.  
  84. if (!($result = $db->sql_query($sql)))
  85. {
  86. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  87. }
  88.  
  89. if ($db->sql_numrows($result) == 0)
  90. {
  91. $sql = 'INSERT INTO ' . QUOTA_TABLE . " (user_id, group_id, quota_type, quota_limit_id)
  92. VALUES (0, $id, $quota_type, $quota_limit_id)";
  93. }
  94. else
  95. {
  96. $sql = 'UPDATE ' . QUOTA_TABLE . " SET quota_limit_id = $quota_limit_id
  97. WHERE group_id = $id AND quota_type = $quota_type";
  98. }
  99. if (!$db->sql_query($sql))
  100. {
  101. message_die(GENERAL_ERROR, 'Unable to update quota Settings', '', __LINE__, __FILE__, $sql);
  102. }
  103. }
  104. }
  105. }
  106.  
  107. function sort_multi_array ($sort_array, $key, $sort_order, $pre_string_sort = 0)
  108. {
  109. $last_element = sizeof($sort_array) - 1;
  110.  
  111. if (!$pre_string_sort)
  112. {
  113. $string_sort = (!is_numeric($sort_array[$last_element-1][$key]) ) ? true : false;
  114. }
  115. else
  116. {
  117. $string_sort = $pre_string_sort;
  118. }
  119.  
  120. for ($i = 0; $i < $last_element; $i++)
  121. {
  122. $num_iterations = $last_element - $i;
  123.  
  124. for ($j = 0; $j < $num_iterations; $j++)
  125. {
  126. $next = 0;
  127. $switch = false;
  128. if (!$string_sort)
  129. {
  130. if (($sort_order == 'DESC' && intval($sort_array[$j][$key]) < intval($sort_array[$j + 1][$key])) || ($sort_order == 'ASC' && intval($sort_array[$j][$key]) > intval($sort_array[$j + 1][$key])))
  131. {
  132. $switch = true;
  133. }
  134. }
  135. else
  136. {
  137. if (($sort_order == 'DESC' && strcasecmp($sort_array[$j][$key], $sort_array[$j + 1][$key]) < 0) || ($sort_order == 'ASC' && strcasecmp($sort_array[$j][$key], $sort_array[$j + 1][$key]) > 0))
  138. {
  139. $switch = true;
  140. }
  141. }
  142.  
  143. if ($switch)
  144. {
  145. $temp = $sort_array[$j];
  146. $sort_array[$j] = $sort_array[$j + 1];
  147. $sort_array[$j + 1] = $temp;
  148. }
  149. }
  150. }
  151.  
  152. return $sort_array;
  153. }
  154.  
  155. function entry_exists($attach_id)
  156. {
  157. global $db;
  158.  
  159. $attach_id = (int) $attach_id;
  160.  
  161. if (!$attach_id)
  162. {
  163. return false;
  164. }
  165. $sql = 'SELECT post_id, privmsgs_id
  166. FROM ' . ATTACHMENTS_TABLE . "
  167. WHERE attach_id = $attach_id";
  168. $result = $db->sql_query($sql);
  169.  
  170. if (!$result)
  171. {
  172. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  173. }
  174.  
  175. $ids = $db->sql_fetchrowset($result);
  176. $num_ids = $db->sql_numrows($result);
  177. $db->sql_freeresult($result);
  178.  
  179. $exists = false;
  180. for ($i = 0; $i < $num_ids; $i++)
  181. {
  182. if (intval($ids[$i]['post_id']) != 0)
  183. {
  184. $sql = 'SELECT post_id
  185. FROM ' . POSTS_TABLE . '
  186. WHERE post_id = ' . intval($ids[$i]['post_id']);
  187. }
  188. else if (intval($ids[$i]['privmsgs_id']) != 0)
  189. {
  190. $sql = 'SELECT privmsgs_id
  191. FROM ' . PRIVMSGS_TABLE . '
  192. WHERE privmsgs_id = ' . intval($ids[$i]['privmsgs_id']);
  193. }
  194. $result = $db->sql_query($sql);
  195.  
  196. if (!$result)
  197. {
  198. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  199. }
  200. $num_rows = $db->sql_numrows($result);
  201. $db->sql_freeresult($result);
  202.  
  203. if ($num_rows > 0)
  204. {
  205. $exists = true;
  206. break;
  207. }
  208. }
  209.  
  210. return $exists;
  211. }
  212.  
  213. function collect_attachments()
  214. {
  215. global $upload_dir, $attach_config;
  216.  
  217. $file_attachments = array();
  218.  
  219. if (!intval($attach_config['allow_ftp_upload']))
  220. {
  221. if ($dir = @opendir($upload_dir))
  222. {
  223. while ($file = @readdir($dir))
  224. {
  225. if ($file != 'index.php' && $file != '.htaccess' && !is_dir($upload_dir . '/' . $file) && !is_link($upload_dir . '/' . $file))
  226. {
  227. $file_attachments[] = trim($file);
  228. }
  229. }
  230. closedir($dir);
  231. }
  232. else
  233. {
  234. message_die(GENERAL_ERROR, 'Is Safe Mode Restriction in effect? The Attachment Mod seems to be unable to collect the Attachments within the upload Directory. Try to use FTP Upload to circumvent this error. Another reason could be that the directory ' . $upload_dir . ' does not exist.');
  235. }
  236. }
  237. else
  238. {
  239. $conn_id = attach_init_ftp();
  240.  
  241. $file_listing = array();
  242.  
  243. $file_listing = @ftp_rawlist($conn_id, '');
  244.  
  245. if (!$file_listing)
  246. {
  247. message_die(GENERAL_ERROR, 'Unable to get Raw File Listing. Please be sure the LIST command is enabled at your FTP Server.');
  248. }
  249.  
  250. for ($i = 0; $i < sizeof($file_listing); $i++)
  251. {
  252. if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file_listing[$i], $regs))
  253. {
  254. if ($regs[1] == 'd')
  255. {
  256. $dirinfo[0] = 1;
  257. }
  258. $dirinfo[1] = $regs[2];
  259. $dirinfo[2] = $regs[3];
  260. $dirinfo[3] = $regs[4];
  261. $dirinfo[4] = $regs[5];
  262. }
  263. if ($dirinfo[0] != 1 && $dirinfo[4] != 'index.php' && $dirinfo[4] != '.htaccess')
  264. {
  265. $file_attachments[] = trim($dirinfo[4]);
  266. }
  267. }
  268.  
  269. @ftp_quit($conn_id);
  270. }
  271.  
  272. return $file_attachments;
  273. }
  274.  
  275. function get_formatted_dirsize()
  276. {
  277. global $attach_config, $upload_dir, $lang;
  278.  
  279. $upload_dir_size = 0;
  280.  
  281. if (!intval($attach_config['allow_ftp_upload']))
  282. {
  283. if ($dirname = @opendir($upload_dir))
  284. {
  285. while ($file = @readdir($dirname))
  286. {
  287. if ($file != 'index.php' && $file != '.htaccess' && !is_dir($upload_dir . '/' . $file) && !is_link($upload_dir . '/' . $file))
  288. {
  289. $upload_dir_size += @filesize($upload_dir . '/' . $file);
  290. }
  291. }
  292. @closedir($dirname);
  293. }
  294. else
  295. {
  296. $upload_dir_size = $lang['Not_available'];
  297. return $upload_dir_size;
  298. }
  299. }
  300. else
  301. {
  302. $conn_id = attach_init_ftp();
  303.  
  304. $file_listing = array();
  305.  
  306. $file_listing = @ftp_rawlist($conn_id, '');
  307.  
  308. if (!$file_listing)
  309. {
  310. $upload_dir_size = $lang['Not_available'];
  311. return $upload_dir_size;
  312. }
  313.  
  314. for ($i = 0; $i < count($file_listing); $i++)
  315. {
  316. if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file_listing[$i], $regs))
  317. {
  318. if ($regs[1] == 'd')
  319. {
  320. $dirinfo[0] = 1;
  321. }
  322. $dirinfo[1] = $regs[2];
  323. $dirinfo[2] = $regs[3];
  324. $dirinfo[3] = $regs[4];
  325. $dirinfo[4] = $regs[5];
  326. }
  327. if ($dirinfo[0] != 1 && $dirinfo[4] != 'index.php' && $dirinfo[4] != '.htaccess')
  328. {
  329. $upload_dir_size += $dirinfo[1];
  330. }
  331. }
  332.  
  333. @ftp_quit($conn_id);
  334. }
  335.  
  336. if ($upload_dir_size >= 1048576)
  337. {
  338. $upload_dir_size = round($upload_dir_size / 1048576 * 100) / 100 . ' ' . $lang['MB'];
  339. }
  340. else if ($upload_dir_size >= 1024)
  341. {
  342. $upload_dir_size = round($upload_dir_size / 1024 * 100) / 100 . ' ' . $lang['KB'];
  343. }
  344. else
  345. {
  346. $upload_dir_size = $upload_dir_size . ' ' . $lang['Bytes'];
  347. }
  348.  
  349. return $upload_dir_size;
  350. }
  351.  
  352. function search_attachments($order_by, &$total_rows)
  353. {
  354. global $db, $HTTP_POST_VARS, $HTTP_GET_VARS, $lang;
  355. $where_sql = array();
  356.  
  357. $search_vars = array('search_keyword_fname', 'search_keyword_comment', 'search_author', 'search_size_smaller', 'search_size_greater', 'search_count_smaller', 'search_count_greater', 'search_days_greater', 'search_forum', 'search_cat');
  358. for ($i = 0; $i < sizeof($search_vars); $i++)
  359. {
  360. $$search_vars[$i] = get_var($search_vars[$i], '');
  361. }
  362.  
  363. if ($search_author != '')
  364. {
  365. $search_author = addslashes(html_entity_decode($search_author));
  366. $search_author = stripslashes(phpbb_clean_username($search_author));
  367. $search_author = str_replace('*', '%', attach_mod_sql_escape($search_author));
  368.  
  369. $sql = 'SELECT user_id
  370. FROM ' . USERS_TABLE . "
  371. WHERE username LIKE '$search_author'";
  372.  
  373. if (!($result = $db->sql_query($sql)))
  374. {
  375. message_die(GENERAL_ERROR, 'Couldn\'t obtain list of matching users (searching for: ' . $search_author . ')', '', __LINE__, __FILE__, $sql);
  376. }
  377.  
  378. $matching_userids = '';
  379. if ($row = $db->sql_fetchrow($result))
  380. {
  381. do
  382. {
  383. $matching_userids .= (($matching_userids != '') ? ', ' : '') . intval($row['user_id']);
  384. }
  385. while ($row = $db->sql_fetchrow($result));
  386. $db->sql_freeresult($result);
  387. }
  388. else
  389. {
  390. message_die(GENERAL_MESSAGE, $lang['No_attach_search_match']);
  391. }
  392.  
  393. $where_sql[] = ' (t.user_id_1 IN (' . $matching_userids . ')) ';
  394. }
  395.  
  396. if ($search_keyword_fname != '')
  397. {
  398. $match_word = str_replace('*', '%', $search_keyword_fname);
  399. $where_sql[] = " (a.real_filename LIKE '" . attach_mod_sql_escape($match_word) . "') ";
  400. }
  401.  
  402. if ($search_keyword_comment != '')
  403. {
  404. $match_word = str_replace('*', '%', $search_keyword_comment);
  405. $where_sql[] = " (a.comment LIKE '" . attach_mod_sql_escape($match_word) . "') ";
  406. }
  407.  
  408. if ($search_count_smaller != '' || $search_count_greater != '')
  409. {
  410. if ($search_count_smaller != '')
  411. {
  412. $where_sql[] = ' (a.download_count < ' . (int) $search_count_smaller . ') ';
  413. }
  414. else if ($search_count_greater != '')
  415. {
  416. $where_sql[] = ' (a.download_count > ' . (int) $search_count_greater . ') ';
  417. }
  418. }
  419.  
  420. if ($search_size_smaller != '' || $search_size_greater != '')
  421. {
  422. if ($search_size_smaller != '')
  423. {
  424. $where_sql[] = ' (a.filesize < ' . (int) $search_size_smaller . ') ';
  425. }
  426. else if ($search_size_greater != '')
  427. {
  428. $where_sql[] = ' (a.filesize > ' . (int) $search_size_greater . ') ';
  429. }
  430. }
  431.  
  432. if ($search_days_greater != '')
  433. {
  434. $where_sql[] = ' (a.filetime < ' . ( time() - ((int) $search_days_greater * 86400)) . ') ';
  435. }
  436.  
  437. if ($search_forum)
  438. {
  439. $where_sql[] = ' (p.forum_id = ' . intval($search_forum) . ') ';
  440. }
  441.  
  442. $sql = 'SELECT a.*, t.post_id, p.post_time, p.topic_id
  443. FROM ' . ATTACHMENTS_TABLE . ' t, ' . ATTACHMENTS_DESC_TABLE . ' a, ' . POSTS_TABLE . ' p WHERE ';
  444. if (sizeof($where_sql) > 0)
  445. {
  446. $sql .= implode('AND', $where_sql) . ' AND ';
  447. }
  448.  
  449. $sql .= 't.post_id = p.post_id AND a.attach_id = t.attach_id ';
  450. $total_rows_sql = $sql;
  451.  
  452. $sql .= $order_by;
  453.  
  454. if (!($result = $db->sql_query($sql)))
  455. {
  456. message_die(GENERAL_ERROR, 'Couldn\'t query attachments', '', __LINE__, __FILE__, $sql);
  457. }
  458.  
  459. $attachments = $db->sql_fetchrowset($result);
  460. $num_attach = $db->sql_numrows($result);
  461. $db->sql_freeresult($result);
  462.  
  463. if ($num_attach == 0)
  464. {
  465. message_die(GENERAL_MESSAGE, $lang['No_attach_search_match']);
  466. }
  467.  
  468. if (!($result = $db->sql_query($total_rows_sql)))
  469. {
  470. message_die(GENERAL_ERROR, 'Could not query attachments', '', __LINE__, __FILE__, $sql);
  471. }
  472.  
  473. $total_rows = $db->sql_numrows($result);
  474. $db->sql_freeresult($result);
  475.  
  476. return $attachments;
  477. }
  478.  
  479. function limit_array($array, $start, $pagelimit)
  480. {
  481. $limit = (sizeof($array) < ($start + $pagelimit)) ? sizeof($array) : $start + $pagelimit;
  482.  
  483. $limit_array = array();
  484.  
  485. for ($i = $start; $i < $limit; $i++)
  486. {
  487. $limit_array[] = $array[$i];
  488. }
  489.  
  490. return $limit_array;
  491. }
  492.  
  493. ?>