PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/PNphpBB2/attach_mod/includes/functions_admin.php

https://gitlab.com/bulwye/reliquerunt
PHP | 550 lines | 415 code | 88 blank | 47 comment | 124 complexity | 5b3daba2235888692e69e93f528b4608 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package attachment_mod
  5. * @version $Id: functions_admin.php,v 1.4 2006/04/22 16:21:09 acydburn Exp $
  6. * @copyright (c) 2002 Meik Sievertsen
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * All Attachment Functions only needed in Admin
  12. */
  13. /**
  14. * Set/Change Quotas
  15. */
  16. function process_quota_settings($mode, $id, $quota_type, $quota_limit_id = 0)
  17. {
  18. global $db;
  19. $id = (int) $id;
  20. $quota_type = (int) $quota_type;
  21. $quota_limit_id = (int) $quota_limit_id;
  22. if ($mode == 'user')
  23. {
  24. if (!$quota_limit_id)
  25. {
  26. $sql = 'DELETE FROM ' . QUOTA_TABLE . "
  27. WHERE user_id = $id
  28. AND quota_type = $quota_type";
  29. }
  30. else
  31. {
  32. // Check if user is already entered
  33. $sql = 'SELECT user_id
  34. FROM ' . QUOTA_TABLE . "
  35. WHERE user_id = $id
  36. AND quota_type = $quota_type";
  37. if (!($result = $db->sql_query($sql)))
  38. {
  39. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  40. }
  41. if ($db->sql_numrows($result) == 0)
  42. {
  43. $sql_ary = array(
  44. 'user_id' => (int) $id,
  45. 'group_id' => 0,
  46. 'quota_type' => (int) $quota_type,
  47. 'quota_limit_id'=> (int) $quota_limit_id
  48. );
  49. $sql = 'INSERT INTO ' . QUOTA_TABLE . ' ' . attach_mod_sql_build_array('INSERT', $sql_ary);
  50. }
  51. else
  52. {
  53. $sql = 'UPDATE ' . QUOTA_TABLE . "
  54. SET quota_limit_id = $quota_limit_id
  55. WHERE user_id = $id
  56. AND quota_type = $quota_type";
  57. }
  58. $db->sql_freeresult($result);
  59. }
  60. if (!($result = $db->sql_query($sql)))
  61. {
  62. message_die(GENERAL_ERROR, 'Unable to update quota Settings', '', __LINE__, __FILE__, $sql);
  63. }
  64. }
  65. else if ($mode == 'group')
  66. {
  67. if (!$quota_limit_id)
  68. {
  69. $sql = 'DELETE FROM ' . QUOTA_TABLE . "
  70. WHERE group_id = $id
  71. AND quota_type = $quota_type";
  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. // Check if user is already entered
  80. $sql = 'SELECT group_id
  81. FROM ' . QUOTA_TABLE . "
  82. WHERE group_id = $id
  83. AND quota_type = $quota_type";
  84. if (!($result = $db->sql_query($sql)))
  85. {
  86. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  87. }
  88. if ($db->sql_numrows($result) == 0)
  89. {
  90. $sql = 'INSERT INTO ' . QUOTA_TABLE . " (user_id, group_id, quota_type, quota_limit_id)
  91. VALUES (0, $id, $quota_type, $quota_limit_id)";
  92. }
  93. else
  94. {
  95. $sql = 'UPDATE ' . QUOTA_TABLE . " SET quota_limit_id = $quota_limit_id
  96. WHERE group_id = $id AND quota_type = $quota_type";
  97. }
  98. if (!$db->sql_query($sql))
  99. {
  100. message_die(GENERAL_ERROR, 'Unable to update quota Settings', '', __LINE__, __FILE__, $sql);
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * sort multi-dimensional Array
  107. */
  108. function sort_multi_array ($sort_array, $key, $sort_order, $pre_string_sort = 0)
  109. {
  110. $last_element = sizeof($sort_array) - 1;
  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. for ($i = 0; $i < $last_element; $i++)
  120. {
  121. $num_iterations = $last_element - $i;
  122. for ($j = 0; $j < $num_iterations; $j++)
  123. {
  124. $next = 0;
  125. // do checks based on key
  126. $switch = false;
  127. if (!$string_sort)
  128. {
  129. 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])))
  130. {
  131. $switch = true;
  132. }
  133. }
  134. else
  135. {
  136. 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))
  137. {
  138. $switch = true;
  139. }
  140. }
  141. if ($switch)
  142. {
  143. $temp = $sort_array[$j];
  144. $sort_array[$j] = $sort_array[$j + 1];
  145. $sort_array[$j + 1] = $temp;
  146. }
  147. }
  148. }
  149. return $sort_array;
  150. }
  151. /**
  152. * See if a post or pm really exist
  153. */
  154. function entry_exists($attach_id)
  155. {
  156. global $db;
  157. $attach_id = (int) $attach_id;
  158. if (!$attach_id)
  159. {
  160. return false;
  161. }
  162. $sql = 'SELECT post_id, privmsgs_id
  163. FROM ' . ATTACHMENTS_TABLE . "
  164. WHERE attach_id = $attach_id";
  165. $result = $db->sql_query($sql);
  166. if (!$result)
  167. {
  168. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  169. }
  170. $ids = $db->sql_fetchrowset($result);
  171. $num_ids = $db->sql_numrows($result);
  172. $db->sql_freeresult($result);
  173. $exists = false;
  174. for ($i = 0; $i < $num_ids; $i++)
  175. {
  176. if (intval($ids[$i]['post_id']) != 0)
  177. {
  178. $sql = 'SELECT post_id
  179. FROM ' . POSTS_TABLE . '
  180. WHERE post_id = ' . intval($ids[$i]['post_id']);
  181. }
  182. else if (intval($ids[$i]['privmsgs_id']) != 0)
  183. {
  184. $sql = 'SELECT privmsgs_id
  185. FROM ' . PRIVMSGS_TABLE . '
  186. WHERE privmsgs_id = ' . intval($ids[$i]['privmsgs_id']);
  187. }
  188. $result = $db->sql_query($sql);
  189. if (!$result)
  190. {
  191. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  192. }
  193. $num_rows = $db->sql_numrows($result);
  194. $db->sql_freeresult($result);
  195. if ($num_rows > 0)
  196. {
  197. $exists = true;
  198. break;
  199. }
  200. }
  201. return $exists;
  202. }
  203. /**
  204. * Collect all Attachments in Filesystem
  205. */
  206. function collect_attachments()
  207. {
  208. global $upload_dir, $attach_config;
  209. $file_attachments = array();
  210. if (!intval($attach_config['allow_ftp_upload']))
  211. {
  212. if ($dir = @opendir($upload_dir))
  213. {
  214. while ($file = @readdir($dir))
  215. {
  216. if ($file != 'index.php' && $file != '.htaccess' && !is_dir($upload_dir . '/' . $file) && !is_link($upload_dir . '/' . $file))
  217. {
  218. $file_attachments[] = trim($file);
  219. }
  220. }
  221. closedir($dir);
  222. }
  223. else
  224. {
  225. 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.');
  226. }
  227. }
  228. else
  229. {
  230. $conn_id = attach_init_ftp();
  231. $file_listing = array();
  232. $file_listing = @ftp_rawlist($conn_id, '');
  233. if (!$file_listing)
  234. {
  235. message_die(GENERAL_ERROR, 'Unable to get Raw File Listing. Please be sure the LIST command is enabled at your FTP Server.');
  236. }
  237. for ($i = 0; $i < sizeof($file_listing); $i++)
  238. {
  239. if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file_listing[$i], $regs))
  240. {
  241. if ($regs[1] == 'd')
  242. {
  243. $dirinfo[0] = 1; // Directory == 1
  244. }
  245. $dirinfo[1] = $regs[2]; // Size
  246. $dirinfo[2] = $regs[3]; // Date
  247. $dirinfo[3] = $regs[4]; // Filename
  248. $dirinfo[4] = $regs[5]; // Time
  249. }
  250. if ($dirinfo[0] != 1 && $dirinfo[4] != 'index.php' && $dirinfo[4] != '.htaccess')
  251. {
  252. $file_attachments[] = trim($dirinfo[4]);
  253. }
  254. }
  255. @ftp_quit($conn_id);
  256. }
  257. return $file_attachments;
  258. }
  259. /**
  260. * Returns the filesize of the upload directory in human readable format
  261. */
  262. function get_formatted_dirsize()
  263. {
  264. global $attach_config, $upload_dir, $lang;
  265. $upload_dir_size = 0;
  266. if (!intval($attach_config['allow_ftp_upload']))
  267. {
  268. if ($dirname = @opendir($upload_dir))
  269. {
  270. while ($file = @readdir($dirname))
  271. {
  272. if ($file != 'index.php' && $file != '.htaccess' && !is_dir($upload_dir . '/' . $file) && !is_link($upload_dir . '/' . $file))
  273. {
  274. $upload_dir_size += @filesize($upload_dir . '/' . $file);
  275. }
  276. }
  277. @closedir($dirname);
  278. }
  279. else
  280. {
  281. $upload_dir_size = $lang['Not_available'];
  282. return $upload_dir_size;
  283. }
  284. }
  285. else
  286. {
  287. $conn_id = attach_init_ftp();
  288. $file_listing = array();
  289. $file_listing = @ftp_rawlist($conn_id, '');
  290. if (!$file_listing)
  291. {
  292. $upload_dir_size = $lang['Not_available'];
  293. return $upload_dir_size;
  294. }
  295. for ($i = 0; $i < count($file_listing); $i++)
  296. {
  297. if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file_listing[$i], $regs))
  298. {
  299. if ($regs[1] == 'd')
  300. {
  301. $dirinfo[0] = 1; // Directory == 1
  302. }
  303. $dirinfo[1] = $regs[2]; // Size
  304. $dirinfo[2] = $regs[3]; // Date
  305. $dirinfo[3] = $regs[4]; // Filename
  306. $dirinfo[4] = $regs[5]; // Time
  307. }
  308. if ($dirinfo[0] != 1 && $dirinfo[4] != 'index.php' && $dirinfo[4] != '.htaccess')
  309. {
  310. $upload_dir_size += $dirinfo[1];
  311. }
  312. }
  313. @ftp_quit($conn_id);
  314. }
  315. if ($upload_dir_size >= 1048576)
  316. {
  317. $upload_dir_size = round($upload_dir_size / 1048576 * 100) / 100 . ' ' . $lang['MB'];
  318. }
  319. else if ($upload_dir_size >= 1024)
  320. {
  321. $upload_dir_size = round($upload_dir_size / 1024 * 100) / 100 . ' ' . $lang['KB'];
  322. }
  323. else
  324. {
  325. $upload_dir_size = $upload_dir_size . ' ' . $lang['Bytes'];
  326. }
  327. return $upload_dir_size;
  328. }
  329. /*
  330. * Build SQL-Statement for the search feature
  331. */
  332. function search_attachments($order_by, &$total_rows)
  333. {
  334. global $db, $HTTP_POST_VARS, $HTTP_GET_VARS, $lang;
  335. $where_sql = array();
  336. // Get submitted Vars
  337. $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');
  338. for ($i = 0; $i < sizeof($search_vars); $i++)
  339. {
  340. $$search_vars[$i] = get_var($search_vars[$i], '');
  341. }
  342. // Author name search
  343. if ($search_author != '')
  344. {
  345. // Bring in line with 2.0.x expected username
  346. $search_author = addslashes(html_entity_decode($search_author));
  347. $search_author = stripslashes(phpbb_clean_username($search_author));
  348. // Prepare for directly going into sql query
  349. $search_author = str_replace('*', '%', attach_mod_sql_escape($search_author));
  350. // We need the post_id's, because we want to query the Attachment Table
  351. $sql = 'SELECT user_id
  352. FROM ' . USERS_TABLE . "
  353. WHERE username LIKE '$search_author'";
  354. if (!($result = $db->sql_query($sql)))
  355. {
  356. message_die(GENERAL_ERROR, 'Couldn\'t obtain list of matching users (searching for: ' . $search_author . ')', '', __LINE__, __FILE__, $sql);
  357. }
  358. $matching_userids = '';
  359. if ($row = $db->sql_fetchrow($result))
  360. {
  361. do
  362. {
  363. $matching_userids .= (($matching_userids != '') ? ', ' : '') . intval($row['user_id']);
  364. }
  365. while ($row = $db->sql_fetchrow($result));
  366. $db->sql_freeresult($result);
  367. }
  368. else
  369. {
  370. message_die(GENERAL_MESSAGE, $lang['No_attach_search_match']);
  371. }
  372. $where_sql[] = ' (t.user_id_1 IN (' . $matching_userids . ')) ';
  373. }
  374. // Search Keyword
  375. if ($search_keyword_fname != '')
  376. {
  377. $match_word = str_replace('*', '%', $search_keyword_fname);
  378. $where_sql[] = " (a.real_filename LIKE '" . attach_mod_sql_escape($match_word) . "') ";
  379. }
  380. if ($search_keyword_comment != '')
  381. {
  382. $match_word = str_replace('*', '%', $search_keyword_comment);
  383. $where_sql[] = " (a.comment LIKE '" . attach_mod_sql_escape($match_word) . "') ";
  384. }
  385. // Search Download Count
  386. if ($search_count_smaller != '' || $search_count_greater != '')
  387. {
  388. if ($search_count_smaller != '')
  389. {
  390. $where_sql[] = ' (a.download_count < ' . (int) $search_count_smaller . ') ';
  391. }
  392. else if ($search_count_greater != '')
  393. {
  394. $where_sql[] = ' (a.download_count > ' . (int) $search_count_greater . ') ';
  395. }
  396. }
  397. // Search Filesize
  398. if ($search_size_smaller != '' || $search_size_greater != '')
  399. {
  400. if ($search_size_smaller != '')
  401. {
  402. $where_sql[] = ' (a.filesize < ' . (int) $search_size_smaller . ') ';
  403. }
  404. else if ($search_size_greater != '')
  405. {
  406. $where_sql[] = ' (a.filesize > ' . (int) $search_size_greater . ') ';
  407. }
  408. }
  409. // Search Attachment Time
  410. if ($search_days_greater != '')
  411. {
  412. $where_sql[] = ' (a.filetime < ' . ( time() - ((int) $search_days_greater * 86400)) . ') ';
  413. }
  414. // Search Forum
  415. if ($search_forum)
  416. {
  417. $where_sql[] = ' (p.forum_id = ' . intval($search_forum) . ') ';
  418. }
  419. // Search Cat... nope... sorry :(
  420. $sql = 'SELECT a.*, t.post_id, p.post_time, p.topic_id
  421. FROM ' . ATTACHMENTS_TABLE . ' t, ' . ATTACHMENTS_DESC_TABLE . ' a, ' . POSTS_TABLE . ' p WHERE ';
  422. if (sizeof($where_sql) > 0)
  423. {
  424. $sql .= implode('AND', $where_sql) . ' AND ';
  425. }
  426. $sql .= 't.post_id = p.post_id AND a.attach_id = t.attach_id ';
  427. $total_rows_sql = $sql;
  428. $sql .= $order_by;
  429. if (!($result = $db->sql_query($sql)))
  430. {
  431. message_die(GENERAL_ERROR, 'Couldn\'t query attachments', '', __LINE__, __FILE__, $sql);
  432. }
  433. $attachments = $db->sql_fetchrowset($result);
  434. $num_attach = $db->sql_numrows($result);
  435. $db->sql_freeresult($result);
  436. if ($num_attach == 0)
  437. {
  438. message_die(GENERAL_MESSAGE, $lang['No_attach_search_match']);
  439. }
  440. if (!($result = $db->sql_query($total_rows_sql)))
  441. {
  442. message_die(GENERAL_ERROR, 'Could not query attachments', '', __LINE__, __FILE__, $sql);
  443. }
  444. $total_rows = $db->sql_numrows($result);
  445. $db->sql_freeresult($result);
  446. return $attachments;
  447. }
  448. /**
  449. * perform LIMIT statement on arrays
  450. */
  451. function limit_array($array, $start, $pagelimit)
  452. {
  453. // array from start - start+pagelimit
  454. $limit = (sizeof($array) < ($start + $pagelimit)) ? sizeof($array) : $start + $pagelimit;
  455. $limit_array = array();
  456. for ($i = $start; $i < $limit; $i++)
  457. {
  458. $limit_array[] = $array[$i];
  459. }
  460. return $limit_array;
  461. }
  462. ?>