1 <?php
2 class application extends plugin
3 {
4 /* application attributes */
5 var $cn= "";
6 var $description= "";
7 var $base= "";
8 var $gosaApplicationExecute= "";
9 var $gosaApplicationName= "";
10 var $gosaApplicationFlags= "";
11 var $gosaApplicationIcon= "";
12 var $gotoLogonScript ="";
13 var $iconData;
14 var $view_logged = FALSE;
16 /* Headpage attributes */
17 var $last_sorting= "invalid";
18 var $applications= array();
20 var $orig_base = "";
21 var $orig_cn = "";
22 var $orig_dn = "";
24 /* attribute list for save action */
25 var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
26 "gosaApplicationFlags","gotoLogonScript");
28 var $objectclasses= array("top", "gosaApplication");
29 var $FAIstate ="";
31 function application (&$config, $dn= NULL, $parent= NULL)
32 {
33 plugin::plugin ($config, $dn, $parent);
35 /* Load icon */
36 $ldap= $config->get_ldap_link();
37 $this->orig_dn = $dn;
38 if ($dn != 'new'){
39 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
40 $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
41 }
42 if ($this->iconData == ""){
43 $this->set_picture("");
44 }
45 session::set('binary',$this->iconData);
46 session::set('binarytype',"image/jpeg");
47 $this->gosaApplicationIcon= $this->iconData;
49 /* This is always an account */
50 $this->is_account= TRUE;
52 if ($this->dn == "new"){
53 if(session::is_set('CurrentMainBase')){
54 $this->base = session::get('CurrentMainBase');
55 }else{
56 $ui= get_userinfo();
57 $this->base= dn2base($ui->dn);
58 }
59 } else {
60 $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou('applicationRDN'), '/')."/i", "", $this->dn);
61 }
63 $this->orig_cn = $this->cn;
64 $this->orig_base = $this->base;
65 $this->gosaApplicationFlags = preg_replace("/[^GDLMO]/","",$this->gosaApplicationFlags);
66 }
69 function generateTemplate()
70 {
71 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
73 $values = array();
74 $names = array();
75 if($this->parent->by_object['applicationParameters']->is_account){
76 $names = $this->parent->by_object['applicationParameters']->option_name;
77 $values = $this->parent->by_object['applicationParameters']->option_value;
78 }
80 if (count($names)){
81 $str .="# This plugin handles these environment variables:\n";
82 } else {
83 $str .="# This plugin handles no environment variables.\n";
84 }
86 foreach($names as $index => $name){
88 // Fix length
89 for($i = strlen($name) ; $i < 30 ; $i++){
90 $name= $name." ";
91 }
92 if((isset($values[$index]))&&(!empty($values[$index]))){
93 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
94 }else{
95 $str.= "# ".$name."\t("._("no example").")\n";
96 }
97 }
98 $str .= "#\n".
99 "# Don't remove the following tag, it is used for header update.\n".
100 "### END HEADER ###";
102 return($str);
103 }
105 function execute()
106 {
107 /* Call parent execute */
108 plugin::execute();
110 /* Log view */
111 if($this->is_account && !$this->view_logged){
112 $this->view_logged = TRUE;
113 new log("view","application/".get_class($this),$this->dn);
114 }
116 $smarty= get_smarty();
118 $tmp = $this->plInfo();
119 foreach($tmp['plProvidedAcls'] as $name => $translation){
120 $smarty->assign($name."ACL",$this->getacl($name, preg_match("/freeze/i",$this->FAIstate)));
121 }
122 $smarty->assign("IconReadable", $this->acl_is_readable("gosaApplicationIcon"));
124 /* Do we represent a valid group? */
125 if (!$this->is_account && $this->parent === NULL){
126 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
127 msgPool::noValidExtension(_("application"))."</b>";
128 return ($display);
129 }
131 /* Download requested */
132 foreach($_POST as $name => $value){
133 if(preg_match("/^downloadScript/",$name)){
134 session::set('binary',$this->gotoLogonScript);
135 session::set('binarytype',"octet-stream");
136 session::set('binaryfile',$this->cn.".gosaApplication");
137 header("location: getbin.php ");
138 exit();
139 }
140 }
142 /* Reassign picture data, sometimes its corrupt cause we started a download of application scripts */
143 session::set('binary',$this->iconData);
144 session::set('binarytype',"image/jpeg");
146 $smarty->assign("rand", rand(0, 10000));
147 $head = $this->generateTemplate();
148 $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
150 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile'])) && !preg_match("/freeze/i",$this->FAIstate)){
151 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
152 $this->gotoLogonScript = $str;
153 }
155 /* Fill templating stuff */
156 $smarty->assign("cn", $this->cn);
157 if(!$this->is_release()){
158 $smarty->assign("bases", $this->get_allowed_bases());
159 }else{
160 $smarty->assign("bases", array());
161 }
162 if ($this->dn == "new"){
163 $smarty->assign("selectmode", "");
164 $smarty->assign("namemode", "");
165 } else {
166 $smarty->assign("namemode", "readonly");
167 $smarty->assign("selectmode", "disabled");
168 }
170 /* Get random number for pictures */
171 srand((double)microtime()*1000000);
172 $smarty->assign("rand", rand(0, 10000));
174 /* Variables */
175 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
176 $smarty->assign($val, $this->$val);
177 }
179 /* Checkboxes */
180 foreach (array("G" => "execForGroupmembers", "O" => "overwriteConfig",
181 "L" => "placeOnKicker",
182 "D" => "placeOnDesktop", "M" => "placeInStartmenu") as $key => $val){
183 if (preg_match("/$key/", $this->gosaApplicationFlags) && $this->acl_is_readable("$val")){
184 $smarty->assign("$val", "checked");
185 } else {
186 $smarty->assign("$val", "");
187 }
188 }
190 $smarty->assign("isReleaseApplikation" ,$this->is_release());
191 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
192 $smarty->assign("base_select", $this->base);
193 /* Show main page */
194 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
195 }
198 function remove_from_parent()
199 {
200 restore_error_handler();
202 /* Parse release out of object dn */
203 $release = preg_replace("/".preg_quote(get_ou("applicationRDN").$this->base, '/')."$/i","",$this->dn);
204 $release = preg_replace("/^cn=[^,]+,/","",$release);
206 /* Get a list of all groups
207 */
208 $groups = array();
209 $ldap= $this->config->get_ldap_link();
210 $ldap->cd($this->config->current['BASE']);
211 $ldap->search("(objectClass=posixGroup)",array("dn"));
212 while($attrs = $ldap->fetch()){
213 $groups[$attrs['dn']] = array();
214 }
216 /* Check if there are groups, useing this application
217 */
218 $found = array();
219 foreach($groups as $group => $data){
220 $ldap->cd($release.$group);
221 $ldap->search("(objectClass=gotoMenuEntry)",array("dn"));
222 while($attrs = $ldap->fetch()){
223 $info = preg_replace("/".preg_quote($release.$group, '/')."$/","",$attrs['dn']);
224 if(preg_match("/^cn=".$this->cn."/",$info) && !preg_match("/ou=[^,]+,/",$info)){
225 $found[] = $attrs['dn'];
226 }
227 }
228 }
230 /* Create an error message an skip remove, if
231 this application is still in use.
232 */
233 if(count($found)){
234 msg_dialog::display(_("Error"), sprintf(_("Cannot remove application - it is still in use by these objects: %s"), "<br>".msgPool::buildList($found)), ERROR_DIALOG);
235 return(FALSE);
236 }
238 $ldap->rmDir($this->dn);
239 new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
240 if (!$ldap->success()){
241 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()), ERROR_DIALOG);
242 }
244 /* Optionally execute a command after we're done */
245 $this->handle_post_events("remove");
247 /* Delete references to object groups */
248 $ldap->cd ($this->config->current['BASE']);
249 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
250 while ($ldap->fetch()){
251 $og= new ogroup($this->config, $ldap->getDN());
252 unset($og->member[$this->dn]);
253 $og->save ();
254 if (!$ldap->success()){
255 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()), ERROR_DIALOG);
256 }
257 }
258 }
261 /* Save data to object */
262 function save_object()
263 {
264 if(preg_match("/freeze/i",$this->FAIstate)){
265 return;
266 }
268 if (isset($_POST['cn'])){
270 /* Create a base backup and reset the
271 base directly after calling plugin::save_object();
272 Base will be set seperatly a few lines below */
273 $base_tmp = $this->base;
274 plugin::save_object();
275 $this->base = $base_tmp;
277 /* Save attributes */
278 parent::save_object();
280 $checks = array("execForGroupmembers" => "G",
281 "placeOnDesktop" => "D",
282 "placeOnKicker" => "L",
283 "placeInStartmenu" => "M",
284 "overwriteConfig" => "O");
287 /* Save application flags */
288 foreach($checks as $name => $flag){
289 if($this->acl_is_writeable($name)){
290 $set = isset($_POST[$name]) && $_POST[$name] == 1;
291 if(!$set && strstr($this->gosaApplicationFlags,$flag)){
292 $this->gosaApplicationFlags = str_replace($flag,"",$this->gosaApplicationFlags);
293 }elseif($set && !preg_match("/".$flag."/",$this->gosaApplicationFlags)){
294 $this->gosaApplicationFlags .= $flag;
295 }
296 }
297 }
299 /* Remove current picture */
300 if(isset($_POST['remove_picture'])){
301 $this->set_picture("");
302 }
304 /* Check for picture upload */
305 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
307 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
308 msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
309 }
311 if (!function_exists("imagick_blob2image")){
312 /* Get temporary file name for conversation */
313 $fname = tempnam (TEMP_DIR, "GOsa");
315 /* Open file and write out photoData */
316 $fp = fopen ($fname, "w");
317 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
318 fclose ($fp);
320 /* Build conversation query. Filename is generated automatically, so
321 we do not need any special security checks. Exec command and save
322 output. For PHP safe mode, you'll need a configuration which respects
323 image magick as executable... */
324 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
325 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
327 /* Read data written by convert */
328 $output= "";
329 $sh= popen($query, 'r');
330 while (!feof($sh)){
331 $output.= fread($sh, 4096);
332 }
333 pclose($sh);
335 unlink($fname);
336 } else {
338 /* Load the new uploaded Photo */
339 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
340 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("no read permission")), ERROR_DIALOG);
341 }
343 /* Resizing image to 147x200 and blur */
344 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
345 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("cannot resize image")), ERROR_DIALOG);
346 }
348 /* Converting image to JPEG */
349 if(!imagick_convert($handle,"PNG")) {
350 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("cannot convert image")), ERROR_DIALOG);
351 }
353 if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
354 msg_dialog::display(_("Error"), msgPool::incorrectUpload(sprintf(_("cannot save image to '%s'"), $_FILES['picture_file']['tmp_name'])), ERROR_DIALOG);
355 }
357 imagick_free($handle);
358 }
360 /* Activate new picture */
361 $this->set_picture($_FILES['picture_file']['tmp_name']);
362 }
364 if(!$this->is_release()){
365 $tmp = $this->get_allowed_bases();
366 if(isset($_POST['base'])){
367 if(isset($tmp[$_POST['base']])){
368 $this->base= $_POST['base'];
369 }
370 }
371 }
372 }
373 }
376 /* Check values */
377 function check()
378 {
379 /* Call common method to give check the hook */
380 $message= plugin::check();
382 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
383 $message[]=msgPool::invalid(_("Execute path"),"","","/some/path");
384 }
386 /* Permissions for that base? */
387 if ($this->base != ""){
388 $new_dn= "cn=".$this->cn.",".get_ou('applicationRDN').$this->base;
389 } else {
390 $new_dn= $this->dn;
391 }
394 if($this->dn == "new"){
395 $this->set_acl_base($this->base);
396 }
398 /* All required fields are set? */
399 if ($this->cn == ""){
400 $message[]= msgPool::required(_("Name"));
401 }
403 if(preg_match("/[^a-z0-9_-]/",$this->cn)) {
404 $message[]=msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9_-]*$/");
405 }
407 if ($this->gosaApplicationExecute == ""){
408 $message[]= msgPool::required(_("Execute"));
409 }
411 /* Check for existing application */
412 $ldap= $this->config->get_ldap_link();
413 $ldap->cd($this->config->current["BASE"]);
415 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
417 if($this->is_release()){
418 $base = $this->parent->parent->app_release;
419 }else{
420 $base = get_ou('applicationRDN').$this->base;
421 }
423 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$base,array("cn"));
424 if($ldap->count()){
425 $attrs = $ldap->fetch();
426 if($this->dn != $attrs['dn']) {
427 $message[]= msgPool::duplicated("cn");
428 }
429 }
431 /* Check if we are allowed to create or move this object
432 */
433 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
434 $message[] = msgPool::permCreate();
435 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
436 $message[] = msgPool::permMove();
437 }
439 return $message;
440 }
443 /* Save to LDAP */
444 function save()
445 {
446 /* Get application script without header part, to check if we must save the script itself */
447 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
449 $this->gosaApplicationFlags ="[{$this->gosaApplicationFlags}]";
450 plugin::save();
451 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
453 /* Write back to ldap */
454 $ldap= $this->config->get_ldap_link();
455 $ldap->cat($this->dn, array('dn'));
457 $a= $ldap->fetch();
458 if (count($a)){
460 /* Remove gotoLogonScript if it is empty */
461 if(empty($script)) {
462 $this->attrs['gotoLogonScript'] = array();
463 }
465 $ldap->cd($this->dn);
466 $this->cleanup();
467 $ldap->modify ($this->attrs);
468 $this->handle_post_events("modify");
469 new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
470 } else {
472 /* Remove gotoLogonScript if it is empty */
473 if(empty($script)) {
474 unset($this->attrs['gotoLogonScript']);
475 }
477 $ldap->cd($this->config->current['BASE']);
478 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
479 $ldap->cd($this->dn);
480 $ldap->add($this->attrs);
481 new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
482 $this->handle_post_events("add");
483 }
484 if (!$ldap->success()){
485 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()), ERROR_DIALOG);
486 }
487 }
490 function is_release()
491 {
492 if(isset($this->parent->parent)){
493 return($this->parent->parent->IsReleaseManagementActivated());
494 }else{
495 /* Check if we should enable the release selection */
496 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
497 if(!empty($tmp)){
498 return(true);
499 }
500 }
501 return(FALSE);
502 }
505 function set_picture($filename)
506 {
507 if (!is_file($filename)){
508 $filename= "./plugins/goto/images/default_icon.png";
509 $this->gosaApplicationIcon= "*removed*";
510 }
512 if (file_exists($filename)){
513 $fd = fopen ($filename, "rb");
514 $this->iconData= fread ($fd, filesize ($filename));
515 session::set('binary',$this->iconData);
516 session::set('binarytype',"image/jpeg");
517 $this->gosaApplicationIcon= $this->iconData;
519 fclose ($fd);
520 }
521 }
523 function getCopyDialog()
524 {
525 $vars = array("cn");
527 $str ="<h3>"._("Application settings")."</h3>
528 <table>
529 <tr>
530 <td>".
531 _("Application name").
532 "</td>
533 <td>
534 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
535 value='".$this->cn."'
536 title='"._("Application name to be displayed (i.e. below icons)")."'>
537 </td>
538 </tr>
539 </table>";
540 $ret = array();
541 $ret['status'] = "";
542 $ret['string'] = $str;
543 return($ret);
544 }
546 function saveCopyDialog()
547 {
548 if(isset($_POST['cn'])){
549 $this->cn = $_POST['cn'];
550 }
551 }
554 function PrepareForCopyPaste($source)
555 {
556 plugin::PrepareForCopyPaste($source);
557 $source_o = new application($this->config,$source['dn']);
558 $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;
559 }
562 /* Return plugin informations for acl handling
563 #FIXME FAIscript seams to ununsed within this class... */
564 static function plInfo()
565 {
566 return (array(
567 "plShortName" => _("Generic"),
568 "plDescription" => _("Application generic"),
569 "plSelfModify" => FALSE,
570 "plDepends" => array(),
571 "plPriority" => 0,
572 "plSection" => array("administration"),
573 "plCategory" => array("application" => array("description" => _("Application"),
574 "objectClass" => "gosaApplication")),
575 "plProvidedAcls"=> array(
576 "cn" => _("Name"),
577 "base" => _("Base"),
578 "description" => _("Description"),
579 "gosaApplicationExecute" => _("Execute"),
580 "gosaApplicationName" => _("Name"),
581 "gosaApplicationIcon" => _("Icon"),
582 "gotoLogonScript" => _("Script content"),
584 "execForGroupmembers" => _("Only executable for members"), // G
585 "placeOnDesktop" => _("Place icon on members desktop"), // D
586 "placeOnKicker" => _("Place entry in members launch bar"), // L
587 "placeInStartmenu" => _("Place entry in members startmenu"), // M
588 "overwriteConfig" => _("Replace user configuration on startup")) // O
589 ));
590 }
591 }
592 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
593 ?>