forked from tikiorg/tiki
/
tiki-pick_avatar.php
150 lines (142 loc) · 5.38 KB
/
tiki-pick_avatar.php
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
145
146
147
148
149
150
<?php
/**
* @package tikiwiki
*/
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
$section = 'mytiki';
require_once ('tiki-setup.php');
$userprefslib = TikiLib::lib('userprefs');
$imagegallib = TikiLib::lib('imagegal');
$access->check_feature('feature_userPreferences');
$access->check_user($user);
$auto_query_args = array('view_user');
if (!isset($_REQUEST["showall"])) $_REQUEST["showall"] = 'n';
$smarty->assign('showall', $_REQUEST["showall"]);
$userwatch = $user;
if (isset($_REQUEST["view_user"])) {
if ($_REQUEST["view_user"] <> $user) {
if ($tiki_p_admin == 'y') {
$userwatch = $_REQUEST["view_user"];
} else {
$smarty->assign('errortype', 401);
$smarty->assign('msg', tra("You do not have permission to view other users data"));
$smarty->display("error.tpl");
die;
}
} else {
$userwatch = $user;
}
}
$smarty->assign('userwatch', $userwatch);
// Upload avatar is processed here
if (isset($_FILES['userfile1']) && is_uploaded_file($_FILES['userfile1']['tmp_name'])) {
check_ticket('pick-avatar');
$type = $_FILES['userfile1']['type'];
$size = $_FILES['userfile1']['size'];
$name = $_FILES['userfile1']['name'];
$fp = fopen($_FILES['userfile1']['tmp_name'], "rb");
$data = fread($fp, filesize($_FILES['userfile1']['tmp_name']));
fclose($fp);
// Get image parameters
list($iwidth, $iheight, $itype, $iattr) = getimagesize($_FILES['userfile1']['tmp_name']);
$itype = image_type_to_mime_type($itype);
// Store full-size file gallery image if that is required
if ($prefs["user_store_file_gallery_picture"] == 'y') {
$fgImageId = $userprefslib->set_file_gallery_image($userwatch, $name, $size, $itype, $data);
}
// Store small avatar
if ($prefs['user_small_avatar_size']) {
$avsize = $prefs['user_small_avatar_size'];
} else {
$avsize = "45"; //default
}
if (($iwidth == $avsize and $iheight <= $avsize) || ($iwidth <= $avsize and $iheight == $avsize)) {
$userprefslib->set_user_avatar($userwatch, 'u', '', $name, $size, $itype, $data);
} else {
if (function_exists("ImageCreateFromString") && (!strstr($type, "gif"))) {
$img = imagecreatefromstring($data);
$size_x = imagesx($img);
$size_y = imagesy($img);
/* if the square crop is set, crop the image before resizing */
if ($prefs['user_small_avatar_square_crop']){
$crop_size = min ($size_x, $size_y);
$offset_x = ($size_x - $crop_size)/2;
$offset_y = ($size_y - $crop_size)/2;
$crop_array = array('x' =>$offset_x , 'y' => $offset_y, 'width' => $crop_size, 'height'=> $crop_size);
$img = imagecrop($img,$crop_array);
$size_x = $size_y = $crop_size;
}
if ($size_x > $size_y) $tscale = ((int)$size_x / $avsize);
else $tscale = ((int)$size_y / $avsize);
$tw = ((int)($size_x / $tscale));
$ty = ((int)($size_y / $tscale));
if ($tw > $size_x) $tw = $size_x;
if ($ty > $size_y) $ty = $size_y;
if (chkgd2()) {
$t = imagecreatetruecolor($tw, $ty);
imagecopyresampled($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
} else {
$t = imagecreate($tw, $ty);
$imagegallib->ImageCopyResampleBicubic($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
}
// CHECK IF THIS TEMP IS WRITEABLE OR CHANGE THE PATH TO A WRITEABLE DIRECTORY
$tmpfname = tempnam($prefs['tmpDir'], "TMPIMG");
imagejpeg($t, $tmpfname);
// Now read the information
$fp = fopen($tmpfname, "rb");
$t_data = fread($fp, filesize($tmpfname));
fclose($fp);
unlink($tmpfname);
$t_type = 'image/jpeg';
$userprefslib->set_user_avatar($userwatch, 'u', '', $name, $size, $t_type, $t_data);
} else {
$userprefslib->set_user_avatar($userwatch, 'u', '', $name, $size, $type, $data);
}
}
if ($prefs['feature_score'] == 'y') {
global $tikilib ,$user;
$userid = $tikilib->get_user_id($user);
$tikilib->score_event($user, 'avatar_added', "avatar:$userid");
}
/* redirect to prevent re-submit on page reload */
if ($tiki_p_admin == 'y' && $user !== $userwatch) {
header('Location: tiki-pick_avatar.php?view_user=' . $userwatch);
} else {
header('Location: tiki-pick_avatar.php');
}
exit;
}
if (isset($_REQUEST["uselib"])) {
check_ticket('pick-avatar');
$userprefslib->set_user_avatar($userwatch, 'l', $_REQUEST["avatar"], '', '', '', '');
}
if (isset($_REQUEST["reset"])) {
check_ticket('pick-avatar');
$userprefslib->set_user_avatar($userwatch, '0', '', '', '', '', '');
$userprefslib->remove_file_gallery_image($userwatch);
}
$avatars = array();
$h = opendir("img/avatars/");
while ($file = readdir($h)) {
if ($file != '.' && $file != '..' && $file != 'index.php' && substr($file, 0, 1) != "." && $file != "CVS" && $file != "README") {
$avatars[] = 'img/avatars/' . $file;
}
}
closedir($h);
$smarty->assign_by_ref('avatars', $avatars);
$smarty->assign('numav', count($avatars));
$smarty->assign('yours', rand(0, count($avatars)));
$avatar = $tikilib->get_user_avatar($userwatch);
$smarty->assign('avatar', $avatar);
// Get full user picture if it is set
if ($prefs["user_store_file_gallery_picture"] == 'y' && $user_picture_id = $userprefslib->get_user_picture_id($userwatch) ) {
$smarty->assign('user_picture_id', $user_picture_id);
}
ask_ticket('pick-avatar');
include_once ('tiki-mytiki_shared.php');
$smarty->assign('mid', 'tiki-pick_avatar.tpl');
$smarty->display("tiki.tpl");