Code

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