Code

44c07f444b818bf795b3714e10d68ebca06dd8d8
[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 src=\"images/stop.png\" align=center>&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){
80                 $smarty->assign($val, $this->$val);
81                 $smarty->assign($val."ACL", chkacl($this->acl, $val));
82         }
84         /* Checkboxes */
85         foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
86                         "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
87                 if (preg_match("/$key/", $this->gosaApplicationFlags)){
88                         $smarty->assign("$val", "checked");
89                 } else {
90                         $smarty->assign("$val", "");
91                 }
92         }
93         $smarty->assign("base_select", $this->base);
94         $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
96         /* Show main page */
97         return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
98   }
100   function remove_from_parent()
101   {
102         $ldap= $this->config->get_ldap_link();
103         $ldap->rmDir($this->dn);
105         /* Optionally execute a command after we're done */
106         $this->handle_post_events("remove");
108         /* Delete references to object groups */
109         $ldap->cd ($this->config->current['BASE']);
110         $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
111         while ($ldap->fetch()){
112                 $og= new ogroup($this->config, $ldap->getDN());
113                 unset($og->member[$this->dn]);
114                 $og->save ();
115         }
117   }
120   /* Save data to object */
121   function save_object()
122   {
123         if (isset($_POST['cn'])){
124                 plugin::save_object();
126                 /* Save application flags */
127                 $flag= "";
128                 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
129                         $flag.= "G";
130                 }
131                 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
132                         $flag.= "D";
133                 }
134                 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
135                         $flag.= "M";
136                 }
137                 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
138                         $flag.= "O";
139                 }
140                 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
141                         $this->gosaApplicationFlags= "[$flag]";
142                 }
144                 /* Check for picture upload */
147                 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
148                         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
149                                 print_red (_("The specified picture has not been uploaded correctly."));
150                         }
151         
152                         /* Load the new uploaded Photo */
153                         if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name']))  {
154                           gosa_log("Can't Load image");
155                         }
157                         /* Resizing image to 147x200 and blur */
158                         if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
159                           gosa_log("imagick_resize failed");
160                         }
162                         /* Converting image to JPEG */
163                         if(!imagick_convert($handle,"PNG")) {
164                           gosa_log("Can't Convert to PNG");
165                         }
167                         if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
168                           gosa_log("can't write to specified folder");
169                         }
170                         
171                         imagick_free($handle);
173                         /* Activate new picture */
174                         $this->set_picture($_FILES['picture_file']['tmp_name']);
175                         }       
178                 /* Save base, since this is no LDAP attribute */
179                 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
180                         $this->base= $_POST['base'];
181                 }
182         }
183   }
186   /* Check values */
187   function check()
188   {
189         $message= array();
191         /* Permissions for that base? */
192         if ($this->base != ""){
193                 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
194         } else {
195                 $new_dn= $this->dn;
196         }
198         $ui= get_userinfo();
199         $acl= get_permissions ($new_dn, $ui->subtreeACL);
200         $acl= get_module_permission($acl, "application", $new_dn);
201         if (chkacl($acl, "create") != ""){
202                 $message[]= _("You have no permissions to create a application on this 'Base'.");
203         }
205         /* All required fields are set? */
206         if ($this->cn == ""){
207                 $message[]= _("Required field 'Name' is not filled.");
208         }
209         if ($this->gosaApplicationExecute == ""){
210                 $message[]= _("Required field 'Execute' is not filled.");
211         }
213         /* Check for existing application */
214         $ldap= $this->config->get_ldap_link();
215         $ldap->cd($this->config->current["BASE"]);
216         $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
217         $ldap->fetch();
218         if ($ldap->count() != 0 && $this->dn == "new"){
219                 $message[]= _("There's already an application with this 'Name'.");
220         }
222         return $message;
223   }
226   /* Save to LDAP */
227   function save()
228   {
229         plugin::save();
230                 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
232         /* Write back to ldap */
233         $ldap= $this->config->get_ldap_link();
234         $ldap->cat($this->dn);
235         $a= $ldap->fetch();
236         if (count($a)){
237                 $ldap->cd($this->dn);
238                         $ldap->modify($this->attrs);
239                         $this->handle_post_events("modify");
240         } else {
241                         $ldap->cd($this->config->current['BASE']);
242                         $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
243                 $ldap->cd($this->dn);
244                         $ldap->add($this->attrs);
245                         $this->handle_post_events("add");
246         }
247         show_ldap_error($ldap->get_error());
248   }
250   function set_picture($filename)
251   {
252         if (!is_file($filename)){
253                 $filename= "./images/default_icon.png";
254                 $this->gosaApplicationIcon= "*removed*";
255         }
257         if (file_exists($filename)){
258                 $fd = fopen ($filename, "rb");
259                 $this->iconData= fread ($fd, filesize ($filename));
260                 $_SESSION['picture']= $this->iconData;
261                 $this->gosaApplicationIcon= $this->iconData;
263                 fclose ($fd);
264         }
265   }
270 ?>