1<?php 2/* 3 * vim:set softtabstop=4 shiftwidth=4 expandtab: 4 * 5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 6 * Copyright 2001 - 2020 Ampache.org 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Affero General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Affero General Public License for more details. 17 * 18 * You should have received a copy of the GNU Affero General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 * 21 */ 22 23declare(strict_types=0); 24 25namespace Ampache\Module\Api\Method; 26 27use Ampache\Config\AmpConfig; 28use Ampache\Repository\Model\User; 29use Ampache\Repository\Model\Userflag; 30use Ampache\Module\Api\Api; 31use Ampache\Module\System\Session; 32use Ampache\Module\Util\ObjectTypeToClassNameMapper; 33 34/** 35 * Class FlagMethod 36 * @package Lib\ApiMethods 37 */ 38final class FlagMethod 39{ 40 private const ACTION = 'flag'; 41 42 /** 43 * flag 44 * MINIMUM_API_VERSION=400001 45 * 46 * This flags a library item as a favorite 47 * Setting flag to true (1) will set the flag 48 * Setting flag to false (0) will remove the flag 49 * 50 * @param array $input 51 * type = (string) 'song', 'album', 'artist', 'playlist', 'podcast', 'podcast_episode', 'video', 'tvshow', 'tvshow_season' $type 52 * id = (integer) $object_id 53 * flag = (integer) 0,1 $flag 54 * @return boolean 55 */ 56 public static function flag(array $input) 57 { 58 if (!AmpConfig::get('userflags')) { 59 Api::error(T_('Enable: userflags'), '4703', self::ACTION, 'system', $input['api_format']); 60 61 return false; 62 } 63 if (!Api::check_parameter($input, array('type', 'id', 'flag'), self::ACTION)) { 64 return false; 65 } 66 ob_end_clean(); 67 $type = (string) $input['type']; 68 $object_id = (int) $input['id']; 69 $flag = (bool) $input['flag']; 70 $user = User::get_from_username(Session::username($input['auth'])); 71 $user_id = null; 72 if ((int) $user->id > 0) { 73 $user_id = $user->id; 74 } 75 // confirm the correct data 76 if (!in_array($type, array('song', 'album', 'artist', 'playlist', 'podcast', 'podcast_episode', 'video', 'tvshow', 'tvshow_season'))) { 77 Api::error(sprintf(T_('Bad Request: %s'), $type), '4710', self::ACTION, 'type', $input['api_format']); 78 79 return false; 80 } 81 82 $className = ObjectTypeToClassNameMapper::map($type); 83 84 if (!$className || !$object_id) { 85 Api::error(sprintf(T_('Bad Request: %s'), $type), '4710', self::ACTION, 'type', $input['api_format']); 86 } else { 87 $item = new $className($object_id); 88 if (!$item->id) { 89 /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */ 90 Api::error(sprintf(T_('Not Found: %s'), $object_id), '4704', self::ACTION, 'id', $input['api_format']); 91 92 return false; 93 } 94 $userflag = new Userflag($object_id, $type); 95 if ($userflag->set_flag($flag, $user_id)) { 96 $message = ($flag) ? 'flag ADDED to ' : 'flag REMOVED from '; 97 Api::message($message . $object_id, $input['api_format']); 98 99 return true; 100 } 101 Api::error('flag failed ' . $object_id, '4710', self::ACTION, 'system', $input['api_format']); 102 } 103 Session::extend($input['auth']); 104 105 return true; 106 } 107} 108