Code

Removed debug output
[gosa.git] / plugins / gofon / phoneaccount / class_phoneAccount.inc
index dd4bc090e3a0b9ca2bd67cc5d49dfbc615aed219..ba00dbf23821050911858ca373af9de98e0efe5a 100644 (file)
@@ -13,7 +13,7 @@ class phoneAccount extends plugin
   var $goFonFormat            = "wav";
   var $goFonPIN               = "";
   var $goFonVoicemailPIN      = "";
-  var $goFonDeliveryMode      = "";
+  var $goFonDeliveryMode      = "[]";
   var $phoneNumbers           = array();
   var $mail                   = "";
   var $hardware_list          = array();
@@ -31,12 +31,6 @@ class phoneAccount extends plugin
   var $init_HomeServer        = "0";            // Contains the dn of the server that manage this account 
   var $goFonHomeServers       = array();        // Contains all available server configurations 
     
-
-  /* CLI vars */
-  var $cli_summary            = "Manage users phone account";
-  var $cli_description        = "Some longer text\nfor help";
-  var $cli_parameters         = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   /* attribute list for save action */
   var $CopyPasteVars          = array("phoneNumbers","macroarray","macrostillavailable"/*"phoneNumbers" -Reset- */,
                                       "hardware_list","used_hardware");
@@ -66,62 +60,74 @@ class phoneAccount extends plugin
       }
     }
 
+    /* Set uid */
+    if((isset($this->parent->by_object['user']->uid))&&(!empty($this->parent->by_object['user']->uid))){
+      $this->uid = $this->parent->by_object['user']->uid;
+    }
+    if((isset($this->parent->by_object['user']->cn))&&(!empty($this->parent->by_object['user']->cn))){
+      $this->cn  = $this->parent->by_object['user']->cn;
+    }
 
     /* Check server configurations 
      * Load all server configuration in $this->goFonHomeServers if available
      *  and first server as default if necessary.
      * Check if connection is successfull for the selected server $this->goFonHomeServer
      */
+
+    /* Set available server */
+    if(isset($_SESSION['config']->data['SERVERS']['FON'])){
+      $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
+    }
+
     $a_SETUP= array();
