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

Размер файла: 11.19Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. function check_image_type(&$type, &$error, &$error_msg)
  7. {
  8. global $lang;
  9.  
  10. switch( $type )
  11. {
  12. case 'jpeg':
  13. case 'pjpeg':
  14. case 'jpg':
  15. return '.jpg';
  16. break;
  17. case 'gif':
  18. return '.gif';
  19. break;
  20. case 'png':
  21. return '.png';
  22. break;
  23. default:
  24. $error = true;
  25. $error_msg = (!empty($error_msg)) ? $error_msg . '<br />' . $lang['Avatar_filetype'] : $lang['Avatar_filetype'];
  26. break;
  27. }
  28.  
  29. return false;
  30. }
  31.  
  32. function user_avatar_delete($avatar_type, $avatar_file)
  33. {
  34. global $board_config, $userdata;
  35.  
  36. $avatar_file = basename($avatar_file);
  37. if ( $avatar_type == USER_AVATAR_UPLOAD && $avatar_file != '' )
  38. {
  39. if ( @file_exists(@phpbb_realpath('./' . $board_config['avatar_path'] . '/' . $avatar_file)) )
  40. {
  41. @unlink('./' . $board_config['avatar_path'] . '/' . $avatar_file);
  42. }
  43. }
  44.  
  45. return ", user_avatar = '', user_avatar_type = " . USER_AVATAR_NONE;
  46. }
  47.  
  48. function user_avatar_gallery($mode, &$error, &$error_msg, $avatar_filename, $avatar_category)
  49. {
  50. global $board_config;
  51.  
  52. $avatar_filename = phpbb_ltrim(basename($avatar_filename), "'");
  53. $avatar_category = phpbb_ltrim(basename($avatar_category), "'");
  54. if(!preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $avatar_filename))
  55. {
  56. return '';
  57. }
  58.  
  59. if ($avatar_filename == "" || $avatar_category == "")
  60. {
  61. return '';
  62. }
  63.  
  64. if ( file_exists(@phpbb_realpath($board_config['avatar_gallery_path'] . '/' . $avatar_category . '/' . $avatar_filename)) )
  65. {
  66. $return = ", user_avatar = '" . str_replace("\'", "''", $avatar_category . '/' . $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_GALLERY;
  67. }
  68. else
  69. {
  70. $return = '';
  71. }
  72. return $return;
  73. }
  74.  
  75. function user_avatar_url($mode, &$error, &$error_msg, $avatar_filename)
  76. {
  77. global $lang;
  78.  
  79. if ( !preg_match('#^(http)|(ftp):\/\/#i', $avatar_filename) )
  80. {
  81. $avatar_filename = 'http://' . $avatar_filename;
  82. }
  83.  
  84. $avatar_filename = substr($avatar_filename, 0, 100);
  85.  
  86. if ( !preg_match("#^((ht|f)tp://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))$)#is", $avatar_filename) )
  87. {
  88. $error = true;
  89. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Wrong_remote_avatar_format'] : $lang['Wrong_remote_avatar_format'];
  90. return;
  91. }
  92.  
  93. return ", user_avatar = '" . str_replace("\'", "''", $avatar_filename) . "', user_avatar_type = " . USER_AVATAR_REMOTE;
  94.  
  95. }
  96.  
  97. function user_avatar_upload($mode, $avatar_mode, &$current_avatar, &$current_type, &$error, &$error_msg, $avatar_filename, $avatar_realname, $avatar_filesize, $avatar_filetype)
  98. {
  99. global $board_config, $db, $lang, $result_ua, $opera_mini;
  100.  
  101. $ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
  102.  
  103. $width = $height = 0;
  104. $type = '';
  105.  
  106. if ( $avatar_mode == 'remote' && preg_match('/^(http:\/\/)?([\w\-\.]+)\:?([0-9]*)\/([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png)))$/', $avatar_filename, $url_ary) )
  107. {
  108. if ( empty($url_ary[4]) )
  109. {
  110. $error = true;
  111. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['Incomplete_URL'] : $lang['Incomplete_URL'];
  112. return;
  113. }
  114.  
  115. $base_get = '/' . $url_ary[4];
  116. $port = ( !empty($url_ary[3]) ) ? $url_ary[3] : 80;
  117.  
  118. if ( !($fsock = @fsockopen($url_ary[2], $port, $errno, $errstr)) )
  119. {
  120. $error = true;
  121. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['No_connection_URL'] : $lang['No_connection_URL'];
  122. return;
  123. }
  124.  
  125. @fputs($fsock, "GET $base_get HTTP/1.1\r\n");
  126. @fputs($fsock, "HOST: " . $url_ary[2] . "\r\n");
  127. @fputs($fsock, "Connection: close\r\n\r\n");
  128.  
  129. unset($avatar_data);
  130. while( !@feof($fsock) )
  131. {
  132. $avatar_data .= @fread($fsock, $board_config['avatar_filesize']);
  133. }
  134. @fclose($fsock);
  135.  
  136. if (!preg_match('#Content-Length\: ([0-9]+)[^ /][\s]+#i', $avatar_data, $file_data1) || !preg_match('#Content-Type\: image/[x\-]*([a-z]+)[\s]+#i', $avatar_data, $file_data2))
  137. {
  138. $error = true;
  139. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $lang['File_no_data'] : $lang['File_no_data'];
  140. return;
  141. }
  142.  
  143. $avatar_filesize = $file_data1[1];
  144. $avatar_filetype = $file_data2[1];
  145.  
  146. if ( !$error && $avatar_filesize > 0 && $avatar_filesize < $board_config['avatar_filesize'] )
  147. {
  148. $avatar_data = substr($avatar_data, strlen($avatar_data) - $avatar_filesize, $avatar_filesize);
  149. if ($result_ua)
  150. {
  151. $tmp_filename = $opera_mini.'/'.$avatar_realname;
  152. } else {
  153. $tmp_path = ( !@$ini_val('safe_mode') ) ? '/tmp' : './' . $board_config['avatar_path'] . '/tmp';
  154. $tmp_filename = tempnam($tmp_path, uniqid(rand()) . '-');
  155. }
  156.  
  157. $fptr = @fopen($tmp_filename, 'wb');
  158. $bytes_written = @fwrite($fptr, $avatar_data, $avatar_filesize);
  159. @fclose($fptr);
  160.  
  161. if ( $bytes_written != $avatar_filesize )
  162. {
  163. @unlink($tmp_filename);
  164. message_die(GENERAL_ERROR, 'Could not write avatar file to local storage. Please contact the board administrator with this message', '', __LINE__, __FILE__);
  165. }
  166.  
  167. list($width, $height, $type) = @getimagesize($tmp_filename);
  168. }
  169. else
  170. {
  171. $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
  172.  
  173. $error = true;
  174. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  175. }
  176. }
  177. else if ( ( file_exists(@phpbb_realpath($avatar_filename)) ) && preg_match('/\.(jpg|jpeg|gif|png)$/i', $avatar_realname) )
  178. {
  179. if ( $avatar_filesize <= $board_config['avatar_filesize'] && $avatar_filesize > 0 )
  180. {
  181. preg_match('#image\/[x\-]*([a-z]+)#', $avatar_filetype, $avatar_filetype);
  182. $avatar_filetype = $avatar_filetype[1];
  183. }
  184. else
  185. {
  186. $l_avatar_size = sprintf($lang['Avatar_filesize'], round($board_config['avatar_filesize'] / 1024));
  187.  
  188. $error = true;
  189. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  190. return;
  191. }
  192.  
  193. list($width, $height, $type) = @getimagesize($avatar_filename);
  194. }
  195.  
  196. if ( !($imgtype = check_image_type($avatar_filetype, $error, $error_msg)) )
  197. {
  198. return;
  199. }
  200.  
  201. switch ($type)
  202. {
  203. case 1:
  204. if ($imgtype != '.gif')
  205. {
  206. @unlink($tmp_filename);
  207. message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
  208. }
  209. break;
  210.  
  211. case 2:
  212. case 9:
  213. case 10:
  214. case 11:
  215. case 12:
  216. if ($imgtype != '.jpg' && $imgtype != '.jpeg')
  217. {
  218. @unlink($tmp_filename);
  219. message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
  220. }
  221. break;
  222.  
  223. case 3:
  224. if ($imgtype != '.png')
  225. {
  226. @unlink($tmp_filename);
  227. message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
  228. }
  229. break;
  230.  
  231. default:
  232. @unlink($tmp_filename);
  233. message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
  234. }
  235.  
  236. if ( $width > 0 && $height > 0 && $width <= $board_config['avatar_max_width'] && $height <= $board_config['avatar_max_height'] )
  237. {
  238. $new_filename = uniqid(rand()) . $imgtype;
  239.  
  240. if ( $current_type == USER_AVATAR_UPLOAD && $current_avatar != '' )
  241. {
  242. user_avatar_delete($current_type, $current_avatar);
  243. }
  244.  
  245. if( $avatar_mode == 'remote' )
  246. {
  247. @copy($tmp_filename, './' . $board_config['avatar_path'] . "/$new_filename");
  248. @unlink($tmp_filename);
  249. }
  250. else
  251. {
  252. if (!is_uploaded_file($avatar_filename) && !$result_ua)
  253. {
  254. message_die(GENERAL_ERROR, 'Unable to upload file', '', __LINE__, __FILE__);
  255. }
  256. @copy($avatar_filename, './' . $board_config['avatar_path'] . "/$new_filename");
  257.  
  258. if ($result_ua)
  259. {
  260. @unlink($avatar_filename);
  261. }
  262. }
  263.  
  264. @chmod('./' . $board_config['avatar_path'] . "/$new_filename", 0777);
  265.  
  266. $avatar_sql = ", user_avatar = '$new_filename', user_avatar_type = " . USER_AVATAR_UPLOAD;
  267. }
  268. else
  269. {
  270. $l_avatar_size = sprintf($lang['Avatar_imagesize'], $board_config['avatar_max_width'], $board_config['avatar_max_height']);
  271.  
  272. $error = true;
  273. $error_msg = ( !empty($error_msg) ) ? $error_msg . '<br />' . $l_avatar_size : $l_avatar_size;
  274. }
  275.  
  276. return $avatar_sql;
  277. }
  278.  
  279. function display_avatar_gallery($mode, &$category, &$user_id, &$viewemail, &$notifypm, &$popup_pm, &$notifyreply, &$attachsig, &$allowhtml, &$allowbbcode, &$allowsmilies, &$hideonline, &$style, &$language, &$timezone, &$dateformat, &$session_id)
  280. {
  281. global $board_config, $db, $template, $lang, $images, $theme;
  282. global $phpbb_root_path, $phpEx;
  283.  
  284. $dir = @opendir($board_config['avatar_gallery_path']);
  285.  
  286. $avatar_images = array();
  287. while( $file = @readdir($dir) )
  288. {
  289. if( $file != '.' && $file != '..' && !is_file($board_config['avatar_gallery_path'] . '/' . $file) && !is_link($board_config['avatar_gallery_path'] . '/' . $file) )
  290. {
  291. $sub_dir = @opendir($board_config['avatar_gallery_path'] . '/' . $file);
  292.  
  293. $avatar_row_count = 0;
  294. $avatar_col_count = 0;
  295. while( $sub_file = @readdir($sub_dir) )
  296. {
  297. if( preg_match('/(\.gif$|\.png$|\.jpg|\.jpeg)$/is', $sub_file) )
  298. {
  299. $avatar_images[$file][$avatar_row_count][$avatar_col_count] = $sub_file;
  300. $avatar_name[$file][$avatar_row_count][$avatar_col_count] = ucfirst(str_replace("_", " ", preg_replace('/^(.*)\..*$/', '\1', $sub_file)));
  301.  
  302. $avatar_col_count++;
  303. if( $avatar_col_count == 5 )
  304. {
  305. $avatar_row_count++;
  306. $avatar_col_count = 0;
  307. }
  308. }
  309. }
  310. }
  311. }
  312.  
  313. @closedir($dir);
  314.  
  315. @ksort($avatar_images);
  316. @reset($avatar_images);
  317.  
  318. if( empty($category) )
  319. {
  320. list($category, ) = each($avatar_images);
  321. }
  322. @reset($avatar_images);
  323.  
  324. $s_categories = '<select name="avatarcategory">';
  325. while( list($key) = each($avatar_images) )
  326. {
  327. $selected = ( $key == $category ) ? ' selected="selected"' : '';
  328. if( count($avatar_images[$key]) )
  329. {
  330. $s_categories .= '<option value="' . $key . '"' . $selected . '>' . ucfirst($key) . '</option>';
  331. }
  332. }
  333. $s_categories .= '</select>';
  334.  
  335. $s_colspan = 0;
  336. for($i = 0; $i < count($avatar_images[$category]); $i++)
  337. {
  338. $template->assign_block_vars("avatar_row", array());
  339.  
  340. $s_colspan = max($s_colspan, count($avatar_images[$category][$i]));
  341.  
  342. for($j = 0; $j < count($avatar_images[$category][$i]); $j++)
  343. {
  344. $template->assign_block_vars('avatar_row.avatar_column', array(
  345. "AVATAR_IMAGE" => $board_config['avatar_gallery_path'] . '/' . $category . '/' . $avatar_images[$category][$i][$j],
  346. "AVATAR_NAME" => $avatar_name[$category][$i][$j],
  347. "S_OPTIONS_AVATAR" => $avatar_images[$category][$i][$j])
  348. );
  349. }
  350. }
  351.  
  352. $params = array('coppa', 'user_id', 'viewemail', 'notifypm', 'popup_pm', 'notifyreply', 'attachsig', 'allowhtml', 'allowbbcode', 'allowsmilies', 'hideonline', 'style', 'language', 'timezone', 'dateformat');
  353.  
  354. $s_hidden_vars = '<input type="hidden" name="sid" value="' . $session_id . '" /><input type="hidden" name="agreed" value="true" /><input type="hidden" name="avatarcatname" value="' . $category . '" />';
  355.  
  356. for($i = 0; $i < count($params); $i++)
  357. {
  358. $s_hidden_vars .= '<input type="hidden" name="' . $params[$i] . '" value="' . str_replace('"', '&quot;', $$params[$i]) . '" />';
  359. }
  360. $template->assign_vars(array(
  361. 'L_AVATAR_GALLERY' => $lang['Avatar_gallery'],
  362. 'L_SELECT_AVATAR' => $lang['Select_avatar'],
  363. 'L_RETURN_PROFILE' => $lang['Return_profile'],
  364. 'L_CATEGORY' => $lang['Select_category'],
  365.  
  366. 'S_CATEGORY_SELECT' => $s_categories,
  367. 'S_COLSPAN' => $s_colspan,
  368. 'S_PROFILE_ACTION' => append_sid("profile.$phpEx?mode=$mode"),
  369. 'S_HIDDEN_FIELDS' => $s_hidden_vars)
  370. );
  371.  
  372. return;
  373. }
  374.  
  375. ?>