Page.php

00001 <?php
00002 /*
00003  * Copyright (C) 2003-2004 Polytechnique.org
00004  * http://opensource.polytechnique.org/
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020  
00021 require_once 'Barrel/File.php';
00022 
00023 
00026 class Diogenes_Barrel_Page
00027 {
00029   var $barrel;
00030   
00032   var $props = array();
00033   
00034   
00040   function Diogenes_Barrel_Page(&$barrel, $props)
00041   {    
00042     if (!is_object($barrel)) 
00043     {
00044       trigger_error("\$barrel is not an object!", E_USER_ERROR);
00045     }
00046     
00047     $this->barrel =& $barrel;
00048     $this->props = array(
00049       'PID' => 0,
00050       'parent' => 0,
00051       'location' => '',
00052       'title' => '',
00053       'perms' => 'public',
00054       'wperms' => 'admin',
00055       'status' => 0,
00056       'template' => ''
00057     );
00058     
00059     if (is_array($props))
00060     {
00061       foreach (array_keys($props) as $key) 
00062       {
00063         $this->props[$key] = $props[$key];
00064       }
00065     }
00066     //echo "[" . $this->props['PID'] . "][". $this->props['location'] . "], parent : [".$this->props['parent']."]<br/>";    
00067   }
00068   
00069 
00076   function delete(&$barrel, $dir, &$caller)
00077   {     
00078     global $globals;
00079       
00080     $loc = $barrel->getLocation($dir);
00081     if (!$loc)
00082     {
00083       $caller->info("Error : not deleting page $dir\n");
00084       return false;
00085     }
00086     
00087     $caller->info(__("Deleting page"). " '$loc'");
00088     
00089     // check there are no child pages
00090     $res = $globals->db->query("select PID from {$barrel->table_page} where parent=$dir");
00091     $num = mysql_num_rows($res);
00092     mysql_free_result($res);  
00093     if ($num > 0)
00094     {
00095       $caller->info(__("Not deleting page, it has child pages!"));
00096       return false;
00097     }
00098     
00099     $rcs = $caller->getRcs();
00100     $globals->db->query("delete from {$barrel->table_page} where PID=$dir");
00101     $caller->log("page_delete","{$barrel->alias}:$loc");
00102     system("rm -rf ". escapeshellarg($rcs->rcsPath($dir)));
00103     system("rm -rf ". escapeshellarg($rcs->spoolPath($dir)));
00104     $barrel->compileTree();
00105     $barrel->readTree();
00106     return true;
00107   }
00108   
00109 
00115   function fromDb(&$barrel, $dir)
00116   {
00117     global $globals;
00118     $retval = '';
00119     
00120     $res = $globals->db->query("select * from {$barrel->table_page} where PID='$dir'");
00121     if ($props = mysql_fetch_assoc($res)) {
00122       $retval = new Diogenes_Barrel_Page($barrel, $props);
00123     }
00124     mysql_free_result($res);
00125     return $retval;
00126   }
00127 
00128     
00133   function getLocation($file = '')
00134   {
00135     $dirloc = $this->barrel->getLocation($this->props['PID']);
00136     $floc = (strlen($dirloc) ? "$dirloc/" : '') . $file;
00137     return $floc;
00138   }
00139   
00140   
00143   function make_actions()
00144   {
00145     global $globals;
00146     $props = $this->props;
00147     
00148     $actions = array(
00149       array( __("view"), "../". $this->getLocation(), "view" ),
00150       array( __("edit"), "edit?dir={$props['PID']}&amp;file=page.html", "edit"),      
00151       array( __("properties"), "pages?action=edit&amp;dir={$props['PID']}", "properties"),
00152       array( __("revisions"), "files?action=revs&amp;dir={$props['PID']}&amp;target={$globals->htmlfile}","revisions")
00153      );
00154      
00155     if ($this->barrel->flags->hasFlag('plug'))
00156     {
00157       array_push($actions, array( __("plugins"), "plugins?plug_page={$props['PID']}", "plugins"));
00158     }
00159     if ($props['location'] != '')
00160       array_push($actions, array( __("delete"), "javascript:page_delete('{$props['PID']}','{$props['location']}');","delete"));   
00161       
00162     return $globals->icons->get_action_icons($actions);
00163   }
00164   
00165   
00168   function make_toolbar(&$caller)
00169   {
00170     global $globals;
00171     $props = $this->props;
00172     
00173     $topbar = array ();
00174     $from = htmlentities($caller->script_uri());
00175     
00176     if ($props['PID']) {    
00177       $hp = $this->barrel->getPID('');
00178       array_push($topbar, array(__("home"), ($props['PID'] == $hp) ? "" : "files?dir=$hp"));
00179       array_push($topbar, array(__("parent page"), $props['parent'] ? "files?dir=".$props['parent'] : ""));
00180 
00181       array_push($topbar, array( __("browse files"), "files?dir={$props['PID']}" ));
00182       array_push($topbar, array( __("page properties"), "pages?dir={$props['PID']}" ));
00183       array_push($topbar, array( __("view page"), "../". $this->getLocation()));
00184       array_push($topbar, array(__("add a page"), "pages?action=edit&amp;parent=".$props['PID']."&amp;from=$from") );
00185       if ($this->barrel->flags->hasFlag("plug")) {
00186         array_push($topbar, array( __("plugins"), "plugins?plug_page={$props['PID']}" ) );
00187       }
00188     }
00189     
00190     return $topbar;
00191 
00192   }
00193   
00194   
00197   function make_doc_toolbar(&$rcs)
00198   {
00199     global $globals;
00200 
00201     if ($globals->word_import && file_exists($rcs->spoolPath($this->props['PID'],$globals->wordfile)) ) {
00202       $bfile = new Diogenes_Barrel_File($this, $globals->wordfile);
00203       $toolbar = $bfile->make_toolbar(0);
00204     } else {
00205       $bfile = new Diogenes_Barrel_File($this, $globals->htmlfile);
00206       $toolbar = $bfile->make_toolbar(1);
00207     }
00208     return $toolbar;
00209   }
00210 
00211 
00217   function toDb($homepage, &$caller)
00218   {
00219     global $globals;
00220     
00221     $props = $this->props;
00222     // check we are not creating a publicly writable page
00223     // on a barrel with PHP execution enabled!
00224     if ($props['PID'])
00225     {
00226       $cache = $globals->plugins->readCache($this->barrel->pluginsCacheFile, $this->barrel->alias);
00227       $plugs_active = $globals->plugins->cachedActive($cache, $this->barrel->alias, $props['PID']);
00228       foreach($plugs_active as $plugentry)
00229       {
00230         $plug_h = $globals->plugins->load($plugentry);
00231         if (!is_object($plug_h))
00232         {
00233           $caller->info("failed to load plugin '{$plugentry['plugin']}'");
00234           return;
00235         }
00236         if (!$plug_h->allow_wperms($props['wperms']))
00237         {
00238           $caller->info("plugin '{$plugentry['plugin']}' is not allowed with write permissions '{$props['wperms']}'!");
00239           return;
00240         }
00241       }
00242     }
00243     
00244     // check that the location is valid
00245     if ($homepage) 
00246     {          
00247       // homepage
00248       $props['location'] = '';    
00249       
00250     } else {
00251     
00252       // check the location is well formatted
00253       if (!preg_match("/^[a-zA-Z0-9_\-]*$/",$props['location']))
00254       {
00255         $caller->info(__("the page location cannot contain spaces or special characters"));
00256         return;
00257       } 
00258       
00259       // check this is not a forbidden location
00260       if (in_array($props['location'], $globals->invalidlocations))
00261       {
00262         $caller->info(__("this location cannot be used, it is reserved by Diogenes"));
00263         return;
00264       }
00265       
00266     }
00267     
00268     // this is a new entry, initialise
00269     if (!$props['PID']) {
00270       // new entry
00271       $globals->db->query("insert into {$this->barrel->table_page} set location='temp'");
00272       $props['PID'] = mysql_insert_id();
00273       $caller->info(__("Creating new page")." {$props['PID']}");
00274 
00275       $caller->log("page_create","{$this->barrel->alias}:{$props['PID']}");
00276         
00277       // initialise the page
00278       $rcs = $caller->getRcs();
00279       $rcs->newdir("",$props['PID']);
00280       $rcs->commit($props['PID'],$globals->htmlfile,"");
00281     } else {
00282       $caller->log("page_props","{$this->barrel->alias}:{$props['PID']}");    
00283     }
00284 
00285 
00286     // check we have a location    
00287     if (!$homepage and !strlen($props['location'])) 
00288     {
00289       $props['location'] = $props['PID'];  
00290     }
00291       
00292         
00293     // check that the filiation is valid
00294     if ($props['parent'])
00295     {
00296       // we need to insure that the parent is not already a child of the current page
00297       $parent = $props['parent'];
00298       while ($parent)
00299       {
00300         $oldparent = $parent;
00301         $res = $globals->db->query("select parent from {$this->barrel->table_page} where PID=$parent");
00302         list($parent) = mysql_fetch_row($res);
00303         mysql_free_result($res);
00304         if ($parent == $props['PID'])
00305         {
00306           $caller->info(__("A page cannot be its own parent (page $oldparent is a child of $parent)!"));
00307           break;      
00308         }        
00309       }
00310     }
00311     
00312 //    $caller->info("setting parent to {$props['parent']}");        
00313 
00314     // update data fields
00315     $sql = 
00316       "update {$this->barrel->table_page} set ".
00317         "parent='{$props['parent']}',".
00318         "location='{$props['location']}',".
00319         "title='{$props['title']}',".
00320         "perms='{$props['perms']}',".
00321         "wperms='{$props['wperms']}',".
00322         "status='{$props['status']}',".
00323         "template='{$props['template']}' ".
00324       "where PID='{$props['PID']}'";
00325     //$caller->info($sql);
00326     $globals->db->query($sql);
00327       
00328     // order by location
00329     $globals->db->query("alter table {$this->barrel->table_page} order by parent,location");
00330     
00331     // recompile tree
00332     $this->barrel->compileTree(); 
00333     $this->barrel->readTree();
00334 
00335     return $props['PID'];
00336   } 
00337 
00338 }
00339  
00340 ?>

Generated on Fri Jan 11 01:20:08 2008 for Diogenes by  doxygen 1.5.1