-    if(array_key_exists('config',$_SESSION) &&
+    if($this->is_account &&
+       array_key_exists('config',$_SESSION) &&
        array_key_exists('SERVERS',$_SESSION['config']->data) &&
        array_key_exists('FON',$_SESSION['config']->data['SERVERS']) &&
        is_callable("mysql_connect")
        ) {
 
-      /* Set available server */
-      $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
+      /* Servers defined? Watch here... */
+      if (count($this->goFonHomeServers)){
 
-      /* Set default server */
-      if($this->dn == "new"){
-        $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
-      }
+        /* Set default server */
+        if(empty($this->goFonHomeServer) || $this->goFonHomeServer == "0"){
+          $this->goFonHomeServer= $this->goFonHomeServers[0]['DN'];
+        }
 
-      /* Remember inital home server, to be able to remove old entries */
-      $this->init_HomeServer = $this->goFonHomeServer;
+        /* Remember inital home server, to be able to remove old entries */
+        $this->init_HomeServer = $this->goFonHomeServer;
 
-      /* get config */
-      if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
-        print_red(sprintf(_("The specified goFonHomeServer '%s' is not available in GOsa server configuration. Saving this account will create a new entry on the server '%s'. Use cancel if you do not want to create a new entry ignoring old accounts."),$this->goFonHomeServer, $this->goFonHomeServers[0]['DN']));
+        /* Get config */
+        if(!isset($this->goFonHomeServers[$this->goFonHomeServer])){
+          print_red(sprintf(_("The specified goFonHomeServer '%s' is not available in GOsa server configuration. Saving this account will create a new entry on the server '%s'. Use cancel if you do not want to create a new entry ignoring old accounts."),$this->goFonHomeServer, $this->goFonHomeServers[0]['DN']));
 
-        $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
-        $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
-      }    
-      $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
+          $this->goFonHomeServer = $this->goFonHomeServers[0]['DN'];
+          $this->init_HomeServer = $this->goFonHomeServers[0]['DN'];
+        }    
+        $cur_cfg = $this->goFonHomeServers[$this->goFonHomeServer];
 
-      $r_con =  @mysql_pconnect($cur_cfg['SERVER'],$cur_cfg['LOGIN'],$cur_cfg['PASSWORD']);
-      if(!$r_con){
-        print_red( sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
-            $cur_cfg['SERVER'],$cur_cfg['LOGIN']));
-        gosa_log(mysql_error());
-        return false;
-      }
-      $db  =  @mysql_select_db($cur_cfg['DB'],$r_con);
-      if(!$db){
-        print_red(sprintf(_("Can't select database %s on %s."),$cur_cfg['DB'],$cur_cfg['SERVER']));
-        gosa_log(mysql_error());
-        return false;
-      }
+        $r_con =  @mysql_pconnect($cur_cfg['SERVER'],$cur_cfg['LOGIN'],$cur_cfg['PASSWORD']);
+        if(!$r_con){
+#          print_red( sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
+#                $cur_cfg['SERVER'],$cur_cfg['LOGIN']));
+          gosa_log(mysql_error());
+        }
+        $db  =  @mysql_select_db($cur_cfg['DB'],$r_con);
+        if(!$db){
+#         print_red(sprintf(_("Can't select database %s on %s."),$cur_cfg['DB'],$cur_cfg['SERVER']));
+          gosa_log(mysql_error());
+        }
 
-      $first = false; 
-      foreach($this->phoneNumbers as $key => $val){
-        if(!$first){
-          $first = $key;
+        $first = false; 
+        foreach($this->phoneNumbers as $key => $val){
+          if(!$first){
+            $first = $key;
+          }
         }
       }
     }
 
-
     /* Get available phone hardware  
      * Search for all available phone hardware  
      */
@@ -143,7 +149,7 @@ class phoneAccount extends plugin
      * Search for all Marcos that are visible and create 
      *  an array with name and parameters 
      */
-    $ldap->search("((objectClass=goFonMacro)(goFonMacroVisible=1))", array("*"));
+    $ldap->search("(&(objectClass=goFonMacro)(goFonMacroVisible=1))", array("*"));
 
     /* Add none for no macro*/
     $this->macros['none']=_("no macro");    
@@ -204,7 +210,7 @@ class phoneAccount extends plugin
       unset($tmp[0]);
 
       /* Check if makro has been removed */
-      if(!isset($this->macroarray[$this->macro])){
+      if(!isset($this->macros[$this->macro])){
         $this->macrostillavailable = false;
       }else{
         $this->macrostillavailable = true;
@@ -243,11 +249,6 @@ class phoneAccount extends plugin
     ksort($this->hardware_list);
     $this->a_old_telenums = $this->phoneNumbers;
 
-    if($this->is_account){
-      $this->is_modified = true;
-    }
-
-
     /* Get voicemail PIN from MySQL DB 
      * Because every user can change his PIN directly from the phone
      *  without any update to the ldap
@@ -336,8 +337,6 @@ class phoneAccount extends plugin
 
     $i_is_accounted = false;  // Ensure that extension entry, for name to number is only once in table
 
-    restore_error_handler();
-
     /* Prepare some basic attributes */
     foreach($this->a_old_telenums as $tele){
       $oldnums[]= preg_replace("/[^0-9]/","",$tele);
@@ -557,7 +556,7 @@ class phoneAccount extends plugin
         $sip_data_array['host']         = $s_host;
         $sip_data_array['insecure']     = NULL;
         $sip_data_array['language']     = NULL;
-        $sip_data_array['mailbox']      = "asterisk";
+        $sip_data_array['mailbox']      = $newnums[$i_new_key];
         $sip_data_array['md5secret']    = NULL;
         $sip_data_array['nat']          = "no";
         $sip_data_array['permit']       = NULL;
@@ -609,6 +608,11 @@ class phoneAccount extends plugin
           "fullname"    => $CNname,
           "email"       => $s_mail);
 
+      /* Set pager number if available */
+      if(isset($this->parent->by_object['user']->pager)){
+        $voice_data_array['pager']   = $this->parent->by_object['user']->pager;
+      }
+
       /* Check if there is already an entry in sip_users for this uid */
       $rid = mysql_query("SELECT * FROM ".$a_New['VOICE_TABLE']." WHERE customer_id='".$customer_id."';\n",$new_connection);
       if(mysql_affected_rows($new_connection)){
@@ -640,7 +644,6 @@ class phoneAccount extends plugin
          * Insert Voice mail entry  
          **********************/
         $voice_data_array['context'] = "default";
-        $voice_data_array['pager']   = "";
   
         /* There is currently no voice mail entry for this user. 
          * We should create one 
@@ -689,12 +692,15 @@ class phoneAccount extends plugin
      
       $i = 0; 
       $EXT = array();
-      $EXT[$i]['context'] = 'GOsa';
-      $EXT[$i]['exten']   = $this->uid;
-      $EXT[$i]['priority']= 1;
-      $EXT[$i]['app']     = "Goto";
-      $EXT[$i]['appdata'] = $newnums[$i_new_key]."|1";
-      $i ++;
+    
+      if(!is_numeric($this->uid)){
+        $EXT[$i]['context'] = 'GOsa';
+        $EXT[$i]['exten']   = $this->uid;
+        $EXT[$i]['priority']= 1;
+        $EXT[$i]['app']     = "Goto";
+        $EXT[$i]['appdata'] = $newnums[$i_new_key]."|1";
+        $i ++;
+      }
 
       // Entension entries  Hint / Dial / Goto
       foreach($newnums as $s_telenums){
@@ -751,10 +757,12 @@ class phoneAccount extends plugin
       }
 
       // Perform queries ...
-      foreach($SQL_query_array as $query){
-        if(!@mysql_query($query,$new_connection)){
-          print_red(_("Error while performing query:")." ".mysql_error());
-          return false;
+      if($this->goFonHardware != "automatic"){
+        foreach($SQL_query_array as $query){
+          if(!@mysql_query($query,$new_connection)){
+            print_red(_("Error while performing query:")." ".mysql_error());
+            return false;
+          }
         }
       }
     }
@@ -816,7 +824,13 @@ class phoneAccount extends plugin
 
     /* Do we need to flip is_account state? */
     if (isset($_POST['modify_state'])){
-      $this->is_account= !$this->is_account;
+
+      /* Onyl change account state if allowed */
+      if($this->is_account && $this->acl == "#all#"){
+        $this->is_account= !$this->is_account;
+      }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
+        $this->is_account= !$this->is_account;
+      }
     }
 
     /* Select no macro if, state is empty, this is the case, if the selected macro is no longer available */
@@ -1031,6 +1045,7 @@ class phoneAccount extends plugin
   function save_object()
   {
     if (isset($_POST["phoneTab"])){
+    
       plugin::save_object();
 
       /* Save checkbox */
@@ -1051,6 +1066,10 @@ class phoneAccount extends plugin
         $this->macrostillavailable=true;
       }
 
+      if(isset($_POST['macro']) && $_POST['macro'] != $this->macro){
+        $this->is_modified =true;
+      }
+
       if(is_array($this->phoneNumbers)){
         foreach($this->phoneNumbers as $telenumms) {
           $nummsinorder[]=$telenumms; 
@@ -1060,8 +1079,12 @@ class phoneAccount extends plugin
       }
 
       /* get all Postvars */
-      if(isset($this->macroarray[$this->macro])){ 
+      if(isset($this->macroarray[$this->macro])){
         foreach($this->macroarray[$this->macro] as $key => $paras){
+
+          $backup = $this->macroarray[$this->macro][$key];  
+
           if(isset($_POST[$paras['var']])){
             $this->macroarray[$this->macro][$key]['choosen'] = $_POST[$paras['var']];
           }
@@ -1079,6 +1102,9 @@ class phoneAccount extends plugin
               }
             }
           }
+          if(count(array_diff($this->macroarray[$this->macro][$key],$backup))){
+            $this->modified = TRUE;
+          }
         }
       }
     }
@@ -1089,6 +1115,14 @@ class phoneAccount extends plugin
     /* Call common method to give check the hook */
     $message= plugin::check();
 
+    if(!count($this->goFonHomeServers)){
+      $message[] = _("There must be at least one server with an asterisk database to create a phone account.");
+    }
+
+    if(empty($this->goFonHomeServer)){
+      $message[] = _("Please select a valid goFonHomeServer.");
+    }
+
     if((strlen($this->goFonVoicemailPIN)==0)||(strlen($this->goFonVoicemailPIN)>4)){
       $message[]=(_("Voicemail PIN must be between 1-4 characters."));
     }else{
@@ -1105,8 +1139,10 @@ class phoneAccount extends plugin
       }
     }
 
-    if(!$this->generate_mysql_entension_entries()){
-      $message[] = $this->generate_error;
+    if ($this->initially_was_account != $this->is_account || $this->is_modified){
+      if(!$this->generate_mysql_entension_entries()){
+        $message[] = $this->generate_error;
+      }
     }
 
     /* We need at least one phone number */
@@ -1131,6 +1167,16 @@ class phoneAccount extends plugin
   {
     plugin::save();
 
+    /* Force saving macro again 
+     * This ensures that 
+     *  - the macro is available on the destiantion server.
+     *  - the macro saved is up to date on the destination server.
+     */
+    if(!empty($this->macro) && $this->macro != "none")  {
+      $macro_tab = new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->macro);
+      $macro_tab -> save();
+    }
+
     /* Save arrays */
     $tmp_numbers = array();
     foreach ($this->phoneNumbers as $number){
@@ -1152,7 +1198,12 @@ class phoneAccount extends plugin
 
     $this->attrs['goFonForwarding']=array();
 
-    $this->generate_mysql_entension_entries(true);
+    if ($this->initially_was_account != $this->is_account || $this->is_modified){
+      $str = $this->generate_mysql_entension_entries(true);
+      if(empty($str)){
+        print_red($str);
+      }
+    }
 
     if($this->attrs['goFonMacro']==""){
       $this->attrs['goFonMacro']=array();
@@ -1292,14 +1343,14 @@ class phoneAccount extends plugin
 
     $ldap= $this->config->get_ldap_link();
     $ldap->cd($this->config->current['BASE']);
-    $ldap->search("(objectClass=goFonQueue)", array("member"));
+    $ldap->search("(&(objectClass=goFonQueue)(member=*))", array("member"));
     while($attr = $ldap->fetch()){
       if(in_array($this->dn,$attr['member'])){
         $new =new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],$attr['dn']);
         unset($new->by_object['ogroup']->memberList[$this->dn]);
         unset($new->by_object['ogroup']->member[$this->dn]);
         $new->save();
-        print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->attrs['cn']));
+        print_red(sprintf(_("Removed user '%s' from phone queue '%s'."),$this->uid,$new->by_object['ogroup']->cn));
       }
     }
     $ldap->cd($this->dn);
@@ -1377,6 +1428,15 @@ class phoneAccount extends plugin
       $this->goFonPIN = $_POST['goFonPIN'];
     }
   }
+
+
+  function allow_remove()
+  {
+    /* Check if previously selected server is still available */
+    if($this->initially_was_account && !isset($this->goFonHomeServers[$this->goFonHomeServer])){
+      return sprintf(_("The previously selected asterisk home server (%s) is no longer available. Remove aborted."),preg_replace("/,/",", ",$this->goFonHomeServer));
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: