Skip to content
This repository
tree: 49fc52717e
Fetching contributors…

Cannot retrieve contributors at this time

file 614 lines (546 sloc) 13.099 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
<?php
/**
* @package Lenses
* @copyright (c) Chris F. Ravenscroft
* @license See 'license.txt'
*/

global $_debug;

function __autoload($class_name)
{
global $inflector, $context;

// CamelCase => camel_case
$file_name = $inflector->inflectee($class_name)->toFile()->value();
ClassLoader::instance()->loadClass($file_name);
}

function import($path)
{
ClassLoader::instance()->import($path);
}

function redirect($url, $redirect_message)
{
global $page_parameters;

$page_parameters['action'] = 'redirect';
$page_parameters['redirect_url'] = Config::$path . $url . '?msg=' . urlencode($redirect_message);
}

function notifyView($msg)
{
$notify_msg = addslashes($msg);
// Copied from util.js...
print <<<EOB
<script type="text/javascript">
if(parent)
var myDoc = parent.document;
else
var myDoc = document;
var notifyfield = myDoc.getElementById('notifyfield');
if(!notifyfield)
{
var div = myDoc.createElement('div');
div.id = 'notifyfield';
myDoc.body.appendChild(div);
}

if(parent)
parent.document.getElementById('notifyfield').innerHTML = '{$notify_msg}';
else
document.getElementById('notifyfield').innerHTML = '{$notify_msg}';
</script>
EOB;
}

global $extraheaders;
function addheader($header)
{
global $extraheaders;
$extraheaders .= $header;
}

global $extrajs;
function addjs($js)
{
global $extrajs;
$extrajs .= $js;
}

function quick_log($txt)
{
$f = @fopen('/tmp/fw.log', 'a+');
if(!$f) return;
@fputs($f, $txt);
@fclose($f);
}

function include_here($path, $file)
{
include(dirname($path).'/'.$file);
}

class GoodException extends Exception {};

// ---------------------------
// Loggers

class FirePHPLogger
{
function __construct()
{
ob_start();
include('libs/FirePHPCore/fb.php');
}

public function info($arg)
{
fb($arg, FirePHP::INFO);
}

public function write($arg)
{
fb($arg);
}

public function error($arg)
{
fb($arg, FirePHP::ERROR);
}
}
class TmpLogger
{
private function _write($str)
{
$f = fopen('/tmp/fw.log', 'a+');
fwrite($f, $str);
fclose($f);
}

public function info($arg)
{
$this->_write("INFO: $arg\n");
}

public function write($arg)
{
$this->_write("$arg\n");
}

public function error($arg)
{
$this->_write("ERROR: $arg\n");
}
}
class EchoLogger
{
public function info($arg)
{
echo "INFO: $arg\n";
}

public function write($arg)
{
echo "$arg\n";
}

public function error($arg)
{
echo "ERROR: $arg\n";
}
}
class NullLogger
{
public function info($arg) {}
public function write($arg) {}
public function error($arg) {}
}
class Logger
{
static private $_logger;

private function __construct() {}

static function instance()
{
if(!self::$_logger)
{
switch(strtolower(Config::$logger))
{
case 'firephp':
self::$_logger = new FirePHPLogger();
break;
case 'tmp':
self::$_logger = new TmpLogger();
break;
case 'echo':
self::$_logger = new EchoLogger();
break;
default: // default - use in production
self::$_logger = new NullLogger();
}
}
return self::$_logger;
}

public function info($arg)
{
self::$_logger->info($arg);
}
}

// ---------------------------
// Translation
function T($token, $context='g')
{
global $_translations, $db;
if(!isset($_translations))
$_translations = array();
if(!isset($_translations[$context]))
{
        $qry = 'SELECT token, translation FROM dict';
$_translations[$context] = array();
        $rs = &$db->execute($qry);
        while(!$rs->EOF)
        {
$_translations[$context][$rs->fields['token']] = $rs->fields['translation'];
            $rs->moveNext();
        }
}
return (isset($_translations[$context][$token]) ? $_translations[$context][$token] : $token);
}

// ---------------------------

