Code

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