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