uploadavatar.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: uploadavatar.php 34944 2014-09-05 08:06:41Z nemohou $
  7. */
  8. if(!defined('IN_MOBILE_API')) {
  9. exit('Access Denied');
  10. }
  11. $_GET['mod'] = 'spacecp';
  12. $_GET['ac'] = 'avatar';
  13. include_once 'home.php';
  14. class mobile_api {
  15. public static $tmpavatar;
  16. public static $tmpavatarbig;
  17. public static $tmpavatarmiddle;
  18. public static $tmpavatarsmall;
  19. public static function common() {
  20. global $_G;
  21. if(empty($_G['uid'])) {
  22. self::error('api_uploadavatar_unavailable_user');
  23. }
  24. if(empty($_FILES['Filedata'])) {
  25. self::error('api_uploadavatar_unavailable_pic');
  26. }
  27. list($width, $height, $type, $attr) = getimagesize($_FILES['Filedata']['tmp_name']);
  28. $imgtype = array(1 => '.gif', 2 => '.jpg', 3 => '.png');
  29. $filetype = $imgtype[$type];
  30. if (!$filetype) {
  31. $filetype = '.jpg';
  32. }
  33. $avatarpath = $_G['setting']['attachdir'];
  34. $tmpavatar = $avatarpath.'./temp/upload'.$_G['uid'].$filetype;
  35. file_exists($tmpavatar) && @unlink($tmpavatar);
  36. if(@copy($_FILES['Filedata']['tmp_name'], $tmpavatar) || @move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmpavatar)) {
  37. @unlink($_FILES['Filedata']['tmp_name']);
  38. list($width, $height, $type, $attr) = getimagesize($tmpavatar);
  39. if($width < 10 || $height < 10 || $type == 4) {
  40. @unlink($tmpavatar);
  41. self::error('api_uploadavatar_unusable_image');
  42. }
  43. } else {
  44. @unlink($_FILES['Filedata']['tmp_name']);
  45. self::error('api_uploadavatar_service_unwritable');
  46. }
  47. $tmpavatarbig = './temp/upload'.$_G['uid'].'big'.$filetype;
  48. $tmpavatarmiddle = './temp/upload'.$_G['uid'].'middle'.$filetype;
  49. $tmpavatarsmall = './temp/upload'.$_G['uid'].'small'.$filetype;
  50. $image = new image;
  51. if($image->Thumb($tmpavatar, $tmpavatarbig, 200, 250, 1) <= 0) {
  52. self::error('api_uploadavatar_unusable_image');
  53. }
  54. if($image->Thumb($tmpavatar, $tmpavatarmiddle, 120, 120, 1) <= 0) {
  55. self::error('api_uploadavatar_unusable_image');
  56. }
  57. if($image->Thumb($tmpavatar, $tmpavatarsmall, 48, 48, 2) <= 0) {
  58. self::error('api_uploadavatar_unusable_image');
  59. }
  60. self::$tmpavatar = $tmpavatar;
  61. self::$tmpavatarbig = $avatarpath.$tmpavatarbig;
  62. self::$tmpavatarmiddle = $avatarpath.$tmpavatarmiddle;
  63. self::$tmpavatarsmall = $avatarpath.$tmpavatarsmall;
  64. }
  65. public static function output() {
  66. global $_G;
  67. if(!empty($_G['uid'])) {
  68. if (self::$tmpavatarbig && self::$tmpavatarmiddle && self::$tmpavatarsmall) {
  69. $avatar1 = self::byte2hex(file_get_contents(self::$tmpavatarbig));
  70. $avatar2 = self::byte2hex(file_get_contents(self::$tmpavatarmiddle));
  71. $avatar3 = self::byte2hex(file_get_contents(self::$tmpavatarsmall));
  72. $extra = '&avatar1='.$avatar1.'&avatar2='.$avatar2.'&avatar3='.$avatar3;
  73. $result = self::uc_api_post_ex('user', 'rectavatar', array('uid' => $_G['uid']), $extra);
  74. @unlink(self::$tmpavatar);
  75. @unlink(self::$tmpavatarbig);
  76. @unlink(self::$tmpavatarmiddle);
  77. @unlink(self::$tmpavatarsmall);
  78. if($result == '<?xml version="1.0" ?><root><face success="1"/></root>') {
  79. $variable = array(
  80. 'uploadavatar' => 'api_uploadavatar_success',
  81. );
  82. C::t('common_member')->update($_G['uid'], array('avatarstatus'=>'1'));
  83. mobile_core::result(mobile_core::variable($variable));
  84. } else {
  85. self::error('api_uploadavatar_uc_error');
  86. }
  87. }
  88. } else {
  89. self::error('api_uploadavatar_unavailable_user');
  90. }
  91. }
  92. public static function byte2hex($string) {
  93. $buffer = '';
  94. $value = unpack('H*', $string);
  95. $value = str_split($value[1], 2);
  96. $b = '';
  97. foreach($value as $k => $v) {
  98. $b .= strtoupper($v);
  99. }
  100. return $b;
  101. }
  102. public static function uc_api_post_ex($module, $action, $arg = array(), $extra = '') {
  103. $s = $sep = '';
  104. foreach($arg as $k => $v) {
  105. $k = urlencode($k);
  106. if(is_array($v)) {
  107. $s2 = $sep2 = '';
  108. foreach($v as $k2 => $v2) {
  109. $k2 = urlencode($k2);
  110. $s2 .= "$sep2{$k}[$k2]=".urlencode(uc_stripslashes($v2));
  111. $sep2 = '&';
  112. }
  113. $s .= $sep.$s2;
  114. } else {
  115. $s .= "$sep$k=".urlencode(uc_stripslashes($v));
  116. }
  117. $sep = '&';
  118. }
  119. $postdata = uc_api_requestdata($module, $action, $s, $extra);
  120. return uc_fopen2(UC_API.'/index.php', 500000, $postdata, '', TRUE, UC_IP, 20);
  121. }
  122. public static function error($errstr) {
  123. $variable = array(
  124. 'uploadavatar' => $errstr,
  125. );
  126. mobile_core::result(mobile_core::variable($variable));
  127. }
  128. }
  129. ?>