b27101d106c18240544b39ef2ea0d45029848272
[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()
163 {
164 global $globals;
165 $props = $this->props;
166
167 $topbar = array ();
168
169 if ($props['PID']) {
170 $hp = $this->barrel->getPID('');
171 array_push($topbar, array(__("home"), ($props['PID'] == $hp) ? "" : "files?dir=$hp"));
172 array_push($topbar, array(__("parent page"), $props['parent'] ? "files?dir=".$props['parent'] : ""));
173
174 array_push($topbar, array( __("browse files"), "files?dir={$props['PID']}" ));
175 array_push($topbar, array( __("page properties"), "pages?dir={$props['PID']}" ));
176 array_push($topbar, array( __("view page"), "../". $this->getLocation()));
177 array_push($topbar, array(__("add a page"), "pages?action=edit&parent=".$props['PID']) );
178 if ($this->barrel->flags->hasFlag("plug")) {
179 array_push($topbar, array( __("plugins"), "plugins?plug_page={$props['PID']}" ) );
180 }
181 }
182
183 return $topbar;
184
185 }
186
187
188 /** Build the 'File' toolbar
189 */
190 function make_doc_toolbar(&$rcs)
191 {
192 global $globals;
193
194 if ($globals->word_import && file_exists($rcs->spoolPath($this->props['PID'],$globals->wordfile)) ) {
195 $bfile = new Diogenes_Barrel_File($this, $globals->wordfile);
196 $toolbar = $bfile->make_toolbar(0);
197 } else {
198 $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile);
199 $toolbar = $bfile->make_toolbar(1);
200 }
201 return $toolbar;
202 }
203
204
205 /** Write the page's properties to database
206 *
207 * @param $homepage
208 * @param $caller
209 */
210 function toDb($homepage, &$caller)
211 {
212 global $globals;
213
214 $props = $this->props;
215 // check we are not creating a publicly writable page
216 // on a barrel with PHP execution enabled!
217 if ($props['PID'])
218 {
219 $cache = $globals->plugins->readCache($this->barrel->pluginsCacheFile, $this->barrel->alias);
220 $plugs_active = $globals->plugins->cachedActive($cache, $this->barrel->alias, $props['PID']);
221 foreach($plugs_active as $plugentry)
222 {
223 $plug_h = $globals->plugins->load($plugentry);
224 if (!is_object($plug_h))
225 {
226 $caller->info("failed to load plugin '{$plugentry['plugin']}'");
227 return;
228 }
229 if (!$plug_h->allow_wperms($props['wperms']))
230 {
231 $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!");
232 return;
233 }
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 }
330
331 ?>