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");
19 /*! Macro attributes, */
20 var $generate_error= "";
22 /*! The name of the Macro in the openldap drirectory */
23 var $cn = "";
25 /*! Display error once */
26 var $error_shown = false;
28 /*! This ist the variable that contains the description of the macro*/
29 var $description = "";
31 /*! The base of the macro, is used to save the macro in the correct directory tree */
32 var $base = "";
34 /*! This is the name of the macro which the enduser will see, instead of the cn */
35 var $displayName = "";
37 /*! Here is the macro content, the real macroscript */
38 var $goFonMacroContent= "";
40 /*! To allow user to use this macro this var must be true, else false */
41 var $goFonMacroVisible= 0;
43 /*! attribute list for save action */
44 var $attributes = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
46 var $is_new=false;
47 var $orig_cn = "";
48 /*! Objectclasses that this calls handles */
49 var $objectclasses = array("top", "goFonMacro");
51 //! The Konstructor
52 /*! Konstructor, load class with attributes of the given dn*/
53 function macro ($config, $dn= NULL)
54 {
55 plugin::plugin ($config, $dn);
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['CurrentMainBase'])){
68 $this->base = $_SESSION['CurrentMainBase'];
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= preg_replace("/ou=macros,ou=asterisk,ou=configs,ou=systems,/","",dn2base($this->dn));
79 }
80 $ui= get_userinfo();
81 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
82 $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
83 }
85 /*! Execute this plugin */
86 function execute()
87 {
88 /* Call parent execute */
89 plugin::execute();
91 /* Variables */
92 $vars = "";
93 $tmp = array();
94 $number = 0;
96 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
97 print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database."));
98 }
100 /* Do we represent a valid group? */
101 if (!$this->is_account && $this->parent == NULL){
102 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
103 _("This 'dn' is no phone macro.")."</b>";
104 return ($display);
105 }
107 /* Fill templating stuff */
108 $smarty= get_smarty();
109 $smarty->assign("bases", $this->config->idepartments);
111 /* Assign all vars to Smarty */
112 foreach($this->attributes as $ar){
113 $smarty->assign($ar, $this->$ar);
114 }
115 /* Checkboxes */
116 $smarty->assign("base_select", $this->base);
117 $smarty->assign("vars", $vars);
119 if($this->goFonMacroVisible){
120 $smarty->assign("goFonMacroVisibleChecked"," checked ");
121 }else{
122 $smarty->assign("goFonMacroVisibleChecked","");
123 }
125 if(isset($_POST['goFonMacroVisible'])) {
126 $this->goFonMacroVisible= 1 ;
127 $smarty->assign("goFonMacroVisibleChecked"," checked ");
128 }else {
129 if(isset($_POST['displayName'])){
130 $this->goFonMacroVisible= 0 ;
131 $smarty->assign("goFonMacroVisibleChecked","");
132 }
133 }
135 if(!$this->is_new){
136 $smarty->assign("disable_cn"," disabled ");
137 $smarty->assign("cn",$this->orig_cn);
138 }else{
139 $smarty->assign("disable_cn"," ");
140 $smarty->assign("cn",$this->cn);
141 }
142 $this->generate_mysql_entension_entries();
143 /* Show main page */
144 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
145 }
147 /*!
148 Remove this Object
149 */
150 function remove_from_parent()
151 {
152 $ldap= $this->config->get_ldap_link();
154 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
156 while ($val = $ldap->fetch()){
157 if(isset($val['goFonMacro'])){
158 if(strstr($val['goFonMacro'][0],$this->dn)){
159 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
160 return false;
161 }
162 }
163 }
165 $ldap->rmDir($this->dn);
166 show_ldap_error($ldap->get_error(), _("Removing phone macro failed"));
167 if(isset($this->orig_cn)){
168 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
169 }else{
170 $this->generate_mysql_entension_entries(false,true);
171 }
173 /* Delete references to object groups */
174 $ldap->cd ($this->config->current['BASE']);
175 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
176 while ($ldap->fetch()){
177 $og= new ogroup($this->config, $ldap->getDN());
178 unset($og->member[$this->dn]);
179 $og->save ();
180 show_ldap_error($ldap->get_error(), _("Removing phone macro reverences failed"));
181 }
182 }
184 // Generate MySQL Syntax
185 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
187 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
188 return(true);
189 }
191 if(!is_callable("mysql_connect")){
192 if((!$this->error_shown)&&($save)){
193 print_red(_("There is no mysql extension available in your php setup, can't save any changes to asterisk db."));
194 $this->error_shown = true;
195 }
196 return(true);
197 }
199 // Get Configuration for Mysql database Server
200 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
202 // Connect to DB server
203 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
205 // Check if we are connected correctly
206 if(!$r_con){
207 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
208 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
209 gosa_log(mysql_error());
210 return false;
211 }
213 // Select database for Extensions
214 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
216 // Test if we have the database selected correctly
217 if(!$db){
218 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
219 gosa_log(mysql_error());
220 return false;
221 }
224 // Context def
225 $context = "macro-".$this->cn;
227 // Parse Content if we connected correctly
228 if($db && $r_con ){
230 // Split Content into lines
231 $a_contentLines = split ("\n",$this->goFonMacroContent);
233 // Foreach single line ...
234 foreach($a_contentLines as $i_linenum => $s_linestr){
236 // Remove unwanted exten => tag
237 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
239 // Remove spaces
240 $s_linestr = trim ( $s_linestr);
242 // Skip empty lines
243 if ($s_linestr == ""){
244 continue;
245 }
247 // If not empty or linebreak at [0]
248 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
250 // Set general SQL statement
251 $SQL[$i_linenum] =
252 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
253 " (context,exten,priority,app,appdata) ".
254 " VALUES ".
255 " (";
257 // Parse linestr to entry data
258 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
260 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
262 if(!isset($tmp[2])){
263 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
264 return false;
265 }
266 $exten = $tmp[0];
267 $prio = $tmp[1];
268 $app = $tmp[2];
269 $para = $linecontents;
270 $para = preg_replace("/^.*\(/","",$para);
271 $para = preg_replace("/\)$/","",$para);
273 // Append SQL syntax
274 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
275 }
276 }
278 if(($save)||($delete_only)){
279 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
280 if(!$res){
281 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
282 gosa_log(mysql_error());
283 return false;
284 }
285 $cnt = mysql_fetch_row($res);
286 $cnt = $cnt[0];
288 if($cnt != 0) {
289 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
290 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
291 gosa_log(mysql_error());
292 return false;
293 }
294 }
296 if($remove_old_macroname!="false"){
297 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
298 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
299 gosa_log(mysql_error());
300 return false;
301 }
302 }
306 }
308 // We want to save this
309 if(($save)&&(isset($SQL))){
310 foreach($SQL as $entry){
311 if(!mysql_query($entry)){
312 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
313 gosa_log(mysql_error());
314 return false;
315 }
316 }
317 }
318 }
319 if((isset($r_con))&&($r_con)){
320 @mysql_close($r_con);
321 }
322 return true;
323 }
328 /*!
329 Save data to object
330 */
331 function save_object()
332 {
333 if (isset($_POST['displayName'])){
334 plugin::save_object();
335 }
336 foreach($this->attributes as $attr){
337 if(isset($_POST[$attr])){
338 $this->$attr= $_POST [$attr];
339 }
340 }
341 }
344 /*!
345 Check values
346 */
347 function check()
348 {
349 /* Call common method to give check the hook */
350 $message= plugin::check();
352 if(!$this->generate_mysql_entension_entries()){
353 $message[] = $this->generate_error;
354 }
356 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
357 $ldap = $this->config->get_ldap_link();
358 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
359 if($ldap->count()>0){
360 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
361 }
362 }
364 if(empty($this->displayName)){
365 $message[] = _("You must specify the 'Display Name' in order to save this macro");
366 }
368 if(strlen("Makro-".$this->cn)>20 ){
369 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
370 }
372 foreach($this->attributes as $attr){
373 if(chkacl($this->acl,"edit")){
374 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
375 return(array($str));
376 }
377 }
379 if(count(split("\n",$this->goFonMacroContent))>100){
380 $message[] = _("Makro length must be lower than 100 lines");
381 }
383 /*Some stupid IE fixes again*/
384 if(empty ($this->base)) {
385 $message[] = _("Please choose a valid base.");
386 }
388 return $message;
389 }
392 /*!
393 Save to LDAP
394 */
395 function save()
396 {
397 /* Post checks */
398 $this->execute();
400 plugin::save();
401 unset($this->attrs['base']);
403 /* Write back to ldap */
404 $ldap= $this->config->get_ldap_link();
405 $ldap->cat($this->dn, array('dn'));
406 $a= $ldap->fetch();
408 if(isset($this->orig_cn)){
409 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
410 }else{
411 $this-> generate_mysql_entension_entries(true);
412 }
414 if($this->generate_mysql_entension_entries()){
415 if (count($a)){
416 $ldap->cd($this->dn);
417 $this->cleanup();
418 $ldap->modify ($this->attrs);
420 $this->handle_post_events("modify");
421 } else {
422 $ldap->cd($this->config->current['BASE']);
423 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
424 $ldap->cd($this->dn);
425 $ldap->add($this->attrs);
426 $this->handle_post_events("add");
427 }
428 show_ldap_error($ldap->get_error(), _("Saving phone macro failed"));
429 }
430 }
432 }
433 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
434 ?>