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 /* Base select dialog */
171 $once = true;
172 foreach($_POST as $name => $value){
173 if(preg_match("/^chooseBase/",$name) && $once && !preg_match("/freeze/i",$this->FAIstate)){
174 $once = false;
175 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
176 $this->dialog->setCurrentBase($this->base);
177 }
178 }
180 /* Dialog handling */
181 if(is_object($this->dialog)){
182 /* Must be called before save_object */
183 $this->dialog->save_object();
185 if($this->dialog->isClosed()){
186 $this->dialog = false;
187 }elseif($this->dialog->isSelected()){
189 /* Just allow selection valid bases */
190 $tmp = $this->get_allowed_bases();
191 if(isset($tmp[$this->dialog->isSelected()])){
192 $this->base = $this->dialog->isSelected();
193 }
194 $this->dialog= false;
195 }else{
196 return($this->dialog->execute());
197 }
198 }
200 /* Get random number for pictures */
201 srand((double)microtime()*1000000);
202 $smarty->assign("rand", rand(0, 10000));
204 /* Variables */
205 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
206 $smarty->assign($val, $this->$val);
207 }
209 /* Checkboxes */
210 foreach (array("G" => "execForGroupmembers", "O" => "overwriteConfig",
211 "L" => "placeOnKicker",
212 "D" => "placeOnDesktop", "M" => "placeInStartmenu") as $key => $val){
213 if (preg_match("/$key/", $this->gosaApplicationFlags) && $this->acl_is_readable("$val")){
214 $smarty->assign("$val", "checked");
215 } else {
216 $smarty->assign("$val", "");
217 }
218 }
220 $smarty->assign("isReleaseApplikation" ,$this->is_release());
221 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
222 $smarty->assign("base_select", $this->base);
223 /* Show main page */
224 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
225 }
228 function remove_from_parent()
229 {
230 restore_error_handler();
232 /* Parse release out of object dn */
233 $release = preg_replace("/".preg_quote(get_ou("applicationRDN").$this->base, '/')."$/i","",$this->dn);
234 $release = preg_replace("/^cn=[^,]+,/","",$release);
236 /* Get a list of all groups
237 */
238 $groups = array();
239 $ldap= $this->config->get_ldap_link();
240 $ldap->cd($this->config->current['BASE']);
241 $ldap->search("(objectClass=posixGroup)",array("dn"));
242 while($attrs = $ldap->fetch()){
243 $groups[$attrs['dn']] = array();
244 }
246 /* Check if there are groups, useing this application
247 */
248 $found = array();
249 foreach($groups as $group => $data){
250 $ldap->cd($release.$group);
251 $ldap->search("(objectClass=gotoMenuEntry)",array("dn"));
252 while($attrs = $ldap->fetch()){
253 $info = preg_replace("/".preg_quote($release.$group, '/')."$/","",$attrs['dn']);
254 if(preg_match("/^cn=".$this->cn."/",$info) && !preg_match("/ou=[^,]+,/",$info)){
255 $found[] = $attrs['dn'];
256 }
257 }
258 }
260 /* Create an error message an skip remove, if
261 this application is still in use.
262 */
263 if(count($found)){
264 msg_dialog::display(_("Error"), sprintf(_("Cannot remove application - it is still in use by these objects: %s"), "<br>".msgPool::buildList($found)), ERROR_DIALOG);
265 return(FALSE);
266 }
268 $ldap->rmDir($this->dn);
269 new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
270 if (!$ldap->success()){
271 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()), ERROR_DIALOG);
272 }
274 /* Optionally execute a command after we're done */
275 $this->handle_post_events("remove");
277 /* Delete references to object groups */
278 $ldap->cd ($this->config->current['BASE']);
279 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
280 while ($ldap->fetch()){
281 $og= new ogroup($this->config, $ldap->getDN());
282 unset($og->member[$this->dn]);
283 $og->save ();
284 if (!$ldap->success()){
285 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()), ERROR_DIALOG);
286 }
287 }
288 }
291 /* Save data to object */
292 function save_object()
293 {
294 if(preg_match("/freeze/i",$this->FAIstate)){
295 return;
296 }
298 if (isset($_POST['cn'])){
300 /* Create a base backup and reset the
301 base directly after calling plugin::save_object();
302 Base will be set seperatly a few lines below */
303 $base_tmp = $this->base;
304 plugin::save_object();
305 $this->base = $base_tmp;
307 /* Save attributes */
308 parent::save_object();
310 $checks = array("execForGroupmembers" => "G",
311 "placeOnDesktop" => "D",
312 "placeOnKicker" => "L",
313 "placeInStartmenu" => "M",
314 "overwriteConfig" => "O");
317 /* Save application flags */
318 foreach($checks as $name => $flag){
319 if($this->acl_is_writeable($name)){
320 $set = isset($_POST[$name]) && $_POST[$name] == 1;
321 if(!$set && strstr($this->gosaApplicationFlags,$flag)){
322 $this->gosaApplicationFlags = str_replace($flag,"",$this->gosaApplicationFlags);
323 }elseif($set && !preg_match("/".$flag."/",$this->gosaApplicationFlags)){
324 $this->gosaApplicationFlags .= $flag;
325 }
326 }
327 }
329 /* Remove current picture */
330 if(isset($_POST['remove_picture'])){
331 $this->set_picture("");
332 }
334 /* Check for picture upload */
335 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
337 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
338 msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
339 }
341 if (!function_exists("imagick_blob2image")){
342 /* Get temporary file name for conversation */
343 $fname = tempnam (TEMP_DIR, "GOsa");
345 /* Open file and write out photoData */
346 $fp = fopen ($fname, "w");
347 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
348 fclose ($fp);
350 /* Build conversation query. Filename is generated automatically, so
351 we do not need any special security checks. Exec command and save
352 output. For PHP safe mode, you'll need a configuration which respects
353 image magick as executable... */
354 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
355 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
357 /* Read data written by convert */
358 $output= "";
359 $sh= popen($query, 'r');
360 while (!feof($sh)){
361 $output.= fread($sh, 4096);
362 }
363 pclose($sh);
365 unlink($fname);
366 } else {
368 /* Load the new uploaded Photo */
369 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
370 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("no read permission")), ERROR_DIALOG);
371 }
373 /* Resizing image to 147x200 and blur */
374 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
375 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("cannot resize image")), ERROR_DIALOG);
376 }
378 /* Converting image to JPEG */
379 if(!imagick_convert($handle,"PNG")) {
380 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("cannot convert image")), ERROR_DIALOG);
381 }
383 if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
384 msg_dialog::display(_("Error"), msgPool::incorrectUpload(sprintf(_("cannot save image to '%s'"), $_FILES['picture_file']['tmp_name'])), ERROR_DIALOG);
385 }
387 imagick_free($handle);
388 }
390 /* Activate new picture */
391 $this->set_picture($_FILES['picture_file']['tmp_name']);
392 }
394 if(!$this->is_release()){
395 $tmp = $this->get_allowed_bases();
396 if(isset($_POST['base'])){
397 if(isset($tmp[$_POST['base']])){
398 $this->base= $_POST['base'];
399 }
400 }
401 }
402 }
403 }
406 /* Check values */
407 function check()
408 {
409 /* Call common method to give check the hook */
410 $message= plugin::check();
412 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
413 $message[]=msgPool::invalid(_("Execute path"),"","","/some/path");
414 }
416 /* Permissions for that base? */
417 if ($this->base != ""){
418 $new_dn= "cn=".$this->cn.",".get_ou('applicationRDN').$this->base;
419 } else {
420 $new_dn= $this->dn;
421 }
424 if($this->dn == "new"){
425 $this->set_acl_base($this->base);
426 }
428 /* All required fields are set? */
429 if ($this->cn == ""){
430 $message[]= msgPool::required(_("Name"));
431 }
433 if(preg_match("/[^a-z0-9_-]/",$this->cn)) {
434 $message[]=msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9_-]*$/");
435 }
437 if ($this->gosaApplicationExecute == ""){
438 $message[]= msgPool::required(_("Execute"));
439 }
441 /* Check for existing application */
442 $ldap= $this->config->get_ldap_link();
443 $ldap->cd($this->config->current["BASE"]);
445 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
447 if($this->is_release()){
448 $base = $this->parent->parent->app_release;
449 }else{
450 $base = get_ou('applicationRDN').$this->base;
451 }
453 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$base,array("cn"));
454 if($ldap->count()){
455 $attrs = $ldap->fetch();
456 if($this->dn != $attrs['dn']) {
457 $message[]= msgPool::duplicated("cn");
458 }
459 }
461 /* Check if we are allowed to create or move this object
462 */
463 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
464 $message[] = msgPool::permCreate();
465 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
466 $message[] = msgPool::permMove();
467 }
469 return $message;
470 }
473 /* Save to LDAP */
474 function save()
475 {
476 /* Get application script without header part, to check if we must save the script itself */
477 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
479 $this->gosaApplicationFlags ="[{$this->gosaApplicationFlags}]";
480 plugin::save();
481 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
483 /* Write back to ldap */
484 $ldap= $this->config->get_ldap_link();
485 $ldap->cat($this->dn, array('dn'));
487 $a= $ldap->fetch();
488 if (count($a)){
490 /* Remove gotoLogonScript if it is empty */
491 if(empty($script)) {
492 $this->attrs['gotoLogonScript'] = array();
493 }
495 $ldap->cd($this->dn);
496 $this->cleanup();
497 $ldap->modify ($this->attrs);
498 $this->handle_post_events("modify");
499 new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
500 } else {
502 /* Remove gotoLogonScript if it is empty */
503 if(empty($script)) {
504 unset($this->attrs['gotoLogonScript']);
505 }
507 $ldap->cd($this->config->current['BASE']);
508 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
509 $ldap->cd($this->dn);
510 $ldap->add($this->attrs);
511 new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
512 $this->handle_post_events("add");
513 }
514 if (!$ldap->success()){
515 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()), ERROR_DIALOG);
516 }
517 }
520 function is_release()
521 {
522 if(isset($this->parent->parent)){
523 return($this->parent->parent->IsReleaseManagementActivated());
524 }else{
525 /* Check if we should enable the release selection */
526 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
527 if(!empty($tmp)){
528 return(true);
529 }
530 }
531 return(FALSE);
532 }
535 function set_picture($filename)
536 {
537 if (!is_file($filename)){
538 $filename= "./plugins/goto/images/default_icon.png";
539 $this->gosaApplicationIcon= "*removed*";
540 }
542 if (file_exists($filename)){
543 $fd = fopen ($filename, "rb");
544 $this->iconData= fread ($fd, filesize ($filename));
545 session::set('binary',$this->iconData);
546 session::set('binarytype',"image/jpeg");
547 $this->gosaApplicationIcon= $this->iconData;
549 fclose ($fd);
550 }
551 }
553 function getCopyDialog()
554 {
555 $vars = array("cn");
557 $str ="<h2>"._("Application settings")."</h2>
558 <table>
559 <tr>
560 <td>".
561 _("Application name").
562 "</td>
563 <td>
564 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
565 value='".$this->cn."'
566 title='"._("Application name to be displayed (i.e. below icons)")."'>
567 </td>
568 </tr>
569 </table>";
570 $ret = array();
571 $ret['status'] = "";
572 $ret['string'] = $str;
573 return($ret);
574 }
576 function saveCopyDialog()
577 {
578 if(isset($_POST['cn'])){
579 $this->cn = $_POST['cn'];
580 }
581 }
584 function PrepareForCopyPaste($source)
585 {
586 plugin::PrepareForCopyPaste($source);
587 $source_o = new application($this->config,$source['dn']);
588 $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;
589 }
592 /* Return plugin informations for acl handling
593 #FIXME FAIscript seams to ununsed within this class... */
594 static function plInfo()
595 {
596 return (array(
597 "plShortName" => _("Generic"),
598 "plDescription" => _("Application generic"),
599 "plSelfModify" => FALSE,
600 "plDepends" => array(),
601 "plPriority" => 0,
602 "plSection" => array("administration"),
603 "plCategory" => array("application" => array("description" => _("Application"),
604 "objectClass" => "gosaApplication")),
605 "plProvidedAcls"=> array(
606 "cn" => _("Name"),
607 "base" => _("Base"),
608 "description" => _("Description"),
609 "gosaApplicationExecute" => _("Execute"),
610 "gosaApplicationName" => _("Name"),
611 "gosaApplicationIcon" => _("Icon"),
612 "gotoLogonScript" => _("Script content"),
614 "execForGroupmembers" => _("Only executable for members"), // G
615 "placeOnDesktop" => _("Place icon on members desktop"), // D
616 "placeOnKicker" => _("Place entry in members launch bar"), // L
617 "placeInStartmenu" => _("Place entry in members startmenu"), // M
618 "overwriteConfig" => _("Replace user configuration on startup")) // O
619 ));
620 }
621 }
622 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
623 ?>