]> andersk Git - sql-web.git/blob - lib/joe/site.lib.php
git-svn-id: svn://presbrey.mit.edu/php/lib@126 a142d4bd-2cfb-0310-9673-cb33a7e74f58
[sql-web.git] / lib / joe / site.lib.php
1 <?php
2 /* Generic Site Classes
3  * 2007/04/09 Joe Presbrey <presbrey@mit.edu>
4  */
5
6 class Site {
7         var $URI, $ARGV;
8         function __construct($uri) {
9                 $this->URI = $uri;
10                 $this->ARGV = array_merge(
11                         array_prepend_keys($_COOKIE,'c_'),
12                         array_prepend_keys($_FILES,'f_'),
13                         array_prepend_keys($_GET,'g_'),
14                         array_prepend_keys($_POST,'p_'));
15         }
16         function Start() {
17                 if (isset($_SESSION)) {
18                         $this->ARGV = array_merge(
19                                 $this->ARGV,
20                                 array_prepend_keys($_SESSION,'s_'));
21                 }
22         }
23         function Run($MODULE, $METHOD) {
24                 $nCalls = 0;
25                 if (file_exists(strtolower('site/'.$MODULE.'.php'))) {
26                         require_once strtolower('site/'.$MODULE.'.php');
27                         if (class_exists($MODULE)) {
28                                 $page = new $MODULE($this, $METHOD);
29                                 $page->Start();
30                                 $handlers = $page->get_handlers($METHOD);
31                                 foreach($this->ARGV as $argk=>$argv) {
32                                         if (in_array($argk, $handlers) &&
33                                                 false !== $page->Run($argk)) {
34                                                 $nCalls++;
35                                         }
36                                 }
37                                 if ($nCalls == 0 && is_callable(array($page, '_default'))) {
38                                         $page->Run('_default');
39                                         $nCalls++;
40                                 }
41                                 $page->Finish();
42                         }
43                 }
44                 return $nCalls;
45         }
46         function Finish() {}
47 }
48
49 class Page {
50         var $SITE, $METHOD, $URI;
51         function __construct($site, $method) {
52                 $this->SITE = $site;
53                 $this->METHOD = $method;
54                 foreach($site->ARGV as $k=>$v)
55                         $this->$k = $v;
56                 $this->URI = $site->URI;
57         }
58         function get_handlers() {
59                 $handlers = isset($this->handlers[$this->METHOD]) ?
60                                         $this->handlers[$this->METHOD] : array();
61                 return $handlers;
62         }
63         function has_handler($handler) {
64                 return isset($this->handlers[$this->METHOD]) ?
65                                 in_array($handler, $this->handlers[$this->METHOD]) : false;
66         }
67
68         function Start() {}
69         function Run($handler) {
70                 if (is_callable(array($this, $handler))) {
71                         return array(0, call_user_func(array($this, $handler)));
72                 } else {
73                         return false;
74                 }
75         }
76         function Finish() {}
77 }
This page took 0.052199 seconds and 5 git commands to generate.