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 $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 array_key_exists('FON',$_SESSION['config']->data['SERVERS'])) {
82 /* Set available server */
83 $this->goFonHomeServers = $_SESSION['config']->data['SERVERS']['FON'];
85 /* Remove default entry, not necessary here */
86 if(isset($this->goFonHomeServers[0])){
87 unset($this->goFonHomeServers[0]);
88 }
89 }
91 /* Load acl */
92 $ui = get_userinfo();
93 $acl = get_permissions ($ui->dn, $ui->subtreeACL);
94 $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
95 }
98 /*! Execute this plugin */
99 function execute()
100 {
101 /* Call parent execute */
102 plugin::execute();
104 /* Variables */
105 $vars = "";
106 $tmp = array();
107 $number = 0;
109 /* Base select dialog */
110 $once = true;
111 foreach($_POST as $name => $value){
112 if(preg_match("/^chooseBase/",$name) && $once){
113 $once = false;
114 $this->dialog = new baseSelectDialog($this->config,$this->allowedBasesToMoveTo());
115 $this->dialog->setCurrentBase($this->base);
116 }
117 }
119 /* Dialog handling */
120 if(is_object($this->dialog)){
121 /* Must be called before save_object */
122 $this->dialog->save_object();
124 if($this->dialog->isClosed()){
125 $this->dialog = false;
126 }elseif($this->dialog->isSelected()){
127 $this->base = $this->dialog->isSelected();
128 $this->dialog= false;
129 }else{
130 return($this->dialog->execute());
131 }
132 }
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->dn != "new"){
164 $smarty->assign("disable_cn"," disabled ");
165 }else{
166 $smarty->assign("disable_cn"," ");
167 }
168 /* Show main page */
169 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
170 }
173 /* This method check if all databases are reachable.
174 * Returns with error message or an empty string on success.
175 *
176 * - Is mysql extension available
177 * - Is every server reachable
178 * - Does the database exists/is accessible
179 */
180 function check_database_accessibility()
181 {
182 /* Check if mysql extension is available */
183 if(!is_callable("mysql_pconnect")){
184 return(_("Can't save any changes to asterisk database, there is currently no mysql extension available in your php setup."));
185 }
187 /********************
188 * Check all home server
189 ********************/
190 foreach($this->goFonHomeServers as $goFonHomeServer => $cfg_Current){
191 $r_current = @mysql_pconnect($cfg_Current['SERVER'],$cfg_Current['LOGIN'],$cfg_Current['PASSWORD']);
192 if(!$r_current){
193 gosa_log(@mysql_error($r_current));
194 return(sprintf(_("The MySQL home server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
195 $cfg_Current['SERVER'],$cfg_Current['LOGIN']));
196 }
197 $db_current = @mysql_select_db($cfg_Current['DB'],$r_current);
198 if(!$db_current){
199 gosa_log(@mysql_error($r_current));
200 mysql_close($r_current);
201 return( sprintf(_("Can't select database '%s' on home server '%s'."),$cfg_Current['DB'],$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 if(!$res){
230 gosa_log(@mysql_error($r_current));
231 return(sprintf(_("Removing marco from '%s' failed. Check GOsa log for mysql error."),$Server['SERVER']));
232 }
233 @mysql_close($r_current);
234 }
235 }
236 }
239 /* Add current macro to all asterisk server.
240 * First of all check if we have access to all databases.
241 * - Remove old entries
242 * - Add new entries
243 */
244 function add_to_database($save)
245 {
246 /* Check if all databases are reachable */
247 $str = $this->check_database_accessibility();
248 if($str){
249 return($str);
250 }
252 /* Remove old entries first. Else we got duplicated entries */
253 $str = $this->remove_from_database($save);
254 if($str){
255 return($str);
256 }
258 /* Create query string */
259 $context = "macro-".$this->cn;
261 /************
262 * Parse Macro content
263 ************/
264 $sql =
265 "INSERT INTO %TABLENAME% ".
266 " (context,exten,priority,app,appdata) ".
267 " VALUES ";
269 $a_contentLines = split("\n",$this->goFonMacroContent);
270 foreach($a_contentLines as $i_linenum => $s_linestr){
272 /* Remove the 'exten => ' string in front of the macro content line
273 * example line 'exten => s,2,GotoIf(${ARG3}?3:5)'
274 * Remove comments introduced by ;
275 * Skip empty lines
276 */
277 $s_linestr = preg_replace ("/^.*=\> /","",$s_linestr);
278 $s_linestr = preg_replace("/;.*$/","",$s_linestr) ;
279 $s_linestr = trim($s_linestr);
280 if(empty($s_linestr)){
281 continue;
282 }
284 /* A line that passes the check above should look like this
285 * s,1,SetLanguage(de)
286 * 3 parts seperated by ,
287 * If there are more or less parts, abort.
288 * The preg_replace exclude parameters from split ..
289 */
290 $tmp = split(",", $s_linestr,3);
292 /* Check if there are exactly 2 , */
293 if(substr_count($s_linestr,",") !=2){
294 return(sprintf(_("More than two ',' given in line : '%s'. Remember that parameters are seperated by '|'."),$i_linenum));
295 }
296 /* Multiple () are not supproted currently ... */
297 if(substr_count($s_linestr,"(") >1 ){
298 return(sprintf(_("More than one '(' is currently not supported. Line : '%s'."),$i_linenum));
299 }
300 if(substr_count($s_linestr,")") >1 ){
301 return(sprintf(_("More than one ')' is currently not supported. Line : '%s'."),$i_linenum));
302 }
303 /* Check if there is an application given */
304 if(empty($tmp[1])){
305 return(sprintf(_("There is no application given in line : '%s'."),$i_linenum));
306 }
307 /* Check if there is an extension given */
308 if(empty($tmp[0])){
309 return(sprintf(_("There is no extension type given in line : '%s'."),$i_linenum));
310 }
312 /* Create extension entry for current line
313 * and add this line to an array that will be inserted
314 * to each database.
315 */
316 $exten = addslashes($tmp[0]);
317 $prio = addslashes($tmp[1]);
318 $app = addslashes(preg_replace("/\(.*\).*$/","",$tmp[2]));
319 $para = addslashes(preg_replace("/^.*\(/","",$tmp[2]));
320 $para = preg_replace("/\).*$/","",$para);
321 $sql.= " ('".$context."','".$exten."','".$prio."','".$app."','".$para."'),";
322 }
324 /* Remove last , from query string */
325 $sql = preg_replace("/,$/","",$sql);
327 /* Save current changes to the database */
328 if($save){
330 /* Macro are spread to each asterisk server */
331 foreach($this->goFonHomeServers as $dn => $cfg){
332 $r_con = @mysql_pconnect($cfg['SERVER'],$cfg['LOGIN'],$cfg['PASSWORD']);
333 $db = @mysql_select_db($cfg['DB'],$r_con);
334 $query = preg_replace("/%TABLENAME%/",$cfg['EXT_TABLE'],$sql);
335 $res = @mysql_query($query,$r_con);
336 if(!$res){
337 gosa_log(@mysql_error($r_con));
338 return(sprintf(_("Insert of new macro failed for server '%s'."),$cfg['SERVER']));
339 }
340 @mysql_close($r_con);
341 }
342 }
343 }
346 /*! Save data to object */
347 function save_object()
348 {
349 if (isset($_POST['displayName'])){
350 plugin::save_object();
352 /* The cn can't be changed if this entry is not new */
353 if($this->dn!= "new"){
354 $this->cn = $this->orig_cn;
355 }
356 }
357 }
360 /*! Check values */
361 function check()
362 {
363 /* Call common method to give check the hook */
364 $message= plugin::check();
366 /* Check if insert/replace is possible and all servers are available */
367 $str = $this->add_to_database(false);
368 if($str){
369 $message[] = $str;
370 }
372 /* Check if cn is already used */
373 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
374 $ldap = $this->config->get_ldap_link();
375 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
376 if($ldap->count()>0){
377 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
378 }
379 }
381 /* Check if display name is set */
382 if(empty($this->displayName)){
383 $message[] = _("You must specify the 'Display Name' in order to save this macro");
384 }
385 /* CN is restricted to 20 chars */
386 if(strlen("Makro-".$this->cn)>20 ){
387 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
388 }
390 /* Check permissions */
391 foreach($this->attributes as $attr){
392 if(chkacl($this->acl,"edit")){
393 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
394 return(array($str));
395 }
396 }
398 /* Macro content must be smaller than 100 lines */
399 if(count(split("\n",$this->goFonMacroContent))>100){
400 $message[] = _("Makro length must be lower than 100 lines");
401 }
403 return $message;
404 }
407 /*! Remove makro from all given databases
408 * and ldap too.
409 */
410 function remove_from_parent()
411 {
412 $ldap= $this->config->get_ldap_link();
414 /* Skip remove if this macro is still in use */
415 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount)(goFonMacro=*))", array("goFonMacro"));
416 while ($val = $ldap->fetch()){
417 if(strstr($val['goFonMacro'][0],$this->dn)){
418 print_red(_("This macro is still in use. To delete this Macro ensure that nobody has selected it."));
419 return false;
420 }
421 }
423 /* Try to remove from database */
424 $str = $this->remove_from_database(true);
425 if($str){
426 print_red($str);
427 }
429 /* Remove phone macro */
430 $ldap->rmDir($this->dn);
431 show_ldap_error($ldap->get_error(), _("Removing phone macro failed"));
433 /* Delete references to object groups */
434 $ldap->cd ($this->config->current['BASE']);
435 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
436 while ($ldap->fetch()){
437 $og= new ogroup($this->config, $ldap->getDN());
438 unset($og->member[$this->dn]);
439 $og->save ();
440 show_ldap_error($ldap->get_error(), _("Removing phone macro reverences failed"));
441 }
442 }
445 /*! Save to LDAP */
446 function save()
447 {
448 /* Post checks */
449 $this->execute();
451 plugin::save();
452 unset($this->attrs['base']);
454 /* Try to add entries to databases */
455 $str = $this->add_to_database(true);
456 if($str){
457 print_red($str);
458 }else{
459 /* Write back to ldap */
460 $ldap= $this->config->get_ldap_link();
461 $ldap->cat($this->dn, array('dn'));
462 $a= $ldap->fetch();
464 if (count($a)){
465 $ldap->cd($this->dn);
466 $this->cleanup();
467 $ldap->modify ($this->attrs);
469 $this->handle_post_events("modify");
470 } else {
471 $ldap->cd($this->config->current['BASE']);
472 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
473 $ldap->cd($this->dn);
474 $ldap->add($this->attrs);
475 $this->handle_post_events("add");
476 }
477 show_ldap_error($ldap->get_error(), _("Saving phone macro failed"));
478 }
479 }
481 }
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>