Code

64a156586ba323a4dab5eb4d539d52e201555297
[gosa.git] / gosa-plugins / gofon / gofon / phoneaccount / class_phoneAccount.inc
1 <?php
3 class phoneAccount extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Phone";
7   var $plDescription= "This does something";
9   /* Attributes */
10   var $telephoneNumber        = array();
11   var $goFonHardware          = "automatic";
12   var $goFonFormat            = "wav";
13   var $goFonPIN               = "";
14   var $goFonVoicemailPIN      = "";
15   var $goFonDeliveryMode      = "";
16   var $phoneNumbers           = array();
17   var $mail                   = "";
18   var $hardware_list          = array();
19   var $used_hardware          = array();
20   var $goFonMacro             = "";
21   var $macro                  = 0;              // Selected Macroi
22   var $lastmacro              = "";
23   var $macros                 = array();        // List of macros for smarty select box
24   var $macroarray             = array();        // All needed macro informations
25   var $macrostillavailable    = false;
26   var $generate_error         = "";
27   var $a_old_telenums         = array();
28   var $goFonPINVoice          = "";
29   var $goFonHomeServer        = "0";            // Contains the dn of the server that manage this account 
30   var $init_HomeServer        = "0";            // Contains the dn of the server that manage this account 
31   var $goFonHomeServers       = array();        // Contains all available server configurations 
33   var $context                = "default";
34   var $voice_context          = "default";
36   /* attribute list for save action */
37   var $CopyPasteVars          = array("phoneNumbers","macroarray","macrostillavailable"/*"phoneNumbers" -Reset- */,
38                                       "hardware_list","used_hardware");
40   var $attributes             = array("goFonDeliveryMode", "goFonFormat","goFonHomeServer",
41       "goFonHardware","goFonPIN","goFonVoicemailPIN","telephoneNumber", "goFonMacro","macro");
42   var $objectclasses= array("goFonAccount");
44   var $uid;
45   var $cn;
47   var $view_logged = FALSE;
48   var $multiple_support = TRUE;
49   var $mailAddress      = "";
50   var $has_mailAccount  = FALSE;
51   var $pager      = "";
53   function phoneAccount (&$config, $dn= NULL, $parent= NULL)
54   {
55     plugin::plugin ($config, $dn, $parent);
57     /* Assemble phone numbers */
58     if (isset($this->attrs['telephoneNumber'])){
59       for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
60         $number= $this->attrs['telephoneNumber'][$i];
61         $this->phoneNumbers[$number]= $number;
62       }
63     }
65     /* Set parent object to tab object */
66     if(is_object($parent)){
67       $this->parent = $parent->parent;
68     }
70     /* Get current uid and cn 
71      */
72     if(isset($this->attrs['uid'][0])){
73       $this->uid = $this->attrs['uid'][0];
74     }
75     if(isset($this->attrs['cn'][0])){
76       $this->cn = $this->attrs['cn'][0];
77     }
78     if(isset($this->attrs['mail'][0])){
79       $this->mailAddress = $this->attrs['mail'][0];
80     }
81     if(isset($this->attrs['pager'][0])){
82       $this->pager = $this->attrs['pager'][0];
83     }
85     /* If there is a parent object present, use references 
86      */
87     if(isset($this->parent->by_object['user']->uid)){
88       $this->uid = &$this->parent->by_object['user']->uid;
89     }
90     if(isset($this->parent->by_object['user']->cn)){
91       $this->cn   =&$this->parent->by_object['user']->cn;
92     }
93     if(isset($this->parent->by_object['user']->pager)){
94       $this->pager   =&$this->parent->by_object['user']->pager;
95     }
96     if(isset($this->parent->by_object['mailAccount']->mail)){
97       $this->mailAddress      = &$this->parent->by_object['mailAccount']->mail;
98       $this->has_mailAccount  = &$this->parent->by_object['mailAccount']->is_account;
99     }
101     /* Check server configurations 
102      * Load all server configuration in $this->goFonHomeServers if available
103      *  and first server as default if necessary.
104      * Check if connection is successfull for the selected server $this->goFonHomeServer
105      */
107   
108     /* Set available server */
109     $config = session::get('config');
110     if(isset($config->data['SERVERS']['FON'])){
111       $this->goFonHomeServers = $config->data['SERVERS']['FON'];
112     }
114     $a_SETUP= array();
115     if($this->is_account && isset($config->data['SERVERS']['FON']) &&
116        array_key_exists('FON',$config->data['SERVERS']) &&
117        is_callable("mysql_connect")
118        ) {
120       /* Servers defined? Watch here... */
121       if (count($this->goFonHomeServers)){
123         /* Set default server */
124         if(empty($this->goFonHomeServer) || $this->goFonHomeServer == "0"){
125           $this->goFonHomeServer= $this->goFonHomeServers[0]['DN'];
126         }
128         /* Remember inital home server, to be able to remove old entries */
129         $this->init_HomeServer = $this->goFonHomeServer;
131         /* Get config */
132         if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
133           msg_dialog::display(_("Obsolete entry"), sprintf(_("The current home server is not available anymore. It will be moved to '%s' if you save this entry!"), preg_replace("/,/",", ",$this->goFonHomeServers[0]['DN'])), ERROR_DIALOG);
135           $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
136           $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
137         }    
138         $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
140         $r_con =  @mysql_pconnect($cur_cfg['SERVER'],$cur_cfg['LOGIN'],$cur_cfg['PASSWORD']);
141         if(!$r_con){
142           new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error());
143         }
144         $db  =  @mysql_select_db($cur_cfg['DB'],$r_con);
145         if(!$db){
146           new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error());
147         }
149         $first = false; 
150         foreach($this->phoneNumbers as $key => $val){
151           if(!$first){
152             $first = $key;
153           }
154         }
155       }
156     }
158     /* Get available phone hardware  
159      * Search for all available phone hardware  
160      */
161     $tmp = get_sub_list("(objectClass=goFonHardware)","phone",array(get_ou("phoneou")),
162                   $this->config->current['BASE'],array("cn","description"), GL_SUBSEARCH);
163     foreach($tmp as $attrs){
164       $cn= $attrs['cn'][0];
165       $description= "";
166       if (isset($attrs['description'])){
167         $description= " - ".$attrs['description'][0];
168       }
169       $this->hardware_list[$cn]= "$cn$description";
170     }
171     $this->hardware_list["automatic"]= _("automatic");
172     ksort($this->hardware_list);
175      /* Collect all usd phones 
176         goFonHardware set.
177      */
178     $deps_a = array(
179         get_people_ou(),
180         get_ou("ogroupou"),
181         get_ou("serverou"),
182         get_ou("terminalou"),
183         get_ou("workstationou"),
184         get_ou("printerou"),
185         get_ou("componentou"),
186         get_ou("phoneou"));
188     $tmp = get_sub_list("(goFonHardware=*)","phone",$deps_a,$this->config->current['BASE'],
189         array('cn','dn','goFonHardware'),GL_SUBSEARCH);
190     foreach($tmp as $attrs){
191       $cn = $attrs['goFonHardware'][0];
192       if(isset($this->hardware_list[$cn])){
193         $this->used_hardware[$cn]= $cn;
194       }
195     }
198     /* Get available Macros  
199      * Search for all Macros that are visible and create 
200      *  an array with name and parameters 
201      */
202     $tmp = get_sub_list("(&(objectClass=goFonMacro)(goFonMacroVisible=1))","gofonmacro",array(get_ou("macroou")),
203                   $this->config->current['BASE'],array("displayName","goFonMacroParameter","dn","cn"), GL_NO_ACL_CHECK | GL_SUBSEARCH );
204     
206     /* Add none for no macro*/
207     $this->macros['none']=_("no macro");    
208     $this->macro ="none";
210     /* Fetch all Macros*/
211     foreach($tmp as $attrs){
213       $ui = get_userinfo(); 
214       $acl = $ui->get_permissions($attrs['dn'],"gofonmacro/macro","");
216       /* Skip all macros we are not able to read 
217           execpt, the currently selected macro.
218        */
219       if(!preg_match("/r/",$acl) && !preg_match("/^".normalizePreg($attrs['dn'])."/",$this->goFonMacro)){
220         continue;
221       }
223       /* unset Count, we don't need that here */
224       unset($attrs['displayName']['count']);
226       /* Parse macro data, unset count for parameterarrays  */
227       if (isset($attrs['goFonMacroParameter']['count'])){
228         unset($attrs['goFonMacroParameter']['count']);
229       }
231       /* fill Selectfield variable with Macros */
232       if(isset($attrs['displayName'][0])){
233         $this->macros[$attrs['dn']] = $attrs['displayName'][0]." (".$attrs['cn'][0].")";
234       }else{
235         $this->macros[$attrs['dn']] = _("undefined");
236       }
238       /* Go through available parameters and parse all attributes, like parametername, type, default ...*/
239       if((isset($attrs['goFonMacroParameter']))&&(is_array($attrs['goFonMacroParameter']))){
241         foreach($attrs['goFonMacroParameter'] as $pkey=>$pval){
242           /* Split Data in readable values, by delimiter !  */
243           $data = split("!",$attrs['goFonMacroParameter'][$pkey]);
245           /* Set all attrs */
246           $id = $data[0];
247           $this->macroarray[$attrs['dn']][$id]['var']    ="var".$id;
248           $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3]; 
249           $this->macroarray[$attrs['dn']][$id]['id']     =$id;
250           $this->macroarray[$attrs['dn']][$id]['name']   =$data[1];
251           $this->macroarray[$attrs['dn']][$id]['type']   =$data[2];
252           $this->macroarray[$attrs['dn']][$id]['default']=$data[3];
253           if($data[2] == "bool"){
254             $this->macroarray[$attrs['dn']][$id]['choosen']=$data[3];
255           }
256         }//foreach
257       }//is_array
258     }//while
261     /* Parse used Macro  
262      * If we have a macro selected, parse it and set values 
263      *  in $this->macroarray[$this->macro]. 
264      */
265     $tmp = split("!",$this->goFonMacro);
266     if(is_array($tmp)){
268       /* First value is the macroname */
269       $this->macro = $tmp[0];
271       /* Macroname saved, delete that index */
272       unset($tmp[0]);
274       /* Check if makro has been removed */
275       if(!isset($this->macros[$this->macro])){
276         $this->macrostillavailable = false;
277       }else{
278         $this->macrostillavailable = true;
279       }
281       /* for each parametervalues ( parameterID#value like 25#twentyfive) */
282       foreach($tmp as $var){
284         /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
285         $varar = split("#",$var);
287         /* Only insert if the parameter still exists */
288         if(isset($this->macroarray[$this->macro][$varar[0]])){
289           /* Assign value */
290           $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
291         }
292       }
293     }
296     $this->a_old_telenums = $this->phoneNumbers;
298     /* Get voicemail PIN from MySQL DB 
299      * Because every user can change his PIN directly from the phone
300      *  without any update to the ldap
301      * This means, the PIN in the DB is up to date
302      */
303     // Connect to DB server
304     if( (is_callable("mysql_pconnect"))&&
305         (isset($cur_cfg))&&
306         (isset($cur_cfg['SERVER']))&&
307         (isset($cur_cfg['LOGIN']))&&
308         (isset($cur_cfg['PASSWORD']))){
310       $r_con =  @mysql_pconnect($cur_cfg['SERVER'],$cur_cfg['LOGIN'],$cur_cfg['PASSWORD']);
311       if($r_con){
312         $r_db  =  @mysql_select_db($cur_cfg['DB'],$r_con);
313   
314         $query_tmp = "SELECT ".$cur_cfg['VOICE_TABLE'].".context as 'v_context', 
315                              ".$cur_cfg['SIP_TABLE'].".context, 
316                              ".$cur_cfg['VOICE_TABLE'].".password 
317                        FROM  ".$cur_cfg['VOICE_TABLE'].", 
318                              ".$cur_cfg['SIP_TABLE']." 
319                        WHERE customer_id = sip_users.mailbox AND name='".$this->uid."'";
322         $res = mysql_query($query_tmp);
323         $vp  = mysql_fetch_assoc($res);
324   
325         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query_tmp, "Database query");
326         if((isset($vp['password']))&&(!empty($vp['password']))){
327           $this->goFonPINVoice = $vp['password'];
328         }
329         if((isset($vp['context']))&&(!empty($vp['context']))){
330           $this->context = $vp['context'];
331         }
332         if((isset($vp['v_context']))&&(!empty($vp['v_context']))){
333           $this->voice_context = $vp['v_context'];
334         }
335       }
336     }
337     $this->lastmacro=$this->macro;
339     if(is_callable("mysql_close")&&(isset($r_con))&&($r_con)){
340       @mysql_close($r_con) ;
341     }
342   }
345   /* This function generates the Database entries. 
346    * The Parameter 'save' could be true or false.
347    *  false - means only testing no database transactions.
348    *  true  - write database entries.
349    *
350    * 'sip_users','voice_mail' and 'extensions' table entries will be created.
351    * 
352    * If the phone hardware is 'automatic' the table entries will only be removed
353    *  and not added. 
354    */
355   function generate_mysql_entension_entries($save = false)
356   {
357     /* Check if there is at least one server available 
358      * If not, return and tell the user that saving failed 
359      */
360     if(!count($this->goFonHomeServers)){
361       if($save){
362         msg_dialog::display(_("Configuration error"), msgPool::noserver(_("GOfon")), WARNING_DIALOG);
363       }
364       return(true);
365     }
367     /* Check if Mysql extension is available */
368     if(!is_callable("mysql_pconnect")){
369       if($save){
370         msg_dialog::display(_("Configuration error"), msgPool::missingext("php-mysql"), WARNING_DIALOG);
371       }
372       return(true);
373     }
374  
375     /********************** 
376      * Attribute Initialisation
377      **********************/
379     $old_connection = false;
381     // Get Configuration for Mysql database Server
382     $s_parameter    = "";                                           // Contains paramter for selected Macro 
383     $r_con          = false;                                        // DB connection
384     $r_db           = false;                                        // Selected DB
385     $r_res          = false;                                        // Result resource
386     $a_ldap_attrs   = array();                                      //  
388     $s_ip           = NULL;                   // Contains ip for Sip entry
389     $s_host         = NULL;                   // Contains host for Sip entry
390     $s_qualify      = "yes";                  // Qualify entry
391     $s_pin          = NULL;                   // Entry for secret
392     $s_type         = NULL;                   // Entry for phone type (friend , peer ..)
394     $sip_data_array = array();                // Contains complete sip entry, to generate SQL syntax
395     $i_old_key      = false;                  // Contains index for first old phonenumber, to delete old entries corectly
396     $i_new_key      = false;                  // Contains index for first new phonenumber, to generate new  entries corectly
398     $s_sip_values   = "";     // Contains string with all values for given attributes in SQL syntax
399     $s_sip_keys     = "";     // Contains all needed attributes to generate sip entry in DB
401     $s_sip_key      = "";     // Key for SIP entry index      
402     $s_sip_val      = "";     // Value for SIP entry index      
404     $b_first_deleted= false;  // Only delete first entry, 
405     $s_telenums     = "";     // for each value variable
407     $i_is_accounted = false;  // Ensure that extension entry, for name to number is only once in table
409     /* Prepare some basic attributes */
410     $oldnums = array();
411     foreach($this->a_old_telenums as $tele){
412       $oldnums[]= preg_replace("/[^0-9]/","",$tele);
413     }
414     foreach($this->phoneNumbers as $tele){
415       $newnums[]= preg_replace("/[^0-9]/","",$tele);
416     }
418     if(empty($this->uid)) trigger_error("Uid is empty.");
421     /* Create voicemail entry 
422      */
423     if((!isset($this->cn))||(empty($this->cn))){
424       $CNname= $this->uid;
425     }else{
426       $CNname= $this->cn;
427     }
429     $s_mail = "";
430     if($this->has_mailAccount){
431       $s_mail = $this->mailAddress;;
432     }
434     /* Get phonehardware to setup sip entry  */
435     $ldap         = $this->config->get_ldap_link();
436     $r_res        = $ldap->search("(&(objectClass=goFonHardware)(cn=".$this->goFonHardware."))", array('*'));
437     $a_ldap_attrs = $ldap->fetch();
439     /* Check selected phone hardware, is a default IP set? */
440     if(((isset($a_ldap_attrs['goFonDefaultIP'][0]))&&($a_ldap_attrs['goFonDefaultIP'][0] != "dynamic"))){
441       $s_ip       = $a_ldap_attrs['goFonDefaultIP'][0];
442       $s_host     = $s_ip;
443     }else{
444       $s_ip       = NULL;
445       $s_host     = "dynamic";
446     }
448     // Attribute GoFonQualify set ?
449     if(isset($a_ldap_attrs['goFonQualify'])){
450       $s_qualify = $a_ldap_attrs['goFonQualify'][0];
451     }
453     // Attribute GoFonPIN set ?
454     if(isset($this->goFonPIN)){
455       $s_pin      = $this->goFonPIN;
456     }
458     // Attribute GoFonType set ?
459     if(isset($a_ldap_attrs['goFonType'])){
460       $s_type = $a_ldap_attrs['goFonType'][0];
461     }
463     if(isset($a_ldap_attrs['goFonDmtfMode'][0])){
464       $sip_data_array['dtmfmode']     = $a_ldap_attrs['goFonDmtfMode'][0];
465     }else{
466       $sip_data_array['dtmfmode']     ="rfc2833";
467     }
469     /* Check if phone number is used */
470     if($this->is_number_used()){
471       $this->generate_error = $this->is_number_used(); 
472       return false;
473     }
477     /********************** 
478      * Check Server Connection Information
479      **********************/
480  
481     /* Create Mysql handle for the current goFonHomeServer, if possible  
482      * Get configuration to old asterisk home server 
483      */ 
484     $a_New = $this->goFonHomeServers[$this->goFonHomeServer];  // DB Configuration
485     $new_connection =  @mysql_pconnect($a_New['SERVER'],$a_New['LOGIN'],$a_New['PASSWORD']);
486     if(!$new_connection){
487       $this->generate_error =  msgPool::dbconnect($a_New['SERVER'],@mysql_error($new_connection),
488           _("Abort saving entries to keep the database consistent."));
489       new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error($new_connection));
490       return false;
491     }
492     $new_database  =  @mysql_select_db($a_New['DB'],$new_connection);
493     if(!$new_database){
494       $this->generate_error =  msgPool::dbselect($a_New['DB'],@mysql_error($new_connection),
495           _("Abort saving entries to keep the database consistent."));
496       new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error($new_connection));
497       return false;
498     }
500     /* If the home server has changed, we must remove entries from old 
501      *  server and add new entries in new server.  
502      */
503     if($this->init_HomeServer != $this->goFonHomeServer){
504     
505       /* Get configuration to old asterisk home server */ 
506       $a_Remove = $this->goFonHomeServers[$this->init_HomeServer];  // DB Configuration
507  
508       /* Create connection to the database that contains the old entry. 
509        */
510       $old_connection =  @mysql_pconnect($a_Remove['SERVER'],$a_Remove['LOGIN'],$a_Remove['PASSWORD']);
511       if(!$old_connection){
512         $this->generate_error =  msgPool::dbconnect($a_Remove['SERVER'],@mysql_error($old_connection),
513             _("Abort saving entries to keep the database consistent."));
514         new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error($old_connection));
515         return false;
516       }
517       $old_database  =  @mysql_select_db($a_Remove['DB'],$old_connection);
518       if(!$old_database){
519         $this->generate_error =  msgPool::dbselect($a_Remove['DB'],@mysql_error($old_connection),
520             _("Abort saving entries to keep the database consistent."));
521         new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error($old_connection));
522         return false;
523       }
524     }
526     /* Save means that we must save changes, not only test  */
527     if($save == true){
528     
529       /********************** 
530        * Remove entries from old home server 
531        **********************/
533       /* Check if there is an old entry 
534        * If there is an old entry, get callerid and remove voicemail and extensions 
535        */
536       if($old_connection){
537         $query  = "SELECT id,name,callerid FROM ".$a_Remove['SIP_TABLE']." WHERE name='".$this->uid."';";
538         $rid    = mysql_query($query,$old_connection);
539         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
541         /* Old entry found, remove it */
542         $query_a = array();
543         if(mysql_affected_rows($old_connection)){
544           $result = mysql_fetch_assoc($rid);
545           $query_a[]= "DELETE FROM ".$a_Remove['SIP_TABLE']." WHERE name='".$this->uid."';";
546           $query_a[]= "DELETE FROM ".$a_Remove['VOICE_TABLE']." WHERE customer_id='".$result['callerid']."';";
547           $query_a[]= "DELETE FROM ".$a_Remove['EXT_TABLE']." WHERE exten='".$this->uid."';";
548           foreach($oldnums as $s_telenums) {
549             $query_a[]= "DELETE FROM ".$a_Remove['EXT_TABLE']." WHERE exten='".$s_telenums."';";
550           }
552           foreach($query_a as $qry){
553                   @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$qry, "Database query");
554             if(!mysql_query($qry,$old_connection)){
555               trigger_error(mysql_error($old_connection));
556             } 
557           }
558         }
559       }
561       /********************** 
562        * Update / Insert sip_users entry  
563        **********************/
565       /* Set the first given phone number as callerid */
566       reset($newnums);        
567       $i_new_key = key($newnums);
568       $sip_data_array['callerid']  =$newnums[$i_new_key];
569       $sip_data_array['mailbox']   =$newnums[$i_new_key];
571       /* Check if there is already an entry in sip_users for this uid */
572       $SQL_query_array = array();
573       $query = "SELECT * FROM ".$a_New['SIP_TABLE']." WHERE name='".$this->uid."';\n"; 
574       $rid = mysql_query($query,$new_connection);
575       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
576       if(mysql_affected_rows($new_connection)){
578         /********************** 
579          * Update sip_users entry 
580          **********************/
581         $result                     = mysql_fetch_assoc($rid);
582         $sip_data_array['host']         = $s_host;
583         $sip_data_array['qualify']      = $s_qualify;
584         $sip_data_array['secret']       = $this->goFonPIN;
585         $sip_data_array['type']         = $s_type ;
586         $sip_data_array['username']     = $this->uid;
587         $sip_data_array['ipaddr']       = $s_ip;
588         $sip_data_array['context']      = $this->context;
590         /* Remove not changed attributes, to avoid updating table with same values */
591         foreach($sip_data_array as $name => $value){
592           if($result[$name] == $value){
593             unset($sip_data_array[$name]);
594           }
595         }
596         /* Only update entry if there is something to uopdate */
597         if(count($sip_data_array)){
598           $query = "UPDATE ".$a_New['SIP_TABLE']." SET ";
599           foreach($sip_data_array as $key => $val){
600             $query.= "".$key."='".$val."',"; 
601           } 
602           $query = preg_replace("/,$/","",$query);
603           $query.= " WHERE name='".$this->uid."';";
604           $SQL_query_array[] = $query;
605         }
606       } else {
607  
608         /********************** 
609          * Insert sip_users entry 
610          **********************/
611         //generate SIP entry
612         $sip_data_array['id']           = "";
613         $sip_data_array['name']         = $this->uid;
614         $sip_data_array['accountcode']  = NULL;          
615         $sip_data_array['amaflags']     = NULL;
616         $sip_data_array['callgroup']    = NULL;
617         $sip_data_array['canreinvite']  = "no";
618         $sip_data_array['context']      = $this->context;
619         $sip_data_array['defaultip']    = NULL;
620         $sip_data_array['fromuser']     = NULL;
621         $sip_data_array['fromdomain']   = NULL;
622         $sip_data_array['host']         = $s_host;
623         $sip_data_array['insecure']     = NULL;
624         $sip_data_array['language']     = NULL;
625         $sip_data_array['mailbox']      = $newnums[$i_new_key];
626         $sip_data_array['md5secret']    = NULL;
627         $sip_data_array['nat']          = "no";
628         $sip_data_array['permit']       = NULL;
629         $sip_data_array['deny']         = NULL;
630         $sip_data_array['mask']         = NULL;
631         $sip_data_array['pickupgroup']  = NULL;
632         $sip_data_array['port']         = NULL;
633         $sip_data_array['qualify']      = $s_qualify;
634         $sip_data_array['restrictcid']  = "n";
635         $sip_data_array['rtptimeout']   = NULL;
636         $sip_data_array['rtpholdtimeout']=NULL;
637         $sip_data_array['secret']       = $this->goFonPIN;
638         $sip_data_array['type']         = $s_type ;
639         $sip_data_array['username']     = $this->uid;
640         $sip_data_array['disallow']     = NULL;
641         $sip_data_array['allow']        = NULL;
642         $sip_data_array['musiconhold']  = NULL;
643         $sip_data_array['regseconds']   = NULL;
644         $sip_data_array['ipaddr']       = $s_ip;
645         $sip_data_array['regexten']     = NULL;
646         $sip_data_array['cancallforward']=NULL;
648         /* There is currently no entry for this user in the sip_users table. 
649          * We should create one i
650          */
651         foreach($sip_data_array as $s_sip_key=>$s_sip_val){
652           if($s_sip_val === NULL) continue;
653           $s_sip_values.="'".$s_sip_val."',";
654           $s_sip_keys  .="`".$s_sip_key."`,";
655         }
656         $s_sip_values =  preg_replace("/,$/","",$s_sip_values);
657         $s_sip_keys   =  preg_replace("/,$/","",$s_sip_keys);
659         /* Add sip entries to mysql queries */
660         $SQL_query_array[] ="INSERT INTO ".$a_New['SIP_TABLE']." (".$s_sip_keys.") VALUES (".$s_sip_values.");";
661       }
664       /********************** 
665        * Update / Insert Voice mail entry  
666        **********************/
668       $customer_id = $newnums[$i_new_key];
670       $query  = "SELECT id,name,callerid FROM ".$a_New['SIP_TABLE']." WHERE name='".$this->uid."';";
672       $rid    = mysql_query($query,$new_connection);
673       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
674       $result = mysql_fetch_assoc($rid);
675   
676       $old_customer_id = ""; 
677       if($result){
678         $old_customer_id = $result['callerid'];
679       }
681       $voice_data_array = array(
682           "customer_id" => $customer_id,
683           "mailbox"     => $customer_id,
684           "password"    => $this->goFonVoicemailPIN,
685           "fullname"    => $CNname,
686           "context"     => $this->voice_context,
687           "email"       => $s_mail);
689       $voice_data_array['pager']   = $this->pager;
691       /* Check if there is already an entry in sip_users for this uid */
692       $query_tmp = "SELECT * FROM ".$a_New['VOICE_TABLE']." WHERE customer_id='".$old_customer_id."';\n";
695       $rid = mysql_query($query_tmp,$new_connection);
696       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query_tmp, "Database query");
697       if(mysql_affected_rows($new_connection)){
699         /********************** 
700          * Update Voice mail entry  
701          **********************/
703         $result = mysql_fetch_assoc($rid)  ;
705         foreach($voice_data_array as $name => $value){
706           if($result[$name] == $value){
707             unset($voice_data_array[$name]);
708           }
709         }
711         /* Only update entry if there is something to update */
712         if(count($voice_data_array)){
713           $query = "UPDATE ".$a_New['VOICE_TABLE']." SET ";
714           foreach($voice_data_array as $key => $val){
715             $query.= "".$key."='".$val."',"; 
716           } 
717           $query = preg_replace("/,$/","",$query);
718           $query.= " WHERE customer_id='".$old_customer_id."';";
719           $SQL_query_array[] = $query;
720         }
721       }else{
723         /********************** 
724          * Insert Voice mail entry  
725          **********************/
726         $voice_data_array['context'] = $this->voice_context;
727   
728         /* There is currently no voice mail entry for this user. 
729          * We should create one 
730          */
731         $s_voi_values = $s_voi_keys = "";
732         foreach($voice_data_array as $s_voi_key=>$s_voi_val){
733           if($s_voi_val === NULL) continue;
734           $s_voi_values.="'".$s_voi_val."',";
735           $s_voi_keys  .="`".$s_voi_key."`,";
736         }
737         $s_voi_values =  preg_replace("/,$/","",$s_voi_values);
738         $s_voi_keys   =  preg_replace("/,$/","",$s_voi_keys);
740         /* Add sip entries to mysql queries */
741         $SQL_query_array[] ="INSERT INTO ".$a_New['VOICE_TABLE']." (".$s_voi_keys.") VALUES (".$s_voi_values.");";
742       }
744      
745       /********************** 
746        * Remove/Insert extension entries
747        **********************/
748       
749       /* Remove old entries */
750       $query = array();
751       $query[]= "DELETE FROM ".$a_New['EXT_TABLE']." WHERE exten=\"".$this->uid."\";";
752       $oldnums= array();
753       foreach($oldnums as $s_telenums){
754         $query[]= "DELETE FROM ".$a_New['EXT_TABLE']." WHERE exten=\"".$s_telenums."\";";
755       }
756       foreach($newnums as $s_telenums){
757         $query[]= "DELETE FROM ".$a_New['EXT_TABLE']." WHERE exten=\"".$s_telenums."\";";
758       }
759       foreach($query as $qry){
760         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$qry, "Database query");
761         if(!mysql_query($qry,$new_connection)){
762           trigger_error(mysql_error($new_connection));
763         } 
764       }
765  
766       /********************** 
767        * Insert extension entries
768        **********************/
769  
770       // Get selected Macro Parameter and create parameter entry 
771       if(isset($this->macroarray[$this->macro])){
772         foreach($this->macroarray[$this->macro] as $key => $val ){
773           $s_parameter .= $val['choosen']."|";
774         }
775         $s_parameter = preg_replace("/\|$/","",$s_parameter);
776       }
777      
778       $i = 0; 
779       $EXT = array();
780       if(!is_numeric($this->uid)){
781         $EXT[$i]['context'] = 'GOsa';
782         $EXT[$i]['exten']   = $this->uid;
783         $EXT[$i]['priority']= 1;
784         $EXT[$i]['app']     = "Goto";
785         $EXT[$i]['appdata'] = $newnums[$i_new_key]."|1";
786         $i ++;
787       }
789       // Entension entries  Hint / Dial / Goto
790       foreach($newnums as $s_telenums){
792         /* Hint Entry */
793         $EXT[$i]['context'] = 'GOsa';
794         $EXT[$i]['exten']   = $s_telenums;
795         $EXT[$i]['priority']= "Hint";
796         $EXT[$i]['app']     = 'SIP/'.$this->uid;
797         $i ++;  
798         /* SetCID */
799         //$EXT[$i]['context'] = 'GOsa';
800         //$EXT[$i]['exten']   = $s_telenums;
801         //$EXT[$i]['priority']= 1;
802         //$EXT[$i]['app']     = "SetCIDName";
803         //$EXT[$i]['appdata'] = $CNname;
804         //$i ++;  
806         // If no macro is selected use Dial
807         if($this->macro!="none"){ 
808           $macroname = preg_replace("/,.*$/","",$this->macro);        
809           $macroname = preg_replace("/^.*=/","",$macroname);        
810           $s_app = "Macro";$macroname;
811           $s_par = $macroname."|".$s_parameter; 
812         }else{
813           $s_app = "Dial";
814           $s_par = 'SIP/'.$this->uid."|20|r";
815         }
817         $EXT[$i]['context'] = 'GOsa';
818         $EXT[$i]['exten']   = $s_telenums;
819         $EXT[$i]['priority']= 1;
820         $EXT[$i]['app']     = $s_app;
821         $EXT[$i]['appdata'] = $s_par;
822         $i ++;
823       }
825       // Append all these Entries 
826       foreach($EXT as $entr){
827         $SQL_syn = "INSERT INTO ".$a_New['EXT_TABLE']." (";
828         foreach($entr as $key2 => $val2){
829           $SQL_syn.= "`".$key2."`,";
830         }
831         $SQL_syn = preg_replace("/,$/","",$SQL_syn);
832         $SQL_syn .= ") VALUES ("; 
833         foreach($entr as $key2 => $val2){
834           $SQL_syn .= "'".$val2."',";
835         }
836         $SQL_syn = preg_replace("/,$/","",$SQL_syn);
837         $SQL_syn .=");\n";
839         $SQL_query_array[] =$SQL_syn;
840         $SQL_syn ="";
841       }
843       // Perform queries ...
844       if($this->goFonHardware != "automatic"){
845         foreach($SQL_query_array as $query){
846           @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
847           if(!@mysql_query($query,$new_connection)){
848             $this->generate_error =  msgPool::dbquery("GOfon",@mysql_error($old_connection));
849             return false;
850           }
851         }
852       }
853     }
854     @mysql_close($new_connection);
855     return true;
856   }
859   function execute()
860   {
861     /* Call parent execute */
862     plugin::execute();
864     /* Log view */
865     if($this->is_account && !$this->view_logged){
866       $this->view_logged = TRUE;
867       new log("view","users/".get_class($this),$this->dn);
868     }
870     $display = "";
871     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
873     if(empty($this->macro)&&(!empty($this->goFonMacro))){
875       /* Go through already saved values, for a parameter */
876       $tmp = split("!",$this->goFonMacro);
878       /* it is possible that nothing has been saved yet */
879       if(is_array($tmp)){
881         /* First value is the macroname */
882         $this->macro = $tmp[0];
884         /* Macroname saved, delete that index */
885         unset($tmp[0]);
887         /* Check if macro has been removed */
888         if(!isset($this->macroarray[$this->macro])){
889           $this->macrostillavailable = false;
890         }else{
891           $this->macrostillavailable = true;
892         }
894         /* for each parametervalues ( parameterID#value like 25#twentyfive) */
895         foreach($tmp as $var){
897           /* Split this, so we have $varar[0] = parameterID $varar[1] = SelectedValue */
898           $varar = split("#",$var);
900           /* Only insert if the parameter still exists */
901           if(isset($this->macroarray[$this->macro][$varar[0]])){
902             /* Assign value */
903             $this->macroarray[$this->macro][$varar[0]]['choosen']=$varar[1];
904           }
905         }
906       }
907     }
908     
909     /* Do we represent a valid account? */
910     if (!$this->is_account && $this->parent === NULL){
911       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
912         msgPool::noValidExtension(_("Phone"))."</b>";
913       $display.= back_to_main();
914       return ($display);
915     }
917     /* Do we need to flip is_account state? */
918     if (isset($_POST['modify_state'])){
919       $this->is_account= !$this->is_account;
920     }
922     /* Do we represent a valid account? */
923     if (!$this->is_account && $this->parent === NULL){
924       $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
925         msgPool::noValidExtension(_("Phone"))."</b>";
926       $display.= back_to_main();
927       return($display);
928     }
930     $display= "";
932     /* Show tab dialog headers */
933     
934     if (!$this->multiple_support_active && $this->parent !== NULL){
935       if ($this->is_account){
936         $display= $this->show_disable_header(_("Remove phone account"),
937             msgPool::featuresEnabled(_("Phone")));
938       } else {
939         if(empty($this->uid)){
940           $display= $this->show_enable_header(_("Create phone account"),
941             msgPool::featuresDisabled(_("Phone"),_("User uid")));
942         }else{
943           $display= $this->show_enable_header(_("Create phone account"),
944             msgPool::featuresDisabled(_("Phone")));
945         }
946         return ($display);
947       }
948     }
949     /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
950     if(empty($this->macro)){
951       $this->macro ="none";
952     }
954     /* Prepare templating */
955     $smarty= get_smarty();
957     /* tell user that the selected plugin is no longer available */
958     if((!$this->macrostillavailable)&&($this->macro!="none")){
959       msg_dialog::display(_("Error"), _("Selected macro is not available anymore!"), ERROR_DIALOG);
960     }
962     /* Assing macroselectbox values  */
963     $smarty->assign("macros",$this->macros);   
964     $smarty->assign("macro", $this->macro);   
966     /* Assign contexts */
967     $smarty->assign("contexts",$this->get_asterisk_contexts());
968     $smarty->assign("context" ,$this->context);
969     $smarty->assign("voice_context" ,$this->voice_context);
971     /* check if there is a FON server created */
972     if(!count($this->goFonHomeServer)){
973       msg_dialog::display(_("Configuration error"), msgPool::noserver(_("GOfon")), WARNING_DIALOG);
974     }
976     /* Create html parameter table for selected macro parameters 
977      *  skip if no parameters given 
978      */
979     if(!isset($this->macroarray[$this->macro])){
980       $macrotab="";
981     }else{
983       $macrotab ="<table summary=\""._("Parameter")."\">";
984       /* for every single parameter-> display textfile,combo, or true false switch*/
986       foreach($this->phoneNumbers as $phonenum){
987         $tmp[] = $phonenum;
988       }
989     
990       if($this->macro != $this->lastmacro){
991         /* Go through all params */
992         foreach($this->macroarray[$this->macro] as $key => $paras){
994           $string = $paras['default'];
996           $string=preg_replace("/%uid/i",$this->uid,$string);
998           if(isset($this->cn)){
999             $string=preg_replace("/%cn/i",$this->cn,$string);
1000           }
1002           for($i = 0 ; $i < 10; $i++){
1003             if(isset($tmp[$i])){
1004               $string = preg_replace("/%telephoneNumber_".($i+1)."/i",$tmp[$i],$string);
1005             }
1006           }
1007           if(isset($tmp[0])){
1008             $string = preg_replace("/%telephoneNumber/i",$tmp[0],$string);
1009           }
1010           $this->macroarray[$this->macro][$key]['choosen']=$string;
1011         }
1012       }
1014       foreach($this->macroarray[$this->macro] as $paras){
1016         /* get al vars */
1017         $var        = $paras['var'];           
1018         $name       = $paras['name'];           
1019         $default    = $paras['default'];
1020         $type       = $paras['type'];
1021         $choosen    = $paras['choosen'] ; 
1022         $str        = $default;
1024         $dis = "";
1025         if(!$this->acl_is_writeable("goFonMacro",$SkipWrite)){
1026           $dis = " disabled ";
1027         }
1029         /* in case of a combo box display a combobox with selected attr */
1030         $macrotab.= "<tr>";
1031         switch ($type){
1033           case "combo":
1034             $str= "<select name='".$var."' ".$dis." >";
1035           foreach(split(":",$default) as $choice){
1036             if($choosen==$choice){
1037               $str.= "\n<option value='".$choice."' selected>".$choice."&nbsp;</option>";
1038             }else{
1039               $str.= "\n<option value='".$choice."'>".$choice."&nbsp;</option>";
1040             }
1041           }
1042           $str.="</select>";
1043           $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
1044           break;
1046           case "bool":
1047             if(!$choosen){
1048               $str="\n<input type='checkbox' name='".$var."' value='1' ".$dis." >";
1049             }else{
1050               $str="\n<input type='checkbox' name='".$var."' value='1' checked  ".$dis.">";
1051             }
1052           $macrotab.= "<td colspan='2'>$str&nbsp;".base64_decode($name)."";
1053           break;
1055           case "string":
1056             $str="<input name='".$var."' value='".$choosen."' ".$dis." style='width:340px;'>";
1057           $macrotab.= "<td>".base64_decode($name)."</td><td>$str";
1058           break;
1060         }
1061         $macrotab.= "</td></tr>";
1063       }
1064       $macrotab.="</table><input name='post_success' type='hidden' value='1'>";
1065     }//is_array()
1067     /* Give smarty the table */
1068     $smarty->assign("macrotab",$macrotab);
1071     /* Add phone number */
1072     if (isset($_POST["add_phonenumber"]) && $_POST['phonenumber']){
1073       if (tests::is_phone_nr($_POST['phonenumber'])){
1074         $number= $_POST["phonenumber"];
1075         $this->phoneNumbers[$number]= $number;
1076         $this->is_modified= TRUE;
1077       } else {
1078         msg_dialog::display(_("Error"), msgPool::invalid("Phone number"), ERROR_DIALOG);
1079       }
1080     }
1082     /* Remove phone number */
1083     if (isset($_POST["delete_phonenumber"]) && isset($_POST["phonenumber_list"])){
1084       foreach ($_POST['phonenumber_list'] as $number){
1085         unset($this->phoneNumbers[$number]);
1086         $this->is_modified= TRUE;
1087       }
1088     }
1090     /* Assign acls */
1091     $tmp = $this->plInfo();
1092     foreach($tmp['plProvidedAcls'] as $name => $translation){
1093       $smarty->assign($name."ACL",$this->getacl($name,$SkipWrite));
1094     }
1096     /* Transfer ACL's */
1097     foreach($this->attributes as $val){
1098       if(isset($this->$val)){
1099         $smarty->assign($val,$this->$val);
1100       }else{
1101         $smarty->assign($val,"");
1102       }
1103     }
1105     /* Create home server array */
1106     $tmp = array();
1107     foreach($this->goFonHomeServers as $dn => $attrs){
1108       if(!is_numeric($dn)){
1109         $tmp[$dn] = $attrs['SERVER'];
1110       }
1111     }
1112     $smarty->assign("goFonHomeServers",$tmp);
1114     /* Fill arrays */
1115     $smarty->assign ("goFonHardware", $this->goFonHardware);
1116     if (!count($this->phoneNumbers)){
1117       $smarty->assign ("phoneNumbers", array());
1118     } else {
1119       $smarty->assign ("phoneNumbers", $this->phoneNumbers);
1120     }
1122     $dis = "";
1123     if(!$this->acl_is_writeable("goFonHardware",$SkipWrite)){
1124       $dis= " disabled ";
1125     }
1126     $hl= "<select size=\"1\" name=\"goFonHardware\" ".$dis." title=\"".
1127        _("Choose your private phone")."\">\n";
1129     foreach ($this->hardware_list as $cn => $description){
1130       if ($cn == $this->goFonHardware){
1131         $selected= "selected";
1132       } else {
1133         $selected= "";
1134       }
1135       if (isset($this->used_hardware[$cn])){
1136         $color= "style=\"color:#A0A0A0\"";
1137       } else {
1138         $color= "";
1139       }
1140       $hl.= "  <option $color label=\"$cn\" value=\"$cn\" $selected>$description&nbsp;</option>\n";
1141     }
1142     $hl.= "</select>\n";
1143     $smarty->assign ("hardware_list", $hl);
1146     foreach($this->attributes as $attr){
1147       if(in_array($attr,$this->multi_boxes)){
1148         $smarty->assign("use_".$attr,TRUE);
1149       }else{
1150         $smarty->assign("use_".$attr,FALSE);
1151       }
1152     }
1154     foreach(array("goFonVoiceMailContext","goFonContext") as $attr){
1155       if(in_array($attr,$this->multi_boxes)){
1156         $smarty->assign("use_".$attr,TRUE);
1157       }else{
1158         $smarty->assign("use_".$attr,FALSE);
1159       }
1160     }
1162     /* Show main page */
1163     $this->lastmacro = $this->macro;
1164     $smarty->assign("multiple_support",$this->multiple_support_active);
1165     $display.= $smarty->fetch(get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
1166     return($display);
1167   }
1170   function save_object()
1171   {
1172     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
1173     if (isset($_POST["phoneTab"])){
1174     
1175       plugin::save_object();
1177       /* Save checkbox */
1178       $tmp = preg_replace("/[^a-z]/i","",$this->goFonDeliveryMode);
1179       if($this->acl_is_writeable("goFonDeliveryMode",$SkipWrite)){
1180         if(isset($_POST['fon_to_mail']) && !preg_match("/M/",$this->goFonDeliveryMode)){
1181           $tmp .= "M";
1182         }elseif(!isset($_POST['fon_to_mail']) && preg_match("/M/",$this->goFonDeliveryMode)){
1183           $tmp  = preg_replace ("/M/","",$tmp);
1184         }
1185       }
1186       $this->goFonDeliveryMode= "[".$tmp."]";
1189       /* Every macro in the select box are available */
1190       if((isset($_POST['macro']))){
1191         $this->macro = $_POST['macro'];
1192         $this->macrostillavailable=true;
1193       }
1195       if(isset($_POST['macro']) && $_POST['macro'] != $this->macro){
1196         $this->is_modified =true;
1197       }
1199       /* Save context */
1200       if(isset($_POST['context'])){
1201         if($this->context != $_POST['context']){
1202           $this->is_modified= TRUE;
1203         }
1204         $this->context= $_POST['context'];
1205       }
1207       /* Save voice context */
1208       if(isset($_POST['voice_context'])){
1209         if($this->voice_context != $_POST['voice_context']){
1210           $this->is_modified= TRUE;
1211         }
1212         $this->voice_context= $_POST['voice_context'];
1213       }
1215       if(is_array($this->phoneNumbers)){
1216         foreach($this->phoneNumbers as $telenumms) {
1217           $nummsinorder[]=$telenumms; 
1218         }
1219       }else{
1220         $nummsinorder=array("");
1221       }
1224       /* get all Postvars */
1225       if(isset($this->macroarray[$this->macro])){
1228         if($this->acl_is_writeable("goFonMacro",$SkipWrite)){
1229           foreach($this->macroarray[$this->macro] as $key => $paras){
1231             $old_macro_settings = $this->macroarray[$this->macro][$key]; 
1232             $backup = $this->macroarray[$this->macro][$key];
1234             if(isset($_POST[$paras['var']])){
1235               $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
1236             }
1238             /* Checkboxes are special, they are not Posted if they are not selected, so the won't be changed with the above code
1239                We need this code below to read and save checkboxes correct
1240              */
1242             if(isset($_POST['post_success'])){
1243               if($this->macroarray[$this->macro][$key]['type']=="bool"){
1244                 if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
1245                   $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
1246                 }else{
1247                   $this->macroarray[$this->macro][$key]['choosen']=false;
1248                 }
1249               }
1250             }
1251             if(array_differs($old_macro_settings,$this->macroarray[$this->macro][$key])){
1252               $this->is_modified = TRUE;
1253             }
1254           }
1256           if(count(array_diff($this->macroarray[$this->macro][$key],$backup))){
1257             $this->is_modified = TRUE;
1258           }
1259         }
1260       }
1261     }
1262   }
1264   function check()
1265   {
1266     /* Call common method to give check the hook */
1267     $message= plugin::check();
1269     if(!count($this->goFonHomeServers)){
1270       $message[] = _("There must be at least one server with an asterisk database to create a phone account.");
1271     }
1273     if(empty($this->goFonHomeServer)){
1274       $message[] = msgPool::invalid(_("Home server"));
1275     }
1277     if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
1278       $message[]= msgPool::invalid(_("Voicemail PIN"),"","",_("Between 1-4 charactes"));
1279     }else{
1280       if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN)){
1281         $message[]= msgPool::invalid(_("Voicemail PIN"),preg_replace("/[0-9]/","X",$this->goFonVoicemailPIN),"/X/");
1282       }
1283     }
1285     if(preg_match("/[^0-9a-z]/i",$this->goFonPIN)){
1286       $message[]= msgPool::invalid(_("Phone PIN"),preg_replace("/[0-9a-z]/i","X",$this->goFonPIN),"/X/");
1287     }
1289     if ($this->initially_was_account != $this->is_account || $this->is_modified){
1290       if(!$this->generate_mysql_entension_entries()){
1291         $message[] = $this->generate_error;
1292       }
1293     }
1295     /* We need at least one phone number */
1296     if (count($this->phoneNumbers) == 0){
1297       $message[]= msgPool::required("Phone number");
1298     }
1300     /* check for ! in any parameter setting*/
1301     if(isset($this->macroarray[$this->macro])){
1302       foreach($this->macroarray[$this->macro] as $val){
1303         if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
1304           $message[] = msgPool::invalid(sprintf(_("macro parameter %s"),$val['name']),$val['choosen'],"/[^\#]/");
1305         }
1306       }
1307     }
1308     return ($message);
1309   }
1313   function save()
1314   {
1315     plugin::save();
1317     /* Force saving macro again 
1318      * This ensures that 
1319      *  - the macro is available on the destiantion server.
1320      *  - the macro saved is up to date on the destination server.
1321      */
1322     if(!empty($this->macro) && $this->macro != "none")  {
1323       $macro_tab= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->macro,"gofonmacro");
1324       $macro_tab -> save();
1325     }
1327     /* Save arrays */
1328     $tmp_numbers = array();
1329     foreach ($this->phoneNumbers as $number){
1330       $tmp_numbers[] = $number;
1331     }
1333     /* Save settings, or remove goFonMacro attribute*/
1334     if($this->macro!="none"){    
1335       $this->attrs['goFonMacro']=$this->macro;
1336       if(isset($this->macroarray[$this->macro])){
1337         foreach($this->macroarray[$this->macro] as $paras)  {
1338           $this->attrs['goFonMacro'].="!".$paras['id']."#".$paras['choosen'];
1339         }
1340       }
1341     }else{
1342       $this->attrs['goFonMacro']=array();
1343     }
1344     unset($this->attrs['macro'])  ;
1346     $this->attrs['goFonForwarding']=array();
1348     if ($this->initially_was_account != $this->is_account || $this->is_modified){
1349       $str = $this->generate_mysql_entension_entries(true);
1350       if(empty($str)){
1351         msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
1352       }
1353     }
1355     if($this->attrs['goFonMacro']==""){
1356       $this->attrs['goFonMacro']=array();
1357     }
1359     unset($this->attrs['cn']);
1361     /* Write back to ldap */
1362     $ldap= $this->config->get_ldap_link();
1363     $ldap->cd($this->dn);
1364     $this->cleanup();
1365     
1366     /* Force saving numbers, else it will be overwriten by user account. */
1367     $this->attrs['telephoneNumber'] =$tmp_numbers;
1368     $ldap->modify ($this->attrs); 
1370     /* Log last action */
1371     if($this->initially_was_account){
1372       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1373     }else{
1374       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1375     }
1377     if (!$ldap->success()){
1378       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
1379     }
1381     /* Optionally execute a command after we're done */
1383     if ($this->initially_was_account == $this->is_account){
1384       if ($this->is_modified){
1385         $this->handle_post_events("modify",array("uid" => $this->uid));
1386       }
1387     } else {
1388       $this->handle_post_events("add",array("uid" => $this->uid));
1389     }
1391   }
1394   function adapt_from_template($dn, $skip= array())
1395   {
1396     plugin::adapt_from_template($dn, $skip);
1398     /* Assemble phone numbers */
1399     if (isset($this->attrs['telephoneNumber']) && !in_array("telephoneNumber", $skip)){
1400       for ($i= 0; $i<$this->attrs['telephoneNumber']['count']; $i++){
1401         $number= $this->attrs['telephoneNumber'][$i];
1402         $this->phoneNumbers[$number]= $number;
1403       }
1404     }
1405   }
1408   function remove_from_parent()
1409   {
1410     if(!$this->initially_was_account) return;
1412     if(count($this->goFonHomeServers) && !empty($this->init_HomeServer) && is_callable("mysql_pconnect")){
1414       // Get Configuration for initial Mysql database Server
1415       $a_SETUP = $this->goFonHomeServers[$this->init_HomeServer];
1416       $s_parameter  ="";
1418       // Connect to DB server
1419       $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
1421       // Check if we are  connected correctly
1422       if(!$r_con){
1423         msg_dialog::display(_("Error"), msgPool::dbconnect("GOfon",@mysql_error()), ERROR_DIALOG);
1424         new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error());
1425         return false;
1426       }
1428       // Select database for Extensions
1429       $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
1431       // Test if we have the database selected correctly
1432       if(!$db){
1433         msg_dialog::display(_("Error"), msgPool::dbselect("GOfon", @mysql_error()), ERROR_DIALOG);
1434         new log("debug","gofonreport/".get_class($this),"",array(),@mysql_error());
1435         return false;
1436       }
1438       $SQL="";
1441       $first_num = false;
1442       // Delete old entries
1443       foreach($this->a_old_telenums as $s_telenums){
1444         if(!$first_num){
1445           $first_num = $s_telenums;
1446         }
1447         $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$s_telenums."';\n";
1448       }
1451       $query  = "SELECT id,name,callerid FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';";
1452       $rid    = mysql_query($query,$r_con);
1453       @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
1454       $result = mysql_fetch_assoc($rid);
1455       $callerid = $first_num;
1456       if($result){
1457         $callerid = $result['callerid'];
1458       }
1460       $SQL[] = "DELETE FROM ".$a_SETUP['VOICE_TABLE']." WHERE customer_id='".$callerid."';";
1461       $SQL[] = "DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE exten='".$this->uid."';\n";
1462       $SQL[] = "DELETE FROM ".$a_SETUP['SIP_TABLE']." WHERE name='".$this->uid."';\n";
1464       foreach($SQL as $query){
1465         @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
1466         if(!@mysql_query($query,$r_con)){
1467           msg_dialog::display(_("Error"), msgPool::dbquery("GOfon",@mysql_error()), ERROR_DIALOG);
1468           return false;
1469         }
1470       }
1471     }else{
1472       msg_dialog::display(_("Configuration error"), msgPool::missingext("php-mysql"), WARNING_DIALOG);
1473       return false;
1474     }
1476     /* unset macro attr, it will cause an error */
1477     $tmp = array_flip($this->attributes);
1478     unset($tmp['macro']);
1479     $this->attributes=array_flip($tmp);
1481     /* Cancel if there's nothing to do here */
1482     if (!$this->initially_was_account){
1483       return;
1484     }
1486     plugin::remove_from_parent();
1488     /* Just keep one phone number */
1489     if (count($this->telephoneNumber) && $this->telephoneNumber != ""){
1490       $this->attrs['telephoneNumber']= $this->telephoneNumber;
1491     } else {
1492       $this->attrs['telephoneNumber']= array();
1493     }
1496     $ldap= $this->config->get_ldap_link();
1497     $ldap->cd($this->config->current['BASE']);
1498     $ldap->search("(&(objectClass=goFonQueue)(member=*))", array("member"));
1499     while($attr = $ldap->fetch()){
1500       if(in_array($this->dn,$attr['member'])){
1501         $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
1502         unset($new->by_object['ogroup']->memberList[$this->dn]);
1503         unset($new->by_object['ogroup']->member[$this->dn]);
1504         $new->save();
1505         msg_dialog::display(_("Information"), sprintf(_("User '%s' has been removed from phone queue '%s'."), $this->cn, $new->by_object['ogroup']->cn), INFO_DIALOG);
1506       }
1507     }
1508     $ldap->cd($this->dn);
1509     $this->cleanup();
1510     $ldap->modify ($this->attrs); 
1512     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1513     if (!$ldap->success()){
1514       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
1515     }
1517     /* Optionally execute a command after we're done */
1518     @mysql_close($r_con);
1519     $this->handle_post_events('remove',array("uid"=> $this->uid));
1520   }
1524   /* This function checks if the given phonenumbers are available or already in use*/
1525   function is_number_used()
1526   {
1527     $ldap= $this->config->get_ldap_link();
1528     $ldap->cd($this->config->current['BASE']);
1529     $ldap->search("(|(objectClass=goFonAccount)(objectClass=goFonQueue)(objectClass=goFonConference))", array("telephoneNumber","cn","uid"));
1530     while($attrs = $ldap->fetch()) {
1531       unset($attrs['telephoneNumber']['count']);
1532       foreach($attrs['telephoneNumber'] as $tele){
1533         if(!isset($attrs['cn'][0])) $attrs['cn'][0]=$attrs['dn'];
1534         if(!isset($attrs['uid'][0])) $attrs['uid'][0]=$attrs['dn'];
1535         $numbers[$tele]=$attrs;
1536       }
1537     }
1539     foreach($this->phoneNumbers as $num){
1540       if((isset($numbers[$num]))&&(($numbers[$num]['uid'][0]!=$this->uid))){
1541         if(isset($numbers[$num]['uid'][0])){
1542           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['uid'][0]);
1543         }else{
1544           return sprintf(_("The specified telephonenumber '%s' is already assigned to '%s'."),$num,$numbers[$num]['cn'][0]);
1545         }
1546       }
1547     }
1548   }
1551   /* Create phoneAccount part of copy & paste dialog */
1552   function getCopyDialog()
1553   { 
1554     if(!$this->is_account) return("");
1555     $smarty = get_smarty();
1556     if (!count($this->phoneNumbers)){
1557       $smarty->assign ("phoneNumbers", array(""));
1558     } else {
1559       $smarty->assign ("phoneNumbers", $this->phoneNumbers);
1560     }
1562     $smarty->assign("goFonVoicemailPIN",$this->goFonVoicemailPIN);
1563     $smarty->assign("goFonPIN",$this->goFonPIN);
1565     $display= $smarty->fetch(get_template_path('paste_generic.tpl', TRUE, dirname(__FILE__)));
1566     $ret =array();
1567     $ret['string'] = $display;
1568     $ret['status'] = "";
1569     return($ret);
1570   }
1572   /* Save posts from copy & paste dialog dialog  */
1573   function saveCopyDialog()
1574   {
1575     if(!$this->is_account) return;
1576     $this->execute();
1577     if(isset($_POST['goFonVoicemailPIN'])) {
1578       $this->goFonVoicemailPIN = $_POST['goFonVoicemailPIN'];
1579     }
1580     if(isset($_POST['goFonPIN'])){
1581       $this->goFonPIN = $_POST['goFonPIN'];
1582     }
1583   }
1586   function allow_remove()
1587   {
1588     /* Check if previously selected server is still available */
1589     if($this->initially_was_account && !isset($this->goFonHomeServers[$this->goFonHomeServer])){
1590       return sprintf(_("The previously selected asterisk home server (%s) is no longer available. Remove aborted."),preg_replace("/,/",", ",$this->goFonHomeServer));
1591     }
1592   }
1594   /* Return plugin informations for acl handling */
1595   static function plInfo()
1596   {
1597     return (array(
1598           "plShortName"     => _("Phone"),
1599           "plDescription"   => _("Phone account settings"),
1600           "plSelfModify"    => TRUE,
1601           "plDepends"       => array("user"),
1602           "plPriority"      => 7,                                 // Position in tabs
1603           "plSection"         => array("personal" => _("My account")),
1604           "plCategory"        => array("users"),
1607           "plOptions"       => array(),
1609           "plProvidedAcls"  => array(
1610             "telephoneNumber"     => _("Telephone number"),
1611             "goFonMacro"          => _("Macro settings"),
1612             "goFonHardware"       => _("Phone hardware"),
1613             "goFonHomeServer"     => _("Home server"),
1614             "goFonContext"          => _("Phone context"),
1615             "goFonVoiceMailContext" => _("Voice mail context"),
1616             "goFonPIN"            => _("Telephone pin"),
1617             "goFonVoicemailPIN"   => _("Voicemail pin"))
1618           ));
1619   }
1623   function multiple_execute()
1624   {
1625     plugin::multiple_execute();
1626     return($this->execute());
1627   }
1629   function get_multi_init_values()
1630   {
1631     $ret = plugin::get_multi_init_values();
1632     $ret['phoneNumbers'] = array();
1633     foreach($this->phoneNumbers as $number){
1634       $ret['phoneNumbers'][] = $number."  [".$this->attrs['cn'][0]."]"; 
1635     }
1636     $ret['phoneNumbers']['count'] = count($ret['phoneNumbers']);
1637     return($ret);
1638   }
1640   function init_multiple_support($attrs,$all)
1641   {
1642     plugin::init_multiple_support($attrs,$all);
1644     $this->phoneNumbers = array();
1645     if(isset($all['phoneNumbers'])){
1646       for($i = 0 ; $i < $all['phoneNumbers']['count'] ; $i++){
1647         $this->phoneNumbers[$all['phoneNumbers'][$i]] = $all['phoneNumbers'][$i];
1648       }
1649     }
1650   }
1652   function multiple_save_object()
1653   {
1654     /* Simply call parents save_object */
1655     if (isset($_POST["phoneTab"])){
1657       plugin::save_object();
1658       plugin::multiple_save_object();
1660       /* Every macro in the select box are available */
1661       if((isset($_POST['macro']))){
1662         $this->macrostillavailable=true;
1663       }
1665       if(isset($_POST['macro']) && $_POST['macro'] != $this->macro){
1666         $this->macro = $_POST['macro'];
1667         $this->is_modified =true;
1668       }
1670       /* get all Postvars */
1671       if(isset($this->macroarray[$this->macro])){
1672         foreach($this->macroarray[$this->macro] as $key => $paras){
1673           $backup = $this->macroarray[$this->macro][$key];
1674           if(isset($_POST[$paras['var']])){
1675             $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
1676           }
1677           if(isset($_POST['post_success'])){
1678             if($this->macroarray[$this->macro][$key]['type']=="bool"){
1679               if(isset($_POST[$this->macroarray[$this->macro][$key]['var']])) {
1680                 $this->macroarray[$this->macro][$key]['choosen']=$_POST[$paras['var']];
1681               }else{
1682                 $this->macroarray[$this->macro][$key]['choosen']=false;
1683               }
1684             }
1685           }
1686         }
1687         if(count(array_diff($this->macroarray[$this->macro][$key],$backup))){
1688           $this->is_modified = TRUE;
1689         }
1690       }
1691     }
1692   }
1694   function multiple_check()
1695   {
1696     $message = plugin::multiple_check();
1698     if(!count($this->goFonHomeServers) && in_array("goFonHomeServers",$this->multi_boxes)){
1699       $message[] = _("There is currently no asterisk server defined!");
1700     }
1702     if(empty($this->goFonHomeServer) && in_array("goFonHomeServers",$this->multi_boxes)){
1703       $message[] = _("Asterisk server is invalid!");
1704     }
1706     if(in_array("goFonVoicemailPIN",$this->multi_boxes) && 
1707         ( (strlen($this->goFonVoicemailPIN)==0)||
1708           (strlen($this->goFonVoicemailPIN)>4))){
1709       $message[]=(_("Voicemail PIN must be 4 characters long!"));
1710     }else{
1711       if(preg_match("/[^0-9]/",$this->goFonVoicemailPIN) && in_array("goFonVoicemailPIN",$this->multi_boxes) ){
1712         $message[]=(_("Voicemail PIN contains invalid characters!"));
1713       }
1714     }
1716     if(preg_match("/[^0-9a-z]/i",$this->goFonPIN) && in_array("goFonPIN",$this->multi_boxes)){
1717       $message[]=(_("Phone pin contains invalid characters!"));
1718     }
1720     /* check for ! in any parameter setting*/
1721     if(isset($this->macroarray[$this->macro]) && in_array("macro",$this->multi_boxes)){
1722       foreach($this->macroarray[$this->macro] as $val){
1723         if((strstr($val['choosen'],"!"))||(strstr($val['choosen'],"#"))){
1724           $message[] = sprintf(_("The parameter %s contains invalid char. '!,#' is used as delimiter"),$val['name']);
1725         }
1726       }
1727     }
1729     return($message);
1730   }
1732   function get_multi_edit_values()
1733   {
1734     $ret = plugin::get_multi_edit_values();
1735     if(in_array("macro",$this->multi_boxes)){
1736       $ret['macro'] = $this->macro;
1737       $ret['macroarray'] = $this->macroarray;
1738       $ret['macros'] = $this->macros;
1739     }
1740     return($ret);
1741   }
1744   /* Return asterisk contexts
1745    * Additionaly read contexts from file.
1746    */
1747   function get_asterisk_contexts()
1748   {
1749     $contexts = array();
1750     $contexts[] = "default";
1751     $contexts[] = "parkedcalls";
1752     $contexts[] = "from-sip";
1753     $contexts[] = "from-capi";
1754     $file = "/etc/gosa/asterisk_contexts.conf";
1755     if(file_exists($file) && is_readable($file)){
1756       foreach(file($file) as $context){
1757         $contexts[] = trim($context);
1758       }
1759     }
1760     array_unique($contexts);
1761     return($contexts);
1762   }
1765 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1766 ?>