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 /*! This ist the variable that contains the description of the macro*/
26 var $description = "";
28 /*! The base of the macro, is used to save the macro in the correct directory tree */
29 var $base = "";
31 /*! This is the name of the macro which the enduser will see, instead of the cn */
32 var $displayName = "";
34 /*! Here is the macro content, the real macroscript */
35 var $goFonMacroContent= "";
37 /*! To allow user to use this macro this var must be true, else false */
38 var $goFonMacroVisible= 0;
40 /*! attribute list for save action */
41 var $attributes = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
43 var $is_new=false;
44 var $orig_cn = "";
45 /*! Objectclasses that this calls handles */
46 var $objectclasses = array("top", "goFonMacro");
48 //! The Konstructor
49 /*! Konstructor, load class with attributes of the given dn*/
50 function macro ($config, $dn= NULL)
51 {
52 plugin::plugin ($config, $dn);
54 $ldap= $config->get_ldap_link();
56 $this->dn = $dn;
58 /* This is always an account */
59 $this->is_account= TRUE;
62 /* Edit or new one ?*/
63 if ($this->dn == "new"){
64 if(isset($_SESSION['macrofilter']['depselect'])){
65 $this->base = $_SESSION['macrofilter']['depselect'];
66 $this->is_new = true;
67 }else{
68 $this->is_new = true;
69 $ui= get_userinfo();
70 $this->base= dn2base($ui->dn);
71 }
72 } else {
73 $this->is_new = false;
74 $this->orig_cn=$this->cn;
75 $this->base= dn2base($this->dn);
76 }
77 }
79 /*! Execute this plugin */
80 function execute()
81 {
82 /* Variables */
83 $vars = "";
84 $tmp = array();
85 $number = 0;
87 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
88 print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). \n Your Settings can't be saved to asterisk Database. "));
89 }
91 /* Do we represent a valid group? */
92 if (!$this->is_account && $this->parent == NULL){
93 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
94 _("This 'dn' is no phone macro.")."</b>";
95 return ($display);
96 }
98 /* Fill templating stuff */
99 $smarty= get_smarty();
100 $smarty->assign("bases", $this->config->idepartments);
102 /* Assign all vars to Smarty */
103 foreach($this->attributes as $ar){
104 $smarty->assign($ar, $this->$ar);
105 }
106 /* Checkboxes */
107 $smarty->assign("base_select", $this->base);
108 $smarty->assign("vars", $vars);
110 if($this->goFonMacroVisible){
111 $smarty->assign("goFonMacroVisibleChecked"," checked ");
112 }else{
113 $smarty->assign("goFonMacroVisibleChecked","");
114 }
116 if(isset($_POST['goFonMacroVisible'])) {
117 $this->goFonMacroVisible= 1 ;
118 $smarty->assign("goFonMacroVisibleChecked"," checked ");
119 }else {
120 if(isset($_POST['displayName'])){
121 $this->goFonMacroVisible= 0 ;
122 $smarty->assign("goFonMacroVisibleChecked","");
123 }
124 }
126 if(!$this->is_new){
127 $smarty->assign("disable_cn"," disabled ");
128 $smarty->assign("cn",$this->orig_cn);
129 }else{
130 $smarty->assign("disable_cn"," ");
131 $smarty->assign("cn",$this->cn);
132 }
133 $this->generate_mysql_entension_entries();
134 /* Show main page */
135 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
136 }
138 /*!
139 Remove this Object
140 */
141 function remove_from_parent()
142 {
143 $ldap= $this->config->get_ldap_link();
145 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
147 while ($val = $ldap->fetch()){
148 if(isset($val['goFonMacro'])){
149 if(strstr($val['goFonMacro'][0],$this->dn)){
150 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
151 return false;
152 }
153 }
154 }
156 $ldap->rmDir($this->dn);
157 if(isset($this->orig_cn)){
158 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
159 }else{
160 $this->generate_mysql_entension_entries(false,true);
161 }
163 /* Delete references to object groups */
164 $ldap->cd ($this->config->current['BASE']);
165 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
166 while ($ldap->fetch()){
167 $og= new ogroup($this->config, $ldap->getDN());
168 unset($og->member[$this->dn]);
169 $og->save ();
170 }
171 }
173 // Generate MySQL Syntax
174 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
176 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
177 return(true);
178 }
180 // Get Configuration for Mysql database Server
181 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
183 // Connect to DB server
184 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
186 // Check if we are connected correctly
187 if(!$r_con){
188 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
189 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
190 gosa_log(mysql_error());
191 return false;
192 }
194 // Select database for Extensions
195 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
197 // Test if we have the database selected correctly
198 if(!$db){
199 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
200 gosa_log(mysql_error());
201 return false;
202 }
205 // Context def
206 $context = "macro-".$this->cn;
208 // Parse Content if we connected correctly
209 if($db && $r_con ){
211 // Split Content into lines
212 $a_contentLines = split ("\n",$this->goFonMacroContent);
214 // Foreach single line ...
215 foreach($a_contentLines as $i_linenum => $s_linestr){
217 // Remove unwanted exten => tag
218 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
220 // Remove spaces
221 $s_linestr = trim ( $s_linestr);
223 // If not empty or linebreak at [0]
224 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
226 // Set general SQL statement
227 $SQL[$i_linenum] =
228 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
229 " (context,exten,priority,app,appdata) ".
230 " VALUES ".
231 " (";
233 // Parse linestr to entry data
234 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
236 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
238 if(!isset($tmp[2])){
239 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
240 return false;
241 }
242 $exten = $tmp[0];
243 $prio = $tmp[1];
244 $app = $tmp[2];
245 $para = $linecontents;
246 $para = preg_replace("/^.*\(/","",$para);
247 $para = preg_replace("/\)$/","",$para);
249 // Append SQL syntax
250 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
251 }
252 }
254 if(($save)||($delete_only)){
255 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
256 if(!$res){
257 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
258 gosa_log(mysql_error());
259 return false;
260 }
261 $cnt = mysql_fetch_row($res);
262 $cnt = $cnt[0];
264 if($cnt != 0) {
265 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
266 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
267 gosa_log(mysql_error());
268 return false;
269 }
270 }
272 if($remove_old_macroname!="false"){
273 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
274 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
275 gosa_log(mysql_error());
276 return false;
277 }
278 }
282 }
284 // We want to save this
285 if(($save)&&(isset($SQL))){
286 foreach($SQL as $entry){
287 if(!mysql_query($entry)){
288 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
289 gosa_log(mysql_error());
290 return false;
291 }
292 }
293 }
294 }
295 if((isset($r_con))&&($r_con)){
296 @mysql_close($r_con);
297 }
298 return true;
299 }
304 /*!
305 Save data to object
306 */
307 function save_object()
308 {
309 if (isset($_POST['displayName'])){
310 plugin::save_object();
312 }
313 }
316 /*!
317 Check values
318 */
319 function check()
320 {
322 $message = array();
323 if(!$this->generate_mysql_entension_entries()){
324 $message[] = $this->generate_error;
325 }
327 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
328 $ldap = $this->config->get_ldap_link();
329 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
330 if($ldap->count()>0){
331 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
332 }
333 }
335 if(empty($this->displayName)){
336 $message[] = _("You must specify the 'Display Name' in order to save this macro");
337 }
339 if(strlen("Makro-".$this->cn)>20 ){
340 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
341 }
343 foreach($this->attributes as $attr){
344 if(chkacl($this->acl,$attr)){
345 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
346 return(array($str));
347 }
348 }
350 if(count(split("\n",$this->goFonMacroContent))>100){
351 $message[] = _("Makro length must be lower than 100 lines");
352 }
354 /*Some stupid IE fixes again*/
355 if(empty ($this->base)) {
356 $message[] = _("Please choose a valid base.");
357 }
359 return $message;
360 }
363 /*!
364 Save to LDAP
365 */
366 function save()
367 {
368 /* Post checks */
369 $this->execute();
371 plugin::save();
372 unset($this->attrs['base']);
374 /* Write back to ldap */
375 $ldap= $this->config->get_ldap_link();
376 $ldap->cat($this->dn);
377 $a= $ldap->fetch();
379 if(isset($this->orig_cn)){
380 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
381 }else{
382 $this-> generate_mysql_entension_entries(true);
383 }
385 if($this->generate_mysql_entension_entries()){
386 if (count($a)){
387 $ldap->cd($this->dn);
388 $ldap->modify($this->attrs);
389 $this->handle_post_events("modify");
390 } else {
391 $ldap->cd($this->config->current['BASE']);
392 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
393 $ldap->cd($this->dn);
394 $ldap->add($this->attrs);
395 $this->handle_post_events("add");
396 }
397 }
398 show_ldap_error($ldap->get_error());
399 }
401 }
402 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
403 ?>