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 /* attribute list for save action */
21 var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
22 "gosaApplicationFlags","gotoLogonScript");
23 var $objectclasses= array("top", "gosaApplication");
25 var $isReleaseApplikation = false;
27 function application (&$config, $dn= NULL, $parent= NULL)
28 {
29 plugin::plugin ($config, $dn, $parent);
31 $appfilter = session::get('appfilter') ;
33 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
34 if(!empty($tmp)) {
35 if(!preg_match("/^".get_ou('applicationou')."/",$appfilter['release'])){
36 $this->isReleaseApplikation = true;
37 }
38 }
40 /* Load icon */
41 $ldap= $config->get_ldap_link();
42 if ($dn != 'new'){
43 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
44 $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
45 }
46 if ($this->iconData == ""){
47 $this->set_picture("");
48 }
49 session::set('binary',$this->iconData);
50 session::set('binarytype',"image/jpeg");
51 $this->gosaApplicationIcon= $this->iconData;
53 /* This is always an account */
54 $this->is_account= TRUE;
56 if ($this->dn == "new"){
57 if(session::is_set('CurrentMainBase')){
58 $this->base = session::get('CurrentMainBase');
59 }else{
60 $ui= get_userinfo();
61 $this->base= dn2base($ui->dn);
62 }
63 } else {
65 if($this->isReleaseApplikation){
66 $this->base = preg_replace("/^.*,".get_ou('applicationou')."/","",$this->dn);
67 }else{
68 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
69 }
70 }
71 }
74 function generateTemplate()
75 {
76 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
78 $values = array();
79 $names = array();
80 if($this->parent->by_object['applicationParameters']->is_account){
81 $names = $this->parent->by_object['applicationParameters']->option_name;
82 $values = $this->parent->by_object['applicationParameters']->option_value;
83 }
85 if (count($names)){
86 $str .="# This plugin handles these environment variables:\n";
87 } else {
88 $str .="# This plugin handles no environment variables.\n";
89 }
91 foreach($names as $index => $name){
93 // Fix length
94 for($i = strlen($name) ; $i < 30 ; $i++){
95 $name= $name." ";
96 }
97 if((isset($values[$index]))&&(!empty($values[$index]))){
98 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
99 }else{
100 $str.= "# ".$name."\t("._("no example").")\n";
101 }
102 }
103 $str .= "#\n".
104 "# Don't remove the following tag, it is used for header update.\n".
105 "### END HEADER ###";
107 return($str);
108 }
110 function execute()
111 {
112 /* Call parent execute */
113 plugin::execute();
115 /* Log view */
116 if($this->is_account && !$this->view_logged){
117 $this->view_logged = TRUE;
118 new log("view","application/".get_class($this),$this->dn);
119 }
121 $smarty= get_smarty();
123 $tmp = $this->plInfo();
124 foreach($tmp['plProvidedAcls'] as $name => $translation){
125 $smarty->assign($name."ACL",$this->getacl($name));
126 }
128 /* Do we represent a valid group? */
129 if (!$this->is_account && $this->parent === NULL){
130 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
131 _("This 'dn' is no application.")."</b>";
132 return ($display);
133 }
135 /* Download requested */
136 foreach($_POST as $name => $value){
137 if(preg_match("/^downloadScript/",$name)){
138 session::set('binary',$this->gotoLogonScript);
139 session::set('binarytype',"octet-stream");
140 session::set('binaryfile',$this->cn.".gosaApplication");
141 header("location: getbin.php ");
142 exit();
143 }
144 }
146 /* Reassign picture data, sometimes its corrupt cause we started a download of application scripts */
147 session::set('binary',$this->iconData);
148 session::set('binarytype',"image/jpeg");
150 $smarty->assign("rand", rand(0, 10000));
151 $head = $this->generateTemplate();
152 $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
154 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
155 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
156 $this->gotoLogonScript = $str;
157 }
159 /* Fill templating stuff */
160 $smarty->assign("cn", $this->cn);
161 $smarty->assign("bases", $this->get_allowed_bases());
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){
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" => "exec_for_groupmembers", "O" => "overwrite_config",
211 "L" => "place_on_kicker",
212 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
213 if (preg_match("/$key/", $this->gosaApplicationFlags)){
214 $smarty->assign("$val", "checked");
215 } else {
216 $smarty->assign("$val", "");
217 }
218 }
220 $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
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("/".get_ou("applicationou").normalizePreg($this->base)."$/","",$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("/".normalizePreg($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 $objs = "";
265 $cnt = 3;
266 for($i = 0 ; $i < $cnt ; $i++ ){
267 if(isset($found[$i])){
268 $objs .= " ".$found[$i].",";
269 }
270 }
271 if(count($found) > $cnt){
272 $objs .= "... ";
273 }
274 $objs = trim($objs,", ");
275 print_red(sprintf(_("Can not remove application, it is still in use by these objects: %s."),$objs));
276 return(FALSE);
277 }
279 $ldap->rmDir($this->dn);
280 new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
281 if (!$ldap->success()){
282 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
283 }
285 /* Optionally execute a command after we're done */
286 $this->handle_post_events("remove");
288 /* Delete references to object groups */
289 $ldap->cd ($this->config->current['BASE']);
290 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
291 while ($ldap->fetch()){
292 $og= new ogroup($this->config, $ldap->getDN());
293 unset($og->member[$this->dn]);
294 $og->save ();
295 if (!$ldap->success()){
296 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
297 }
298 }
299 }
302 /* Save data to object */
303 function save_object()
304 {
305 if (isset($_POST['cn'])){
307 /* Create a base backup and reset the
308 base directly after calling plugin::save_object();
309 Base will be set seperatly a few lines below */
310 $base_tmp = $this->base;
311 plugin::save_object();
312 $this->base = $base_tmp;
314 /* Save attributes */
315 parent::save_object();
317 /* Save application flags */
318 $flag= "";
319 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
320 $flag.= "G";
321 }
322 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
323 $flag.= "D";
324 }
325 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
326 $flag.= "L";
327 }
328 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
329 $flag.= "M";
330 }
331 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
332 $flag.= "O";
333 }
334 if ($this->acl_is_writeable("gosaApplicationFlags")){
335 $this->gosaApplicationFlags= "[$flag]";
336 }
338 /* Remove current picture */
339 if(isset($_POST['remove_picture'])){
340 $this->set_picture("");
341 }
343 /* Check for picture upload */
344 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
346 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
347 print_red (_("The specified picture has not been uploaded correctly."));
348 }
350 if (!function_exists("imagick_blob2image")){
351 /* Get temporary file name for conversation */
352 $fname = tempnam ("/tmp", "GOsa");
354 /* Open file and write out photoData */
355 $fp = fopen ($fname, "w");
356 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
357 fclose ($fp);
359 /* Build conversation query. Filename is generated automatically, so
360 we do not need any special security checks. Exec command and save
361 output. For PHP safe mode, you'll need a configuration which respects
362 image magick as executable... */
363 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
364 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
366 /* Read data written by convert */
367 $output= "";
368 $sh= popen($query, 'r');
369 while (!feof($sh)){
370 $output.= fread($sh, 4096);
371 }
372 pclose($sh);
374 unlink($fname);
375 } else {
377 /* Load the new uploaded Photo */
378 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
379 print_red(_("Can't access uploaded image."));
380 }
382 /* Resizing image to 147x200 and blur */
383 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
384 print_red(_("Uploaded image could not be resized, possilby the image magick extension is missing."));
385 }
387 /* Converting image to JPEG */
388 if(!imagick_convert($handle,"PNG")) {
389 print_red(_("Could not convert image to png, possilby the image magick extension is missing."));
390 }
392 if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
393 print_red(sprintf(_("Could not save uploaded image to %s."),$_FILES['picture_file']['tmp_name']));
394 }
396 imagick_free($handle);
397 }
399 /* Activate new picture */
400 $this->set_picture($_FILES['picture_file']['tmp_name']);
401 }
403 if(!$this->isReleaseApplikation){
404 $tmp = $this->get_allowed_bases();
405 if(isset($_POST['base'])){
406 if(isset($tmp[$_POST['base']])){
407 $this->base= $_POST['base'];
408 }
409 }
410 }
411 }
412 }
415 /* Check values */
416 function check()
417 {
418 /* Call common method to give check the hook */
419 $message= plugin::check();
421 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
422 $message[]=(_("Specified execute path must start with '/'."));
423 }
425 /* Permissions for that base? */
426 if ($this->base != ""){
427 $new_dn= "cn=".$this->cn.",".get_ou('applicationou').$this->base;
428 } else {
429 $new_dn= $this->dn;
430 }
433 if($this->dn == "new"){
434 $this->set_acl_base($this->base);
435 }
437 /* All required fields are set? */
438 if ($this->cn == ""){
439 $message[]= _("Required field 'Name' is not filled.");
440 }
442 if(preg_match("/[^a-z0-9]/",$this->cn)) {
443 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
444 }
446 if ($this->gosaApplicationExecute == ""){
447 $message[]= _("Required field 'Execute' is not filled.");
448 }
450 /* Check for existing application */
451 $ldap= $this->config->get_ldap_link();
452 $ldap->cd($this->config->current["BASE"]);
454 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
455 $appfilter = session::get('appfilter');
456 if((!empty($tmp)) && (isset($appfilter['release']))){
457 $baseDn = str_replace($this->config->current['BASE'],$this->base,$appfilter['release']);
458 $baseDn = preg_replace("/".get_ou('applicationou').".*/",get_ou('applicationou').$this->base,$appfilter['release']);
459 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
460 if($ldap->count()){
461 $attrs = $ldap->fetch();
462 if($this->dn != $attrs['dn']) {
463 $message[]= _("There's already an application with this 'Name'.");
464 }
465 }
466 }else{
467 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",get_ou('applicationou').$this->base,array("cn"));
468 if ($ldap->count()){
469 $attrs = $ldap->fetch();
470 if($this->dn != $attrs['dn']) {
471 $message[]= _("There's already an application with this 'Name'.");
472 }
473 }
474 }
475 return $message;
476 }
479 /* Save to LDAP */
480 function save()
481 {
482 /* Get application script without header part, to check if we must save the script itself */
483 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
485 plugin::save();
486 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
488 /* Write back to ldap */
489 $ldap= $this->config->get_ldap_link();
490 $ldap->cat($this->dn, array('dn'));
492 $a= $ldap->fetch();
493 if (count($a)){
495 /* Remove gotoLogonScript if it is empty */
496 if(empty($script)) {
497 $this->attrs['gotoLogonScript'] = array();
498 }
500 $ldap->cd($this->dn);
501 $this->cleanup();
502 $ldap->modify ($this->attrs);
503 $this->handle_post_events("modify");
504 new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
505 } else {
507 /* Remove gotoLogonScript if it is empty */
508 if(empty($script)) {
509 unset($this->attrs['gotoLogonScript']);
510 }
512 $ldap->cd($this->config->current['BASE']);
513 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
514 $ldap->cd($this->dn);
515 $ldap->add($this->attrs);
516 new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
517 $this->handle_post_events("add");
518 }
519 if (!$ldap->success()){
520 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
521 }
522 }
524 function set_picture($filename)
525 {
526 if (!is_file($filename)){
527 $filename= "./images/default_icon.png";
528 $this->gosaApplicationIcon= "*removed*";
529 }
531 if (file_exists($filename)){
532 $fd = fopen ($filename, "rb");
533 $this->iconData= fread ($fd, filesize ($filename));
534 session::set('binary',$this->iconData);
535 session::set('binarytype',"image/jpeg");
536 $this->gosaApplicationIcon= $this->iconData;
538 fclose ($fd);
539 }
540 }
542 function getCopyDialog()
543 {
544 $vars = array("cn");
546 $str ="<h2>"._("Application settings")."</h2>
547 <table>
548 <tr>
549 <td>".
550 _("Application name").
551 "</td>
552 <td>
553 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
554 value='".$this->cn."'
555 title='"._("Application name to be displayed (i.e. below icons)")."'>
556 </td>
557 </tr>
558 </table>";
559 $ret = array();
560 $ret['status'] = "";
561 $ret['string'] = $str;
562 return($ret);
563 }
565 function saveCopyDialog()
566 {
567 if(isset($_POST['cn'])){
568 $this->cn = $_POST['cn'];
569 }
570 }
573 function PrepareForCopyPaste($source)
574 {
575 plugin::PrepareForCopyPaste($source);
576 $source_o = new application($this->config,$source['dn']);
577 $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;
578 }
581 /* Return plugin informations for acl handling
582 #FIXME FAIscript seams to ununsed within this class... */
583 static function plInfo()
584 {
585 return (array(
586 "plShortName" => _("Generic"),
587 "plDescription" => _("Application generic"),
588 "plSelfModify" => FALSE,
589 "plDepends" => array(),
590 "plPriority" => 0,
591 "plSection" => array("administration"),
592 "plCategory" => array("application" => array("description" => _("Application"),
593 "objectClass" => "gosaApplication")),
594 "plProvidedAcls"=> array(
595 "cn" => _("Name"),
596 "base" => _("Base"),
597 "description" => _("Description"),
598 "gosaApplicationExecute" => _("Execute"),
599 "gosaApplicationName" => _("Name"),
600 "gosaApplicationIcon" => _("Icon"),
601 "gosaApplicationFlags" => _("Flag"),
602 "gotoLogonScript" => _("Script content"),
604 "exec_for_groupmembers" => _("Only executable for members"), // G
605 "place_on_desktop" => _("Place icon on members desktop"), // D
606 "place_on_kicker" => _("Place entry in members launch bar"), // L
607 "place_in_startmenu" => _("Place entry in members startmenu"), // M
608 "overwrite_config" => _("Replace user configuration on startup")) // O
609 ));
610 }
611 }
612 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
613 ?>