userEdit.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 SCRIPTS.'user_data.php';
00026 include_once FORM_PATH.'formWebObj.php';
00027 
00038 class userEdit extends formWebObj
00039 {
00040   var $user_data =  array( 
00041                           'username' => '',
00042                           'password' => '',
00043                           'name'     => '',
00044                           'email'    => '',
00045                           'books'    => ''
00046                           );
00047 
00048   function init()
00049   {
00050     $this->addSVar('mode', 'add');    // add | edit
00051   }
00052 
00054   function on_add($event_args)
00055   {
00056     //they are merged with $this->user_data in order
00057     //to be sure about the order of the fields
00058     $user_data = array_merge($this->user_data, $event_args);
00059 
00060     $books = $user_data['books'];
00061     $user_data['books'] = preg_replace('#\s+#', ',', $books);
00062 
00063     //check that such a username does not exist
00064     $username = $event_args['username'];
00065     $arr = get_user_data($username);
00066     if ($arr['username'] != '')
00067       {
00068         WebApp::message($arr['username']);
00069         $msg = T_("The username 'v_username' is already used for \n\
00070 somebody else.  Please choose another username.");
00071         $msg = str_replace('v_username', $username, $msg);
00072         WebApp::message($msg);
00073         $user_data['username'] = '';
00074         $this->user_data = $user_data;
00075         return;
00076       }
00077 
00078     //if the password is given, set the password of the new user
00079     $password = $user_data['password'];
00080     $password = trim($password);
00081     if ($password!='')
00082       {
00083         //encrypt the given password
00084         srand(time());
00085         $user_data['password'] = crypt($password, rand());
00086       }
00087 
00088     //add the user
00089     save_user_data($user_data);
00090 
00091     //set the new user as current user and change the mode to edit
00092     WebApp::setSVar('userList->currentUser', $username);
00093     $this->setSVar('mode', 'edit');
00094   }
00095 
00097   function on_delete($event_args)
00098   {
00099     $username = WebApp::getSVar('userList->currentUser');
00100     shell(SCRIPTS."users/del_user.sh $username");
00101 
00102     //the currentUser is deleted,
00103     //set current the first user in the list
00104     $userList = WebApp::getObject('userList');
00105     $userList->selectFirst();
00106 
00107     //acknowledgment message
00108     $msg = T_("User v_username deleted.");
00109     $msg = str_replace('v_username', $username, $msg);
00110     WebApp::message($msg);
00111   }
00112 
00114   function on_save($event_args)
00115   {
00116     //get old user data
00117     $username = WebApp::getSVar('userList->currentUser');
00118     $old_user_data = get_user_data($username);
00119 
00120     //they are merged with $this->user_data in order
00121     //to be sure about the order of the fields
00122     $event_args['username'] = $username;
00123     $new_user_data = array_merge($this->user_data, $event_args);
00124 
00125     //if the password is not given, keep the old password
00126     $new_password = $new_user_data['password'];
00127     $new_password = trim($new_password);
00128     if ($new_password=='')
00129       {
00130         //set the old password
00131         $new_user_data['password'] = $old_user_data['password'];
00132       }
00133     else
00134       {
00135         //encrypt the given password
00136         srand(time());
00137         $new_user_data['password'] = crypt($new_password, rand());
00138       }
00139 
00140     $books = $new_user_data['books'];
00141     $new_user_data['books'] = preg_replace('#\s+#', ',', $books);
00142 
00143     save_user_data($new_user_data);
00144   }
00145 
00146   function onParse()
00147   {
00148     //get the current user from the list of users
00149     $user = WebApp::getSVar('userList->currentUser');
00150 
00151     if ($user==UNDEFINED)
00152       {
00153         $this->setSVar('mode', 'add');
00154       }
00155     else
00156       {
00157         $this->setSVar('mode', 'edit');
00158       }
00159 
00160     WebApp::setSVar('edit_rights->user', $user);
00161   }
00162 
00163   function onRender()
00164   {
00165     $mode = $this->getSVar('mode');
00166     if ($mode=='add')
00167       {
00168         $user_data = $this->user_data; 
00169       }
00170     else
00171       {
00172         $username = WebApp::getSVar('userList->currentUser');
00173         $user_data = get_user_data($username);
00174       }
00175     $user_data['books'] = str_replace(',', "\n", $user_data['books'])."\n";
00176     WebApp::addVars($user_data);      
00177   }
00178 }
00179 ?>

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