PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class_images.php

https://github.com/MightyGorgon/icy_phoenix
PHP | 374 lines | 257 code | 51 blank | 66 comment | 28 complexity | 1abf6a62807c274496ce610eaf7c22e1 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. if (!defined('IN_ICYPHOENIX'))
  11. {
  12. die('Hacking attempt');
  13. }
  14. /**
  15. * Images Class
  16. */
  17. class class_images
  18. {
  19. /**
  20. * Construct
  21. */
  22. function __construct()
  23. {
  24. }
  25. /**
  26. * Initialize vars
  27. */
  28. function var_init()
  29. {
  30. return true;
  31. }
  32. /**
  33. * Get image name and upload folder
  34. */
  35. function get_image_upload_data($filename, $extension, $upload_dir)
  36. {
  37. global $user;
  38. $image_upload_data = array('filename' => $filename, 'upload_dir' => $upload_dir);
  39. if ($user->data['user_id'] < 0)
  40. {
  41. $image_upload_data['filename'] = 'guest_' . preg_replace('/[^a-z0-9]+/', '_', $image_upload_data['filename']);
  42. }
  43. else
  44. {
  45. $image_upload_data['filename'] = preg_replace('/[^a-z0-9]+/', '_', $image_upload_data['filename']);
  46. if (USERS_SUBFOLDERS_IMG == true)
  47. {
  48. if (@is_dir($image_upload_data['upload_dir'] . $user->data['user_id']))
  49. {
  50. $image_upload_data['upload_dir'] = $image_upload_data['upload_dir'] . $user->data['user_id'] . '/';
  51. }
  52. else
  53. {
  54. $dir_creation = @mkdir($image_upload_data['upload_dir'] . $user->data['user_id'], 0777);
  55. if ($dir_creation == true)
  56. {
  57. $image_upload_data['upload_dir'] = $image_upload_data['upload_dir'] . $user->data['user_id'] . '/';
  58. }
  59. else
  60. {
  61. $image_upload_data['filename'] = 'user_' . $user->data['user_id'] . '_' . $image_upload_data['filename'];
  62. }
  63. }
  64. }
  65. else
  66. {
  67. $image_upload_data['filename'] = 'user_' . $user->data['user_id'] . '_' . $image_upload_data['filename'];
  68. }
  69. }
  70. while(@file_exists($image_upload_data['upload_dir'] . $image_upload_data['filename'] . '.' . $extension))
  71. {
  72. $image_upload_data['filename'] = $image_upload_data['filename'] . '_' . time() . '_' . mt_rand(100000, 999999);
  73. }
  74. return $image_upload_data;
  75. }
  76. /**
  77. * Upload an image, returns false on error
  78. */
  79. function upload_image($filename, $extension, $upload_dir, $filename_tmp)
  80. {
  81. if(is_uploaded_file($filename_tmp))
  82. {
  83. @move_uploaded_file($filename_tmp, $upload_dir . $filename . '.' . $extension);
  84. @chmod($upload_dir . $filename . '.' . $extension, 0777);
  85. }
  86. $pic_size = @getimagesize($upload_dir . $filename . '.' . $extension);
  87. if($pic_size == false)
  88. {
  89. @unlink($upload_dir . $filename . '.' . $extension);
  90. //return false;
  91. }
  92. return $pic_size;
  93. }
  94. /**
  95. * Generate image paths
  96. */
  97. function generate_image_paths($image_data)
  98. {
  99. global $user, $lang;
  100. $image_paths = array();
  101. $image_paths['sub_path'] = (USERS_SUBFOLDERS_IMG && (!empty($image_data['pic_user_id'])) ? ($image_data['pic_user_id'] . '/') : '') . $image_data['pic_filename'];
  102. $image_paths['url'] = POSTED_IMAGES_PATH . $image_paths['sub_path'];
  103. $image_paths['thumbnail_fullpath'] = POSTED_IMAGES_THUMBS_S_PATH . $image_paths['sub_path'];
  104. $image_paths['thumb'] = (@file_exists($image_paths['thumbnail_fullpath']) ? $image_paths['thumbnail_fullpath'] : append_sid(CMS_PAGE_IMAGE_THUMBNAIL_S . '?pic_id=' . urlencode($image_paths['sub_path'])));
  105. $image_paths['delete_url'] = (($user->data['user_level'] == ADMIN) ? ('<br /><span class="gensmall"><a href="' . append_sid(CMS_PAGE_IMAGES . '?mode=delete&amp;pic_id=' . $image_data['pic_id']) . '">' . $lang['Delete'] . '</a></span>') : '');
  106. return $image_paths;
  107. }
  108. /**
  109. * Get thumbnail data
  110. */
  111. function get_thumbnail_data($pic_thumbnail_path, $pic_thumbnail, $pic_thumbnail_fullpath, $pic_filename, $pic_thumbnail_prefix = '')
  112. {
  113. $thumbnail_data = array(
  114. 'thumbnail' => $pic_thumbnail,
  115. 'full_path' => $pic_thumbnail_fullpath
  116. );
  117. if (is_dir($pic_thumbnail_path))
  118. {
  119. $thumbnail_data['thumbnail'] = $pic_thumbnail_prefix . $pic_filename;
  120. $thumbnail_data['full_path'] = $pic_thumbnail_path . '/' . $thumbnail_data['thumbnail'];
  121. }
  122. else
  123. {
  124. $dir_creation = @mkdir($pic_thumbnail_path, 0777);
  125. if ($dir_creation == true)
  126. {
  127. $thumbnail_data['thumbnail'] = $pic_thumbnail_prefix . $pic_filename;
  128. $thumbnail_data['full_path'] = $pic_thumbnail_path . '/' . $thumbnail_data['thumbnail'];
  129. }
  130. }
  131. return $thumbnail_data;
  132. }
  133. /**
  134. * Get user dir
  135. */
  136. function get_user_dir($upload_dir, $user_upload_dir)
  137. {
  138. global $user;
  139. $user_dir = array(
  140. 'upload_dir' => $upload_dir,
  141. 'user_upload_dir' => $user_upload_dir
  142. );
  143. if (is_dir($user_dir['upload_dir'] . $user->data['user_id']))
  144. {
  145. $user_dir['user_upload_dir'] = $user->data['user_id'] . '/';
  146. $user_dir['upload_dir'] = $user_dir['upload_dir'] . $user_dir['user_upload_dir'];
  147. }
  148. else
  149. {
  150. $dir_creation = @mkdir($user_dir['upload_dir'] . $user->data['user_id'], 0777);
  151. if ($dir_creation)
  152. {
  153. $user_dir['user_upload_dir'] = $user->data['user_id'] . '/';
  154. $user_dir['upload_dir'] = $user_dir['upload_dir'] . $user_dir['user_upload_dir'];
  155. }
  156. }
  157. return $user_dir;
  158. }
  159. /**
  160. * Get image details
  161. */
  162. function get_image_details($image_path)
  163. {
  164. $image_details = array(
  165. 'time' => filemtime($image_path),
  166. 'size' => filesize($image_path),
  167. );
  168. return $image_details;
  169. }
  170. /**
  171. * Get user images
  172. */
  173. function get_user_images($user_id, $order_by = '', $start = 0, $n_items = 0)
  174. {
  175. global $db, $cache, $config, $lang;
  176. $images = array();
  177. $user_id = (int) $user_id;
  178. if (!empty($user_id))
  179. {
  180. $order_sql = " ORDER BY " . (!empty($order_by) ? $order_by : "i.pic_time DESC");
  181. $limit_sql = (!empty($n_items) ? (" LIMIT " . (!empty($start) ? ($start . ", " . $n_items) : ($n_items . " "))) : "");
  182. $sql = "SELECT i.*, u.user_id, u.username, u.user_active, u.user_color, u.user_level, u.user_rank, u.user_first_name, u.user_last_name
  183. FROM " . IMAGES_TABLE . " i, " . USERS_TABLE . " u
  184. WHERE i.pic_user_id = " . $user_id . "
  185. AND i.pic_user_id = u.user_id"
  186. . $order_sql
  187. . $limit_sql;
  188. $result = $db->sql_query($sql);
  189. $images = $db->sql_fetchrowset($result);
  190. $db->sql_freeresult($result);
  191. }
  192. return $images;
  193. }
  194. /**
  195. * Get all user images
  196. */
  197. function get_all_user_images($order_by = '', $start = 0, $n_items = 0)
  198. {
  199. global $db, $cache, $config, $lang;
  200. $images = array();
  201. $order_sql = " ORDER BY " . (!empty($order_by) ? $order_by : "i.pic_time DESC");
  202. $limit_sql = (!empty($n_items) ? (" LIMIT " . (!empty($start) ? ($start . ", " . $n_items) : ($n_items . " "))) : "");
  203. // Query only existing users...
  204. /*
  205. $sql = "SELECT i.*, u.user_id, u.username, u.user_active, u.user_color, u.user_level, u.user_rank, u.user_first_name, u.user_last_name
  206. FROM " . IMAGES_TABLE . " i, " . USERS_TABLE . " u
  207. WHERE i.pic_user_id = u.user_id"
  208. . $order_sql
  209. . $limit_sql;
  210. */
  211. // Query all images, regardless if the user still exists or not...
  212. $sql = "SELECT i.*, u.user_id, u.username, u.user_active, u.user_color, u.user_level, u.user_rank, u.user_first_name, u.user_last_name
  213. FROM " . IMAGES_TABLE . " AS i LEFT JOIN " . USERS_TABLE . " AS u ON i.pic_user_id = u.user_id"
  214. . $order_sql
  215. . $limit_sql;
  216. $result = $db->sql_query($sql);
  217. $images = $db->sql_fetchrowset($result);
  218. $db->sql_freeresult($result);
  219. return $images;
  220. }
  221. /**
  222. * Get image
  223. */
  224. function get_image($pic_id)
  225. {
  226. global $db, $cache, $config, $lang;
  227. $image = array();
  228. $pic_id = (int) $pic_id;
  229. if (!empty($pic_id))
  230. {
  231. $sql = "SELECT i.*, u.user_id, u.username, u.user_active, u.user_color, u.user_level, u.user_rank, u.user_first_name, u.user_last_name
  232. FROM " . IMAGES_TABLE . " i, " . USERS_TABLE . " u
  233. WHERE i.pic_id = " . $pic_id . "
  234. AND i.pic_user_id = u.user_id";
  235. $result = $db->sql_query($sql);
  236. $image = $db->sql_fetchrow($result);
  237. $db->sql_freeresult($result);
  238. }
  239. return $image;
  240. }
  241. /**
  242. * Get total user images
  243. */
  244. function get_total_user_images($user_id)
  245. {
  246. global $db, $cache, $config, $lang;
  247. $user_id = (int) $user_id;
  248. $sql = "SELECT COUNT(i.pic_id) AS total_images
  249. FROM " . IMAGES_TABLE . " i
  250. WHERE i.pic_user_id = " . $user_id;
  251. $result = $db->sql_query($sql);
  252. $images_data = $db->sql_fetchrow($result);
  253. $total_images = $images_data['total_images'];
  254. $db->sql_freeresult($result);
  255. return $total_images;
  256. }
  257. /**
  258. * Get total images
  259. */
  260. function get_total_images()
  261. {
  262. global $db, $cache, $config, $lang;
  263. $sql = "SELECT COUNT(i.pic_id) AS total_images
  264. FROM " . IMAGES_TABLE . " i";
  265. $result = $db->sql_query($sql);
  266. $images_data = $db->sql_fetchrow($result);
  267. $total_images = $images_data['total_images'];
  268. $db->sql_freeresult($result);
  269. return $total_images;
  270. }
  271. /**
  272. * Submit an image
  273. */
  274. function submit_image(&$image_data, $mode = 'insert')
  275. {
  276. global $db, $cache, $config, $lang;
  277. if ($mode == 'insert')
  278. {
  279. $sql = "INSERT INTO " . IMAGES_TABLE . " " . $db->sql_build_insert_update($image_data, true);
  280. $result = $db->sql_query($sql);
  281. $image_data['pic_id'] = $db->sql_nextid();
  282. }
  283. else
  284. {
  285. $sql = "UPDATE " . IMAGES_TABLE . " SET
  286. " . $db->sql_build_insert_update($image_data, false) . "
  287. WHERE pic_id = " . (int) $image_data['pic_id'];
  288. $result = $db->sql_query($sql);
  289. }
  290. return true;
  291. }
  292. /*
  293. * Remove an image
  294. */
  295. function remove_image($pic_id)
  296. {
  297. global $db;
  298. $pic_id = (int) $pic_id;
  299. $image_data = $this->get_image($pic_id);
  300. if (!empty($image_data))
  301. {
  302. $image_full_path = POSTED_IMAGES_PATH . ((USERS_SUBFOLDERS_IMG && !empty($image_data['pic_user_id'])) ? ($image_data['pic_user_id'] . '/') : '') . (!empty($image_data['pic_filename']) ? $image_data['pic_filename'] : '');
  303. if (@file_exists($image_full_path))
  304. {
  305. @unlink($image_full_path);
  306. }
  307. $thumbnail_full_path = POSTED_IMAGES_THUMBS_PATH . ((USERS_SUBFOLDERS_IMG && !empty($image_data['pic_user_id'])) ? ($image_data['pic_user_id'] . '/') : '') . (!empty($image_data['pic_filename']) ? $image_data['pic_filename'] : '');
  308. if (@file_exists($thumbnail_full_path))
  309. {
  310. @unlink($thumbnail_full_path);
  311. }
  312. $thumbnail_list_full_path = POSTED_IMAGES_THUMBS_PATH . ((USERS_SUBFOLDERS_IMG && !empty($image_data['pic_user_id'])) ? ($image_data['pic_user_id'] . '/') : '') . (!empty($image_data['pic_filename']) ? ('_' . $image_data['pic_filename']) : '');
  313. if (@file_exists($thumbnail_list_full_path))
  314. {
  315. @unlink($thumbnail_list_full_path);
  316. }
  317. $sql = "DELETE FROM " . IMAGES_TABLE . " WHERE pic_id = " . $pic_id;
  318. $result = $db->sql_query($sql);
  319. }
  320. return true;
  321. }
  322. }
  323. ?>