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