PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/attach_mod/includes/functions_admin.php

http://torrentpier2.googlecode.com/
PHP | 394 lines | 299 code | 62 blank | 33 comment | 91 complexity | 460ac08be4c0363881fd4df567dabd55 MD5 | raw file
  1. <?php
  2. /**
  3. * All Attachment Functions only needed in Admin
  4. */
  5. /**
  6. * Set/Change Quotas
  7. */
  8. function process_quota_settings($mode, $id, $quota_type, $quota_limit_id = 0)
  9. {
  10. $id = (int) $id;
  11. $quota_type = (int) $quota_type;
  12. $quota_limit_id = (int) $quota_limit_id;
  13. if ($mode == 'user')
  14. {
  15. if (!$quota_limit_id)
  16. {
  17. $sql = 'DELETE FROM ' . BB_QUOTA . "
  18. WHERE user_id = $id
  19. AND quota_type = $quota_type";
  20. }
  21. else
  22. {
  23. // Check if user is already entered
  24. $sql = 'SELECT user_id
  25. FROM ' . BB_QUOTA . "
  26. WHERE user_id = $id
  27. AND quota_type = $quota_type";
  28. if( !($result = DB()->sql_query($sql)) )
  29. {
  30. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  31. }
  32. if (DB()->num_rows($result) == 0)
  33. {
  34. $sql_ary = array(
  35. 'user_id' => (int) $id,
  36. 'group_id' => 0,
  37. 'quota_type' => (int) $quota_type,
  38. 'quota_limit_id'=> (int) $quota_limit_id
  39. );
  40. $sql = 'INSERT INTO ' . BB_QUOTA . ' ' . attach_mod_sql_build_array('INSERT', $sql_ary);
  41. }
  42. else
  43. {
  44. $sql = 'UPDATE ' . BB_QUOTA . "
  45. SET quota_limit_id = $quota_limit_id
  46. WHERE user_id = $id
  47. AND quota_type = $quota_type";
  48. }
  49. DB()->sql_freeresult($result);
  50. }
  51. if (!($result = DB()->sql_query($sql)))
  52. {
  53. message_die(GENERAL_ERROR, 'Unable to update quota Settings', '', __LINE__, __FILE__, $sql);
  54. }
  55. }
  56. else if ($mode == 'group')
  57. {
  58. if (!$quota_limit_id)
  59. {
  60. $sql = 'DELETE FROM ' . BB_QUOTA . "
  61. WHERE group_id = $id
  62. AND quota_type = $quota_type";
  63. if( !($result = DB()->sql_query($sql)) )
  64. {
  65. message_die(GENERAL_ERROR, 'Unable to delete quota Settings', '', __LINE__, __FILE__, $sql);
  66. }
  67. }
  68. else
  69. {
  70. // Check if user is already entered
  71. $sql = 'SELECT group_id
  72. FROM ' . BB_QUOTA . "
  73. WHERE group_id = $id
  74. AND quota_type = $quota_type";
  75. if( !($result = DB()->sql_query($sql)) )
  76. {
  77. message_die(GENERAL_ERROR, 'Could not get Entry', '', __LINE__, __FILE__, $sql);
  78. }
  79. if (DB()->num_rows($result) == 0)
  80. {
  81. $sql = 'INSERT INTO ' . BB_QUOTA . " (user_id, group_id, quota_type, quota_limit_id)
  82. VALUES (0, $id, $quota_type, $quota_limit_id)";
  83. }
  84. else
  85. {
  86. $sql = 'UPDATE ' . BB_QUOTA . " SET quota_limit_id = $quota_limit_id
  87. WHERE group_id = $id AND quota_type = $quota_type";
  88. }
  89. if (!DB()->sql_query($sql))
  90. {
  91. message_die(GENERAL_ERROR, 'Unable to update quota Settings', '', __LINE__, __FILE__, $sql);
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * sort multi-dimensional Array
  98. */
  99. function sort_multi_array ($sort_array, $key, $sort_order, $pre_string_sort = 0)
  100. {
  101. $last_element = sizeof($sort_array) - 1;
  102. if (!$pre_string_sort)
  103. {
  104. $string_sort = (!is_numeric(@$sort_array[$last_element-1][$key]) ) ? true : false;
  105. }
  106. else
  107. {
  108. $string_sort = $pre_string_sort;
  109. }
  110. for ($i = 0; $i < $last_element; $i++)
  111. {
  112. $num_iterations = $last_element - $i;
  113. for ($j = 0; $j < $num_iterations; $j++)
  114. {
  115. $next = 0;
  116. // do checks based on key
  117. $switch = false;
  118. if (!$string_sort)
  119. {
  120. 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])))
  121. {
  122. $switch = true;
  123. }
  124. }
  125. else
  126. {
  127. 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))
  128. {
  129. $switch = true;
  130. }
  131. }
  132. if ($switch)
  133. {
  134. $temp = $sort_array[$j];
  135. $sort_array[$j] = $sort_array[$j + 1];
  136. $sort_array[$j + 1] = $temp;
  137. }
  138. }
  139. }
  140. return $sort_array;
  141. }
  142. /**
  143. * Returns the filesize of the upload directory in human readable format
  144. */
  145. function get_formatted_dirsize()
  146. {
  147. global $attach_config, $upload_dir, $lang;
  148. $upload_dir_size = 0;
  149. if (!intval($attach_config['allow_ftp_upload']))
  150. {
  151. if ($dirname = @opendir($upload_dir))
  152. {
  153. while ($file = @readdir($dirname))
  154. {
  155. if ($file != 'index.php' && $file != '.htaccess' && !is_dir($upload_dir . '/' . $file) && !is_link($upload_dir . '/' . $file))
  156. {
  157. $upload_dir_size += @filesize($upload_dir . '/' . $file);
  158. }
  159. }
  160. @closedir($dirname);
  161. }
  162. else
  163. {
  164. $upload_dir_size = $lang['NOT_AVAILABLE'];
  165. return $upload_dir_size;
  166. }
  167. }
  168. else
  169. {
  170. $conn_id = attach_init_ftp();
  171. $file_listing = array();
  172. $file_listing = @ftp_rawlist($conn_id, '');
  173. if (!$file_listing)
  174. {
  175. $upload_dir_size = $lang['NOT_AVAILABLE'];
  176. return $upload_dir_size;
  177. }
  178. for ($i = 0; $i < count($file_listing); $i++)
  179. {
  180. if (preg_match("/([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)/", $file_listing[$i], $regs))
  181. {
  182. if ($regs[1] == 'd')
  183. {
  184. $dirinfo[0] = 1; // Directory == 1
  185. }
  186. $dirinfo[1] = $regs[2]; // Size
  187. $dirinfo[2] = $regs[3]; // Date
  188. $dirinfo[3] = $regs[4]; // Filename
  189. $dirinfo[4] = $regs[5]; // Time
  190. }
  191. if ($dirinfo[0] != 1 && $dirinfo[4] != 'index.php' && $dirinfo[4] != '.htaccess')
  192. {
  193. $upload_dir_size += $dirinfo[1];
  194. }
  195. }
  196. @ftp_quit($conn_id);
  197. }
  198. return humn_size($upload_dir_size);
  199. }
  200. /*
  201. * Build SQL-Statement for the search feature
  202. */
  203. function search_attachments($order_by, &$total_rows)
  204. {
  205. global $lang;
  206. $where_sql = array();
  207. // Get submitted Vars
  208. $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');
  209. for ($i = 0; $i < sizeof($search_vars); $i++)
  210. {
  211. $$search_vars[$i] = get_var($search_vars[$i], '');
  212. }
  213. // Author name search
  214. if ($search_author != '')
  215. {
  216. // Bring in line with 2.0.x expected username
  217. $search_author = addslashes(html_entity_decode($search_author));
  218. $search_author = stripslashes(clean_username($search_author));
  219. // Prepare for directly going into sql query
  220. $search_author = str_replace('*', '%', attach_mod_sql_escape($search_author));
  221. // We need the post_id's, because we want to query the Attachment Table
  222. $sql = 'SELECT user_id
  223. FROM ' . BB_USERS . "
  224. WHERE username LIKE '$search_author'";
  225. if (!($result = DB()->sql_query($sql)))
  226. {
  227. message_die(GENERAL_ERROR, 'Couldn\'t obtain list of matching users (searching for: ' . $search_author . ')', '', __LINE__, __FILE__, $sql);
  228. }
  229. $matching_userids = '';
  230. if ( $row = DB()->sql_fetchrow($result) )
  231. {
  232. do
  233. {
  234. $matching_userids .= (($matching_userids != '') ? ', ' : '') . intval($row['user_id']);
  235. }
  236. while ($row = DB()->sql_fetchrow($result));
  237. DB()->sql_freeresult($result);
  238. }
  239. else
  240. {
  241. message_die(GENERAL_MESSAGE, $lang['NO_ATTACH_SEARCH_MATCH']);
  242. }
  243. $where_sql[] = ' (t.user_id_1 IN (' . $matching_userids . ')) ';
  244. }
  245. // Search Keyword
  246. if ($search_keyword_fname != '')
  247. {
  248. $match_word = str_replace('*', '%', $search_keyword_fname);
  249. $where_sql[] = " (a.real_filename LIKE '" . attach_mod_sql_escape($match_word) . "') ";
  250. }
  251. if ($search_keyword_comment != '')
  252. {
  253. $match_word = str_replace('*', '%', $search_keyword_comment);
  254. $where_sql[] = " (a.comment LIKE '" . attach_mod_sql_escape($match_word) . "') ";
  255. }
  256. // Search Download Count
  257. if ($search_count_smaller != '' || $search_count_greater != '')
  258. {
  259. if ($search_count_smaller != '')
  260. {
  261. $where_sql[] = ' (a.download_count < ' . (int) $search_count_smaller . ') ';
  262. }
  263. else if ($search_count_greater != '')
  264. {
  265. $where_sql[] = ' (a.download_count > ' . (int) $search_count_greater . ') ';
  266. }
  267. }
  268. // Search Filesize
  269. if ($search_size_smaller != '' || $search_size_greater != '')
  270. {
  271. if ($search_size_smaller != '')
  272. {
  273. $where_sql[] = ' (a.filesize < ' . (int) $search_size_smaller . ') ';
  274. }
  275. else if ($search_size_greater != '')
  276. {
  277. $where_sql[] = ' (a.filesize > ' . (int) $search_size_greater . ') ';
  278. }
  279. }
  280. // Search Attachment Time
  281. if ($search_days_greater != '')
  282. {
  283. $where_sql[] = ' (a.filetime < ' . ( TIMENOW - ((int) $search_days_greater * 86400)) . ') ';
  284. }
  285. // Search Forum
  286. if ($search_forum)
  287. {
  288. $where_sql[] = ' (p.forum_id = ' . intval($search_forum) . ') ';
  289. }
  290. // Search Cat... nope... sorry :(
  291. $sql = 'SELECT a.*, t.post_id, p.post_time, p.topic_id
  292. FROM ' . BB_ATTACHMENTS . ' t, ' . BB_ATTACHMENTS_DESC . ' a, ' . BB_POSTS . ' p WHERE ';
  293. if (sizeof($where_sql) > 0)
  294. {
  295. $sql .= implode('AND', $where_sql) . ' AND ';
  296. }
  297. $sql .= 't.post_id = p.post_id AND a.attach_id = t.attach_id ';
  298. $total_rows_sql = $sql;
  299. $sql .= $order_by;
  300. if (!($result = DB()->sql_query($sql)))
  301. {
  302. message_die(GENERAL_ERROR, 'Couldn\'t query attachments', '', __LINE__, __FILE__, $sql);
  303. }
  304. $attachments = DB()->sql_fetchrowset($result);
  305. $num_attach = DB()->num_rows($result);
  306. DB()->sql_freeresult($result);
  307. if ($num_attach == 0)
  308. {
  309. message_die(GENERAL_MESSAGE, $lang['NO_ATTACH_SEARCH_MATCH']);
  310. }
  311. if (!($result = DB()->sql_query($total_rows_sql)))
  312. {
  313. message_die(GENERAL_ERROR, 'Could not query attachments', '', __LINE__, __FILE__, $sql);
  314. }
  315. $total_rows = DB()->num_rows($result);
  316. DB()->sql_freeresult($result);
  317. return $attachments;
  318. }
  319. /**
  320. * perform LIMIT statement on arrays
  321. */
  322. function limit_array($array, $start, $pagelimit)
  323. {
  324. // array from start - start+pagelimit
  325. $limit = (sizeof($array) < ($start + $pagelimit)) ? sizeof($array) : $start + $pagelimit;
  326. $limit_array = array();
  327. for ($i = $start; $i < $limit; $i++)
  328. {
  329. $limit_array[] = $array[$i];
  330. }
  331. return $limit_array;
  332. }