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(msgPool::missingext("php-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 ( msgPool::dbconnect("GOfon",@mysql_error(),$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 ( msgPool::dbselect("GOfon",@mysql_error(),$cfg_Current['DB']));
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 ( msgPool::dbquery("GOfon",@mysql_error($r_current),$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 /* Multiple () are not supproted currently ... */
295 if(substr_count($s_linestr,"(") >1 ){
296 return(sprintf(_("Not supported multiple brace in line %s!"),$i_linenum));
297 }
298 if(substr_count($s_linestr,")") >1 ){
299 return(sprintf(_("Not supported multiple brace in line %s!"),$i_linenum));
300 }
301 /* Check if there is an application given */
302 if(empty($tmp[1])){
303 return(sprintf(_("Application missing in line %s!"),$i_linenum));
304 }
305 /* Check if there is an extension given */
306 if(empty($tmp[0])){
307 return(sprintf(_("Extension missing in line %s!"),$i_linenum));
308 }
310 /* Create extension entry for current line
311 * and add this line to an array that will be inserted
312 * to each database.
313 */
314 $exten = addslashes($tmp[0]);
315 $prio = addslashes($tmp[1]);
316 $app = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
317 $para = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
318 $para = preg_replace("/\).*$/","",$para);
319 $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
320 }
322 /* Remove last , from query string */
323 $sql = preg_replace("/,$/","",$sql);
325 /* Save current changes to the database */
326 if($save){
328 /* Macro are spread to each asterisk server */
329 foreach($this->goFonHomeServers as $dn => $cfg){
330 $r_con = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']);
331 $db = @mysql_select_db($cfg['DB'],$r_con);
332 $query = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
333 $res = @mysql_query($query,$r_con);
334 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
335 if(!$res){
336 new log("debug","gofonmacro/".get_class($this),"",array(),@mysql_error($r_con));
337 }
338 @mysql_close($r_con);
339 }
340 }
341 }
344 function save_object()
345 {
346 if (isset($_POST['gofonMacroGenericPosted'])){
348 $old_cn = $this->cn;
349 $old_visible = $this->goFonMacroVisible;
351 /* Create a base backup and reset the
352 base directly after calling plugin::save_object();
353 Base will be set seperatly a few lines below */
354 $base_tmp = $this->base;
355 plugin::save_object();
356 $this->base = $base_tmp;
358 /* Save base, since this is no LDAP attribute */
359 $tmp = $this->get_allowed_bases();
360 if(isset($_POST['base'])){
361 if(isset($tmp[$_POST['base']])){
362 $this->base= $_POST['base'];
363 }
364 }
366 /* Restore old cn if we have insuficient acls to change cn ... */
367 if(!$this->acl_is_writeable("cn",$this->initially_was_account)){
368 $this->cn = $old_cn;
369 }
371 /* check if we are allowed to toggle visibility */
372 if($this->acl_is_writeable("goFonMacroVisible")) {
374 /* Checkbox selected ? */
375 if(isset($_POST['goFonMacroVisible'])) {
376 $this->goFonMacroVisible= 1 ;
377 }else {
378 if(isset($_POST['displayName'])){
379 $this->goFonMacroVisible= 0 ;
380 }
381 }
382 }else{
383 $this->goFonMacroVisible = $old_visible;
384 }
385 }
386 }
389 /*! Check values */
390 function check()
391 {
392 /* Call common method to give check the hook */
393 $message= plugin::check();
395 if(!count($this->goFonHomeServers)){
396 $message[] = msgPool::noserver(_("GOfon"));
397 }
399 /* Check if insert/replace is possible and all servers are available */
400 $str = $this->add_to_database(false);
401 if($str){
402 $message[] = $str;
403 }
405 /* Check if cn is already used */
406 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
407 $ldap = $this->config->get_ldap_link();
408 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
409 if($ldap->count()>0){
410 $message[]= msgPool::duplicated(_("Name"));
411 }
412 }
414 /* Check if display name is set */
415 if(empty($this->displayName)){
416 $message[] = msgPool::required(_("Name"));
417 }
418 /* CN is restricted to 20 chars */
419 if(strlen("Makro-".$this->cn)>20 ){
420 $message[]=_("Name can be 20 characters at maximum!");
421 }
423 /* If this macro is still in use we should not change the visible for user flag to invisible */
424 if(!$this->goFonMacroVisible){
425 $ldap = $this->config->get_ldap_link();
426 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
427 while ($val = $ldap->fetch()){
428 if(strstr($val['goFonMacro'][0],$this->dn)){
429 $message[] = _("Macro is still in use!");
430 return($message);
431 }
432 }
433 }
435 if(empty($this->goFonMacroContent)){
436 $message[] = _("Macro is empty!");
437 }
438 return $message;
439 }
442 /*! Remove makro from all given databases
443 * and ldap too.
444 */
445 function remove_from_parent()
446 {
447 $ldap= $this->config->get_ldap_link();
449 /* Skip remove if this macro is still in use */
450 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro", "cn"));
451 while ($val = $ldap->fetch()){
452 if(strstr($val['goFonMacro'][0],$this->dn)){
453 msg_dialog::display(_("Error"), sprintf(_("Cannot delete entry because it is still in use by '%s'!"), $val['cn'][0]), ERROR_DIALOG);
454 return false;
455 }
456 }
458 /* Try to remove from database */
459 if(count($this->goFonHomeServers)){
460 $str = $this->remove_from_database(true);
461 if($str){
462 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
463 return false;
464 }
465 }else{
466 msg_dialog::display(_("Configuration error"), msgPool::noserver(_("GOfon")), WARNING_DIALOG);
467 return false;
468 }
470 /* Remove phone macro */
471 $ldap->rmDir($this->dn);
472 new log("remove","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
473 if (!$ldap->success()){
474 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
475 }
477 /* Delete references to object groups */
478 $ldap->cd ($this->config->current['BASE']);
479 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
480 while ($ldap->fetch()){
481 $og= new ogroup($this->config, $ldap->getDN());
482 unset($og->member[$this->dn]);
483 $og->save ();
484 if (!$ldap->success()){
485 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
486 }
487 }
488 }
491 /*! Save to LDAP */
492 function save()
493 {
494 plugin::save();
495 unset($this->attrs['base']);
497 /* Try to add entries to databases */
498 $str = $this->add_to_database(true);
499 if($str){
500 msg_dialog::display(_("Error"), $str, ERROR_DIALOG);
501 }else{
502 /* Write back to ldap */
503 $ldap= $this->config->get_ldap_link();
504 $ldap->cat($this->dn, array('dn'));
505 $a= $ldap->fetch();
507 if (count($a)){
508 $ldap->cd($this->dn);
509 $this->cleanup();
510 $ldap->modify ($this->attrs);
512 $this->handle_post_events("modify");
513 } else {
514 $ldap->cd($this->config->current['BASE']);
515 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
516 $ldap->cd($this->dn);
517 $ldap->add($this->attrs);
518 $this->handle_post_events("add");
519 }
520 if (!$ldap->success()){
521 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
522 }
524 /* Log last action */
525 if($this->initially_was_account){
526 new log("modify","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
527 }else{
528 new log("create","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
529 }
530 }
531 }
534 function getCopyDialog()
535 {
536 $smarty = get_smarty();
537 $smarty->assign("cn" ,$this->cn);
538 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
539 $ret = array();
540 $ret['string'] = $str;
541 $ret['status'] = "";
542 return($ret);
543 }
546 function saveCopyDialog()
547 {
548 if(isset($_POST['cn'])){
549 $this->cn = $_POST['cn'];
550 }
551 }
554 static function plInfo()
555 {
556 return (array(
557 "plShortName" => _("Generic"),
558 "plDescription" => _("Asterisk macro management"),
559 "plSelfModify" => FALSE,
560 "plDepends" => array(),
561 "plPriority" => 0,
562 "plSection" => array("administration"),
563 "plCategory" => array("gofonmacro" => array("description" => _("GOfon macro"),
564 "objectClass" => "gofonMacro")),
566 "plProvidedAcls" => array(
567 "cn" => _("Macro name"),
568 "base" => _("Base"),
569 "description" => _("Description"),
570 "displayName" => _("Display name"),
571 "goFonMacroContent" => _("Macro content and parameter"),
572 "goFonMacroVisible" => _("Visibility flag"))
573 ));
574 }
576 }
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>