docbook.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00025 include_once dirname(__FILE__).'/funcs.php';
00026 
00032 class docbook extends WebObject
00033 {
00034   function init()
00035   {
00036     $this->addSVar('book_id', UNDEFINED);
00037     $this->addSVar('node_path', './');
00038     $this->addSVar('languages', 'en,sq_AL');
00039     $this->addSVar('lng', 'en');
00040     $this->addSVar('mode', 'view');  //view | edit | approve | admin
00041   }
00042 
00043   function on_set_node($event_args)
00044   {
00045     $node_path = $event_args['node_path'];
00046     $this->setSVar('node_path', $node_path);
00047   }
00048 
00049   function on_set_node_id($event_args)
00050   {
00051     $id = $event_args['node_id'];
00052     $path = process_index('edit/get_node_path.xsl', array('id'=>$id));
00053     $this->setSVar('node_path', $path);
00054   }
00055 
00056   function on_set_mode($event_args)
00057   {
00058     $mode = $event_args['mode'];
00059     $access_vars = $this->get_button_vars();
00060     $access_vars['view'] = 'true';
00061     if ($access_vars[$mode] == 'true')
00062       {
00063         $this->setSVar('mode', $mode);
00064       }
00065     else
00066       {
00067         WebApp::message(T_("Don't have access."));
00068       }
00069   }
00070 
00071   function on_set_lng($event_args)
00072   {
00073     $lng = $event_args['lng'];
00074     $this->setSVar('lng', $lng);
00075 
00076     //set book_title
00077     $book_id = $this->getSVar('book_id');
00078     $book_title = main::get_book_title($book_id, $lng);
00079     WebApp::setSVar('book_title', $book_title);
00080   }
00081 
00082   function onParse()
00083   {
00084     $mode = $this->getSVar('mode');
00085     WebApp::addVar('node', "$mode/$mode.html");
00086  
00087     //add variables about access rights of the user
00088     $this->add_accessright_vars();
00089   }
00090 
00092   function add_accessright_vars()
00093   {
00094     WebApp::addGlobalVar('is_admin', $this->is_admin());
00095     list($can_edit, $can_approve) = $this->get_edit_rights();
00096     WebApp::addGlobalVar('can_edit', $can_edit);
00097     WebApp::addGlobalVar('can_approve', $can_approve);
00098   }
00099 
00104   function is_admin()
00105   {
00106     //superuser is considered to be admin of each book
00107     if (SU=='true')  return 'true';
00108 
00109     //get the books for which the user is admin
00110     $record = shell(SCRIPTS.'users/get_user.sh '.USER);
00111     $record = trim($record);
00112     $arr_fields = explode(':', $record);
00113     $book_list = $arr_fields[4];
00114     $arr_books = explode(',', $book_list);
00115 
00116     $book_id = $this->getSVar('book_id');
00117     $is_admin = (in_array($book_id, $arr_books));
00118     return ($is_admin ? 'true' : 'false');
00119   }
00120 
00121   function onRender()
00122   {
00123     //add navigation variables
00124     $node_path = $this->getSVar('node_path');
00125     $vars = get_arr_navigation($node_path);
00126     if ($vars['this_full_title']=='') 
00127       $vars['this_full_title'] = T_("Table Of Contents");
00128     if (trim($vars['this_full_title'])=='Info /') 
00129       $vars['this_full_title'] = T_("Info");
00130     WebApp::addVars($vars);
00131     WebApp::addVar('info_path', './INFO/');
00132     WebApp::addVar('toc_path', './');
00133 
00134     //add the variables {{edit}}, {{approve}} and {{admin}} which 
00135     //are used to display the buttons Edit, Approve and Admin
00136     WebApp::addVars($this->get_button_vars());
00137 
00138     //add state vars
00139     $arr_state = get_node_state();
00140     $locked = locked_by_somebody($arr_state);
00141     $str_locked = ($locked ? 'locked' : 'unlocked');
00142     WebApp::addVar('locked', $str_locked);
00143     WebApp::addVar('status', $arr_state['status']);
00144   }
00145 
00151   function get_button_vars()
00152   {
00153     $is_admin = WebApp::getVar('is_admin');
00154     if ($is_admin==UNDEFINED)
00155       {
00156         $this->add_accessright_vars();
00157         $is_admin = WebApp::getVar('is_admin');
00158       }
00159 
00160     if ( !defined('EDIT') )
00161       {
00162         //no buttons, if not in edit interface
00163         $edit = 'false';
00164         $approve = 'false';
00165         $admin = 'false';
00166       }
00167     else if ($is_admin=='true')
00168       {
00169         //both buttons, no restrictions
00170         //for the admins of the book
00171         $edit = 'true';
00172         $approve = 'true';
00173 
00174         //the admin button is used only in the root node
00175         $node_path = WebApp::getSVar('docbook->node_path');
00176         $admin = ($node_path=='./' ? 'true' : 'false');
00177       }
00178     else
00179       {
00180         //otherwise show the buttons 
00181         //according to the edit rights of the user
00182         $edit = WebApp::getVar('can_edit');
00183         $approve = WebApp::getVar('can_approve');
00184 
00185         //don't display the admin button for no admins
00186         $admin = 'false';
00187       }
00188 
00189     $vars = array('edit'=>$edit, 'approve'=>$approve, 'admin'=>$admin);
00190     return $vars;
00191   }
00192 
00201   function get_edit_rights()
00202   {
00203     //initiate them to false
00204     $edit = 'false';
00205     $approve = 'false';
00206 
00207     if (!defined('EDIT'))  return array($edit, $approve);
00208 
00209     //get the lines of the edit rights file of the user
00210     $book_id = $this->getSVar('book_id');
00211     $accr_file = ADMIN."access_rights/$book_id/".USER;
00212     $arr_lines = (file_exists($accr_file) ? file($accr_file) : array());
00213 
00214     //try to match the lines with the current node and language
00215     //and set the permissions accordingly
00216     for ($i=0; $i < sizeof($arr_lines); $i++)
00217       {
00218         $line = $arr_lines[$i];
00219         $line = trim($line);
00220         list($access,$levels,$nodes,$langs) = explode(':', $line);
00221 
00222         //if this line matches the current node and language 
00223         //then set the values of edit and approve according to it
00224         if ($this->node_match($nodes) and $this->lang_match($langs))
00225           {
00226             $value = ($access=='allow' ? 'true' : 'false');
00227             $arr_levels = explode(',', $levels);
00228             if (in_array('edit', $arr_levels))      $edit = $value;
00229             if (in_array('approve', $arr_levels))   $approve = $value;
00230           }
00231       }
00232 
00233     return array($edit, $approve);
00234   }
00235 
00243   function node_match($node_list)
00244   {
00245     if (strtoupper($node_list)=='ALL')  return true;
00246 
00247     $node_path = $this->getSVar('node_path');
00248     $arr_nodes = explode(',', $node_list);
00249     for ($i=0; $i < sizeof($arr_nodes); $i++)
00250       {
00251         $expr = $arr_nodes[$i];
00252         if (ereg('^'.$expr, $node_path)) return true;
00253       }
00254   }
00255 
00262   function lang_match($lang_list)
00263   {
00264     if (strtoupper($lang_list)=='ALL')  return true;
00265 
00266     $lng = $this->getSVar('lng');
00267     $arr_langs = explode(',', $lang_list);
00268     $match = in_array($lng, $arr_langs);
00269 
00270     return $match;
00271   }
00272 }
00273 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2