Code

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