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 /* Call parent execute */
83 plugin::execute();
85 /* Variables */
86 $vars = "";
87 $tmp = array();
88 $number = 0;
90 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
91 print_red(_("There is currently no asterisk server defined. Possibly you are missing a server that handles the asterisk management (goFonServer). Your settings can't be saved to asterisk database."));
92 }
94 /* Do we represent a valid group? */
95 if (!$this->is_account && $this->parent == NULL){
96 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
97 _("This 'dn' is no phone macro.")."</b>";
98 return ($display);
99 }
101 /* Fill templating stuff */
102 $smarty= get_smarty();
103 $smarty->assign("bases", $this->config->idepartments);
105 /* Assign all vars to Smarty */
106 foreach($this->attributes as $ar){
107 $smarty->assign($ar, $this->$ar);
108 }
109 /* Checkboxes */
110 $smarty->assign("base_select", $this->base);
111 $smarty->assign("vars", $vars);
113 if($this->goFonMacroVisible){
114 $smarty->assign("goFonMacroVisibleChecked"," checked ");
115 }else{
116 $smarty->assign("goFonMacroVisibleChecked","");
117 }
119 if(isset($_POST['goFonMacroVisible'])) {
120 $this->goFonMacroVisible= 1 ;
121 $smarty->assign("goFonMacroVisibleChecked"," checked ");
122 }else {
123 if(isset($_POST['displayName'])){
124 $this->goFonMacroVisible= 0 ;
125 $smarty->assign("goFonMacroVisibleChecked","");
126 }
127 }
129 if(!$this->is_new){
130 $smarty->assign("disable_cn"," disabled ");
131 $smarty->assign("cn",$this->orig_cn);
132 }else{
133 $smarty->assign("disable_cn"," ");
134 $smarty->assign("cn",$this->cn);
135 }
136 $this->generate_mysql_entension_entries();
137 /* Show main page */
138 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
139 }
141 /*!
142 Remove this Object
143 */
144 function remove_from_parent()
145 {
146 $ldap= $this->config->get_ldap_link();
148 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
150 while ($val = $ldap->fetch()){
151 if(isset($val['goFonMacro'])){
152 if(strstr($val['goFonMacro'][0],$this->dn)){
153 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
154 return false;
155 }
156 }
157 }
159 $ldap->rmDir($this->dn);
160 if(isset($this->orig_cn)){
161 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
162 }else{
163 $this->generate_mysql_entension_entries(false,true);
164 }
166 /* Delete references to object groups */
167 $ldap->cd ($this->config->current['BASE']);
168 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
169 while ($ldap->fetch()){
170 $og= new ogroup($this->config, $ldap->getDN());
171 unset($og->member[$this->dn]);
172 $og->save ();
173 }
174 }
176 // Generate MySQL Syntax
177 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
179 if(!isset($_SESSION['config']->data['SERVERS']['FON'])){
180 return(true);
181 }
183 // Get Configuration for Mysql database Server
184 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
186 // Connect to DB server
187 $r_con = @mysql_pconnect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
189 // Check if we are connected correctly
190 if(!$r_con){
191 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
192 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
193 gosa_log(mysql_error());
194 return false;
195 }
197 // Select database for Extensions
198 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
200 // Test if we have the database selected correctly
201 if(!$db){
202 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
203 gosa_log(mysql_error());
204 return false;
205 }
208 // Context def
209 $context = "macro-".$this->cn;
211 // Parse Content if we connected correctly
212 if($db && $r_con ){
214 // Split Content into lines
215 $a_contentLines = split ("\n",$this->goFonMacroContent);
217 // Foreach single line ...
218 foreach($a_contentLines as $i_linenum => $s_linestr){
220 // Remove unwanted exten => tag
221 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
223 // Remove spaces
224 $s_linestr = trim ( $s_linestr);
226 // Skip empty lines
227 if ($s_linestr == ""){
228 continue;
229 }
231 // If not empty or linebreak at [0]
232 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
234 // Set general SQL statement
235 $SQL[$i_linenum] =
236 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
237 " (context,exten,priority,app,appdata) ".
238 " VALUES ".
239 " (";
241 // Parse linestr to entry data
242 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
244 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
246 if(!isset($tmp[2])){
247 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
248 return false;
249 }
250 $exten = $tmp[0];
251 $prio = $tmp[1];
252 $app = $tmp[2];
253 $para = $linecontents;
254 $para = preg_replace("/^.*\(/","",$para);
255 $para = preg_replace("/\)$/","",$para);
257 // Append SQL syntax
258 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
259 }
260 }
262 if(($save)||($delete_only)){
263 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
264 if(!$res){
265 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
266 gosa_log(mysql_error());
267 return false;
268 }
269 $cnt = mysql_fetch_row($res);
270 $cnt = $cnt[0];
272 if($cnt != 0) {
273 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
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 }
280 if($remove_old_macroname!="false"){
281 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
282 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
283 gosa_log(mysql_error());
284 return false;
285 }
286 }
290 }
292 // We want to save this
293 if(($save)&&(isset($SQL))){
294 foreach($SQL as $entry){
295 if(!mysql_query($entry)){
296 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
297 gosa_log(mysql_error());
298 return false;
299 }
300 }
301 }
302 }
303 if((isset($r_con))&&($r_con)){
304 @mysql_close($r_con);
305 }
306 return true;
307 }
312 /*!
313 Save data to object
314 */
315 function save_object()
316 {
317 if (isset($_POST['displayName'])){
318 plugin::save_object();
320 }
321 }
324 /*!
325 Check values
326 */
327 function check()
328 {
330 $message = array();
331 if(!$this->generate_mysql_entension_entries()){
332 $message[] = $this->generate_error;
333 }
335 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
336 $ldap = $this->config->get_ldap_link();
337 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
338 if($ldap->count()>0){
339 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
340 }
341 }
343 if(empty($this->displayName)){
344 $message[] = _("You must specify the 'Display Name' in order to save this macro");
345 }
347 if(strlen("Makro-".$this->cn)>20 ){
348 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
349 }
351 foreach($this->attributes as $attr){
352 if(chkacl($this->acl,$attr)){
353 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
354 return(array($str));
355 }
356 }
358 if(count(split("\n",$this->goFonMacroContent))>100){
359 $message[] = _("Makro length must be lower than 100 lines");
360 }
362 /*Some stupid IE fixes again*/
363 if(empty ($this->base)) {
364 $message[] = _("Please choose a valid base.");
365 }
367 return $message;
368 }
371 /*!
372 Save to LDAP
373 */
374 function save()
375 {
376 /* Post checks */
377 $this->execute();
379 plugin::save();
380 unset($this->attrs['base']);
382 /* Write back to ldap */
383 $ldap= $this->config->get_ldap_link();
384 $ldap->cat($this->dn);
385 $a= $ldap->fetch();
387 if(isset($this->orig_cn)){
388 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
389 }else{
390 $this-> generate_mysql_entension_entries(true);
391 }
393 if($this->generate_mysql_entension_entries()){
394 if (count($a)){
395 $ldap->cd($this->dn);
396 $ldap->modify($this->attrs);
397 $this->handle_post_events("modify");
398 } else {
399 $ldap->cd($this->config->current['BASE']);
400 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
401 $ldap->cd($this->dn);
402 $ldap->add($this->attrs);
403 $this->handle_post_events("add");
404 }
405 }
406 show_ldap_error($ldap->get_error());
407 }
409 }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>