00001 <?php
00012 class ExceptionRedirect extends Exception {
00016 public function __construct($msg=null) {
00017 parent::__construct($msg);
00018 }
00019 };
00020
00024 class PageServer {
00025 private $src_;
00026 private $dest_;
00027 private $xslTemplate_;
00032 public static function isPageValid($page) {
00033 $page = ereg_replace('\/$', '', $page);
00034 if (false === ereg('^[A-Za-z_][A-Za-z0-9_\.\-]*$', $page))
00035 return false;
00036 $config = Config::instance();
00037 return file_exists(
00038 $config->documentRoot().
00039 $config->pageDir().'/'.
00040 $page.'.xml'
00041 );
00042 }
00047 public function __construct($page, $nestedPage) {
00048 if (!self::isPageValid($page))
00049 throw new ExceptionNotFound;
00050
00051
00052 $config = Config::instance();
00053 $docRoot = $config->documentRoot();
00054 $pageDir_ = $docRoot.$config->pageDir();
00055 $cacheDir_ = $docRoot.$config->cacheDir();
00056 $xslBase = $pageDir_.'/'.__CLASS__;
00057 $this->page_ = $page;
00058 $this->src_ = $pageDir_.'/'.$page.'.xml';
00059 $this->pageContentTemplate_ = $xslBase.'.xsl';
00060 $this->titleInitTemplate_ = $xslBase.'_init.xsl';
00061 $this->pageContentScript_ = $cacheDir_.'/'.$page.'.php';
00062 $this->titleInitScript_ = $cacheDir_.'/'.$page.'_init.php';
00063
00064 if (!file_exists($this->src_))
00065
00066 throw new ExceptionNotFound;
00067
00068 if (!empty($nestedPage)) {
00069 $xml = new DOMDocument;
00070 $xml->load($this->src_);
00071 $xpath = new DOMXpath($xml);
00072 $list = $xpath->query('
00073 if (1!=$list->length)
00074 throw new ExceptionRedirect;
00075 }
00076
00077
00078 if ($this->needUpdate())
00079 $this->updateScript();
00080 }
00085 public function page() {
00086 return $this->page_;
00087 }
00092 public function titleInitScript() {
00093 return $this->titleInitScript_;
00094 }
00099 public function pageContentScript() {
00100 return $this->pageContentScript_;
00101 }
00105 private function needUpdate() {
00106 if (
00107 !file_exists($this->pageContentScript_) ||
00108 !file_exists($this->titleInitScript_))
00109 return true;
00110 else
00111 return
00112 min(filemtime($this->pageContentScript_),filemtime($this->titleInitScript_)) <
00113 max(filemtime($this->src_),filemtime($this->pageContentTemplate_),filemtime($this->titleInitTemplate_));
00114 }
00118 private function updateScript() {
00119
00120 $xml = new DOMDocument;
00121 $xml->load($this->src_);
00122 $xsl = new DOMDocument;
00123 $xsl->load($this->pageContentTemplate_);
00124
00125
00126 $proc = new XSLTProcessor;
00127 $proc->importStyleSheet($xsl);
00128
00129
00130 $fd = fopen($this->pageContentScript_, 'w');
00131 fwrite($fd, $proc->transformToXML($xml));
00132 fclose($fd);
00133
00134 $xsl = new DOMDocument;
00135 $xsl->load($this->titleInitTemplate_);
00136
00137
00138 $proc = new XSLTProcessor;
00139 $proc->importStyleSheet($xsl);
00140
00141
00142 $fd = fopen($this->titleInitScript_, 'w');
00143 fwrite($fd, $proc->transformToXML($xml));
00144 fclose($fd);
00145 }
00146 };
00147 ?>