Code

Added FAIprofile selection for Workstations
[gosa.git] / plugins / admin / applications / class_applicationGeneric.inc
1 <?php
2 class application extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Handling of GOsa's application object";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* application attributes */
10   var $cn= "";
11   var $description= "";
12   var $base= "";
13   var $gosaApplicationExecute= "";
14   var $gosaApplicationName= "";
15   var $gosaApplicationFlags= "";
16   var $gosaApplicationIcon= "";
17   var $gotoLogonScript  ="";
18   var $iconData;
20   /* Headpage attributes */
21   var $last_sorting= "invalid";
22   var $applications= array();
24   /* attribute list for save action */
25   var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName",
26                         "gosaApplicationFlags","gotoLogonScript");
27   var $objectclasses= array("top", "gosaApplication");
29   function application ($config, $dn= NULL)
30   {
31         plugin::plugin ($config, $dn);
33         /* Load icon */
34         $ldap= $config->get_ldap_link();
35         if ($dn != 'new'){
36                 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
37         }
38         if ($this->iconData == ""){
39                 $this->set_picture("");
40         }
41         $_SESSION['binary']= $this->iconData;
42         $_SESSION['binarytype']= "image/jpeg";
43         $this->gosaApplicationIcon= $this->iconData;
45         /* This is always an account */
46         $this->is_account= TRUE;
48         if ($this->dn == "new"){
49                 if(isset($_SESSION['appfilter']['depselect'])){
50                         $this->base=$_SESSION['appfilter']['depselect'];
51                 }else{
52                         $ui= get_userinfo();
53                         $this->base= dn2base($ui->dn);
54                 }
55         } else {
56                 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
57         }
58   }
61   function generateTempalte(){
62         $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n".
63             "#\n".
64             "# This program is free software; you can redistribute it and/or modify\n".
65             "# it under the terms of the GNU General Public License as published by\n".
66             "# the Free Software Foundation; either version 2 of the License, or\n".
67             "# (at your option) any later version.\n".
68             "#\n";
70         $names = $this->parent->by_object['applicationParameters']->option_name;
71         $values = $this->parent->by_object['applicationParameters']->option_value;
73         if (count($names)){
74                 $str .="# This plugin handles these environment variables:\n";
75         } else {
76                 $str .="# This plugin handles no environment variables.\n";
77         }
79         foreach($names as $index => $name){
81                 // Fix length
82                 if (strlen($name) < 35){
83                         $name= sprintf("%.35s", $name);
84                 }
85                 if(isset($values[$index])){
86                         $str.= "# ".$name."\t\t(e.g. ".$values[$index].")\n";
87                 }else{
88                         $str.= "# ".$name."\n";
89                 }
90         }
91         $str .= "#\n".
92                         "# Don't remove the following tag, it is used for header update.\n".
93                         "### END HEADER ###";
94  
95         return($str);
96   }
98   function execute()
99   {
100         /* Do we represent a valid group? */
101         if (!$this->is_account && $this->parent == NULL){
102                  $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
103                          _("This 'dn' is no application.")."</b>";
104                  return ($display);
105         }
107         if(isset($_POST['ScriptTemplate'])){
108                 $head = $this->generateTempalte();
109                 $scr  = $this->gotoLogonScript; 
110                 
111                 if(preg_match("/### END HEADER ###/",$scr)){
112                         $pos = strrpos($scr,"### END HEADER ###");
113                         $pos = $pos + strlen("### END HEADER ###");
114                         $scr=substr($scr,$pos,(strlen($scr)));
115                 }
117                 $this->gotoLogonScript = $head.$scr;
118         }
119         
120         if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
121                 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
122                 $this->gotoLogonScript = $str;
123         }
124  
125         /* Fill templating stuff */
126         $smarty= get_smarty();
127         $smarty->assign("cn", $this->cn);
128         $smarty->assign("bases", $this->config->idepartments);
129         if ($this->dn == "new"){
130                 $smarty->assign("selectmode", "");
131                 $smarty->assign("namemode", "");
132         } else {
133                 $smarty->assign("namemode", "readonly");
134                 $smarty->assign("selectmode", "disabled");
135         }
137         /* Get random number for pictures */
138         srand((double)microtime()*1000000);
139         $smarty->assign("rand", rand(0, 10000));
141         /* Variables */
142         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
143                 $smarty->assign($val, $this->$val);
144                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
145         }
147         /* Checkboxes */
148         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
149                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
150                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
151                         $smarty->assign("$val", "checked");
152                 } else {
153                         $smarty->assign("$val", "");
154                 }
155         }
156         $smarty->assign("gotoLogonScript",$this->gotoLogonScript);
157         $smarty->assign("base_select", $this->base);
158         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
160         /* Show main page */
161         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
162   }
165   function remove_from_parent()
166   {
167         $ldap= $this->config->get_ldap_link();
168         $ldap->rmDir($this->dn);
170         /* Optionally execute a command after we're done */
171         $this->handle_post_events("remove");
173         /* Delete references to object groups */
174         $ldap->cd ($this->config->current['BASE']);
175         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
176         while ($ldap->fetch()){
177                 $og= new ogroup($this->config, $ldap->getDN());
178                 unset($og->member[$this->dn]);
179                 $og->save ();
180         }
181         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
182         while ($attrs= $ldap->fetch()){
183                 $ag= new appgroup($this->config, $ldap->getDN());
184                 $ag->removeApp($this->cn);
185                 $ag->save ();
186         }
188   }
191   /* Save data to object */
192   function save_object()
193   {
194         if (isset($_POST['cn'])){
195         
196                 plugin::save_object();
198                 /* Save application flags */
199                 $flag= "";
200                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
201                         $flag.= "G";
202                 }
203                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
204                         $flag.= "D";
205                 }
206                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
207                         $flag.= "M";
208                 }
209                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
210                         $flag.= "O";
211                 }
212                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
213                         $this->gosaApplicationFlags= "[$flag]";
214                 }
216                 /* Check for picture upload */
217                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
218                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
219                                 print_red (_("The specified picture has not been uploaded correctly."));
220                         }
221         
222                         if (!function_exists("imagick_blob2image")){
223                                 /* Get temporary file name for conversation */
224                                 $fname = tempnam ("/tmp", "GOsa");
226                                 /* Open file and write out photoData */
227                                 $fp = fopen ($fname, "w");
228                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
229                                 fclose ($fp);
231                                 /* Build conversation query. Filename is generated automatically, so
232                                    we do not need any special security checks. Exec command and save
233                                    output. For PHP safe mode, you'll need a configuration which respects
234                                    image magick as executable... */
235                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
236                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
238                                 /* Read data written by convert */
239                                 $output= "";
240                                 $sh= popen($query, 'r');
241                                 while (!feof($sh)){
242                                         $output.= fread($sh, 4096);
243                                 }
244                                 pclose($sh);
246                                 unlink($fname); 
247                         } else {
249                                 /* Load the new uploaded Photo */
250                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
251                                   gosa_log("Can't Load image");
252                                 }
254                                 /* Resizing image to 147x200 and blur */
255                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
256                                   gosa_log("imagick_resize failed");
257                                 }
259                                 /* Converting image to JPEG */
260                                 if(!imagick_convert($handle,"PNG")) {
261                                   gosa_log("Can't Convert to PNG");
262                                 }
264                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
265                                   gosa_log("can't write to specified folder");
266                                 }
267                                 
268                                 imagick_free($handle);
269                         }
271                         /* Activate new picture */
272                         $this->set_picture($_FILES['picture_file']['tmp_name']);
273                         }       
276                 /* Save base, since this is no LDAP attribute */
277                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
278                         $this->base= $_POST['base'];
279                 }
280         }
281   }
284   /* Check values */
285   function check()
286   {
287         $message= array();
289         /* Permissions for that base? */
290         if ($this->base != ""){
291                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
292         } else {
293                 $new_dn= $this->dn;
294         }
296         $ui= get_userinfo();
297         $acl= get_permissions ($new_dn, $ui->subtreeACL);
298         $acl= get_module_permission($acl, "application", $new_dn);
299         if (chkacl($acl, "create") != ""){
300                 $message[]= _("You have no permissions to create a application on this 'Base'.");
301         }
303         /* All required fields are set? */
304         if ($this->cn == ""){
305                 $message[]= _("Required field 'Name' is not filled.");
306         }
308         if(preg_match("/[^a-z0-9]/",$this->cn)) {
309                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
310         }
312         if ($this->gosaApplicationExecute == ""){
313                 $message[]= _("Required field 'Execute' is not filled.");
314         }
316         /* Check for existing application */
317         $ldap= $this->config->get_ldap_link();
318         $ldap->cd($this->config->current["BASE"]);
319         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
320         $ldap->fetch();
321         if ($ldap->count() != 0 && $this->dn == "new"){
322                 $message[]= _("There's already an application with this 'Name'.");
323         }
325         return $message;
326   }
329   /* Save to LDAP */
330   function save()
331   {
332         plugin::save();
333                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
335         /* Write back to ldap */
336         $ldap= $this->config->get_ldap_link();
337         $ldap->cat($this->dn);
338         $a= $ldap->fetch();
339         if (count($a)){
340                 $ldap->cd($this->dn);
341                         $ldap->modify($this->attrs);
342                         $this->handle_post_events("modify");
343         } else {
344                         $ldap->cd($this->config->current['BASE']);
345                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
346                 $ldap->cd($this->dn);
347                         $ldap->add($this->attrs);
348                         $this->handle_post_events("add");
349         }
350         show_ldap_error($ldap->get_error());
351   }
353   function set_picture($filename)
354   {
355         if (!is_file($filename)){
356                 $filename= "./images/default_icon.png";
357                 $this->gosaApplicationIcon= "*removed*";
358         }
360         if (file_exists($filename)){
361                 $fd = fopen ($filename, "rb");
362                 $this->iconData= fread ($fd, filesize ($filename));
363                 $_SESSION['binary']= $this->iconData;
364                 $_SESSION['binarytype']= "image/jpeg";
365                 $this->gosaApplicationIcon= $this->iconData;
367                 fclose ($fd);
368         }
369   }
374 ?>