Code

Added some functionalty
[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 $iconData;
19   /* Headpage attributes */
20   var $last_sorting= "invalid";
21   var $applications= array();
23   /* attribute list for save action */
24   var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName",
25                         "gosaApplicationFlags");
26   var $objectclasses= array("top", "gosaApplication");
28   function application ($config, $dn= NULL)
29   {
30         plugin::plugin ($config, $dn);
32         /* Load icon */
33         $ldap= $config->get_ldap_link();
34         if ($dn != 'new'){
35                 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
36         }
37         if ($this->iconData == ""){
38                 $this->set_picture("");
39         }
40         $_SESSION['picture']= $this->iconData;
41         $this->gosaApplicationIcon= $this->iconData;
43         /* This is always an account */
44         $this->is_account= TRUE;
46         if ($this->dn == "new"){
47                 if(isset($_SESSION['appfilter']['depselect'])){
48                         $this->base=$_SESSION['appfilter']['depselect'];
49                 }else{
50                         $ui= get_userinfo();
51                         $this->base= dn2base($ui->dn);
52                 }
53         } else {
54                 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
55         }
56   }
58   function execute()
59   {
60         /* Do we represent a valid group? */
61         if (!$this->is_account && $this->parent == NULL){
62                  $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
63                          _("This 'dn' is no application.")."</b>";
64                  return ($display);
65         }
66   
67         /* Fill templating stuff */
68         $smarty= get_smarty();
69         $smarty->assign("cn", $this->cn);
70         $smarty->assign("bases", $this->config->idepartments);
71         if ($this->dn == "new"){
72                 $smarty->assign("selectmode", "");
73                 $smarty->assign("namemode", "");
74         } else {
75                 $smarty->assign("namemode", "readonly");
76                 $smarty->assign("selectmode", "disabled");
77         }
79         /* Get random number for pictures */
80         srand((double)microtime()*1000000);
81         $smarty->assign("rand", rand(0, 10000));
83         /* Variables */
84         foreach(array("description", "gosaApplicationExecute", "gosaApplicationName") as $val){
85                 $smarty->assign($val, $this->$val);
86                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
87         }
89         /* Checkboxes */
90         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
91                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
92                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
93                         $smarty->assign("$val", "checked");
94                 } else {
95                         $smarty->assign("$val", "");
96                 }
97         }
98         $smarty->assign("base_select", $this->base);
99         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
101         /* Show main page */
102         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
103   }
106   function remove_from_parent()
107   {
108         $ldap= $this->config->get_ldap_link();
109         $ldap->rmDir($this->dn);
111         /* Optionally execute a command after we're done */
112         $this->handle_post_events("remove");
114         /* Delete references to object groups */
115         $ldap->cd ($this->config->current['BASE']);
116         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
117         while ($ldap->fetch()){
118                 $og= new ogroup($this->config, $ldap->getDN());
119                 unset($og->member[$this->dn]);
120                 $og->save ();
121         }
122         $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
123         while ($attrs= $ldap->fetch()){
124                 $ag= new appgroup($this->config, $ldap->getDN());
125                 $ag->removeApp($this->cn);
126                 $ag->save ();
127         }
129   }
132   /* Save data to object */
133   function save_object()
134   {
135         if (isset($_POST['cn'])){
136         
137                 plugin::save_object();
139                 /* Save application flags */
140                 $flag= "";
141                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
142                         $flag.= "G";
143                 }
144                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
145                         $flag.= "D";
146                 }
147                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
148                         $flag.= "M";
149                 }
150                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
151                         $flag.= "O";
152                 }
153                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
154                         $this->gosaApplicationFlags= "[$flag]";
155                 }
157                 /* Check for picture upload */
158                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
159                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
160                                 print_red (_("The specified picture has not been uploaded correctly."));
161                         }
162         
163                         if (!function_exists("imagick_blob2image")){
164                                 /* Get temporary file name for conversation */
165                                 $fname = tempnam ("/tmp", "GOsa");
167                                 /* Open file and write out photoData */
168                                 $fp = fopen ($fname, "w");
169                                 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
170                                 fclose ($fp);
172                                 /* Build conversation query. Filename is generated automatically, so
173                                    we do not need any special security checks. Exec command and save
174                                    output. For PHP safe mode, you'll need a configuration which respects
175                                    image magick as executable... */
176                                 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
177                                 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
179                                 /* Read data written by convert */
180                                 $output= "";
181                                 $sh= popen($query, 'r');
182                                 while (!feof($sh)){
183                                         $output.= fread($sh, 4096);
184                                 }
185                                 pclose($sh);
187                                 unlink($fname); 
188                         } else {
190                                 /* Load the new uploaded Photo */
191                                 if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
192                                   gosa_log("Can't Load image");
193                                 }
195                                 /* Resizing image to 147x200 and blur */
196                                 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
197                                   gosa_log("imagick_resize failed");
198                                 }
200                                 /* Converting image to JPEG */
201                                 if(!imagick_convert($handle,"PNG")) {
202                                   gosa_log("Can't Convert to PNG");
203                                 }
205                                 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
206                                   gosa_log("can't write to specified folder");
207                                 }
208                                 
209                                 imagick_free($handle);
210                         }
212                         /* Activate new picture */
213                         $this->set_picture($_FILES['picture_file']['tmp_name']);
214                         }       
217                 /* Save base, since this is no LDAP attribute */
218                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
219                         $this->base= $_POST['base'];
220                 }
221         }
222   }
225   /* Check values */
226   function check()
227   {
228         $message= array();
230         /* Permissions for that base? */
231         if ($this->base != ""){
232                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
233         } else {
234                 $new_dn= $this->dn;
235         }
237         $ui= get_userinfo();
238         $acl= get_permissions ($new_dn, $ui->subtreeACL);
239         $acl= get_module_permission($acl, "application", $new_dn);
240         if (chkacl($acl, "create") != ""){
241                 $message[]= _("You have no permissions to create a application on this 'Base'.");
242         }
244         /* All required fields are set? */
245         if ($this->cn == ""){
246                 $message[]= _("Required field 'Name' is not filled.");
247         }
249         if(preg_match("/[^a-z0-9]/",$this->cn)) {
250                 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
251         }
253         if ($this->gosaApplicationExecute == ""){
254                 $message[]= _("Required field 'Execute' is not filled.");
255         }
257         /* Check for existing application */
258         $ldap= $this->config->get_ldap_link();
259         $ldap->cd($this->config->current["BASE"]);
260         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
261         $ldap->fetch();
262         if ($ldap->count() != 0 && $this->dn == "new"){
263                 $message[]= _("There's already an application with this 'Name'.");
264         }
266         return $message;
267   }
270   /* Save to LDAP */
271   function save()
272   {
273         plugin::save();
274                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
276         /* Write back to ldap */
277         $ldap= $this->config->get_ldap_link();
278         $ldap->cat($this->dn);
279         $a= $ldap->fetch();
280         if (count($a)){
281                 $ldap->cd($this->dn);
282                         $ldap->modify($this->attrs);
283                         $this->handle_post_events("modify");
284         } else {
285                         $ldap->cd($this->config->current['BASE']);
286                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
287                 $ldap->cd($this->dn);
288                         $ldap->add($this->attrs);
289                         $this->handle_post_events("add");
290         }
291         show_ldap_error($ldap->get_error());
292   }
294   function set_picture($filename)
295   {
296         if (!is_file($filename)){
297                 $filename= "./images/default_icon.png";
298                 $this->gosaApplicationIcon= "*removed*";
299         }
301         if (file_exists($filename)){
302                 $fd = fopen ($filename, "rb");
303                 $this->iconData= fread ($fd, filesize ($filename));
304                 $_SESSION['picture']= $this->iconData;
305                 $this->gosaApplicationIcon= $this->iconData;
307                 fclose ($fd);
308         }
309   }
314 ?>