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/File.php';
24 /** This class describes a Barrel Page.
26 class Diogenes_Barrel_Page
28 /** The Barrel this Page belongs to. */
31 /** The Page's properties */
35 /** Construct a Barrel Page object.
40 function Diogenes_Barrel_Page(&$barrel, $props)
42 if (!is_object($barrel))
44 trigger_error("\$barrel is not an object!", E_USER_ERROR
);
47 $this->barrel
=& $barrel;
61 foreach (array_keys($props) as $key)
63 $this->props
[$key] = $props[$key];
66 //echo "[" . $this->props['PID'] . "][". $this->props['location'] . "], parent : [".$this->props['parent']."]<br/>";
70 /** Delete a Barrel Page.
76 function delete(&$barrel, $dir, &$caller)
80 $loc = $barrel->getLocation($dir);
83 $caller->info("Error : not deleting page $dir\n");
87 $caller->info(__("Deleting page"). " '$loc'");
89 // check there are no child pages
90 $res = $globals->db
->query("select PID from {$barrel->table_page} where parent=$dir");
91 $num = mysql_num_rows($res);
92 mysql_free_result($res);
95 $caller->info(__("Not deleting page, it has child pages!"));
99 $rcs = $caller->getRcs();
100 $globals->db
->query("delete from {$barrel->table_page} where PID=$dir");
101 $caller->log("page_delete","{$barrel->alias}:$loc");
102 system("rm -rf ". escapeshellarg($rcs->rcsPath($dir)));
103 system("rm -rf ". escapeshellarg($rcs->spoolPath($dir)));
104 $barrel->compileTree();
110 /** Read a Page's properties from database.
115 function fromDb(&$barrel, $dir)
120 $res = $globals->db
->query("select * from {$barrel->table_page} where PID='$dir'");
121 if ($props = mysql_fetch_assoc($res)) {
122 $retval = new Diogenes_Barrel_Page($barrel, $props);
124 mysql_free_result($res);
129 /** Returns the location of a given file
133 function getLocation($file = '')
135 $dirloc = $this->barrel
->getLocation($this->props
['PID']);
136 $floc = (strlen($dirloc) ?
"$dirloc/" : '') . $file;
141 /** Return the list of action applicable to the page
143 function make_actions()
146 $props = $this->props
;
149 array( __("view"), "../". $this->getLocation(), "view" ),
150 array( __("edit"), "edit?dir={$props['PID']}&file=page.html", "edit"),
151 array( __("properties"), "pages?action=edit&dir={$props['PID']}", "properties"),
152 array( __("revisions"), "files?action=revs&dir={$props['PID']}&target={$globals->htmlfile}","revisions")
155 if ($this->barrel
->flags
->hasFlag('plug'))
157 array_push($actions, array( __("plugins"), "plugins?plug_page={$props['PID']}", "plugins"));
159 if ($props['location'] != '')
160 array_push($actions, array( __("delete"), "javascript:page_delete('{$props['PID']}','{$props['location']}');","delete"));
162 return $globals->icons
->get_action_icons($actions);
166 /** Build the 'Page' toolbar
168 function make_toolbar(&$caller)
171 $props = $this->props
;
174 $from = htmlentities($caller->script_uri(), ENT_COMPAT | ENT_HTML401
, "ISO-8859-1");
177 $hp = $this->barrel
->getPID('');
178 array_push($topbar, array(__("home"), ($props['PID'] == $hp) ?
"" : "files?dir=$hp"));
179 array_push($topbar, array(__("parent page"), $props['parent'] ?
"files?dir=".$props['parent'] : ""));
181 array_push($topbar, array( __("browse files"), "files?dir={$props['PID']}" ));
182 array_push($topbar, array( __("page properties"), "pages?dir={$props['PID']}" ));
183 array_push($topbar, array( __("view page"), "../". $this->getLocation()));
184 array_push($topbar, array(__("add a page"), "pages?action=edit&parent=".$props['PID']."&from=$from") );
185 if ($this->barrel
->flags
->hasFlag("plug")) {
186 array_push($topbar, array( __("plugins"), "plugins?plug_page={$props['PID']}" ) );
195 /** Build the 'File' toolbar
197 function make_doc_toolbar(&$rcs)
201 if ($globals->word_import
&& file_exists($rcs->spoolPath($this->props
['PID'],$globals->wordfile
)) ) {
202 $bfile = new Diogenes_Barrel_File($this, $globals->wordfile
);
203 $toolbar = $bfile->make_toolbar(0);
205 $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile
);
206 $toolbar = $bfile->make_toolbar(1);
212 /** Write the page's properties to database and returns the PID of that page.
217 function toDb($homepage, &$caller)
221 $props = $this->props
;
222 // check we are not creating a publicly writable page
223 // on a barrel with PHP execution enabled!
226 $cache = $globals->plugins
->readCache($this->barrel
->pluginsCacheFile
, $this->barrel
->alias
);
227 $plugs_active = $globals->plugins
->cachedActive($cache, $this->barrel
->alias
, $props['PID']);
228 foreach($plugs_active as $plugentry)
230 $plug_h = $globals->plugins
->load($plugentry);
231 if (!is_object($plug_h))
233 $caller->info("failed to load plugin '{$plugentry['plugin']}'");
236 if (!$plug_h->allow_wperms($props['wperms']))
238 $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!");
244 // check that the location is valid
248 $props['location'] = '';
252 // check the location is well formatted
253 if (!preg_match("/^[a-zA-Z0-9_\-]*$/",$props['location']))
255 $caller->info(__("the page location cannot contain spaces or special characters"));
259 // check this is not a forbidden location
260 if (in_array($props['location'], $globals->invalidlocations
))
262 $caller->info(__("this location cannot be used, it is reserved by Diogenes"));
268 // this is a new entry, initialise
269 if (!$props['PID']) {
271 $globals->db
->query("insert into {$this->barrel->table_page} set location='temp'");
272 $props['PID'] = mysql_insert_id();
273 $caller->info(__("Creating new page")." {$props['PID']}");
275 $caller->log("page_create","{$this->barrel->alias}:{$props['PID']}");
277 // initialise the page
278 $rcs = $caller->getRcs();
279 $rcs->newdir("",$props['PID']);
280 $rcs->commit($props['PID'],$globals->htmlfile
,"");
282 $caller->log("page_props","{$this->barrel->alias}:{$props['PID']}");
286 // check we have a location
287 if (!$homepage and !strlen($props['location']))
289 $props['location'] = $props['PID'];
293 // check that the filiation is valid
294 if ($props['parent'])
296 // we need to insure that the parent is not already a child of the current page
297 $parent = $props['parent'];
300 $oldparent = $parent;
301 $res = $globals->db
->query("select parent from {$this->barrel->table_page} where PID=$parent");
302 list($parent) = mysql_fetch_row($res);
303 mysql_free_result($res);
304 if ($parent == $props['PID'])
306 $caller->info(__("A page cannot be its own parent (page $oldparent is a child of $parent)!"));
312 // $caller->info("setting parent to {$props['parent']}");
314 // update data fields
316 "update {$this->barrel->table_page} set ".
317 "parent='{$props['parent']}',".
318 "location='{$props['location']}',".
319 "title='{$props['title']}',".
320 "perms='{$props['perms']}',".
321 "wperms='{$props['wperms']}',".
322 "status='{$props['status']}',".
323 "template='{$props['template']}' ".
324 "where PID='{$props['PID']}'";
325 //$caller->info($sql);
326 $globals->db
->query($sql);
329 $globals->db
->query("alter table {$this->barrel->table_page} order by parent,location");
332 $this->barrel
->compileTree();
333 $this->barrel
->readTree();
335 return $props['PID'];