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 'diogenes.icons.inc.php';
22 require_once 'Barrel/File.php';
25 /** This class describes a Barrel Page.
27 class Diogenes_Barrel_Page
29 /** The Barrel this Page belongs to. */
32 /** The Page's properties */
36 /** Construct a Barrel Page object.
41 function Diogenes_Barrel_Page(&$barrel, $props)
43 if (!is_object($barrel))
45 trigger_error("\$barrel is not an object!", E_USER_ERROR
);
48 $this->barrel
=& $barrel;
62 foreach (array_keys($props) as $key)
64 $this->props
[$key] = $props[$key];
67 //echo "[" . $this->props['PID'] . "][". $this->props['location'] . "], parent : [".$this->props['parent']."]<br/>";
71 /** Delete a Barrel Page.
77 function delete(&$barrel, $dir, &$caller)
81 $caller->info(__("Deleting page"). " $dir");
83 // check there are no child pages
84 $res = $globals->db
->query("select PID from {$barrel->table_page} where parent=$dir");
85 $num = mysql_num_rows($res);
86 mysql_free_result($res);
89 $caller->info(__("Not deleting page, it has child pages!"));
93 $rcs = $caller->getRcs();
94 $globals->db
->query("delete from {$barrel->table_page} where PID=$dir");
95 $caller->log("page_delete","{$barrel->alias}:$dir");
96 system("rm -rf ". escapeshellarg($rcs->rcsPath($dir)));
97 system("rm -rf ". escapeshellarg($rcs->spoolPath($dir)));
98 $barrel->compileTree();
104 /** Read a Page's properties from database.
109 function fromDb(&$barrel, $dir)
114 $res = $globals->db
->query("select * from {$barrel->table_page} where PID='$dir'");
115 if ($props = mysql_fetch_assoc($res)) {
116 $retval = new Diogenes_Barrel_Page($barrel, $props);
118 mysql_free_result($res);
123 /** Returns the location of a given file
127 function getLocation($file = '')
129 $dirloc = $this->barrel
->getLocation($this->props
['PID']);
130 $floc = (strlen($dirloc) ?
"$dirloc/" : '') . $file;
135 /** Return the list of action applicable to the page
137 function make_actions()
140 $props = $this->props
;
143 array( __("view"), "../". $this->getLocation(), "view" ),
144 array( __("edit"), "edit?dir={$props['PID']}&file=page.html", "edit"),
145 array( __("properties"), "pages?action=edit&dir={$props['PID']}", "properties"),
146 array( __("revisions"), "files?action=revs&dir={$props['PID']}&target={$globals->htmlfile}","revisions")
149 if ($this->barrel
->flags
->hasFlag('plug'))
151 array_push($actions, array( __("plugins"), "plugins?plug_page={$props['PID']}", "plugins"));
153 if ($props['location'] != '')
154 array_push($actions, array( __("delete"), "javascript:page_delete('{$props['PID']}','{$props['location']}');","delete"));
156 return $globals->icons
->get_action_icons($actions);
160 /** Build the 'Page' toolbar
162 function make_toolbar(&$caller)
165 $props = $this->props
;
168 $from = htmlentities($caller->script_uri());
171 $hp = $this->barrel
->getPID('');
172 array_push($topbar, array(__("home"), ($props['PID'] == $hp) ?
"" : "files?dir=$hp"));
173 array_push($topbar, array(__("parent page"), $props['parent'] ?
"files?dir=".$props['parent'] : ""));
175 array_push($topbar, array( __("browse files"), "files?dir={$props['PID']}" ));
176 array_push($topbar, array( __("page properties"), "pages?dir={$props['PID']}" ));
177 array_push($topbar, array( __("view page"), "../". $this->getLocation()));
178 array_push($topbar, array(__("add a page"), "pages?action=edit&parent=".$props['PID']."&from=$from") );
179 if ($this->barrel
->flags
->hasFlag("plug")) {
180 array_push($topbar, array( __("plugins"), "plugins?plug_page={$props['PID']}" ) );
189 /** Build the 'File' toolbar
191 function make_doc_toolbar(&$rcs)
195 if ($globals->word_import
&& file_exists($rcs->spoolPath($this->props
['PID'],$globals->wordfile
)) ) {
196 $bfile = new Diogenes_Barrel_File($this, $globals->wordfile
);
197 $toolbar = $bfile->make_toolbar(0);
199 $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile
);
200 $toolbar = $bfile->make_toolbar(1);
206 /** Write the page's properties to database and returns the PID of that page.
211 function toDb($homepage, &$caller)
215 $props = $this->props
;
216 // check we are not creating a publicly writable page
217 // on a barrel with PHP execution enabled!
220 $cache = $globals->plugins
->readCache($this->barrel
->pluginsCacheFile
, $this->barrel
->alias
);
221 $plugs_active = $globals->plugins
->cachedActive($cache, $this->barrel
->alias
, $props['PID']);
222 foreach($plugs_active as $plugentry)
224 $plug_h = $globals->plugins
->load($plugentry);
225 if (!is_object($plug_h))
227 $caller->info("failed to load plugin '{$plugentry['plugin']}'");
230 if (!$plug_h->allow_wperms($props['wperms']))
232 $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!");
238 // check that the location is valid
242 $props['location'] = '';
246 // check the location is well formatted
247 if (!preg_match("/^[a-zA-Z0-9_\-]*$/",$props['location']))
249 $caller->info(__("the page location cannot contain spaces or special characters"));
253 // check this is not a forbidden location
254 if (in_array($props['location'], $globals->invalidlocations
))
256 $caller->info(__("this location cannot be used, it is reserved by Diogenes"));
262 // this is a new entry, initialise
263 if (!$props['PID']) {
265 $globals->db
->query("insert into {$this->barrel->table_page} set location='temp'");
266 $props['PID'] = mysql_insert_id();
267 $caller->info(__("Creating new page")." {$props['PID']}");
269 $caller->log("page_create","{$this->barrel->alias}:{$props['PID']}");
271 // initialise the page
272 $rcs = $caller->getRcs();
273 $rcs->newdir("",$props['PID']);
274 $rcs->commit($props['PID'],$globals->htmlfile
,"");
276 $caller->log("page_props","{$this->barrel->alias}:{$props['PID']}");
280 // check we have a location
281 if (!$homepage and !strlen($props['location']))
283 $props['location'] = $props['PID'];
287 // check that the filiation is valid
288 if ($props['parent'])
290 // we need to insure that the parent is not already a child of the current page
291 $parent = $props['parent'];
294 $oldparent = $parent;
295 $res = $globals->db
->query("select parent from {$this->barrel->table_page} where PID=$parent");
296 list($parent) = mysql_fetch_row($res);
297 mysql_free_result($res);
298 if ($parent == $props['PID'])
300 $caller->info(__("A page cannot be its own parent (page $oldparent is a child of $parent)!"));
306 // $caller->info("setting parent to {$props['parent']}");
308 // update data fields
310 "update {$this->barrel->table_page} set ".
311 "parent='{$props['parent']}',".
312 "location='{$props['location']}',".
313 "title='{$props['title']}',".
314 "perms='{$props['perms']}',".
315 "wperms='{$props['wperms']}',".
316 "status='{$props['status']}',".
317 "template='{$props['template']}' ".
318 "where PID='{$props['PID']}'";
319 //$caller->info($sql);
320 $globals->db
->query($sql);
323 $globals->db
->query("alter table {$this->barrel->table_page} order by parent,location");
326 $this->barrel
->compileTree();
327 $this->barrel
->readTree();
329 return $props['PID'];