65ad7ff4c7c77fc7008aae2a50449c76857af4bb
3 * Copyright (C) 2003-2005 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 require_once 'diogenes.page.inc.php';
23 require_once 'Barrel.php';
24 require_once 'Barrel/Menu.php';
26 /** This class is used to display a page of a Diogenes barrel,
27 * that is an RCS-managed website with a virtual directory
30 class DiogenesBarrel
extends DiogenesPage
32 // barrel definition info
34 /** The database table holding the menus */
37 /** The barrel's alias. */
40 /** The Diogenes_Barrel representing the barrel */
44 /** A Diogenes_Barrel_Page representing the current page */
47 /** Information about the current location */
50 /** Can the current user edit this page? */
54 /** Constructs a Smarty-derived object to display contents within a barrel.
56 * @param $override_pathinfo
58 function DiogenesBarrel($override_pathinfo = null
)
62 // call parent constructor
63 $this->DiogenesPage();
65 // break down PATH_INFO into site and location components
66 $mypathinfo = $override_pathinfo ?
$override_pathinfo : $_SERVER['PATH_INFO'];
67 $this->pathinfo
= $this->parsePathInfo($mypathinfo);
69 $this->kill404("Invalid location specified!");
71 // Retrieve site-wide info from database
72 $this->barrel
= new Diogenes_Barrel($this->pathinfo
['alias']);
73 if (!$this->barrel
->alias
)
74 $this->kill404("Unknown barrel requested : {$this->pathinfo['alias']}");
77 $this->alias
= $this->barrel
->alias
;
78 $this->table_menu
= $this->barrel
->table_menu
;
83 // Check the requested page exists
84 $tdir = $this->pathinfo
['dir'];
87 if (!$this->pathinfo
['PID'] = $this->barrel
->getPID($tdir))
89 $this->kill404("Unknown location specified '$tdir'!");
94 /** Check the user has the right permissions.
95 * Read the user's permissions for the current site from database.
97 * @param level the required permissions level
99 function checkPerms($level) {
102 if ($level != "public")
103 $_SESSION['session']->doAuth($this);
105 $_SESSION['session']->setBarrelPerms($this->alias
);
107 if (!$_SESSION['session']->hasPerms($level))
108 $this->kill(__("You are not authorized to view this page!"), 403);
112 /** Read the contents for the current location.
118 // Retrieve information specific to the current page
120 // enable directory index
121 $file = $this->pathinfo
['file'] ?
$this->pathinfo
['file'] : $globals->htmlfile
;
124 if (!$bpage = Diogenes_Barrel_Page
::fromDb($this->barrel
, $this->pathinfo
['PID']))
126 $this->kill404("Directory not found : '{$this->pathinfo['dir']}' ({$this->pathinfo['PID']}) !");
128 $this->curpage
=& $bpage;
130 // check the permissions for the current location
131 if (!$this->pathinfo
['file'] ||
$bpage->props
['perms'] != 'public' ||
isset($_REQUEST['rev'])) {
132 $this->startSession();
134 // handle login/logout requests
135 if (isset($_REQUEST['dologout']))
137 if (isset($_REQUEST['doauth']))
138 $this->checkPerms('auth');
140 $this->checkPerms($bpage->props
['perms']);
142 // can we edit this page?
143 $this->canedit
= $_SESSION['session']->hasPerms($bpage->props
['wperms']);
146 // now we can display the page
147 // check the location is valid
148 if (!$this->barrel
->spool
->checkPath($bpage->props
['PID'],$file,false
))
149 $this->kill404("Malformed location!");
151 // check that the page is 'live'
152 switch ($bpage->props
['status']) {
156 $this->assign('page_content', "<p>".__("This page is currently under construction.")."<p>");
160 $this->assign('page_content', "<p>".__("This page is currently unavailable.")."<p>");
165 // if necessary, do a checkout
166 if (isset($_REQUEST['rev'])) {
167 $rcs = $this->getRcs();
168 $path = $rcs->checkout($bpage->props
['PID'],$file,$_REQUEST['rev'],System
::mktemp("-d"));
170 $path = $this->barrel
->spool
->spoolPath($bpage->props
['PID'],$file);
174 $this->kill404("File not found : $path!");
176 if (!$this->pathinfo
['file']) {
177 // this is a page, display it within header/footer framework
178 $this->doPage($path, $bpage);
180 // otherwise, we send back the raw file
181 $type = get_mime_type($path);
182 if (is_mime_multipart($type)) {
183 $boundary = get_mime_boundary($path);
184 if ($boundary) $type = "$type; boundary=\"$boundary\"";
186 header("Content-Type:$type");
187 header("Content-Length:".filesize($path));
188 header("Last-modified:".gmdate("D, d M Y H:i:s T", filemtime($path)));
195 /** Display a page within the header/footer framework.
197 * @param path the path of the file
198 * @param bpage a Diogenes_Barrel_Page representing the current page
200 function doPage($path, $bpage)
204 $this->assign('page',stripslashes($bpage->props
['title']));
207 $this->barrel
->readPlugins();
208 $active_plugins = $this->barrel
->loadPlugins($bpage);
210 // search for rendering pluging
212 foreach ($active_plugins as $plugname => $plugobj) {
213 if (is_object($plugobj) && ($plugobj->type
== "render")) {
214 $render_plugin = $plugobj;
217 // source page or pass it to a rendering plugin
218 if (is_object($render_plugin)) {
219 $content = $render_plugin->render($path);
221 $content = file_get_contents($path);
224 // apply plugin filtering
225 foreach ($active_plugins as $plugname => $plugobj) {
226 if (is_object($plugobj) && ($plugobj->type
== "filter")) {
227 $content = $plugobj->filter($content);
230 $this->assign('page_content', $content);
232 parent
::display('', $this->getTemplate($bpage->props
['template']));
236 /** Return an RCS handle. */
240 return new $globals->rcs($this,$this->alias
,$_SESSION['session']->username
);
244 /** Returns the master template for the current context.
248 function getTemplate($template = '')
252 // we have a page-specific template, get its full path
253 $tpl = $this->templatePath($template);
254 } else if ($this->barrel
->options
->template
) {
255 // we have default site template, get is full path
256 $tpl = $this->templatePath($this->barrel
->options
->template
);
258 // fall back on the system-wide default template
259 $tpl = parent
::getTemplate();
265 /** Returns the available master templates. */
266 function getTemplates()
268 // the system-wide templates
269 $templates = parent
::getTemplates();
270 $bbarrel =& $this->barrel
;
272 // lookup templates in the template directory
273 if ($bbarrel->hasFlag('tpl') && $bbarrel->options
->template_dir
) {
274 $dir = $bbarrel->spool
->spoolPath($bbarrel->options
->template_dir
);
275 $files = System
::find($dir.' -maxdepth 1 -name *.tpl');
276 foreach ($files as $file)
277 $templates["barrel:".basename($file)] = "[barrel] ".basename($file);
283 /** Is the user an administrator for the current barrel ? */
285 return isset($_SESSION['session']) && $_SESSION['session']->hasPerms('admin');
289 /** Build the page's "head" tag.
291 function makeHead() {
293 $bbarrel =& $this->barrel
;
296 $this->assign('site', stripslashes($bbarrel->options
->title
));
299 array_push($this->head
, '<meta name="description" content="'.stripslashes($bbarrel->options
->description
).'" />');
300 array_push($this->head
, '<meta name="keywords" content="'.stripslashes($bbarrel->options
->keywords
).'" />');
303 $this->sheets
= array();
304 array_push($this->sheets
, $this->url("common.css"));
305 if ($bbarrel->options
->menu_style
== 1 ||
$bbarrel->options
->menu_style
== 2)
306 array_push($this->sheets
, $this->url("phplayersmenu/{$bbarrel->options->menu_theme}/style.css"));
307 array_push($this->sheets
, $this->urlSite("", $globals->cssfile
));
309 // add stylesheets to head
310 foreach ($this->sheets
as $mysheet) {
311 array_push($this->head
, '<link rel="stylesheet" href="'.$mysheet.'" type="text/css" />');
314 if ($bbarrel->options
->favicon
)
315 array_push($this->head
, '<link rel="icon" href="'.$this->urlSite("", $bbarrel->options
->favicon
).'" type="image/png" />');
318 array_push($this->head
, '<link rel="alternate" type="application/rss+xml" title="'.stripslashes($bbarrel->options
->title
).'" href="'.$this->urlSite("admin", "rss").'" />');
322 /** Build the barrel's menu.
324 function makeMenu() {
326 $bbarrel =& $this->barrel
;
328 // menu style & theme
329 $this->assign('menustyle', $bbarrel->options
->menu_style
);
330 $this->assign('menutheme', $bbarrel->options
->menu_theme
);
332 $PID = $this->curpage
->props
['PID'];
334 // build the Diogenes part of the menu
335 if (!$bbarrel->options
->menu_hide_diogenes
) {
336 array_push($this->menu
,array(0,__("Home"),$this->urlSite(""), 1));
337 if ($this->isLogged()) {
338 array_push($this->menu
, array(1,__("Logout"), "?dologout=1") );
339 array_push($this->menu
, array(1,__("Preferences"), $this->urlSite("admin", "prefs")));
341 array_push($this->menu
, array(1,__("Login"), "?doauth=1") );
344 if ($this->isAdmin()) {
345 array_push($this->menu
, array(1, __("Administration"), $this->urlSite("admin")));
347 array_push($this->menu
, array(1, __("Page properties"), $this->urlSite("admin", "pages?dir=$PID")));
348 } elseif ($this->canedit
&& $PID) {
349 array_push($this->menu
, array(0, __("Edit this page"), "", 1));
350 array_push($this->menu
, array(1, __("Raw editor"), $this->urlSite("admin", "edit?dir=$PID&file={$globals->htmlfile}")));
351 array_push($this->menu
, array(1, __("HTML editor"), $this->urlSite("admin" , "compose?dir=$PID&file={$globals->htmlfile}")));
355 // if this is an error page, we need to bail out here
356 if (!isset($this->table_menu
))
359 // add the user-defined part of the menu
360 $bmenu = new Diogenes_Barrel_Menu($this->dbh
, $this->table_menu
);
361 $this->menu
= array_merge($this->menu
, $bmenu->makeMenu($PID, $this->barrel
->options
->menu_min_level
, array($this, 'urlSiteByPid')));
365 /** Read this barrel's menu entries from database.
370 $menu[0]['children'] = array();
371 $res = $this->dbh
->query("select MID,MIDpere,title,link,PID from {$this->table_menu} order by ordre");
372 while (list($mid, $parent, $title, $link, $pid) = mysql_fetch_row($res))
374 $menu[$mid]['parent'] = $parent;
375 $menu[$mid]['title'] = $title;
376 $menu[$mid]['link'] = $link;
377 $menu[$mid]['title'] = $title;
378 $menu[$mid]['pid'] = $pid;
379 if (!is_array($menu[$mid]['children']))
380 $menu[$mid]['children'] = array();
382 // register this entry with its parent
383 if (!is_array($menu[$parent]['children']))
384 $menu[$parent]['children'] = array();
385 array_push($menu[$parent]['children'], $mid);
387 mysql_free_result($res);
393 * Break down a PATH_INFO into site, page id and file
394 * Directories *must* be accessed with a final slash.
396 * @param path the path to parse
398 function parsePathInfo($path) {
399 if (empty($path) ||
!preg_match("/^\/([^\/]+)\/((.+)\/)?([^\/]*)$/",$path,$asplit))
402 $split['alias'] = $asplit[1];
403 $split['dir'] = isset($asplit[3]) ?
$asplit[3] : "";
404 $split['file'] = isset($asplit[4]) ?
$asplit[4] : "";
409 /** Return the current URI.
411 function script_uri()
413 if ($this->barrel
->vhost
)
414 return preg_replace("/^(.*)\/site(\.php)?\/{$this->alias}\/(.*)/", "/\$3",$_SERVER['REQUEST_URI']);
416 return $_SERVER['REQUEST_URI'];
420 /** Returns the path to a given template. */
421 function templatePath($template)
425 $bits = split(":", $template);
428 $path = $globals->template_dir
."/". $bits[1];
431 $path = $this->barrel
->spool
->spoolPath($this->barrel
->options
->template_dir
, $bits[1]);
434 $path = parent
::templatePath($template);
440 /** Returns the URL to one of the barrel's pages relative to
441 * the current location.
446 function urlSite($dir, $file = '') {
448 $tosite = strlen($this->pathinfo
['dir']) ?
str_repeat("../",1+
substr_count($this->pathinfo
['dir'],"/")) : '';
449 $url = $tosite . (strlen($dir) ?
"$dir/" : "") . $file;
450 return strlen($url) ?
$url : "./";
454 /** Returns the URL to one of the barrel's pages relative to
455 * the current location.
460 function urlSiteByPid($PID, $file = '')
462 return $this->urlSite($this->barrel
->getLocation($PID), $file);