Code

Added empty lines
[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#\n";
64         $names = $this->parent->by_object['applicationParameters']->option_name;
65         $values = $this->parent->by_object['applicationParameters']->option_value;
67         if (count($names)){
68                 $str .="# This plugin handles these environment variables:\n";
69         } else {
70                 $str .="# This plugin handles no environment variables.\n";
71         }
73         foreach($names as $index => $name){
75                 // Fix length
76                 for($i = strlen($name) ; $i < 30 ; $i++){
77                         $name= $name." ";
78                 }
79                 if((isset($values[$index]))&&(!empty($values[$index]))){
80                         $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
81                 }else{
82                         $str.= "# ".$name."\t("._("no example").")\n";
83                 }
84         }
85         $str .= "#\n".
86                         "# Don't remove the following tag, it is used for header update.\n".
87                         "### END HEADER ###";
88  
89         return($str);
90   }
92   function execute()
93   {
94         /* Call parent execute */
95         plugin::execute();
97         /* Do we represent a valid group? */
98         if (!$this->is_account && $this->parent == NULL){
99                  $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
100                          _("This 'dn' is no application.")."</b>";
101                  return ($display);
102         }
104 #       if(isset($_POST['ScriptTemplate'])){
105                 $head = $this->generateTempalte();
106                 $scr  = $this->gotoLogonScript; 
107                 
108                 if(preg_match("/### END HEADER ###/",$scr)){
109                         $pos = strrpos($scr,"### END HEADER ###");
110                         $pos = $pos + strlen("### END HEADER ###");
111                         $scr=substr($scr,$pos,(strlen($scr)));
112                 }
114                 $this->gotoLogonScript = $head.$scr;
115 #       }
116         
117         if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
118                 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
119                 $this->gotoLogonScript = $str;
120         }
121  
122         /* Fill templating stuff */
123         $smarty= get_smarty();
124         $smarty->assign("cn", $this->cn);
125         $smarty->assign("bases", $this->config->idepartments);
126         if ($this->dn == "new"){
127                 $smarty->assign("selectmode", "");
128                 $smarty->assign("namemode", "");
129         } else {
130                 $smarty->assign("namemode", "readonly");
131                 $smarty->assign("selectmode", "disabled");
132         }
134         /* Get random number for pictures */
135         srand((double)microtime()*1000000);
136         $smarty->assign("rand", rand(0, 10000));
138         /* Variables */
139         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
140                 $smarty->assign($val, $this->$val);
141                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
142         }
144         /* Checkboxes */
145         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
146                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
147                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
148                         $smarty->assign("$val", "checked");
149                 } else {
150                         $smarty->assign("$val", "");
151                 }
152         }
153         $smarty->assign("gotoLogonScript",$this->gotoLogonScript);
154         $smarty->assign("base_select", $this->base);
155         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
157         /* Show main page */
158         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
159   }
162   function remove_from_parent()
163   {
164         $ldap= $this->config->get_ldap_link();
165         $ldap->rmDir($this->dn);
167         /* Optionally execute a command after we're done */
168         $this->handle_post_events("remove");
170         /* Delete references to object groups */
171         $ldap->cd ($this->config->current['BASE']);
172         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
173         while ($ldap->fetch()){
174                 $og= new ogroup($this->config, $ldap->getDN());
175                 unset($og->member[$this->dn]);
176                 $og->save ();
177         }
178         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
179         while ($attrs= $ldap->fetch()){
180                 $ag= new appgroup($this->config, $ldap->getDN());
181                 $ag->removeApp($this->cn);
182                 $ag->save ();
183         }
185   }
188   /* Save data to object */
189   function save_object()
190   {
191         if (isset($_POST['cn'])){
192         
193                 plugin::save_object();
195                 /* Save application flags */
196                 $flag= "";
197                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
198                         $flag.= "G";
199                 }
200                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
201                         $flag.= "D";
202                 }
203                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
204                         $flag.= "M";
205                 }
206                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
207                         $flag.= "O";
208                 }
209                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
210                         $this->gosaApplicationFlags= "[$flag]";
211                 }
213                 /* Check for picture upload */
214                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
215                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
216                                 print_red (_("The specified picture has not been uploaded correctly."));
217                         }
218         
219                         if (!function_exists("imagick_blob2image")){
220                                 /* Get temporary file name for conversation */
221                                 $fname = tempnam ("/tmp", "GOsa");
223                                 /* Open file and write out photoData */
224                                 $fp = fopen ($fname, "w");
225                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
226                                 fclose ($fp);
228                                 /* Build conversation query. Filename is generated automatically, so
229                                    we do not need any special security checks. Exec command and save
230                                    output. For PHP safe mode, you'll need a configuration which respects
231                                    image magick as executable... */
232                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
233                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
235                                 /* Read data written by convert */
236                                 $output= "";
237                                 $sh= popen($query, 'r');
238                                 while (!feof($sh)){
239                                         $output.= fread($sh, 4096);
240                                 }
241                                 pclose($sh);
243                                 unlink($fname); 
244                         } else {
246                                 /* Load the new uploaded Photo */
247                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
248                                   gosa_log("Can't Load image");
249                                 }
251                                 /* Resizing image to 147x200 and blur */
252                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
253                                   gosa_log("imagick_resize failed");
254                                 }
256                                 /* Converting image to JPEG */
257                                 if(!imagick_convert($handle,"PNG")) {
258                                   gosa_log("Can't Convert to PNG");
259                                 }
261                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
262                                   gosa_log("can't write to specified folder");
263                                 }
264                                 
265                                 imagick_free($handle);
266                         }
268                         /* Activate new picture */
269                         $this->set_picture($_FILES['picture_file']['tmp_name']);
270                         }       
273                 /* Save base, since this is no LDAP attribute */
274                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
275                         $this->base= $_POST['base'];
276                 }
277         }
278   }
281   /* Check values */
282   function check()
283   {
284         $message= array();
286         /* Permissions for that base? */
287         if ($this->base != ""){
288                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
289         } else {
290                 $new_dn= $this->dn;
291         }
293         $ui= get_userinfo();
294         $acl= get_permissions ($new_dn, $ui->subtreeACL);
295         $acl= get_module_permission($acl, "application", $new_dn);
296         if (chkacl($acl, "create") != ""){
297                 $message[]= _("You have no permissions to create a application on this 'Base'.");
298         }
300         /* All required fields are set? */
301         if ($this->cn == ""){
302                 $message[]= _("Required field 'Name' is not filled.");
303         }
305         if(preg_match("/[^a-z0-9]/",$this->cn)) {
306                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
307         }
309         if ($this->gosaApplicationExecute == ""){
310                 $message[]= _("Required field 'Execute' is not filled.");
311         }
313         /* Check for existing application */
314         $ldap= $this->config->get_ldap_link();
315         $ldap->cd($this->config->current["BASE"]);
316         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
317         $ldap->fetch();
318         if ($ldap->count() != 0 && $this->dn == "new"){
319                 $message[]= _("There's already an application with this 'Name'.");
320         }
322         return $message;
323   }
326   /* Save to LDAP */
327   function save()
328   {
329         plugin::save();
330                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
332         /* Write back to ldap */
333         $ldap= $this->config->get_ldap_link();
334         $ldap->cat($this->dn);
335         $a= $ldap->fetch();
336         if (count($a)){
337                 $ldap->cd($this->dn);
338                         $ldap->modify($this->attrs);
339                         $this->handle_post_events("modify");
340         } else {
341                         $ldap->cd($this->config->current['BASE']);
342                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
343                 $ldap->cd($this->dn);
344                         $ldap->add($this->attrs);
345                         $this->handle_post_events("add");
346         }
347         show_ldap_error($ldap->get_error());
348   }
350   function set_picture($filename)
351   {
352         if (!is_file($filename)){
353                 $filename= "./images/default_icon.png";
354                 $this->gosaApplicationIcon= "*removed*";
355         }
357         if (file_exists($filename)){
358                 $fd = fopen ($filename, "rb");
359                 $this->iconData= fread ($fd, filesize ($filename));
360                 $_SESSION['binary']= $this->iconData;
361                 $_SESSION['binarytype']= "image/jpeg";
362                 $this->gosaApplicationIcon= $this->iconData;
364                 fclose ($fd);
365         }
366   }
371 ?>