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).\nYour 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 // Skip empty lines
224 if ($s_linestr == ""){
225 continue;
226 }
228 // If not empty or linebreak at [0]
229 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
231 // Set general SQL statement
232 $SQL[$i_linenum] =
233 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
234 " (context,exten,priority,app,appdata) ".
235 " VALUES ".
236 " (";
238 // Parse linestr to entry data
239 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
241 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
243 if(!isset($tmp[2])){
244 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
245 return false;
246 }
247 $exten = $tmp[0];
248 $prio = $tmp[1];
249 $app = $tmp[2];
250 $para = $linecontents;
251 $para = preg_replace("/^.*\(/","",$para);
252 $para = preg_replace("/\)$/","",$para);
254 // Append SQL syntax
255 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
256 }
257 }
259 if(($save)||($delete_only)){
260 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
261 if(!$res){
262 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
263 gosa_log(mysql_error());
264 return false;
265 }
266 $cnt = mysql_fetch_row($res);
267 $cnt = $cnt[0];
269 if($cnt != 0) {
270 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
271 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
272 gosa_log(mysql_error());
273 return false;
274 }
275 }
277 if($remove_old_macroname!="false"){
278 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
279 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
280 gosa_log(mysql_error());
281 return false;
282 }
283 }
287 }
289 // We want to save this
290 if(($save)&&(isset($SQL))){
291 foreach($SQL as $entry){
292 if(!mysql_query($entry)){
293 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
294 gosa_log(mysql_error());
295 return false;
296 }
297 }
298 }
299 }
300 if((isset($r_con))&&($r_con)){
301 @mysql_close($r_con);
302 }
303 return true;
304 }
309 /*!
310 Save data to object
311 */
312 function save_object()
313 {
314 if (isset($_POST['displayName'])){
315 plugin::save_object();
317 }
318 }
321 /*!
322 Check values
323 */
324 function check()
325 {
327 $message = array();
328 if(!$this->generate_mysql_entension_entries()){
329 $message[] = $this->generate_error;
330 }
332 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
333 $ldap = $this->config->get_ldap_link();
334 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
335 if($ldap->count()>0){
336 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
337 }
338 }
340 if(empty($this->displayName)){
341 $message[] = _("You must specify the 'Display Name' in order to save this macro");
342 }
344 if(strlen("Makro-".$this->cn)>20 ){
345 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
346 }
348 foreach($this->attributes as $attr){
349 if(chkacl($this->acl,$attr)){
350 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
351 return(array($str));
352 }
353 }
355 if(count(split("\n",$this->goFonMacroContent))>100){
356 $message[] = _("Makro length must be lower than 100 lines");
357 }
359 /*Some stupid IE fixes again*/
360 if(empty ($this->base)) {
361 $message[] = _("Please choose a valid base.");
362 }
364 return $message;
365 }
368 /*!
369 Save to LDAP
370 */
371 function save()
372 {
373 /* Post checks */
374 $this->execute();
376 plugin::save();
377 unset($this->attrs['base']);
379 /* Write back to ldap */
380 $ldap= $this->config->get_ldap_link();
381 $ldap->cat($this->dn);
382 $a= $ldap->fetch();
384 if(isset($this->orig_cn)){
385 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
386 }else{
387 $this-> generate_mysql_entension_entries(true);
388 }
390 if($this->generate_mysql_entension_entries()){
391 if (count($a)){
392 $ldap->cd($this->dn);
393 $ldap->modify($this->attrs);
394 $this->handle_post_events("modify");
395 } else {
396 $ldap->cd($this->config->current['BASE']);
397 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
398 $ldap->cd($this->dn);
399 $ldap->add($this->attrs);
400 $this->handle_post_events("add");
401 }
402 }
403 show_ldap_error($ldap->get_error());
404 }
406 }
407 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
408 ?>