Code

* Integrated Jan's changes for binary downloads
[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   }
60   function generateTempalte(){
61         $str =  "# This code is part of GOsa (https://gosa.gonicus.de)\n".
62                         "#\n".
63             "# This program is free software; you can redistribute it and/or modify\n".
64             "# it under the terms of the GNU General Public License as published by\n".
65             "# the Free Software Foundation; either version 2 of the License, or\n".
66             "# (at your option) any later version.\n".
67             "#\n".
68             "# This program is distributed in the hope that it will be useful,\n".
69             "# but WITHOUT ANY WARRANTY; without even the implied warranty of\n".
70             "# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n".
71             "# GNU General Public License for more details.\n".
72             "#\n".
73             "# You should have received a copy of the GNU General Public License\n".
74             "# along with this program; if not, write to the Free Software\n".
75             "# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n".
76             "#\n";
78         $vars = ($this->attrs['gosaApplicationParameter']);
79         unset($vars['count']);
82         $str .="# This plugin handles these environment variables:\n";
84         foreach($vars as $var){
85                 if(preg_match("/\:/",$var)){
86                         $stra   = split(":",$var);
87                         $Name   = $stra[0];
88                         $Value  = $stra[1];
89                 }else{
90                         $Name   = $var;
91                         $Value  = "";
92                 }
93                 for($i = strlen($Name);$i < 25 ; $i++){
94                         $Name.=" ";
95                 }
96                 if(!empty($Value)){
97                         $str.= "# ".$Name."\t\t(e.g. ".$Value.")\n";
98                 }else{
99                         $str.= "# ".$Name."\n";
100                 }
101         }
102         $str .= "#\n".
103                         "# Don't remove the following tag, it is used for header update.\n".
104                         "### END HEADER ###";
105  
106         return($str);
107   }
109   function execute()
110   {
111         /* Do we represent a valid group? */
112         if (!$this->is_account && $this->parent == NULL){
113                  $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
114                          _("This 'dn' is no application.")."</b>";
115                  return ($display);
116         }
118         if(isset($_POST['ScriptTemplate'])){
119                 $head = $this->generateTempalte();
120                 $scr  = $this->gotoLogonScript; 
121                 
122                 if(preg_match("/### END HEADER ###/",$scr)){
123                         $pos = strrpos($scr,"### END HEADER ###");
124                         $pos = $pos + strlen("### END HEADER ###");
125                         $scr=substr($scr,$pos,(strlen($scr)));
126                 }
128                 $this->gotoLogonScript = $head.$scr;
129         }
130         
131         if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
132                 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
133                 $this->gotoLogonScript = $str;
134         }
135  
136         /* Fill templating stuff */
137         $smarty= get_smarty();
138         $smarty->assign("cn", $this->cn);
139         $smarty->assign("bases", $this->config->idepartments);
140         if ($this->dn == "new"){
141                 $smarty->assign("selectmode", "");
142                 $smarty->assign("namemode", "");
143         } else {
144                 $smarty->assign("namemode", "readonly");
145                 $smarty->assign("selectmode", "disabled");
146         }
148         /* Get random number for pictures */
149         srand((double)microtime()*1000000);
150         $smarty->assign("rand", rand(0, 10000));
152         /* Variables */
153         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
154                 $smarty->assign($val, $this->$val);
155                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
156         }
158         /* Checkboxes */
159         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
160                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
161                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
162                         $smarty->assign("$val", "checked");
163                 } else {
164                         $smarty->assign("$val", "");
165                 }
166         }
167         $smarty->assign("gotoLogonScript",$this->gotoLogonScript);
168         $smarty->assign("base_select", $this->base);
169         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
171         /* Show main page */
172         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
173   }
176   function remove_from_parent()
177   {
178         $ldap= $this->config->get_ldap_link();
179         $ldap->rmDir($this->dn);
181         /* Optionally execute a command after we're done */
182         $this->handle_post_events("remove");
184         /* Delete references to object groups */
185         $ldap->cd ($this->config->current['BASE']);
186         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
187         while ($ldap->fetch()){
188                 $og= new ogroup($this->config, $ldap->getDN());
189                 unset($og->member[$this->dn]);
190                 $og->save ();
191         }
192         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
193         while ($attrs= $ldap->fetch()){
194                 $ag= new appgroup($this->config, $ldap->getDN());
195                 $ag->removeApp($this->cn);
196                 $ag->save ();
197         }
199   }
202   /* Save data to object */
203   function save_object()
204   {
205         if (isset($_POST['cn'])){
206         
207                 plugin::save_object();
209                 /* Save application flags */
210                 $flag= "";
211                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
212                         $flag.= "G";
213                 }
214                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
215                         $flag.= "D";
216                 }
217                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
218                         $flag.= "M";
219                 }
220                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
221                         $flag.= "O";
222                 }
223                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
224                         $this->gosaApplicationFlags= "[$flag]";
225                 }
227                 /* Check for picture upload */
228                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
229                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
230                                 print_red (_("The specified picture has not been uploaded correctly."));
231                         }
232         
233                         if (!function_exists("imagick_blob2image")){
234                                 /* Get temporary file name for conversation */
235                                 $fname = tempnam ("/tmp", "GOsa");
237                                 /* Open file and write out photoData */
238                                 $fp = fopen ($fname, "w");
239                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
240                                 fclose ($fp);
242                                 /* Build conversation query. Filename is generated automatically, so
243                                    we do not need any special security checks. Exec command and save
244                                    output. For PHP safe mode, you'll need a configuration which respects
245                                    image magick as executable... */
246                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
247                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
249                                 /* Read data written by convert */
250                                 $output= "";
251                                 $sh= popen($query, 'r');
252                                 while (!feof($sh)){
253                                         $output.= fread($sh, 4096);
254                                 }
255                                 pclose($sh);
257                                 unlink($fname); 
258                         } else {
260                                 /* Load the new uploaded Photo */
261                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
262                                   gosa_log("Can't Load image");
263                                 }
265                                 /* Resizing image to 147x200 and blur */
266                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
267                                   gosa_log("imagick_resize failed");
268                                 }
270                                 /* Converting image to JPEG */
271                                 if(!imagick_convert($handle,"PNG")) {
272                                   gosa_log("Can't Convert to PNG");
273                                 }
275                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
276                                   gosa_log("can't write to specified folder");
277                                 }
278                                 
279                                 imagick_free($handle);
280                         }
282                         /* Activate new picture */
283                         $this->set_picture($_FILES['picture_file']['tmp_name']);
284                         }       
287                 /* Save base, since this is no LDAP attribute */
288                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
289                         $this->base= $_POST['base'];
290                 }
291         }
292   }
295   /* Check values */
296   function check()
297   {
298         $message= array();
300         /* Permissions for that base? */
301         if ($this->base != ""){
302                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
303         } else {
304                 $new_dn= $this->dn;
305         }
307         $ui= get_userinfo();
308         $acl= get_permissions ($new_dn, $ui->subtreeACL);
309         $acl= get_module_permission($acl, "application", $new_dn);
310         if (chkacl($acl, "create") != ""){
311                 $message[]= _("You have no permissions to create a application on this 'Base'.");
312         }
314         /* All required fields are set? */
315         if ($this->cn == ""){
316                 $message[]= _("Required field 'Name' is not filled.");
317         }
319         if(preg_match("/[^a-z0-9]/",$this->cn)) {
320                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
321         }
323         if ($this->gosaApplicationExecute == ""){
324                 $message[]= _("Required field 'Execute' is not filled.");
325         }
327         /* Check for existing application */
328         $ldap= $this->config->get_ldap_link();
329         $ldap->cd($this->config->current["BASE"]);
330         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
331         $ldap->fetch();
332         if ($ldap->count() != 0 && $this->dn == "new"){
333                 $message[]= _("There's already an application with this 'Name'.");
334         }
336         return $message;
337   }
340   /* Save to LDAP */
341   function save()
342   {
343         plugin::save();
344                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
346         /* Write back to ldap */
347         $ldap= $this->config->get_ldap_link();
348         $ldap->cat($this->dn);
349         $a= $ldap->fetch();
350         if (count($a)){
351                 $ldap->cd($this->dn);
352                         $ldap->modify($this->attrs);
353                         $this->handle_post_events("modify");
354         } else {
355                         $ldap->cd($this->config->current['BASE']);
356                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
357                 $ldap->cd($this->dn);
358                         $ldap->add($this->attrs);
359                         $this->handle_post_events("add");
360         }
361         show_ldap_error($ldap->get_error());
362   }
364   function set_picture($filename)
365   {
366         if (!is_file($filename)){
367                 $filename= "./images/default_icon.png";
368                 $this->gosaApplicationIcon= "*removed*";
369         }
371         if (file_exists($filename)){
372                 $fd = fopen ($filename, "rb");
373                 $this->iconData= fread ($fd, filesize ($filename));
374                 $_SESSION['binary']= $this->iconData;
375                 $_SESSION['binarytype']= "image/jpeg";
376                 $this->gosaApplicationIcon= $this->iconData;
378                 fclose ($fd);
379         }
380   }
385 ?>