3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Api\Friendica\Photo;
25 use Friendica\Core\ACL;
26 use Friendica\Core\L10n;
27 use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto;
28 use Friendica\Module\BaseApi;
29 use Friendica\Model\Photo;
30 use Friendica\Module\Api\ApiResponse;
31 use Friendica\Network\HTTPException;
32 use Friendica\Util\Profiler;
33 use Psr\Log\LoggerInterface;
36 * API endpoint: /api/friendica/photo/update
38 class Update extends BaseApi
40 /** @var FriendicaPhoto */
41 private $friendicaPhoto;
44 public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = [])
46 parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
48 $this->friendicaPhoto = $friendicaPhoto;
51 protected function post(array $request = [])
53 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
54 $uid = BaseApi::getCurrentUserID();
55 $type = $this->getRequestValue($this->parameters, 'extension', 'json');
58 $photo_id = $this->getRequestValue($request, 'photo_id');
59 $desc = $this->getRequestValue($request, 'desc');
60 $album = $this->getRequestValue($request, 'album');
61 $album_new = $this->getRequestValue($request, 'album_new');
62 $allow_cid = $this->getRequestValue($request, 'allow_cid');
63 $deny_cid = $this->getRequestValue($request, 'deny_cid');
64 $allow_gid = $this->getRequestValue($request, 'allow_gid');
65 $deny_gid = $this->getRequestValue($request, 'deny_gid');
67 // do several checks on input parameters
68 // we do not allow calls without album string
70 throw new HTTPException\BadRequestException('no albumname specified');
73 // check if photo is existing in database
74 if (!Photo::exists(['resource-id' => $photo_id, 'uid' => $uid, 'album' => $album])) {
75 throw new HTTPException\BadRequestException('photo not available');
78 // checks on acl strings provided by clients
79 $acl_input_error = false;
80 $acl_input_error |= !ACL::isValidContact($allow_cid, $uid);
81 $acl_input_error |= !ACL::isValidContact($deny_cid, $uid);
82 $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid);
83 $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid);
84 if ($acl_input_error) {
85 throw new HTTPException\BadRequestException('acl data invalid');
90 if (!is_null($desc)) {
91 $updated_fields['desc'] = $desc;
94 if (!is_null($album_new)) {
95 $updated_fields['album'] = $album_new;
98 if (!is_null($allow_cid)) {
99 $allow_cid = trim($allow_cid);
100 $updated_fields['allow_cid'] = $allow_cid;
103 if (!is_null($deny_cid)) {
104 $deny_cid = trim($deny_cid);
105 $updated_fields['deny_cid'] = $deny_cid;
108 if (!is_null($allow_gid)) {
109 $allow_gid = trim($allow_gid);
110 $updated_fields['allow_gid'] = $allow_gid;
113 if (!is_null($deny_gid)) {
114 $deny_gid = trim($deny_gid);
115 $updated_fields['deny_gid'] = $deny_gid;
119 if (count($updated_fields) > 0) {
120 $nothingtodo = false;
121 $result = Photo::update($updated_fields, ['uid' => $uid, 'resource-id' => $photo_id, 'album' => $album]);
126 if (!empty($_FILES['media'])) {
127 $nothingtodo = false;
128 $photo = Photo::upload($uid, $_FILES['media'], $album, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc, $photo_id);
129 if (!empty($photo)) {
130 $data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)];
131 $this->response->exit('photo_update', $data, $this->parameters['extension'] ?? null);
136 // return success of updating or error message
138 Photo::clearAlbumCache($uid);
139 $answer = ['result' => 'updated', 'message' => 'Image id `' . $photo_id . '` has been updated.'];
140 $this->response->exit('photo_update', ['$result' => $answer], $this->parameters['extension'] ?? null);
144 $answer = ['result' => 'cancelled', 'message' => 'Nothing to update for image id `' . $photo_id . '`.'];
145 $this->response->exit('photo_update', ['$result' => $answer], $this->parameters['extension'] ?? null);
148 throw new HTTPException\InternalServerErrorException('unknown error - update photo entry in database failed');
151 throw new HTTPException\InternalServerErrorException('unknown error - this error on uploading or updating a photo should never happen');