Code

Fix script update
[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;
19   var $Categories = array();
21   /* Headpage attributes */
22   var $last_sorting= "invalid";
23   var $applications= array();
25   /* attribute list for save action */
26   var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName",
27                         "gosaApplicationFlags","gotoLogonScript");
28   var $objectclasses= array("top", "gosaApplication");
30   function application ($config, $dn= NULL)
31   {
32         plugin::plugin ($config, $dn);
34         $tmp = new xmlParse();
35         
36         $this->Categories= array();
37         if(!isset($this->config->data['MAIN']['KDE_APPLICATIONS_MENU']))        {
38                 print_red(_("There is no value for 'KDE_APPLICATIONS_MENU' specified in your gosa.conf."));
39         }else{
40                 $path = $this->config->data['MAIN']['KDE_APPLICATIONS_MENU'];
41                 if(file_exists($path)){
42                         if(is_readable($path)){
43                                 $tmp->parseMenu($path);
44                                 $this->Categories = $tmp->GetData();
45                         }else{
46                                 print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
47                         }
48                 }else{
49                         print_red(sprintf(_("The specified file '%s' for 'KDE_APPLICATIONS_MENU' in your gosa.conf is not accessable."),$path));
50                 }
51         }
53         /* Load icon */
54         $ldap= $config->get_ldap_link();
55         if ($dn != 'new'){
56                 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
57         }
58         if ($this->iconData == ""){
59                 $this->set_picture("");
60         }
61         $_SESSION['binary']= $this->iconData;
62         $_SESSION['binarytype']= "image/jpeg";
63         $this->gosaApplicationIcon= $this->iconData;
65         /* This is always an account */
66         $this->is_account= TRUE;
68         if ($this->dn == "new"){
69                 if(isset($_SESSION['appfilter']['depselect'])){
70                         $this->base=$_SESSION['appfilter']['depselect'];
71                 }else{
72                         $ui= get_userinfo();
73                         $this->base= dn2base($ui->dn);
74                 }
75         } else {
76                 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
77         }
78   }
81   function generateTempalte(){
82         $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
84         $values = array();
85         $names  = array();
86         if($this->parent->by_object['applicationParameters']->is_account){
87                 $names = $this->parent->by_object['applicationParameters']->option_name;
88                 $values = $this->parent->by_object['applicationParameters']->option_value;
89         }
91         if (count($names)){
92                 $str .="# This plugin handles these environment variables:\n";
93         } else {
94                 $str .="# This plugin handles no environment variables.\n";
95         }
97         foreach($names as $index => $name){
99                 // Fix length
100                 for($i = strlen($name) ; $i < 30 ; $i++){
101                         $name= $name." ";
102                 }
103                 if((isset($values[$index]))&&(!empty($values[$index]))){
104                         $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
105                 }else{
106                         $str.= "# ".$name."\t("._("no example").")\n";
107                 }
108         }
109         $str .= "#\n".
110                         "# Don't remove the following tag, it is used for header update.\n".
111                         "### END HEADER ###";
112  
113         return($str);
114   }
116   function execute()
117   {
118         /* Call parent execute */
119         plugin::execute();
121         $smarty= get_smarty();
122         $smarty->assign("categories",array_flip($this->Categories));
124         /* Do we represent a valid group? */
125         if (!$this->is_account && $this->parent == NULL){
126                  $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
127                          _("This 'dn' is no application.")."</b>";
128                  return ($display);
129         }
131 #       if(isset($_POST['ScriptTemplate'])){
132                 $head = $this->generateTempalte();
133                 $scr  = $this->gotoLogonScript; 
134                 
135                 if(preg_match("/### END HEADER ###/",$scr)){
136                         $pos = strrpos($scr,"### END HEADER ###");
137                         $pos = $pos + strlen("### END HEADER ###");
138                         $scr=substr($scr,$pos,(strlen($scr)));
139                 }
141                 $this->gotoLogonScript = $head.$scr;
142 #       }
143         
144         if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
145                 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
146                 $this->gotoLogonScript = $str;
147         }
148  
149         /* Fill templating stuff */
150         $smarty->assign("cn", $this->cn);
151         $smarty->assign("bases", $this->config->idepartments);
152         if ($this->dn == "new"){
153                 $smarty->assign("selectmode", "");
154                 $smarty->assign("namemode", "");
155         } else {
156                 $smarty->assign("namemode", "readonly");
157                 $smarty->assign("selectmode", "disabled");
158         }
160         /* Get random number for pictures */
161         srand((double)microtime()*1000000);
162         $smarty->assign("rand", rand(0, 10000));
164         /* Variables */
165         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
166                 $smarty->assign($val, $this->$val);
167                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
168         }
170         /* Checkboxes */
171         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
172                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
173                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
174                         $smarty->assign("$val", "checked");
175                 } else {
176                         $smarty->assign("$val", "");
177                 }
178         }
179         $smarty->assign("gotoLogonScript",$this->gotoLogonScript);
180         $smarty->assign("base_select", $this->base);
181         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
183         /* Show main page */
184         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
185   }
188   function remove_from_parent()
189   {
190         $ldap= $this->config->get_ldap_link();
191         $ldap->rmDir($this->dn);
193         /* Optionally execute a command after we're done */
194         $this->handle_post_events("remove");
196         /* Delete references to object groups */
197         $ldap->cd ($this->config->current['BASE']);
198         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
199         while ($ldap->fetch()){
200                 $og= new ogroup($this->config, $ldap->getDN());
201                 unset($og->member[$this->dn]);
202                 $og->save ();
203         }
204         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
205         while ($attrs= $ldap->fetch()){
206                 $ag= new appgroup($this->config, $ldap->getDN());
207                 $ag->removeApp($this->cn);
208                 $ag->save ();
209         }
211   }
214   /* Save data to object */
215   function save_object()
216   {
217         if (isset($_POST['cn'])){
218         
219                 plugin::save_object();
221                 /* Save application flags */
222                 $flag= "";
223                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
224                         $flag.= "G";
225                 }
226                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
227                         $flag.= "D";
228                 }
229                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
230                         $flag.= "M";
231                 }
232                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
233                         $flag.= "O";
234                 }
235                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
236                         $this->gosaApplicationFlags= "[$flag]";
237                 }
239                 /* Check for picture upload */
240                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
241                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
242                                 print_red (_("The specified picture has not been uploaded correctly."));
243                         }
244         
245                         if (!function_exists("imagick_blob2image")){
246                                 /* Get temporary file name for conversation */
247                                 $fname = tempnam ("/tmp", "GOsa");
249                                 /* Open file and write out photoData */
250                                 $fp = fopen ($fname, "w");
251                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
252                                 fclose ($fp);
254                                 /* Build conversation query. Filename is generated automatically, so
255                                    we do not need any special security checks. Exec command and save
256                                    output. For PHP safe mode, you'll need a configuration which respects
257                                    image magick as executable... */
258                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
259                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
261                                 /* Read data written by convert */
262                                 $output= "";
263                                 $sh= popen($query, 'r');
264                                 while (!feof($sh)){
265                                         $output.= fread($sh, 4096);
266                                 }
267                                 pclose($sh);
269                                 unlink($fname); 
270                         } else {
272                                 /* Load the new uploaded Photo */
273                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
274                                   gosa_log("Can't Load image");
275                                 }
277                                 /* Resizing image to 147x200 and blur */
278                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
279                                   gosa_log("imagick_resize failed");
280                                 }
282                                 /* Converting image to JPEG */
283                                 if(!imagick_convert($handle,"PNG")) {
284                                   gosa_log("Can't Convert to PNG");
285                                 }
287                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
288                                   gosa_log("can't write to specified folder");
289                                 }
290                                 
291                                 imagick_free($handle);
292                         }
294                         /* Activate new picture */
295                         $this->set_picture($_FILES['picture_file']['tmp_name']);
296                         }       
299                 /* Save base, since this is no LDAP attribute */
300                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
301                         $this->base= $_POST['base'];
302                 }
303         }
304   }
307   /* Check values */
308   function check()
309   {
310         $message= array();
312         /* Permissions for that base? */
313         if ($this->base != ""){
314                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
315         } else {
316                 $new_dn= $this->dn;
317         }
319         $ui= get_userinfo();
320         $acl= get_permissions ($new_dn, $ui->subtreeACL);
321         $acl= get_module_permission($acl, "application", $new_dn);
322         if (chkacl($acl, "create") != ""){
323                 $message[]= _("You have no permissions to create a application on this 'Base'.");
324         }
326         /* All required fields are set? */
327         if ($this->cn == ""){
328                 $message[]= _("Required field 'Name' is not filled.");
329         }
331         if(preg_match("/[^a-z0-9]/",$this->cn)) {
332                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
333         }
335         if ($this->gosaApplicationExecute == ""){
336                 $message[]= _("Required field 'Execute' is not filled.");
337         }
339         /* Check for existing application */
340         $ldap= $this->config->get_ldap_link();
341         $ldap->cd($this->config->current["BASE"]);
342         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
343         $ldap->fetch();
344         if ($ldap->count() != 0 && $this->dn == "new"){
345                 $message[]= _("There's already an application with this 'Name'.");
346         }
348         return $message;
349   }
352   /* Save to LDAP */
353   function save()
354   {
355         plugin::save();
356                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
358         /* Write back to ldap */
359         $ldap= $this->config->get_ldap_link();
360         $ldap->cat($this->dn);
361         $a= $ldap->fetch();
362         if (count($a)){
363                 $ldap->cd($this->dn);
364                         $ldap->modify($this->attrs);
365                         $this->handle_post_events("modify");
366         } else {
367                         $ldap->cd($this->config->current['BASE']);
368                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
369                 $ldap->cd($this->dn);
370                         $ldap->add($this->attrs);
371                         $this->handle_post_events("add");
372         }
373         show_ldap_error($ldap->get_error());
374   }
376   function set_picture($filename)
377   {
378         if (!is_file($filename)){
379                 $filename= "./images/default_icon.png";
380                 $this->gosaApplicationIcon= "*removed*";
381         }
383         if (file_exists($filename)){
384                 $fd = fopen ($filename, "rb");
385                 $this->iconData= fread ($fd, filesize ($filename));
386                 $_SESSION['binary']= $this->iconData;
387                 $_SESSION['binarytype']= "image/jpeg";
388                 $this->gosaApplicationIcon= $this->iconData;
390                 fclose ($fd);
391         }
392   }
397 ?>