Code

461ed7f442a77f2a771e5b780195df689a0859f8
[gosa.git] / branches / old / gosa-plugins / openxchange / personal / connectivity / openxchange / class_oxchangeAccount.inc
1 <?php
2 /*
3   This code is part of GOsa (https://gosa.gonicus.de)
4   Copyright (C) 2005 Alejandro Escanero Blanco
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.
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.
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 */
21 class oxchangeAccount extends plugin
22 {
23   /* Definitions */
24   var $plHeadline= "Open-Xchange";
25   var $plDescription= "This does something";
27   /* GW attributes */
28   var $OXAppointmentDays="500";
29   var $OXTaskDays="5";
30   var $OXTimeZone="GMT";
31   var $mailEnabled="OK";
32   var $dnModeValue = "";
33   var $view_logged = FALSE;
35   /* attribute list for save action */
36   var $attributes= array("OXAppointmentDays", "OXTaskDays", "OXTimeZone","mailEnabled");
37   var $objectclasses= array("OXUserObject");
38   var $ReadOnly =false;
39   var $oxconf = array();
40   var $timezones=array();
41   var $dnmode= "";
42   var $uid ="";
44   function oxchangeAccount (&$config, $dn= NULL)
45   {
46     plugin::plugin ($config, $dn);
47     for ($i=0;$i<count($this->config->data['TABS']['CONNECTIVITY']);$i++){
48       if($this->config->data['TABS']['CONNECTIVITY'][$i]['CLASS']=='oxchangeAccount') {
49         $this->oxconf=$this->config->data['TABS']['CONNECTIVITY'][$i];
50         break;
51       }
52     }
54     $zones = timezone::_get_tz_zones();
55     $this->timezones= array_keys($zones['TIMEZONES']);
57     /* Setting uid to default */
58     if(isset($this->attrs['uid'][0])){
59       $this->uid = $this->attrs['uid'][0];
60     }
62     $this->dnmode= $this->config->get_cfg_value("dnmode");
63     if(isset($this->attrs[$this->dnmode][0])){
64       $this->dnModeValue = $this->attrs[$this->dnmode][0];
65     }
66   }
68   function execute()
69   {
70     /* Call parent execute */
71     //plugin::execute();
73     /* Log view */
74     if($this->is_account && !$this->view_logged){
75       $this->view_logged = TRUE;
76       new log("view","users/".get_class($this),$this->dn);
77     }
79     $display="";
81     /* Show main page */
82     $smarty= get_smarty();
84     if (function_exists("pg_connect")){
85       $smarty->assign("pg", true);
86     }else{
87       $smarty->assign("pg", false);
88     }
90     /* Load attributes */
91     foreach($this->attributes as $val){
92       $smarty->assign("$val", $this->$val);
93     }
94     if ($this->is_account){
95       $smarty->assign("oxchangeState", "checked");
96       $smarty->assign("oxState", "");
97     } else {
98       $smarty->assign("oxchangeState", "");
99       $smarty->assign("oxState", "disabled");
100     }
102     $tmp = $this->plInfo();
103     foreach($tmp['plProvidedAcls'] as $key => $desc){
104       $smarty->assign($key."ACL",$this->getacl($key,$this->ReadOnly));
105       $smarty->assign($key."_W",$this->acl_is_writeable($key));
106     }
108     if((!$this->ReadOnly) && (($this->is_account && $this->acl_is_removeable()) || (!$this->is_account && $this->acl_is_createable())) ){
109       $smarty->assign('oxchangeAccountACL', "");
110     }else{
111       $smarty->assign('oxchangeAccountACL', " disabled ");
112     }
114     $smarty->assign("timezones", $this->timezones);
117     if ($this->parent !== NULL){
118       $smarty->assign("tabbed", 1);
119     }else{
120       $smarty->assign("tabbed", 0);
121         }
123         /* Trying to open a Postgresql Database Server */
124         if (function_exists("pg_connect")){
125                 if(isset($this->oxconf["PGUSER"]) and isset($this->oxconf["PGHOST"]) and isset($this->oxconf["PGDBNAME"]) and isset($this->oxconf["PGPASSWD"])){
126                         $pgcon =  @pg_connect("host=".$this->oxconf["PGHOST"]." user=".$this->oxconf["PGUSER"]." password=".$this->oxconf["PGPASSWD"]." dbname=".$this->oxconf["PGDBNAME"]);
127                         if (! $pgcon){
128                                 $smarty->assign("pg", false);
129                         }
130                 }else{
131                         $smarty->assign("pg", false);
132                 }
133         }else{
134                 $smarty->assign("pg", false);
135         }
137     $display.= $smarty->fetch (get_template_path('oxchange.tpl', TRUE, dirname(__FILE__)));
138     return ($display);
139   }
141   function remove_from_parent()
142   {
143     if(!$this->acl_is_removeable())  return;
144     $this->attrs[$this->dnmode][0] = $this->dnModeValue;
145     $sqldeluser=array(
146         "delete from prg_notes where (user_right like '".$this->attrs[$this->dnmode][0]."') and (group_right like 's')",
147         "delete from prg_documents_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
148         "delete from prg_documents_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
149         "delete from prg_docufolders_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
150         "delete from prg_docufolders_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
151         "delete from prg_date_rights where (user_right like '".$this->attrs[$this->dnmode][0]."')",
152         "delete from prg_date_notification where (member_uid like '".$this->attrs[$this->dnmode][0]."')",
153         "delete from prg_dates_members where (member_uid like '".$this->attrs[$this->dnmode][0]."')",
154         "delete from prg_knowledge_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
155         "delete from prg_knowledge_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
156         "delete from prg_knowledge_folder_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
157         "delete from prg_knowledge_folder_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
158         "delete from prg_pin_board_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
159         "delete from prg_pin_board_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
160         "delete from prg_bookmarks_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
161         "delete from prg_bookmarks_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
162         "delete from prg_bookmarks_folder_read where (user_right like '".$this->attrs[$this->dnmode][0]."')",
163         "delete from prg_bookmarks_folder_mod where (user_right like '".$this->attrs[$this->dnmode][0]."')",
164         "delete from prg_tasks_rights where (user_right like '".$this->attrs[$this->dnmode][0]."')",
165         "delete from prg_tasks_notification where (member_uid like '".$this->attrs[$this->dnmode][0]."')",
166         "delete from prg_tasks_members where (member_uid like '".$this->attrs[$this->dnmode][0]."')",
167         "delete from prg_projects_rights where (user_right like '".$this->attrs[$this->dnmode][0]."')",
168         "delete from prg_projects_notification where (member_uid like '".$this->attrs[$this->dnmode][0]."')",
169         "delete from prg_projects_members where (member_uid like '".$this->attrs[$this->dnmode][0]."')",
170         "delete from oxfolder_permissions where (entity like '".$this->attrs[$this->dnmode][0]."') AND ((role = 256) OR (role = 1024))",
171         "delete from oxfolder_standardfolders where owner like '".$this->attrs[$this->dnmode][0]."'",
172         "delete from prg_forum_read where user_right = '".$this->attrs[$this->dnmode][0]."'",
173         "delete from prg_forum_mod where user_right = '".$this->attrs[$this->dnmode][0]."'",
174         "delete from prg_forum_abo where user_right = '".$this->attrs[$this->dnmode][0]."'",
175         "delete from prg_forum_seen where username = '".$this->attrs[$this->dnmode][0]."'",
176         "delete from sys_holiday where (userid like '".$this->attrs[$this->dnmode][0]."')",
177         "delete from usr_holiday where (userid like '".$this->attrs[$this->dnmode][0]."')");
179     /* Trying to open a Postgresql Database Server */
180     if (function_exists("pg_connect")){
181       if(isset($this->oxconf["PGUSER"]) and 
182                  isset($this->oxconf["PGHOST"]) and 
183              isset($this->oxconf["PGDBNAME"]) and 
184          isset($this->oxconf["PGPASSWD"])){
185         $pgcon = @pg_connect("host=".$this->oxconf["PGHOST"]." user=".$this->oxconf["PGUSER"]." password=".$this->oxconf["PGPASSWD"]." dbname=".$this->oxconf["PGDBNAME"]);
186         if (! $pgcon){
187           msg_dialog::display(_("Error"), msgPool::dbconnect(_("Open-Xchange"),@pg_last_error($pgcon)), ERROR_DIALOG);
188           return;
189         }
190       }else{
191         msg_dialog::display(_("Error"), msgPool::noserver(_("Open-Xchange database")), ERROR_DIALOG);
192         return;
193       }
194     }else{
195       msg_dialog::display(_("Configuration error"),msgPool::missingext("postgresql"), ERROR_DIALOG);
196       return;
197     }
199     plugin::remove_from_parent();
200     $ldap= $this->config->get_ldap_link();
202     if($ldap->dn_exists("ou=addr,".$this->dn)){
203         $ldap->rmdir_recursive("ou=addr,".$this->dn);
204       if (!$ldap->success()){
205         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
206       }
207     }
209     $ldap->cd($this->dn);
210     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
211         $this->attributes, "Save");
212     $this->cleanup();
213     $ldap->modify ($this->attrs); 
215     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
217     if (!$ldap->success()){
218       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
219     }
221     /* Optionally execute a command after we're done */
222     $this->postremove(array("uid" => $this->uid));
224     /* Finally remove data from postgresql server */
225     foreach ($sqldeluser as $sqls){
226        @pg_exec($pgcon,$sqls);
227     }
228     pg_close();
229   }
231   /* Save data to object */
232   function save_object()
233   {
234     /* Do we need to flip is_account state? */
235     if (isset($_POST['connectivityTab'])){
236       if (isset($_POST['oxchange'])){
237         if (!$this->is_account && $_POST['oxchange'] == "B") {
238           if($this->acl_is_createable()){
239             $this->is_account= TRUE;
240           }
241         }
242       } else {
243         if($this->acl_is_removeable()){
244           $this->is_account= FALSE;
245         }
246       }
247     }
249     /* Get objects */
250     foreach(array("OXTimeZone","OXTaskDays","OXAppointmentDays") as $name) {
251       if($this->acl_is_writeable($name) && isset($_POST[$name])){
252         $this->$name = $_POST[$name];
253       } 
254     }
255   
256     if (isset($_POST["oxchangeStatus"])){
257       $this->oxchangeStatus = "disabled";
258     } else {
259       $this->oxchangeStatus = "enabled";
260     }
261   }
264   /* Save to LDAP */
265   function save()
266   {
268     $needupdate=TRUE;
269     $istemplate=FALSE;
271     /*First at all, we must check if this is new or is updated */
272     /*Also check is we have a template, if is a template, is a new user */
273     if (isset($this->attrs['objectClass'])){
274       foreach ($this->attrs['objectClass'] as $object){
275         if($object=="OXUserObject") $needupdate=FALSE;
276         if($object=="gosaUserTemplate") $istemplate=TRUE;
277       }
278     }
280     $uidarray=array();
281     preg_match("/^(\w+(?=\=))=((\w|\s|\.)+(?=\,)),.*/",$this->dn,$uidarray);
282     $uid=$uidarray[2];
283     if (trim($uid) == "") {
284       msg_dialog::display(_("Error"), msgPool::invalid(_("Open-Xchange account name")), ERROR_DIALOG);
285       return;
286     }
287         
288     if ($istemplate) $needupdate=TRUE;
290     if($needupdate){
291       /* Trying to open a Postgresql Database Server */
292       if (function_exists("pg_connect")){
293         if(isset($this->oxconf["PGUSER"]) and isset($this->oxconf["PGHOST"]) and isset($this->oxconf["PGDBNAME"]) and isset($this->oxconf["PGPASSWD"])){
294           $pgcon =  @pg_connect("host=".$this->oxconf["PGHOST"]." user=".$this->oxconf["PGUSER"]." password=".$this->oxconf["PGPASSWD"]." dbname=".$this->oxconf["PGDBNAME"]);
295           if (! $pgcon){
296             msg_dialog::display(_("Error"), msgPool::dbconnect(_("Open-Xchange"),@pg_last_error($pgcon)), ERROR_DIALOG);
297             return;
298           }
299         }else{
300           msg_dialog::display(_("Error"), msgPool::noserver(_("Open-Xchange database")), ERROR_DIALOG);
301           return;
302         }
303       }else{
304         msg_dialog::display(_("Configuration error"),msgPool::missingext("postgresql"), ERROR_DIALOG);
305         return;
306       }
307     }
309     plugin::save();
311     /* Write back to ldap */
312     $ldap= $this->config->get_ldap_link();
313     $ldap->cd($this->dn);
314     $this->cleanup();
315     $ldap->modify ($this->attrs); 
317     if($this->initially_was_account){
318       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
319     }else{
320       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
321     }
323     if (!$ldap->success()){
324       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
325     }
327     /* Optionally execute a command after we're done */
328     $this->postcreate(array("uid" => $this->uid));
330     if($needupdate){
331       $ldap->create_missing_trees("ou=addr,".$this->dn);
332       if (!$ldap->success()){
333         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
334       }
336       /* Finally save data to postgresql server */
337       pg_set_client_encoding ("UNICODE");
338       $nv = "SELECT nextval ('serial_id')";
339       $ot = "insert into oxfolder_tree (fuid, parent, fname, module, type, owner, creator, creating_date, created_from, changing_date, changed_from) VALUES (%d, 1, '%s', '%s', 'private', '%s', '%s', 'now', 'System', 'now', 'System')";
340       $op = "INSERT INTO oxfolder_permissions (puid, pid, role, entity, sealed, fp, orp, owp, odp) VALUES (%d, %d, 32768,  '%s', 0, 128, 128, 128, 128)";
341       $os = "insert into oxfolder_standardfolders (owner,module_calendar,module_contact,module_task) VALUES ('%s',%d,%d,%d)";
342       $ugr= "INSERT INTO usr_general_rights SELECT creating_date, created_from, changing_date, changed_from,text('%s'),  addr_u, addr_r, addr_d, cont_u, cont_r, cont_d, data_u, data_r, data_d, serie_u, serie_r, serie_d, task_u, task_r, task_d,  refer, proj_u, proj_r, proj_d, dfolder_u, dfolder_r, dfolder_d, doc_u, doc_r, doc_d, knowl_u, knowl_r, knowl_d, bfolder_u,  bfolder_r, bfolder_d, bookm_u, bookm_r, bookm_d, pin_u, pin_r, pin_d, forum_n, fentrie_n, setup, pin_public, internal,  int_groups, kfolder_u, kfolder_r, kfolder_d, webmail FROM sys_gen_rights_template WHERE login LIKE 'default_template'";
344       $error = FALSE;
345       $res=@pg_exec($pgcon,$nv); 
347       if(!$res){
348         $error = TRUE;
349       }else{
350         $calendarid=pg_fetch_row($res); 
351         pg_freeresult($res);
353         $q=sprintf($ot,$calendarid[0],'My Appointments','calendar',$uid,$uid);
354         @pg_exec($pgcon,$q);
355       }
357       $res=@pg_exec($pgcon,$nv); 
359       if(!$res){
360         $error = TRUE;
361       }else{
362         $nid=pg_fetch_row($res); 
363         pg_freeresult($res);
365         $q=sprintf($op,$nid[0],$calendarid[0],$uid);
366         @pg_exec($pgcon,$q);
367       }
369       $res=@pg_exec($pgcon,$nv); 
370       if(!$res){
371         $error = TRUE;
372       }else{
373         $contactsid=pg_fetch_row($res); 
374         pg_freeresult($res);
376         $q=sprintf($ot,$contactsid[0],'My Contacts','contact',$uid,$uid);
377         @pg_exec($pgcon,$q);
378       }
380       $res=@pg_exec($pgcon,$nv); 
381       if(!$res){
382         $error = TRUE;
383       }else{
384         $nid=pg_fetch_row($res); 
385         pg_freeresult($res);
387         $q=sprintf($op,$nid[0],$contactsid[0],$uid);
388         @pg_exec($pgcon,$q);
389       }
391       $res=@pg_exec($pgcon,$nv); 
392       if(!$res){
393         $error = TRUE;
394       }else{
395         $tasksid=pg_fetch_row($res); 
396         pg_freeresult($res);
398         $q=sprintf($ot,$tasksid[0],'My Tasks','task',$uid,$uid);
399         @pg_exec($pgcon,$q);
400       }
402       $res=@pg_exec($pgcon,$nv); 
403       if(!$res){
404         $error = TRUE;
405       }else{
406         $nid=pg_fetch_row($res); 
407         pg_freeresult($res);
409         $q=sprintf($op,$nid[0],$tasksid[0],$uid);
410         @pg_exec($pgcon,$q);
411       
412         $q=sprintf($os,$uid,$calendarid[0],$contactsid[0],$tasksid[0]);
413         @pg_exec($pgcon,$q);
414       
415         $q=sprintf($ugr,$uid);
416         @pg_exec($pgcon,$q);
417       }
418     
419       @pg_close($pgcon);
421       if($error){
422         msg_dialog::display(_("Error"), _("Cannot save Open-Xchange account!"), ERROR_DIALOG);
423       }
424     }
425   }
428   /* Return plugin informations for acl handling */
429   static function plInfo()
430   {
431     return (array(
432           "plShortName"     => _("Open-Xchange"),
433           "plDescription"   => _("Open-Xchange account settings")."&nbsp;:&nbsp;<u>"._("Connectivity addon")."</u>",
434           "plSelfModify"    => TRUE,
435           "plDepends"       => array("user"),
436           "plPriority"      => 27,                                 // Position in tabs
437           "plSection"     => array("personal" => _("My account")),
438           "plCategory"    => array("users"),
439           "plOptions"       => array(),
441           "plProvidedAcls"  => array(
442             "OXAppointmentDays" => _("OXAppointmentDays"),
443             "OXTaskDays"            => _("OXTaskDays"),
444             "OXTimeZone"            => _("OXTimeZone"))
445           ));
446   }
448 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
449 ?>