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 {
13 /*! Macro attributes, */
14 var $generate_error= "";
16 /*! The name of the Macro in the openldap drirectory */
17 var $cn = "";
19 /*! Display error once */
20 var $error_shown = false;
22 /*! This ist the variable that contains the description of the macro*/
23 var $description = "";
25 /*! The base of the macro, is used to save the macro in the correct directory tree */
26 var $base = "";
28 /*! This is the name of the macro which the enduser will see, instead of the cn */
29 var $displayName = "";
31 /*! Here is the macro content, the real macroscript */
32 var $goFonMacroContent= "";
34 /*! To allow user to use this macro this var must be true, else false */
35 var $goFonMacroVisible= 0;
37 /*! attribute list for save action */
38 var $attributes = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
39 var $view_logged = FALSE;
40 var $orig_cn = "";
41 /*! Objectclasses that this calls handles */
42 var $objectclasses = array("top", "goFonMacro");
44 var $goFonHomeServers = array(); // Contains all available asterisk database server
46 //! The Konstructor
47 /*! Konstructor, load class with attributes of the given dn*/
48 function macro (&$config, $dn= NULL, $parent= NULL)
49 {
50 plugin::plugin ($config, $dn, $parent);
52 /* This is always an account */
53 $this->is_account= TRUE;
55 /* Edit or new one ?*/
56 if ($this->dn == "new"){
57 if(session::is_set('CurrentMainBase')){
58 $this->base = session::get('CurrentMainBase');
59 }else{
60 $ui= get_userinfo();
61 $this->base= dn2base($ui->dn);
62 }
63 } else {
64 $this->orig_cn=$this->cn;
65 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,[^,]+,[^,]+,/", "", $this->dn);
66 }
68 /* Check server configurations
69 * Load all server configuration in $this->goFonHomeServers if available
70 */
71 $a_SETUP= array();
72 if(isset($config->data['SERVERS']['FON'])){
74 /* Set available server */
75 $this->goFonHomeServers = $config->data['SERVERS']['FON'];
77 /* Remove default entry, not necessary here */
78 if(isset($this->goFonHomeServers[0])){
79 unset($this->goFonHomeServers[0]);
80 }
81 }
82 }
85 /*! Execute this plugin */
86 function execute()
87 {
88 /* Call parent execute */
89 plugin::execute();
91 /* Log view */
92 if($this->is_account && !$this->view_logged){
93 $this->view_logged = TRUE;
94 new log("view","gofonmacro/".get_class($this),$this->dn);
95 }
97 /* Variables */
98 $vars = "";
99 $tmp = array();
100 $number = 0;
102 /* Base select dialog */
103 $once = true;
104 foreach($_POST as $name => $value){
105 if(preg_match("/^chooseBase/",$name) && $once){
106 $once = false;
107 $this->dialog = new baseSelectDialog($this->config,$this,$this->allowedBasesToMoveTo());
108 $this->dialog->setCurrentBase($this->base);
109 }
110 }
112 /* Dialog handling */
113 if(is_object($this->dialog)){
114 /* Must be called before save_object */
115 $this->dialog->save_object();
117 if($this->dialog->isClosed()){
118 $this->dialog = false;
119 }elseif($this->dialog->isSelected()){
121 /* A new base was selected, check if it is a valid one */
122 $tmp = $this->get_allowed_bases();
123 if(isset($tmp[$this->dialog->isSelected()])){
124 $this->base = $this->dialog->isSelected();
125 }
127 $this->dialog= false;
128 }else{
129 return($this->dialog->execute());
130 }
131 }
133 /* Fill templating stuff */
134 $smarty= get_smarty();
135 $smarty->assign("bases", $this->get_allowed_bases());
137 $tmp = $this->plInfo();
138 foreach($tmp['plProvidedAcls'] as $name => $translation){
139 $smarty->assign($name."ACL",$this->getacl($name));
140 }
142 if($this->acl_is_writeable("base")){
143 $smarty->assign("baseSelect",true);
144 }else{
145 $smarty->assign("baseSelect",false);
146 }
149 /* Assign all vars to Smarty */
150 foreach($this->attributes as $ar){
151 $smarty->assign($ar, $this->$ar);
152 }
153 /* Checkboxes */
154 $smarty->assign("base_select", $this->base);
155 $smarty->assign("vars", $vars);
157 if($this->goFonMacroVisible){
158 $smarty->assign("goFonMacroVisibleChecked"," checked ");
159 }else{
160 $smarty->assign("goFonMacroVisibleChecked","");
161 }
163 $smarty->assign("cnACL",$this->getacl("cn",$this->initially_was_account));
164 $smarty->assign("cn",$this->cn);
166 /* Ensure that macro content is displayed correctly encoded */
167 $smarty->assign("goFonMacroContent",htmlentities(utf8_decode ($this->goFonMacroContent)));
169 /* Show main page */
170 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
171 }
174 /* This method check if all databases are reachable.
175 * Returns with error message or an empty string on success.
176 *
177 * - Is mysql extension available
178 * - Is every server reachable
179 * - Does the database exists/is accessible
180 */
181 function check_database_accessibility()
182 {
183 /* Check if mysql extension is available */
184 if(!is_callable("mysql_pconnect")){
185 return(sprintf(_("Missing %s PHP extension!"), "mysql"));
186 }
188 /********************
189 * Check all home server
190 ********************/
191 foreach($this->goFonHomeServers as $goFonHomeServer => $cfg_Current){
192 $r_current = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
193 if(!$r_current){
194 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_current));
195 return (sprintf(_("Cannot connect to %s database on server '%s'!"), "GOfon", $cfg_Current['SERVER']));
196 }
197 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
198 if(!$db_current){
199 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_current));
200 mysql_close($r_current);
201 return (sprintf(_("Cannot select %s database on server '%s'!"), "GOfon", $cfg_Current['SERVER']));
202 }
203 }
204 }
207 /* Remove current macro from all asterisk server.
208 * First of all check if we have access to all databases.
209 * - Remove old entries
210 */
211 function remove_from_database($save)
212 {
213 /* Check if all databases are reachable */
214 $str = $this->check_database_accessibility();
215 if($str){
216 return($str);
217 }
219 /* Create query string */
220 $context = addslashes("macro-".$this->cn);
222 /* Remove current macro from each server available */
223 if($save){
224 foreach($this->goFonHomeServers as $dn => $Server){
225 $query = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context='".$context."';";
226 $r_current = @mysql_pconnect($Server['SERVER'],$Server['LOGIN'],$Server['PASSWORD']);
227 $db_current = @mysql_select_db($Server['DB'],$r_current);
228 $res = @mysql_query($query,$r_current);
229 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
230 if(!$res){
231 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_current));
232 return(sprintf(_("Cannot remove macro from '%s'!"),$Server['SERVER']));
233 }
234 @mysql_close($r_current);
235 }
236 }
237 }
240 /* Add current macro to all asterisk server.
241 * First of all check if we have access to all databases.
242 * - Remove old entries
243 * - Add new entries
244 */
245 function add_to_database($save)
246 {
247 /* Check if all databases are reachable */
248 $str = $this->check_database_accessibility();
249 if($str){
250 return($str);
251 }
253 /* Remove old entries first. Else we got duplicated entries */
254 $str = $this->remove_from_database($save);
255 if($str){
256 return($str);
257 }
259 /* Create query string */
260 $context = "macro-".$this->cn;
262 /************
263 * Parse Macro content
264 ************/
265 $sql =
266 "INSERT INTO %TABLENAME% ".
267 " (context,exten,priority,app,appdata) ".
268 " VALUES ";
270 $a_contentLines = split("\n",$this->goFonMacroContent);
271 foreach($a_contentLines as $i_linenum => $s_linestr){
273 /* Remove the 'exten => ' string in front of the macro content line
274 * example line 'exten => s,2,GotoIf(${ARG3}?3:5)'
275 * Remove comments introduced by ;
276 * Skip empty lines
277 */
278 $s_linestr = trim($s_linestr);
279 $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
280 $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
282 if(empty($s_linestr)){
283 continue;
284 }
286 /* A line that passes the check above should look like this
287 * s,1,SetLanguage(de)
288 * 3 parts seperated by ,
289 * If there are more or less parts, abort.
290 * The preg_replace exclude parameters from split ..
291 */
292 $tmp = split(",", $s_linestr,3);
294 /* Check if there are exactly 2 , */
295 # if(substr_count($s_linestr,",") !=2){
296 # return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
297 # }
298 /* Multiple () are not supproted currently ... */
299 if(substr_count($s_linestr,"(") >1 ){
300 return(sprintf(_("Not supported multiple brace in line %s!"),$i_linenum));
301 }
302 if(substr_count($s_linestr,")") >1 ){
303 return(sprintf(_("Not supported multiple brace in line %s!"),$i_linenum));
304 }
305 /* Check if there is an application given */
306 if(empty($tmp[1])){
307 return(sprintf(_("Application missing in line %s!"),$i_linenum));
308 }
309 /* Check if there is an extension given */
310 if(empty($tmp[0])){
311 return(sprintf(_("Extension missing in line %s!"),$i_linenum));
312 }
314 /* Create extension entry for current line
315 * and add this line to an array that will be inserted
316 * to each database.
317 */
318 $exten = addslashes($tmp[0]);
319 $prio = addslashes($tmp[1]);
320 $app = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
321 $para = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
322 $para = preg_replace("/\).*$/","",$para);
323 $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
324 }
326 /* Remove last , from query string */
327 $sql = preg_replace("/,$/","",$sql);
329 /* Save current changes to the database */
330 if($save){
332 /* Macro are spread to each asterisk server */
333 foreach($this->goFonHomeServers as $dn => $cfg){
334 $r_con = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']);
335 $db = @mysql_select_db($cfg['DB'],$r_con);
336 $query = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
337 $res = @mysql_query($query,$r_con);
338 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
339 if(!$res){
340 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_con));
341 return(sprintf(_("Cannot insert new macro on server '%s'!"),$cfg['SERVER']));
342 }
343 @mysql_close($r_con);
344 }
345 }
346 }
349 function save_object()
350 {
351 if (isset($_POST['gofonMacroGenericPosted'])){
353 $old_cn = $this->cn;
354 $old_visible = $this->goFonMacroVisible;
356 /* Create a base backup and reset the
357 base directly after calling plugin::save_object();
358 Base will be set seperatly a few lines below */
359 $base_tmp = $this->base;
360 plugin::save_object();
361 $this->base = $base_tmp;
363 /* Save base, since this is no LDAP attribute */
364 $tmp = $this->get_allowed_bases();
365 if(isset($_POST['base'])){
366 if(isset($tmp[$_POST['base']])){
367 $this->base= $_POST['base'];
368 }
369 }
371 /* Restore old cn if we have insuficient acls to change cn ... */
372 if(!$this->acl_is_writeable("cn",$this->initially_was_account)){
373 $this->cn = $old_cn;
374 }
376 /* check if we are allowed to toggle visibility */
377 if($this->acl_is_writeable("goFonMacroVisible")) {
379 /* Checkbox selected ? */
380 if(isset($_POST['goFonMacroVisible'])) {
381 $this->goFonMacroVisible= 1 ;
382 }else {
383 if(isset($_POST['displayName'])){
384 $this->goFonMacroVisible= 0 ;
385 }
386 }
387 }else{
388 $this->goFonMacroVisible = $old_visible;
389 }
390 }
391 }
394 /*! Check values */
395 function check()
396 {
397 /* Call common method to give check the hook */
398 $message= plugin::check();
400 if(!count($this->goFonHomeServers)){
401 $message[] = _("There is currently no asterisk server defined!");
402 }
404 /* Check if insert/replace is possible and all servers are available */
405 $str = $this->add_to_database(false);
406 if($str){
407 $message[] = $str;
408 }
410 /* Check if cn is already used */
411 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
412 $ldap = $this->config->get_ldap_link();
413 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
414 if($ldap->count()>0){
415 $message[]= _("Name is already in use!");
416 }
417 }
419 /* Check if display name is set */
420 if(empty($this->displayName)){
421 $message[] = _("Display name is not set!");
422 }
423 /* CN is restricted to 20 chars */
424 if(strlen("Makro-".$this->cn)>20 ){
425 $message[]=_("Name can be 20 characters at maximum!");
426 }
428 /* If this macro is still in use we should not change the visible for user flag to invisible */
429 if(!$this->goFonMacroVisible){
430 $ldap = $this->config->get_ldap_link();
431 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
432 while ($val = $ldap->fetch()){
433 if(strstr($val['goFonMacro'][0],$this->dn)){
434 $message[] = _("Macro is still in use!");
435 return($message);
436 }
437 }
438 }
440 if(empty($this->goFonMacroContent)){
441 $message[] = _("Macro is empty!");
442 }
443 return $message;
444 }
447 /*! Remove makro from all given databases
448 * and ldap too.
449 */
450 function remove_from_parent()
451 {
452 $ldap= $this->config->get_ldap_link();
454 /* Skip remove if this macro is still in use */
455 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro", "cn"));
456 while ($val = $ldap->fetch()){
457 if(strstr($val['goFonMacro'][0],$this->dn)){
458 msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $val['cn'][0]), ERROR_DIALOG);
459 return false;
460 }
461 }
463 /* Try to remove from database */
464 if(count($this->goFonHomeServers)){
465 $str = $this->remove_from_database(true);
466 if($str){
467 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
468 return false;
469 }
470 }else{
471 msg_dialog::display(_("Configuration error"), _("There is currently no asterisk server defined!"), WARNING_DIALOG);
472 return false;
473 }
475 /* Remove phone macro */
476 $ldap->rmDir($this->dn);
477 new log("remove","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
478 if (!$ldap->success()){
479 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
480 }
482 /* Delete references to object groups */
483 $ldap->cd ($this->config->current['BASE']);
484 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
485 while ($ldap->fetch()){
486 $og= new ogroup($this->config, $ldap->getDN());
487 unset($og->member[$this->dn]);
488 $og->save ();
489 if (!$ldap->success()){
490 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
491 }
492 }
493 }
496 /*! Save to LDAP */
497 function save()
498 {
499 plugin::save();
500 unset($this->attrs['base']);
502 /* Try to add entries to databases */
503 $str = $this->add_to_database(true);
504 if($str){
505 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
506 }else{
507 /* Write back to ldap */
508 $ldap= $this->config->get_ldap_link();
509 $ldap->cat($this->dn, array('dn'));
510 $a= $ldap->fetch();
512 if (count($a)){
513 $ldap->cd($this->dn);
514 $this->cleanup();
515 $ldap->modify ($this->attrs);
517 $this->handle_post_events("modify");
518 } else {
519 $ldap->cd($this->config->current['BASE']);
520 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
521 $ldap->cd($this->dn);
522 $ldap->add($this->attrs);
523 $this->handle_post_events("add");
524 }
525 if (!$ldap->success()){
526 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
527 }
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 ?>