00001 <?
00014 class BaseJumper
00015 {
00019 public $defaultClass;
00020
00024 public $defaultTemplate;
00025
00029 public $object;
00030
00034 public static $tables = null;
00035
00039 public static $tableData = array();
00040
00044 public static $tableCacheLife = 3000000;
00045
00049 public static $trackHits = true;
00050
00054 public static $hitTrackingTable = 'site_hits';
00055
00059 private $modules = array();
00060
00067 public function __construct($defaultTemplate = 'DefaultTemplate', $defaultClass = 'Main')
00068 {
00069
00070 $this->defaultClass = $defaultClass;
00071 $this->defaultTemplate = $defaultTemplate;
00072
00073 if (!Config::get('basejumper_installed'))
00074 {
00075 self::$trackHits = false;
00076 }
00077 }
00078
00084 public function addModule($module)
00085 {
00086 $module = strtolower($module);
00087
00088
00089 if (!$this->hasModule($module))
00090 $this->modules[] = $module;
00091 }
00092
00098 public function addModules($modules)
00099 {
00100 foreach ($modules AS $module)
00101 $this->addModule($module);
00102 }
00103
00109 public function removeModule($module)
00110 {
00111 $module = strtolower($module);
00112
00113
00114 if ($this->hasModule($module))
00115
00116 foreach ($this->modules AS $key => $val)
00117
00118 if ($val == $module)
00119 unset($this->modules[$key]);
00120 }
00121
00127 public function hasModule($module)
00128 {
00129 $module = strtolower($module);
00130
00131 return in_array($module, $this->modules);
00132 }
00133
00137 public static function getDbTables()
00138 {
00139 self::lookupDbTables();
00140
00141 return self::$tables;
00142 }
00143
00151 public static function getDbTable($table)
00152 {
00153 self::lookupDbTable($table);
00154
00155 return self::$tableData[$table];
00156 }
00157
00165 public static function getTableIndexes($table)
00166 {
00167
00168 $rval = array();
00169
00170
00171 $rs = dbQuery("
00172 SHOW INDEXES FROM $table
00173 ");
00174 while ($ar = dbFetchAssoc($rs))
00175 $rval[] = $ar;
00176
00177
00178 return $rval;
00179 }
00180
00188 public static function dbHasTable($table)
00189 {
00190 self::lookupDbTables();
00191
00192 return isset(self::$tables[$table]);
00193 }
00194
00203 public static function tableHasField($table, $field)
00204 {
00205 self::lookupDbTable($table);
00206
00207 return isset(self::$tableData[$table][$field]);
00208 }
00209
00214 private static function lookupDbTables()
00215 {
00216
00217 if (self::$tables === null)
00218 {
00219
00220 $rval = CacheBot::get("BaseJumper:Tables", self::$tableCacheLife);
00221
00222
00223 if (!$rval)
00224 {
00225
00226 $tableRs = dbQuery("SHOW TABLES");
00227 while ($tableAr = dbFetchArray($tableRs))
00228 $rval[$tableAr[0]] = true;
00229
00230
00231 CacheBot::set("BaseJumper:Tables", $rval, self::$tableCacheLife);
00232 }
00233
00234
00235 self::$tables = $rval;
00236 }
00237 }
00238
00243 private static function lookupDbTable($table)
00244 {
00245
00246 if (!isset(self::$tableData[$table]))
00247 {
00248
00249 $rval = CacheBot::get("BaseJumper:Table:$table", self::$tableCacheLife);
00250
00251
00252 if (!$rval)
00253 {
00254
00255 if (self::dbHasTable($table))
00256 {
00257
00258 $descRs = dbQuery("DESCRIBE $table");
00259 while ($descAr = dbFetchAssoc($descRs))
00260 {
00261
00262 $field = $descAr['Field'];
00263
00264
00265 $rval[$field] = $descAr;
00266 }
00267
00268
00269 CacheBot::set("BaseJumper:Table:$table", $rval, self::$tableCacheLife);
00270 }
00271 }
00272
00273
00274 self::$tableData[$table] = $rval;
00275 }
00276 }
00277
00281 protected function checkCredentials()
00282 {
00283
00284 global $me;
00285 $me = new User();
00286 $me->authenticate();
00287 }
00288
00293 protected function checkHost()
00294 {
00295
00296 if ($_SERVER["HTTP_HOST"] != Config::get('site_hostname'))
00297 Util::redirect("http://" . Config::get('site_hostname') . $_SERVER["REQUEST_URI"]);
00298 }
00299
00304 public function start()
00305 {
00306
00307 ob_start();
00308
00309
00310 try
00311 {
00312
00313 $this->checkHost();
00314
00315
00316 if (self::$trackHits)
00317 $this->trackPageHit();
00318
00319
00320 $page = BaseModule::getCachedPage();
00321 if ($page)
00322 echo $page;
00323 else
00324 {
00325
00326 $this->checkCredentials();
00327
00328
00329 if (Config::get('db_host'))
00330 self::getDbTables();
00331
00332
00333 $this->init();
00334
00335
00336 ob_start();
00337 $this->object->draw();
00338 $contents = ob_get_contents();
00339 ob_end_clean();
00340
00341
00342 echo $contents;
00343
00344
00345 if ($this->object->doCachePage)
00346 BaseModule::setCachedPage($contents);
00347 }
00348 }
00349
00350 catch (Exception $e)
00351 {
00352 ErrorHandler::handleException($e);
00353 }
00354
00355
00356 $contents = ob_get_contents();
00357 ob_end_clean();
00358
00359
00360 echo $this->filterFinalOutput($contents);
00361 }
00362
00372 protected function filterFinalOutput($output)
00373 {
00374 global $runtimeStart, $sqlQueryCount;
00375
00376
00377 $runtimeStop = microtime(true);
00378 $total = round($runtimeStop - $runtimeStart, 4);
00379
00380
00381 $output = str_replace("%%BJ_RUNTIME%%", $total, $output);
00382 $output = str_replace("%%BJ_QUERIES%%", (int)$sqlQueryCount, $output);
00383 $output = str_replace("%%BJ_CACHE_HITS%%", (int)EasyCache::$hits, $output);
00384
00385
00386 return $output;
00387 }
00388
00395 protected function init()
00396 {
00397
00398 $this->setModule(new MyModule());
00399
00400
00401 try
00402 {
00403
00404 $object = $this->loadModule();
00405
00406
00407 if ($object)
00408 {
00409 $this->setModule($object);
00410
00411
00412 $page = $this->getPage();
00413 $params = $this->getParams();
00414
00415
00416 $template = $this->getGlobalTemplate($params);
00417 if ($template instanceOf BaseTemplate)
00418 $this->object->setTemplate($template);
00419
00420
00421 $this->checkAuthCode($params);
00422
00423
00424 $this->object->init($page, $params);
00425
00426
00427 if ($template instanceOf BaseTemplate && get_class($template) != get_class($this->object->template))
00428 $this->object->setTemplate($template);
00429
00430
00431 return true;
00432 }
00433
00434 else
00435 return false;
00436 }
00437
00438 catch(PageError $err)
00439 {
00440 $this->object->setError($err->getMessage());
00441 }
00442 }
00443
00449 public function setModule(BaseModule $module)
00450 {
00451
00452 $this->object = $module;
00453
00454
00455 $this->object->setTemplate(new $this->defaultTemplate());
00456 }
00457
00463 protected function getPage()
00464 {
00465 if (isset($_REQUEST['page']))
00466 return $_REQUEST['page'];
00467 else
00468 return 'main';
00469 }
00470
00477 protected function getParams()
00478 {
00479
00480 $vars = $_REQUEST;
00481
00482
00483 if (Config::get("use_mod_rewrite"))
00484 {
00485
00486 $params = $vars['params'];
00487 unset($vars['params']);
00488
00489
00490 $paramA = explode('/', $params);
00491 if (count($paramA) && $params != '')
00492 {
00493
00494 if (count($paramA) % 2 == 0)
00495 for ($i = 0; $i < count($paramA); $i += 2)
00496 $vars[$paramA[$i]] = $paramA[$i+1];
00497 else
00498 throw new PageError('Wrong parameter count.');
00499 }
00500 }
00501
00502
00503 return $vars;
00504 }
00505
00513 protected function getGlobalTemplate($params)
00514 {
00515
00516 if (isset($params['bjtemplate']))
00517 {
00518
00519 $class = trim($params['bjtemplate']) . "Template";
00520 if (class_exists($class))
00521 {
00522
00523 unset($params['bjtemplate']);
00524 return new $class();
00525 }
00526 }
00527 }
00528
00536 protected function checkAuthCode($params)
00537 {
00538
00539 if (isset($params['bjauthcode']))
00540 {
00541
00542 $code = trim($params['bjauthcode']);
00543
00544
00545 $user = new User();
00546 if (!$user->tryAuthCode($code, false))
00547 throw new PageError('Authorization code failed.');
00548 }
00549 }
00550
00559 protected function loadModule($module = null)
00560 {
00561
00562 if ($module === null)
00563 {
00564
00565 if (isset($_REQUEST['module']))
00566 $module = trim($_REQUEST['module']);
00567 else
00568 $module = $this->defaultClass;
00569 }
00570
00571
00572 if ($this->hasModule($module))
00573 {
00574
00575 if (class_exists($module))
00576 return new $module();
00577
00578 else
00579 throw new Exception("Class $module not found.");
00580 }
00581
00582 else
00583 throw new PageError("The module '$module' does not exist in " . Config::get('site_name') . ".");
00584
00585 return false;
00586 }
00587
00595 public static function autoload($class)
00596 {
00597
00598 $lowerClass = strtolower($class);
00599 $dashClass = strtolower(preg_replace("/([a-z])([A-Z])/", "$1-$2", $class));
00600
00601
00602 if (file_exists("include/$lowerClass.inc.php"))
00603 {
00604 require_once("include/$lowerClass.inc.php");
00605 return true;
00606 }
00607
00608 else if (file_exists("include/$dashClass.inc.php"))
00609 {
00610 require_once("include/$dashClass.inc.php");
00611 return true;
00612 }
00613
00614 else if (file_exists("include/{$class}.inc.php"))
00615 {
00616 require_once("include/{$class}.inc.php");
00617 return true;
00618 }
00619
00620 return false;
00621 }
00622
00630 public function getCreateHitTrackingTable()
00631 {
00632 return
00633 "CREATE TABLE " . self::$hitTrackingTable . "
00634 (
00635 id int(11) not null auto_increment,
00636 module varchar(64) default '' not null,
00637 page varchar(64) default '' not null,
00638 access_date datetime not null,
00639 PRIMARY KEY(id)
00640 ) TYPE=MyISAM;";
00641 }
00642
00649 public function trackPageHit($module = null, $page = null)
00650 {
00651
00652 if ($module === null)
00653 $module = $_REQUEST['module'];
00654 if ($page === null)
00655 $page = $_REQUEST['page'];
00656
00657
00658 $module = dbEscape($module);
00659 $page = dbEscape($page);
00660
00661
00662 dbExecute("
00663 INSERT INTO " . self::$hitTrackingTable . "
00664 (module, page, access_date)
00665 VALUES
00666 ('$module', '$page', NOW())
00667 ");
00668 }
00669
00673 public function clearHits()
00674 {
00675 dbExecute("DELETE FROM " . self::$hitTrackingTable);
00676 }
00677
00683 public function getModulesArray()
00684 {
00685
00686 sort($this->modules);
00687
00688 return $this->modules;
00689 }
00690
00696 public function getModulesXml()
00697 {
00698 $xml = '<modules>';
00699
00700
00701 $modules = $this->getModulesArray();
00702 foreach ($modules AS $module)
00703 {
00704 $obj = new $module;
00705 $xml .= $obj->getModuleXml();
00706 }
00707
00708 $xml .= '</modules>';
00709
00710 return $xml;
00711 }
00712 }
00713
00781 ?>