PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/attach_mod/includes/functions_attach.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 327 lines | 216 code | 60 blank | 51 comment | 41 complexity | 3646fbdf4d51e052f871cd6511173248 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * (c) 2002 Meik Sievertsen (Acyd Burn)
  14. *
  15. */
  16. /**
  17. * All Attachment Functions needed everywhere
  18. */
  19. /**
  20. * A simple dectobase64 function
  21. */
  22. function base64_pack($number)
  23. {
  24. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
  25. $base = strlen($chars);
  26. if ($number > 4096)
  27. {
  28. return;
  29. }
  30. elseif ($number < $base)
  31. {
  32. return $chars[$number];
  33. }
  34. $hexval = '';
  35. while ($number > 0)
  36. {
  37. $remainder = $number%$base;
  38. if ($remainder < $base)
  39. {
  40. $hexval = $chars[$remainder] . $hexval;
  41. }
  42. $number = floor($number/$base);
  43. }
  44. return $hexval;
  45. }
  46. /**
  47. * base64todec function
  48. */
  49. function base64_unpack($string)
  50. {
  51. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
  52. $base = strlen($chars);
  53. $length = strlen($string);
  54. $number = 0;
  55. for($i = 1; $i <= $length; $i++)
  56. {
  57. $pos = $length - $i;
  58. $operand = strpos($chars, substr($string,$pos,1));
  59. $exponent = pow($base, $i-1);
  60. $decValue = $operand * $exponent;
  61. $number += $decValue;
  62. }
  63. return $number;
  64. }
  65. /**
  66. * Used for determining if Forum ID is authed, please use this Function on all Posting Screens
  67. */
  68. function is_forum_authed($auth_cache, $check_forum_id)
  69. {
  70. $one_char_encoding = '#';
  71. $two_char_encoding = '.';
  72. if (trim($auth_cache) == '')
  73. {
  74. return true;
  75. }
  76. $auth = array();
  77. $auth_len = 1;
  78. for ($pos = 0; $pos < strlen($auth_cache); $pos+=$auth_len)
  79. {
  80. $forum_auth = substr($auth_cache, $pos, 1);
  81. if ($forum_auth == $one_char_encoding)
  82. {
  83. $auth_len = 1;
  84. continue;
  85. }
  86. elseif ($forum_auth == $two_char_encoding)
  87. {
  88. $auth_len = 2;
  89. $pos--;
  90. continue;
  91. }
  92. $forum_auth = substr($auth_cache, $pos, $auth_len);
  93. $forum_id = (int) base64_unpack($forum_auth);
  94. if ($forum_id == $check_forum_id)
  95. {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /**
  102. * Init FTP Session
  103. */
  104. function attach_init_ftp($mode = false)
  105. {
  106. global $lang, $config;
  107. $server = (trim($config['ftp_server']) == '') ? 'localhost' : trim($config['ftp_server']);
  108. $ftp_path = ($mode == MODE_THUMBNAIL) ? trim($config['ftp_path']) . '/' . THUMB_DIR : trim($config['ftp_path']);
  109. $conn_id = @ftp_connect($server);
  110. if (!$conn_id)
  111. {
  112. message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_connect'], $server));
  113. }
  114. $login_result = @ftp_login($conn_id, $config['ftp_user'], $config['ftp_pass']);
  115. if (!$login_result)
  116. {
  117. message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_login'], $config['ftp_user']));
  118. }
  119. if (!@ftp_pasv($conn_id, intval($config['ftp_pasv_mode'])))
  120. {
  121. message_die(GENERAL_ERROR, $lang['Ftp_error_pasv_mode']);
  122. }
  123. $result = @ftp_chdir($conn_id, $ftp_path);
  124. if (!$result)
  125. {
  126. message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_path'], $ftp_path));
  127. }
  128. return $conn_id;
  129. }
  130. /**
  131. * Count Filesize of Attachments in Database based on the attachment id
  132. */
  133. function get_total_attach_filesize($attach_ids)
  134. {
  135. global $db;
  136. if (!is_array($attach_ids) || !sizeof($attach_ids))
  137. {
  138. return 0;
  139. }
  140. $attach_ids = implode(', ', array_map('intval', $attach_ids));
  141. if (!$attach_ids)
  142. {
  143. return 0;
  144. }
  145. $sql = 'SELECT filesize
  146. FROM ' . ATTACHMENTS_DESC_TABLE . "
  147. WHERE attach_id IN ($attach_ids)";
  148. $result = $db->sql_query($sql);
  149. $total_filesize = 0;
  150. while ($row = $db->sql_fetchrow($result))
  151. {
  152. $total_filesize += (int) $row['filesize'];
  153. }
  154. $db->sql_freeresult($result);
  155. return $total_filesize;
  156. }
  157. /**
  158. * Realpath replacement for attachment mod
  159. */
  160. function amod_realpath($path)
  161. {
  162. return (function_exists('realpath')) ? realpath($path) : $path;
  163. }
  164. /**
  165. * get all attachments from a post (could be an array of posts as well)
  166. */
  167. function get_attachments_from_post($post_id_array)
  168. {
  169. global $db, $config;
  170. $attachments = array();
  171. if (!is_array($post_id_array))
  172. {
  173. if (empty($post_id_array))
  174. {
  175. return $attachments;
  176. }
  177. $post_id = intval($post_id_array);
  178. $post_id_array = array();
  179. $post_id_array[] = $post_id;
  180. }
  181. $post_id_array = implode(', ', array_map('intval', $post_id_array));
  182. if ($post_id_array == '')
  183. {
  184. return $attachments;
  185. }
  186. $display_order = (intval($config['display_order']) == 0) ? 'DESC' : 'ASC';
  187. $sql = 'SELECT a.post_id, d.*
  188. FROM ' . ATTACHMENTS_TABLE . ' a, ' . ATTACHMENTS_DESC_TABLE . " d
  189. WHERE a.post_id IN ($post_id_array)
  190. AND a.attach_id = d.attach_id
  191. ORDER BY d.filetime $display_order";
  192. $result = $db->sql_query($sql);
  193. $num_rows = $db->sql_numrows($result);
  194. $attachments = $db->sql_fetchrowset($result);
  195. $db->sql_freeresult($result);
  196. if ($num_rows == 0)
  197. {
  198. return array();
  199. }
  200. return $attachments;
  201. }
  202. /**
  203. * Update attachments stats
  204. */
  205. function update_attachments_stats($attach_id)
  206. {
  207. global $db, $user, $lang;
  208. $sql = 'UPDATE ' . ATTACHMENTS_DESC_TABLE . '
  209. SET download_count = download_count + 1
  210. WHERE attach_id = ' . (int) $attach_id;
  211. $db->sql_query($sql);
  212. if (!$user->data['is_bot'] && defined('USE_ATTACHMENTS_STATS') && USE_ATTACHMENTS_STATS)
  213. {
  214. $sql = "INSERT INTO " . ATTACHMENTS_STATS_TABLE . " (`attach_id`, `user_id`, `user_ip`, `user_browser`, `download_time`)
  215. VALUES ('" . $attach_id . "', '" . $user->data['user_id'] . "', '" . $db->sql_escape($user->ip) . "', '" . $db->sql_escape(addslashes($user->browser)) . "', '" . time() . "')";
  216. $result = $db->sql_query($sql);
  217. }
  218. return true;
  219. }
  220. /**
  221. * Gets the upload dir
  222. */
  223. function get_upload_dir($is_image = false)
  224. {
  225. global $config;
  226. if (!intval($config['allow_ftp_upload']))
  227. {
  228. //$upload_dir = !empty($is_image) ? rtrim(POSTED_IMAGES_PATH, '/') : $config['upload_dir'];
  229. $upload_dir = $config['upload_dir'];
  230. if (defined('IN_ADMIN'))
  231. {
  232. if ($config['upload_dir'][0] == '/' || ($config['upload_dir'][0] != '/' && $config['upload_dir'][1] == ':'))
  233. {
  234. $upload_dir = $config['upload_dir'];
  235. }
  236. else
  237. {
  238. $upload_dir = IP_ROOT_PATH . $config['upload_dir'];
  239. }
  240. }
  241. }
  242. else
  243. {
  244. $upload_dir = $config['download_path'];
  245. }
  246. return $upload_dir;
  247. }
  248. /**
  249. * Gets physical filename
  250. */
  251. function get_physical_filename($physical_filename, $is_thumbnail = false)
  252. {
  253. if (ATTACHMENT_MOD_BASENAME)
  254. {
  255. $physical_filename = ($is_thumbnail ? (THUMB_DIR . '/t_') : '') . basename($physical_filename);
  256. }
  257. return $physical_filename;
  258. }
  259. /**
  260. * Move personal image to user subfolder
  261. */
  262. function move_uploaded_image($filename)
  263. {
  264. global $config, $user;
  265. return 1;
  266. }
  267. ?>