| [ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 class restServer 24 { 25 public $rsp; 26 public $functions = array(); 27 28 public function __construct() 29 { 30 $this->rsp = new xmlTag('rsp'); 31 } 32 33 public function addFunction($name, $callback) 34 { 35 if (is_callable($callback)) { 36 $this->functions[$name] = $callback; 37 } 38 } 39 40 protected function callFunction($name,$get,$post) 41 { 42 if (isset($this->functions[$name])) { 43 return call_user_func($this->functions[$name],$get,$post); 44 } 45 } 46 47 public function serve($encoding='UTF-8') 48 { 49 $get = array(); 50 if (isset($_GET)) { 51 $get = $_GET; 52 } 53 54 $post = array(); 55 if (isset($_POST)) { 56 $post = $_POST; 57 } 58 59 if (!isset($_REQUEST['f'])) { 60 $this->rsp->status = 'failed'; 61 $this->rsp->message('No function given'); 62 $this->getXML($encoding); 63 return false; 64 } 65 66 if (!isset($this->functions[$_REQUEST['f']])) { 67 $this->rsp->status = 'failed'; 68 $this->rsp->message('Function does not exist'); 69 $this->getXML($encoding); 70 return false; 71 } 72 73 try { 74 $res = $this->callFunction($_REQUEST['f'],$get,$post); 75 } catch (Exception $e) { 76 $this->rsp->status = 'failed'; 77 $this->rsp->message($e->getMessage()); 78 $this->getXML($encoding); 79 return false; 80 } 81 82 $this->rsp->status = 'ok'; 83 84 $this->rsp->insertNode($res); 85 86 $this->getXML($encoding); 87 return true; 88 } 89 90 private function getXML($encoding='UTF-8') 91 { 92 header('Content-Type: text/xml; charset='.$encoding); 93 echo $this->rsp->toXML(1,$encoding); 94 } 95 } 96 97 class xmlTag 98 { 99 private $_name; 100 private $_attr = array(); 101 private $_nodes = array(); 102 103 public function __construct($name=null, $content=null) 104 { 105 $this->_name = $name; 106 107 if ($content !== null) { 108 $this->insertNode($content); 109 } 110 } 111 112 public function __set($name, $value) 113 { 114 $this->insertAttr($name, $value); 115 } 116 117 public function __call($name, $args) 118 { 119 if (!preg_match('#^[a-z_]#',$name)) { 120 return false; 121 } 122 123 if (!isset($args[0])) { 124 $args[0] = null; 125 } 126 127 $this->insertNode(new self($name,$args[0])); 128 } 129 130 public function CDATA($value) 131 { 132 $this->insertNode($value); 133 } 134 135 public function insertAttr($name, $value) 136 { 137 $this->_attr[$name] = $value; 138 } 139 140 public function insertNode($node=null) 141 { 142 if ($node instanceof self) 143 { 144 $this->_nodes[] = $node; 145 } 146 elseif (is_array($node)) 147 { 148 $child = new self(null); 149 foreach ($node as $tag => $n) { 150 $child->insertNode(new self($tag,$n)); 151 } 152 $this->_nodes[] = $child; 153 } 154 elseif (is_bool($node)) 155 { 156 $this->_nodes[] = $node ? '1' : '0'; 157 } 158 else 159 { 160 $this->_nodes[] = (string) $node; 161 } 162 } 163 164 public function toXML($prolog=false,$encoding='UTF-8') 165 { 166 if ($this->_name && count($this->_nodes) > 0) { 167 $p = '<%1$s%2$s>%3$s</%1$s>'; 168 } elseif ($this->_name && count($this->_nodes) == 0) { 169 $p = '<%1$s%2$s/>'; 170 } else { 171 $p = '%3$s'; 172 } 173 174 $res = $attr = $content = ''; 175 176 177 foreach ($this->_attr as $k => $v) { 178 $attr .= ' '.$k.'="'.htmlspecialchars($v,ENT_QUOTES,$encoding).'"'; 179 } 180 181 foreach ($this->_nodes as $node) 182 { 183 if ($node instanceof self) { 184 $content .= $node->toXML(); 185 } else { 186 $content .= htmlspecialchars($node,ENT_QUOTES,$encoding); 187 } 188 } 189 190 $res = sprintf($p,$this->_name,$attr,$content); 191 192 if ($prolog && $this->_name) { 193 $res = '<?xml version="1.0" encoding="'.$encoding.'" ?>'."\n".$res; 194 } 195 196 return $res; 197 } 198 } 199 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |