via: 9ecdde39b5fb94c4708b530a7c90e82900ff15ed (commit)
from: 69415c934e4b3dc70b22816aa847706336c442f5 (commit)
= COMMIT LOG ===========================================================
committer: Mathieu Parent
comments:
MediaBrotha: various enhancements
- add a HTML frontend
- frontend choosen automaticaly depending on browser
- handling of parent media
- max length for metadata and display_name
diff --git a/sathieu/cisco-xml/browser/index.php b/sathieu/cisco-xml/browser/index.php
index d142ff5..137b409 100644
--- a/sathieu/cisco-xml/browser/index.php
+++ b/sathieu/cisco-xml/browser/index.php
@@ -35,7 +35,11 @@ require_once('MediaBrotha/Core.php');
// Init
MediaBrotha_Core::init();
-MediaBrotha_Core::connectFrontend('CiscoXML', Array('push_url' => 'http://cisco:cisco@192.168.0.220/CGI/Execute'));
+if (CiscoIPPhone::userAgentIsCiscoIPPhone()) {
+ MediaBrotha_Core::connectFrontend('CiscoXML', Array('push_url' => 'http://cisco:cisco@192.168.0.220/CGI/Execute'));
+} else {
+ MediaBrotha_Core::connectFrontend('HTML');
+}
// Backends
MediaBrotha_Core::loadBackend('FileSystem', Array('base_path' => '/home/share/music/'));
@@ -51,10 +55,9 @@ $ldap_connect_config = Array(
MediaBrotha_Core::loadBackend('LDAP', Array('ldap_connect_config' => $ldap_connect_config));
// Roots
-MediaBrotha_Core::addRootMedia('file:///home/share/music/', Array('name'=> 'Fichiers'));
-MediaBrotha_Core::addRootMedia('ldap:///', Array('name'=> 'Annuaire'));
+MediaBrotha_Core::addRootMedia('file:///home/share/music/', Array('display_name'=> 'Fichiers'));
+MediaBrotha_Core::addRootMedia('ldap:///', Array('display_name'=> 'Annuaire'));
MediaBrotha_Core::go();
-
diff --git a/sathieu/cisco-xml/lib/Cisco-XML/Cisco-XML.php b/sathieu/cisco-xml/lib/Cisco-XML/Cisco-XML.php
index c711f9c..216aad6 100644
--- a/sathieu/cisco-xml/lib/Cisco-XML/Cisco-XML.php
+++ b/sathieu/cisco-xml/lib/Cisco-XML/Cisco-XML.php
@@ -23,6 +23,12 @@ This file is part of XML services for Cisco IP Phones.
/**
* @author Mathieu Parent
*/
+class CiscoIPPhone {
+ static public function userAgentIsCiscoIPPhone() {
+ return preg_match('/^Allegro-Software-WebClient\//', $_SERVER['HTTP_USER_AGENT']);
+
+ }
+}
class CiscoXMLField {
private $_allowed_fields = NULL;
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Backend.php b/sathieu/cisco-xml/lib/MediaBrotha/Backend.php
index 84a6a89..28a8441 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Backend.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Backend.php
@@ -71,12 +71,17 @@ class MediaBrotha_Backend {
}
// Actions
- public function getMediaActions($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
+ public function getMediaActions(MediaBrotha_Media $media) {
return Array();
}
public function doMediaAction($action, MediaBrotha_Media $media) {
return false;
}
+
+ // Populate
+ public function populateMedia(MediaBrotha_Media $media) {
+ }
+
}
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Backend/FileSystem.php b/sathieu/cisco-xml/lib/MediaBrotha/Backend/FileSystem.php
index 39928b4..006814b 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Backend/FileSystem.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Backend/FileSystem.php
@@ -40,10 +40,13 @@ class MediaBrotha_Backend_FileSystem extends MediaBrotha_Backend {
// Browsing
public function mediaFromBufferItem($file) {
if ($file) {
+ if ($file->getFilename() === '..') {
+ return NULL;
+ }
$media = new MediaBrotha_Media(
'file://'.realpath($file->getPathname()),
Array(
- 'name' => $file->getFilename(),
+ 'display_name' => $file->getFilename(),
'hidden' => !$file->isReadable() || preg_match('/^\.([^.]|$)/', $file->getFilename()),
)
);
@@ -59,31 +62,47 @@ class MediaBrotha_Backend_FileSystem extends MediaBrotha_Backend {
}
public function fetch(MediaBrotha_Media $media) {
- $uri =$media->getURI();
+ $uri = $media->getURI();
if ((parse_url($uri, PHP_URL_SCHEME) != 'file') || !$this->isURISafe($uri)) {
- $uri = 'file://'.$this->getMetadata('base_path');
+ $uri = 'file://'.$this->getParam('base_path');
}
$path = parse_url($uri, PHP_URL_PATH);
if (is_dir($path)) {
return new MediaBrotha_MediaIterator($this, $media, new DirectoryIterator($path));
} else {
- $this->setURI(NULL);
return false;
}
}
// Actions
- public function getMediaActions($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
- if ($mime_type === 'application/x-directory') {
- return Array(
- 'browse',
- );
- } elseif ($uri && (substr($uri, 0, 7) === 'file://')) {
+ protected function _isHandled(MediaBrotha_Media $media) {
+ return ($media->getMimeType() === 'application/x-directory')
+ || (substr($media->getURI(), 0, 7) === 'file://');
+ }
+
+ public function getMediaActions(MediaBrotha_Media $media) {
+ if ($this->_isHandled($media)) {
return Array(
'browse',
);
} else {
- return parent::getMediaActions($uri, $mime_type, $mime_encoding);
+ return parent::getMediaActions($media);
+ }
+ }
+
+ // Populate
+ public function populateMedia(MediaBrotha_Media $media) {
+ if ($this->_isHandled($media)) {
+ $parent_path = 'file://'.dirname(parse_url($media->getURI(), PHP_URL_PATH));
+ if ($this->isURISafe($parent_path)) {
+ $media->setParent(
+ new MediaBrotha_Media($parent_path,
+ Array(
+ 'display_name' => '..',
+ )
+ )
+ );
+ }
}
}
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Backend/LDAP.php b/sathieu/cisco-xml/lib/MediaBrotha/Backend/LDAP.php
index c882dd7..365e78e 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Backend/LDAP.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Backend/LDAP.php
@@ -54,7 +54,7 @@ class MediaBrotha_Backend_LDAP extends MediaBrotha_Backend {
$media = new MediaBrotha_Media(
'ldap:///'.$entry->dn(),
Array(
- 'name' => $entry->dn(), //$entry->getValue('sn', 'single'),
+ 'display_name' => $entry->dn(), //$entry->getValue('sn', 'single'),
),
'text/directory'
//$finfo->file($file->getPathname(), FILEINFO_MIME_ENCODING);
@@ -79,17 +79,18 @@ class MediaBrotha_Backend_LDAP extends MediaBrotha_Backend {
return true;
}
// Actions
- public function getMediaActions($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
- if ($mime_type === 'text/directory') {
- return Array(
- 'browse',
- );
- } elseif ($uri && (substr($uri, 0, 7) === 'ldap://')) {
+ protected function _isHandled(MediaBrotha_Media $media) {
+ return ($media->getMimeType() === 'text/directory')
+ || (substr($media->getURI(), 0, 7) === 'ldap://');
+ }
+
+ public function getMediaActions(MediaBrotha_Media $media) {
+ if ($this->_isHandled($media)) {
return Array(
'browse',
);
} else {
- return parent::getMediaActions($uri, $mime_type, $mime_encoding);
+ return parent::getMediaActions($media);
}
}
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Backend/Root.php b/sathieu/cisco-xml/lib/MediaBrotha/Backend/Root.php
index addba42..9616be8 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Backend/Root.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Backend/Root.php
@@ -30,11 +30,18 @@ class MediaBrotha_Backend_Root extends MediaBrotha_Backend {
public function __construct($params = NULL) {
parent::__construct($params);
- $this->_media = new MediaBrotha_Media('MediaBrotha:///');
+ $this->_media = new MediaBrotha_Media('MediaBrotha:///',
+ Array(
+ 'display_name' => 'Root',
+ )
+ );
$this->_media_iterator = new MediaBrotha_MediaIterator($this, $this->_media);
}
- public function addRootMedia($media) {
+ public function addRootMedia($URI, array $metadata = Array(), $mime_type = NULL, $mime_encoding = NULL) {
+ $this->addRootMediaObj(new MediaBrotha_Media($URI, $metadata, $mime_type,$mime_encoding));
+ }
+ public function addRootMediaObj($media) {
$this->_media_iterator->bufferAdd($media);
}
@@ -42,13 +49,6 @@ class MediaBrotha_Backend_Root extends MediaBrotha_Backend {
public function fetch(MediaBrotha_Media $media) {
return $this->_media_iterator;
}
-
- // Actions
- public function getMediaActions($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
- return Array(
- );
- }
-
}
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Backend/VLC.php b/sathieu/cisco-xml/lib/MediaBrotha/Backend/VLC.php
index 2b4e7c6..8c3208e 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Backend/VLC.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Backend/VLC.php
@@ -50,17 +50,13 @@ class MediaBrotha_Backend_VLC extends MediaBrotha_Backend {
}
// Actions
- public function getMediaActions($uri = NULL, $mime_type = NULL, $mime_encoding = NULL) {
- if (in_array($mime_type, Array($this::$_supported_mime_types))) {
- return Array(
- 'play_media',
- 'pause_media',
- 'stop_media',
- 'enqueue_media',
- 'next_media',
- 'previous_media',
- );
- } elseif ($uri && (substr($uri, 0, 7) === 'file://')) {
+ protected function _isHandled(MediaBrotha_Media $media) {
+ return in_array($media->getMimeType(), Array($this::$_supported_mime_types))
+ || (substr($media->getURI(), 0, 7) === 'file://');
+ }
+
+ public function getMediaActions(MediaBrotha_Media $media) {
+ if ($this->_isHandled($media)) {
return Array(
'play_media',
'pause_media',
@@ -70,7 +66,7 @@ class MediaBrotha_Backend_VLC extends MediaBrotha_Backend {
'previous_media',
);
} else {
- return parent::getMediaActions($uri, $mime_type, $mime_encoding);
+ return parent::getMediaActions($media);
}
}
public function doMediaAction($action, MediaBrotha_Media $media) {
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Core.php b/sathieu/cisco-xml/lib/MediaBrotha/Core.php
index 5ec90e8..8997a7c 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Core.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Core.php
@@ -34,47 +34,17 @@ class MediaBrotha_Core {
private static $_backends = Array();
private static $_media = NULL;
+ /*
+ * Core
+ */
public static function init() {
if (!session_start()) {
die('Unable to start session');
}
- if (!isset($_SESSION['HASHES'])) {
- $_SESSION['HASHES'] = Array();
- }
- if (isset($_SERVER["QUERY_STRING"])) {
- $tmp = explode('&', $_SERVER["QUERY_STRING"]);
- foreach ($tmp as $h) {
- if (preg_match('/^[a-z0-9]+$/', $h)) {
- if ($s = MediaBrotha_Core::hash2string($h)) {
- parse_str($s, $output);
- $_GET = array_merge($_GET, $output);
- }
- }
- }
- }
+ MediaBrotha_Core::_unhashRequest();
MediaBrotha_Core::loadBackend('Root');
}
- public static function hash2string($h) {
- if (isset($_SESSION['HASHES'][$h])) {
- return $_SESSION['HASHES'][$h];
- } else {
- //print 'NO string for hash '.$h;
- return NULL;
- }
- }
-
- public static function string2hash($s) {
- for($hash_length = 1 ; $hash_length <=41 ; $hash_length++) {
- $h = substr(sha1($s), 0, $hash_length);
- if (!isset($_SESSION['HASHES'][$h]) || ($_SESSION['HASHES'][$h] === $s)) {
- break;
- }
- }
- $_SESSION['HASHES'][$h] = $s;
- return $h;
- }
-
public static function go() {
$current_backend = MediaBrotha_Core::getCurrentBackend();
$current_media = MediaBrotha_Core::getCurrentMedia();
@@ -82,7 +52,7 @@ class MediaBrotha_Core {
if ($media_iterator = $current_backend->fetch($current_media)) {
MediaBrotha_Core::$_frontend->begin($current_media);
foreach ($media_iterator as $item) {
- if ($item->isHidden()) {
+ if (($item === NULL) || $item->isHidden()) {
continue;
}
MediaBrotha_Core::$_frontend->addItem($item);
@@ -97,10 +67,9 @@ class MediaBrotha_Core {
}
public function addRootMedia($URI, array $metadata = Array(), $mime_type = NULL, $mime_encoding = NULL) {
- MediaBrotha_Core::$_backends['Root']->addRootMedia(
- new MediaBrotha_Media($URI, $metadata, $mime_type,$mime_encoding)
- );
+ MediaBrotha_Core::$_backends['Root']->addRootMedia($URI, $metadata, $mime_type, $mime_encoding);
}
+
/*
* Backends
*/
@@ -120,22 +89,7 @@ class MediaBrotha_Core {
public static function getBackends() {
return MediaBrotha_Core::$_backends;
}
- public static function getCurrentBackend() {
- if (!empty($_GET['backend']) && !empty(MediaBrotha_Core::$_backends[$_GET['backend']])) {
- return MediaBrotha_Core::$_backends[$_GET['backend']];
- }
- foreach (MediaBrotha_Core::getBackends() as $backend) {
- foreach ($backend->getMediaActions(MediaBrotha_Core::getCurrentURI()) as $action) {
- if ($backend->getBackendName() == 'Root') {
- continue;
- }
- if ($action === MediaBrotha_Core::getAction()) {
- return $backend;
- }
- }
- }
- return MediaBrotha_Core::$_backends['Root'];
- }
+
/*
* Frontend
*/
@@ -151,8 +105,26 @@ class MediaBrotha_Core {
MediaBrotha_Core::$_frontend = new $class_name($args);
}
/*
- * ...
+ * Current *
*/
+ public static function getCurrentBackend() {
+ if (!empty($_GET['backend']) && !empty(MediaBrotha_Core::$_backends[$_GET['backend']])) {
+ return MediaBrotha_Core::$_backends[$_GET['backend']];
+ }
+ $current_media = MediaBrotha_Core::getCurrentMedia();
+ foreach (MediaBrotha_Core::getBackends() as $backend) {
+ foreach ($backend->getMediaActions($current_media) as $action) {
+ if ($backend->getBackendName() == 'Root') {
+ continue;
+ }
+ if ($action === MediaBrotha_Core::getAction()) {
+ return $backend;
+ }
+ }
+ }
+ return MediaBrotha_Core::$_backends['Root'];
+ }
+
public static function getAction() {
if (!empty($_GET['action'])) {
return $_GET['action'];
@@ -161,17 +133,63 @@ class MediaBrotha_Core {
}
}
- public static function getCurrentURI() {
- if (!empty($_GET['uri'])) {
- return (string) $_GET['uri'];
- }
- }
public static function getCurrentMedia() {
if (empty(MediaBrotha_Core::$_media)) {
- MediaBrotha_Core::$_media = new MediaBrotha_Media(MediaBrotha_Core::getCurrentURI());
+ if (!empty($_GET['uri'])) {
+ MediaBrotha_Core::$_media = new MediaBrotha_Media($_GET['uri']);
+ } else {
+ MediaBrotha_Core::$_media = new MediaBrotha_Media('MediaBrotha:///');
+ }
+ foreach (MediaBrotha_Core::getBackends() as $backend) {
+ $backend->populateMedia(MediaBrotha_Core::$_media);
+ }
}
return MediaBrotha_Core::$_media;
}
+ /*
+ * helper functions
+ */
+ /* hashes */
+ public static function hash2value($h) {
+ if (isset($_SESSION['HASHES'][$h])) {
+ return $_SESSION['HASHES'][$h];
+ } else {
+ return NULL;
+ }
+ }
+
+ public static function value2hash($v) {
+ for($hash_length = 1 ; $hash_length <=41 ; $hash_length++) {
+ $h = substr(sha1($v), 0, $hash_length);
+ if (!isset($_SESSION['HASHES'][$h]) || ($_SESSION['HASHES'][$h] === $v)) {
+ break;
+ }
+ }
+ $_SESSION['HASHES'][$h] = $v;
+ return $h;
+ }
+
+ public static function hash2array($h) {
+ if ($v = MediaBrotha_Core::hash2value($h)) {
+ parse_str($v, $output);
+ return $output;
+ }
+ }
+
+ private static function _unhashRequest() {
+ if (!isset($_SESSION['HASHES'])) {
+ $_SESSION['HASHES'] = Array();
+ }
+ if (isset($_SERVER["QUERY_STRING"])) {
+ $tmp = explode('&', $_SERVER["QUERY_STRING"]);
+ foreach ($tmp as $h) {
+ if (preg_match('/^[a-z0-9]+$/', $h)) {
+ $_GET = array_merge($_GET, MediaBrotha_Core::hash2array($h));
+ }
+ }
+ }
+ }
+
}
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Frontend/CiscoXML.php b/sathieu/cisco-xml/lib/MediaBrotha/Frontend/CiscoXML.php
index 2baa6af..914e6ee 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Frontend/CiscoXML.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Frontend/CiscoXML.php
@@ -30,12 +30,15 @@ class MediaBrotha_Frontend_CiscoXML extends MediaBrotha_Frontend_HTTP {
private $_xml = NULL;
public function begin($item) {
$this->_xml = new CiscoIPPhoneMenu();
- $this->_xml->setCiscoElement('Title', $item->getDisplayName());
+ $this->_xml->setCiscoElement('Title', $item->getDisplayName(32));
$this->_xml->setCiscoElement('Prompt', substr($item->getURI(), -32));
+ if ($parent = $item->getParent()) {
+ $this->addItem($parent);
+ }
}
public function addItem($item) {
$url = $this->rootURL().'?'.
- MediaBrotha_Core::string2hash(
+ MediaBrotha_Core::value2hash(
'mime_type='.urlencode($item->getMimeType()).'&'.
($item->getMimeEncoding() ? ('mime_encoding='.urlencode($item->getMimeEncoding()).'&') : '').
'uri='.urlencode($item->getURI()));
@@ -49,10 +52,10 @@ class MediaBrotha_Frontend_CiscoXML extends MediaBrotha_Frontend_HTTP {
'URL' => 'SoftKey:Select',
'Position' => $pos++));
foreach (MediaBrotha_Core::getBackends() as $backend) {
- foreach ($backend->getMediaActions(MediaBrotha_Core::getCurrentURI()) as $action) {
+ foreach ($backend->getMediaActions(MediaBrotha_Core::getCurrentMedia()) as $action) {
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => $action,
- 'URL' => 'QueryStringParam:'.MediaBrotha_Core::string2hash('action='.$action.'&backend='.$backend->getBackendName()),
+ 'URL' => 'QueryStringParam:'.MediaBrotha_Core::value2hash('action='.$action.'&backend='.$backend->getBackendName()),
'Position' => $pos++));
}
}
diff --git a/sathieu/cisco-xml/lib/MediaBrotha/Media.php b/sathieu/cisco-xml/lib/MediaBrotha/Media.php
index 6efd1db..7964473 100644
--- a/sathieu/cisco-xml/lib/MediaBrotha/Media.php
+++ b/sathieu/cisco-xml/lib/MediaBrotha/Media.php
@@ -25,14 +25,15 @@ This file is part of MediaBrotha.
*/
class MediaBrotha_Media {
- private $_metadata = Array();
-
private $_URI = NULL;
+ private $_metadata = Array();
private $_mimeType = NULL;
private $_mimeEncoding = NULL;
+ private $_parent = NULL;
+
public function __construct($URI, array $metadata = Array(), $mime_type = NULL, $mime_encoding = NULL) {
- $this->setURI($URI);
+ $this->_URI = $URI;
$this->setMetadata($metadata);
$this->setMimeType($mime_type);
$this->setMimeEncoding($mime_encoding);
@@ -42,16 +43,17 @@ class MediaBrotha_Media {
public function getURI() {
return $this->_URI;
}
- public function setURI($URI) {
- $this->_URI = $URI;
- }
/* metadata */
- public function getMetadata($name = NULL) {
+ public function getMetadata($name = NULL, $max_length = NULL) {
if ($name === NULL) {
return $this->_metadata;
} elseif (isset($this->_metadata[$name])) {
- return $this->_metadata[$name];
+ if ($max_length) {
+ return substr($this->_metadata[$name], 0, $max_length);
+ } else {
+ return $this->_metadata[$name];
+ }
} else {
return NULL;
}
@@ -60,6 +62,17 @@ class MediaBrotha_Media {
if (is_array($name)) {
$this->_metadata = $name;
} else {
+ switch($name) {
+ case 'hidden':
+ case 'display_name':
+ case 'Array':
+ //music
+ case 'artist':
+ case 'album':
+ break;
+ default:
+ print "Unusual metadata '$name'.\n";
+ }
$this->_metadata[$name] = $value;
}
@@ -81,10 +94,22 @@ class MediaBrotha_Media {
$this->_mimeEncoding = $mime_encoding;
}
+ /* parent */
+ public function getParent() {
+ return $this->_parent;
+ }
+ public function setParent($parent) {
+ $this->_parent = $parent;
+ }
+
/* Most used metadata */
- public function getDisplayName($max_length = 0) {
- if ($name = $this->getMetadata('name')) {
- return $name;
+ public function getDisplayName($max_length = NULL) {
+ if ($name = $this->getMetadata('display_name')) {
+ if ($max_length) {
+ return substr($name, 0, $max_length);
+ } else {
+ return $name;
+ }
} else {
return '?';
}
@@ -97,13 +122,3 @@ class MediaBrotha_Media {
}
}
-/*
-
-Mandatory metadata:
-uri
-name
-
-Usual metadata:
-artist
-album
-*/
Summary of changes: sathieu/cisco-xml/browser/index.php | 11 +- sathieu/cisco-xml/lib/Cisco-XML/Cisco-XML.php | 6 + sathieu/cisco-xml/lib/MediaBrotha/Backend.php | 7 +- .../lib/MediaBrotha/Backend/FileSystem.php | 41 ++++-- sathieu/cisco-xml/lib/MediaBrotha/Backend/LDAP.php | 17 ++- sathieu/cisco-xml/lib/MediaBrotha/Backend/Root.php | 18 ++-- sathieu/cisco-xml/lib/MediaBrotha/Backend/VLC.php | 20 +-- sathieu/cisco-xml/lib/MediaBrotha/Core.php | 140 +++++++++++--------- .../lib/MediaBrotha/Frontend/CiscoXML.php | 11 +- sathieu/cisco-xml/lib/MediaBrotha/Media.php | 57 +++++--- 10 files changed, 197 insertions(+), 131 deletions(-)
this email was generated because of /git/your-repo.git/hooks/post-receive by the file /git-core/contrib/hooks/post-receive-email
For more info, see http://blog.chomperstomp.com/?p=630
--
FreeSWITCH user-contributed scripts, etc