提交 10f46e14 authored 作者: Mathieu Parent's avatar Mathieu Parent

MediaBrotha: LDAP search form

And more:
- callback
- rework of actions
- exceptions handling
- forms
上级 855b7802
......@@ -25,7 +25,7 @@ This file is part of XML services for Cisco IP Phones.
*/
//* Debug
ini_set('error_reporting', E_ALL | E_STRUCT);
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
// */
......
......@@ -25,7 +25,9 @@ This file is part of XML services for Cisco IP Phones.
*/
class CiscoIPPhone {
static public function userAgentIsCiscoIPPhone() {
return preg_match('/^Allegro-Software-WebClient\//', $_SERVER['HTTP_USER_AGENT']);
if (isset($_SERVER['HTTP_USER_AGENT'])) {
return preg_match('/^Allegro-Software-WebClient\//', $_SERVER['HTTP_USER_AGENT']);
}
}
}
......
......@@ -26,8 +26,8 @@ This file is part of MediaBrotha.
require_once('MediaBrotha/Media.php');
class MediaBrotha_Backend {
protected $_params = NULL;
abstract class MediaBrotha_Backend {
private $_params = NULL;
public function __construct($params = NULL) {
$this->setParam($params);
......@@ -35,7 +35,7 @@ class MediaBrotha_Backend {
}
/* params */
public function getParam($name = NULL) {
final public function getParam($name = NULL) {
if ($name === NULL) {
return $this->_params;
} elseif (isset($this->_params[$name])) {
......@@ -44,7 +44,7 @@ class MediaBrotha_Backend {
return NULL;
}
}
public function setParam($name, $value = NULL) {
final public function setParam($name, $value = NULL) {
if (is_array($name)) {
$this->_params = $name;
} else {
......@@ -66,10 +66,6 @@ class MediaBrotha_Backend {
return $item;
}
public function fetch(MediaBrotha_Media $media) {
return false;
}
// Actions
public function getMediaActions(MediaBrotha_Media $media) {
return Array();
......
......@@ -61,19 +61,6 @@ class MediaBrotha_Backend_FileSystem extends MediaBrotha_Backend {
}
}
public function fetch(MediaBrotha_Media $media) {
$uri = $media->getURI();
if ((parse_url($uri, PHP_URL_SCHEME) != 'file') || !$this->isURISafe($uri)) {
$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 {
return false;
}
}
// Actions
protected function _isHandled(MediaBrotha_Media $media) {
return ($media->getMimeType() === 'application/x-directory')
......@@ -83,6 +70,7 @@ class MediaBrotha_Backend_FileSystem extends MediaBrotha_Backend {
public function getMediaActions(MediaBrotha_Media $media) {
if ($this->_isHandled($media)) {
return Array(
'_default',
'browse',
);
} else {
......@@ -90,6 +78,25 @@ class MediaBrotha_Backend_FileSystem extends MediaBrotha_Backend {
}
}
public function doMediaAction($action, MediaBrotha_Media $media) {
switch($action) {
case '_default':
case 'browse':
$uri = $media->getURI();
if ((parse_url($uri, PHP_URL_SCHEME) != 'file') || !$this->isURISafe($uri)) {
$uri = 'file://'.$this->getParam('base_path');
}
$path = parse_url($uri, PHP_URL_PATH);
if (is_dir($path)) {
return new MediaBrotha_MediaIterator($this, new DirectoryIterator($path));
} else {
return false;
}
default:
return parent::doMediaAction($action, $media);
}
}
// Populate
public function populateMedia(MediaBrotha_Media $media) {
if ($this->_isHandled($media)) {
......
......@@ -63,21 +63,6 @@ class MediaBrotha_Backend_LDAP extends MediaBrotha_Backend {
}
}
public function fetch(MediaBrotha_Media $media) {
$uri = $media->getURI();
if (($this::parseLDAPURI($uri, PHP_URL_SCHEME) != 'ldap') || !$this->isURISafe($uri)) {
$ldap_connect_config = $this->getParam('ldap_connect_config');
$uri = 'ldap:///'.$ldap_connect_config['basedn'];
}
$basedn = $this::parseLDAPURI($uri, PHP_URL_PATH);
if ($basedn && ($basedn{0} = '/')) {
$basedn = substr($basedn, 1);
}
$ldap_search = $this->_ldap->search($basedn, NULL,
Array());
return new MediaBrotha_MediaIterator($this, $media, $ldap_search);
return true;
}
// Actions
protected function _isHandled(MediaBrotha_Media $media) {
return ($media->getMimeType() === 'text/directory')
......@@ -87,12 +72,74 @@ class MediaBrotha_Backend_LDAP extends MediaBrotha_Backend {
public function getMediaActions(MediaBrotha_Media $media) {
if ($this->_isHandled($media)) {
return Array(
'_default',
'browse',
'search',
);
} else {
return parent::getMediaActions($media);
}
}
public function doMediaAction($action, MediaBrotha_Media $media) {
switch($action) {
case 'browse':
$uri = $media->getURI();
if (($this::parseLDAPURI($uri, PHP_URL_SCHEME) != 'ldap') || !$this->isURISafe($uri)) {
$ldap_connect_config = $this->getParam('ldap_connect_config');
$uri = 'ldap:///'.$ldap_connect_config['basedn'];
}
$basedn = $this::parseLDAPURI($uri, PHP_URL_PATH);
if ($basedn && ($basedn{0} = '/')) {
$basedn = substr($basedn, 1);
}
$ldap_search = $this->_ldap->search($basedn, NULL,
Array());
return new MediaBrotha_MediaIterator($this, $ldap_search);
case '_default':
case 'search':
$form = new MediaBrotha_Form();
$form->setTitle('search');
$search_fields = Array(
'sn' => Array('display_name' => 'last name'),
'givenName' => Array('display_name' => 'first name'),
'telephoneNumber' => Array('display_name' => 'phone', 'filter' => '(telephoneNumber=*%s*)'),
//'o' => Array('display_name' => 'organization'),
//'mobile' => Array('display_name' => 'mobile'),
//'facsimileTelephoneNumber' => Array('display_name' => 'fax'),
//'l' => Array('display_name' => 'location'),
//'st' => Array('display_name' => 'department'),
//'roomNumber' => Array('display_name' => 'room number'),
);
$ldap_connect_config = $this->getParam('ldap_connect_config');
$filter = $ldap_connect_config['filter'];
foreach ($search_fields as $attribute_name => $field) {
$field['name'] = $attribute_name;
if (!empty($_GET[$attribute_name])) {
$field['value'] = $_GET[$attribute_name];
if(empty($infos['filter'])) {
$filter="(&$filter($attribute_name=".$_GET[$attribute_name]."*))";
} else {
$filter="(&$filter".sprintf($infos['filter'], $_GET[$attribute_name]).")";
}
}
$form->addField($field);
}
if (empty($_GET['ldap_search'])) {
MediaBrotha_Core::addCallback('MediaBrotha_Backend_LDAP::searchCallback');
return $form;
} else {
$ldap_search = $this->_ldap->search(NULL, $filter,
Array('scope' => 'sub'));
return new MediaBrotha_MediaIterator($this, $ldap_search);
}
default:
return parent::doMediaAction($action, $media);
}
}
static public function searchCallback() {
$_GET['backend'] = 'LDAP';
$_GET['action'] = 'search';
$_GET['ldap_search'] = '1';
}
}
......@@ -35,7 +35,7 @@ class MediaBrotha_Backend_Root extends MediaBrotha_Backend {
'display_name' => 'Root',
)
);
$this->_media_iterator = new MediaBrotha_MediaIterator($this, $this->_media);
$this->_media_iterator = new MediaBrotha_MediaIterator($this);
}
public function addRootMedia($URI, array $metadata = Array(), $mime_type = NULL, $mime_encoding = NULL) {
......@@ -45,9 +45,15 @@ class MediaBrotha_Backend_Root extends MediaBrotha_Backend {
$this->_media_iterator->bufferAdd($media);
}
// Browsing
public function fetch(MediaBrotha_Media $media) {
return $this->_media_iterator;
// Actions
public function doMediaAction($action, MediaBrotha_Media $media) {
switch($action) {
case '_default':
case 'browse':
return $this->_media_iterator;
default:
return parent::doMediaAction($action, $media);
}
}
}
......
......@@ -43,12 +43,6 @@ class MediaBrotha_Backend_VLC extends MediaBrotha_Backend {
print $output.'<!--'.$url."-->\n";
}
// Browsing
public function fetch(MediaBrotha_Media $media) {
$this->_buffer[] = $media;
return true;
}
// Actions
protected function _isHandled(MediaBrotha_Media $media) {
return in_array($media->getMimeType(), Array($this::$_supported_mime_types))
......
......@@ -28,6 +28,7 @@ require_once('MediaBrotha/Backend.php');
require_once('MediaBrotha/Frontend.php');
require_once('MediaBrotha/Media.php');
require_once('MediaBrotha/MediaIterator.php');
require_once('MediaBrotha/Form.php');
class MediaBrotha_Core {
private static $_frontend = NULL;
......@@ -38,20 +39,26 @@ class MediaBrotha_Core {
* Core
*/
public static function init() {
if (!session_start()) {
die('Unable to start session');
try {
if (!session_start()) {
throw new Exception('Unable to start session');
}
MediaBrotha_Core::_unhashRequest();
MediaBrotha_Core::loadBackend('Root');
} catch (Exception $e) {
die($e);
}
MediaBrotha_Core::_unhashRequest();
MediaBrotha_Core::loadBackend('Root');
}
public static function go() {
$current_backend = MediaBrotha_Core::getCurrentBackend();
$current_media = MediaBrotha_Core::getCurrentMedia();
if (MediaBrotha_Core::getAction() == 'browse') {
if ($media_iterator = $current_backend->fetch($current_media)) {
try {
MediaBrotha_Core::runCallback();
$current_backend = MediaBrotha_Core::getCurrentBackend();
$current_media = MediaBrotha_Core::getCurrentMedia();
$result = $current_backend->doMediaAction(MediaBrotha_Core::getAction(), $current_media);
if (is_a($result, 'MediaBrotha_MediaIterator')) {
MediaBrotha_Core::$_frontend->begin($current_media);
foreach ($media_iterator as $item) {
foreach ($result as $item) {
if (($item === NULL) || $item->isHidden()) {
continue;
}
......@@ -59,17 +66,32 @@ class MediaBrotha_Core {
}
MediaBrotha_Core::$_frontend->finish($current_media);
MediaBrotha_Core::$_frontend->render($current_media);
} elseif (is_a($result, 'MediaBrotha_Form')) {
MediaBrotha_Core::$_frontend->renderForm($result);
} elseif (is_a($result, 'Exception')) {
throw $result;
} else {
throw new Exception("Got unexpected result '$result'.");
}
} else {
$current_backend->doMediaAction(MediaBrotha_Core::getAction(), $current_media);
} catch (Exception $e) {
MediaBrotha_Core::$_frontend->renderException($e);
}
}
public function addRootMedia($URI, array $metadata = Array(), $mime_type = NULL, $mime_encoding = NULL) {
public static function addRootMedia($URI, array $metadata = Array(), $mime_type = NULL, $mime_encoding = NULL) {
MediaBrotha_Core::$_backends['Root']->addRootMedia($URI, $metadata, $mime_type, $mime_encoding);
}
public static function addCallback($callback, $arg = NULL) {
$_SESSION['CALLBACK'][] = Array($callback, $arg);
}
public static function runCallback() {
while($callback_info = array_shift($_SESSION['CALLBACK'])) {
call_user_func($callback_info[0],$callback_info[1]);
}
}
/*
* Backends
*/
......@@ -129,7 +151,7 @@ class MediaBrotha_Core {
if (!empty($_GET['action'])) {
return $_GET['action'];
} else {
return 'browse';
return '_default';
}
}
......
<?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_Field {
private $_attrs;
public function __construct(array $attrs) {
$this->_attrs = $attrs;
}
public function get($name) {
if (isset($this->_attrs[$name])) {
return $this->_attrs[$name];
}
}
}
class MediaBrotha_Form extends ArrayIterator {
private $_title;
public function getTitle() {
return $this->_title;
}
public function setTitle($title) {
$this->_title = $title;
}
public function addField(array $args) {
$this[] = new MediaBrotha_Field($args);
}
}
......@@ -24,11 +24,19 @@ This file is part of MediaBrotha.
* @author Mathieu Parent
*/
class MediaBrotha_Frontend {
abstract class MediaBrotha_Frontend {
protected $_infos = NULL;
public function __construct(array $args = Array()) {
$this->_infos = $args;
}
public function renderException(Exception $e) {
print $e;
}
public function renderForm(MediaBrotha_Form $form) {
throw new Exception(__FUNCTION__ .' not implemented in class "'.get_class($this).'"');
}
}
......@@ -52,7 +52,7 @@ class MediaBrotha_Frontend_CiscoXML extends MediaBrotha_Frontend_HTTP {
'URL' => 'SoftKey:Select',
'Position' => $pos++));
foreach (MediaBrotha_Core::getBackends() as $backend) {
foreach ($backend->getMediaActions(MediaBrotha_Core::getCurrentMedia()) as $action) {
foreach ($backend->getMediaActions($item) as $action) {
$this->_xml->setCiscoElement('SoftKeyItem',
Array('Name' => $action,
'URL' => 'QueryStringParam:'.MediaBrotha_Core::value2hash('action='.$action.'&backend='.$backend->getBackendName()),
......@@ -70,5 +70,25 @@ class MediaBrotha_Frontend_CiscoXML extends MediaBrotha_Frontend_HTTP {
print $this->_xml->execute($this->_infos['push_url']);
}
}
public function renderForm(MediaBrotha_Form $form) {
$xml = new CiscoIPPhoneInput();
$xml->setCiscoElement('Title', $form->getTitle());
//$xml->setCiscoElement('Prompt', ...);
$xml->setCiscoElement('URL', $this->rootURL());
foreach($form as $field) {
$xml->setCiscoElement('InputItem',
Array(
'DisplayName' => $field->get('display_name'),
'QueryStringParam' => $field->get('name'),
'DefaultValue' => $field->get('value'),
'InputFlags' => '',
)
);
}
CiscoXMLObject::HttpHeader();
print $xml;
}
}
......@@ -54,7 +54,7 @@ class MediaBrotha_Frontend_HTML extends MediaBrotha_Frontend_HTTP {
}
}
public function begin($item) {
private function _createHTMLDocument($title) {
$this->_xml = new DOMDocument('1.0');
$root = $this->_xml->createElement('html');
$root = $this->_xml->appendChild($root);
......@@ -62,11 +62,10 @@ class MediaBrotha_Frontend_HTML extends MediaBrotha_Frontend_HTTP {
$head = $this->_xml->createElement('head');
$head = $root->appendChild($head);
$title = $this->_xml->createElement('title');
$title = $head->appendChild($title);
$text = $this->_xml->createTextNode($item->getDisplayName());
$text = $title->appendChild($text);
$titleElement = $this->_xml->createElement('title');
$titleElement = $head->appendChild($titleElement);
$text = $this->_xml->createTextNode($title);
$text = $titleElement->appendChild($text);
$style = $this->_xml->createElement('link');
$style = $head->appendChild($style);
......@@ -82,7 +81,10 @@ class MediaBrotha_Frontend_HTML extends MediaBrotha_Frontend_HTTP {
$this->_body = $this->_xml->createElement('body');
$this->_body = $root->appendChild($this->_body);
$this->_body->setAttribute('onload', 'bodyLoaded();');
}
public function begin($item) {
$this->_createHTMLDocument($item->getDisplayName());
$this->_mediaListElement = $this->_xml->createElement('ul');
$this->_mediaListElement = $this->_body->appendChild($this->_mediaListElement);
$this->_mediaListElement->setAttribute('id', 'mediaListElement');
......@@ -123,5 +125,60 @@ class MediaBrotha_Frontend_HTML extends MediaBrotha_Frontend_HTTP {
public function render() {
print $this->_xml->saveHTML();
}
public function renderForm(MediaBrotha_Form $form) {
$this->_createHTMLDocument($form->getTitle());
$formElement = $this->_xml->createElement('form');
$formElement = $this->_body->appendChild($formElement);
$tableElement = $this->_xml->createElement('table');
$tableElement = $formElement->appendChild($tableElement);
foreach($form as $field) {
$trElement = $this->_xml->createElement('tr');
$trElement = $tableElement->appendChild($trElement);
$tdElement1 = $this->_xml->createElement('th');
$tdElement1 = $trElement->appendChild($tdElement1);
$text1 = $this->_xml->createTextNode($field->get('display_name'));
$text1 = $tdElement1->appendChild($text1);
$tdElement2 = $this->_xml->createElement('td');
$tdElement2 = $trElement->appendChild($tdElement2);
$inputElement = $this->_xml->createElement('input');
$inputElement = $tdElement2->appendChild($inputElement);
$inputElement->setAttribute('name', $field->get('name'));
$inputElement->setAttribute('value', $field->get('value'));
}
$trElement = $this->_xml->createElement('tr');
$trElement = $tableElement->appendChild($trElement);
$tdElement = $this->_xml->createElement('td');
$tdElement = $trElement->appendChild($tdElement);
$tdElement->setAttribute('colspan', 2);
$inputElement = $this->_xml->createElement('input');
$inputElement = $tdElement->appendChild($inputElement);
$inputElement->setAttribute('type', 'submit');
print $this->_xml->saveHTML();
}
public function renderException(Exception $e) {
$this->_createHTMLDocument('Error');
$divElement = $this->_xml->createElement('pre');
$divElement = $this->_body->appendChild($divElement);
$text = $this->_xml->createTextNode($e->getMessage());
$text = $divElement->appendChild($text);
$divElement = $this->_xml->createElement('pre');
$divElement = $this->_body->appendChild($divElement);
$text = $this->_xml->createTextNode($e->getTraceAsString());
$text = $divElement->appendChild($text);
print $this->_xml->saveHTML();
}
}
......@@ -24,10 +24,14 @@ This file is part of MediaBrotha.
* @author Mathieu Parent
*/
class MediaBrotha_Frontend_HTTP extends MediaBrotha_Frontend {
abstract class MediaBrotha_Frontend_HTTP extends MediaBrotha_Frontend {
public function rootURL() {
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
$url = preg_replace('/index\.php$/', '', $url);
if (isset($_SERVER['SERVER_NAME'])) {
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
$url = preg_replace('/index\.php$/', '', $url);
} else {
$url = $_SERVER['SCRIPT_NAME'];
}
return $url;
}
}
......@@ -24,7 +24,7 @@ This file is part of MediaBrotha.
* @author Mathieu Parent
*/
class MediaBrotha_Media {
final class MediaBrotha_Media {
private $_URI = NULL;
private $_metadata = Array();
private $_mimeType = NULL;
......
......@@ -28,13 +28,11 @@ require_once('MediaBrotha/Media.php');
class MediaBrotha_MediaIterator implements Iterator {
private $_backend = NULL;
private $_media = NULL;
private $_handle = NULL;
protected $_buffer = NULL;
public function __construct(MediaBrotha_Backend $backend, MediaBrotha_Media $media, $buffer = NULL) {
public function __construct(MediaBrotha_Backend $backend, $buffer = NULL) {
$this->_backend = $backend;
$this->_media = $media;
if ($buffer) {
$this->_buffer = $buffer;
} else {
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论