class ClassLoader
{
private $_classpath, $_imports, $_wildcardFiles;
static private $_classLoader;

private function __construct()
{
$this->_classpath = array(
'models',
'helpers',
);
$this->_imports = array();
$this->_wildcardFiles = array();

// Constants for classpath management
define('SEARCH_CP', true);
define('NO_SEARCH', false);

// Constants for displayMessage()
define('DISPLAY_BREAK', true);
define('DISPLAY_NO_BREAK', false);

// Session persistence constants
define('SESSION_PERSIST', true);
define('SESSION_NO_PERSIST',true);

// Active records constants
define('FIRST', 'f');
define('ALL', 'a');
}

static function instance()
{
if(!self::$_classLoader)
self::$_classLoader = new ClassLoader();
return self::$_classLoader;
}

public function addToClasspath($path)
{
// Take precedence
array_unshift($this->_classpath, $path);
}

public function import($path)
{
global $inflector;

$c = $this->_getClass($path);
if($c == '*')
{
$searchPath =
$inflector->
inflectee($this->_getPath($path))->
toPath()->value();
foreach($this->_classpath as $classpath)
{
$fullPath = $classpath . '/' . $searchPath;
if(file_exists($fullPath) && is_dir($fullPath))
{
foreach(glob($fullPath . '/*.php') as $filename)
{
$filename = str_replace('.php', '', $filename);
$p = strrpos($filename, '/');
$c_name = substr($filename, $p+1);
$this->_import($c_name, $filename, NO_SEARCH);
}
}
}
}
else
{
$this->_import($c, $inflector->inflectee($path)->toPath()->value(), SEARCH_CP);
}
}

function loadClass($fileName)
{
global $inflector, $context;

if(isset($this->_imports[$fileName]))
{
include_once($this->_imports[$fileName] . '.php');
return;
}
else
{
foreach($this->_classpath as $classpath)
{
$fullPath = $classpath . '/' . $fileName . '.php';
if(file_exists($fullPath))
{
include_once($fullPath);
return;
}
}
throw new Exception("Class not found error: $fileName");
}
}

private function _import($name, $path, $searchClasspath)
{
global $inflector;

if(SEARCH_CP == $searchClasspath)
{
$found = false;
foreach($this->_classpath as $classpath)
{
$fullPath = $classpath . '/' . $path . '.php';
if(file_exists($fullPath))
{
$found = true;
$path = str_replace('.php', '', $fullPath);
break;
}
}
if(!$found)
throw new Exception("Class not found error: $path");
}

$key = $inflector->inflectee($name)->toFile()->value();
$value = $inflector->inflectee($path)->toLower()->value();
if(isset($this->_imports[$key]) && $this->_imports[$key] != $value)
throw new Exception(
"Package conflict when importing from $value.php: conflicts with " .
$this->_imports[$key] . '.php');
$this->_imports[$key] = $value;
}

private function _getClass($path)
{
if(false !== ($p = strrpos($path, '.')))
return substr($path, $p+1);
else
return $path;
# throw new Exception("Sorry, no class found in $path");
}

private function _getPath($path)
{
if(false !== ($p = strrpos($path, '.')))
return substr($path, 0, $p);
return '';
}
}

// ---------------------------

// It's always the same old story: controller does
// its magic, asks model for time of the day,
// then we render our view.

// --------------------------- router

