summaryrefslogtreecommitdiffstats
path: root/core/avatar/controller.php
blob: 8f1d6a570693db06c8289a0289ec422932c17b7c (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
<?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);

		if ($image instanceof \OC_Image) {
			$image->show();
		} elseif ($image === false) {
			\OC_JSON::success(array('user' => \OC_User::getDisplayName($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);
		}

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

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

			\OC_Cache::set('tmpavatar', $image->data(), 7200);
			\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) {
		$user = OC_User::getUser();

		$tmpavatar = \OC_Cache::get('tmpavatar');
		if ($tmpavatar === false) {
			\OC_JSON::error();
			return;
		}

		$image = new \OC_Image($tmpavatar);
		$image->show();
	}

	public static function postCroppedAvatar($args) {
		$user = OC_User::getUser();
		$crop = $_POST['crop'];

		$tmpavatar = \OC_Cache::get('tmpavatar');
		if ($tmpavatar === false) {
			\OC_JSON::error();
			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()) ));
		}
	}
}