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");
45 var $view_logged = FALSE;
46 var $orig_cn = "";
47 /*! Objectclasses that this calls handles */
48 var $objectclasses = array("top", "goFonMacro");
50 var $goFonHomeServers = array(); // Contains all available asterisk database server
52 //! The Konstructor
53 /*! Konstructor, load class with attributes of the given dn*/
54 function macro (&$config, $dn= NULL, $parent= NULL)
55 {
56 plugin::plugin ($config, $dn, $parent);
58 /* This is always an account */
59 $this->is_account= TRUE;
61 /* Edit or new one ?*/
62 if ($this->dn == "new"){
63 if(session::is_set('CurrentMainBase')){
64 $this->base = session::get('CurrentMainBase');
65 }else{
66 $ui= get_userinfo();
67 $this->base= dn2base($ui->dn);
68 }
69 } else {
70 $this->orig_cn=$this->cn;
71 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,[^,]+,[^,]+,/", "", $this->dn);
72 }
74 /* Check server configurations
75 * Load all server configuration in $this->goFonHomeServers if available
76 */
77 $a_SETUP= array();
78 if(isset($config->data['SERVERS']['FON'])){
80 /* Set available server */
81 $this->goFonHomeServers = $config->data['SERVERS']['FON'];
83 /* Remove default entry, not necessary here */
84 if(isset($this->goFonHomeServers[0])){
85 unset($this->goFonHomeServers[0]);
86 }
87 }
88 }
91 /*! Execute this plugin */
92 function execute()
93 {
94 /* Call parent execute */
95 plugin::execute();
97 /* Log view */
98 if($this->is_account && !$this->view_logged){
99 $this->view_logged = TRUE;
100 new log("view","gofonmacro/".get_class($this),$this->dn);
101 }
103 /* Variables */
104 $vars = "";
105 $tmp = array();
106 $number = 0;
108 /* Base select dialog */
109 $once = true;
110 foreach($_POST as $name => $value){
111 if(preg_match("/^chooseBase/",$name) && $once){
112 $once = false;
113 $this->dialog = new baseSelectDialog($this->config,$this,$this->allowedBasesToMoveTo());
114 $this->dialog->setCurrentBase($this->base);
115 }
116 }
118 /* Dialog handling */
119 if(is_object($this->dialog)){
120 /* Must be called before save_object */
121 $this->dialog->save_object();
123 if($this->dialog->isClosed()){
124 $this->dialog = false;
125 }elseif($this->dialog->isSelected()){
127 /* A new base was selected, check if it is a valid one */
128 $tmp = $this->get_allowed_bases();
129 if(isset($tmp[$this->dialog->isSelected()])){
130 $this->base = $this->dialog->isSelected();
131 }
133 $this->dialog= false;
134 }else{
135 return($this->dialog->execute());
136 }
137 }
139 /* Fill templating stuff */
140 $smarty= get_smarty();
141 $smarty->assign("bases", $this->get_allowed_bases());
143 $tmp = $this->plInfo();
144 foreach($tmp['plProvidedAcls'] as $name => $translation){
145 $smarty->assign($name."ACL",$this->getacl($name));
146 }
148 if($this->acl_is_writeable("base")){
149 $smarty->assign("baseSelect",true);
150 }else{
151 $smarty->assign("baseSelect",false);
152 }
155 /* Assign all vars to Smarty */
156 foreach($this->attributes as $ar){
157 $smarty->assign($ar, $this->$ar);
158 }
159 /* Checkboxes */
160 $smarty->assign("base_select", $this->base);
161 $smarty->assign("vars", $vars);
163 if($this->goFonMacroVisible){
164 $smarty->assign("goFonMacroVisibleChecked"," checked ");
165 }else{
166 $smarty->assign("goFonMacroVisibleChecked","");
167 }
169 $smarty->assign("cnACL",$this->getacl("cn",$this->initially_was_account));
170 $smarty->assign("cn",$this->cn);
172 /* Ensure that macro content is displayed correctly encoded */
173 $smarty->assign("goFonMacroContent",htmlentities(utf8_decode ($this->goFonMacroContent)));
175 /* Show main page */
176 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
177 }
180 /* This method check if all databases are reachable.
181 * Returns with error message or an empty string on success.
182 *
183 * - Is mysql extension available
184 * - Is every server reachable
185 * - Does the database exists/is accessible
186 */
187 function check_database_accessibility()
188 {
189 /* Check if mysql extension is available */
190 if(!is_callable("mysql_pconnect")){
191 return(sprintf(_("Missing %s PHP extension!"), "mysql"));
192 }
194 /********************
195 * Check all home server
196 ********************/
197 foreach($this->goFonHomeServers as $goFonHomeServer => $cfg_Current){
198 $r_current = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
199 if(!$r_current){
200 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_current));
201 return (sprintf(_("Cannot connect to %s database on server '%s'!"), "GOfon", $cfg_Current['SERVER']));
202 }
203 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
204 if(!$db_current){
205 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_current));
206 mysql_close($r_current);
207 return (sprintf(_("Cannot select %s database on server '%s'!"), "GOfon", $cfg_Current['SERVER']));
208 }
209 }
210 }
213 /* Remove current macro from all asterisk server.
214 * First of all check if we have access to all databases.
215 * - Remove old entries
216 */
217 function remove_from_database($save)
218 {
219 /* Check if all databases are reachable */
220 $str = $this->check_database_accessibility();
221 if($str){
222 return($str);
223 }
225 /* Create query string */
226 $context = addslashes("macro-".$this->cn);
228 /* Remove current macro from each server available */
229 if($save){
230 foreach($this->goFonHomeServers as $dn => $Server){
231 $query = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context='".$context."';";
232 $r_current = @mysql_pconnect($Server['SERVER'],$Server['LOGIN'],$Server['PASSWORD']);
233 $db_current = @mysql_select_db($Server['DB'],$r_current);
234 $res = @mysql_query($query,$r_current);
235 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
236 if(!$res){
237 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_current));
238 return(sprintf(_("Cannot remove macro from '%s'!"),$Server['SERVER']));
239 }
240 @mysql_close($r_current);
241 }
242 }
243 }
246 /* Add current macro to all asterisk server.
247 * First of all check if we have access to all databases.
248 * - Remove old entries
249 * - Add new entries
250 */
251 function add_to_database($save)
252 {
253 /* Check if all databases are reachable */
254 $str = $this->check_database_accessibility();
255 if($str){
256 return($str);
257 }
259 /* Remove old entries first. Else we got duplicated entries */
260 $str = $this->remove_from_database($save);
261 if($str){
262 return($str);
263 }
265 /* Create query string */
266 $context = "macro-".$this->cn;
268 /************
269 * Parse Macro content
270 ************/
271 $sql =
272 "INSERT INTO %TABLENAME% ".
273 " (context,exten,priority,app,appdata) ".
274 " VALUES ";
276 $a_contentLines = split("\n",$this->goFonMacroContent);
277 foreach($a_contentLines as $i_linenum => $s_linestr){
279 /* Remove the 'exten => ' string in front of the macro content line
280 * example line 'exten => s,2,GotoIf(${ARG3}?3:5)'
281 * Remove comments introduced by ;
282 * Skip empty lines
283 */
284 $s_linestr = trim($s_linestr);
285 $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
286 $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
288 if(empty($s_linestr)){
289 continue;
290 }
292 /* A line that passes the check above should look like this
293 * s,1,SetLanguage(de)
294 * 3 parts seperated by ,
295 * If there are more or less parts, abort.
296 * The preg_replace exclude parameters from split ..
297 */
298 $tmp = split(",", $s_linestr,3);
300 /* Check if there are exactly 2 , */
301 # if(substr_count($s_linestr,",") !=2){
302 # return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
303 # }
304 /* Multiple () are not supproted currently ... */
305 if(substr_count($s_linestr,"(") >1 ){
306 return(sprintf(_("Not supported multiple brace in line %s!"),$i_linenum));
307 }
308 if(substr_count($s_linestr,")") >1 ){
309 return(sprintf(_("Not supported multiple brace in line %s!"),$i_linenum));
310 }
311 /* Check if there is an application given */
312 if(empty($tmp[1])){
313 return(sprintf(_("Application missing in line %s!"),$i_linenum));
314 }
315 /* Check if there is an extension given */
316 if(empty($tmp[0])){
317 return(sprintf(_("Extension missing in line %s!"),$i_linenum));
318 }
320 /* Create extension entry for current line
321 * and add this line to an array that will be inserted
322 * to each database.
323 */
324 $exten = addslashes($tmp[0]);
325 $prio = addslashes($tmp[1]);
326 $app = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
327 $para = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
328 $para = preg_replace("/\).*$/","",$para);
329 $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
330 }
332 /* Remove last , from query string */
333 $sql = preg_replace("/,$/","",$sql);
335 /* Save current changes to the database */
336 if($save){
338 /* Macro are spread to each asterisk server */
339 foreach($this->goFonHomeServers as $dn => $cfg){
340 $r_con = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']);
341 $db = @mysql_select_db($cfg['DB'],$r_con);
342 $query = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
343 $res = @mysql_query($query,$r_con);
344 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
345 if(!$res){
346 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_con));
347 return(sprintf(_("Cannot insert new macro on server '%s'!"),$cfg['SERVER']));
348 }
349 @mysql_close($r_con);
350 }
351 }
352 }
355 function save_object()
356 {
357 if (isset($_POST['gofonMacroGenericPosted'])){
359 $old_cn = $this->cn;
360 $old_visible = $this->goFonMacroVisible;
362 /* Create a base backup and reset the
363 base directly after calling plugin::save_object();
364 Base will be set seperatly a few lines below */
365 $base_tmp = $this->base;
366 plugin::save_object();
367 $this->base = $base_tmp;
369 /* Save base, since this is no LDAP attribute */
370 $tmp = $this->get_allowed_bases();
371 if(isset($_POST['base'])){
372 if(isset($tmp[$_POST['base']])){
373 $this->base= $_POST['base'];
374 }
375 }
377 /* Restore old cn if we have insuficient acls to change cn ... */
378 if(!$this->acl_is_writeable("cn",$this->initially_was_account)){
379 $this->cn = $old_cn;
380 }
382 /* check if we are allowed to toggle visibility */
383 if($this->acl_is_writeable("goFonMacroVisible")) {
385 /* Checkbox selected ? */
386 if(isset($_POST['goFonMacroVisible'])) {
387 $this->goFonMacroVisible= 1 ;
388 }else {
389 if(isset($_POST['displayName'])){
390 $this->goFonMacroVisible= 0 ;
391 }
392 }
393 }else{
394 $this->goFonMacroVisible = $old_visible;
395 }
396 }
397 }
400 /*! Check values */
401 function check()
402 {
403 /* Call common method to give check the hook */
404 $message= plugin::check();
406 if(!count($this->goFonHomeServers)){
407 $message[] = _("There is currently no asterisk server defined!");
408 }
410 /* Check if insert/replace is possible and all servers are available */
411 $str = $this->add_to_database(false);
412 if($str){
413 $message[] = $str;
414 }
416 /* Check if cn is already used */
417 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
418 $ldap = $this->config->get_ldap_link();
419 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
420 if($ldap->count()>0){
421 $message[]= _("Name is already in use!");
422 }
423 }
425 /* Check if display name is set */
426 if(empty($this->displayName)){
427 $message[] = _("Display name is not set!");
428 }
429 /* CN is restricted to 20 chars */
430 if(strlen("Makro-".$this->cn)>20 ){
431 $message[]=_("Name can be 20 characters at maximum!");
432 }
434 /* If this macro is still in use we should not change the visible for user flag to invisible */
435 if(!$this->goFonMacroVisible){
436 $ldap = $this->config->get_ldap_link();
437 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
438 while ($val = $ldap->fetch()){
439 if(strstr($val['goFonMacro'][0],$this->dn)){
440 $message[] = _("Macro is still in use!");
441 return($message);
442 }
443 }
444 }
446 if(empty($this->goFonMacroContent)){
447 $message[] = _("Macro is empty!");
448 }
449 return $message;
450 }
453 /*! Remove makro from all given databases
454 * and ldap too.
455 */
456 function remove_from_parent()
457 {
458 $ldap= $this->config->get_ldap_link();
460 /* Skip remove if this macro is still in use */
461 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro", "cn"));
462 while ($val = $ldap->fetch()){
463 if(strstr($val['goFonMacro'][0],$this->dn)){
464 msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $val['cn'][0]), ERROR_DIALOG);
465 return false;
466 }
467 }
469 /* Try to remove from database */
470 if(count($this->goFonHomeServers)){
471 $str = $this->remove_from_database(true);
472 if($str){
473 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
474 return false;
475 }
476 }else{
477 msg_dialog::display(_("Configuration error"), _("There is currently no asterisk server defined!"), WARNING_DIALOG);
478 return false;
479 }
481 /* Remove phone macro */
482 $ldap->rmDir($this->dn);
483 new log("remove","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
484 show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed!"),$this->dn));
486 /* Delete references to object groups */
487 $ldap->cd ($this->config->current['BASE']);
488 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
489 while ($ldap->fetch()){
490 $og= new ogroup($this->config, $ldap->getDN());
491 unset($og->member[$this->dn]);
492 $og->save ();
493 show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed!"),$this->dn));
494 }
495 }
498 /*! Save to LDAP */
499 function save()
500 {
501 plugin::save();
502 unset($this->attrs['base']);
504 /* Try to add entries to databases */
505 $str = $this->add_to_database(true);
506 if($str){
507 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
508 }else{
509 /* Write back to ldap */
510 $ldap= $this->config->get_ldap_link();
511 $ldap->cat($this->dn, array('dn'));
512 $a= $ldap->fetch();
514 if (count($a)){
515 $ldap->cd($this->dn);
516 $this->cleanup();
517 $ldap->modify ($this->attrs);
519 $this->handle_post_events("modify");
520 } else {
521 $ldap->cd($this->config->current['BASE']);
522 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
523 $ldap->cd($this->dn);
524 $ldap->add($this->attrs);
525 $this->handle_post_events("add");
526 }
527 show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonMacro/generic account with dn '%s' failed."),$this->dn));
529 /* Log last action */
530 if($this->initially_was_account){
531 new log("modify","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
532 }else{
533 new log("create","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
534 }
535 }
536 }
539 function getCopyDialog()
540 {
541 $smarty = get_smarty();
542 $smarty->assign("cn" ,$this->cn);
543 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
544 $ret = array();
545 $ret['string'] = $str;
546 $ret['status'] = "";
547 return($ret);
548 }
551 function saveCopyDialog()
552 {
553 if(isset($_POST['cn'])){
554 $this->cn = $_POST['cn'];
555 }
556 }
559 static function plInfo()
560 {
561 return (array(
562 "plShortName" => _("Generic"),
563 "plDescription" => _("Asterisk macro management"),
564 "plSelfModify" => FALSE,
565 "plDepends" => array(),
566 "plPriority" => 0,
567 "plSection" => array("administration"),
568 "plCategory" => array("gofonmacro" => array("description" => _("GOfon macro"),
569 "objectClass" => "gofonMacro")),
571 "plProvidedAcls" => array(
572 "cn" => _("Macro name"),
573 "base" => _("Base"),
574 "description" => _("Description"),
575 "displayName" => _("Display name"),
576 "goFonMacroContent" => _("Macro content and parameter"),
577 "goFonMacroVisible" => _("Visibility flag"))
578 ));
579 }
581 }
582 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
583 ?>