提交 43172057 authored 作者: Mathieu Parent's avatar Mathieu Parent

Initial implementation of cisco-xml libs and MediaBrotha

Subject to major changes and more possibly lack of time
上级 3a79e26e
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of XML services for Cisco IP Phones.
XML services for Cisco IP Phones 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, either
version 3 of the License, or (at your option) any later version.
XML services for Cisco IP Phones 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 XML services for Cisco IP Phones. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
//* Debug
ini_set('error_reporting', E_ALL | E_STRUCT);
ini_set('display_errors', 1);
// */
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.'../lib');
require('Cisco-XML/Cisco-XML.php');
require_once('MediaBrotha/Core.php');
MediaBrotha_Core::init();
MediaBrotha_Core::connectFrontend('CiscoXML', Array('push_url' => 'http://cisco:cisco@192.168.0.220/CGI/Execute'));
MediaBrotha_Core::loadBackend('FileSystem', Array('base_path' => '/home/share/music/'));
MediaBrotha_Core::loadBackend('VLC', Array('http_intf' => 'http://127.0.0.1:8080'));
MediaBrotha_Core::go();
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of XML services for Cisco IP Phones.
XML services for Cisco IP Phones 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, either
version 3 of the License, or (at your option) any later version.
XML services for Cisco IP Phones 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 XML services for Cisco IP Phones. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class CiscoXMLField {
private $_allowed_fields = NULL;
private $_xml = NULL;
public function __construct($tag_name, $value = NULL, $parent = NULL, $allowed_fields = Array()) {
$this->_allowed_fields = $allowed_fields;
if (is_a($parent, 'DOMDocument')) {
$doc = $parent;
} else {
$doc = $parent->ownerDocument;
}
$this->_xml = $doc->createElement($tag_name);
if (is_array($value)) {
$this->setCiscoElements($value);
} else {
$this->_xml->nodeValue = htmlspecialchars($value);
}
$parent->appendChild($this->_xml);
}
public function setCiscoElements($args) {
foreach ($args as $k => $v) {
$this->setCiscoElement($k, $v);
}
}
public function setCiscoElement($name, $value) {
$short_name = preg_replace('/(:[^:]+)$/','', $name);
if (!array_key_exists($short_name, $this->_allowed_fields))
return;
return new CiscoXMLField($short_name, $value, $this->_xml, $this->_allowed_fields[$short_name]);
}
}
class CiscoXMLObject {
protected $_allowed_fields = NULL;
private $_xml = NULL;
private $_root = NULL;
/*
* Constructor
*/
public function __construct($args = Array()) {
if (!$this->_allowed_fields) {
die("Error: Unknow CiscoXMLObject '".get_class($this)."'");
}
$this->addAllowedFields();
$this->_xml = new DOMDocument('1.0', 'utf-8');
$this->_root = new CiscoXMLField(get_class($this), NULL, $this->_xml, $this->_allowed_fields);
$this->_root->setCiscoElements($args);
}
protected function addAllowedFields() {
switch(get_class($this)) {
case 'CiscoIPPhoneStatus':
case 'CiscoIPPhoneExecute':
break;
default:
$this->_allowed_fields['SoftKeyItem'] = Array(
'Name' => true,
'URL' => true,
'URLDown' => true,
'Position' => true,
);
}
}
/*
* Private methods
*/
/*
* Public methods
*/
public function __toString() {
return $this->_xml->saveXML();
}
public function setCiscoElement($name, $value) {
return $this->_root->setCiscoElement($name, $value);
}
public function setCiscoElements($args) {
$this->_root->setCiscoElements($args);
}
static public function HttpHeader() {
Header('Content-Type: text/xml');
header("Connection: close");
header("Expires: -1");
}
public function execute($url) {
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => "XML=".urlencode($this)
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
}
class CiscoIPPhoneMenu extends CiscoXMLObject {
protected $_allowed_fields = Array(
'Title' => true,
'Prompt' => true,
'MenuItem' => Array(
'Name' => true,
'URL' => true),
);
}
class CiscoIPPhoneText extends CiscoXMLObject {
protected $_allowed_fields = Array(
'Title' => true,
'Prompt' => true,
'Text' => true,
);
}
class CiscoIPPhoneInput extends CiscoXMLObject {
protected $_allowed_fields = Array(
'Title' => true,
'Prompt' => true,
'URL' => true,
'InputItem' => Array(
'DisplayName' => true,
'QueryStringParam' => true,
'DefaultValue' => true,
'InputFlags' => true),
);
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
require_once('MediaBrotha/Media.php');
class MediaBrotha_Backend extends MediaBrotha_Media implements Iterator {
protected $_buffer = Array();
public function __construct($args = Array()) {
parent::__construct($args);
$this->register();
$this->setMimeType('application/x-mediabrotha-backend-'.strtolower($this->getName()));
MediaBrotha_Core::registerMimeType($this, 'application/x-mediabrotha-backend-'.strtolower($this->getName()));
$this->setMetadata('name', $this->getName());
$this->setMetadata('uri', 'MediaBrotha_Backend://'.$this->getName());
}
public function register() {
MediaBrotha_Core::registerBackend($this);
}
public function getName() {
return preg_replace('/^MediaBrotha_Backend_(.*)$/', '$1', get_class($this));
}
public function capabilities($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
return Array();
}
public function isHidden() {
return parent::isHidden() || !in_array('browse', $this->capabilities());
}
// Capability browse
public function fetch($uri) {
return false;
}
public function rewind() {
reset($this->_buffer);
}
public function current() {
return current($this->_buffer);
}
public function key() {
return key($this->_buffer);
}
public function next() {
return next($this->_buffer);
}
public function valid() {
$key = key($this->_buffer);
$var = ($key !== NULL && $key !== FALSE);
return $var;
}
// Capability play
public function play($media) {
return false;
}
public function pause() {
return false;
}
public function stop() {
return false;
}
// Capability playlist
public function playlistEnqueue($media) {
return false;
}
public function playlistNext($media) {
return false;
}
public function playlistPrevious($media) {
return false;
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class MediaBrotha_Backend_FileSystem extends MediaBrotha_Backend {
public function register() {
parent::register();
MediaBrotha_Core::registerMimeType($this, 'application/x-directory');
}
public function isURISafe($uri) {
$real_path = realpath(parse_url($uri, PHP_URL_PATH));
$real_base_path = realpath($this->getMetadata('base_path'));
do {
if ($real_path === $real_base_path) {
return true;
}
} while(($real_path != dirname($real_path)) && $real_path = dirname($real_path));
return false;
}
public function fetch($uri) {
if ((parse_url($uri, PHP_URL_SCHEME) != 'file') || !$this->isURISafe($uri)) {
$uri = 'file://'.$this->getMetadata('base_path');
}
$this->_buffer = new DirectoryIterator(parse_url($uri, PHP_URL_PATH));
return true;
}
public function capabilities($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
return Array(
'browse',
);
}
// Iterator
public function mediaFromFile($file) {
if ($file) {
$media = new MediaBrotha_Media(Array(
'uri' => 'file://'.realpath($file->getPathname()),
'name' => $file->getFilename(),
'hidden' => !$file->isReadable() || preg_match('/^\.([^.]|$)/', $file->getFilename()),
));
if ($file->isDir()) {
$media->setMimeType('application/x-directory');
} else {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$media->setMimeType($finfo->file($file->getPathname()));
$media->setMimeEncoding($finfo->file($file->getPathname(), FILEINFO_MIME_ENCODING));
}
return $media;
}
}
public function current() {
return $this->mediaFromFile($this->_buffer->current());
}
public function next() {
return $this->mediaFromFile($this->_buffer->next());
}
public function valid() {
return $this->_buffer->valid();
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class MediaBrotha_Backend_Root extends MediaBrotha_Backend {
public function fetch($uri) {
$this->_buffer = Array();
foreach(MediaBrotha_Core::getBackends() as $id => $backend) {
$this->_buffer[] = $backend;
}
return true;
}
public function capabilities($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
return Array(
'browse',
);
}
public function isHidden() {
return true;
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class MediaBrotha_Backend_VLC extends MediaBrotha_Backend {
public function register() {
parent::register();
MediaBrotha_Core::registerMimeType($this, 'audio/mpeg');
}
public function fetch($uri) {
$this->_buffer[] = new MediaBrotha_Media();
return true;
}
public function capabilities($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
return Array(
'play',
'playlist',
);
}
private function send_command($command, $args = Array()) {
$url = $this->getMetadata('http_intf').
'/requests/status.xml'.
'?command='.$command;
foreach ($args as $k => $v) {
$url.= '&'.$k.'='.rawurlencode($v);
}
return fopen($url, 'r');
}
// Capability play
public function play($media) {
return $this->send_command('in_play', Array('input' => $media->getURI()));
}
public function pause() {
return $this->send_command('pl_pause');
}
public function stop() {
return $this->send_command('pl_stop');
}
// Capability playlist
public function playlistEnqueue($media) {
return $this->send_command('in_enqueue', Array('input' => $media->getURI()));
}
public function playlistNext($media) {
return $this->send_command('pl_next');
}
public function playlistPrevious($media) {
return $this->send_command('pl_previous');
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
require_once('MediaBrotha/Backend.php');
require_once('MediaBrotha/Frontend.php');
require_once('MediaBrotha/Media.php');
class MediaBrotha_Core {
private static $_frontend = NULL;
private static $_backends = Array();
private static $_mime_types = Array();
private static $_media = NULL;
public static function init() {
MediaBrotha_Core::loadBackend('Root');
}
public static function go() {
$current_backend = MediaBrotha_Core::getCurrentBackend();
switch(MediaBrotha_Core::getAction()) {
case 'play':
$current_backend->play(MediaBrotha_Core::getCurrentMedia());
break;
case 'pause':
$current_backend->pause(MediaBrotha_Core::getCurrentMedia());
break;
case 'stop':
$current_backend->stop(MediaBrotha_Core::getCurrentMedia());
break;
case 'pl_enqueue':
$current_backend->playlistEnqueue(MediaBrotha_Core::getCurrentMedia());
break;
case 'pl_next':
$current_backend->playlistNext(MediaBrotha_Core::getCurrentMedia());
break;
case 'pl_previous':
$current_backend->playlistPrevious(MediaBrotha_Core::getCurrentMedia());
break;
case 'browse':
default:
$current_backend->fetch(MediaBrotha_Core::getCurrentURI());
MediaBrotha_Core::$_frontend->begin($current_backend);
foreach($current_backend as $item) {
if ($item->isHidden()) {
continue;
}
MediaBrotha_Core::$_frontend->addItem($item);
}
MediaBrotha_Core::$_frontend->finish($current_backend);
MediaBrotha_Core::$_frontend->render();
}
}
/*
* Backends
*/
public static function loadBackend($provider_name, $args = Array()) {
$class_name = "MediaBrotha_Backend_${provider_name}";
if (!class_exists($class_name)) {
require_once("MediaBrotha/Backend/${provider_name}.php");
}
return new $class_name($args);
}
public static function registerBackend($backend) {
MediaBrotha_Core::$_backends[] = $backend;
}
public static function registerMimeType($backend, $mime_type) {
MediaBrotha_Core::$_mime_types[$mime_type][] = $backend;
}
public static function getBackends() {
return MediaBrotha_Core::$_backends;
}
public static function getCurrentBackend() {
if (!empty($_GET['mime_type']) && !empty(MediaBrotha_Core::$_mime_types[$_GET['mime_type']])) {
return MediaBrotha_Core::$_mime_types[$_GET['mime_type']][0];
} else {
return MediaBrotha_Core::$_backends[0];
}
}
/*
* Frontend
*/
public static function connectFrontend($provider_name, $args = Array()) {
if (!$provider_name) {
return;
}
$args['provider'] = $provider_name;
$class_name = "MediaBrotha_Frontend_$provider_name";
if (!class_exists($class_name)) {
require_once("MediaBrotha/Frontend/${provider_name}.php");
}
MediaBrotha_Core::$_frontend = new $class_name($args);
}
/*
* ...
*/
public static function getAction() {
if (!empty($_GET['action'])) {
return $_GET['action'];
} else {
return 'browse';
}
}
public static function getCurrentURI() {
if (!empty($_GET['uri'])) {
return $_GET['uri'];
}
}
public static function getCurrentMedia() {
if (empty(MediaBrotha_Core::$_media)) {
MediaBrotha_Core::$_media = new MediaBrotha_Media();
MediaBrotha_Core::$_media->setURI(MediaBrotha_Core::getCurrentURI());
}
return MediaBrotha_Core::$_media;
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class MediaBrotha_Frontend {
protected $_browser = NULL;
protected $_infos = NULL;
public function __construct($browser, $args = Array()) {
$this->_browser = $browser;
$this->_infos = $args;
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class MediaBrotha_Frontend_CiscoXML extends MediaBrotha_Frontend {
private $_xml = NULL;
public function begin($item) {
$this->_xml = new CiscoIPPhoneMenu();
$this->_xml->setCiscoElement('Title', $item->getDisplayName());
//$this->_xml->setCiscoElement('Prompt', 'kk');
}
public function addItem($item) {
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'?'.
'mime_type='.urlencode($item->getMimeType()).'&'.
($item->getMimeEncoding() ? ('mime_encoding='.urlencode($item->getMimeEncoding()).'&') : '').
'uri='.urlencode($item->getURI());
//$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
$this->_xml->setCiscoElement('MenuItem',
Array('Name' => $item->getDisplayName(), 'URL' => $url));
}
public function finish() {
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Select', 'URL' => 'SoftKey:Select', 'Position' => '1'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Play', 'URL' => 'QueryStringParam:action=play', 'Position' => '2'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Pause', 'URL' => 'QueryStringParam:action=pause', 'Position' => '3'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Stop', 'URL' => 'QueryStringParam:action=stop', 'Position' => '4'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Enqueue', 'URL' => 'QueryStringParam:action=pl_enqueue', 'Position' => '5'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Enqueue', 'URL' => 'QueryStringParam:action=pl_next', 'Position' => '6'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Enqueue', 'URL' => 'QueryStringParam:action=pl_previous', 'Position' => '7'));
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => 'Quit', 'URL' => 'SoftKey:Exit', 'Position' => '8'));
}
public function render() {
CiscoXMLObject::HttpHeader();
print $this->_xml;
//$this->_xml->execute($this->_infos['push_url']);
}
}
<?php
/*
Copyright 2011 Mathieu Parent <math.parent@gmail.com>
This file is part of MediaBrotha.
MediaBrotha 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, either
version 3 of the License, or (at your option) any later version.
MediaBrotha 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 MediaBrotha. If not, see
<http://www.gnu.org/licenses/>.
*/
/**
* @author Mathieu Parent
*/
class MediaBrotha_Media {
private $_metadata = Array();
private $_mimeType = NULL;
private $_mimeEncoding = NULL;
public function __construct($metadata = Array()) {
$this->_metadata = $metadata;
}
public function setMimeType($mime_type) {
$this->_mimeType = $mime_type;
}
public function getMimeType() {
// TODO
return $this->_mimeType;
}
public function setMimeEncoding($mime_encoding) {
$this->_mimeEncoding = $mime_encoding;
}
public function getMimeEncoding() {
// TODO
return $this->_mimeEncoding;
}
public function getMetadata($name = NULL) {
if ($name === NULL) {
return $this->_metadata;
} elseif (isset($this->_metadata[$name])) {
return $this->_metadata[$name];
} else {
return NULL;
}
}
public function setMetadata($name, $value) {
if (is_array($name)) {
$this->_metadata = $name;
} else {
$this->_metadata[$name] = $value;
}
}
/* Most used metadata */
public function getDisplayName() {
return $this->getMetadata('name');
}
public function getURI() {
return $this->getMetadata('uri');
}
public function setURI($value) {
return $this->setMetadata('uri', $value);
}
public function getURIComponent($component) {
return parse_url($this->getURI($component));
}
public function isHidden() {
return isset($this->_metadata['hidden']) && $this->_metadata['hidden'];
}
}
/*
Mandatory metadata:
uri
name
Usual metadata:
artist
album
*/
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论