Code

Updated gofoMacro.
[gosa.git] / plugins / gofon / macro / class_gofonMacro.inc
old mode 100755 (executable)
new mode 100644 (file)
index 67221de..9ea248a
@@ -9,13 +9,6 @@
 */
 class macro extends plugin
 {
-  /*! CLI vars */
-  var $cli_summary= "Handling of GOsa's macro object";
-  /*! CLI vars */
-  var $cli_description= "Some longer text\nfor help";
-  /*! CLI vars */
-  var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
-
   /*! Macro attributes,  */
   var $generate_error= "";
   
@@ -49,6 +42,8 @@ class macro extends plugin
 
   var $goFonHomeServers = array(); // Contains all available asterisk database server 
 
+  var $is_new = FALSE;
+
   //! The Konstructor   
   /*!  Konstructor, load class with  attributes of the given dn*/
   function macro ($config, $dn= NULL, $parent= NULL)
@@ -60,6 +55,7 @@ class macro extends plugin
 
     /* Edit or new one ?*/
     if ($this->dn == "new"){
+      $this->is_new = TRUE;
       if(isset($_SESSION['CurrentMainBase'])){
         $this->base = $_SESSION['CurrentMainBase'];
       }else{
@@ -77,6 +73,7 @@ class macro extends plugin
     $a_SETUP= array();
     if(array_key_exists('config',$_SESSION) &&
        array_key_exists('SERVERS',$_SESSION['config']->data) &&
+       count($_SESSION['config']->data['SERVERS']['FON']) && 
        array_key_exists('FON',$_SESSION['config']->data['SERVERS'])) {
 
       /* Set available server */
@@ -150,21 +147,15 @@ class macro extends plugin
       $smarty->assign("goFonMacroVisibleChecked","");
     }
 
-    if(isset($_POST['goFonMacroVisible'])) {
-      $this->goFonMacroVisible= 1 ;
-      $smarty->assign("goFonMacroVisibleChecked"," checked ");
-    }else  {
-      if(isset($_POST['displayName'])){
-        $this->goFonMacroVisible= 0 ;
-        $smarty->assign("goFonMacroVisibleChecked","");
-      }
-    }
-    
     if($this->dn != "new"){
       $smarty->assign("disable_cn"," disabled ");
     }else{
       $smarty->assign("disable_cn","  ");
     }
+
+    /* Ensure that macro content is displayed correctly encoded */
+    $smarty->assign("goFonMacroContent",htmlentities(utf8_decode ($this->goFonMacroContent)));
+
     /* Show main page */
     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
   }
@@ -217,18 +208,19 @@ class macro extends plugin
     }
 
     /* Create query string */
-    $context  = "macro-".$this->cn;
+    $context  = addslashes("macro-".$this->cn);
 
     /* Remove current macro from each server available */ 
     if($save){
       foreach($this->goFonHomeServers as $dn => $Server){
-        $query      = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context= '".$context."';";
+        $query      = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context='".$context."';";
         $r_current  =  @mysql_pconnect($Server['SERVER'],$Server['LOGIN'],$Server['PASSWORD']);
         $db_current =  @mysql_select_db($Server['DB'],$r_current);
         $res = @mysql_query($query,$r_current);
+        @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
         if(!$res){
           gosa_log(@mysql_error($r_current));
-          return(sprintf(_("Removing marco from '%s' failed. Check GOsa log for mysql error."),$Server['SERVER']));
+          return(sprintf(_("Removing macro from '%s' failed. Check GOsa log for mysql error."),$Server['SERVER']));
         }
         @mysql_close($r_current);
       }
@@ -274,9 +266,10 @@ class macro extends plugin
        * Remove comments introduced by ;
        * Skip empty lines 
        */ 
-      $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
-      $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
       $s_linestr = trim($s_linestr);
+      $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
+      $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
+
       if(empty($s_linestr)){
         continue;
       }
@@ -290,9 +283,9 @@ class macro extends plugin
       $tmp  = split(",", $s_linestr,3);
 
       /* Check if there are exactly 2 , */ 
-      if(substr_count($s_linestr,",") !=2){
-        return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
-      }
+#      if(substr_count($s_linestr,",") !=2){
+#        return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
+#      }
       /* Multiple () are not supproted currently ... */  
       if(substr_count($s_linestr,"(") >1 ){
         return(sprintf(_("More than one '(' is currently not supported. Line : '%s'."),$i_linenum));
@@ -313,10 +306,10 @@ class macro extends plugin
        *  and add this line to an array that will be inserted 
        *  to each database.
        */
-      $exten  = $tmp[0];
-      $prio   = $tmp[1];
-      $app    = preg_replace("/\(.*\).*$/","",$tmp[2]);
-      $para   = preg_replace("/^.*\(/","",$tmp[2]);
+      $exten  = addslashes($tmp[0]);
+      $prio   = addslashes($tmp[1]);
+      $app    = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
+      $para   = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
       $para   = preg_replace("/\).*$/","",$para);
       $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
     }
@@ -333,6 +326,7 @@ class macro extends plugin
         $db     = @mysql_select_db($cfg['DB'],$r_con);
         $query  = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
         $res    = @mysql_query($query,$r_con);
+        @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
         if(!$res){
           gosa_log(@mysql_error($r_con));
           return(sprintf(_("Insert of new macro failed for server '%s'."),$cfg['SERVER']));
@@ -350,9 +344,15 @@ class macro extends plugin
       plugin::save_object();
 
       /* The cn can't be changed if this entry is not new */
-      if($this->dn!= "new"){
+      if(!$this->is_new){
         $this->cn = $this->orig_cn;
       }
+
+      if(isset($_POST['goFonMacroVisible'])) {
+        $this->goFonMacroVisible= 1 ;
+      }else  {
+        $this->goFonMacroVisible= 0 ;
+      }
     }
   }
 
@@ -363,6 +363,10 @@ class macro 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 save this phone macro.");
+    }
+
     /* Check if insert/replace is possible and all servers are available */
     $str = $this->add_to_database(false);
     if($str){
@@ -395,11 +399,17 @@ class macro extends plugin
       }
     }
 
