Code

Added \$this->cleanup before all calls of $ldap->modify
[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         $head = $this->generateTemplate();
111         $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
112         
113         if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
114                 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
115                 $this->gotoLogonScript = $str;
116         }
117  
118         /* Fill templating stuff */
119         $smarty->assign("cn", $this->cn);
120         $smarty->assign("bases", $this->config->idepartments);
121         if ($this->dn == "new"){
122                 $smarty->assign("selectmode", "");
123                 $smarty->assign("namemode", "");
124         } else {
125                 $smarty->assign("namemode", "readonly");
126                 $smarty->assign("selectmode", "disabled");
127         }
129         /* Get random number for pictures */
130         srand((double)microtime()*1000000);
131         $smarty->assign("rand", rand(0, 10000));
133         /* Variables */
134         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
135                 $smarty->assign($val, $this->$val);
136                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
137         }
139         /* Checkboxes */
140         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
141                         "L" => "place_on_kicker",
142                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
143                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
144                         $smarty->assign("$val", "checked");
145                 } else {
146                         $smarty->assign("$val", "");
147                 }
148         }
149         $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript));
150         $smarty->assign("base_select", $this->base);
151         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
152         /* Show main page */
153         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
154   }
157   function remove_from_parent()
158   {
159         $ldap= $this->config->get_ldap_link();
160         $ldap->rmDir($this->dn);
162         /* Optionally execute a command after we're done */
163         $this->handle_post_events("remove");
165         /* Delete references to object groups */
166         $ldap->cd ($this->config->current['BASE']);
167         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
168         while ($ldap->fetch()){
169                 $og= new ogroup($this->config, $ldap->getDN());
170                 unset($og->member[$this->dn]);
171                 $og->save ();
172         }
173         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
174         while ($attrs= $ldap->fetch()){
175                 $ag= new appgroup($this->config, $ldap->getDN());
176                 $ag->removeApp($this->cn);
177                 $ag->save ();
178         }
180   }
183   /* Save data to object */
184   function save_object()
185   {
186         if (isset($_POST['cn'])){
187         
188                 plugin::save_object();
190                 /* Save application flags */
191                 $flag= "";
192                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
193                         $flag.= "G";
194                 }
195                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
196                         $flag.= "D";
197                 }
198                 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
199                         $flag.= "L";
200                 }
201                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
202                         $flag.= "M";
203                 }
204                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
205                         $flag.= "O";
206                 }
207                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
208                         $this->gosaApplicationFlags= "[$flag]";
209                 }
211                 /* Check for picture upload */
212                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
213                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
214                                 print_red (_("The specified picture has not been uploaded correctly."));
215                         }
216         
217                         if (!function_exists("imagick_blob2image")){
218                                 /* Get temporary file name for conversation */
219                                 $fname = tempnam ("/tmp", "GOsa");
221                                 /* Open file and write out photoData */
222                                 $fp = fopen ($fname, "w");
223                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
224                                 fclose ($fp);
226                                 /* Build conversation query. Filename is generated automatically, so
227                                    we do not need any special security checks. Exec command and save
228                                    output. For PHP safe mode, you'll need a configuration which respects
229                                    image magick as executable... */
230                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
231                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
233                                 /* Read data written by convert */
234                                 $output= "";
235                                 $sh= popen($query, 'r');
236                                 while (!feof($sh)){
237                                         $output.= fread($sh, 4096);
238                                 }
239                                 pclose($sh);
241                                 unlink($fname); 
242                         } else {
244                                 /* Load the new uploaded Photo */
245                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
246                                   gosa_log("Can't Load image");
247                                 }
249                                 /* Resizing image to 147x200 and blur */
250                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
251                                   gosa_log("imagick_resize failed");
252                                 }
254                                 /* Converting image to JPEG */
255                                 if(!imagick_convert($handle,"PNG")) {
256                                   gosa_log("Can't Convert to PNG");
257                                 }
259                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
260                                   gosa_log("can't write to specified folder");
261                                 }
262                                 
263                                 imagick_free($handle);
264                         }
266                         /* Activate new picture */
267                         $this->set_picture($_FILES['picture_file']['tmp_name']);
268                         }       
271                 /* Save base, since this is no LDAP attribute */
272                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
273                         $this->base= $_POST['base'];
274                 }
275         }
276   }
279   /* Check values */
280   function check()
281   {
282         $message= array();
284         /* Permissions for that base? */
285         if ($this->base != ""){
286                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
287         } else {
288                 $new_dn= $this->dn;
289         }
291         $ui= get_userinfo();
292         $acl= get_permissions ($new_dn, $ui->subtreeACL);
293         $acl= get_module_permission($acl, "application", $new_dn);
294         if (chkacl($acl, "create") != ""){
295                 $message[]= _("You have no permissions to create a application on this 'Base'.");
296         }
298         /* All required fields are set? */
299         if ($this->cn == ""){
300                 $message[]= _("Required field 'Name' is not filled.");
301         }
303         if(preg_match("/[^a-z0-9]/",$this->cn)) {
304                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
305         }
307         if ($this->gosaApplicationExecute == ""){
308                 $message[]= _("Required field 'Execute' is not filled.");
309         }
311         /* Check for existing application */
312         $ldap= $this->config->get_ldap_link();
313         $ldap->cd($this->config->current["BASE"]);
314         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))",array("cn"));
315         $ldap->fetch();
316         if ($ldap->count() != 0 && $this->dn == "new"){
317                 $message[]= _("There's already an application with this 'Name'.");
318         }
320         return $message;
321   }
324   /* Save to LDAP */
325   function save()
326   {
327         plugin::save();
328                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
330         /* Write back to ldap */
331         $ldap= $this->config->get_ldap_link();
332         $ldap->cat($this->dn);
333         $a= $ldap->fetch();
334         if (count($a)){
335                 $ldap->cd($this->dn);
336                         $this->cleanup();
337 $ldap->modify ($this->attrs); 
339                         $this->handle_post_events("modify");
340         } else {
341                         $ldap->cd($this->config->current['BASE']);
342                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
343                 $ldap->cd($this->dn);
344                         $ldap->add($this->attrs);
345                         $this->handle_post_events("add");
346         }
347         show_ldap_error($ldap->get_error());
348   }
350   function set_picture($filename)
351   {
352         if (!is_file($filename)){
353                 $filename= "./images/default_icon.png";
354                 $this->gosaApplicationIcon= "*removed*";
355         }
357         if (file_exists($filename)){
358                 $fd = fopen ($filename, "rb");
359                 $this->iconData= fread ($fd, filesize ($filename));
360                 $_SESSION['binary']= $this->iconData;
361                 $_SESSION['binarytype']= "image/jpeg";
362                 $this->gosaApplicationIcon= $this->iconData;
364                 fclose ($fd);
365         }
366   }
371 ?>