1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8// this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
10	header('location: index.php');
11	exit;
12}
13
14
15/**
16 * This class bundles several avatar-related functions
17 *
18 * todo: It would be good to get existing avatar functionality and bring it into this lib.
19 *
20 * @author patrick-proulx
21 * @since 15.0
22 */
23class AvatarLib extends TikiLib
24{
25
26	/**
27	 * sets the avatar from a given image file's URL
28	 *
29	 * @return string	URL for the current page
30	 */
31	function set_avatar_from_url($url, $userwatch = "", $name = "")
32	{
33		global $user, $prefs;
34
35		$access = TikiLib::lib('access');
36		$access->check_feature('feature_userPreferences');
37		$access->check_user($user);
38
39		$userprefslib = TikiLib::lib('userprefs');
40		$imagegallib = TikiLib::lib('imagegal');
41
42		if (empty($userwatch)) {
43			$userwatch = $user;
44		}
45
46		$data = file_get_contents($url);
47		list($iwidth, $iheight, $itype, $iattr) = getimagesize($url);
48		$itype = image_type_to_mime_type($itype);
49
50		// Get proper file size of image
51		$imgdata = get_headers($url, true);
52		if (isset($imgdata['Content-Length'])) {
53			# Return file size
54			$size = (int) $imgdata['Content-Length'];
55		}
56
57		// Store full-size file gallery image if that is required
58		if ($prefs["user_store_file_gallery_picture"] == 'y') {
59			$fgImageId = $userprefslib->set_file_gallery_image($userwatch, $name, $size, $itype, $data);
60		}
61
62		// Store small avatar
63		if ($prefs['user_small_avatar_size']) {
64			$avsize = $prefs['user_small_avatar_size'];
65		} else {
66			$avsize = "45"; //default
67		}
68
69		if (($iwidth == $avsize and $iheight <= $avsize) || ($iwidth <= $avsize and $iheight == $avsize)) {
70			$userprefslib->set_user_avatar($userwatch, 'u', '', $name, $size, $itype, $data);
71		} else {
72			if (function_exists("ImageCreateFromString") && (! strstr($type, "gif"))) {
73				$img = imagecreatefromstring($data);
74				$size_x = imagesx($img);
75				$size_y = imagesy($img);
76				/* if the square crop is set, crop the image before resizing */
77				if ($prefs['user_small_avatar_square_crop']) {
78					$crop_size = min($size_x, $size_y);
79					$offset_x = ($size_x - $crop_size) / 2;
80					$offset_y = ($size_y - $crop_size) / 2;
81					$crop_array = ['x' => $offset_x , 'y' => $offset_y, 'width' => $crop_size, 'height' => $crop_size];
82					$img = imagecrop($img, $crop_array);
83					$size_x = $size_y = $crop_size;
84				}
85				if ($size_x > $size_y) {
86					$tscale = ((int)$size_x / $avsize);
87				} else {
88					$tscale = ((int)$size_y / $avsize);
89				}
90				$tw = ((int)($size_x / $tscale));
91				$ty = ((int)($size_y / $tscale));
92				if ($tw > $size_x) {
93					$tw = $size_x;
94				}
95				if ($ty > $size_y) {
96					$ty = $size_y;
97				}
98				if (chkgd2()) {
99					$t = imagecreatetruecolor($tw, $ty);
100					// trick to have a transparent background for png instead of black
101					imagesavealpha($t, true);
102					$trans_colour = imagecolorallocatealpha($t, 0, 0, 0, 127);
103					imagefill($t, 0, 0, $trans_colour);
104					imagecopyresampled($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
105				} else {
106					$t = imagecreate($tw, $ty);
107					$imagegallib->ImageCopyResampleBicubic($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
108				}
109				// CHECK IF THIS TEMP IS WRITEABLE OR CHANGE THE PATH TO A WRITEABLE DIRECTORY
110				$tmpfname = tempnam($prefs['tmpDir'], "TMPIMG");
111				imagepng($t, $tmpfname);
112				// Now read the information
113				$fp = fopen($tmpfname, "rb");
114				$t_data = fread($fp, filesize($tmpfname));
115				fclose($fp);
116				unlink($tmpfname);
117				$t_type = 'image/png';
118				$userprefslib->set_user_avatar($userwatch, 'u', '', $name, $size, $t_type, $t_data);
119			} else {
120				$userprefslib->set_user_avatar($userwatch, 'u', '', $name, $size, $type, $data);
121			}
122		}
123		TikiLib::events()->trigger(
124			'tiki.user.avatar',
125			[
126				'type' => 'user',
127				'object' => $userwatch,
128				'user' => $userwatch,
129			]
130		);
131	}
132}
133