class Router
{
static function run()
{
// Does whoever came up with magic_quotes_gpc deserve to die? Discuss!
if(get_magic_quotes_gpc())
{
// Oh no!
$inputs = array(&$_GET, &$_POST, &$_COOKIE);
while(list($key, $value) = each($inputs))
{
foreach($value as $akey => $avalue)
{
if(is_array($avalue))
$inputs[] = &$inputs[$key][$akey];
else
$inputs[$key][$akey] = stripslashes($avalue);
}
}
unset($inputs);
}
//
include('libs/error_reporter.php');
register_shutdown_function('handleShutdown');
set_error_handler("displayErrorScreen");
set_exception_handler("displayExceptionScreen");
include('config.php');
Logger::instance()->info('Using FirePHP');
include('libs/ajax.php');
include('libs/inflector.php');
include('libs/validator.php');
include('libs/util.php');
include('libs/form_tags.php');

global $extraheaders, $extrajs;

if(!empty(Config::$dblayer))
{
include('libs/adodb/adodb.inc.php');
include('libs/adodb/adodb-active-record.inc.php');
include('libs/adodb/adodb-exceptions.inc.php');
include('libs/adodb/session/adodb-session2.php');
global $db;
$db = NewADOConnection(
Config::$dbengine.'://'.
Config::$dbuser.':'.
Config::$dbpassword.'@'.
Config::$dbhost.'/'.
Config::$dbname);
ADOdb_Active_Record::SetDatabaseAdapter($db);
ADOdb_Session::config(
Config::$dbengine,
Config::$dbhost,
Config::$dbuser,
Config::$dbpassword,
Config::$dbname,
array('table' => 'sessions'));
}
else
{
// TODO Time to panic here!
die("Please implement a better error handler.");
}

include('libs/base/base_object.php');
include('libs/base/application_model.php');
include('libs/base/application_controller.php');
include('libs/base/active_record.php');
include('libs/base/active_ws_record.php');

// Member structure
include('libs/models/member.php');

include('libs/session.php');
Session::start();

new Ajax();

// All done. Initialize configuration.
Config::initialize();

// Let's start routing. For real.
global $context, $controller, $action;
$controller = $action = null;
// Are we using prettified URLs level 2?
if(isset($_REQUEST['rewrite']) && $_REQUEST['rewrite']=='true')
{
$raw_url = preg_replace('#^' . Config::$path . '#', '', $_SERVER['REQUEST_URI']);
// Restore good old GET variables
if(false !== ($p = strpos($raw_url, '?')))
{
$gets = explode('&', substr($raw_url, $p + 1));
foreach($gets as $get)
{
list($k, $v) = explode('=', $get);
$_GET[$k] = $_REQUEST[$k] = urldecode($v);
}
}
}
else
$raw_url = str_replace(Config::$path . 'index.php/', '', $_SERVER['REQUEST_URI']);
$contextPrefix = '';
$args = explode('/', $raw_url);
if(count($args)>0)
{
if(!empty($args[0]))
{
if(isset(Config::$altroutes[$args[0]]))
{
$context = Config::$altroutes[$args[0]];
}
else
{
$context = $args[0];
$contextPrefix = 'app/' . $context . '/';
ClassLoader::instance()->addToClasspath('app/' . $context . '/models');
ClassLoader::instance()->addToClasspath('app/' . $context . '/helpers');
}
}
if(!empty($context))
array_shift($args);

if(count($args)>0)
{
$controller = $args[0];
array_shift($args);
if(count($args)>0)
{
$action = $args[0];
array_shift($args);
}
else
{
$action = 'index';
}
}
}
if(empty($controller))
{
$controller = 'main';
$action = 'index';
}
global $message_type, $message_css, $message_text, $page_parameters;
$message_type = null;
if(false !== ($p = strpos($action, '?')))
{
$action = substr($action, 0, $p);
}
// The code above is far from perfect...nothing beats a good old GET variable
if(isset($_GET['msg']))
{
displayMessage(MESSAGE_INFO, $_GET['msg'], DISPLAY_NO_BREAK);
}

$controller_parts = explode('_', $controller);
$controller_class = '';
foreach($controller_parts as $part)
{
$controller_class .= ucwords($part);
}

$controller_class .= 'Controller';
include($contextPrefix . 'controllers/' . $controller . '_controller.php');
$c_class = new $controller_class($controller, $action, $args);

if($action)
{
try
{
$c_class->$action();
}
catch(GoodException $ex)
{
// That's OK...just a quick way to exit an action callback
// For instance, when invoking displayMessage()
}

// Give local scope to controller's vars
$members = get_object_vars($c_class);
foreach($members as $key => $value)
{
$$key = $value;
}

global $_debug;
if(!empty($_debug))
$debug = '<br />' . $_debug;
else
$debug = '';

// Now, we have everything ready...let's grab a view, if any.
if(empty($page_parameters['action']))
{
$v = $contextPrefix .
'views/' .
(empty($options['controller']) ? $controller : $options['controller']) .
'/' .
(empty($options['view']) ? 'index' : $options['view']) .
'.html.php';
if(!(file_exists($v)))
self::panic('Sorry, view [' . $v . '] does not exist.');

if($options['header'])
include("views/{$options['header']}.html.php");
else
include('views/header.html.php');
if($options['body'])
include("views/{$options['body']}.html.php");
else
include($v);
if($options['footer'])
include("views/{$options['footer']}.html.php");
else
include('views/footer.html.php');
}
else
{
switch($page_parameters['action'])
{
case 'redirect':
include('views/redirect.html.php');
break;
case 'notify':
notifyView($page_parameters['arg']->getMessage());
break;
case 'api':
break;
default:
}
}
}
}

static function panic($ack)
{
die($ack);
}
}

define(ROOT, dirname(__FILE__));

Router::run();
?>
Something went wrong with that request. Please try again.