8472a17413c5d24868bc1f2897c4040f3a831067
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 $gotoLogonScript ="";
18 var $iconData;
20 /* Headpage attributes */
21 var $last_sorting= "invalid";
22 var $applications= array();
24 /* attribute list for save action */
25 var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
26 "gosaApplicationFlags","gotoLogonScript");
27 var $objectclasses= array("top", "gosaApplication");
29 function application ($config, $dn= NULL)
30 {
31 plugin::plugin ($config, $dn);
33 /* Load icon */
34 $ldap= $config->get_ldap_link();
35 if ($dn != 'new'){
36 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
37 $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
38 }
39 if ($this->iconData == ""){
40 $this->set_picture("");
41 }
42 $_SESSION['binary']= $this->iconData;
43 $_SESSION['binarytype']= "image/jpeg";
44 $this->gosaApplicationIcon= $this->iconData;
46 /* This is always an account */
47 $this->is_account= TRUE;
49 if ($this->dn == "new"){
50 if(isset($_SESSION['appfilter']['depselect'])){
51 $this->base=$_SESSION['appfilter']['depselect'];
52 }else{
53 $ui= get_userinfo();
54 $this->base= dn2base($ui->dn);
55 }
56 } else {
57 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
58 }
59 }
62 function generateTemplate(){
63 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
65 $values = array();
66 $names = array();
67 if($this->parent->by_object['applicationParameters']->is_account){
68 $names = $this->parent->by_object['applicationParameters']->option_name;
69 $values = $this->parent->by_object['applicationParameters']->option_value;
70 }
72 if (count($names)){
73 $str .="# This plugin handles these environment variables:\n";
74 } else {
75 $str .="# This plugin handles no environment variables.\n";
76 }
78 foreach($names as $index => $name){
80 // Fix length
81 for($i = strlen($name) ; $i < 30 ; $i++){
82 $name= $name." ";
83 }
84 if((isset($values[$index]))&&(!empty($values[$index]))){
85 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
86 }else{
87 $str.= "# ".$name."\t("._("no example").")\n";
88 }
89 }
90 $str .= "#\n".
91 "# Don't remove the following tag, it is used for header update.\n".
92 "### END HEADER ###";
94 return($str);
95 }
97 function execute()
98 {
99 /* Call parent execute */
100 plugin::execute();
102 $smarty= get_smarty();
104 /* Do we represent a valid group? */
105 if (!$this->is_account && $this->parent == NULL){
106 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
107 _("This 'dn' is no application.")."</b>";
108 return ($display);
109 }
111 $head = $this->generateTemplate();
112 $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
114 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
115 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
116 $this->gotoLogonScript = $str;
117 }
119 /* Fill templating stuff */
120 $smarty->assign("cn", $this->cn);
121 $smarty->assign("bases", $this->config->idepartments);
122 if ($this->dn == "new"){
123 $smarty->assign("selectmode", "");
124 $smarty->assign("namemode", "");
125 } else {
126 $smarty->assign("namemode", "readonly");
127 $smarty->assign("selectmode", "disabled");
128 }
130 /* Get random number for pictures */
131 srand((double)microtime()*1000000);
132 $smarty->assign("rand", rand(0, 10000));
134 /* Variables */
135 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
136 $smarty->assign($val, $this->$val);
137 $smarty->assign($val."ACL", chkacl($this->acl, $val));
138 }
140 /* Checkboxes */
141 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
142 "L" => "place_on_kicker",
143 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
144 if (preg_match("/$key/", $this->gosaApplicationFlags)){
145 $smarty->assign("$val", "checked");
146 } else {
147 $smarty->assign("$val", "");
148 }
149 }
150 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript));
151 $smarty->assign("base_select", $this->base);
152 $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
153 /* Show main page */
154 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
155 }
158 function remove_from_parent()
159 {
160 $ldap= $this->config->get_ldap_link();
161 $ldap->rmDir($this->dn);
163 /* Optionally execute a command after we're done */
164 $this->handle_post_events("remove");
166 /* Delete references to object groups */
167 $ldap->cd ($this->config->current['BASE']);
168 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
169 while ($ldap->fetch()){
170 $og= new ogroup($this->config, $ldap->getDN());
171 unset($og->member[$this->dn]);
172 $og->save ();
173 }
174 $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
175 while ($attrs= $ldap->fetch()){
176 $ag= new appgroup($this->config, $ldap->getDN());
177 $ag->removeApp($this->cn);
178 $ag->save ();
179 }
181 }
184 /* Save data to object */
185 function save_object()
186 {
187 if (isset($_POST['cn'])){
189 plugin::save_object();
191 /* Save application flags */
192 $flag= "";
193 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
194 $flag.= "G";
195 }
196 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
197 $flag.= "D";
198 }
199 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
200 $flag.= "L";
201 }
202 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
203 $flag.= "M";
204 }
205 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
206 $flag.= "O";
207 }
208 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
209 $this->gosaApplicationFlags= "[$flag]";
210 }
212 /* Check for picture upload */
213 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
214 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
215 print_red (_("The specified picture has not been uploaded correctly."));
216 }
218 if (!function_exists("imagick_blob2image")){
219 /* Get temporary file name for conversation */
220 $fname = tempnam ("/tmp", "GOsa");
222 /* Open file and write out photoData */
223 $fp = fopen ($fname, "w");
224 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
225 fclose ($fp);
227 /* Build conversation query. Filename is generated automatically, so
228 we do not need any special security checks. Exec command and save
229 output. For PHP safe mode, you'll need a configuration which respects
230 image magick as executable... */
231 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
232 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
234 /* Read data written by convert */
235 $output= "";
236 $sh= popen($query, 'r');
237 while (!feof($sh)){
238 $output.= fread($sh, 4096);
239 }
240 pclose($sh);
242 unlink($fname);
243 } else {
245 /* Load the new uploaded Photo */
246 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
247 gosa_log("Can't Load image");
248 }
250 /* Resizing image to 147x200 and blur */
251 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
252 gosa_log("imagick_resize failed");
253 }
255 /* Converting image to JPEG */
256 if(!imagick_convert($handle,"PNG")) {
257 gosa_log("Can't Convert to PNG");
258 }
260 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
261 gosa_log("can't write to specified folder");
262 }
264 imagick_free($handle);
265 }
267 /* Activate new picture */
268 $this->set_picture($_FILES['picture_file']['tmp_name']);
269 }
272 /* Save base, since this is no LDAP attribute */
273 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
274 $this->base= $_POST['base'];
275 }
276 }
277 }
280 /* Check values */
281 function check()
282 {
283 $message= array();
285 /* Permissions for that base? */
286 if ($this->base != ""){
287 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
288 } else {
289 $new_dn= $this->dn;
290 }
292 $ui= get_userinfo();
293 $acl= get_permissions ($new_dn, $ui->subtreeACL);
294 $acl= get_module_permission($acl, "application", $new_dn);
295 if (chkacl($acl, "create") != ""){
296 $message[]= _("You have no permissions to create a application on this 'Base'.");
297 }
299 /* All required fields are set? */
300 if ($this->cn == ""){
301 $message[]= _("Required field 'Name' is not filled.");
302 }
304 if(preg_match("/[^a-z0-9]/",$this->cn)) {
305 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
306 }
308 if ($this->gosaApplicationExecute == ""){
309 $message[]= _("Required field 'Execute' is not filled.");
310 }
312 /* Check for existing application */
313 $ldap= $this->config->get_ldap_link();
314 $ldap->cd($this->config->current["BASE"]);
315 $ldap->search("(&(objectClass=gosaApplication)(cn=$this->cn))",array("cn"));
316 $ldap->fetch();
317 if ($ldap->count() != 0 && $this->dn == "new"){
318 $message[]= _("There's already an application with this 'Name'.");
319 }
321 return $message;
322 }
325 /* Save to LDAP */
326 function save()
327 {
328 plugin::save();
329 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
331 /* Write back to ldap */
332 $ldap= $this->config->get_ldap_link();
333 $ldap->cat($this->dn);
334 $a= $ldap->fetch();
335 if (count($a)){
336 $ldap->cd($this->dn);
337 $this->cleanup();
338 $ldap->modify ($this->attrs);
340 $this->handle_post_events("modify");
341 } else {
342 $ldap->cd($this->config->current['BASE']);
343 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
344 $ldap->cd($this->dn);
345 $ldap->add($this->attrs);
346 $this->handle_post_events("add");
347 }
348 show_ldap_error($ldap->get_error());
349 }
351 function set_picture($filename)
352 {
353 if (!is_file($filename)){
354 $filename= "./images/default_icon.png";
355 $this->gosaApplicationIcon= "*removed*";
356 }
358 if (file_exists($filename)){
359 $fd = fopen ($filename, "rb");
360 $this->iconData= fread ($fd, filesize ($filename));
361 $_SESSION['binary']= $this->iconData;
362 $_SESSION['binarytype']= "image/jpeg";
363 $this->gosaApplicationIcon= $this->iconData;
365 fclose ($fd);
366 }
367 }
370 }
372 ?>