| 1 |
joko |
1.1 |
<?php |
| 2 |
|
|
|
| 3 |
|
|
/** |
| 4 |
|
|
* This file contains some core functions extending php. |
| 5 |
|
|
* |
| 6 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
| 7 |
|
|
* @package org.netfrag.glib |
| 8 |
|
|
* @name php |
| 9 |
|
|
* |
| 10 |
|
|
*/ |
| 11 |
|
|
|
| 12 |
|
|
/** |
| 13 |
joko |
1.8 |
* $Id: extensions.php,v 1.7 2003/06/25 23:41:14 joko Exp $ |
| 14 |
jonen |
1.2 |
* |
| 15 |
|
|
* $Log: extensions.php,v $ |
| 16 |
joko |
1.8 |
* Revision 1.7 2003/06/25 23:41:14 joko |
| 17 |
|
|
* now using "global $Dumper_mode" |
| 18 |
|
|
* |
| 19 |
joko |
1.7 |
* Revision 1.6 2003/04/16 16:24:23 joko |
| 20 |
|
|
* + odd and even from php.net |
| 21 |
|
|
* |
| 22 |
joko |
1.6 |
* Revision 1.5 2003/04/11 01:31:50 joko |
| 23 |
|
|
* + function log (from flib-shortcuts) |
| 24 |
|
|
* |
| 25 |
joko |
1.5 |
* Revision 1.4 2003/04/09 07:55:47 joko |
| 26 |
|
|
* + function untwingle_reference |
| 27 |
|
|
* |
| 28 |
joko |
1.4 |
* Revision 1.3 2003/04/08 22:35:16 joko |
| 29 |
|
|
* + 'function yesno' from utils/links.php |
| 30 |
|
|
* |
| 31 |
joko |
1.3 |
* Revision 1.2 2003/04/06 01:37:31 jonen |
| 32 |
|
|
* + added functions to generate and handle unique ID's |
| 33 |
|
|
* |
| 34 |
jonen |
1.2 |
* Revision 1.1 2003/04/05 19:53:45 joko |
| 35 |
|
|
* moved here from ../ |
| 36 |
joko |
1.1 |
* |
| 37 |
|
|
* Revision 1.7 2003/04/04 17:35:40 joko |
| 38 |
|
|
* fixed php croaking |
| 39 |
|
|
* moved error handling function to 'php_errors.php' |
| 40 |
|
|
* + function php::maintenance |
| 41 |
|
|
* |
| 42 |
|
|
* Revision 1.6 2003/03/11 02:14:18 joko |
| 43 |
|
|
* + fixed metadata for phpDocumentor |
| 44 |
|
|
* |
| 45 |
|
|
* Revision 1.5 2003/03/10 22:58:46 joko |
| 46 |
|
|
* + fixed metadata for phpDocumentor |
| 47 |
|
|
* |
| 48 |
|
|
* Revision 1.4 2003/03/05 23:16:46 joko |
| 49 |
|
|
* updated docu - phpDocumentor is very strict about its 'blocks'... |
| 50 |
|
|
* |
| 51 |
|
|
* Revision 1.3 2003/03/05 18:54:41 joko |
| 52 |
|
|
* updated docu - phpDocumentor is very strict about its 'blocks'... |
| 53 |
|
|
* |
| 54 |
|
|
* Revision 1.2 2003/03/05 11:58:49 joko |
| 55 |
|
|
* modified is_hash, mkInstance and others |
| 56 |
|
|
* |
| 57 |
|
|
* Revision 1.1 2003/03/03 21:08:21 joko |
| 58 |
|
|
* refactored from flib/utils |
| 59 |
|
|
* |
| 60 |
|
|
* |
| 61 |
|
|
*/ |
| 62 |
|
|
|
| 63 |
|
|
|
| 64 |
|
|
/** |
| 65 |
|
|
* --- shortcut functions |
| 66 |
|
|
* Is there a mechanism in php to 'export' these methods |
| 67 |
|
|
* to the global symbol table automagically? |
| 68 |
|
|
* We wouldn't require declaring these shortcuts below and |
| 69 |
|
|
* could also do some nice stuff at runtime. |
| 70 |
|
|
* (maybe resembling CPAN's Exporter) |
| 71 |
|
|
* |
| 72 |
|
|
* Okay, 'eval' and 'create_function' are alternatives, but i'm |
| 73 |
|
|
* still looking for something that would let us manipulate the |
| 74 |
|
|
* symbol table.... (would also be a nice-to-have feature for |
| 75 |
|
|
* mungling "other" symbol table entries, like variables |
| 76 |
|
|
* and/or object references etc.) |
| 77 |
|
|
* |
| 78 |
|
|
* TODO: $php::EXPORT_OK = array('Dumper', 'session_register_safe', 'mkObject', 'is_hash', 'merge_to'); |
| 79 |
|
|
* |
| 80 |
|
|
*/ |
| 81 |
|
|
// this is (and will probably always be) required by the "Exporter" |
| 82 |
|
|
function &Dumper() { |
| 83 |
|
|
$args = func_get_args(); |
| 84 |
|
|
// shrink array to single item? |
| 85 |
|
|
//if (sizeof($args) == 1) { $args = $args[0]; } |
| 86 |
|
|
php::array_shrink($args); |
| 87 |
|
|
return php::Dumper($args); |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
// these could be refactore using Exporter proposal below, |
| 91 |
|
|
// which just does this code at runtime via eval |
| 92 |
|
|
//function &session_register_safe() { return php::session_register_safe(func_get_args()); } |
| 93 |
|
|
//function &is_hash() { return php::is_hash(func_get_args()); } |
| 94 |
|
|
//function &loadModule() { return php::loadModule(func_get_args()); } |
| 95 |
|
|
//function &mkObject() { return php::mkInstance(func_get_args()); } |
| 96 |
|
|
|
| 97 |
|
|
|
| 98 |
|
|
//exit; |
| 99 |
|
|
|
| 100 |
|
|
// yes! |
| 101 |
|
|
php::export_symbols(); |
| 102 |
|
|
|
| 103 |
|
|
|
| 104 |
|
|
|
| 105 |
|
|
/** |
| 106 |
|
|
* --- php extension functions living in the php:: namespace |
| 107 |
|
|
* |
| 108 |
|
|
* <pre> |
| 109 |
|
|
* x php::Dumper |
| 110 |
|
|
* x php::session_register_safe |
| 111 |
|
|
* x php::array_init |
| 112 |
|
|
* x php::is_hash |
| 113 |
|
|
* x php::array_join_merge |
| 114 |
|
|
* o php::merge |
| 115 |
|
|
* o php::merge_to |
| 116 |
|
|
* x php::loadModule |
| 117 |
|
|
* x php::mkInstance |
| 118 |
|
|
* </pre> |
| 119 |
|
|
* |
| 120 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
| 121 |
|
|
* @copyright (c) 2003 - All Rights reserved. |
| 122 |
|
|
* @license GNU LGPL (GNU Lesser General Public License) |
| 123 |
|
|
* |
| 124 |
|
|
* @link http://www.netfrag.org/~joko/ |
| 125 |
|
|
* @link http://www.gnu.org/licenses/lgpl.txt |
| 126 |
|
|
* |
| 127 |
|
|
* @package org.netfrag.glib |
| 128 |
|
|
* @name php |
| 129 |
|
|
* |
| 130 |
|
|
* <pre> |
| 131 |
|
|
* Todo: |
| 132 |
|
|
* @todo (o) establish mkInstance here (move from DesignPattern::Object::mkObject) |
| 133 |
|
|
* @todo (o) establish loadModule here (move from DesignPattern::Object::loadModule) |
| 134 |
|
|
* |
| 135 |
|
|
* Ideas: |
| 136 |
|
|
* @todo (o) php::create_redirector_function('func', array( type => 'lamba|function|method', name => 'Abc::do_xyz') ) |
| 137 |
|
|
* @todo (o) php::create_redirector_method('class|obj_ref', 'method', $target) |
| 138 |
|
|
* @todo (o) php::export_symbol(from, to) (maybe solves above two) |
| 139 |
|
|
* </pre> |
| 140 |
|
|
* |
| 141 |
|
|
*/ |
| 142 |
|
|
class php { |
| 143 |
|
|
|
| 144 |
|
|
|
| 145 |
|
|
// Exporter proposal |
| 146 |
|
|
|
| 147 |
|
|
// does (e.g.) Exporter::export_symbol('php', 'loadModule', array( php_function => 'loadModule' )); |
| 148 |
|
|
// (or) Exporter::export_symbol('php', 'mkInstance', array( php_function => 'mkObject' )); |
| 149 |
|
|
// -> just all from EXPORT_OK |
| 150 |
|
|
|
| 151 |
|
|
function export_symbols() { |
| 152 |
|
|
|
| 153 |
|
|
$symbols = func_get_args(); |
| 154 |
|
|
php::array_shrink($symbols); |
| 155 |
|
|
|
| 156 |
|
|
global $PHP_EXTENSIONS_EXPORT; |
| 157 |
|
|
//print Dumper($PHP_EXTENSIONS_EXPORT); |
| 158 |
|
|
//exit; |
| 159 |
|
|
|
| 160 |
|
|
if (is_array($PHP_EXTENSIONS_EXPORT)) { |
| 161 |
|
|
//if (defined('EXPORT_OK')) { |
| 162 |
|
|
//print EXPORT_OK . "<br/>"; |
| 163 |
|
|
//$symbols = split(' ', EXPORT_OK); |
| 164 |
|
|
//foreach ($php->EXPORT_OK as $symbol) { |
| 165 |
|
|
/* |
| 166 |
|
|
foreach (EXPORT_OK as $symbol) { |
| 167 |
|
|
print "exporting: $symbol<br/>"; |
| 168 |
|
|
Exporter::export_symbol('php', $symbol, array( php_function => $symbol )); |
| 169 |
|
|
} |
| 170 |
|
|
*/ |
| 171 |
|
|
|
| 172 |
|
|
$symbols = php::array_join_merge($PHP_EXTENSIONS_EXPORT, $symbols); |
| 173 |
|
|
|
| 174 |
|
|
php::loadModule('Exporter'); |
| 175 |
|
|
if (class_exists('Exporter')) { |
| 176 |
|
|
Exporter::export_symbols('php', $symbols); |
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
} |
| 180 |
|
|
|
| 181 |
|
|
} |
| 182 |
|
|
|
| 183 |
|
|
function Dumper() { |
| 184 |
joko |
1.7 |
global $Dumper_mode; |
| 185 |
joko |
1.1 |
$arg_list = func_get_args(); |
| 186 |
|
|
$count = 1; |
| 187 |
|
|
|
| 188 |
|
|
// build dump by catching output of php's 'print_r' |
| 189 |
|
|
// using php's output buffering functions |
| 190 |
|
|
ob_start(); |
| 191 |
|
|
foreach ($arg_list as $arg) { |
| 192 |
|
|
print_r($arg); |
| 193 |
|
|
if (is_string($arg)) { |
| 194 |
|
|
print "\n"; |
| 195 |
|
|
} |
| 196 |
|
|
$count++; |
| 197 |
|
|
} |
| 198 |
|
|
$var_dump = ob_get_contents(); |
| 199 |
|
|
ob_end_clean(); |
| 200 |
|
|
|
| 201 |
|
|
//print "mode: " . $this->Dumper_mode . "<br/>"; |
| 202 |
joko |
1.7 |
if ($Dumper_mode == 'single_line') { |
| 203 |
|
|
$var_dump = str_replace("\n", ' ', $var_dump); |
| 204 |
|
|
} else { |
| 205 |
joko |
1.1 |
$var_dump = str_replace("\n", '<br/>', $var_dump); |
| 206 |
|
|
$var_dump = str_replace(" ", ' ', $var_dump); |
| 207 |
joko |
1.7 |
} |
| 208 |
joko |
1.1 |
|
| 209 |
|
|
//if (sizeof($args) == 1) { $var_dump .= '<br/>'; } |
| 210 |
|
|
|
| 211 |
|
|
return $var_dump; |
| 212 |
|
|
} |
| 213 |
|
|
|
| 214 |
|
|
function session_register_safe($varname) { |
| 215 |
|
|
if (!session_is_registered($varname)) { |
| 216 |
|
|
session_register($varname); |
| 217 |
|
|
return 1; |
| 218 |
|
|
} |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
// ------------------------------------------------- |
| 222 |
|
|
// array initializer |
| 223 |
|
|
// initializes passed variable (taken by reference) with an empty array |
| 224 |
|
|
// does this only if it doesn't exist yet or initialization is explicitely requested ($clear = 1) |
| 225 |
|
|
function array_init(&$var, $clear = 0) { |
| 226 |
|
|
if (!is_array($var) || $clear) { $var = array(); } |
| 227 |
|
|
} |
| 228 |
|
|
|
| 229 |
|
|
// from: php.net - http://www.php.net/manual/en/function.array-merge-recursive.php |
| 230 |
|
|
function is_hash( $var = null ) { |
| 231 |
|
|
if( is_array( $var ) ) { |
| 232 |
|
|
$keys = array_keys( $var ); |
| 233 |
|
|
$all_num = true; |
| 234 |
|
|
for( $i=0; $i<count($keys); $i++ ) |
| 235 |
|
|
if( is_string($keys[$i] ) ) return true; |
| 236 |
|
|
} |
| 237 |
|
|
return false; |
| 238 |
|
|
} |
| 239 |
|
|
|
| 240 |
|
|
// from: php.net - http://www.php.net/manual/en/function.array-merge-recursive.php |
| 241 |
|
|
function array_join_merge( $arr1, $arr2 ) { |
| 242 |
|
|
if( is_array( $arr1 ) and is_array( $arr2 ) ) { |
| 243 |
|
|
// the same -> merge |
| 244 |
|
|
$new_array = array(); |
| 245 |
|
|
|
| 246 |
|
|
if( php::is_hash( $arr1 ) and php::is_hash( $arr2 ) ) { |
| 247 |
|
|
// hashes -> merge based on keys |
| 248 |
|
|
$keys = array_merge( array_keys( $arr1 ), array_keys( $arr2 ) ); |
| 249 |
|
|
foreach( $keys as $key ) { |
| 250 |
|
|
$new_array[$key] = php::array_join_merge( $arr1[$key], $arr2[$key] ); |
| 251 |
|
|
} |
| 252 |
|
|
} else { |
| 253 |
|
|
// two real arrays -> merge |
| 254 |
|
|
$new_array = |
| 255 |
|
|
array_reverse(array_unique(array_reverse(array_merge($arr1,$arr2)))); |
| 256 |
|
|
} |
| 257 |
|
|
|
| 258 |
|
|
return $new_array; |
| 259 |
|
|
} else { |
| 260 |
|
|
// not the same ... take new one if defined, else the old one stays |
| 261 |
|
|
return $arr2 ? $arr2 : $arr1; |
| 262 |
|
|
} |
| 263 |
|
|
} |
| 264 |
|
|
|
| 265 |
|
|
|
| 266 |
|
|
// TODO: |
| 267 |
|
|
// o refactor to the php:: namespace? |
| 268 |
|
|
// o PEAR::??? |
| 269 |
|
|
// x rename mkObject to mkInstance |
| 270 |
|
|
// x rename loadModule? let it like is.... (suggestions? loadPackage?) |
| 271 |
|
|
// o enhance loadModule: make it possible to load packages from anywhere?! #?)&%$ |
| 272 |
|
|
|
| 273 |
|
|
function namespace2filename($nsName) { |
| 274 |
|
|
if ($filename = str_replace('::', '/', $nsName)) { |
| 275 |
|
|
return $filename; |
| 276 |
|
|
} |
| 277 |
|
|
} |
| 278 |
|
|
|
| 279 |
|
|
function loadModule($namespacedClassname, $options = null) { |
| 280 |
|
|
|
| 281 |
|
|
if (DEBUG_PHP_EXTENSIONS == 1) { |
| 282 |
|
|
print "php::loadModule: " . Dumper($namespacedClassname); |
| 283 |
|
|
} |
| 284 |
|
|
if (LOG_PHP_EXTENSIONS == 1) { |
| 285 |
|
|
php::append_log( "php::loadModule( $namespacedClassname )" ); |
| 286 |
|
|
} |
| 287 |
|
|
|
| 288 |
|
|
if ($filename = php::namespace2filename($namespacedClassname)) { |
| 289 |
|
|
|
| 290 |
|
|
if (is_array($options)) { |
| 291 |
|
|
$extension = $options[extension]; |
| 292 |
|
|
} elseif ($options) { |
| 293 |
|
|
$extension = $options; |
| 294 |
|
|
} else { |
| 295 |
|
|
$extension = 'php'; |
| 296 |
|
|
} |
| 297 |
|
|
|
| 298 |
|
|
$filename .= ".$extension"; |
| 299 |
|
|
|
| 300 |
|
|
// V1: just require |
| 301 |
|
|
//require_once($filename); |
| 302 |
|
|
|
| 303 |
|
|
// V2: check file for existance (didn't work!!! problem with path-style conversion regarding platform running on?) |
| 304 |
|
|
/* |
| 305 |
|
|
if (file_exists($filename)) { |
| 306 |
|
|
require_once($filename); |
| 307 |
|
|
} else { |
| 308 |
|
|
//user_error("Could not load module '$namespacedClassname', file '$filename' does not exist."); |
| 309 |
|
|
} |
| 310 |
|
|
*/ |
| 311 |
|
|
|
| 312 |
|
|
// V3: wrapped through eval (weird) |
| 313 |
|
|
/* |
| 314 |
|
|
$evalstring = "return include_once('$filename');"; |
| 315 |
|
|
$result = eval($evalstring); |
| 316 |
|
|
if (!$result) { |
| 317 |
|
|
user_error("DesignPattern::Object: Could not load module '$namespacedClassname', file '$filename' does not exist."); |
| 318 |
|
|
return; |
| 319 |
|
|
} |
| 320 |
|
|
*/ |
| 321 |
|
|
|
| 322 |
|
|
// V4: just include |
| 323 |
|
|
//define ("WARNING", E_USER_NOTICE); |
| 324 |
|
|
//error_reporting(E_USER_WARNING); |
| 325 |
|
|
//error_reporting(E_ALL); |
| 326 |
|
|
//error_reporting(E_CORE_ERROR); |
| 327 |
|
|
//set_error_handler(array('php::error_handler_2')); |
| 328 |
|
|
|
| 329 |
|
|
//set_error_handler('php_error_handler'); |
| 330 |
|
|
// TODO: do stack-backtracing here to determine function called two or three levels before!!! |
| 331 |
|
|
//print "file: $filename<br/>"; |
| 332 |
|
|
|
| 333 |
|
|
if (!include_once($filename)) { |
| 334 |
|
|
//if (!@include_once($filename)) { |
| 335 |
|
|
$msg = "php::loadModule error: Could not load module '$namespacedClassname', file '$filename' does not exist."; |
| 336 |
|
|
user_error($msg); |
| 337 |
|
|
php::append_log($msg, PEAR_LOG_ERR); |
| 338 |
|
|
//trigger_error($msg); |
| 339 |
|
|
//print "msg: $msg<br/>"; |
| 340 |
|
|
return; |
| 341 |
|
|
} |
| 342 |
|
|
//restore_error_handler(); |
| 343 |
|
|
|
| 344 |
|
|
//exit; |
| 345 |
|
|
|
| 346 |
|
|
return 1; |
| 347 |
|
|
} |
| 348 |
|
|
} |
| 349 |
|
|
|
| 350 |
|
|
|
| 351 |
|
|
function &mkComponent() { |
| 352 |
|
|
|
| 353 |
|
|
// argument handling - perl style |
| 354 |
|
|
$arg_list = &func_get_args(); |
| 355 |
|
|
|
| 356 |
|
|
// trace |
| 357 |
|
|
//print "php::mkInstance: " . Dumper($arg_list); |
| 358 |
|
|
//exit; |
| 359 |
|
|
|
| 360 |
|
|
// patch arglist if all arguments are passed in as a single array |
| 361 |
|
|
if (sizeof($arg_list) == 1 && is_array($arg_list[0])) { |
| 362 |
|
|
$arg_list = &$arg_list[0]; |
| 363 |
|
|
} |
| 364 |
|
|
|
| 365 |
|
|
//print "arg_list: $arg_list<br/>"; |
| 366 |
|
|
$namespacedClassname = array_shift($arg_list); |
| 367 |
|
|
|
| 368 |
|
|
// debug |
| 369 |
|
|
if (DEBUG_PHP_EXTENSIONS == 1) { |
| 370 |
|
|
print "php::mkComponent: $namespacedClassname<br/>"; |
| 371 |
|
|
} |
| 372 |
|
|
if (LOG_PHP_EXTENSIONS == 1) { |
| 373 |
|
|
php::append_log( "php::mkComponent( $namespacedClassname )" ); |
| 374 |
|
|
} |
| 375 |
|
|
|
| 376 |
|
|
$classname = $namespacedClassname; |
| 377 |
|
|
$filename = $namespacedClassname; |
| 378 |
|
|
$load_module_options = null; |
| 379 |
|
|
|
| 380 |
|
|
if (strstr($namespacedClassname, '_')) { |
| 381 |
|
|
//print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; |
| 382 |
|
|
//return; |
| 383 |
|
|
} |
| 384 |
|
|
|
| 385 |
|
|
if (strstr($namespacedClassname, '.')) { |
| 386 |
|
|
//print "mkObject: unallowed character in namespaced classname: '_' ($namespacedClassname)<br/>"; |
| 387 |
|
|
//return; |
| 388 |
|
|
$parts = split('\.', $namespacedClassname); |
| 389 |
|
|
$filename = $parts[0]; |
| 390 |
|
|
$extension = $parts[sizeof($parts) - 1]; |
| 391 |
|
|
$load_module_options = array( extension => $extension ); |
| 392 |
|
|
} |
| 393 |
|
|
|
| 394 |
|
|
|
| 395 |
|
|
// trace |
| 396 |
|
|
//print "mkObject: $classname<br/>"; |
| 397 |
|
|
//print "file: $filename<br/>"; |
| 398 |
|
|
|
| 399 |
|
|
// build native class name from namespaced one |
| 400 |
|
|
$classname = str_replace('::', '_', $classname); |
| 401 |
|
|
$classname = str_replace('/', '_', $classname); |
| 402 |
|
|
|
| 403 |
|
|
// load class-file |
| 404 |
|
|
if (!class_exists($classname)) { |
| 405 |
|
|
php::loadModule($filename, $load_module_options); |
| 406 |
|
|
} |
| 407 |
|
|
|
| 408 |
|
|
// instantiate object |
| 409 |
|
|
$attributes = $arg_list; |
| 410 |
|
|
// V0: |
| 411 |
|
|
//$obj = new $classname($attributes); |
| 412 |
|
|
// V1: |
| 413 |
|
|
//$obj = new DesignPattern_Bridge($classname, $attributes); |
| 414 |
|
|
// V2: |
| 415 |
|
|
$obj = php::mkInstance($classname, $attributes); |
| 416 |
|
|
|
| 417 |
|
|
// constructor autocall - if possible |
| 418 |
|
|
// FIXME: take care for side-effects!!! |
| 419 |
|
|
/* |
| 420 |
|
|
if (method_exists($obj, 'constructor')) { |
| 421 |
|
|
$obj->constructor(); |
| 422 |
|
|
} |
| 423 |
|
|
*/ |
| 424 |
|
|
|
| 425 |
|
|
// return instance |
| 426 |
|
|
return $obj; |
| 427 |
|
|
|
| 428 |
|
|
} |
| 429 |
|
|
|
| 430 |
|
|
// from: http://www.php.net/manual/en/function.method-exists.php |
| 431 |
|
|
function class_has_method($className, $methodName) { |
| 432 |
joko |
1.8 |
|
| 433 |
|
|
// new as of 2004-07-21: php5 compatibility |
| 434 |
|
|
// taken from http://php.net/manual/en/function.version-compare.php |
| 435 |
|
|
if (version_compare(phpversion(), "5.0.0", "<")) { |
| 436 |
|
|
$methodName = strtolower($methodName); |
| 437 |
|
|
} |
| 438 |
|
|
|
| 439 |
|
|
$bool_exists = (in_array($methodName, get_class_methods($className))); |
| 440 |
joko |
1.1 |
return $bool_exists; |
| 441 |
|
|
} |
| 442 |
|
|
|
| 443 |
|
|
// down: array( item ) => item |
| 444 |
|
|
function &array_shrink(&$array) { |
| 445 |
|
|
// shrink array to single item? yes! |
| 446 |
|
|
if (is_array($array) && !php::is_hash($array) && (sizeof($array) == 1)) { $array = $array[0]; } |
| 447 |
|
|
return $array; |
| 448 |
|
|
} |
| 449 |
|
|
|
| 450 |
|
|
function append_log($message, $level = PEAR_LOG_DEBUG) { |
| 451 |
|
|
static $Class_Logger_loaded; |
| 452 |
|
|
static $Class_Logger_instance; |
| 453 |
|
|
if (!$Class_Logger_loaded) { |
| 454 |
|
|
$Class_Logger_loaded++; |
| 455 |
|
|
//print "LOAD<br/>"; |
| 456 |
|
|
php::loadModule('Class::Logger'); |
| 457 |
|
|
} |
| 458 |
|
|
if (class_exists('Class_Logger')) { |
| 459 |
|
|
//$Class_Logger_instance = new Class_Logger(); |
| 460 |
|
|
$Class_Logger_instance = php::mkComponent('Class::Logger'); |
| 461 |
|
|
//print "YAI<br/>"; |
| 462 |
|
|
//Class_Logger::log($message); |
| 463 |
|
|
$Class_Logger_instance->log($message, $level); |
| 464 |
|
|
} else { |
| 465 |
|
|
// FIXME! pre-logger log-messages (two or three): |
| 466 |
|
|
// [PEAR_LOG_DEBUG] php::loadModule( Class::Logger ) |
| 467 |
|
|
// [PEAR_LOG_DEBUG] php::loadModule( DesignPattern::AbstractClass ) |
| 468 |
|
|
//print "[$level] $message" . "<br/>"; |
| 469 |
|
|
} |
| 470 |
|
|
} |
| 471 |
|
|
|
| 472 |
|
|
function &mkInstance($classname, $attributes = array()) { |
| 473 |
|
|
|
| 474 |
|
|
$oo = is_object($this); |
| 475 |
|
|
|
| 476 |
|
|
// autocall constructor? |
| 477 |
|
|
if ($oo) { |
| 478 |
|
|
//print Dumper($this); |
| 479 |
|
|
//exit; |
| 480 |
|
|
//print "this: $this<br/>"; |
| 481 |
|
|
//print "parent: $parent<br/>"; |
| 482 |
|
|
// V1: |
| 483 |
|
|
//parent::constructor(); |
| 484 |
|
|
// V2: |
| 485 |
|
|
//call_user_func('parent::constructor'); |
| 486 |
|
|
// V3: |
| 487 |
|
|
//call_user_func(array($this, 'constructor')); |
| 488 |
|
|
if (method_exists($this, 'constructor')) { |
| 489 |
|
|
//print "YAI<br/>"; |
| 490 |
|
|
//call_user_func(array($this, 'constructor')); |
| 491 |
|
|
} |
| 492 |
|
|
} |
| 493 |
|
|
|
| 494 |
|
|
if ($oo) { |
| 495 |
|
|
//$this->log( get_class($this) . "->mkInstance( classname $classname )" ); |
| 496 |
|
|
//Class_Logger::log( get_class($this) . "->mkInstance( classname $classname )" ); |
| 497 |
|
|
} else { |
| 498 |
|
|
//php::append_log( "php::mkInstance( classname $classname )" ); |
| 499 |
|
|
} |
| 500 |
|
|
|
| 501 |
|
|
if (isset($attributes)) { |
| 502 |
|
|
//print Dumper($attributes); |
| 503 |
|
|
|
| 504 |
|
|
/* |
| 505 |
|
|
// pass single argument 1:1 |
| 506 |
|
|
if (count($attributes) == 1) { |
| 507 |
|
|
$attributes_merged = $attributes[0]; |
| 508 |
|
|
|
| 509 |
|
|
|
| 510 |
|
|
|
| 511 |
|
|
// pass hash 1:1 |
| 512 |
|
|
} elseif (is_hash($attributes)) { |
| 513 |
|
|
$attributes_merged = $attributes; |
| 514 |
|
|
|
| 515 |
|
|
} else { |
| 516 |
|
|
$attributes_merged = $attributes; |
| 517 |
|
|
} |
| 518 |
|
|
*/ |
| 519 |
|
|
|
| 520 |
|
|
$args_pass = array(); |
| 521 |
|
|
for ($i=0; $i<=count($attributes); $i++) { |
| 522 |
|
|
array_push($args_pass, '&$attributes[' . $i . ']'); |
| 523 |
|
|
} |
| 524 |
|
|
|
| 525 |
|
|
$arg_string = join(', ', $args_pass); |
| 526 |
|
|
|
| 527 |
|
|
/* |
| 528 |
|
|
// merge entries of numerical indexed arrays together into one hash |
| 529 |
|
|
} else { |
| 530 |
|
|
$attributes_merged = array(); |
| 531 |
|
|
foreach ($attributes as $entry) { |
| 532 |
|
|
$attributes_merged = array_merge($attributes_merged, $entry); |
| 533 |
|
|
} |
| 534 |
|
|
} |
| 535 |
|
|
*/ |
| 536 |
|
|
|
| 537 |
|
|
if (!class_exists($classname)) { |
| 538 |
joko |
1.7 |
php::log( "Class '$classname' doesn't exist.", PEAR_LOG_ERR ); |
| 539 |
joko |
1.1 |
return; |
| 540 |
|
|
} |
| 541 |
|
|
|
| 542 |
|
|
//print Dumper($attributes_merged); |
| 543 |
|
|
//print Dumper($attributes); |
| 544 |
|
|
//$instance = new $classname($attributes_merged); |
| 545 |
|
|
//$instance = new $classname($attributes[0]); |
| 546 |
|
|
$evalstr = 'return new $classname(' . $arg_string . ');'; |
| 547 |
|
|
|
| 548 |
|
|
//print "eval: $evalstr<br/>"; |
| 549 |
|
|
|
| 550 |
|
|
$instance = eval($evalstr); |
| 551 |
|
|
//print $evalstr . "<br/>"; |
| 552 |
|
|
} else { |
| 553 |
|
|
$instance = new $classname; |
| 554 |
|
|
} |
| 555 |
|
|
//$this->log("ok"); |
| 556 |
|
|
return $instance; |
| 557 |
|
|
} |
| 558 |
|
|
|
| 559 |
|
|
// up: string -> array |
| 560 |
|
|
function array_cast_safe(&$array_or_not) { |
| 561 |
|
|
if (!is_array($array_or_not)) { |
| 562 |
|
|
$array_or_not = array($array_or_not); |
| 563 |
|
|
//print Dumper($array_or_not); |
| 564 |
|
|
//exit; |
| 565 |
|
|
} |
| 566 |
|
|
} |
| 567 |
|
|
|
| 568 |
|
|
// To return all ancestors class of an object |
| 569 |
|
|
// from: http://www.php.net/manual/en/function.get-parent-class.php |
| 570 |
|
|
function get_ancestors_class($classname) { |
| 571 |
|
|
$father = get_parent_class($classname); |
| 572 |
|
|
if ($father != "") { |
| 573 |
|
|
$ancestors = php::get_ancestors_class($father); |
| 574 |
|
|
$ancestors[] = $father; |
| 575 |
|
|
} |
| 576 |
|
|
return $ancestors; |
| 577 |
|
|
} |
| 578 |
|
|
|
| 579 |
|
|
// ------ override/expand php's 'include_path' setting ------ |
| 580 |
|
|
function add_libpath($paths) { |
| 581 |
|
|
|
| 582 |
|
|
if (!is_array($paths)) { |
| 583 |
|
|
$paths = array($paths); |
| 584 |
|
|
} |
| 585 |
|
|
array_push($paths, ini_get("include_path")); |
| 586 |
|
|
|
| 587 |
|
|
// determine OS |
| 588 |
|
|
$os = 'linux'; |
| 589 |
|
|
if (stristr($_SERVER["SERVER_SOFTWARE"], 'win32')) { |
| 590 |
|
|
$os = 'windows'; |
| 591 |
|
|
} |
| 592 |
|
|
|
| 593 |
|
|
$path_delimiter = ':'; |
| 594 |
|
|
// change path-delimiter for win32 |
| 595 |
|
|
if ($os == 'windows') { $path_delimiter = ';'; } |
| 596 |
|
|
// build new 'include_path'-string |
| 597 |
|
|
$path_new = join($path_delimiter, $paths); |
| 598 |
|
|
ini_set("include_path", $path_new); |
| 599 |
|
|
} |
| 600 |
|
|
|
| 601 |
|
|
// from: http://www.php.net/manual/en/function.is-subclass-of.php |
| 602 |
|
|
function is_subclass($child, $parent) { |
| 603 |
|
|
if (is_string($child)) { |
| 604 |
|
|
do { |
| 605 |
|
|
$child = get_parent_class($child); |
| 606 |
|
|
if (!$child) return false; |
| 607 |
|
|
//} while ($child == $parent); |
| 608 |
|
|
} while ($child != $parent); |
| 609 |
|
|
return true; |
| 610 |
|
|
} else { |
| 611 |
|
|
return is_subclass_of($child, $parent); |
| 612 |
|
|
} |
| 613 |
|
|
} |
| 614 |
|
|
|
| 615 |
|
|
|
| 616 |
|
|
function maintenance($type = 'system', $options = array()) { |
| 617 |
|
|
|
| 618 |
|
|
switch ($type) { |
| 619 |
|
|
|
| 620 |
|
|
case 'system': |
| 621 |
|
|
print "<br/><br/>"; |
| 622 |
|
|
print "<p align=\"center\">We have taken down our systems for maintenance purposes. <br/>Please apologize the downtime.</p>"; |
| 623 |
|
|
print "<p align=\"center\">Wir führen gerade Wartungsarbeiten an unseren Systemen durch. <br/>Bitte entschuldigen Sie die Unterbrechung des Betriebs.</p>"; |
| 624 |
|
|
|
| 625 |
|
|
print "<br/><br/>"; |
| 626 |
|
|
print "<p align=\"center\"><a href=\"mailto:info@top-scores.net\">Top-Scores Ltd.</a></p>"; |
| 627 |
|
|
$do_exit = 1; |
| 628 |
|
|
break; |
| 629 |
|
|
|
| 630 |
|
|
case 'database': |
| 631 |
|
|
print " |
| 632 |
|
|
Database error, we are already notified of this failure and working to resolve it.<br/> |
| 633 |
|
|
We apologize for the inconvenience, please come back later.<br/> |
| 634 |
|
|
<br/> |
| 635 |
|
|
Es ist ein Datenbankfehler aufgetreten. Wir haben bereits Kenntnis von diesem Fehler, an der Lösung des Problems wird gearbeitet.<br/> |
| 636 |
|
|
Bitte versuchen Sie es später nocheinmal.<br/> |
| 637 |
|
|
"; |
| 638 |
|
|
$do_exit = 1; |
| 639 |
|
|
break; |
| 640 |
|
|
|
| 641 |
|
|
case 'rpc': |
| 642 |
|
|
|
| 643 |
|
|
// V0 |
| 644 |
|
|
//return; |
| 645 |
|
|
|
| 646 |
|
|
// V2 |
| 647 |
|
|
// FIXME: implement some more behaviour here! |
| 648 |
|
|
// via constants? |
| 649 |
|
|
// e.g.: |
| 650 |
|
|
if (!constants::get('APP_MODE_DEBUG_LIGHT')) { return; } |
| 651 |
|
|
|
| 652 |
|
|
// V1 |
| 653 |
|
|
print "<font color=\"red\"><b>FIXME! - in 'php::maintenance(rpc)'</b></font><br/>"; |
| 654 |
|
|
print "RPC Problems!<br/>"; |
| 655 |
|
|
print "Status:<br/>"; |
| 656 |
|
|
print Dumper($options[status]); |
| 657 |
|
|
break; |
| 658 |
|
|
|
| 659 |
|
|
} |
| 660 |
|
|
|
| 661 |
|
|
if ($options[no_exit]) { $do_exit = 0; } |
| 662 |
|
|
if ($do_exit) { |
| 663 |
|
|
exit; |
| 664 |
|
|
} |
| 665 |
|
|
|
| 666 |
jonen |
1.2 |
} |
| 667 |
|
|
|
| 668 |
|
|
//examples on howto create unique id's |
| 669 |
|
|
// from: http://www.php.net/manual/en/function.uniqid.php |
| 670 |
|
|
function CreateGUID(){ |
| 671 |
|
|
srand((double)microtime()*1000000); |
| 672 |
|
|
$r = rand ; |
| 673 |
|
|
$u = uniqid(getmypid() . $r . (double)microtime()*1000000,1); |
| 674 |
|
|
$m = md5 ($u); |
| 675 |
|
|
return($m); |
| 676 |
|
|
} |
| 677 |
|
|
|
| 678 |
|
|
function CompressID( $ID ){ |
| 679 |
|
|
return(Base64_encode(pack("H*",$ID))); |
| 680 |
|
|
} |
| 681 |
|
|
|
| 682 |
|
|
function ExpandID ($ID){ |
| 683 |
|
|
return ( implode(unpack("H*",Base64_decode($ID)), '') ); |
| 684 |
joko |
1.1 |
} |
| 685 |
|
|
|
| 686 |
joko |
1.3 |
// from: utils/links.php |
| 687 |
|
|
function yesno($bool) { |
| 688 |
|
|
return $bool ? 'yes' : 'no'; |
| 689 |
joko |
1.4 |
} |
| 690 |
|
|
|
| 691 |
|
|
function untwingle_reference($item, $options = array()) { |
| 692 |
|
|
|
| 693 |
|
|
// default |
| 694 |
|
|
if (!$options['seperator']) { $options['seperator'] = '_'; } |
| 695 |
|
|
|
| 696 |
|
|
if ( is_string($item) && (substr($item, 0, 2) == "o_") ) { |
| 697 |
|
|
//print "YAI!<br/>"; |
| 698 |
|
|
$parts = split($options['seperator'], $item); |
| 699 |
|
|
$result = array( |
| 700 |
|
|
ident => $parts[1], |
| 701 |
|
|
type => $parts[2], |
| 702 |
|
|
); |
| 703 |
|
|
return $result; |
| 704 |
|
|
} |
| 705 |
joko |
1.5 |
} |
| 706 |
|
|
|
| 707 |
|
|
// wrapper-function for getting the logger instance |
| 708 |
|
|
// TODO: review! what about php::append_log? |
| 709 |
|
|
function log($string, $level = PEAR_LOG_DEBUG) { |
| 710 |
|
|
global $app; |
| 711 |
|
|
$app->log($string, $level); |
| 712 |
joko |
1.6 |
} |
| 713 |
|
|
|
| 714 |
|
|
// from: http://www.php.net/manual/en/function.array-filter.php#AEN7432 |
| 715 |
|
|
function odd($var) { |
| 716 |
|
|
return ($var % 2 == 1); |
| 717 |
|
|
} |
| 718 |
|
|
function even($var) { |
| 719 |
|
|
return ($var % 2 == 0); |
| 720 |
joko |
1.3 |
} |
| 721 |
joko |
1.1 |
|
| 722 |
|
|
} |
| 723 |
|
|
|
| 724 |
|
|
|
| 725 |
|
|
?> |