d680bc456cab05b7fd3128d109f4c35ec048ff4c
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();
103 /** Read a Page's properties from database.
108 function fromDb(&$barrel, $dir)
113 $res = $globals->db
->query("select * from {$barrel->table_page} where PID='$dir'");
114 if ($props = mysql_fetch_assoc($res)) {
115 $retval = new Diogenes_Barrel_Page($barrel, $props);
117 mysql_free_result($res);
122 /** Returns the location of a given file
126 function getLocation($file = '')
128 $dirloc = $this->barrel
->getLocation($this->props
['PID']);
129 $floc = (strlen($dirloc) ?
"$dirloc/" : '') . $file;
134 /** Return the list of action applicable to the page
136 function make_actions()
139 $props = $this->props
;
142 array( __("view"), "../". $this->getLocation(), "view" ),
143 array( __("edit"), "edit?dir={$props['PID']}&file=page.html", "edit"),
144 array( __("properties"), "pages?action=edit&dir={$props['PID']}", "properties"),
145 array( __("revisions"), "files?action=revs&dir={$props['PID']}&target={$globals->htmlfile}","revisions")
148 if ($this->barrel
->flags
->hasFlag('plug'))
150 array_push($actions, array( __("plugins"), "plugins?plug_page={$props['PID']}", "plugins"));
152 if ($props['location'] != '')
153 array_push($actions, array( __("delete"), "javascript:page_delete('{$props['PID']}','{$props['location']}');","delete"));
155 return $globals->icons
->get_action_icons($actions);
159 /** Build the 'Page' toolbar
161 function make_toolbar()
164 $props = $this->props
;
169 $hp = $this->barrel
->getPID('');
170 array_push($topbar, array(__("home"), ($props['PID'] == $hp) ?
"" : "files?dir=$hp"));
171 array_push($topbar, array(__("parent page"), $props['parent'] ?
"files?dir=".$props['parent'] : ""));
173 array_push($topbar, array( __("browse files"), "files?dir={$props['PID']}" ));
174 array_push($topbar, array( __("page properties"), "pages?dir={$props['PID']}" ));
175 array_push($topbar, array( __("view page"), "../". $this->getLocation()));
176 array_push($topbar, array(__("add a page"), "pages?action=edit&parent=".$props['PID']) );
177 if ($this->barrel
->flags
->hasFlag("plug")) {
178 array_push($topbar, array( __("plugins"), "plugins?plug_page={$props['PID']}" ) );
187 /** Build the 'File' toolbar
189 function make_doc_toolbar(&$rcs)
193 if ($globals->word_import
&& file_exists($rcs->spoolPath($this->props
['PID'],$globals->wordfile
)) ) {
194 $bfile = new Diogenes_Barrel_File($this, $globals->wordfile
);
195 $toolbar = $bfile->make_toolbar(0);
197 $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile
);
198 $toolbar = $bfile->make_toolbar(1);
204 /** Write the page's properties to database
209 function toDb($homepage, &$caller)
213 $props = $this->props
;
214 // check we are not creating a publicly writable page
215 // on a barrel with PHP execution enabled!
218 $cache = $globals->plugins
->readCache($this->barrel
->pluginsCacheFile
, $this->barrel
->alias
);
219 $plugs_active = $globals->plugins
->cachedActive($cache, $this->barrel
->alias
, $props['PID']);
220 foreach($plugs_active as $plugentry)
222 $plug_h = $globals->plugins
->load($plugentry);
223 if (!is_object($plug_h))
225 $caller->info("failed to load plugin '{$plugentry['plugin']}'");
228 if (!$plug_h->allow_wperms($props['wperms']))
230 $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!");
237 // check that the location is valid
241 $props['location'] = '';
245 // check the location is well formatted
246 if (!preg_match("/^[a-zA-Z0-9_\-]*$/",$props['location']))
248 $caller->info(__("the page location cannot contain spaces or special characters"));
252 // check this is not a forbidden location
253 if (in_array($props['location'], $globals->invalidlocations
))
255 $caller->info(__("this location cannot be used, it is reserved by Diogenes"));
261 // this is a new entry, initialise
262 if (!$props['PID']) {
264 $globals->db
->query("insert into {$this->barrel->table_page} set location='temp'");
265 $props['PID'] = mysql_insert_id();
266 $caller->info(__("Creating new page")." {$props['PID']}");
268 $caller->log("page_create","{$this->barrel->alias}:{$props['PID']}");
270 // initialise the page
271 $rcs = $caller->getRcs();
272 $rcs->newdir("",$props['PID']);
273 $rcs->commit($props['PID'],$globals->htmlfile
,"");
275 $caller->log("page_props","{$this->barrel->alias}:{$props['PID']}");
279 // check we have a location
280 if (!$homepage and !strlen($props['location']))
282 $props['location'] = $props['PID'];
286 // check that the filiation is valid
287 if ($props['parent'])
289 // we need to insure that the parent is not already a child of the current page
290 $parent = $props['parent'];
293 $oldparent = $parent;
294 $res = $globals->db
->query("select parent from {$this->barrel->table_page} where PID=$parent");
295 list($parent) = mysql_fetch_row($res);
296 mysql_free_result($res);
297 if ($parent == $props['PID'])
299 $caller->info(__("A page cannot be its own parent (page $oldparent is a child of $parent)!"));
305 // $caller->info("setting parent to {$props['parent']}");
307 // update data fields
309 "update {$this->barrel->table_page} set ".
310 "parent='{$props['parent']}',".
311 "location='{$props['location']}',".
312 "title='{$props['title']}',".
313 "perms='{$props['perms']}',".
314 "wperms='{$props['wperms']}',".
315 "status='{$props['status']}',".
316 "template='{$props['template']}' ".
317 "where PID='{$props['PID']}'";
318 //$caller->info($sql);
319 $globals->db
->query($sql);
322 $globals->db
->query("alter table {$this->barrel->table_page} order by parent,location");
325 $this->barrel
->compileTree();
326 $this->barrel
->readTree();