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;
19 var $view_logged = FALSE;
21 /* Headpage attributes */
22 var $last_sorting= "invalid";
23 var $applications= array();
25 /* attribute list for save action */
26 var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
27 "gosaApplicationFlags","gotoLogonScript");
28 var $objectclasses= array("top", "gosaApplication");
30 var $isReleaseApplikation = false;
32 function application (&$config, $dn= NULL, $parent= NULL)
33 {
34 plugin::plugin ($config, $dn, $parent);
36 $appfilter = session::get('appfilter') ;
38 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
39 if(!empty($tmp)) {
40 if(!preg_match("/^".get_ou('applicationou')."/",$appfilter['release'])){
41 $this->isReleaseApplikation = true;
42 }
43 }
45 /* Load icon */
46 $ldap= $config->get_ldap_link();
47 if ($dn != 'new'){
48 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
49 $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
50 }
51 if ($this->iconData == ""){
52 $this->set_picture("");
53 }
54 session::set('binary',$this->iconData);
55 session::set('binarytype',"image/jpeg");
56 $this->gosaApplicationIcon= $this->iconData;
58 /* This is always an account */
59 $this->is_account= TRUE;
61 if ($this->dn == "new"){
62 if(session::is_set('CurrentMainBase')){
63 $this->base = session::get('CurrentMainBase');
64 }else{
65 $ui= get_userinfo();
66 $this->base= dn2base($ui->dn);
67 }
68 } else {
70 if($this->isReleaseApplikation){
71 $this->base = preg_replace("/^.*,".get_ou('applicationou')."/","",$this->dn);
72 }else{
73 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
74 }
75 }
76 }
79 function generateTemplate()
80 {
81 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
83 $values = array();
84 $names = array();
85 if($this->parent->by_object['applicationParameters']->is_account){
86 $names = $this->parent->by_object['applicationParameters']->option_name;
87 $values = $this->parent->by_object['applicationParameters']->option_value;
88 }
90 if (count($names)){
91 $str .="# This plugin handles these environment variables:\n";
92 } else {
93 $str .="# This plugin handles no environment variables.\n";
94 }
96 foreach($names as $index => $name){
98 // Fix length
99 for($i = strlen($name) ; $i < 30 ; $i++){
100 $name= $name." ";
101 }
102 if((isset($values[$index]))&&(!empty($values[$index]))){
103 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
104 }else{
105 $str.= "# ".$name."\t("._("no example").")\n";
106 }
107 }
108 $str .= "#\n".
109 "# Don't remove the following tag, it is used for header update.\n".
110 "### END HEADER ###";
112 return($str);
113 }
115 function execute()
116 {
117 /* Call parent execute */
118 plugin::execute();
120 /* Log view */
121 if($this->is_account && !$this->view_logged){
122 $this->view_logged = TRUE;
123 new log("view","application/".get_class($this),$this->dn);
124 }
126 $smarty= get_smarty();
128 $tmp = $this->plInfo();
129 foreach($tmp['plProvidedAcls'] as $name => $translation){
130 $smarty->assign($name."ACL",$this->getacl($name));
131 }
133 /* Do we represent a valid group? */
134 if (!$this->is_account && $this->parent === NULL){
135 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
136 _("This 'dn' is no application.")."</b>";
137 return ($display);
138 }
140 /* Download requested */
141 foreach($_POST as $name => $value){
142 if(preg_match("/^downloadScript/",$name)){
143 session::set('binary',$this->gotoLogonScript);
144 session::set('binarytype',"octet-stream");
145 session::set('binaryfile',$this->cn.".gosaApplication");
146 header("location: getbin.php ");
147 exit();
148 }
149 }
151 /* Reassign picture data, sometimes its corrupt cause we started a download of application scripts */
152 session::set('binary',$this->iconData);
153 session::set('binarytype',"image/jpeg");
155 $smarty->assign("rand", rand(0, 10000));
156 $head = $this->generateTemplate();
157 $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
159 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
160 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
161 $this->gotoLogonScript = $str;
162 }
164 /* Fill templating stuff */
165 $smarty->assign("cn", $this->cn);
166 $smarty->assign("bases", $this->get_allowed_bases());
167 if ($this->dn == "new"){
168 $smarty->assign("selectmode", "");
169 $smarty->assign("namemode", "");
170 } else {
171 $smarty->assign("namemode", "readonly");
172 $smarty->assign("selectmode", "disabled");
173 }
175 /* Base select dialog */
176 $once = true;
177 foreach($_POST as $name => $value){
178 if(preg_match("/^chooseBase/",$name) && $once){
179 $once = false;
180 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
181 $this->dialog->setCurrentBase($this->base);
182 }
183 }
185 /* Dialog handling */
186 if(is_object($this->dialog)){
187 /* Must be called before save_object */
188 $this->dialog->save_object();
190 if($this->dialog->isClosed()){
191 $this->dialog = false;
192 }elseif($this->dialog->isSelected()){
194 /* Just allow selection valid bases */
195 $tmp = $this->get_allowed_bases();
196 if(isset($tmp[$this->dialog->isSelected()])){
197 $this->base = $this->dialog->isSelected();
198 }
199 $this->dialog= false;
200 }else{
201 return($this->dialog->execute());
202 }
203 }
205 /* Get random number for pictures */
206 srand((double)microtime()*1000000);
207 $smarty->assign("rand", rand(0, 10000));
209 /* Variables */
210 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
211 $smarty->assign($val, $this->$val);
212 }
214 /* Checkboxes */
215 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
216 "L" => "place_on_kicker",
217 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
218 if (preg_match("/$key/", $this->gosaApplicationFlags)){
219 $smarty->assign("$val", "checked");
220 } else {
221 $smarty->assign("$val", "");
222 }
223 }
225 $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
226 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
227 $smarty->assign("base_select", $this->base);
228 /* Show main page */
229 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
230 }
233 function remove_from_parent()
234 {
235 restore_error_handler();
237 /* Parse release out of object dn */
238 $release = preg_replace("/".get_ou("applicationou").normalizePreg($this->base)."$/","",$this->dn);
239 $release = preg_replace("/^cn=[^,]+,/","",$release);
241 /* Get a list of all groups
242 */
243 $groups = array();
244 $ldap= $this->config->get_ldap_link();
245 $ldap->cd($this->config->current['BASE']);
246 $ldap->search("(objectClass=posixGroup)",array("dn"));
247 while($attrs = $ldap->fetch()){
248 $groups[$attrs['dn']] = array();
249 }
251 /* Check if there are groups, useing this application
252 */
253 $found = array();
254 foreach($groups as $group => $data){
255 $ldap->cd($release.$group);
256 $ldap->search("(objectClass=gotoMenuEntry)",array("dn"));
257 while($attrs = $ldap->fetch()){
258 $info = preg_replace("/".normalizePreg($release.$group)."$/","",$attrs['dn']);
259 if(preg_match("/^cn=".$this->cn."/",$info) && !preg_match("/ou=[^,]+,/",$info)){
260 $found[] = $attrs['dn'];
261 }
262 }
263 }
265 /* Create an error message an skip remove, if
266 this application is still in use.
267 */
268 if(count($found)){
269 $objs = "";
270 $cnt = 3;
271 for($i = 0 ; $i < $cnt ; $i++ ){
272 if(isset($found[$i])){
273 $objs .= " ".$found[$i].",";
274 }
275 }
276 if(count($found) > $cnt){
277 $objs .= "... ";
278 }
279 $objs = trim($objs,", ");
280 print_red(sprintf(_("Can not remove application, it is still in use by these objects: %s."),$objs));
281 return(FALSE);
282 }
284 $ldap->rmDir($this->dn);
285 new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
286 show_ldap_error($ldap->get_error(), sprintf(_("Removing of application with dn '%s' failed."),$this->dn));
288 /* Optionally execute a command after we're done */
289 $this->handle_post_events("remove");
291 /* Delete references to object groups */
292 $ldap->cd ($this->config->current['BASE']);
293 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
294 while ($ldap->fetch()){
295 $og= new ogroup($this->config, $ldap->getDN());
296 unset($og->member[$this->dn]);
297 $og->save ();
298 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
299 }
300 }
303 /* Save data to object */
304 function save_object()
305 {
306 if (isset($_POST['cn'])){
308 /* Create a base backup and reset the
309 base directly after calling plugin::save_object();
310 Base will be set seperatly a few lines below */
311 $base_tmp = $this->base;
312 plugin::save_object();
313 $this->base = $base_tmp;
315 /* Save attributes */
316 parent::save_object();
318 /* Save application flags */
319 $flag= "";
320 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
321 $flag.= "G";
322 }
323 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
324 $flag.= "D";
325 }
326 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
327 $flag.= "L";
328 }
329 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
330 $flag.= "M";
331 }
332 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
333 $flag.= "O";
334 }
335 if ($this->acl_is_writeable("gosaApplicationFlags")){
336 $this->gosaApplicationFlags= "[$flag]";
337 }
339 /* Remove current picture */
340 if(isset($_POST['remove_picture'])){
341 $this->set_picture("");
342 }
344 /* Check for picture upload */
345 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
347 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
348 print_red (_("The specified picture has not been uploaded correctly."));
349 }
351 if (!function_exists("imagick_blob2image")){
352 /* Get temporary file name for conversation */
353 $fname = tempnam ("/tmp", "GOsa");
355 /* Open file and write out photoData */
356 $fp = fopen ($fname, "w");
357 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
358 fclose ($fp);
360 /* Build conversation query. Filename is generated automatically, so
361 we do not need any special security checks. Exec command and save
362 output. For PHP safe mode, you'll need a configuration which respects
363 image magick as executable... */
364 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
365 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
367 /* Read data written by convert */
368 $output= "";
369 $sh= popen($query, 'r');
370 while (!feof($sh)){
371 $output.= fread($sh, 4096);
372 }
373 pclose($sh);
375 unlink($fname);
376 } else {
378 /* Load the new uploaded Photo */
379 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
380 print_red(_("Can't access uploaded image."));
381 }
383 /* Resizing image to 147x200 and blur */
384 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
385 print_red(_("Uploaded image could not be resized, possilby the image magick extension is missing."));
386 }
388 /* Converting image to JPEG */
389 if(!imagick_convert($handle,"PNG")) {
390 print_red(_("Could not convert image to png, possilby the image magick extension is missing."));
391 }
393 if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
394 print_red(sprintf(_("Could not save uploaded image to %s."),$_FILES['picture_file']['tmp_name']));
395 }
397 imagick_free($handle);
398 }
400 /* Activate new picture */
401 $this->set_picture($_FILES['picture_file']['tmp_name']);
402 }
404 if(!$this->isReleaseApplikation){
405 $tmp = $this->get_allowed_bases();
406 if(isset($_POST['base'])){
407 if(isset($tmp[$_POST['base']])){
408 $this->base= $_POST['base'];
409 }
410 }
411 }
412 }
413 }
416 /* Check values */
417 function check()
418 {
419 /* Call common method to give check the hook */
420 $message= plugin::check();
422 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
423 $message[]=(_("Specified execute path must start with '/'."));
424 }
426 /* Permissions for that base? */
427 if ($this->base != ""){
428 $new_dn= "cn=".$this->cn.",".get_ou('applicationou').$this->base;
429 } else {
430 $new_dn= $this->dn;
431 }
434 if($this->dn == "new"){
435 $this->set_acl_base($this->base);
436 }
438 /* All required fields are set? */
439 if ($this->cn == ""){
440 $message[]= _("Required field 'Name' is not filled.");
441 }
443 if(preg_match("/[^a-z0-9]/",$this->cn)) {
444 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
445 }
447 if ($this->gosaApplicationExecute == ""){
448 $message[]= _("Required field 'Execute' is not filled.");
449 }
451 /* Check for existing application */
452 $ldap= $this->config->get_ldap_link();
453 $ldap->cd($this->config->current["BASE"]);
455 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
456 $appfilter = session::get('appfilter');
457 if((!empty($tmp)) && (isset($appfilter['release']))){
458 $baseDn = str_replace($this->config->current['BASE'],$this->base,$appfilter['release']);
459 $baseDn = preg_replace("/".get_ou('applicationou').".*/",get_ou('applicationou').$this->base,$appfilter['release']);
460 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
461 if($ldap->count()){
462 $attrs = $ldap->fetch();
463 if($this->dn != $attrs['dn']) {
464 $message[]= _("There's already an application with this 'Name'.");
465 }
466 }
467 }else{
468 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",get_ou('applicationou').$this->base,array("cn"));
469 if ($ldap->count()){
470 $attrs = $ldap->fetch();
471 if($this->dn != $attrs['dn']) {
472 $message[]= _("There's already an application with this 'Name'.");
473 }
474 }
475 }
476 return $message;
477 }
480 /* Save to LDAP */
481 function save()
482 {
483 /* Get application script without header part, to check if we must save the script itself */
484 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
486 plugin::save();
487 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
489 /* Write back to ldap */
490 $ldap= $this->config->get_ldap_link();
491 $ldap->cat($this->dn, array('dn'));
493 $a= $ldap->fetch();
494 if (count($a)){
496 /* Remove gotoLogonScript if it is empty */
497 if(empty($script)) {
498 $this->attrs['gotoLogonScript'] = array();
499 }
501 $ldap->cd($this->dn);
502 $this->cleanup();
503 $ldap->modify ($this->attrs);
504 $this->handle_post_events("modify");
505 new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
506 } else {
508 /* Remove gotoLogonScript if it is empty */
509 if(empty($script)) {
510 unset($this->attrs['gotoLogonScript']);
511 }
513 $ldap->cd($this->config->current['BASE']);
514 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
515 $ldap->cd($this->dn);
516 $ldap->add($this->attrs);
517 new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
518 $this->handle_post_events("add");
519 }
520 show_ldap_error($ldap->get_error(), sprintf(_("Saving of application with dn '%s' failed."),$this->dn));
521 }
523 function set_picture($filename)
524 {
525 if (!is_file($filename)){
526 $filename= "./images/default_icon.png";
527 $this->gosaApplicationIcon= "*removed*";
528 }
530 if (file_exists($filename)){
531 $fd = fopen ($filename, "rb");
532 $this->iconData= fread ($fd, filesize ($filename));
533 session::set('binary',$this->iconData);
534 session::set('binarytype',"image/jpeg");
535 $this->gosaApplicationIcon= $this->iconData;
537 fclose ($fd);
538 }
539 }
541 function getCopyDialog()
542 {
543 $vars = array("cn");
545 $str ="<h2>"._("Application settings")."</h2>
546 <table>
547 <tr>
548 <td>".
549 _("Application name").
550 "</td>
551 <td>
552 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
553 value='".$this->cn."'
554 title='"._("Application name to be displayed (i.e. below icons)")."'>
555 </td>
556 </tr>
557 </table>";
558 $ret = array();
559 $ret['status'] = "";
560 $ret['string'] = $str;
561 return($ret);
562 }
564 function saveCopyDialog()
565 {
566 if(isset($_POST['cn'])){
567 $this->cn = $_POST['cn'];
568 }
569 }
572 function PrepareForCopyPaste($source)
573 {
574 plugin::PrepareForCopyPaste($source);
575 $source_o = new application($this->config,$source['dn']);
576 $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;
577 }
580 /* Return plugin informations for acl handling
581 #FIXME FAIscript seams to ununsed within this class... */
582 static function plInfo()
583 {
584 return (array(
585 "plShortName" => _("Generic"),
586 "plDescription" => _("Application generic"),
587 "plSelfModify" => FALSE,
588 "plDepends" => array(),
589 "plPriority" => 0,
590 "plSection" => array("administration"),
591 "plCategory" => array("application" => array("description" => _("Application"),
592 "objectClass" => "gosaApplication")),
593 "plProvidedAcls"=> array(
594 "cn" => _("Name"),
595 "base" => _("Base"),
596 "description" => _("Description"),
597 "gosaApplicationExecute" => _("Execute"),
598 "gosaApplicationName" => _("Name"),
599 "gosaApplicationIcon" => _("Icon"),
600 "gosaApplicationFlags" => _("Flag"),
601 "gotoLogonScript" => _("Script content"),
603 "exec_for_groupmembers" => _("Only executable for members"), // G
604 "place_on_desktop" => _("Place icon on members desktop"), // D
605 "place_on_kicker" => _("Place entry in members launch bar"), // L
606 "place_in_startmenu" => _("Place entry in members startmenu"), // M
607 "overwrite_config" => _("Replace user configuration on startup")) // O
608 ));
609 }
610 }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>