Code

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