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 /* Base select dialog */
108 $once = true;
109 foreach($_POST as $name => $value){
110 if(preg_match("/^chooseBase/",$name) && $once){
111 $once = false;
112 $this->dialog = new baseSelectDialog($this->config, $this);
113 $this->dialog->setCurrentBase($this->base);
114 }
115 }
117 /* Dialog handling */
118 if(is_object($this->dialog)){
119 /* Must be called before save_object */
120 $this->dialog->save_object();
122 if($this->dialog->isClosed()){
123 $this->dialog = false;
124 }elseif($this->dialog->isSelected()){
125 $this->base = $this->dialog->isSelected();
126 $this->dialog= false;
127 }else{
128 return($this->dialog->execute());
129 }
130 }
134 /* Fill templating stuff */
135 $smarty= get_smarty();
136 $smarty->assign("bases", $this->config->idepartments);
138 /* Assign all vars to Smarty */
139 foreach($this->attributes as $ar){
140 $smarty->assign($ar, $this->$ar);
141 $smarty->assign($ar."ACL", chkacl($this->acl,$ar));
142 }
143 /* Checkboxes */
144 $smarty->assign("base_select", $this->base);
145 $smarty->assign("vars", $vars);
147 if($this->goFonMacroVisible){
148 $smarty->assign("goFonMacroVisibleChecked"," checked ");
149 }else{
150 $smarty->assign("goFonMacroVisibleChecked","");
151 }
153 if(isset($_POST['goFonMacroVisible'])) {
154 $this->goFonMacroVisible= 1 ;
155 $smarty->assign("goFonMacroVisibleChecked"," checked ");
156 }else {
157 if(isset($_POST['displayName'])){
158 $this->goFonMacroVisible= 0 ;
159 $smarty->assign("goFonMacroVisibleChecked","");
160 }
161 }
163 if(!$this->is_new){
164 $smarty->assign("disable_cn"," disabled ");
165 $smarty->assign("cn",$this->orig_cn);
166 }else{
167 $smarty->assign("disable_cn"," ");
168 $smarty->assign("cn",$this->cn);
169 }
170 $this->generate_mysql_entension_entries();
171 /* Show main page */
172 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
173 }
175 /*!
176 Remove this Object
177 */
178 function remove_from_parent()
179 {
180 $ldap= $this->config->get_ldap_link();
182 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
184 while ($val = $ldap->fetch()){
185 if(isset($val['goFonMacro'])){
186 if(strstr($val['goFonMacro'][0],$this->dn)){
187 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
188 return false;
189 }
190 }
191 }
193 $ldap->rmDir($this->dn);
194 show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
196 if(isset($this->orig_cn)){
197 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
198 }else{
199 $this->generate_mysql_entension_entries(false,true);
200 }
202 /* Delete references to object groups */
203 $ldap->cd ($this->config->current['BASE']);
204 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
205 while ($ldap->fetch()){
206 $og= new ogroup($this->config, $ldap->getDN());
207 unset($og->member[$this->dn]);
208 $og->save ();
209 show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
210 }
211 }
213 // Generate MySQL Syntax
214 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
216 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
217 return(true);
218 }
220 if(!is_callable("mysql_connect")){
221 if((!$this->error_shown)&&($save)){
222 print_red(_("There is no mysql extension available in your php setup, can't save any changes to asterisk db."));
223 $this->error_shown = true;
224 }
225 return(true);
226 }
228 // Get Configuration for Mysql database Server
229 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
231 // Connect to DB server
232 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
234 // Check if we are connected correctly
235 if(!$r_con){
236 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
237 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
238 gosa_log(mysql_error());
239 return false;
240 }
242 // Select database for Extensions
243 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
245 // Test if we have the database selected correctly
246 if(!$db){
247 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
248 gosa_log(mysql_error());
249 return false;
250 }
253 // Context def
254 $context = "macro-".$this->cn;
256 // Parse Content if we connected correctly
257 if($db && $r_con ){
259 // Split Content into lines
260 $a_contentLines = split ("\n",$this->goFonMacroContent);
262 // Foreach single line ...
263 foreach($a_contentLines as $i_linenum => $s_linestr){
265 // Remove unwanted exten => tag
266 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
268 // Remove spaces
269 $s_linestr = trim ( $s_linestr);
271 // Skip empty lines
272 if ($s_linestr == ""){
273 continue;
274 }
276 // If not empty or linebreak at [0]
277 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
279 // Set general SQL statement
280 $SQL[$i_linenum] =
281 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
282 " (context,exten,priority,app,appdata) ".
283 " VALUES ".
284 " (";
286 // Parse linestr to entry data
287 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
289 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
291 if(!isset($tmp[2])){
292 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
293 return false;
294 }
295 $exten = $tmp[0];
296 $prio = $tmp[1];
297 $app = $tmp[2];
298 $para = $linecontents;
299 $para = preg_replace("/^.*\(/","",$para);
300 $para = preg_replace("/\)$/","",$para);
302 // Append SQL syntax
303 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
304 }
305 }
307 if(($save)||($delete_only)){
308 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
309 if(!$res){
310 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
311 gosa_log(mysql_error());
312 return false;
313 }
314 $cnt = mysql_fetch_row($res);
315 $cnt = $cnt[0];
317 if($cnt != 0) {
318 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
319 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
320 gosa_log(mysql_error());
321 return false;
322 }
323 }
325 if($remove_old_macroname!="false"){
326 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
327 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
328 gosa_log(mysql_error());
329 return false;
330 }
331 }
335 }
337 // We want to save this
338 if(($save)&&(isset($SQL))){
339 foreach($SQL as $entry){
340 if(!mysql_query($entry)){
341 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
342 gosa_log(mysql_error());
343 return false;
344 }
345 }
346 }
347 }
348 if((isset($r_con))&&($r_con)){
349 @mysql_close($r_con);
350 }
351 return true;
352 }
357 /*!
358 Save data to object
359 */
360 function save_object()
361 {
362 if (isset($_POST['displayName'])){
363 plugin::save_object();
364 }
365 foreach($this->attributes as $attr){
366 if(isset($_POST[$attr])){
367 $this->$attr= $_POST [$attr];
368 }
369 }
370 }
373 /*!
374 Check values
375 */
376 function check()
377 {
378 /* Call common method to give check the hook */
379 $message= plugin::check();
381 if(!$this->generate_mysql_entension_entries()){
382 $message[] = $this->generate_error;
383 }
385 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
386 $ldap = $this->config->get_ldap_link();
387 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
388 if($ldap->count()>0){
389 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
390 }
391 }
393 if(empty($this->displayName)){
394 $message[] = _("You must specify the 'Display Name' in order to save this macro");
395 }
397 if(strlen("Makro-".$this->cn)>20 ){
398 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
399 }
401 foreach($this->attributes as $attr){
402 if(chkacl($this->acl,"edit")){
403 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
404 return(array($str));
405 }
406 }
408 if(count(split("\n",$this->goFonMacroContent))>100){
409 $message[] = _("Makro length must be lower than 100 lines");
410 }
412 /*Some stupid IE fixes again*/
413 if(empty ($this->base)) {
414 $message[] = _("Please choose a valid base.");
415 }
417 return $message;
418 }
421 /*!
422 Save to LDAP
423 */
424 function save()
425 {
426 /* Post checks */
427 $this->execute();
429 plugin::save();
430 unset($this->attrs['base']);
432 /* Write back to ldap */
433 $ldap= $this->config->get_ldap_link();
434 $ldap->cat($this->dn, array('dn'));
435 $a= $ldap->fetch();
437 if(isset($this->orig_cn)){
438 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
439 }else{
440 $this-> generate_mysql_entension_entries(true);
441 }
443 if($this->generate_mysql_entension_entries()){
444 if (count($a)){
445 $ldap->cd($this->dn);
446 $this->cleanup();
447 $ldap->modify ($this->attrs);
449 $this->handle_post_events("modify");
450 } else {
451 $ldap->cd($this->config->current['BASE']);
452 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
453 $ldap->cd($this->dn);
454 $ldap->add($this->attrs);
455 $this->handle_post_events("add");
456 }
457 show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonMacro/generic account with dn '%s' failed."),$this->dn));
458 }
459 }
462 /* Return plugin informations for acl handling */
463 function plInfo()
464 {
465 return (array(
466 "plDescription" => _("Asterisk macro management"),
467 "plSelfModify" => TRUE,
468 "plDepends" => array("objectClass" => "goFonMacro","description" => _("Macro")),
470 "base" => _("Base"),
471 "description" => _("Description"),
472 "displayName" => _("Display name"),
473 "goFonMacroContent" => _("Macro content"),
474 "goFonMacroVisible" => _("Visibility flag")));
475 }
476 }
477 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
478 ?>