2013-08-26 18:46:55 +04:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2013-08-27 14:50:21 +04:00
|
|
|
class OC_Core_Avatar_Controller {
|
2013-08-26 18:46:55 +04:00
|
|
|
public static function getAvatar($args) {
|
|
|
|
if (!\OC_User::isLoggedIn()) {
|
2013-08-27 14:50:21 +04:00
|
|
|
$l = new \OC_L10n('core');
|
2013-08-26 18:46:55 +04:00
|
|
|
header("HTTP/1.0 403 Forbidden");
|
2013-08-27 14:50:21 +04:00
|
|
|
\OC_Template::printErrorPage($l->t("Permission denied"));
|
2013-08-26 18:46:55 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = stripslashes($args['user']);
|
|
|
|
$size = (int)$args['size'];
|
|
|
|
if ($size > 2048) {
|
|
|
|
$size = 2048;
|
|
|
|
}
|
|
|
|
// Undefined size
|
|
|
|
elseif ($size === 0) {
|
|
|
|
$size = 64;
|
|
|
|
}
|
|
|
|
|
2013-08-31 20:27:28 +04:00
|
|
|
$avatar = new \OC_Avatar();
|
|
|
|
$image = $avatar->get($user, $size);
|
2013-08-26 18:46:55 +04:00
|
|
|
|
|
|
|
if ($image instanceof \OC_Image) {
|
|
|
|
$image->show();
|
|
|
|
} elseif ($image === false) {
|
2013-08-29 16:26:11 +04:00
|
|
|
\OC_JSON::success(array('user' => \OC_User::getDisplayName($user), 'size' => $size));
|
2013-08-26 18:46:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function postAvatar($args) {
|
|
|
|
$user = \OC_User::getUser();
|
|
|
|
|
|
|
|
if (isset($_POST['path'])) {
|
|
|
|
$path = stripslashes($_POST['path']);
|
2013-08-28 18:39:00 +04:00
|
|
|
$view = new \OC\Files\View('/'.$user.'/files');
|
2013-09-01 18:04:39 +04:00
|
|
|
$newAvatar = $view->file_get_contents($path);
|
2013-08-26 18:46:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($_FILES)) {
|
|
|
|
$files = $_FILES['files'];
|
2013-09-01 21:12:54 +04:00
|
|
|
if (
|
|
|
|
$files['error'][0] === 0 &&
|
|
|
|
is_uploaded_file($files['tmp_name'][0]) &&
|
|
|
|
!\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
|
|
|
|
) {
|
2013-09-01 18:04:39 +04:00
|
|
|
$newAvatar = file_get_contents($files['tmp_name'][0]);
|
2013-08-26 18:46:55 +04:00
|
|
|
unlink($files['tmp_name'][0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-09-01 18:04:39 +04:00
|
|
|
$avatar = new \OC_Avatar();
|
|
|
|
$avatar->set($user, $newAvatar);
|
2013-08-26 18:46:55 +04:00
|
|
|
\OC_JSON::success();
|
|
|
|
} catch (\OC\NotSquareException $e) {
|
2013-09-01 18:04:39 +04:00
|
|
|
$image = new \OC_Image($newAvatar);
|
2013-08-28 18:39:00 +04:00
|
|
|
|
2013-09-01 21:12:54 +04:00
|
|
|
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 (!$img->valid()) {
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("Invalid image")) ));
|
|
|
|
}
|
|
|
|
}
|
2013-08-26 18:46:55 +04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function deleteAvatar($args) {
|
|
|
|
$user = OC_User::getUser();
|
|
|
|
|
|
|
|
try {
|
2013-08-28 18:39:00 +04:00
|
|
|
$avatar = new \OC_Avatar();
|
|
|
|
$avatar->remove($user);
|
2013-08-26 18:46:55 +04:00
|
|
|
\OC_JSON::success();
|
|
|
|
} catch (\Exception $e) {
|
2013-09-01 21:12:54 +04:00
|
|
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
2013-08-26 18:46:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTmpAvatar($args) {
|
|
|
|
$user = OC_User::getUser();
|
2013-08-28 18:39:00 +04:00
|
|
|
|
2013-08-29 18:56:32 +04:00
|
|
|
$tmpavatar = \OC_Cache::get('tmpavatar');
|
|
|
|
if ($tmpavatar === false) {
|
2013-08-28 18:39:00 +04:00
|
|
|
\OC_JSON::error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-29 18:56:32 +04:00
|
|
|
$image = new \OC_Image($tmpavatar);
|
2013-08-28 18:39:00 +04:00
|
|
|
$image->show();
|
2013-08-26 18:46:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function postCroppedAvatar($args) {
|
|
|
|
$user = OC_User::getUser();
|
2013-09-01 21:12:54 +04:00
|
|
|
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;
|
|
|
|
}
|
2013-08-28 18:39:00 +04:00
|
|
|
|
2013-08-29 18:56:32 +04:00
|
|
|
$tmpavatar = \OC_Cache::get('tmpavatar');
|
|
|
|
if ($tmpavatar === false) {
|
2013-09-01 21:12:54 +04:00
|
|
|
$l = new \OC_L10n('core');
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("No temporary avatar available, try again")) ));
|
2013-08-28 18:39:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-29 18:56:32 +04:00
|
|
|
$image = new \OC_Image($tmpavatar);
|
2013-08-28 18:39:00 +04:00
|
|
|
$image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
|
|
|
|
try {
|
|
|
|
$avatar = new \OC_Avatar();
|
|
|
|
$avatar->set($user, $image->data());
|
|
|
|
// Clean up
|
2013-08-29 18:56:32 +04:00
|
|
|
\OC_Cache::remove('tmpavatar');
|
2013-08-28 18:39:00 +04:00
|
|
|
\OC_JSON::success();
|
2013-08-30 15:16:13 +04:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
|
|
|
}
|
2013-08-26 18:46:55 +04:00
|
|
|
}
|
|
|
|
}
|