Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

basejumper.inc.php

Go to the documentation of this file.
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         //set our stuff.
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         //if we dont already have it, add it in.
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         //do we have the module loaded?
00114         if ($this->hasModule($module))
00115             //yup, go thru each of our modules.
00116             foreach ($this->modules AS $key => $val)
00117                 //if this is the right one, delete it.
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         //init
00168         $rval = array();
00169         
00170         //get our keys from the table.
00171         $rs = dbQuery("
00172             SHOW INDEXES FROM $table
00173         ");
00174         while ($ar = dbFetchAssoc($rs))
00175             $rval[] = $ar;
00176 
00177         //send her home
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         //if we already have it, do nothing
00217         if (self::$tables === null)
00218         {
00219             //try our cache.
00220             $rval = CacheBot::get("BaseJumper:Tables", self::$tableCacheLife);
00221             
00222             //no cache?  do it from db.
00223             if (!$rval)
00224             {
00225                 //get all the tables.
00226                 $tableRs = dbQuery("SHOW TABLES");
00227                 while ($tableAr = dbFetchArray($tableRs))
00228                     $rval[$tableAr[0]] = true;
00229                 
00230                 //cache it for 4 hours.
00231                 CacheBot::set("BaseJumper:Tables", $rval, self::$tableCacheLife);
00232             }
00233             
00234             //send it home
00235             self::$tables = $rval;
00236         }
00237     }
00238     
00243     private static function lookupDbTable($table)
00244     {
00245         //if we already have it, do nothing
00246         if (!isset(self::$tableData[$table]))
00247         {
00248             //try our cache.
00249             $rval = CacheBot::get("BaseJumper:Table:$table", self::$tableCacheLife);
00250             
00251             //did we get it?
00252             if (!$rval)
00253             {
00254                 //save the hassle (and exception) of looking up a nonexistant table.
00255                 if (self::dbHasTable($table))
00256                 {
00257                     //nope have the db tell us.
00258                     $descRs = dbQuery("DESCRIBE $table");
00259                     while ($descAr = dbFetchAssoc($descRs))
00260                     {
00261                         //get our field.
00262                         $field = $descAr['Field'];
00263                         
00264                         //save it.
00265                         $rval[$field] = $descAr;
00266                     }
00267                     
00268                     //cache it for 4 hours.
00269                     CacheBot::set("BaseJumper:Table:$table", $rval, self::$tableCacheLife);
00270                 }
00271             }
00272 
00273             //save it.
00274             self::$tableData[$table] = $rval;
00275         }
00276     }
00277     
00281     protected function checkCredentials()
00282     {
00283         //authenticate...
00284         global $me;
00285         $me = new User();
00286         $me->authenticate();
00287     }
00288 
00293     protected function checkHost()
00294     {
00295         //first check to see if we're on teh right server...
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         //we want output buffering so we can add variables in...
00307         ob_start();
00308 
00309         //attempt to have the object draw itself.
00310         try
00311         {
00312             //make sure we're on the right server.
00313             $this->checkHost();
00314 
00315             //log our page request
00316             if (self::$trackHits)
00317                 $this->trackPageHit();
00318             
00319             //try to get our page if its cached.
00320             $page = BaseModule::getCachedPage();
00321             if ($page)
00322                 echo $page;
00323             else
00324             {
00325                 //now do our checking.
00326                 $this->checkCredentials();
00327 
00328                 //do we need tables?
00329                 if (Config::get('db_host'))
00330                     self::getDbTables();
00331 
00332                 //initialize our module
00333                 $this->init();
00334                 
00335                 //turn output buffering on so we can show error forms and stuff cleanly.
00336                 ob_start();
00337                 $this->object->draw();
00338                 $contents = ob_get_contents();
00339                 ob_end_clean();
00340 
00341                 //show the contents.
00342                 echo $contents;
00343 
00344                 //save it if we got it.
00345                 if ($this->object->doCachePage)
00346                     BaseModule::setCachedPage($contents);
00347             }
00348         }
00349         //have our error handler clean up any juices.
00350         catch (Exception $e)
00351         {
00352             ErrorHandler::handleException($e);      
00353         }
00354         
00355         //get the output.
00356         $contents = ob_get_contents();
00357         ob_end_clean();
00358         
00359         //add in our stuff...
00360         echo $this->filterFinalOutput($contents);
00361     }
00362 
00372     protected function filterFinalOutput($output)
00373     {
00374         global $runtimeStart, $sqlQueryCount;
00375 
00376         //how long did it take?
00377         $runtimeStop = microtime(true);
00378         $total = round($runtimeStop - $runtimeStart, 4);
00379         
00380         //replace our shit.
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         //finally show our site.
00386         return $output;
00387     }
00388 
00395     protected function init()
00396     {
00397         //we want our default module.
00398         $this->setModule(new MyModule());
00399         
00400         //make the object init.
00401         try
00402         {
00403             //well first try and load our object.
00404             $object = $this->loadModule();
00405             
00406             //did we get an object?
00407             if ($object)
00408             {
00409                 $this->setModule($object);
00410                 
00411                 //get our parameters array..
00412                 $page = $this->getPage();
00413                 $params = $this->getParams();
00414                 
00415                 //did we get a global template passed in?
00416                 $template = $this->getGlobalTemplate($params);
00417                 if ($template instanceOf BaseTemplate)
00418                     $this->object->setTemplate($template);
00419                 
00420                 //check our auth code.
00421                 $this->checkAuthCode($params);
00422                 
00423                 //finially, init!
00424                 $this->object->init($page, $params);
00425                 
00426                 //we may need to reset the template if the module set its own template
00427                 if ($template instanceOf BaseTemplate && get_class($template) != get_class($this->object->template))
00428                     $this->object->setTemplate($template);
00429 
00430                 //let them know we were successful
00431                 return true;
00432             }
00433             //no object... return false
00434             else
00435                 return false;
00436         }
00437         //did we get any errors?
00438         catch(PageError $err)
00439         {
00440             $this->object->setError($err->getMessage());
00441         }
00442     }
00443 
00449     public function setModule(BaseModule $module)
00450     {
00451         //save the module
00452         $this->object = $module;
00453                 
00454         //give it a default template.
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         //start off with all our request vars.
00480         $vars = $_REQUEST;
00481         
00482         //do our url rewriting stuff
00483         if (Config::get("use_mod_rewrite"))
00484         {
00485             //get our params
00486             $params = $vars['params'];
00487             unset($vars['params']);
00488 
00489             //explode our parameters
00490             $paramA = explode('/', $params);
00491             if (count($paramA) && $params != '')
00492             {
00493                 //put them in the array
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         //send them home
00503         return $vars;
00504     }
00505     
00513     protected function getGlobalTemplate($params)
00514     {
00515         //did we get one passed in?
00516         if (isset($params['bjtemplate']))
00517         {
00518             //does it exist?
00519             $class = trim($params['bjtemplate']) . "Template";
00520             if (class_exists($class))
00521             {
00522                 //yup, unset it and send it home.
00523                 unset($params['bjtemplate']);
00524                 return new $class();
00525             }
00526         }
00527     }
00528 
00536     protected function checkAuthCode($params)
00537     {
00538         //did we get one passed in?
00539         if (isset($params['bjauthcode']))
00540         {
00541             //get it..
00542             $code = trim($params['bjauthcode']);
00543 
00544             //try our auth code!!
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         //did we get a module?
00562         if ($module === null)
00563         {
00564             //figure out what our class is.
00565             if (isset($_REQUEST['module']))
00566                 $module = trim($_REQUEST['module']);
00567             else
00568                 $module = $this->defaultClass;
00569         }
00570 
00571         //does it already exist?
00572         if ($this->hasModule($module))
00573         {
00574             //if we can find the class.. do it.
00575             if (class_exists($module))
00576                 return new $module();
00577             //otherwise, this is a programmer error, throw an exception for them.
00578             else
00579                 throw new Exception("Class $module not found.");
00580         }
00581         //nope, its not on our list... you cant get in.
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         //set up some default names...
00598         $lowerClass = strtolower($class);
00599         $dashClass = strtolower(preg_replace("/([a-z])([A-Z])/", "$1-$2", $class));
00600         
00601         //if it doesnt exist, can we open the file?
00602         if (file_exists("include/$lowerClass.inc.php"))
00603         {
00604             require_once("include/$lowerClass.inc.php");
00605             return true;
00606         }
00607         //if it doesnt exist, can we open the file?
00608         else if (file_exists("include/$dashClass.inc.php"))
00609         {
00610             require_once("include/$dashClass.inc.php");
00611             return true;
00612         }
00613         //if it doesnt exist, can we open the file?
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         //set some defaults.
00652         if ($module === null)
00653             $module = $_REQUEST['module'];
00654         if ($page === null)
00655             $page = $_REQUEST['page'];
00656 
00657         //safety first
00658         $module = dbEscape($module);
00659         $page = dbEscape($page);
00660 
00661         //track it.
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         //sort first!
00686         sort($this->modules);
00687         
00688         return $this->modules;
00689     }
00690 
00696     public function getModulesXml()
00697     {
00698         $xml = '<modules>';
00699 
00700         //get our modules xml data.
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 ?>

Generated on Fri Oct 27 12:26:39 2006 for BaseJumper by doxygen 1.3.9.1