1
0
Fork 0
mirror of https://github.com/Yetangitu/ampache synced 2025-10-03 09:49:30 +02:00
ampache/lib/class/plex_api.class.php
2013-11-25 08:31:29 +01:00

325 lines
11 KiB
PHP

<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright 2001 - 2013 Ampache.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
require_once Config::get('prefix') . '/modules/plugins/helper.php';
/**
* Plex Class
*
* This class wrap Ampache to Plex API library functions. See http://wiki.plexapp.com/index.php/HTTP_API
* These are all static calls.
*
*/
class Plex_Api
{
/**
* constructor
* This really isn't anything to do here, so it's private
*/
private function __construct()
{
}
public static function setHeader($f)
{
header("HTTP/1.0 200 OK", true, 200);
header("Connection: close", true);
header("X-Plex-Protocol: 1.0");
header_remove("x-powered-by");
if (strtolower($f) == "xml") {
header("Cache-Control: no-cache", true);
header("Content-type: text/xml; charset=" . Config::get('site_charset'), true);
} elseif (substr(strtolower($f), 0, 6) == "image/") {
header("Cache-Control: public, max-age=604800", true);
header("Content-type: " . $f, true);
} else {
header("Content-type: " . $f, true);
}
}
public static function apiOutput($string)
{
ob_start('ob_gzhandler');
echo $string;
ob_end_flush();
header("X-Plex-Compressed-Content-Length: " . ob_get_length());
header("X-Plex-Original-Content-Length: " . strlen($string));
}
public static function createError($code)
{
$error = "";
switch ($code) {
case 404:
$error = "Not Found";
break;
}
header("Content-type: text/html", true);
header("HTTP/1.0 ". $code . " " . $error, true, $code);
$html = "<html><head><title>" . $error . "</title></head><body><h1>" . $code . " " . $error . "</h1></body></html>";
self::apiOutput($html);
exit();
}
public static function root()
{
$r = Plex_XML_Data::createContainer();
Plex_XML_Data::setRootContent($r, Catalog::get_catalogs());
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function library($params)
{
$r = Plex_XML_Data::createLibContainer();
Plex_XML_Data::setLibraryContent($r);
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function system($params)
{
$r = Plex_XML_Data::createSysContainer();
Plex_XML_Data::setSystemContent($r);
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function clients($params)
{
$r = Plex_XML_Data::createContainer();
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function channels($params)
{
$r = Plex_XML_Data::createContainer();
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function photos($params)
{
$r = Plex_XML_Data::createPluginContainer();
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function photo($params)
{
if (count($params) == 2) {
if ($params[0] == ':' && $params[1] == 'transcode') {
$width = $_REQUEST['width'];
$height = $_REQUEST['height'];
$url = $_REQUEST['url'];
if ($width && $height && $url) {
$response = PluginHelper::wsGet($url);
if ($response['status'] == 200) {
$mime = $response['headers']['Content-Type'];
self::setHeader($mime);
$art = new Art(0);
$art->raw = $response['body'];
$thumb = $art->generate_thumb($art->raw, array('width' => $width, 'height' => $height), $mime);
echo $thumb['thumb'];
exit();
}
}
}
}
}
public static function music($params)
{
$r = Plex_XML_Data::createPluginContainer();
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function video($params)
{
$r = Plex_XML_Data::createPluginContainer();
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function applications($params)
{
$r = Plex_XML_Data::createPluginContainer();
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function library_sections($params)
{
$r = Plex_XML_Data::createLibContainer();
$n = count($params);
if ($n == 0) {
Plex_XML_Data::setSections($r, Catalog::get_catalogs());
} else {
$key = $params[0];
$catalog = Catalog::create_from_id($key);
if (!$catalog) {
self::createError(404);
}
if ($n == 1) {
Plex_XML_Data::setSectionContent($r, $catalog);
} elseif ($n == 2) {
$view = $params[1];
if ($view == "all") {
Plex_XML_Data::setSectionAll($r, $catalog);
} elseif ($view == "albums") {
Plex_XML_Data::setSectionAlbums($r, $catalog);
} elseif ($view == "recentlyadded") {
Plex_XML_Data::setCustomSectionView($r, $catalog, Stats::get_recent('album', 25, 0, $key));
}
}
}
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function library_metadata($params)
{
$r = Plex_XML_Data::createLibContainer();
$n = count($params);
if ($n > 0) {
$key = $params[0];
$id = Plex_XML_Data::getAmpacheId($key);
if ($n == 1) {
if (Plex_XML_Data::isArtist($key)) {
$artist = new Artist($id);
$artist->format();
Plex_XML_Data::addArtist($r, $artist);
} elseif (Plex_XML_Data::isAlbum($key)) {
$album = new Album($id);
$album->format();
Plex_XML_Data::addAlbum($r, $album);
}
} else {
$subact = $params[1];
if ($subact == "children") {
if (Plex_XML_Data::isArtist($key)) {
$artist = new Artist($id);
$artist->format();
Plex_XML_Data::setArtistRoot($r, $artist);
} else if (Plex_XML_Data::isAlbum($key)) {
$album = new Album($id);
$album->format();
Plex_XML_Data::setAlbumRoot($r, $album);
} else if (Plex_XML_Data::isTrack($key)) {
/*$song = new Song($id);
$song->format();
Plex_XML_Data::setSongRoot($r, $song);*/
}
} elseif ($subact == "thumb") {
if ($n == 3) {
// Ignore thumb id as we can only have 1 thumb
$art = null;
if (Plex_XML_Data::isArtist($key)) {
$art = new Art($id, "artist");
} else if (Plex_XML_Data::isAlbum($key)) {
$art = new Art($id, "album");
} else if (Plex_XML_Data::isSong($key)) {
$art = new Art($id, "song");
}
if ($art != null) {
$art->get_db();
if (!$size) {
self::setHeader($art->raw_mime);
echo $art->raw;
} else {
$dim = array();
$dim['width'] = $size;
$dim['height'] = $size;
$thumb = $art->get_thumb($dim);
self::setHeader($art->thumb_mime);
echo $thumb['thumb'];
}
exit();
}
}
}
}
}
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function library_parts($params)
{
$n = count($params);
if ($n == 2) {
$key = $params[0];
$file = $params[1];
$id = Plex_XML_Data::getAmpacheId($key);
$song = new Song($id);
if ($song->id) {
$url = Song::play_url($id);
header("Location: " . $url);
} else {
self::createError(404);
}
}
}
public static function library_recentlyadded($params)
{
$data = array();
$data['album'] = Stats::get_newest('album', 25);
$r = Plex_XML_Data::createLibContainer();
Plex_XML_Data::setCustomView($r, $data);
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function library_ondeck($params)
{
$data = array();
$data['album'] = Stats::get_recent('album', 25);
$r = Plex_XML_Data::createLibContainer();
Plex_XML_Data::setCustomView($r, $data);
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
public static function system_library_sections($params)
{
$r = Plex_XML_Data::createSysContainer();
Plex_XML_Data::setSysSections($r, Catalog::get_catalogs());
Plex_XML_Data::setContainerSize($r);
self::apiOutput($r->asXML());
}
}