| 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; |
| 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 | } |
| 101 | |
| 102 | |
| 103 | /** Read a Page's properties from database. |
| 104 | * |
| 105 | * @param $barrel |
| 106 | * @param $dir |
| 107 | */ |
| 108 | function fromDb(&$barrel, $dir) |
| 109 | { |
| 110 | global $globals; |
| 111 | $retval = ''; |
| 112 | |
| 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); |
| 116 | } |
| 117 | mysql_free_result($res); |
| 118 | return $retval; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** Returns the location of a given file |
| 123 | * |
| 124 | * @param $file |
| 125 | */ |
| 126 | function getLocation($file = '') |
| 127 | { |
| 128 | $dirloc = $this->barrel->getLocation($this->props['PID']); |
| 129 | $floc = (strlen($dirloc) ? "$dirloc/" : '') . $file; |
| 130 | return $floc; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /** Return the list of action applicable to the page |
| 135 | */ |
| 136 | function make_actions() |
| 137 | { |
| 138 | global $globals; |
| 139 | $props = $this->props; |
| 140 | |
| 141 | $actions = array( |
| 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") |
| 146 | ); |
| 147 | |
| 148 | if ($this->barrel->flags->hasFlag('plug')) |
| 149 | { |
| 150 | array_push($actions, array( __("plugins"), "plugins?plug_page={$props['PID']}", "plugins")); |
| 151 | } |
| 152 | if ($props['location'] != '') |
| 153 | array_push($actions, array( __("delete"), "javascript:page_delete('{$props['PID']}','{$props['location']}');","delete")); |
| 154 | |
| 155 | return $globals->icons->get_action_icons($actions); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** Build the 'Page' toolbar |
| 160 | */ |
| 161 | function make_toolbar() |
| 162 | { |
| 163 | global $globals; |
| 164 | $props = $this->props; |
| 165 | |
| 166 | $topbar = array (); |
| 167 | |
| 168 | if ($props['PID']) { |
| 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'] : "")); |
| 172 | |
| 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']}" ) ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return $topbar; |
| 183 | |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** Build the 'File' toolbar |
| 188 | */ |
| 189 | function make_doc_toolbar(&$rcs) |
| 190 | { |
| 191 | global $globals; |
| 192 | |
| 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); |
| 196 | } else { |
| 197 | $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile); |
| 198 | $toolbar = $bfile->make_toolbar(1); |
| 199 | } |
| 200 | return $toolbar; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** Write the page's properties to database |
| 205 | * |
| 206 | * @param $homepage |
| 207 | * @param $caller |
| 208 | */ |
| 209 | function toDb($homepage, &$caller) |
| 210 | { |
| 211 | global $globals; |
| 212 | |
| 213 | $props = $this->props; |
| 214 | // check we are not creating a publicly writable page |
| 215 | // on a barrel with PHP execution enabled! |
| 216 | if ($props['PID']) |
| 217 | { |
| 218 | $cache = $globals->plugins->readCache($this->barrel->pluginsCacheFile, $this->barrel->alias); |
| 219 | $plugs_active = $globals->plugins->cacheGetActive($cache, $this->barrel->alias, $props['PID']); |
| 220 | foreach($plugs_active as $plugentry) |
| 221 | { |
| 222 | $plug_h = $globals->plugins->load($plugentry); |
| 223 | if (!is_object($plug_h)) |
| 224 | { |
| 225 | $caller->info("failed to load plugin '{$plugentry['plugin']}'"); |
| 226 | return; |
| 227 | } |
| 228 | if (!$plug_h->allow_wperms($props['wperms'])) |
| 229 | { |
| 230 | $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!"); |
| 231 | return; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | // check that the location is valid |
| 238 | if ($homepage) |
| 239 | { |
| 240 | // homepage |
| 241 | $props['location'] = ''; |
| 242 | |
| 243 | } else { |
| 244 | |
| 245 | // check the location is well formatted |
| 246 | if (!preg_match("/^[a-zA-Z0-9_\-]*$/",$props['location'])) |
| 247 | { |
| 248 | $caller->info(__("the page location cannot contain spaces or special characters")); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | // check this is not a forbidden location |
| 253 | if (in_array($props['location'], $globals->invalidlocations)) |
| 254 | { |
| 255 | $caller->info(__("this location cannot be used, it is reserved by Diogenes")); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | } |
| 260 | |
| 261 | // this is a new entry, initialise |
| 262 | if (!$props['PID']) { |
| 263 | // new entry |
| 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']}"); |
| 267 | |
| 268 | $caller->log("page_create","{$this->barrel->alias}:{$props['PID']}"); |
| 269 | |
| 270 | // initialise the page |
| 271 | $rcs = $caller->getRcs(); |
| 272 | $rcs->newdir("",$props['PID']); |
| 273 | $rcs->commit($props['PID'],$globals->htmlfile,""); |
| 274 | } else { |
| 275 | $caller->log("page_props","{$this->barrel->alias}:{$props['PID']}"); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | // check we have a location |
| 280 | if (!$homepage and !strlen($props['location'])) |
| 281 | { |
| 282 | $props['location'] = $props['PID']; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | // check that the filiation is valid |
| 287 | if ($props['parent']) |
| 288 | { |
| 289 | // we need to insure that the parent is not already a child of the current page |
| 290 | $parent = $props['parent']; |
| 291 | while ($parent) |
| 292 | { |
| 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']) |
| 298 | { |
| 299 | $caller->info(__("A page cannot be its own parent (page $oldparent is a child of $parent)!")); |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // $caller->info("setting parent to {$props['parent']}"); |
| 306 | |
| 307 | // update data fields |
| 308 | $sql = |
| 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); |
| 320 | |
| 321 | // order by location |
| 322 | $globals->db->query("alter table {$this->barrel->table_page} order by parent,location"); |
| 323 | |
| 324 | // recompile tree |
| 325 | $this->barrel->compileTree(); |
| 326 | $this->barrel->readTree(); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | ?> |