aboutsummaryrefslogtreecommitdiffstats
path: root/core/avatar/controller.php
blob: d0edde506476eb42febd8af3955d9bb680792637 (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
140
141
142
143
144
145
146
147
148
149
<?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;
		}

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

		\OC_Response::disableCaching();
		\OC_Response::setLastModifiedHeader(time());
		if ($image instanceof \OC_Image) {
			\OC_Response::setETagHeader(crc32($image->data()));
			$image->show();
		} else {
			\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');
			$newAvatar = $view->file_get_contents($path);
		} elseif (!empty($_FILES)) {
			$files = $_FILES['files'];
			if (
				$files['error'][0] === 0 &&
				is_uploaded_file($files['tmp_name'][0]) &&
				!\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
			) {
				$newAvatar = file_get_contents($files['tmp_name'][0]);
				unlink($files['tmp_name'][0]);
			}
		} else {
			$l = new \OC_L10n('core');
			\OC_JSON::error(array("data" => array("message" => $l->t("No image or file provided")) ));
			return;
		}

		try {
			$avatar = new \OC_Avatar();
			$avatar->set($user, $newAvatar);
			\OC_JSON::success();
		} catch (\OC\NotSquareException $e) {
			$image = new \OC_Image($newAvatar);

			if ($image->valid()) {
				\OC_Cache::set('tmpavatar', $image->data(), 7200);
				\OC_JSON::error(array("data" => array("message" => "notsquare") ));
			} else {
				$l = new \OC_L10n('core');
				$type = substr($image->mimeType(), -3);
				if ($type === 'peg') {
					$type = 'jpg';
				}
				if ($type !== 'jpg' && $type !== 'png') {
					\OC_JSON::error(array("data" => array("message" => $l->t("Unknown filetype")) ));
				}

				if (!$image->valid()) {
					\OC_JSON::error(array("data" => array("message" => $l->t("Invalid image")) ));
				}
			}
		} 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) {
		$tmpavatar = \OC_Cache::get('tmpavatar');
		if (is_null($tmpavatar)) {
			$l = new \OC_L10n('core');
			\OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
			return;
		}

		$image = new \OC_Image($tmpavatar);
		\OC_Response::disableCaching();
		\OC_Response::setLastModifiedHeader(time());
		\OC_Response::setETagHeader(crc32($image->data()));
		$image->show();
	}

	public static function postCroppedAvatar($args) {
		$user = OC_User::getUser();
		if (isset($_POST['crop'])) {
			$crop = $_POST['crop'];
		} else {
			$l = new \OC_L10n('core');
			\OC_JSON::error(array("data" => array("message" => $l->t("No crop data provided")) ));
			return;
		}

		$tmpavatar = \OC_Cache::get('tmpavatar');
		if (is_null($tmpavatar)) {
			$l = new \OC_L10n('core');
			\OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
			return;
		}

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