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