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");
20 /*! Macro attributes, */
21 var $generate_error= "";
23 /*! The name of the Macro in the openldap drirectory */
24 var $cn = "";
26 /*! This ist the variable that contains the description of the macro*/
27 var $description = "";
29 /*! The base of the macro, is used to save the macro in the correct directory tree */
30 var $base = "";
32 /*! This is the name of the macro which the enduser will see, instead of the cn */
33 var $displayName = "";
35 /*! Here is the macro content, the real macroscript */
36 var $goFonMacroContent= "";
38 /*! To allow user to use this macro this var must be true, else false */
39 var $goFonMacroVisible= 0;
41 /*! attribute list for save action */
42 var $attributes = array("cn","base", "description","displayName","goFonMacroContent","goFonMacroVisible");
44 var $is_new=false;
46 /*! Objectclasses that this calls handles */
47 var $objectclasses = array("top", "goFonMacro");
49 //! The Konstructor
50 /*! Konstructor, load class with attributes of the given dn*/
51 function macro ($config, $dn= NULL)
52 {
53 plugin::plugin ($config, $dn);
55 $ldap= $config->get_ldap_link();
57 $this->dn = $dn;
59 /* This is always an account */
60 $this->is_account= TRUE;
63 /* Edit or new one ?*/
64 if ($this->dn == "new"){
65 $this->is_new = true;
66 $ui= get_userinfo();
67 $this->base= dn2base($ui->dn);
68 } else {
69 $this->is_new = false;
70 $this->orig_cn=$this->cn;
71 $this->base= dn2base($this->dn);
72 }
73 }
75 /*! Execute this plugin */
76 function execute()
77 {
78 /* Variables */
79 $vars = "";
80 $tmp = array();
81 $number = 0;
83 /* Do we represent a valid group? */
84 if (!$this->is_account && $this->parent == NULL){
85 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
86 _("This 'dn' is no phone macro.")."</b>";
87 return ($display);
88 }
90 /* Fill templating stuff */
91 $smarty= get_smarty();
92 $smarty->assign("bases", $this->config->idepartments);
94 /* Assign all vars to Smarty */
95 foreach($this->attributes as $ar){
96 $smarty->assign($ar, $this->$ar);
97 }
98 /* Checkboxes */
99 $smarty->assign("base_select", $this->base);
100 $smarty->assign("vars", $vars);
102 if($this->goFonMacroVisible){
103 $smarty->assign("goFonMacroVisibleChecked"," checked ");
104 }else{
105 $smarty->assign("goFonMacroVisibleChecked","");
106 }
108 if(isset($_POST['goFonMacroVisible'])) {
109 $this->goFonMacroVisible= 1 ;
110 $smarty->assign("goFonMacroVisibleChecked"," checked ");
111 }else {
112 if(isset($_POST['displayName'])){
113 $this->goFonMacroVisible= 0 ;
114 $smarty->assign("goFonMacroVisibleChecked","");
115 }
116 }
118 if(!$this->is_new){
119 $smarty->assign("disable_cn"," disabled ");
120 $smarty->assign("cn",$this->orig_cn);
121 }else{
122 $smarty->assign("disable_cn"," ");
123 $smarty->assign("cn","");
124 }
125 $this->generate_mysql_entension_entries();
126 /* Show main page */
127 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
128 }
130 /*!
131 Remove this Object
132 */
133 function remove_from_parent()
134 {
135 $ldap= $this->config->get_ldap_link();
137 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
139 while ($val = $ldap->fetch()){
140 if(isset($val['goFonMacro'])){
141 if(strstr($val['goFonMacro'][0],$this->dn)){
142 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
143 return false;
144 }
145 }
146 }
148 $ldap->rmDir($this->dn);
149 if(isset($this->orig_cn)){
150 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
151 }else{
152 $this->generate_mysql_entension_entries(false,true);
153 }
155 /* Delete references to object groups */
156 $ldap->cd ($this->config->current['BASE']);
157 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
158 while ($ldap->fetch()){
159 $og= new ogroup($this->config, $ldap->getDN());
160 unset($og->member[$this->dn]);
161 $og->save ();
162 }
163 }
165 // Generate MySQL Syntax
166 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
168 // Get Configuration for Mysql database Server
169 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
171 // Connect to DB server
172 $r_con = @mysql_connect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
174 // Check if we are connected correctly
175 if(!$r_con){
176 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
177 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
178 gosa_log(mysql_error());
179 return false;
180 }
182 // Select database for Extensions
183 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
185 // Test if we have the database selected correctly
186 if(!$db){
187 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
188 gosa_log(mysql_error());
189 return false;
190 }
193 // Context def
194 $context = "macro-".$this->cn;
196 // Parse Content if we connected correctly
197 if($db && $r_con && $save){
199 // Split Content into lines
200 $a_contentLines = split ("\n",$this->goFonMacroContent);
202 // Foreach single line ...
203 foreach($a_contentLines as $i_linenum => $s_linestr){
205 // Remove unwanted exten => tag
206 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
208 // Remove spaces
209 $s_linestr = trim ( $s_linestr);
211 // If not empty or linebreak at [0]
212 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
214 // Set general SQL statement
215 $SQL[$i_linenum] =
216 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
217 " (context,exten,priority,app,appdata) ".
218 " VALUES ".
219 " (";
221 // Parse linestr to entry data
222 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
224 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
226 if(!isset($tmp[2])){
227 print_red(sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum));
228 }
230 $exten = $tmp[0];
231 $prio = $tmp[1];
232 $app = $tmp[2];
233 $para = $linecontents;
234 $para = preg_replace("/^.*\(/","",$para);
235 $para = preg_replace("/\)$/","",$para);
237 // Append SQL syntax
238 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
239 }
240 }
242 if(($save)||($delete_only)){
243 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
244 if(!$res){
245 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
246 gosa_log(mysql_error());
247 return false;
248 }
249 $cnt = mysql_fetch_row($res);
250 $cnt = $cnt[0];
252 if($cnt != 0) {
253 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
254 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
255 gosa_log(mysql_error());
256 return false;
257 }
258 }
260 if($remove_old_macroname!="false"){
261 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
262 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
263 gosa_log(mysql_error());
264 return false;
265 }
266 }
270 }
272 // We want to save this
273 if($save){
274 foreach($SQL as $entry){
275 if(!mysql_query($entry)){
276 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
277 gosa_log(mysql_error());
278 return false;
279 }
280 }
281 }
282 }
284 return true;
286 }
291 /*!
292 Save data to object
293 */
294 function save_object()
295 {
296 if (isset($_POST['displayName'])){
297 plugin::save_object();
299 }
300 }
303 /*!
304 Check values
305 */
306 function check()
307 {
309 $message = array();
310 if(!$this->generate_mysql_entension_entries()){
311 $message[] = $this->generate_error;
312 }
314 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
315 $ldap = $this->config->get_ldap_link();
316 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
317 if($ldap->count()>0){
318 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
319 }
320 }
322 if(empty($this->displayName)){
323 $message[] = _("You must specify the 'Display Name' in order to save this macro");
324 }
326 if(strlen("Makro-".$this->cn)>20 ){
327 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
328 }
330 foreach($this->attributes as $attr){
331 if(chkacl($this->acl,$attr)){
332 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
333 return(array($str));
334 }
335 }
337 if(count(split("\n",$this->goFonMacroContent))>100){
338 $message[] = _("Makro length must be lower than 100 lines");
339 }
341 /*Some stupid IE fixes again*/
342 if(empty ($this->base)) {
343 $message[] = _("Please choose a valid base.");
344 }
346 return $message;
347 }
350 /*!
351 Save to LDAP
352 */
353 function save()
354 {
355 /* Post checks */
356 $this->execute();
358 plugin::save();
359 unset($this->attrs['base']);
361 /* Write back to ldap */
362 $ldap= $this->config->get_ldap_link();
363 $ldap->cat($this->dn);
364 $a= $ldap->fetch();
367 if(isset($this->orig_cn)){
368 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
369 }else{
370 $this-> generate_mysql_entension_entries(true);
371 }
373 if($this->generate_mysql_entension_entries()){
374 if (count($a)){
375 $ldap->cd($this->dn);
376 $ldap->modify($this->attrs);
377 $this->handle_post_events("modify");
378 } else {
379 $ldap->cd($this->dn);
380 $ldap->create_missing_trees( $this->dn);
381 $ldap->cd($this->dn);
382 $ldap->add($this->attrs);
383 $this->handle_post_events("add");
384 }
385 }
386 show_ldap_error($ldap->get_error());
387 }
389 }
390 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
391 ?>