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 $ldap= $this->config->get_ldap_link();
236 $ldap->rmDir($this->dn);
237 new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
238 show_ldap_error($ldap->get_error(), sprintf(_("Removing of application with dn '%s' failed."),$this->dn));
240 /* Optionally execute a command after we're done */
241 $this->handle_post_events("remove");
243 /* Delete references to object groups */
244 $ldap->cd ($this->config->current['BASE']);
245 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
246 while ($ldap->fetch()){
247 $og= new ogroup($this->config, $ldap->getDN());
248 unset($og->member[$this->dn]);
249 $og->save ();
250 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
251 }
252 $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
253 while ($attrs= $ldap->fetch()){
254 $ag= new appgroup($this->config, $ldap->getDN());
255 $ag->removeApp($this->cn);
256 $ag->save ();
257 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
258 }
260 }
263 /* Save data to object */
264 function save_object()
265 {
266 if (isset($_POST['cn'])){
268 /* Create a base backup and reset the
269 base directly after calling plugin::save_object();
270 Base will be set seperatly a few lines below */
271 $base_tmp = $this->base;
272 plugin::save_object();
273 $this->base = $base_tmp;
275 /* Save attributes */
276 parent::save_object();
278 /* Save application flags */
279 $flag= "";
280 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
281 $flag.= "G";
282 }
283 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
284 $flag.= "D";
285 }
286 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
287 $flag.= "L";
288 }
289 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
290 $flag.= "M";
291 }
292 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
293 $flag.= "O";
294 }
295 if ($this->acl_is_writeable("gosaApplicationFlags")){
296 $this->gosaApplicationFlags= "[$flag]";
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 print_red (_("The specified picture has not been uploaded correctly."));
309 }
311 if (!function_exists("imagick_blob2image")){
312 /* Get temporary file name for conversation */
313 $fname = tempnam ("/tmp", "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 print_red(_("Can't access uploaded image."));
341 }
343 /* Resizing image to 147x200 and blur */
344 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
345 print_red(_("Uploaded image could not be resized, possilby the image magick extension is missing."));
346 }
348 /* Converting image to JPEG */
349 if(!imagick_convert($handle,"PNG")) {
350 print_red(_("Could not convert image to png, possilby the image magick extension is missing."));
351 }
353 if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
354 print_red(sprintf(_("Could not save uploaded image to %s."),$_FILES['picture_file']['tmp_name']));
355 }
357 imagick_free($handle);
358 }
360 /* Activate new picture */
361 $this->set_picture($_FILES['picture_file']['tmp_name']);
362 }
364 if(!$this->isReleaseApplikation){
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[]=(_("Specified execute path must start with '/'."));
384 }
386 /* Permissions for that base? */
387 if ($this->base != ""){
388 $new_dn= "cn=".$this->cn.",".get_ou('applicationou').$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[]= _("Required field 'Name' is not filled.");
401 }
403 if(preg_match("/[^a-z0-9]/",$this->cn)) {
404 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
405 }
407 if ($this->gosaApplicationExecute == ""){
408 $message[]= _("Required field 'Execute' is not filled.");
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'));
416 $appfilter = session::get('appfilter');
417 if((!empty($tmp)) && (isset($appfilter['release']))){
418 $baseDn = str_replace($this->config->current['BASE'],$this->base,$appfilter['release']);
419 $baseDn = preg_replace("/".get_ou('applicationou').".*/",get_ou('applicationou').$this->base,$appfilter['release']);
420 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
421 if($ldap->count()){
422 $attrs = $ldap->fetch();
423 if($this->dn != $attrs['dn']) {
424 $message[]= _("There's already an application with this 'Name'.");
425 }
426 }
427 }else{
428 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",get_ou('applicationou').$this->base,array("cn"));
429 if ($ldap->count()){
430 $attrs = $ldap->fetch();
431 if($this->dn != $attrs['dn']) {
432 $message[]= _("There's already an application with this 'Name'.");
433 }
434 }
435 }
436 return $message;
437 }
440 /* Save to LDAP */
441 function save()
442 {
443 /* Get application script without header part, to check if we must save the script itself */
444 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
446 plugin::save();
447 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
449 /* Write back to ldap */
450 $ldap= $this->config->get_ldap_link();
451 $ldap->cat($this->dn, array('dn'));
453 $a= $ldap->fetch();
454 if (count($a)){
456 /* Remove gotoLogonScript if it is empty */
457 if(empty($script)) {
458 $this->attrs['gotoLogonScript'] = array();
459 }
461 $ldap->cd($this->dn);
462 $this->cleanup();
463 $ldap->modify ($this->attrs);
464 $this->handle_post_events("modify");
465 new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
466 } else {
468 /* Remove gotoLogonScript if it is empty */
469 if(empty($script)) {
470 unset($this->attrs['gotoLogonScript']);
471 }
473 $ldap->cd($this->config->current['BASE']);
474 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
475 $ldap->cd($this->dn);
476 $ldap->add($this->attrs);
477 new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
478 $this->handle_post_events("add");
479 }
480 show_ldap_error($ldap->get_error(), sprintf(_("Saving of application with dn '%s' failed."),$this->dn));
481 }
483 function set_picture($filename)
484 {
485 if (!is_file($filename)){
486 $filename= "./images/default_icon.png";
487 $this->gosaApplicationIcon= "*removed*";
488 }
490 if (file_exists($filename)){
491 $fd = fopen ($filename, "rb");
492 $this->iconData= fread ($fd, filesize ($filename));
493 session::set('binary',$this->iconData);
494 session::set('binarytype',"image/jpeg");
495 $this->gosaApplicationIcon= $this->iconData;
497 fclose ($fd);
498 }
499 }
501 function getCopyDialog()
502 {
503 $vars = array("cn");
505 $str ="<h2>"._("Application settings")."</h2>
506 <table>
507 <tr>
508 <td>".
509 _("Application name").
510 "</td>
511 <td>
512 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
513 value='".$this->cn."'
514 title='"._("Application name to be displayed (i.e. below icons)")."'>
515 </td>
516 </tr>
517 </table>";
518 $ret = array();
519 $ret['status'] = "";
520 $ret['string'] = $str;
521 return($ret);
522 }
524 function saveCopyDialog()
525 {
526 if(isset($_POST['cn'])){
527 $this->cn = $_POST['cn'];
528 }
529 }
532 function PrepareForCopyPaste($source)
533 {
534 plugin::PrepareForCopyPaste($source);
535 $source_o = new application($this->config,$source['dn']);
536 $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;
537 }
540 /* Return plugin informations for acl handling
541 #FIXME FAIscript seams to ununsed within this class... */
542 static function plInfo()
543 {
544 return (array(
545 "plShortName" => _("Generic"),
546 "plDescription" => _("Application generic"),
547 "plSelfModify" => FALSE,
548 "plDepends" => array(),
549 "plPriority" => 0,
550 "plSection" => array("administration"),
551 "plCategory" => array("application" => array("description" => _("Application"),
552 "objectClass" => "gosaApplication")),
553 "plProvidedAcls"=> array(
554 "cn" => _("Name"),
555 "base" => _("Base"),
556 "description" => _("Description"),
557 "gosaApplicationExecute" => _("Execute"),
558 "gosaApplicationName" => _("Name"),
559 "gosaApplicationIcon" => _("Icon"),
560 "gosaApplicationFlags" => _("Flag"),
561 "gotoLogonScript" => _("Script content"),
563 "exec_for_groupmembers" => _("Only executable for members"), // G
564 "place_on_desktop" => _("Place icon on members desktop"), // D
565 "place_on_kicker" => _("Place entry in members launch bar"), // L
566 "place_in_startmenu" => _("Place entry in members startmenu"), // M
567 "overwrite_config" => _("Replace user configuration on startup")) // O
568 ));
569 }
570 }
571 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
572 ?>