Code

Removed unwanted unset
[gosa.git] / plugins / gofon / macro / class_gofonMacro.inc
1 <?php
3 //!  The Phone Macro Class: Handles Macro Contents, and some attributes. 
4 /*!
5      This class handles the basic information about phone macros, like
6      cn base description displayName goFonMacroContent goFonMacroVisible
8      This is not the only Class that manages phone Macros, there ist also the class_goFonMacroParameter.
9 */
10 class macro extends plugin
11 {
12   /*! CLI vars */
13   var $cli_summary= "Handling of GOsa's macro object";
14   /*! CLI vars */
15   var $cli_description= "Some longer text\nfor help";
16   /*! CLI vars */
17   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
20   /*! Macro attributes,  */
21   var $generate_error= "";
22   
23   /*! The name of the Macro in the openldap drirectory */
24   var $cn               = ""; 
25   
26   /*! This ist the variable that contains the description of the macro*/
27   var $description      = "";
29   /*! The base of the macro, is used to save the macro in the correct directory tree */
30   var $base             = "";
32   /*! This is the name of the macro which the enduser will see, instead of the cn */
33   var $displayName      = "";
34     
35   /*! Here is the macro content, the real macroscript */
36   var $goFonMacroContent= "";
37   
38   /*! To allow user to use this macro this var must be true, else false */
39   var $goFonMacroVisible= 0;
41   /*! attribute list for save action */
42   var $attributes     = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
44   var $is_new=false;
45   var $orig_cn = ""; 
46   /*! Objectclasses that this calls handles */
47   var $objectclasses  = array("top", "goFonMacro");
49   //! The Konstructor   
50   /*!  Konstructor, load class with  attributes of the given dn*/
51   function macro ($config, $dn= NULL)
52   {
53     plugin::plugin ($config, $dn);
55     $ldap= $config->get_ldap_link();
57     $this->dn = $dn;
59     /* This is always an account */
60     $this->is_account= TRUE;
63     /* Edit or new one ?*/
64     if ($this->dn == "new"){
65       if(isset($_SESSION['macrofilter']['depselect'])){
66         $this->base = $_SESSION['macrofilter']['depselect'];
67         $this->is_new = true;
68       }else{
69         $this->is_new = true;
70         $ui= get_userinfo();
71         $this->base= dn2base($ui->dn);
72       }
73     } else {
74       $this->is_new = false;
75       $this->orig_cn=$this->cn;
76       $this->base= dn2base($this->dn);
77     }
78   }
80   /*!  Execute this plugin */
81   function execute()
82   {
83     /* Variables */
84     $vars       = "";
85     $tmp        = array();
86     $number = 0; 
88     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
89       print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). \n  Your Settings can't be saved to asterisk Database. "));
90     }
92     /* Do we represent a valid group? */
93     if (!$this->is_account && $this->parent == NULL){
94       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
95         _("This 'dn' is no phone macro.")."</b>";
96       return ($display);
97     }
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $smarty->assign("bases", $this->config->idepartments);
103     /* Assign all vars to Smarty */
104     foreach($this->attributes as $ar){
105       $smarty->assign($ar, $this->$ar);
106     }
107     /* Checkboxes */
108     $smarty->assign("base_select", $this->base);
109     $smarty->assign("vars", $vars);
111     if($this->goFonMacroVisible){
112       $smarty->assign("goFonMacroVisibleChecked"," checked ");
113     }else{
114       $smarty->assign("goFonMacroVisibleChecked","");
115     }
117     if(isset($_POST['goFonMacroVisible'])) {
118       $this->goFonMacroVisible= 1 ;
119       $smarty->assign("goFonMacroVisibleChecked"," checked ");
120     }else  {
121       if(isset($_POST['displayName'])){
122         $this->goFonMacroVisible= 0 ;
123         $smarty->assign("goFonMacroVisibleChecked","");
124       }
125     }
126     
127     if(!$this->is_new){
128       $smarty->assign("disable_cn"," disabled ");
129       $smarty->assign("cn",$this->orig_cn);
130     }else{
131       $smarty->assign("disable_cn","  ");
132       $smarty->assign("cn",$this->cn);
133     }
134     $this->generate_mysql_entension_entries();
135     /* Show main page */
136     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
137   }
139   /*!
140   Remove this Object
141   */
142   function remove_from_parent()
143   {
144     $ldap= $this->config->get_ldap_link();
145     
146     $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
148     while ($val = $ldap->fetch()){ 
149       if(isset($val['goFonMacro'])){
150         if(strstr($val['goFonMacro'][0],$this->dn)){ 
151           print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
152           return false;
153         }
154       }
155     }
156   
157     $ldap->rmDir($this->dn); 
158     if(isset($this->orig_cn)){
159       $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
160     }else{
161       $this->generate_mysql_entension_entries(false,true);
162     }
164     /* Delete references to object groups */
165     $ldap->cd ($this->config->current['BASE']);
166     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
167     while ($ldap->fetch()){
168         $og= new ogroup($this->config, $ldap->getDN());
169         unset($og->member[$this->dn]);
170         $og->save ();
171     }
172   }
174   // Generate MySQL Syntax 
175   function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
177     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
178       return(true);
179     }
181     // Get Configuration for Mysql database Server  
182     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
184     // Connect to DB server
185     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']); 
186   
187     // Check if we are  connected correctly 
188     if(!$r_con){
189       $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
190           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
191       gosa_log(mysql_error());
192       return false;
193     }
195     // Select database for Extensions 
196     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
198     // Test if we have the database selected correctly
199     if(!$db){
200       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
201       gosa_log(mysql_error());
202       return false;
203     }
206     // Context def
207     $context  = "macro-".$this->cn;
209     // Parse Content if we connected correctly
210     if($db &&  $r_con ){
212       // Split Content into lines 
213       $a_contentLines = split ("\n",$this->goFonMacroContent);
215       // Foreach single line ...
216       foreach($a_contentLines as $i_linenum => $s_linestr){
217     
218         // Remove unwanted exten => tag 
219         $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
220       
221         // Remove  spaces 
222         $s_linestr = trim ( $s_linestr);
224         // If not empty or linebreak at [0]
225         if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
226   
227           // Set general SQL statement
228           $SQL[$i_linenum] =  
229             "INSERT INTO ".$a_SETUP['EXT_TABLE'].
230             " (context,exten,priority,app,appdata) ".
231             " VALUES ".
232             " (";
233       
234           // Parse linestr to entry data
235           $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
237           $tmp  = split(",", preg_replace("/\(.*$/","",$linecontents));
238   
239           if(!isset($tmp[2])){
240             $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
241             return false;
242           }    
243             $exten = $tmp[0];
244             $prio = $tmp[1];
245             $app  = $tmp[2];
246             $para = $linecontents; 
247             $para = preg_replace("/^.*\(/","",$para);
248             $para = preg_replace("/\)$/","",$para);
250             // Append SQL syntax
251             $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
252         }
253      }
255       if(($save)||($delete_only)){
256         $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
257         if(!$res){
258           $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
259           gosa_log(mysql_error());
260           return false;
261         }
262         $cnt = mysql_fetch_row($res);
263         $cnt = $cnt[0];
265         if($cnt != 0) {
266           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
267             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
268             gosa_log(mysql_error());
269             return false;
270           }
271         }
273         if($remove_old_macroname!="false"){
274           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
275             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
276             gosa_log(mysql_error());
277             return false;
278           }
279         }
283       }
285       // We want to save this 
286       if(($save)&&(isset($SQL))){
287         foreach($SQL as $entry){
288           if(!mysql_query($entry)){
289             $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
290             gosa_log(mysql_error());
291             return false;
292           }
293         } 
294       }    
295     }
296     if((isset($r_con))&&($r_con)){
297       @mysql_close($r_con);
298     }
299     return true;
300   }
305   /*!
306   Save data to object 
307   */
308   function save_object()
309   {
310     if (isset($_POST['displayName'])){
311       plugin::save_object();
313     }
314   }
317   /*! 
318   Check values 
319   */
320   function check()
321   {
323     $message = array();
324     if(!$this->generate_mysql_entension_entries()){
325       $message[] = $this->generate_error;
326     }
328     if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
329       $ldap = $this->config->get_ldap_link();
330       $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
331       if($ldap->count()>0){
332         $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
333       }
334     }
336     if(empty($this->displayName)){
337       $message[] = _("You must specify the 'Display Name' in order to save this macro");
338     }  
339  
340     if(strlen("Makro-".$this->cn)>20 ){
341       $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
342     }
343  
344     foreach($this->attributes as $attr){
345       if(chkacl($this->acl,$attr)){
346         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
347         return(array($str));
348         }
349       }
350     
351     if(count(split("\n",$this->goFonMacroContent))>100){
352       $message[] = _("Makro length must be lower than 100 lines");
353     }
354    
355     /*Some stupid IE fixes again*/
356     if(empty ($this->base)) {
357       $message[] = _("Please choose a valid  base.");
358     }
359  
360     return $message;
361   }
364   /*! 
365   Save to LDAP 
366   */
367   function save()
368   {
369     /* Post checks */
370     $this->execute();
372     plugin::save();
373     unset($this->attrs['base']);
375     /* Write back to ldap */
376     $ldap= $this->config->get_ldap_link();
377     $ldap->cat($this->dn);
378     $a= $ldap->fetch();
379     
380     if(isset($this->orig_cn)){
381       $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
382     }else{
383       $this-> generate_mysql_entension_entries(true);
384     }
386     if($this->generate_mysql_entension_entries()){
387       if (count($a)){
388         $ldap->cd($this->dn);
389         $ldap->modify($this->attrs);
390         $this->handle_post_events("modify");
391       } else {
392         $ldap->cd($this->config->current['BASE']);
393         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
394         $ldap->cd($this->dn);
395         $ldap->add($this->attrs);
396         $this->handle_post_events("add");
397       }
398     }
399     show_ldap_error($ldap->get_error());
400   }
403 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
404 ?>