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(isset($_SESSION['CurrentMainBase'])){
64 $this->base = $_SESSION['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(array_key_exists('config',$_SESSION) &&
79 array_key_exists('SERVERS',$_SESSION['config']->data) &&
80 count($_SESSION['config']->data['SERVERS']['FON']) &&
81 array_key_exists('FON',$_SESSION['config']->data['SERVERS'])) {
83 /* Set available server */
84 $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
86 /* Remove default entry, not necessary here */
87 if(isset($this->goFonHomeServers[0])){
88 unset($this->goFonHomeServers[0]);
89 }
90 }
91 }
94 /*! Execute this plugin */
95 function execute()
96 {
97 /* Call parent execute */
98 plugin::execute();
100 /* Log view */
101 if($this->is_account && !$this->view_logged){
102 $this->view_logged = TRUE;
103 @log::log("view","gofonmacro/".get_class($this),$this->dn);
104 }
106 /* Variables */
107 $vars = "";
108 $tmp = array();
109 $number = 0;
111 /* Base select dialog */
112 $once = true;
113 foreach($_POST as $name => $value){
114 if(preg_match("/^chooseBase/",$name) && $once){
115 $once = false;
116 $this->dialog = new baseSelectDialog($this->config,$this,$this->allowedBasesToMoveTo());
117 $this->dialog->setCurrentBase($this->base);
118 }
119 }
121 /* Dialog handling */
122 if(is_object($this->dialog)){
123 /* Must be called before save_object */
124 $this->dialog->save_object();
126 if($this->dialog->isClosed()){
127 $this->dialog = false;
128 }elseif($this->dialog->isSelected()){
130 /* A new base was selected, check if it is a valid one */
131 $tmp = $this->get_allowed_bases();
132 if(isset($tmp[$this->dialog->isSelected()])){
133 $this->base = $this->dialog->isSelected();
134 }
136 $this->dialog= false;
137 }else{
138 return($this->dialog->execute());
139 }
140 }
142 /* Fill templating stuff */
143 $smarty= get_smarty();
144 $smarty->assign("bases", $this->get_allowed_bases());
146 $tmp = $this->plInfo();
147 foreach($tmp['plProvidedAcls'] as $name => $translation){
148 $smarty->assign($name."ACL",$this->getacl($name));
149 }
151 if($this->acl_is_writeable("base")){
152 $smarty->assign("baseSelect",true);
153 }else{
154 $smarty->assign("baseSelect",false);
155 }
158 /* Assign all vars to Smarty */
159 foreach($this->attributes as $ar){
160 $smarty->assign($ar, $this->$ar);
161 }
162 /* Checkboxes */
163 $smarty->assign("base_select", $this->base);
164 $smarty->assign("vars", $vars);
166 if($this->goFonMacroVisible){
167 $smarty->assign("goFonMacroVisibleChecked"," checked ");
168 }else{
169 $smarty->assign("goFonMacroVisibleChecked","");
170 }
172 $smarty->assign("cnACL",$this->getacl("cn",$this->initially_was_account));
173 $smarty->assign("cn",$this->cn);
175 /* Ensure that macro content is displayed correctly encoded */
176 $smarty->assign("goFonMacroContent",htmlentities(utf8_decode ($this->goFonMacroContent)));
178 /* Show main page */
179 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
180 }
183 /* This method check if all databases are reachable.
184 * Returns with error message or an empty string on success.
185 *
186 * - Is mysql extension available
187 * - Is every server reachable
188 * - Does the database exists/is accessible
189 */
190 function check_database_accessibility()
191 {
192 /* Check if mysql extension is available */
193 if(!is_callable("mysql_pconnect")){
194 return(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
195 }
197 /********************
198 * Check all home server
199 ********************/
200 foreach($this->goFonHomeServers as $goFonHomeServer => $cfg_Current){
201 $r_current = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
202 if(!$r_current){
203 gosa_log(@mysql_error($r_current));
204 return(sprintf(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
205 $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
206 }
207 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
208 if(!$db_current){
209 gosa_log(@mysql_error($r_current));
210 mysql_close($r_current);
211 return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$cfg_Current['SERVER']));
212 }
213 }
214 }
217 /* Remove current macro from all asterisk server.
218 * First of all check if we have access to all databases.
219 * - Remove old entries
220 */
221 function remove_from_database($save)
222 {
223 /* Check if all databases are reachable */
224 $str = $this->check_database_accessibility();
225 if($str){
226 return($str);
227 }
229 /* Create query string */
230 $context = addslashes("macro-".$this->cn);
232 /* Remove current macro from each server available */
233 if($save){
234 foreach($this->goFonHomeServers as $dn => $Server){
235 $query = "DELETE FROM ".$Server['EXT_TABLE']." WHERE context='".$context."';";
236 $r_current = @mysql_pconnect($Server['SERVER'],$Server['LOGIN'],$Server['PASSWORD']);
237 $db_current = @mysql_select_db($Server['DB'],$r_current);
238 $res = @mysql_query($query,$r_current);
239 if(!$res){
240 gosa_log(@mysql_error($r_current));
241 return(sprintf(_("Removing macro from '%s' failed. Check GOsa log for mysql error."),$Server['SERVER']));
242 }
243 @mysql_close($r_current);
244 }
245 }
246 }
249 /* Add current macro to all asterisk server.
250 * First of all check if we have access to all databases.
251 * - Remove old entries
252 * - Add new entries
253 */
254 function add_to_database($save)
255 {
256 /* Check if all databases are reachable */
257 $str = $this->check_database_accessibility();
258 if($str){
259 return($str);
260 }
262 /* Remove old entries first. Else we got duplicated entries */
263 $str = $this->remove_from_database($save);
264 if($str){
265 return($str);
266 }
268 /* Create query string */
269 $context = "macro-".$this->cn;
271 /************
272 * Parse Macro content
273 ************/
274 $sql =
275 "INSERT INTO %TABLENAME% ".
276 " (context,exten,priority,app,appdata) ".
277 " VALUES ";
279 $a_contentLines = split("\n",$this->goFonMacroContent);
280 foreach($a_contentLines as $i_linenum => $s_linestr){
282 /* Remove the 'exten => ' string in front of the macro content line
283 * example line 'exten => s,2,GotoIf(${ARG3}?3:5)'
284 * Remove comments introduced by ;
285 * Skip empty lines
286 */
287 $s_linestr = trim($s_linestr);
288 $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
289 $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
291 if(empty($s_linestr)){
292 continue;
293 }
295 /* A line that passes the check above should look like this
296 * s,1,SetLanguage(de)
297 * 3 parts seperated by ,
298 * If there are more or less parts, abort.
299 * The preg_replace exclude parameters from split ..
300 */
301 $tmp = split(",", $s_linestr,3);
303 /* Check if there are exactly 2 , */
304 # if(substr_count($s_linestr,",") !=2){
305 # return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
306 # }
307 /* Multiple () are not supproted currently ... */
308 if(substr_count($s_linestr,"(") >1 ){
309 return(sprintf(_("More than one '(' is currently not supported. Line : '%s'."),$i_linenum));
310 }
311 if(substr_count($s_linestr,")") >1 ){
312 return(sprintf(_("More than one ')' is currently not supported. Line : '%s'."),$i_linenum));
313 }
314 /* Check if there is an application given */
315 if(empty($tmp[1])){
316 return(sprintf(_("There is no application given in line : '%s'."),$i_linenum));
317 }
318 /* Check if there is an extension given */
319 if(empty($tmp[0])){
320 return(sprintf(_("There is no extension type given in line : '%s'."),$i_linenum));
321 }
323 /* Create extension entry for current line
324 * and add this line to an array that will be inserted
325 * to each database.
326 */
327 $exten = addslashes($tmp[0]);
328 $prio = addslashes($tmp[1]);
329 $app = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
330 $para = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
331 $para = preg_replace("/\).*$/","",$para);
332 $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
333 }
335 /* Remove last , from query string */
336 $sql = preg_replace("/,$/","",$sql);
338 /* Save current changes to the database */
339 if($save){
341 /* Macro are spread to each asterisk server */
342 foreach($this->goFonHomeServers as $dn => $cfg){
343 $r_con = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']);
344 $db = @mysql_select_db($cfg['DB'],$r_con);
345 $query = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
346 $res = @mysql_query($query,$r_con);
347 if(!$res){
348 gosa_log(@mysql_error($r_con));
349 gosa_log($query);
350 return(sprintf(_("Insert of new macro failed for server '%s'."),$cfg['SERVER']));
351 }
352 @mysql_close($r_con);
353 }
354 }
355 }
358 function save_object()
359 {
360 if (isset($_POST['gofonMacroGenericPosted'])){
362 $old_cn = $this->cn;
363 $old_visible = $this->goFonMacroVisible;
365 /* Create a base backup and reset the
366 base directly after calling plugin::save_object();
367 Base will be set seperatly a few lines below */
368 $base_tmp = $this->base;
369 plugin::save_object();
370 $this->base = $base_tmp;
372 /* Save base, since this is no LDAP attribute */
373 $tmp = $this->get_allowed_bases();
374 if(isset($_POST['base'])){
375 if(isset($tmp[$_POST['base']])){
376 $this->base= $_POST['base'];
377 }
378 }
380 /* Restore old cn if we have insuficient acls to change cn ... */
381 if(!$this->acl_is_writeable("cn",$this->initially_was_account)){
382 $this->cn = $old_cn;
383 }
385 /* check if we are allowed to toggle visibility */
386 if($this->acl_is_writeable("goFonMacroVisible")) {
388 /* Checkbox selected ? */
389 if(isset($_POST['goFonMacroVisible'])) {
390 $this->goFonMacroVisible= 1 ;
391 }else {
392 if(isset($_POST['displayName'])){
393 $this->goFonMacroVisible= 0 ;
394 }
395 }
396 }else{
397 $this->goFonMacroVisible = $old_visible;
398 }
399 }
400 }
403 /*! Check values */
404 function check()
405 {
406 /* Call common method to give check the hook */
407 $message= plugin::check();
409 if(!count($this->goFonHomeServers)){
410 $message[] = _("There must be at least one server with an asterisk database to save this phone macro.");
411 }
413 /* Check if insert/replace is possible and all servers are available */
414 $str = $this->add_to_database(false);
415 if($str){
416 $message[] = $str;
417 }
419 /* Check if cn is already used */
420 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
421 $ldap = $this->config->get_ldap_link();
422 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
423 if($ldap->count()>0){
424 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
425 }
426 }
428 /* Check if display name is set */
429 if(empty($this->displayName)){
430 $message[] = _("You must specify the 'Display Name' in order to save this macro");
431 }
432 /* CN is restricted to 20 chars */
433 if(strlen("Makro-".$this->cn)>20 ){
434 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
435 }
437 /* If this macro is still in use we should not change the visible for user flag to invisible */
438 if(!$this->goFonMacroVisible){
439 $ldap = $this->config->get_ldap_link();
440 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
441 while ($val = $ldap->fetch()){
442 if(strstr($val['goFonMacro'][0],$this->dn)){
443 $message[] = _("This macro is still in use. It is necessary to mark this macro as visible for users.");
444 return($message);
445 }
446 }
447 }
449 /* Macro content must be smaller than 100 lines */
450 if(count(split("\n",$this->goFonMacroContent))>100){
451 $message[] = _("Makro length must be lower than 100 lines");
452 }
454 /* Macro content must be smaller than 100 lines */
455 if(empty($this->goFonMacroContent)){
456 $message[] = _("You can't save an empty macro.");
457 }
458 return $message;
459 }
462 /*! Remove makro from all given databases
463 * and ldap too.
464 */
465 function remove_from_parent()
466 {
467 $ldap= $this->config->get_ldap_link();
469 /* Skip remove if this macro is still in use */
470 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
471 while ($val = $ldap->fetch()){
472 if(strstr($val['goFonMacro'][0],$this->dn)){
473 print_red(_("This macro is still in use. To delete this Macro ensure that nobody has selected it."));
474 return false;
475 }
476 }
478 /* Try to remove from database */
479 if(count($this->goFonHomeServers)){
480 $str = $this->remove_from_database(true);
481 if($str){
482 print_red($str);
483 return false;
484 }
485 }else{
486 print_red(_("Could not remove the macro entry from asterisk databases. Please check your asterisk database configurations."));
487 return false;
488 }
490 /* Remove phone macro */
491 $ldap->rmDir($this->dn);
492 @log::log("remove","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
493 show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
495 /* Delete references to object groups */
496 $ldap->cd ($this->config->current['BASE']);
497 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
498 while ($ldap->fetch()){
499 $og= new ogroup($this->config, $ldap->getDN());
500 unset($og->member[$this->dn]);
501 $og->save ();
502 show_ldap_error($ldap->get_error(), sprintf(_("Removing of goFonMacro/generic account with dn '%s' failed."),$this->dn));
503 }
504 }
507 /*! Save to LDAP */
508 function save()
509 {
510 plugin::save();
511 unset($this->attrs['base']);
513 /* Try to add entries to databases */
514 $str = $this->add_to_database(true);
515 if($str){
516 print_red($str);
517 }else{
518 /* Write back to ldap */
519 $ldap= $this->config->get_ldap_link();
520 $ldap->cat($this->dn, array('dn'));
521 $a= $ldap->fetch();
523 if (count($a)){
524 $ldap->cd($this->dn);
525 $this->cleanup();
526 $ldap->modify ($this->attrs);
528 $this->handle_post_events("modify");
529 } else {
530 $ldap->cd($this->config->current['BASE']);
531 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
532 $ldap->cd($this->dn);
533 $ldap->add($this->attrs);
534 $this->handle_post_events("add");
535 }
536 show_ldap_error($ldap->get_error(), sprintf(_("Saving of goFonMacro/generic account with dn '%s' failed."),$this->dn));
538 /* Log last action */
539 if($this->initially_was_account){
540 @log::log("modify","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
541 }else{
542 @log::log("create","gofonmacro/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
543 }
544 }
545 }
548 function plInfo()
549 {
550 return (array(
551 "plShortName" => _("Generic"),
552 "plDescription" => _("Asterisk macro management"),
553 "plSelfModify" => FALSE,
554 "plDepends" => array(),
555 "plPriority" => 0,
556 "plSection" => array("administration"),
557 "plCategory" => array("gofonmacro" => array("description" => _("GOfon macro"),
558 "objectClass" => "gofonMacro")),
560 "plProvidedAcls" => array(
561 "cn" => _("Macro name"),
562 "base" => _("Base"),
563 "description" => _("Description"),
564 "displayName" => _("Display name"),
565 "goFonMacroContent" => _("Macro content and parameter"),
566 "goFonMacroVisible" => _("Visibility flag"))
567 ));
568 }
570 }
571 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
572 ?>