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\"> <b>".
63 _("This 'dn' is no application.")."</b>";
64 return ($display);
65 }
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 plugin::save_object();
138 /* Save application flags */
139 $flag= "";
140 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
141 $flag.= "G";
142 }
143 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
144 $flag.= "D";
145 }
146 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
147 $flag.= "M";
148 }
149 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
150 $flag.= "O";
151 }
152 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
153 $this->gosaApplicationFlags= "[$flag]";
154 }
156 /* Check for picture upload */
157 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
158 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
159 print_red (_("The specified picture has not been uploaded correctly."));
160 }
162 if (!function_exists("imagick_blob2image")){
163 /* Get temporary file name for conversation */
164 $fname = tempnam ("/tmp", "GOsa");
166 /* Open file and write out photoData */
167 $fp = fopen ($fname, "w");
168 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
169 fclose ($fp);
171 /* Build conversation query. Filename is generated automatically, so
172 we do not need any special security checks. Exec command and save
173 output. For PHP safe mode, you'll need a configuration which respects
174 image magick as executable... */
175 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
176 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
178 /* Read data written by convert */
179 $output= "";
180 $sh= popen($query, 'r');
181 while (!feof($sh)){
182 $output.= fread($sh, 4096);
183 }
184 pclose($sh);
186 unlink($fname);
187 } else {
189 /* Load the new uploaded Photo */
190 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
191 gosa_log("Can't Load image");
192 }
194 /* Resizing image to 147x200 and blur */
195 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
196 gosa_log("imagick_resize failed");
197 }
199 /* Converting image to JPEG */
200 if(!imagick_convert($handle,"PNG")) {
201 gosa_log("Can't Convert to PNG");
202 }
204 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
205 gosa_log("can't write to specified folder");
206 }
208 imagick_free($handle);
209 }
211 /* Activate new picture */
212 $this->set_picture($_FILES['picture_file']['tmp_name']);
213 }
216 /* Save base, since this is no LDAP attribute */
217 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
218 $this->base= $_POST['base'];
219 }
220 }
221 }
224 /* Check values */
225 function check()
226 {
227 $message= array();
229 /* Permissions for that base? */
230 if ($this->base != ""){
231 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
232 } else {
233 $new_dn= $this->dn;
234 }
236 $ui= get_userinfo();
237 $acl= get_permissions ($new_dn, $ui->subtreeACL);
238 $acl= get_module_permission($acl, "application", $new_dn);
239 if (chkacl($acl, "create") != ""){
240 $message[]= _("You have no permissions to create a application on this 'Base'.");
241 }
243 /* All required fields are set? */
244 if ($this->cn == ""){
245 $message[]= _("Required field 'Name' is not filled.");
246 }
247 if ($this->gosaApplicationExecute == ""){
248 $message[]= _("Required field 'Execute' is not filled.");
249 }
251 /* Check for existing application */
252 $ldap= $this->config->get_ldap_link();
253 $ldap->cd($this->config->current["BASE"]);
254 $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))");
255 $ldap->fetch();
256 if ($ldap->count() != 0 && $this->dn == "new"){
257 $message[]= _("There's already an application with this 'Name'.");
258 }
260 return $message;
261 }
264 /* Save to LDAP */
265 function save()
266 {
267 plugin::save();
268 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
270 /* Write back to ldap */
271 $ldap= $this->config->get_ldap_link();
272 $ldap->cat($this->dn);
273 $a= $ldap->fetch();
274 if (count($a)){
275 $ldap->cd($this->dn);
276 $ldap->modify($this->attrs);
277 $this->handle_post_events("modify");
278 } else {
279 $ldap->cd($this->config->current['BASE']);
280 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
281 $ldap->cd($this->dn);
282 $ldap->add($this->attrs);
283 $this->handle_post_events("add");
284 }
285 show_ldap_error($ldap->get_error());
286 }
288 function set_picture($filename)
289 {
290 if (!is_file($filename)){
291 $filename= "./images/default_icon.png";
292 $this->gosaApplicationIcon= "*removed*";
293 }
295 if (file_exists($filename)){
296 $fd = fopen ($filename, "rb");
297 $this->iconData= fread ($fd, filesize ($filename));
298 $_SESSION['picture']= $this->iconData;
299 $this->gosaApplicationIcon= $this->iconData;
301 fclose ($fd);
302 }
303 }
306 }
308 ?>