[3400] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * CPAINT - Cross-Platform Asynchronous INterface Toolkit
|
---|
| 4 | *
|
---|
| 5 | * http://sf.net/projects/cpaint
|
---|
| 6 | *
|
---|
| 7 | * released under the terms of the LGPL
|
---|
| 8 | * see http://www.fsf.org/licensing/licenses/lgpl.txt for details
|
---|
| 9 | *
|
---|
| 10 | * @package CPAINT
|
---|
| 11 | * @author Paul Sullivan <wiley14@gmail.com>
|
---|
| 12 | * @author Dominique Stender <dstender@st-webdevelopment.de>
|
---|
| 13 | * @copyright Copyright (c) 2005-2006 Paul Sullivan, Dominique Stender - http://sf.net/projects/cpaint
|
---|
| 14 | * @version $Id$
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | //---- includes ----------------------------------------------------------------
|
---|
| 18 | /**
|
---|
| 19 | * @include JSON
|
---|
| 20 | */
|
---|
| 21 | require_once(dirname(__FILE__) . '/json.php');
|
---|
| 22 |
|
---|
| 23 | /**
|
---|
| 24 | * @include config
|
---|
| 25 | */
|
---|
| 26 | require_once("cpaint2.config.php");
|
---|
| 27 |
|
---|
| 28 | //---- variables ---------------------------------------------------------------
|
---|
| 29 | $GLOBALS['__cpaint_json'] = new JSON();
|
---|
| 30 |
|
---|
| 31 | //---- error reporting ---------------------------------------------------------
|
---|
| 32 | error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
|
---|
| 33 |
|
---|
| 34 | //---- classes -----------------------------------------------------------------
|
---|
| 35 | /**
|
---|
| 36 | * cpaint base class.
|
---|
| 37 | *
|
---|
| 38 | * @package CPAINT
|
---|
| 39 | * @access public
|
---|
| 40 | * @author Paul Sullivan <wiley14@gmail.com>
|
---|
| 41 | * @author Dominique Stender <dstender@st-webdevelopment.de>
|
---|
| 42 | * @copyright Copyright (c) 2005-2006 Paul Sullivan, Dominique Stender - http://sf.net/projects/cpaint
|
---|
| 43 | * @version 2.0.2
|
---|
| 44 | */
|
---|
| 45 | class cpaint {
|
---|
| 46 | /**
|
---|
| 47 | * version number
|
---|
| 48 | *
|
---|
| 49 | * @access private
|
---|
| 50 | * @var string $version
|
---|
| 51 | */
|
---|
| 52 | var $version = '2.0.2';
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | * response type.
|
---|
| 56 | *
|
---|
| 57 | * @access protected
|
---|
| 58 | * @var string $response_type
|
---|
| 59 | */
|
---|
| 60 | var $response_type;
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * the basenode ajaxResponse.
|
---|
| 64 | *
|
---|
| 65 | * @access protected
|
---|
| 66 | * @var object $basenode
|
---|
| 67 | */
|
---|
| 68 | var $basenode;
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * list of registered methods available through the CPAINT API
|
---|
| 72 | *
|
---|
| 73 | * @access protected
|
---|
| 74 | * @var array $api_functions
|
---|
| 75 | */
|
---|
| 76 | var $api_functions;
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * list of registered complex types used in the CPAINT API
|
---|
| 80 | *
|
---|
| 81 | * @access protected
|
---|
| 82 | * @var array $api_datatypes
|
---|
| 83 | */
|
---|
| 84 | var $api_datatypes;
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * whether or not the CPAINT API generates a WSDL when called with ?wsdl querystring
|
---|
| 88 | *
|
---|
| 89 | * @access private
|
---|
| 90 | * @var boolean $use_wsdl
|
---|
| 91 | */
|
---|
| 92 | var $use_wsdl;
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * PHP4 constructor.
|
---|
| 96 | *
|
---|
| 97 | * @access public
|
---|
| 98 | * @return void
|
---|
| 99 | */
|
---|
| 100 | function cpaint() {
|
---|
| 101 | $this->__construct();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * PHP 5 constructor.
|
---|
| 106 | *
|
---|
| 107 | * @access public
|
---|
| 108 | * @return void
|
---|
| 109 | * @todo -o"Dominique Stender" -ccpaint implement a better debugging
|
---|
| 110 | */
|
---|
| 111 | function __construct() {
|
---|
| 112 | // initialize properties
|
---|
| 113 | $this->basenode = new cpaint_node();
|
---|
| 114 | $this->basenode->set_name('ajaxResponse');
|
---|
| 115 | $this->basenode->set_attribute('id', '');
|
---|
| 116 | $this->basenode->set_encoding('UTF-8');
|
---|
| 117 |
|
---|
| 118 | $this->response_type = 'TEXT';
|
---|
| 119 | $this->api_functions = array();
|
---|
| 120 | $this->api_datatypes = array();
|
---|
| 121 | $this->use_wsdl = true;
|
---|
| 122 |
|
---|
| 123 | $this->complex_type(array(
|
---|
| 124 | 'name' => 'cpaintResponseType',
|
---|
| 125 | 'type' => 'restriction', // (restriction|complex|list)
|
---|
| 126 | 'base_type' => 'string', // scalar type of all values, e.g. 'string', for type = (restriction|list) only
|
---|
| 127 | 'values' => array( // for type = 'restriction' only: list of allowed values
|
---|
| 128 | 'XML', 'TEXT', 'OBJECT', 'E4X', 'JSON',
|
---|
| 129 | ),
|
---|
| 130 | )
|
---|
| 131 | );
|
---|
| 132 | $this->complex_type(array(
|
---|
| 133 | 'name' => 'cpaintDebugLevel',
|
---|
| 134 | 'type' => 'restriction',
|
---|
| 135 | 'base_type' => 'long',
|
---|
| 136 | 'values' => array(
|
---|
| 137 | -1, 0, 1, 2
|
---|
| 138 | ),
|
---|
| 139 | )
|
---|
| 140 | );
|
---|
| 141 | $this->complex_type(array(
|
---|
| 142 | 'name' => 'cpaintDebugMessage',
|
---|
| 143 | 'type' => 'list',
|
---|
| 144 | 'base_type' => 'string',
|
---|
| 145 | )
|
---|
| 146 | );
|
---|
| 147 |
|
---|
| 148 | $this->complex_type(array(
|
---|
| 149 | 'name' => 'cpaintRequestHead',
|
---|
| 150 | 'type' => 'complex',
|
---|
| 151 | 'struct' => array(
|
---|
| 152 | 0 => array('name' => 'functionName', 'type' => 'string'),
|
---|
| 153 | 1 => array('name' => 'responseType', 'type' => 'cpaintResponseType'),
|
---|
| 154 | 2 => array('name' => 'debugLevel', 'type' => 'cpaintDebugLevel'),
|
---|
| 155 | ),
|
---|
| 156 | )
|
---|
| 157 | );
|
---|
| 158 |
|
---|
| 159 | $this->complex_type(array(
|
---|
| 160 | 'name' => 'cpaintResponseHead',
|
---|
| 161 | 'type' => 'complex',
|
---|
| 162 | 'struct' => array(
|
---|
| 163 | 0 => array('name' => 'success', 'type' => 'boolean'),
|
---|
| 164 | 1 => array('name' => 'debugger', 'type' => 'cpaintDebugMessage'),
|
---|
| 165 | ),
|
---|
| 166 | )
|
---|
| 167 | );
|
---|
| 168 |
|
---|
| 169 | // determine response type
|
---|
| 170 | if (isset($_REQUEST['cpaint_response_type'])) {
|
---|
| 171 | $this->response_type = strtoupper((string) $_REQUEST['cpaint_response_type']);
|
---|
| 172 | } // end: if
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * calls the user function responsible for this specific call.
|
---|
| 177 | *
|
---|
| 178 | * @access public
|
---|
| 179 | * @param string $input_encoding input data character encoding, default is UTF-8
|
---|
| 180 | * @return void
|
---|
| 181 | */
|
---|
| 182 | function start($input_encoding = 'UTF-8') {
|
---|
| 183 | $user_function = '';
|
---|
| 184 | $arguments = array();
|
---|
| 185 |
|
---|
| 186 | // work only if there is no API version request
|
---|
| 187 | if (!isset($_REQUEST['api_query'])
|
---|
| 188 | && !isset($_REQUEST['wsdl'])) {
|
---|
| 189 | $this->basenode->set_encoding($input_encoding);
|
---|
| 190 |
|
---|
| 191 | if ($_REQUEST['cpaint_function'] != '') {
|
---|
| 192 | $user_function = $_REQUEST['cpaint_function'];
|
---|
| 193 | $arguments = $_REQUEST['cpaint_argument'];
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | // perform character conversion on every argument
|
---|
| 197 | foreach ($arguments as $key => $value) {
|
---|
| 198 |
|
---|
| 199 | if (get_magic_quotes_gpc() == true) {
|
---|
| 200 | $value = stripslashes($value);
|
---|
| 201 | } // end: if
|
---|
| 202 |
|
---|
| 203 | // convert from JSON string
|
---|
| 204 | $arguments[$key] = $GLOBALS['__cpaint_json']->parse($value);
|
---|
| 205 | } // end: foreach
|
---|
| 206 |
|
---|
| 207 | $arguments = cpaint_transformer::decode_array($arguments, $this->basenode->get_encoding());
|
---|
| 208 |
|
---|
| 209 | if (is_array($this->api_functions[$user_function])
|
---|
| 210 | && is_callable($this->api_functions[$user_function]['call'])) {
|
---|
| 211 | // a valid API function is to be called
|
---|
| 212 | call_user_func_array($this->api_functions[$user_function]['call'], $arguments);
|
---|
| 213 |
|
---|
| 214 | } else if ($user_function != '') {
|
---|
| 215 | // desired function is not registered as API function
|
---|
| 216 | $this->basenode->set_data('[CPAINT] A function name was passed that is not allowed to execute on this server.');
|
---|
| 217 | }
|
---|
| 218 | } // end: if
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
| 222 | * generates and prints the response based on response type supplied by the frontend.
|
---|
| 223 | *
|
---|
| 224 | * @access public
|
---|
| 225 | * @return void
|
---|
| 226 | */
|
---|
| 227 | function return_data() {
|
---|
| 228 | // send appropriate headers to avoid caching
|
---|
| 229 | header('Expires: Fri, 14 Mar 1980 20:53:00 GMT');
|
---|
| 230 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
---|
| 231 | header('Cache-Control: no-cache, must-revalidate');
|
---|
| 232 | header('Pragma: no-cache');
|
---|
| 233 | header('X-Powered-By: CPAINT v' . $this->version . '/PHP v' . phpversion());
|
---|
| 234 |
|
---|
| 235 | // work only if there is no API version request
|
---|
| 236 |
|
---|
| 237 | if (!isset($_REQUEST['api_query'])
|
---|
| 238 | && !isset($_REQUEST['wsdl'])) {
|
---|
| 239 | // trigger generation of response
|
---|
| 240 | switch (trim($this->response_type)) {
|
---|
| 241 |
|
---|
| 242 | case 'TEXT':
|
---|
| 243 | header('Content-type: text/plain; charset=' . cpaint_transformer::find_output_charset($this->basenode->get_encoding()));
|
---|
| 244 | echo cpaint_transformer::toString($this->basenode);
|
---|
| 245 | break;
|
---|
| 246 |
|
---|
| 247 | case 'JSON':
|
---|
| 248 | header('Content-type: text/plain; charset=' . cpaint_transformer::find_output_charset($this->basenode->get_encoding()));
|
---|
| 249 | echo cpaint_transformer::toJSON($this->basenode);
|
---|
| 250 | break;
|
---|
| 251 |
|
---|
| 252 | case 'OBJECT':
|
---|
| 253 | case 'E4X':
|
---|
| 254 | case 'XML':
|
---|
| 255 | header('Content-type: text/xml; charset=' . cpaint_transformer::find_output_charset($this->basenode->get_encoding()));
|
---|
| 256 | echo '<?xml version="1.0" encoding="' . cpaint_transformer::find_output_charset($this->basenode->get_encoding()) . '"?>'
|
---|
| 257 | . cpaint_transformer::toXML($this->basenode);
|
---|
| 258 | break;
|
---|
| 259 |
|
---|
| 260 | default:
|
---|
| 261 | echo 'ERROR: invalid response type \'' . $this->response_type . '\'';
|
---|
| 262 | } // end: switch
|
---|
| 263 |
|
---|
| 264 | } elseif (isset($_REQUEST['api_query'])) {
|
---|
| 265 | // API version request
|
---|
| 266 | header('Content-type: text/plain; charset=ISO-8859-1');
|
---|
| 267 | echo 'CPAINT v' . $this->version . '/PHP v' . phpversion();
|
---|
| 268 |
|
---|
| 269 | } elseif ($this->use_wsdl == true
|
---|
| 270 | && isset($_REQUEST['wsdl'])) {
|
---|
| 271 |
|
---|
| 272 | if (is_file(dirname(__FILE__) . '/cpaint2.wsdl.php')
|
---|
| 273 | && is_readable(dirname(__FILE__) . '/cpaint2.wsdl.php')) {
|
---|
| 274 |
|
---|
| 275 | require_once(dirname(__FILE__) . '/cpaint2.wsdl.php');
|
---|
| 276 |
|
---|
| 277 | if (class_exists('cpaint_wsdl')) {
|
---|
| 278 | // create new instance of WSDL library
|
---|
| 279 | $wsdl = new cpaint_wsdl();
|
---|
| 280 |
|
---|
| 281 | // build WSDL info
|
---|
| 282 | header('Content-type: text/xml; charset=UTF-8');
|
---|
| 283 | echo $wsdl->generate($this->api_functions, $this->api_datatypes);
|
---|
| 284 |
|
---|
| 285 | } else {
|
---|
| 286 | header('Content-type: text/plain; charset=ISO-8859-1');
|
---|
| 287 | echo 'WSDL generator is unavailable';
|
---|
| 288 | } // end: if
|
---|
| 289 |
|
---|
| 290 | } else {
|
---|
| 291 | header('Content-type: text/plain; charset=ISO-8859-1');
|
---|
| 292 | echo 'WSDL generator is unavailable';
|
---|
| 293 | } // end: if
|
---|
| 294 | } // end: if
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /**
|
---|
| 298 | * registers a new function or method as part of the CPAINT API
|
---|
| 299 | *
|
---|
| 300 | * @access public
|
---|
| 301 | * @param mixed $func function name, array(&$object, 'function_name') or array('class', 'function_name')
|
---|
| 302 | * @param array $input function input parameters (not yet used by CPAINT and subject to change)
|
---|
| 303 | * @param array $output function output format (not yed used by CPAINT and subject to change)
|
---|
| 304 | * @param string $comment description of the functionality
|
---|
| 305 | * @return boolean
|
---|
| 306 | */
|
---|
| 307 | function register($func, $input = array(), $output = array(), $comment = '') {
|
---|
| 308 | $return_value = false;
|
---|
| 309 | $input = (array) $input;
|
---|
| 310 | $output = (array) $output;
|
---|
| 311 | $comment = (string) $comment;
|
---|
| 312 |
|
---|
| 313 | if (is_array($func)
|
---|
| 314 | && (is_object($func[0]) || is_string($func[0]))
|
---|
| 315 | && is_string($func[1])
|
---|
| 316 | && is_callable($func)) {
|
---|
| 317 |
|
---|
| 318 | // calling a method of an object
|
---|
| 319 | $this->api_functions[$func[1]] = array(
|
---|
| 320 | 'call' => $func,
|
---|
| 321 | 'input' => $input,
|
---|
| 322 | 'output' => $output,
|
---|
| 323 | 'comment' => $comment,
|
---|
| 324 | );
|
---|
| 325 | $return_value = true;
|
---|
| 326 |
|
---|
| 327 | } elseif (is_string($func)) {
|
---|
| 328 | // calling a standalone function
|
---|
| 329 | $this->api_functions[$func] = array(
|
---|
| 330 | 'call' => $func,
|
---|
| 331 | 'input' => $input,
|
---|
| 332 | 'output' => $output,
|
---|
| 333 | 'comment' => $comment,
|
---|
| 334 | );
|
---|
| 335 | $return_value = true;
|
---|
| 336 | } // end: if
|
---|
| 337 |
|
---|
| 338 | return $return_value;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | /**
|
---|
| 344 | * unregisters a function that is currently part of the CPAINT API.
|
---|
| 345 | *
|
---|
| 346 | * proves useful when the same set of functions is to be used in the
|
---|
| 347 | * frontend and in some kind of administration environment. you might
|
---|
| 348 | * want to unregister a few (admin) functions for the frontend in this
|
---|
| 349 | * case.
|
---|
| 350 | *
|
---|
| 351 | * @access public
|
---|
| 352 | * @param string $func function name
|
---|
| 353 | * @return boolean
|
---|
| 354 | */
|
---|
| 355 | function unregister($func) {
|
---|
| 356 | $retval = false;
|
---|
| 357 |
|
---|
| 358 | if (is_array($this->api_functions[$func])) {
|
---|
| 359 | unset($this->api_functions[$func]);
|
---|
| 360 | } // end: if
|
---|
| 361 |
|
---|
| 362 | return $retval;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 |
|
---|
| 366 |
|
---|
| 367 | /**
|
---|
| 368 | * registers a complex data type
|
---|
| 369 | *
|
---|
| 370 | * @access public
|
---|
| 371 | * @param array $schema schema definition for the complex type
|
---|
| 372 | * @return boolean
|
---|
| 373 | */
|
---|
| 374 | function complex_type($schema) {
|
---|
| 375 | $return_value = false;
|
---|
| 376 | $schema = (array) $schema;
|
---|
| 377 |
|
---|
| 378 | if ($schema['name'] != ''
|
---|
| 379 | && in_array($schema['type'], array('restriction', 'complex', 'list'))) {
|
---|
| 380 |
|
---|
| 381 | $this->api_datatypes[] = $schema;
|
---|
| 382 | $return_value = true;
|
---|
| 383 | } // end: if
|
---|
| 384 |
|
---|
| 385 | return $return_value;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /**
|
---|
| 389 | * switches the generation of WSDL on/off. default is on
|
---|
| 390 | *
|
---|
| 391 | * @access public
|
---|
| 392 | * @param boolean $state state of WSDL generation
|
---|
| 393 | * @return void
|
---|
| 394 | */
|
---|
| 395 | function use_wsdl($state) {
|
---|
| 396 | $this->use_wsdl = (boolean) $state;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | /**
|
---|
| 400 | * adds a new subnode to the basenode.
|
---|
| 401 | *
|
---|
| 402 | * will return a reference to it for further processing.
|
---|
| 403 | *
|
---|
| 404 | * @access public
|
---|
| 405 | * @param string $nodename name of the new node
|
---|
| 406 | * @param string $id id of the new node
|
---|
| 407 | * @return object
|
---|
| 408 | */
|
---|
| 409 | function &add_node($nodename, $id = '') {
|
---|
| 410 | return $this->basenode->add_node($nodename, $id);
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | /**
|
---|
| 414 | * assigns textual data to the basenode.
|
---|
| 415 | *
|
---|
| 416 | * @access public
|
---|
| 417 | * @param mixed $data data to assign to this node
|
---|
| 418 | * @return void
|
---|
| 419 | */
|
---|
| 420 | function set_data($data) {
|
---|
| 421 | $this->basenode->set_data($data);
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | function set_encoding($encoding){
|
---|
| 425 | $this->basenode->set_encoding($encoding);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * returns the data assigned to the basenode.
|
---|
| 430 | *
|
---|
| 431 | * @access public
|
---|
| 432 | * @return mixed
|
---|
| 433 | */
|
---|
| 434 | function get_data() {
|
---|
| 435 | return $this->basenode->get_data();
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | /**
|
---|
| 439 | * sets the id property of the basenode.
|
---|
| 440 | *
|
---|
| 441 | * @deprecated deprecated since version 2.0.0
|
---|
| 442 | * @access public
|
---|
| 443 | * @param string $id the id
|
---|
| 444 | * @return void
|
---|
| 445 | */
|
---|
| 446 | function set_id($id) {
|
---|
| 447 | $this->basenode->set_attribute('id', $id);
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | /**
|
---|
| 451 | * gets the id property of the basenode.
|
---|
| 452 | *
|
---|
| 453 | * @deprecated deprecated since version 2.0.0
|
---|
| 454 | * @access public
|
---|
| 455 | * @return string
|
---|
| 456 | */
|
---|
| 457 | function get_id() {
|
---|
| 458 | return $this->basenode->get_attribute('id');
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | /**
|
---|
| 462 | * adds a new attribute to the basenode.
|
---|
| 463 | *
|
---|
| 464 | * @access public
|
---|
| 465 | * @param string $name attribute name
|
---|
| 466 | * @param mixed $value attribute value
|
---|
| 467 | * @return void
|
---|
| 468 | */
|
---|
| 469 | function set_attribute($name, $value) {
|
---|
| 470 | $this->basenode->set_attribute($name, $value);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | /**
|
---|
| 474 | * retrieves an attribute of the basenode by name.
|
---|
| 475 | *
|
---|
| 476 | * @access public
|
---|
| 477 | * @param string $name attribute name
|
---|
| 478 | * @return string
|
---|
| 479 | */
|
---|
| 480 | function get_attribute($name) {
|
---|
| 481 | return $this->basenode->get_attributes($name);
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | /**
|
---|
| 485 | * set name property of the basenode.
|
---|
| 486 | *
|
---|
| 487 | * @access public
|
---|
| 488 | * @param string $name the name
|
---|
| 489 | * @return void
|
---|
| 490 | */
|
---|
| 491 | function set_name($name) {
|
---|
| 492 | $this->basenode->set_name($name);
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | /**
|
---|
| 496 | * get name property of the basenode.
|
---|
| 497 | *
|
---|
| 498 | * @access public
|
---|
| 499 | * @return string
|
---|
| 500 | */
|
---|
| 501 | function get_name() {
|
---|
| 502 | return $this->basenode->get_name();
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 |
|
---|
| 506 |
|
---|
| 507 | /**
|
---|
| 508 | * returns the response type as requested by the client
|
---|
| 509 | *
|
---|
| 510 | * @access public
|
---|
| 511 | * @return string
|
---|
| 512 | */
|
---|
| 513 | function get_response_type() {
|
---|
| 514 | return $this->response_type;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /**
|
---|
| 520 | * a cpaint data node. Data nodes are used to build up the response.
|
---|
| 521 | *
|
---|
| 522 | * @package CPAINT
|
---|
| 523 | * @access public
|
---|
| 524 | * @author Dominique Stender <dstender@st-webdevelopment.de>
|
---|
| 525 | * @copyright 2005 (Dominique Stender); All rights reserved
|
---|
| 526 | * @version 2.0.2
|
---|
| 527 | */
|
---|
| 528 | class cpaint_node {
|
---|
| 529 | /**
|
---|
| 530 | * array of subnodes.
|
---|
| 531 | *
|
---|
| 532 | * @access public
|
---|
| 533 | * @var array $composites
|
---|
| 534 | */
|
---|
| 535 | var $composites;
|
---|
| 536 |
|
---|
| 537 | /**
|
---|
| 538 | * node attributes.
|
---|
| 539 | *
|
---|
| 540 | * @access public
|
---|
| 541 | * @var array $attributes
|
---|
| 542 | */
|
---|
| 543 | var $attributes;
|
---|
| 544 |
|
---|
| 545 | /**
|
---|
| 546 | * name of this node.
|
---|
| 547 | *
|
---|
| 548 | * @access public
|
---|
| 549 | * @var string $nodename
|
---|
| 550 | */
|
---|
| 551 | var $nodename;
|
---|
| 552 |
|
---|
| 553 | /**
|
---|
| 554 | * textual data of this node.
|
---|
| 555 | *
|
---|
| 556 | * @access public
|
---|
| 557 | * @var string $data
|
---|
| 558 | */
|
---|
| 559 | var $data;
|
---|
| 560 |
|
---|
| 561 | /**
|
---|
| 562 | * character encoding for input data
|
---|
| 563 | *
|
---|
| 564 | * @access private
|
---|
| 565 | * @var $input_encoding
|
---|
| 566 | */
|
---|
| 567 | var $input_encoding;
|
---|
| 568 |
|
---|
| 569 | /**
|
---|
| 570 | * PHP4 constructor.
|
---|
| 571 | *
|
---|
| 572 | * @package CPAINT
|
---|
| 573 | * @access public
|
---|
| 574 | * @return void
|
---|
| 575 | */
|
---|
| 576 | function cpaint_node() {
|
---|
| 577 | $this->__construct();
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | /**
|
---|
| 581 | * PHP 5 constructor.
|
---|
| 582 | *
|
---|
| 583 | * @access public
|
---|
| 584 | * @return void
|
---|
| 585 | */
|
---|
| 586 | function __construct() {
|
---|
| 587 | // initialize properties
|
---|
| 588 | $this->composites = array();
|
---|
| 589 | $this->attributes = array();
|
---|
| 590 | $this->data = '';
|
---|
| 591 |
|
---|
| 592 | $this->set_encoding('UTF-8');
|
---|
| 593 | $this->set_name('');
|
---|
| 594 | $this->set_attribute('id', '');
|
---|
| 595 | }
|
---|
| 596 |
|
---|
| 597 | /**
|
---|
| 598 | * adds a new subnode to this node.
|
---|
| 599 | *
|
---|
| 600 | * will return a reference to it for further processing.
|
---|
| 601 | *
|
---|
| 602 | * @access public
|
---|
| 603 | * @param string $nodename name of the new node
|
---|
| 604 | * @param string $id id of the new node
|
---|
| 605 | * @return object
|
---|
| 606 | */
|
---|
| 607 | function &add_node($nodename, $id = '') {
|
---|
| 608 | $composites = count($this->composites);
|
---|
| 609 |
|
---|
| 610 | // create new node
|
---|
| 611 | $this->composites[$composites] =& new cpaint_node();
|
---|
| 612 | $this->composites[$composites]->set_name($nodename);
|
---|
| 613 | $this->composites[$composites]->set_attribute('id', $id);
|
---|
| 614 | $this->composites[$composites]->set_encoding($this->input_encoding);
|
---|
| 615 |
|
---|
| 616 | return $this->composites[$composites];
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | /**
|
---|
| 620 | * assigns textual data to this node.
|
---|
| 621 | *
|
---|
| 622 | * @access public
|
---|
| 623 | * @param mixed $data data to assign to this node
|
---|
| 624 | * @return void
|
---|
| 625 | */
|
---|
| 626 | function set_data($data) {
|
---|
| 627 | $this->data = $data;
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | /**
|
---|
| 631 | * returns the textual data assigned to this node.
|
---|
| 632 | *
|
---|
| 633 | * @access public
|
---|
| 634 | * @return mixed
|
---|
| 635 | */
|
---|
| 636 | function get_data() {
|
---|
| 637 | return $this->data;
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | /**
|
---|
| 641 | * sets the id property of this node.
|
---|
| 642 | *
|
---|
| 643 | * @deprecated deprecated since version 2.0.0
|
---|
| 644 | * @access public
|
---|
| 645 | * @param string id the id
|
---|
| 646 | * @return void
|
---|
| 647 | */
|
---|
| 648 | function set_id($id) {
|
---|
| 649 | if ($id != '') {
|
---|
| 650 | $this->set_attribute('id', $id);
|
---|
| 651 | } // end: if
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | /**
|
---|
| 655 | * returns the id property if this node.
|
---|
| 656 | *
|
---|
| 657 | * @deprecated deprecated since version 2.0.0
|
---|
| 658 | * @access public
|
---|
| 659 | * @return string
|
---|
| 660 | */
|
---|
| 661 | function get_id() {
|
---|
| 662 | return $this->get_attribute('id');
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /**
|
---|
| 666 | * adds a new attribute to this node.
|
---|
| 667 | *
|
---|
| 668 | * @access public
|
---|
| 669 | * @param string $name attribute name
|
---|
| 670 | * @param mixed $value attribute value
|
---|
| 671 | * @return void
|
---|
| 672 | */
|
---|
| 673 | function set_attribute($name, $value) {
|
---|
| 674 | $this->attributes[$name] = (string) $value;
|
---|
| 675 | }
|
---|
| 676 |
|
---|
| 677 | /**
|
---|
| 678 | * retrieves an attribute by name.
|
---|
| 679 | *
|
---|
| 680 | * @access public
|
---|
| 681 | * @param string $name attribute name
|
---|
| 682 | * @return string
|
---|
| 683 | */
|
---|
| 684 | function get_attribute($name) {
|
---|
| 685 | return $this->attributes[$name];
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | /**
|
---|
| 689 | * set name property.
|
---|
| 690 | *
|
---|
| 691 | * @access public
|
---|
| 692 | * @param string $name the name
|
---|
| 693 | * @return void
|
---|
| 694 | */
|
---|
| 695 | function set_name($name) {
|
---|
| 696 | $this->nodename = (string) $name;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /**
|
---|
| 700 | * get name property.
|
---|
| 701 | *
|
---|
| 702 | * @access public
|
---|
| 703 | * @return string
|
---|
| 704 | */
|
---|
| 705 | function get_name() {
|
---|
| 706 | return $this->nodename;
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | /**
|
---|
| 710 | * sets the character encoding for this node
|
---|
| 711 | *
|
---|
| 712 | * @access public
|
---|
| 713 | * @param string $encoding character encoding
|
---|
| 714 | * @return void
|
---|
| 715 | */
|
---|
| 716 | function set_encoding($encoding) {
|
---|
| 717 | $this->input_encoding = strtoupper((string) $encoding);
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | /**
|
---|
| 721 | * returns the character encoding for this node
|
---|
| 722 | *
|
---|
| 723 | * @access public
|
---|
| 724 | * @return string
|
---|
| 725 | */
|
---|
| 726 | function get_encoding() {
|
---|
| 727 | return $this->input_encoding;
|
---|
| 728 | }
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | /**
|
---|
| 732 | * static class of output transformers.
|
---|
| 733 | *
|
---|
| 734 | * @package CPAINT
|
---|
| 735 | * @access public
|
---|
| 736 | * @author Dominique Stender <dstender@st-webdevelopment.de>
|
---|
| 737 | * @copyright 2002-2005 (Dominique Stender); All rights reserved
|
---|
| 738 | * @version 2.0.2
|
---|
| 739 | */
|
---|
| 740 | class cpaint_transformer {
|
---|
| 741 | /**
|
---|
| 742 | * toString method, used to generate response of type TEXT.
|
---|
| 743 | * will perform character transformation according to parameters.
|
---|
| 744 | *
|
---|
| 745 | * @access public
|
---|
| 746 | * @param object $node a cpaint_node object
|
---|
| 747 | * @return string
|
---|
| 748 | */
|
---|
| 749 | function toString(&$node) {
|
---|
| 750 | $return_value = '';
|
---|
| 751 |
|
---|
| 752 | foreach ($node->composites as $composite) {
|
---|
| 753 | $return_value .= cpaint_transformer::toString($composite);
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | $return_value .= cpaint_transformer::encode($node->get_data(), $node->get_encoding());
|
---|
| 757 |
|
---|
| 758 | return $return_value;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | /**
|
---|
| 762 | * XML response generator.
|
---|
| 763 | * will perform character transformation according to parameters.
|
---|
| 764 | *
|
---|
| 765 | * @access public
|
---|
| 766 | * @param object $node a cpaint_node object
|
---|
| 767 | * @return string
|
---|
| 768 | */
|
---|
| 769 | function toXML(&$node) {
|
---|
| 770 | $return_value = '<' . $node->get_name();
|
---|
| 771 |
|
---|
| 772 | // handle attributes
|
---|
| 773 | foreach ($node->attributes as $name => $value) {
|
---|
| 774 | if ($value != '') {
|
---|
| 775 | $return_value .= ' ' . $name . '="' . $node->get_attribute($name) . '"';
|
---|
| 776 | }
|
---|
| 777 | } // end: foreach
|
---|
| 778 |
|
---|
| 779 | $return_value .= '>';
|
---|
| 780 |
|
---|
| 781 | // handle subnodes
|
---|
| 782 | foreach ($node->composites as $composite) {
|
---|
| 783 | $return_value .= cpaint_transformer::toXML($composite);
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | $return_value .= cpaint_transformer::encode($node->get_data(), $node->get_encoding())
|
---|
| 787 | . '</' . $node->get_name() . '>';
|
---|
| 788 |
|
---|
| 789 | return $return_value;
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | /**
|
---|
| 793 | * JSON response generator.
|
---|
| 794 | * will perform character transformation according to parameters.
|
---|
| 795 | *
|
---|
| 796 | * @access public
|
---|
| 797 | * @param object $node a cpaint_node object
|
---|
| 798 | * @return string
|
---|
| 799 | */
|
---|
| 800 | function toJSON($node) {
|
---|
| 801 | $return_value = '';
|
---|
| 802 | $JSON_node = new stdClass();
|
---|
| 803 |
|
---|
| 804 | // handle attributes
|
---|
| 805 | $JSON_node->attributes = $node->attributes;
|
---|
| 806 |
|
---|
| 807 | // handle subnodes
|
---|
| 808 | foreach ($node->composites as $composite) {
|
---|
| 809 |
|
---|
| 810 | if (!is_array($JSON_node->{$composite->nodename})) {
|
---|
| 811 | $JSON_node->{$composite->nodename} = array();
|
---|
| 812 | } // end: if
|
---|
| 813 |
|
---|
| 814 | // we need to parse the JSON object again to avoid multiple encoding
|
---|
| 815 | $JSON_node->{$composite->nodename}[] = $GLOBALS['__cpaint_json']->parse(cpaint_transformer::toJSON($composite));
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 | // handle data
|
---|
| 819 | $JSON_node->data = $node->data;
|
---|
| 820 |
|
---|
| 821 | return $GLOBALS['__cpaint_json']->stringify($JSON_node);
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | /**
|
---|
| 825 | * performs conversion to JavaScript-safe UTF-8 characters
|
---|
| 826 | *
|
---|
| 827 | * @access public
|
---|
| 828 | * @param string $data data to convert
|
---|
| 829 | * @param string $encoding character encoding
|
---|
| 830 | * @return string
|
---|
| 831 | */
|
---|
| 832 | function encode($data, $encoding) {
|
---|
| 833 | // convert string
|
---|
| 834 | if (function_exists('iconv')) {
|
---|
| 835 | // iconv is by far the most flexible approach, try this first
|
---|
| 836 | $return_value = iconv($encoding, 'UTF-8', $data);
|
---|
| 837 |
|
---|
| 838 | } elseif ($encoding == 'ISO-8859-1') {
|
---|
| 839 | // for ISO-8859-1 we can use utf8-encode()
|
---|
| 840 | $return_value = utf8_encode($data);
|
---|
| 841 |
|
---|
| 842 | } else {
|
---|
| 843 | // give up. if UTF-8 data was supplied everything is fine!
|
---|
| 844 | $return_value = $data;
|
---|
| 845 | } /* end: if */
|
---|
| 846 |
|
---|
| 847 | // now encode non-printable characters
|
---|
[7655] | 848 | for ($i = 0; $i < 32; ++$i) {
|
---|
[3400] | 849 | $return_value = str_replace(chr($i), '\u00' . sprintf('%02x', $i), $return_value);
|
---|
| 850 | } // end: for
|
---|
| 851 |
|
---|
| 852 | // encode <, >, and & respectively for XML sanity
|
---|
| 853 | $return_value = str_replace(chr(0x26), '\u0026', $return_value);
|
---|
| 854 | $return_value = str_replace(chr(0x3c), '\u003c', $return_value);
|
---|
| 855 | $return_value = str_replace(chr(0x3e), '\u003e', $return_value);
|
---|
| 856 |
|
---|
| 857 | return $return_value;
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | /**
|
---|
| 861 | * performs conversion from JavaScript encodeURIComponent() string (UTF-8) to
|
---|
| 862 | * the charset in use.
|
---|
| 863 | *
|
---|
| 864 | * @access public
|
---|
| 865 | * @param string $data data to convert
|
---|
| 866 | * @param string $encoding character encoding
|
---|
| 867 | * @return string
|
---|
| 868 | */
|
---|
| 869 | function decode($data, $encoding) {
|
---|
| 870 | // convert string
|
---|
| 871 |
|
---|
| 872 | if (is_string($data)) {
|
---|
| 873 | if (function_exists('iconv')) {
|
---|
| 874 | // iconv is by far the most flexible approach, try this first
|
---|
| 875 | $return_value = iconv('UTF-8', $encoding, $data);
|
---|
| 876 |
|
---|
| 877 | } elseif ($encoding == 'ISO-8859-1') {
|
---|
| 878 | // for ISO-8859-1 we can use utf8-decode()
|
---|
| 879 | $return_value = utf8_decode($data);
|
---|
| 880 |
|
---|
| 881 | } else {
|
---|
| 882 | // give up. if data was supplied in the correct format everything is fine!
|
---|
| 883 | $return_value = $data;
|
---|
| 884 | } // end: if
|
---|
| 885 |
|
---|
| 886 | } else {
|
---|
| 887 | // non-string value
|
---|
| 888 | $return_value = $data;
|
---|
| 889 | } // end: if
|
---|
| 890 |
|
---|
| 891 | return $return_value;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 | /**
|
---|
| 895 | * decodes a (nested) array of data from UTF-8 into the configured character set
|
---|
| 896 | *
|
---|
| 897 | * @access public
|
---|
| 898 | * @param array $data data to convert
|
---|
| 899 | * @param string $encoding character encoding
|
---|
| 900 | * @return array
|
---|
| 901 | */
|
---|
| 902 | function decode_array($data, $encoding) {
|
---|
| 903 | $return_value = array();
|
---|
| 904 |
|
---|
| 905 | foreach ($data as $key => $value) {
|
---|
| 906 |
|
---|
| 907 | if (!is_array($value)) {
|
---|
| 908 | $return_value[$key] = cpaint_transformer::decode($value, $encoding);
|
---|
| 909 |
|
---|
| 910 | } else {
|
---|
| 911 | $return_value[$key] = cpaint_transformer::decode_array($value, $encoding);
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | return $return_value;
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 | /**
|
---|
| 919 | * determines the output character set
|
---|
| 920 | * based on input character set
|
---|
| 921 | *
|
---|
| 922 | * @access public
|
---|
| 923 | * @param string $encoding character encoding
|
---|
| 924 | * @return string
|
---|
| 925 | */
|
---|
| 926 | function find_output_charset($encoding) {
|
---|
| 927 | $return_value = 'UTF-8';
|
---|
| 928 |
|
---|
| 929 | if (function_exists('iconv')
|
---|
| 930 | || $encoding == 'UTF-8'
|
---|
| 931 | || $encoding == 'ISO-8859-1') {
|
---|
| 932 |
|
---|
| 933 | $return_value = 'UTF-8';
|
---|
| 934 |
|
---|
| 935 | } else {
|
---|
| 936 | $return_value = $encoding;
|
---|
| 937 | } // end: if
|
---|
| 938 |
|
---|
| 939 | return $return_value;
|
---|
| 940 | }
|
---|
| 941 | }
|
---|
| 942 |
|
---|
| 943 | ?> |
---|