]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Friendica/Photo/Create.php
Some standards
[friendica.git] / src / Module / Api / Friendica / Photo / Create.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Friendica\Photo;
23
24 use Friendica\Core\ACL;
25 use Friendica\DI;
26 use Friendica\Model\Photo;
27 use Friendica\Module\BaseApi;
28 use Friendica\Network\HTTPException;
29
30 /**
31  * API endpoint: /api/friendica/photo/create
32  */
33 class Create extends BaseApi
34 {
35         protected function post(array $request = [])
36         {
37                 BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE);
38                 $uid  = BaseApi::getCurrentUserID();
39                 $type = $this->parameters['extension'] ?? '';
40
41                 // input params
42                 $desc      = $_REQUEST['desc']      ?? null;
43                 $album     = $_REQUEST['album']     ?? null;
44                 $allow_cid = $_REQUEST['allow_cid'] ?? null;
45                 $deny_cid  = $_REQUEST['deny_cid' ] ?? null;
46                 $allow_gid = $_REQUEST['allow_gid'] ?? null;
47                 $deny_gid  = $_REQUEST['deny_gid' ] ?? null;
48
49                 // do several checks on input parameters
50                 // we do not allow calls without album string
51                 if ($album == null) {
52                         throw new HTTPException\BadRequestException('no albumname specified');
53                 }
54
55                 // error if no media posted in create-mode
56                 if (empty($_FILES['media'])) {
57                         // Output error
58                         throw new HTTPException\BadRequestException('no media data submitted');
59                 }
60
61                 // checks on acl strings provided by clients
62                 $acl_input_error = false;
63                 $acl_input_error |= !ACL::isValidContact($allow_cid, $uid);
64                 $acl_input_error |= !ACL::isValidContact($deny_cid, $uid);
65                 $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid);
66                 $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid);
67                 if ($acl_input_error) {
68                         throw new HTTPException\BadRequestException('acl data invalid');
69                 }
70                 // now let's upload the new media in create-mode
71                 $photo = Photo::upload($uid, $_FILES['media'], $album, trim($allow_cid), trim($allow_gid), trim($deny_cid), trim($deny_gid), $desc);
72
73                 // return success of updating or error message
74                 if (!empty($photo)) {
75                         $data = ['photo' => DI::friendicaPhoto()->createFromId($photo['resource_id'], null, $uid, $type)];
76                         $this->response->exit('photo_create', $data, $this->parameters['extension'] ?? null);
77                         return;
78                 } else {
79                         throw new HTTPException\InternalServerErrorException('unknown error - uploading photo failed, see Friendica log for more information');
80                 }
81         }
82 }