summaryrefslogtreecommitdiffstats
path: root/core/avatar/controller.php
blob: c889385c21dba8c5c4d22ad24c91534a037d3baf (plain)
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
<?php
/**
 * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class OC_Core_Avatar_Controller {
	public static function getAvatar($args) {
		if (!\OC_User::isLoggedIn()) {
			$l = new \OC_L10n('core');
			header("HTTP/1.0 403 Forbidden");
			\OC_Template::printErrorPage($l->t("Permission denied"));
			return;
		}

		$user = stripslashes($args['user']);
		$size = (int)$args['size'];
		if ($size > 2048) {
			$size = 2048;
		}
		// Undefined size
		elseif ($size === 0) {
			$size = 64;
		}

		$ava = new \OC_Avatar();
		$image = $ava->get($user, $size);

		if ($image instanceof \OC_Image) {
			$image->show();
		} elseif ($image === false) {
			\OC_JSON::success(array('user' => $user, 'size' => $size));
		}
	}

	public static function postAvatar($args) {
		$user = \OC_User::getUser();

		if (isset($_POST['path'])) {
			$path = stripslashes($_POST['path']);
			$view = new \OC\Files\View('/'.$user.'/files');
			$avatar = $view->file_get_contents($path);
		}

		if (!empty($_FILES)) {
			$files = $_FILES['files'];
			if ($files['error'][0] === 0) {
				$avatar = file_get_contents($files['tmp_name'][0]);
				unlink($files['tmp_name'][0]);
			}
		}

		try {
			$ava = new \OC_Avatar();
			$ava->set($user, $avatar);
			\OC_JSON::success();
		} catch (\OC\NotSquareException $e) {
			$image = new \OC_Image($avatar);
			$ext = substr($image->mimeType(), -3);
			if ($ext === 'peg') {
				$ext = 'jpg';
			} elseif ($ext !== 'png') {
				\OC_JSON::error();
			}

			$view = new \OC\Files\View('/'.$user);
			$view->unlink('tmpavatar.png');
			$view->unlink('tmpavatar.jpg');
			$view->file_put_contents('tmpavatar.'.$ext, $image->data());
			\OC_JSON::error(array("data" => array("message" => "notsquare") ));
		} catch (\Exception $e) {
			\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
		}
	}

	public static function deleteAvatar($args) {
		$user = OC_User::getUser();

		try {
			$avatar = new \OC_Avatar();
			$avatar->remove($user);
			\OC_JSON::success();
		} catch (\Exception $e) {
			\OC_JSON::error(array("data" => array ("message" => $e->getMessage()) ));
		}
	}

	public static function getTmpAvatar($args) {
		// TODO deliver actual size here as well, so Jcrop can do its magic and we have the actual coordinates here again
		// TODO or don't have a size parameter and only resize client sided (looks promising)
		//
		// TODO make a cronjob that cleans up the tmpavatar after it's older than 2 hours, should be run every hour
		$user = OC_User::getUser();

		$view = new \OC\Files\View('/'.$user);
		if ($view->file_exists('tmpavatar.png')) {
			$ext = 'png';
		} elseif ($view->file_exists('tmpavatar.jpg')) {
			$ext = 'jpg';
		} else {
			\OC_JSON::error();
			return;
		}

		$image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext));
		$image->resize($args['size']);
		$image->show();
	}

	public static function postCroppedAvatar($args) {
		$user = OC_User::getUser();
		$view = new \OC\Files\View('/'.$user);
		$crop = $_POST['crop'];

		if ($view->file_exists('tmpavatar.png')) {
			$ext = 'png';
		} elseif ($view->file_exists('tmpavatar.jpg')) {
			$ext = 'jpg';
		} else {
			\OC_JSON::error();
			return;
		}

		$image = new \OC_Image($view->file_get_contents('tmpavatar.'.$ext));
		$image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
		try {
			$avatar = new \OC_Avatar();
			$avatar->set($user, $image->data());
			// Clean up
			$view->unlink('tmpavatar.png');
			$view->unlink('tmpavatar.jpg');
			\OC_JSON::success();
                } catch (\Exception $e) {
                        \OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
                }
	}
}