2013-08-26 14:46:55 +00: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-09-04 20:22:56 +00:00
|
|
|
namespace OC\Core\Avatar;
|
|
|
|
|
|
|
|
class Controller {
|
2013-08-26 14:46:55 +00:00
|
|
|
public static function getAvatar($args) {
|
2013-09-04 22:04:31 +00:00
|
|
|
\OC_JSON::checkLoggedIn();
|
|
|
|
\OC_JSON::callCheck();
|
2013-08-26 14:46:55 +00:00
|
|
|
|
|
|
|
$user = stripslashes($args['user']);
|
|
|
|
$size = (int)$args['size'];
|
|
|
|
if ($size > 2048) {
|
|
|
|
$size = 2048;
|
|
|
|
}
|
|
|
|
// Undefined size
|
|
|
|
elseif ($size === 0) {
|
|
|
|
$size = 64;
|
|
|
|
}
|
|
|
|
|
2013-09-09 14:57:46 +00:00
|
|
|
$avatar = new \OC_Avatar($user);
|
|
|
|
$image = $avatar->get($size);
|
2013-08-26 14:46:55 +00:00
|
|
|
|
2013-09-01 19:57:28 +00:00
|
|
|
\OC_Response::disableCaching();
|
2013-09-01 21:25:50 +00:00
|
|
|
\OC_Response::setLastModifiedHeader(time());
|
2013-08-26 14:46:55 +00:00
|
|
|
if ($image instanceof \OC_Image) {
|
2013-09-01 21:15:45 +00:00
|
|
|
\OC_Response::setETagHeader(crc32($image->data()));
|
2013-08-26 14:46:55 +00:00
|
|
|
$image->show();
|
2013-09-04 20:13:59 +00:00
|
|
|
} else {
|
2013-09-11 14:04:41 +00:00
|
|
|
// Signalizes $.avatar() to display a defaultavatar
|
2013-09-05 21:12:52 +00:00
|
|
|
\OC_JSON::success();
|
2013-08-26 14:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function postAvatar($args) {
|
2013-09-04 22:04:31 +00:00
|
|
|
\OC_JSON::checkLoggedIn();
|
|
|
|
\OC_JSON::callCheck();
|
|
|
|
|
2013-08-26 14:46:55 +00:00
|
|
|
$user = \OC_User::getUser();
|
|
|
|
|
|
|
|
if (isset($_POST['path'])) {
|
|
|
|
$path = stripslashes($_POST['path']);
|
2013-08-28 14:39:00 +00:00
|
|
|
$view = new \OC\Files\View('/'.$user.'/files');
|
2013-09-01 14:04:39 +00:00
|
|
|
$newAvatar = $view->file_get_contents($path);
|
2013-09-04 20:13:59 +00:00
|
|
|
} elseif (!empty($_FILES)) {
|
2013-08-26 14:46:55 +00:00
|
|
|
$files = $_FILES['files'];
|
2013-09-01 17:12:54 +00:00
|
|
|
if (
|
|
|
|
$files['error'][0] === 0 &&
|
|
|
|
is_uploaded_file($files['tmp_name'][0]) &&
|
|
|
|
!\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
|
|
|
|
) {
|
2013-09-01 14:04:39 +00:00
|
|
|
$newAvatar = file_get_contents($files['tmp_name'][0]);
|
2013-08-26 14:46:55 +00:00
|
|
|
unlink($files['tmp_name'][0]);
|
|
|
|
}
|
2013-09-04 20:13:59 +00:00
|
|
|
} else {
|
|
|
|
$l = new \OC_L10n('core');
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("No image or file provided")) ));
|
|
|
|
return;
|
2013-08-26 14:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2013-09-09 14:57:46 +00:00
|
|
|
$avatar = new \OC_Avatar($user);
|
|
|
|
$avatar->set($newAvatar);
|
2013-08-26 14:46:55 +00:00
|
|
|
\OC_JSON::success();
|
|
|
|
} catch (\OC\NotSquareException $e) {
|
2013-09-01 14:04:39 +00:00
|
|
|
$image = new \OC_Image($newAvatar);
|
2013-08-28 14:39:00 +00:00
|
|
|
|
2013-09-01 17:12:54 +00:00
|
|
|
if ($image->valid()) {
|
|
|
|
\OC_Cache::set('tmpavatar', $image->data(), 7200);
|
2013-09-06 11:46:50 +00:00
|
|
|
\OC_JSON::error(array("data" => "notsquare"));
|
2013-09-01 17:12:54 +00:00
|
|
|
} else {
|
|
|
|
$l = new \OC_L10n('core');
|
2013-09-05 21:12:52 +00:00
|
|
|
|
|
|
|
$mimeType = $image->mimeType();
|
|
|
|
if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
|
2013-09-01 17:12:54 +00:00
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("Unknown filetype")) ));
|
|
|
|
}
|
|
|
|
|
2013-09-04 19:22:36 +00:00
|
|
|
if (!$image->valid()) {
|
2013-09-01 17:12:54 +00:00
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("Invalid image")) ));
|
|
|
|
}
|
|
|
|
}
|
2013-08-26 14:46:55 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function deleteAvatar($args) {
|
2013-09-04 22:04:31 +00:00
|
|
|
\OC_JSON::checkLoggedIn();
|
|
|
|
\OC_JSON::callCheck();
|
|
|
|
|
2013-09-04 20:22:56 +00:00
|
|
|
$user = \OC_User::getUser();
|
2013-08-26 14:46:55 +00:00
|
|
|
|
|
|
|
try {
|
2013-09-09 14:57:46 +00:00
|
|
|
$avatar = new \OC_Avatar($user);
|
|
|
|
$avatar->remove();
|
2013-08-26 14:46:55 +00:00
|
|
|
\OC_JSON::success();
|
|
|
|
} catch (\Exception $e) {
|
2013-09-01 17:12:54 +00:00
|
|
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
2013-08-26 14:46:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getTmpAvatar($args) {
|
2013-09-04 22:04:31 +00:00
|
|
|
\OC_JSON::checkLoggedIn();
|
|
|
|
\OC_JSON::callCheck();
|
|
|
|
|
2013-08-29 14:56:32 +00:00
|
|
|
$tmpavatar = \OC_Cache::get('tmpavatar');
|
2013-09-01 19:17:48 +00:00
|
|
|
if (is_null($tmpavatar)) {
|
|
|
|
$l = new \OC_L10n('core');
|
2013-09-02 15:07:38 +00:00
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
|
2013-08-28 14:39:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:56:32 +00:00
|
|
|
$image = new \OC_Image($tmpavatar);
|
2013-09-01 19:57:28 +00:00
|
|
|
\OC_Response::disableCaching();
|
2013-09-01 21:25:50 +00:00
|
|
|
\OC_Response::setLastModifiedHeader(time());
|
2013-09-01 21:07:38 +00:00
|
|
|
\OC_Response::setETagHeader(crc32($image->data()));
|
2013-08-28 14:39:00 +00:00
|
|
|
$image->show();
|
2013-08-26 14:46:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function postCroppedAvatar($args) {
|
2013-09-04 22:04:31 +00:00
|
|
|
\OC_JSON::checkLoggedIn();
|
|
|
|
\OC_JSON::callCheck();
|
|
|
|
|
2013-09-04 20:22:56 +00:00
|
|
|
$user = \OC_User::getUser();
|
2013-09-01 17:12:54 +00: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 14:39:00 +00:00
|
|
|
|
2013-08-29 14:56:32 +00:00
|
|
|
$tmpavatar = \OC_Cache::get('tmpavatar');
|
2013-09-01 19:17:48 +00:00
|
|
|
if (is_null($tmpavatar)) {
|
2013-09-01 17:12:54 +00:00
|
|
|
$l = new \OC_L10n('core');
|
2013-09-02 15:07:38 +00:00
|
|
|
\OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")) ));
|
2013-08-28 14:39:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:56:32 +00:00
|
|
|
$image = new \OC_Image($tmpavatar);
|
2013-08-28 14:39:00 +00:00
|
|
|
$image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
|
|
|
|
try {
|
2013-09-09 14:57:46 +00:00
|
|
|
$avatar = new \OC_Avatar($user);
|
2013-09-14 12:15:52 +00:00
|
|
|
$avatar->set($image->data());
|
2013-08-28 14:39:00 +00:00
|
|
|
// Clean up
|
2013-08-29 14:56:32 +00:00
|
|
|
\OC_Cache::remove('tmpavatar');
|
2013-08-28 14:39:00 +00:00
|
|
|
\OC_JSON::success();
|
2013-08-30 11:16:13 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
\OC_JSON::error(array("data" => array("message" => $e->getMessage()) ));
|
|
|
|
}
|
2013-08-26 14:46:55 +00:00
|
|
|
}
|
|
|
|
}
|