1<?php
2
3/*
4 * vim:set softtabstop=4 shiftwidth=4 expandtab:
5 *
6 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
7 * Copyright 2001 - 2020 Ampache.org
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 *
22 */
23
24declare(strict_types=0);
25
26namespace Ampache\Module\Api\Method;
27
28use Ampache\Config\AmpConfig;
29use Ampache\Repository\Model\Bookmark;
30use Ampache\Repository\Model\User;
31use Ampache\Module\Api\Api;
32use Ampache\Module\Api\Json_Data;
33use Ampache\Module\Api\Xml_Data;
34use Ampache\Module\System\Session;
35use Ampache\Module\Util\ObjectTypeToClassNameMapper;
36
37/**
38 * Class BookmarkEditMethod
39 * @package Lib\ApiMethods
40 */
41final class BookmarkEditMethod
42{
43    private const ACTION = 'bookmark_edit';
44
45    /**
46     * bookmark_edit
47     * MINIMUM_API_VERSION=5.0.0
48     *
49     * Edit a placeholder for the current media that you can return to later.
50     *
51     * @param array $input
52     * filter   = (string) object_id
53     * type     = (string) object_type ('song', 'video', 'podcast_episode')
54     * position = (integer) current track time in seconds
55     * client   = (string) Agent string Default: 'AmpacheAPI' // optional
56     * date     = (integer) UNIXTIME() //optional
57     * @return boolean
58     */
59    public static function bookmark_edit(array $input)
60    {
61        if (!Api::check_parameter($input, array('filter', 'position'), self::ACTION)) {
62            return false;
63        }
64        $user      = User::get_from_username(Session::username($input['auth']));
65        $object_id = $input['filter'];
66        $type      = $input['type'];
67        $position  = $input['position'];
68        $comment   = (isset($input['client'])) ? $input['client'] : 'AmpacheAPI';
69        $time      = (isset($input['date'])) ? (int) $input['date'] : time();
70        if (!AmpConfig::get('allow_video') && $type == 'video') {
71            Api::error(T_('Enable: video'), '4703', self::ACTION, 'system', $input['api_format']);
72
73            return false;
74        }
75        // confirm the correct data
76        if (!in_array($type, array('song', 'video', 'podcast_episode'))) {
77            /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
78            Api::error(T_('Bad Request'), '4710', self::ACTION, $type, $input['api_format']);
79
80            return false;
81        }
82        $className = ObjectTypeToClassNameMapper::map($type);
83
84        if ($className === $type || !$object_id) {
85            /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
86            Api::error(T_('Bad Request'), '4710', self::ACTION, $type, $input['api_format']);
87
88            return false;
89        }
90
91        $item = new $className($object_id);
92        if (!$item->id) {
93            /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
94            Api::error(sprintf(T_('Not Found: %s'), $object_id), '4704', self::ACTION, 'filter', $input['api_format']);
95
96            return false;
97        }
98        $object = array(
99            'object_id' => $object_id,
100            'object_type' => $type,
101            'comment' => $comment,
102            'position' => $position,
103        );
104
105        // check for the bookmark first
106        $bookmark = Bookmark::get_bookmark($object);
107        if (empty($bookmark)) {
108            Api::empty('bookmark', $input['api_format']);
109
110            return false;
111        }
112        // edit it
113        Bookmark::edit($object, $user->id, $time);
114
115        ob_end_clean();
116        switch ($input['api_format']) {
117            case 'json':
118                echo Json_Data::bookmarks($bookmark);
119                break;
120            default:
121                echo Xml_Data::bookmarks($bookmark);
122        }
123        Session::extend($input['auth']);
124
125        return true;
126    }
127}
128