Code

d61cdae734c6781b99bb3090f98cb3be83590308
[gosa.git] / 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 generateTemplate(){
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();
123         /* Do we represent a valid group? */
124         if (!$this->is_account && $this->parent == NULL){
125                  $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
126                          _("This 'dn' is no application.")."</b>";
127                  return ($display);
128         }
130 #       if(isset($_POST['ScriptTemplate'])){
131                 $head = $this->generateTemplate();
132                 $scr  = $this->gotoLogonScript; 
133                 
134                 if(preg_match("/### END HEADER ###/",$scr)){
135                         $pos = strrpos($scr,"### END HEADER ###");
136                         $pos = $pos + strlen("### END HEADER ###");
137                         $scr=substr($scr,$pos,(strlen($scr)));
138                 }
140                 $this->gotoLogonScript = $head.$scr;
141 #       }
142         
143         if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
144                 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
145                 $this->gotoLogonScript = $str;
146         }
147  
148         /* Fill templating stuff */
149         $smarty->assign("cn", $this->cn);
150         $smarty->assign("bases", $this->config->idepartments);
151         if ($this->dn == "new"){
152                 $smarty->assign("selectmode", "");
153                 $smarty->assign("namemode", "");
154         } else {
155                 $smarty->assign("namemode", "readonly");
156                 $smarty->assign("selectmode", "disabled");
157         }
159         /* Get random number for pictures */
160         srand((double)microtime()*1000000);
161         $smarty->assign("rand", rand(0, 10000));
163         /* Variables */
164         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
165                 $smarty->assign($val, $this->$val);
166                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
167         }
169         /* Checkboxes */
170         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
171                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
172                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
173                         $smarty->assign("$val", "checked");
174                 } else {
175                         $smarty->assign("$val", "");
176                 }
177         }
178         $smarty->assign("gotoLogonScript",$this->gotoLogonScript);
179         $smarty->assign("base_select", $this->base);
180         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
182         /* Show main page */
183         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
184   }
187   function remove_from_parent()
188   {
189         $ldap= $this->config->get_ldap_link();
190         $ldap->rmDir($this->dn);
192         /* Optionally execute a command after we're done */
193         $this->handle_post_events("remove");
195         /* Delete references to object groups */
196         $ldap->cd ($this->config->current['BASE']);
197         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
198         while ($ldap->fetch()){
199                 $og= new ogroup($this->config, $ldap->getDN());
200                 unset($og->member[$this->dn]);
201                 $og->save ();
202         }
203         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
204         while ($attrs= $ldap->fetch()){
205                 $ag= new appgroup($this->config, $ldap->getDN());
206                 $ag->removeApp($this->cn);
207                 $ag->save ();
208         }
210   }
213   /* Save data to object */
214   function save_object()
215   {
216         if (isset($_POST['cn'])){
217         
218                 plugin::save_object();
220                 /* Save application flags */
221                 $flag= "";
222                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
223                         $flag.= "G";
224                 }
225                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
226                         $flag.= "D";
227                 }
228                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
229                         $flag.= "M";
230                 }
231                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
232                         $flag.= "O";
233                 }
234                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
235                         $this->gosaApplicationFlags= "[$flag]";
236                 }
238                 /* Check for picture upload */
239                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
240                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
241                                 print_red (_("The specified picture has not been uploaded correctly."));
242                         }
243         
244                         if (!function_exists("imagick_blob2image")){
245                                 /* Get temporary file name for conversation */
246                                 $fname = tempnam ("/tmp", "GOsa");
248                                 /* Open file and write out photoData */
249                                 $fp = fopen ($fname, "w");
250                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
251                                 fclose ($fp);
253                                 /* Build conversation query. Filename is generated automatically, so
254                                    we do not need any special security checks. Exec command and save
255                                    output. For PHP safe mode, you'll need a configuration which respects
256                                    image magick as executable... */
257                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
258                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
260                                 /* Read data written by convert */
261                                 $output= "";
262                                 $sh= popen($query, 'r');
263                                 while (!feof($sh)){
264                                         $output.= fread($sh, 4096);
265                                 }
266                                 pclose($sh);
268                                 unlink($fname); 
269                         } else {
271                                 /* Load the new uploaded Photo */
272                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
273                                   gosa_log("Can't Load image");
274                                 }
276                                 /* Resizing image to 147x200 and blur */
277                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
278                                   gosa_log("imagick_resize failed");
279                                 }
281                                 /* Converting image to JPEG */
282                                 if(!imagick_convert($handle,"PNG")) {
283                                   gosa_log("Can't Convert to PNG");
284                                 }
286                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
287                                   gosa_log("can't write to specified folder");
288                                 }
289                                 
290                                 imagick_free($handle);
291                         }
293                         /* Activate new picture */
294                         $this->set_picture($_FILES['picture_file']['tmp_name']);
295                         }       
298                 /* Save base, since this is no LDAP attribute */
299                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
300                         $this->base= $_POST['base'];
301                 }
302         }
303   }
306   /* Check values */
307   function check()
308   {
309         $message= array();
311         /* Permissions for that base? */
312         if ($this->base != ""){
313                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
314         } else {
315                 $new_dn= $this->dn;
316         }
318         $ui= get_userinfo();
319         $acl= get_permissions ($new_dn, $ui->subtreeACL);
320         $acl= get_module_permission($acl, "application", $new_dn);
321         if (chkacl($acl, "create") != ""){
322                 $message[]= _("You have no permissions to create a application on this 'Base'.");
323         }
325         /* All required fields are set? */
326         if ($this->cn == ""){
327                 $message[]= _("Required field 'Name' is not filled.");
328         }
330         if(preg_match("/[^a-z0-9]/",$this->cn)) {
331                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
332         }
334         if ($this->gosaApplicationExecute == ""){
335                 $message[]= _("Required field 'Execute' is not filled.");
336         }
338         /* Check for existing application */
339         $ldap= $this->config->get_ldap_link();
340         $ldap->cd($this->config->current["BASE"]);
341         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
342         $ldap->fetch();
343         if ($ldap->count() != 0 && $this->dn == "new"){
344                 $message[]= _("There's already an application with this 'Name'.");
345         }
347         return $message;
348   }
351   /* Save to LDAP */
352   function save()
353   {
354         plugin::save();
355                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
357         /* Write back to ldap */
358         $ldap= $this->config->get_ldap_link();
359         $ldap->cat($this->dn);
360         $a= $ldap->fetch();
361         if (count($a)){
362                 $ldap->cd($this->dn);
363                         $ldap->modify($this->attrs);
364                         $this->handle_post_events("modify");
365         } else {
366                         $ldap->cd($this->config->current['BASE']);
367                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
368                 $ldap->cd($this->dn);
369                         $ldap->add($this->attrs);
370                         $this->handle_post_events("add");
371         }
372         show_ldap_error($ldap->get_error());
373   }
375   function set_picture($filename)
376   {
377         if (!is_file($filename)){
378                 $filename= "./images/default_icon.png";
379                 $this->gosaApplicationIcon= "*removed*";
380         }
382         if (file_exists($filename)){
383                 $fd = fopen ($filename, "rb");
384                 $this->iconData= fread ($fd, filesize ($filename));
385                 $_SESSION['binary']= $this->iconData;
386                 $_SESSION['binarytype']= "image/jpeg";
387                 $this->gosaApplicationIcon= $this->iconData;
389                 fclose ($fd);
390         }
391   }
396 ?>