-    /* Macro content must be smaller than 100 lines */
-    if(count(split("\n",$this->goFonMacroContent))>100){
-      $message[] = _("Makro length must be lower than 100 lines");
+    /* If this macro is still in use we should not change the visible for user flag to invisible */
+    if(!$this->goFonMacroVisible){
+      $ldap = $this->config->get_ldap_link();
+      $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
+      while ($val = $ldap->fetch()){
+        if(strstr($val['goFonMacro'][0],$this->dn)){
+          $message[] = _("This macro is still in use. It is necessary to mark this macro as visible for users.");
+          return($message);
+        }
+      }
     }
-
     return $message;
   }
 
@@ -421,9 +431,15 @@ class macro extends plugin
     }
 
     /* Try to remove from database */
-    $str = $this->remove_from_database(true);
-    if($str){ 
-      print_red($str);
+    if(count($this->goFonHomeServers)){
+      $str = $this->remove_from_database(true);
+      if($str){ 
+        print_red($str);
+        return false;
+      }
+    }else{
+      print_red(_("Could not remove the macro entry from asterisk databases. Please check your asterisk database configurations."));
+      return false;
     }
 
     /* Remove phone macro */ 
@@ -432,7 +448,7 @@ class macro extends plugin
 
     /* Delete references to object groups */
     $ldap->cd ($this->config->current['BASE']);
-    $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
+    $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".@LDAP::prepare4filter($this->dn)."))", array("cn"));
     while ($ldap->fetch()){
       $og= new ogroup($this->config, $ldap->getDN());
       unset($og->member[$this->dn]);
@@ -445,16 +461,13 @@ class macro extends plugin
   /*! Save to LDAP */
   function save()
   {
-    /* Post checks */
-    $this->execute();
-
     plugin::save();
     unset($this->attrs['base']);
 
     /* Try to add entries to databases */
     $str = $this->add_to_database(true);
     if($str){
-      print_a($str);
+      print_red($str);
     }else{
       /* Write back to ldap */
       $ldap= $this->config->get_ldap_link();