3 * Copyright (C) 2003-2004 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
21 require_once 'Barrel/Page.php';
22 require_once 'Barrel/Options.php';
23 require_once 'diogenes/diogenes.flagset.inc.php';
24 require_once 'Plugin/Skel.php';
27 /** This class describes a Diogenes Barrel.
31 /** The barrel's alias. */
34 /** The database table holding the menus */
37 /** The database table holding the pages */
40 /** The site's flags. */
43 /** The site's options. */
46 /** Cache of the homepage ID */
49 /** Handle to the spool */
52 /** If the barrel is running on a virtualhost. */
58 /** File containing the tree cache. */
61 /** Cache of the plugins */
64 /** File containing the plugin cache. */
65 var $pluginsCacheFile;
68 /** Construct a Diogenes Barrel.
72 function Diogenes_Barrel($alias)
77 // Retrieve site-wide info from database
78 $res = $globals->db
->query("select alias,vhost,flags from diogenes_site where alias='$alias'");
79 if (!list($this->alias
,$this->vhost
,$flags) = mysql_fetch_row($res)) {
82 mysql_free_result($res);
84 $this->table_menu
= "{$this->alias}_menu";
85 $this->table_page
= "{$this->alias}_page";
86 $this->treeCacheFile
= $globals->spoolroot
."/diogenes_c/". $this->alias
.".tree";
87 $this->pluginsCacheFile
= $globals->plugins
->cacheFile($this->alias
);
89 $this->flags
= new flagset($flags);
90 $this->options
= new Diogenes_Barrel_Options($this->alias
);
91 $this->spool
= new DiogenesSpool($this,$this->alias
);
97 /** Create a new Diogenes barrel. This creates the database, RCS and
98 * spool entries for the new barrel.
103 function create($alias, &$caller)
108 if (!preg_match("/^[a-zA-Z0-9_]+$/",$alias) or
109 in_array($alias, $globals->invalidaliases
))
111 $caller->info("Invalid barrel name!");
115 $res = $globals->db
->query("select alias from diogenes_site where alias='$alias'");
116 if (mysql_num_rows($res) > 0) {
117 $caller->info("Entry '{$alias}' already exists in table 'diogenes_site'!");
121 if (file_exists("{$globals->rcsroot}/$alias")) {
122 $caller->info("Directory '{$globals->rcsroot}/$alias' already exists!");
126 if (!is_dir($globals->rcsroot
) ||
!is_writable($globals->rcsroot
)) {
127 $caller->info("Directory '{$globals->rcsroot}' is not writable!");
132 $caller->log("barrel_create","$alias:*");
134 /* create DB entry */
135 $globals->db
->query("insert into diogenes_site set alias='$alias'");
137 $globals->db
->query("CREATE TABLE {$alias}_menu ("
138 . "MID int(10) unsigned NOT NULL auto_increment,"
139 . "MIDpere int(10) unsigned NOT NULL,"
140 . "ordre int(10) unsigned NOT NULL,"
141 . "title tinytext NOT NULL,"
142 . "link text NOT NULL,"
143 . "PID int(10) unsigned NOT NULL,"
144 . "PRIMARY KEY (MID)"
147 $globals->db
->query("CREATE TABLE {$alias}_page ("
148 . "PID int(10) unsigned NOT NULL auto_increment,"
149 . "parent INT( 10 ) UNSIGNED NOT NULL default '0',"
150 . "location tinytext NOT NULL,"
151 . "title tinytext NOT NULL,"
152 . "status tinyint(1) unsigned NOT NULL default '0',"
153 . "perms enum('public','auth','user','admin','forbidden') NOT NULL default 'public',"
154 . "wperms enum('public','auth','user','admin','forbidden') NOT NULL default 'admin',"
155 . "template varchar(255) NOT NULL,"
156 . "PRIMARY KEY (PID)"
159 /* set the barrel's title */
160 $opt = new Diogenes_Barrel_Options($alias);
161 $opt->updateOption('title',$alias);
163 /* create entry for the homepage */
164 $globals->db
->query("insert into {$alias}_page set location='temp'");
165 $homepage = mysql_insert_id();
166 $globals->db
->query("update {$alias}_page set location='',title='Home',perms='public' where PID='$homepage'");
168 /* create home page & copy CSS template */
169 $rcs = new $globals->rcs($caller,$alias,$_SESSION['session']->username
,true
);
170 $rcs->newdir("",$homepage);
171 $rcs->commit($homepage,$globals->htmlfile
,"");
172 $rcs->commit($homepage,$globals->cssfile
,
173 file_get_contents("{$globals->root}/{$globals->cssfile}") );
177 /** Destroy a Diogenes barrel. This removes the related database, RCS
183 function destroy($alias, &$caller) {
188 $caller->info("Empty alias supplied!");
193 $caller->log("barrel_delete","$alias:*");
195 system(escapeshellcmd("rm -rf ".escapeshellarg("{$globals->spoolroot}/$alias")));
196 system(escapeshellcmd("rm -rf ".escapeshellarg("{$globals->rcsroot}/$alias")));
197 system(escapeshellcmd("rm -f ".escapeshellarg("{$globals->spoolroot}/diogenes_c/$alias.tree")));
198 system(escapeshellcmd("rm -f ".escapeshellarg($globals->plugins
->cacheFile($alias))));
199 $globals->db
->query("drop table {$alias}_menu");
200 $globals->db
->query("drop table {$alias}_page");
201 $globals->db
->query("delete from diogenes_perm where alias='$alias'");
202 $globals->db
->query("delete from diogenes_site where alias='$alias'");
203 $globals->db
->query("delete from diogenes_option where barrel='$alias'");
204 $globals->db
->query("delete from diogenes_plugin where barrel='$alias'");
208 /** Return the location corresponding to a given page ID
212 function getLocation($PID)
214 return array_search($PID, $this->treeCache
);
219 /** Return all the barrel's pages.
226 $res = $globals->db
->query("select * from {$this->table_page}");
227 while ($props = mysql_fetch_assoc($res)) {
228 $bpages[$props['PID']] = new Diogenes_Barrel_Page($this, $props);
230 mysql_free_result($res);
235 /** Return the page ID matching a given directory location
239 function getPID($dir)
241 if (isset($this->treeCache
[$dir]))
242 return $this->treeCache
[$dir];
248 /** Check whether the barrel has a given flag
252 function hasFlag($flag)
254 return $this->flags
->hasFlag($flag);
258 /** Compile the directory tree
260 function compileTree()
264 if (!$fp = fopen($this->treeCacheFile
, "w")) {
265 trigger_error("failed to open '{$this->treeCacheFile}' for writing", E_USER_ERROR
);
269 // load all the pages
270 $res = $globals->db
->query("select * from {$this->table_page}");
272 while ($props = mysql_fetch_assoc($res))
274 $tpage = new Diogenes_Barrel_Page($this, $props);
275 $tpages[$props['PID']] = $tpage;
276 if (!strlen($props['location']))
278 $homepage = $props['PID'];
282 // recursively build the tree, starting at the homepage
283 $str = $this->compilePageTree($tpages, $homepage, '');
289 /** Compile a single page
291 function compilePageTree(&$tpages, $PID, $ploc)
295 $cpage = $tpages[$PID];
296 $ploc = ($ploc ?
"$ploc/" : "") . $cpage->props
['location'];
299 $out = "$ploc\t$PID\t".$cpage->props
['parent']."\n";
302 $res = $globals->db
->query("select PID from {$this->table_page} where parent='$PID'");
303 while (list($child) = mysql_fetch_row($res))
305 $out .= $this->compilePageTree($tpages, $child, $ploc);
307 mysql_free_result($res);
312 /** Load all active plugins for the specified page.
316 function loadPlugins(&$bpage)
320 $plugins = $globals->plugins
->cacheGetActive($this->pluginsCache
, $this->alias
, $page);
322 foreach ($plugins as $plugname => $plugentry)
324 $loaded[$plugname] =& $globals->plugins
->load($plugname, $plugentry);
330 /** Read the compiled plugin cache
332 function readPlugins()
336 $this->pluginsCache
= $globals->plugins
->readCache($this->pluginsCacheFile
, $this->alias
);
340 /** Read the compiled directory tree
346 // if the tree cache does not exits, try to init it
347 if (!file_exists($this->treeCacheFile
)) {
348 $this->compileTree();
351 if (!$fp = fopen($this->treeCacheFile
, "r")) {
352 trigger_error("failed to open '{$this->treeCacheFile}' for reading", E_USER_ERROR
);
356 $locations = array();
357 while ($line = fgets($fp))
359 $line = substr($line, 0, -1);
360 $bits = explode("\t", $line);
361 list($loc, $pid, $parent) = $bits;
362 $locations[$loc] = $pid;
366 $this->treeCache
= $locations;