Code

Added check for existence of goFonServer to Macros
[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     unset($_SESSION['config']->data['SERVERS']['FON']);
57     $ldap= $config->get_ldap_link();
59     $this->dn = $dn;
61     /* This is always an account */
62     $this->is_account= TRUE;
65     /* Edit or new one ?*/
66     if ($this->dn == "new"){
67       if(isset($_SESSION['macrofilter']['depselect'])){
68         $this->base = $_SESSION['macrofilter']['depselect'];
69         $this->is_new = true;
70       }else{
71         $this->is_new = true;
72         $ui= get_userinfo();
73         $this->base= dn2base($ui->dn);
74       }
75     } else {
76       $this->is_new = false;
77       $this->orig_cn=$this->cn;
78       $this->base= dn2base($this->dn);
79     }
80   }
82   /*!  Execute this plugin */
83   function execute()
84   {
85     /* Variables */
86     $vars       = "";
87     $tmp        = array();
88     $number = 0; 
90     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
91       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. "));
92     }
94     /* Do we represent a valid group? */
95     if (!$this->is_account && $this->parent == NULL){
96       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
97         _("This 'dn' is no phone macro.")."</b>";
98       return ($display);
99     }
101     /* Fill templating stuff */
102     $smarty= get_smarty();
103     $smarty->assign("bases", $this->config->idepartments);
105     /* Assign all vars to Smarty */
106     foreach($this->attributes as $ar){
107       $smarty->assign($ar, $this->$ar);
108     }
109     /* Checkboxes */
110     $smarty->assign("base_select", $this->base);
111     $smarty->assign("vars", $vars);
113     if($this->goFonMacroVisible){
114       $smarty->assign("goFonMacroVisibleChecked"," checked ");
115     }else{
116       $smarty->assign("goFonMacroVisibleChecked","");
117     }
119     if(isset($_POST['goFonMacroVisible'])) {
120       $this->goFonMacroVisible= 1 ;
121       $smarty->assign("goFonMacroVisibleChecked"," checked ");
122     }else  {
123       if(isset($_POST['displayName'])){
124         $this->goFonMacroVisible= 0 ;
125         $smarty->assign("goFonMacroVisibleChecked","");
126       }
127     }
128     
129     if(!$this->is_new){
130       $smarty->assign("disable_cn"," disabled ");
131       $smarty->assign("cn",$this->orig_cn);
132     }else{
133       $smarty->assign("disable_cn","  ");
134       $smarty->assign("cn",$this->cn);
135     }
136     $this->generate_mysql_entension_entries();
137     /* Show main page */
138     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
139   }
141   /*!
142   Remove this Object
143   */
144   function remove_from_parent()
145   {
146     $ldap= $this->config->get_ldap_link();
147     
148     $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
150     while ($val = $ldap->fetch()){ 
151       if(isset($val['goFonMacro'])){
152         if(strstr($val['goFonMacro'][0],$this->dn)){ 
153           print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
154           return false;
155         }
156       }
157     }
158   
159     $ldap->rmDir($this->dn); 
160     if(isset($this->orig_cn)){
161       $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
162     }else{
163       $this->generate_mysql_entension_entries(false,true);
164     }
166     /* Delete references to object groups */
167     $ldap->cd ($this->config->current['BASE']);
168     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
169     while ($ldap->fetch()){
170         $og= new ogroup($this->config, $ldap->getDN());
171         unset($og->member[$this->dn]);
172         $og->save ();
173     }
174   }
176   // Generate MySQL Syntax 
177   function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
179     if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
180       return(true);
181     }
183     // Get Configuration for Mysql database Server  
184     $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
186     // Connect to DB server
187     $r_con =  @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']); 
188   
189     // Check if we are  connected correctly 
190     if(!$r_con){
191       $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
192           $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
193       gosa_log(mysql_error());
194       return false;
195     }
197     // Select database for Extensions 
198     $db  =  @mysql_select_db($a_SETUP['DB'],$r_con);
200     // Test if we have the database selected correctly
201     if(!$db){
202       $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
203       gosa_log(mysql_error());
204       return false;
205     }
208     // Context def
209     $context  = "macro-".$this->cn;
211     // Parse Content if we connected correctly
212     if($db &&  $r_con ){
214       // Split Content into lines 
215       $a_contentLines = split ("\n",$this->goFonMacroContent);
217       // Foreach single line ...
218       foreach($a_contentLines as $i_linenum => $s_linestr){
219     
220         // Remove unwanted exten => tag 
221         $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
222       
223         // Remove  spaces 
224         $s_linestr = trim ( $s_linestr);
226         // If not empty or linebreak at [0]
227         if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
228   
229           // Set general SQL statement
230           $SQL[$i_linenum] =  
231             "INSERT INTO ".$a_SETUP['EXT_TABLE'].
232             " (context,exten,priority,app,appdata) ".
233             " VALUES ".
234             " (";
235       
236           // Parse linestr to entry data
237           $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
239           $tmp  = split(",", preg_replace("/\(.*$/","",$linecontents));
240   
241           if(!isset($tmp[2])){
242             $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
243             return false;
244           }    
245             $exten = $tmp[0];
246             $prio = $tmp[1];
247             $app  = $tmp[2];
248             $para = $linecontents; 
249             $para = preg_replace("/^.*\(/","",$para);
250             $para = preg_replace("/\)$/","",$para);
252             // Append SQL syntax
253             $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
254         }
255      }
257       if(($save)||($delete_only)){
258         $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
259         if(!$res){
260           $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
261           gosa_log(mysql_error());
262           return false;
263         }
264         $cnt = mysql_fetch_row($res);
265         $cnt = $cnt[0];
267         if($cnt != 0) {
268           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
269             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
270             gosa_log(mysql_error());
271             return false;
272           }
273         }
275         if($remove_old_macroname!="false"){
276           if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
277             $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
278             gosa_log(mysql_error());
279             return false;
280           }
281         }
285       }
287       // We want to save this 
288       if(($save)&&(isset($SQL))){
289         foreach($SQL as $entry){
290           if(!mysql_query($entry)){
291             $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
292             gosa_log(mysql_error());
293             return false;
294           }
295         } 
296       }    
297     }
298     if((isset($r_con))&&($r_con)){
299       @mysql_close($r_con);
300     }
301     return true;
302   }
307   /*!
308   Save data to object 
309   */
310   function save_object()
311   {
312     if (isset($_POST['displayName'])){
313       plugin::save_object();
315     }
316   }
319   /*! 
320   Check values 
321   */
322   function check()
323   {
325     $message = array();
326     if(!$this->generate_mysql_entension_entries()){
327       $message[] = $this->generate_error;
328     }
330     if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
331       $ldap = $this->config->get_ldap_link();
332       $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
333       if($ldap->count()>0){
334         $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
335       }
336     }
338     if(empty($this->displayName)){
339       $message[] = _("You must specify the 'Display Name' in order to save this macro");
340     }  
341  
342     if(strlen("Makro-".$this->cn)>20 ){
343       $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
344     }
345  
346     foreach($this->attributes as $attr){
347       if(chkacl($this->acl,$attr)){
348         $str =  sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
349         return(array($str));
350         }
351       }
352     
353     if(count(split("\n",$this->goFonMacroContent))>100){
354       $message[] = _("Makro length must be lower than 100 lines");
355     }
356    
357     /*Some stupid IE fixes again*/
358     if(empty ($this->base)) {
359       $message[] = _("Please choose a valid  base.");
360     }
361  
362     return $message;
363   }
366   /*! 
367   Save to LDAP 
368   */
369   function save()
370   {
371     /* Post checks */
372     $this->execute();
374     plugin::save();
375     unset($this->attrs['base']);
377     /* Write back to ldap */
378     $ldap= $this->config->get_ldap_link();
379     $ldap->cat($this->dn);
380     $a= $ldap->fetch();
381     
382     if(isset($this->orig_cn)){
383       $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
384     }else{
385       $this-> generate_mysql_entension_entries(true);
386     }
388     if($this->generate_mysql_entension_entries()){
389       if (count($a)){
390         $ldap->cd($this->dn);
391         $ldap->modify($this->attrs);
392         $this->handle_post_events("modify");
393       } else {
394         $ldap->cd($this->config->current['BASE']);
395         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
396         $ldap->cd($this->dn);
397         $ldap->add($this->attrs);
398         $this->handle_post_events("add");
399       }
400     }
401     show_ldap_error($ldap->get_error());
402   }
405 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
406 ?>