6ac7d11dc3c330c44ebd3b2d472f29b49be96bb7
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;
45 var $orig_cn = "";
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 if(isset($_SESSION['macrofilter']['depselect'])){
66 $this->base = $_SESSION['macrofilter']['depselect'];
67 $this->is_new = true;
68 }else{
69 $this->is_new = true;
70 $ui= get_userinfo();
71 $this->base= dn2base($ui->dn);
72 }
73 } else {
74 $this->is_new = false;
75 $this->orig_cn=$this->cn;
76 $this->base= dn2base($this->dn);
77 }
78 }
80 /*! Execute this plugin */
81 function execute()
82 {
83 /* Variables */
84 $vars = "";
85 $tmp = array();
86 $number = 0;
88 /* Do we represent a valid group? */
89 if (!$this->is_account && $this->parent == NULL){
90 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
91 _("This 'dn' is no phone macro.")."</b>";
92 return ($display);
93 }
95 /* Fill templating stuff */
96 $smarty= get_smarty();
97 $smarty->assign("bases", $this->config->idepartments);
99 /* Assign all vars to Smarty */
100 foreach($this->attributes as $ar){
101 $smarty->assign($ar, $this->$ar);
102 }
103 /* Checkboxes */
104 $smarty->assign("base_select", $this->base);
105 $smarty->assign("vars", $vars);
107 if($this->goFonMacroVisible){
108 $smarty->assign("goFonMacroVisibleChecked"," checked ");
109 }else{
110 $smarty->assign("goFonMacroVisibleChecked","");
111 }
113 if(isset($_POST['goFonMacroVisible'])) {
114 $this->goFonMacroVisible= 1 ;
115 $smarty->assign("goFonMacroVisibleChecked"," checked ");
116 }else {
117 if(isset($_POST['displayName'])){
118 $this->goFonMacroVisible= 0 ;
119 $smarty->assign("goFonMacroVisibleChecked","");
120 }
121 }
123 if(!$this->is_new){
124 $smarty->assign("disable_cn"," disabled ");
125 $smarty->assign("cn",$this->orig_cn);
126 }else{
127 $smarty->assign("disable_cn"," ");
128 $smarty->assign("cn",$this->cn);
129 }
130 $this->generate_mysql_entension_entries();
131 /* Show main page */
132 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
133 }
135 /*!
136 Remove this Object
137 */
138 function remove_from_parent()
139 {
140 $ldap= $this->config->get_ldap_link();
142 $res = $ldap->search("(&(objectClass=goFonAccount)(objectClass=gosaAccount))", array("goFonMacro"));
144 while ($val = $ldap->fetch()){
145 if(isset($val['goFonMacro'])){
146 if(strstr($val['goFonMacro'][0],$this->dn)){
147 print_red(_("This macro ist still in use. To delete this Macro ensure that nobody has selected this Macro."));
148 return false;
149 }
150 }
151 }
153 $ldap->rmDir($this->dn);
154 if(isset($this->orig_cn)){
155 $this->generate_mysql_entension_entries(false,true,$this->orig_cn);
156 }else{
157 $this->generate_mysql_entension_entries(false,true);
158 }
160 /* Delete references to object groups */
161 $ldap->cd ($this->config->current['BASE']);
162 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
163 while ($ldap->fetch()){
164 $og= new ogroup($this->config, $ldap->getDN());
165 unset($og->member[$this->dn]);
166 $og->save ();
167 }
168 }
170 // Generate MySQL Syntax
171 function generate_mysql_entension_entries($save = false,$delete_only=false,$remove_old_macroname=false){
173 // Get Configuration for Mysql database Server
174 $a_SETUP = $_SESSION['config']->data['SERVERS']['FON'];
176 // Connect to DB server
177 $r_con = @mysql_connect($a_SETUP['SERVER'],$a_SETUP['LOGIN'],$a_SETUP['PASSWORD']);
179 // Check if we are connected correctly
180 if(!$r_con){
181 $this->generate_error = sprintf(_("The MySQL Server '%s' isn't reachable as user '%s', check GOsa log for mysql error."),
182 $a_SETUP['SERVER'],$a_SETUP['LOGIN']);
183 gosa_log(mysql_error());
184 return false;
185 }
187 // Select database for Extensions
188 $db = @mysql_select_db($a_SETUP['DB'],$r_con);
190 // Test if we have the database selected correctly
191 if(!$db){
192 $this->generate_error = sprintf(_("Can't select database %s on %s."),$a_SETUP['DB'],$a_SETUP['SERVER']);
193 gosa_log(mysql_error());
194 return false;
195 }
198 // Context def
199 $context = "macro-".$this->cn;
201 // Parse Content if we connected correctly
202 if($db && $r_con ){
204 // Split Content into lines
205 $a_contentLines = split ("\n",$this->goFonMacroContent);
207 // Foreach single line ...
208 foreach($a_contentLines as $i_linenum => $s_linestr){
210 // Remove unwanted exten => tag
211 $s_linestr= preg_replace ("/^.*\> /","",$s_linestr);
213 // Remove spaces
214 $s_linestr = trim ( $s_linestr);
216 // If not empty or linebreak at [0]
217 if((!empty($s_linestr))&&($s_linestr[0]!=";")&&(ord($s_linestr[0]) !=13)){
219 // Set general SQL statement
220 $SQL[$i_linenum] =
221 "INSERT INTO ".$a_SETUP['EXT_TABLE'].
222 " (context,exten,priority,app,appdata) ".
223 " VALUES ".
224 " (";
226 // Parse linestr to entry data
227 $linecontents = preg_replace("/;.*$/","",$s_linestr) ;
229 $tmp = split(",", preg_replace("/\(.*$/","",$linecontents));
231 if(!isset($tmp[2])){
232 $this->generate_error = sprintf(_("Unable to parse macro contents on line: %s"),$i_linenum);
233 return false;
234 }
235 $exten = $tmp[0];
236 $prio = $tmp[1];
237 $app = $tmp[2];
238 $para = $linecontents;
239 $para = preg_replace("/^.*\(/","",$para);
240 $para = preg_replace("/\)$/","",$para);
242 // Append SQL syntax
243 $SQL[$i_linenum].="'".$context."','".$exten."','".$prio."','".$app."','".$para."');";
244 }
245 }
247 if(($save)||($delete_only)){
248 $res = mysql_query("SELECT count(*) FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'");
249 if(!$res){
250 $this->generate_error = sprintf(_("Can't perform SELECT query in DB '%s'"),$a_SETUP['DB']);
251 gosa_log(mysql_error());
252 return false;
253 }
254 $cnt = mysql_fetch_row($res);
255 $cnt = $cnt[0];
257 if($cnt != 0) {
258 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= '".$context."'")){
259 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
260 gosa_log(mysql_error());
261 return false;
262 }
263 }
265 if($remove_old_macroname!="false"){
266 if(!mysql_query("DELETE FROM ".$a_SETUP['EXT_TABLE']." WHERE context= 'Makro-".$remove_old_macroname."'")){
267 $this->generate_error = sprintf(_("Can't perform DELETE query in DB '%s'"),$a_SETUP['DB']);
268 gosa_log(mysql_error());
269 return false;
270 }
271 }
275 }
277 // We want to save this
278 if(($save)&&(isset($SQL))){
279 foreach($SQL as $entry){
280 if(!mysql_query($entry)){
281 $this->generate_error = sprintf(_("Can't perform INSERT query in DB '%s'"),$a_SETUP['DB']);
282 gosa_log(mysql_error());
283 return false;
284 }
285 }
286 }
287 }
289 return true;
291 }
296 /*!
297 Save data to object
298 */
299 function save_object()
300 {
301 if (isset($_POST['displayName'])){
302 plugin::save_object();
304 }
305 }
308 /*!
309 Check values
310 */
311 function check()
312 {
314 $message = array();
315 if(!$this->generate_mysql_entension_entries()){
316 $message[] = $this->generate_error;
317 }
319 if(($this->dn=="new")||($this->orig_cn!=$this->cn)){
320 $ldap = $this->config->get_ldap_link();
321 $ldap->search("(&(objectClass=goFonMacro)(cn=".$this->cn."))",array("cn"));
322 if($ldap->count()>0){
323 $message[]=sprintf(_("The given cn '%s' already exists."),$this->cn);
324 }
325 }
327 if(empty($this->displayName)){
328 $message[] = _("You must specify the 'Display Name' in order to save this macro");
329 }
331 if(strlen("Makro-".$this->cn)>20 ){
332 $message[]=_("The given cn is too long, to create a Makro entry, maximum 20 chars.");
333 }
335 foreach($this->attributes as $attr){
336 if(chkacl($this->acl,$attr)){
337 $str = sprintf( _("Insufficient permissions, can't change attribute '%s' in goFonMacro"),$attr) ;
338 return(array($str));
339 }
340 }
342 if(count(split("\n",$this->goFonMacroContent))>100){
343 $message[] = _("Makro length must be lower than 100 lines");
344 }
346 /*Some stupid IE fixes again*/
347 if(empty ($this->base)) {
348 $message[] = _("Please choose a valid base.");
349 }
351 return $message;
352 }
355 /*!
356 Save to LDAP
357 */
358 function save()
359 {
360 /* Post checks */
361 $this->execute();
363 plugin::save();
364 unset($this->attrs['base']);
366 /* Write back to ldap */
367 $ldap= $this->config->get_ldap_link();
368 $ldap->cat($this->dn);
369 $a= $ldap->fetch();
371 if(isset($this->orig_cn)){
372 $this-> generate_mysql_entension_entries(true,false,$this->orig_cn);
373 }else{
374 $this-> generate_mysql_entension_entries(true);
375 }
377 if($this->generate_mysql_entension_entries()){
378 if (count($a)){
379 $ldap->cd($this->dn);
380 $ldap->modify($this->attrs);
381 $this->handle_post_events("modify");
382 } else {
383 $ldap->cd($this->config->current['BASE']);
384 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
385 $ldap->cd($this->dn);
386 $ldap->add($this->attrs);
387 $this->handle_post_events("add");
388 }
389 }
390 show_ldap_error($ldap->get_error());
391 }
393 }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>