tighten return codes
[diogenes.git] / include / Barrel / Page.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
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.
10 *
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.
15 *
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
19 */
20
21 require_once 'diogenes.icons.inc.php';
22 require_once 'Barrel/File.php';
23
24
25 /** This class describes a Barrel Page.
26 */
27 class Diogenes_Barrel_Page
28 {
29 /** The Barrel this Page belongs to. */
30 var $barrel;
31
32 /** The Page's properties */
33 var $props = array();
34
35
36 /** Construct a Barrel Page object.
37 *
38 * @param $barrel
39 * @param $props
40 */
41 function Diogenes_Barrel_Page(&$barrel, $props)
42 {
43 if (!is_object($barrel))
44 {
45 trigger_error("\$barrel is not an object!", E_USER_ERROR);
46 }
47
48 $this->barrel =& $barrel;
49 $this->props = array(
50 'PID' => 0,
51 'parent' => 0,
52 'location' => '',
53 'title' => '',
54 'perms' => 'public',
55 'wperms' => 'admin',
56 'status' => 0,
57 'template' => ''
58 );
59
60 if (is_array($props))
61 {
62 foreach (array_keys($props) as $key)
63 {
64 $this->props[$key] = $props[$key];
65 }
66 }
67 //echo "[" . $this->props['PID'] . "][". $this->props['location'] . "], parent : [".$this->props['parent']."]<br/>";
68 }
69
70
71 /** Delete a Barrel Page.
72 *
73 * @param $barrel
74 * @param $dir
75 * @param $caller
76 */
77 function delete(&$barrel, $dir, &$caller)
78 {
79 global $globals;
80
81 $caller->info(__("Deleting page"). " $dir");
82
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);
87 if ($num > 0)
88 {
89 $caller->info(__("Not deleting page, it has child pages!"));
90 return false;
91 }
92
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();
99 $barrel->readTree();
100 return true;
101 }
102
103
104 /** Read a Page's properties from database.
105 *
106 * @param $barrel
107 * @param $dir
108 */
109 function fromDb(&$barrel, $dir)
110 {
111 global $globals;
112 $retval = '';
113
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);
117 }
118 mysql_free_result($res);
119 return $retval;
120 }
121
122
123 /** Returns the location of a given file
124 *
125 * @param $file
126 */
127 function getLocation($file = '')
128 {
129 $dirloc = $this->barrel->getLocation($this->props['PID']);
130 $floc = (strlen($dirloc) ? "$dirloc/" : '') . $file;
131 return $floc;
132 }
133
134
135 /** Return the list of action applicable to the page
136 */
137 function make_actions()
138 {
139 global $globals;
140 $props = $this->props;
141
142 $actions = array(
143 array( __("view"), "../". $this->getLocation(), "view" ),
144 array( __("edit"), "edit?dir={$props['PID']}&amp;file=page.html", "edit"),
145 array( __("properties"), "pages?action=edit&amp;dir={$props['PID']}", "properties"),
146 array( __("revisions"), "files?action=revs&amp;dir={$props['PID']}&amp;target={$globals->htmlfile}","revisions")
147 );
148
149 if ($this->barrel->flags->hasFlag('plug'))
150 {
151 array_push($actions, array( __("plugins"), "plugins?plug_page={$props['PID']}", "plugins"));
152 }
153 if ($props['location'] != '')
154 array_push($actions, array( __("delete"), "javascript:page_delete('{$props['PID']}','{$props['location']}');","delete"));
155
156 return $globals->icons->get_action_icons($actions);
157 }
158
159
160 /** Build the 'Page' toolbar
161 */
162 function make_toolbar(&$caller)
163 {
164 global $globals;
165 $props = $this->props;
166
167 $topbar = array ();
168 $from = htmlentities($caller->script_uri());
169
170 if ($props['PID']) {
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'] : ""));
174
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&amp;parent=".$props['PID']."&amp;from=$from") );
179 if ($this->barrel->flags->hasFlag("plug")) {
180 array_push($topbar, array( __("plugins"), "plugins?plug_page={$props['PID']}" ) );
181 }
182 }
183
184 return $topbar;
185
186 }
187
188
189 /** Build the 'File' toolbar
190 */
191 function make_doc_toolbar(&$rcs)
192 {
193 global $globals;
194
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);
198 } else {
199 $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile);
200 $toolbar = $bfile->make_toolbar(1);
201 }
202 return $toolbar;
203 }
204
205
206 /** Write the page's properties to database and returns the PID of that page.
207 *
208 * @param $homepage
209 * @param $caller
210 */
211 function toDb($homepage, &$caller)
212 {
213 global $globals;
214
215 $props = $this->props;
216 // check we are not creating a publicly writable page
217 // on a barrel with PHP execution enabled!
218 if ($props['PID'])
219 {
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)
223 {
224 $plug_h = $globals->plugins->load($plugentry);
225 if (!is_object($plug_h))
226 {
227 $caller->info("failed to load plugin '{$plugentry['plugin']}'");
228 return;
229 }
230 if (!$plug_h->allow_wperms($props['wperms']))
231 {
232 $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!");
233 return;
234 }
235 }
236 }
237
238 // check that the location is valid
239 if ($homepage)
240 {
241 // homepage
242 $props['location'] = '';
243
244 } else {
245
246 // check the location is well formatted
247 if (!preg_match("/^[a-zA-Z0-9_\-]*$/",$props['location']))
248 {
249 $caller->info(__("the page location cannot contain spaces or special characters"));
250 return;
251 }
252
253 // check this is not a forbidden location
254 if (in_array($props['location'], $globals->invalidlocations))
255 {
256 $caller->info(__("this location cannot be used, it is reserved by Diogenes"));
257 return;
258 }
259
260 }
261
262 // this is a new entry, initialise
263 if (!$props['PID']) {
264 // new entry
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']}");
268
269 $caller->log("page_create","{$this->barrel->alias}:{$props['PID']}");
270
271 // initialise the page
272 $rcs = $caller->getRcs();
273 $rcs->newdir("",$props['PID']);
274 $rcs->commit($props['PID'],$globals->htmlfile,"");
275 } else {
276 $caller->log("page_props","{$this->barrel->alias}:{$props['PID']}");
277 }
278
279
280 // check we have a location
281 if (!$homepage and !strlen($props['location']))
282 {
283 $props['location'] = $props['PID'];
284 }
285
286
287 // check that the filiation is valid
288 if ($props['parent'])
289 {
290 // we need to insure that the parent is not already a child of the current page
291 $parent = $props['parent'];
292 while ($parent)
293 {
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'])
299 {
300 $caller->info(__("A page cannot be its own parent (page $oldparent is a child of $parent)!"));
301 break;
302 }
303 }
304 }
305
306 // $caller->info("setting parent to {$props['parent']}");
307
308 // update data fields
309 $sql =
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);
321
322 // order by location
323 $globals->db->query("alter table {$this->barrel->table_page} order by parent,location");
324
325 // recompile tree
326 $this->barrel->compileTree();
327 $this->barrel->readTree();
328
329 return $props['PID'];
330 }
331
332 }
333
334 ?>