Code

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