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");
30 function application (&$config, $dn= NULL, $parent= NULL)
31 {
32 plugin::plugin ($config, $dn, $parent);
34 /* Load icon */
35 $ldap= $config->get_ldap_link();
36 $this->orig_dn = $dn;
37 if ($dn != 'new'){
38 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
39 $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
40 }
41 if ($this->iconData == ""){
42 $this->set_picture("");
43 }
44 session::set('binary',$this->iconData);
45 session::set('binarytype',"image/jpeg");
46 $this->gosaApplicationIcon= $this->iconData;
48 /* This is always an account */
49 $this->is_account= TRUE;
51 if ($this->dn == "new"){
52 if(session::is_set('CurrentMainBase')){
53 $this->base = session::get('CurrentMainBase');
54 }else{
55 $ui= get_userinfo();
56 $this->base= dn2base($ui->dn);
57 }
58 } else {
59 $this->base= preg_replace ("/^[^,]+,".get_ou('applicationou')."/", "", $this->dn);
60 }
62 $this->orig_cn = $this->cn;
63 $this->orig_base = $this->base;
64 }
67 function generateTemplate()
68 {
69 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
71 $values = array();
72 $names = array();
73 if($this->parent->by_object['applicationParameters']->is_account){
74 $names = $this->parent->by_object['applicationParameters']->option_name;
75 $values = $this->parent->by_object['applicationParameters']->option_value;
76 }
78 if (count($names)){
79 $str .="# This plugin handles these environment variables:\n";
80 } else {
81 $str .="# This plugin handles no environment variables.\n";
82 }
84 foreach($names as $index => $name){
86 // Fix length
87 for($i = strlen($name) ; $i < 30 ; $i++){
88 $name= $name." ";
89 }
90 if((isset($values[$index]))&&(!empty($values[$index]))){
91 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
92 }else{
93 $str.= "# ".$name."\t("._("no example").")\n";
94 }
95 }
96 $str .= "#\n".
97 "# Don't remove the following tag, it is used for header update.\n".
98 "### END HEADER ###";
100 return($str);
101 }
103 function execute()
104 {
105 /* Call parent execute */
106 plugin::execute();
108 /* Log view */
109 if($this->is_account && !$this->view_logged){
110 $this->view_logged = TRUE;
111 new log("view","application/".get_class($this),$this->dn);
112 }
114 $smarty= get_smarty();
116 $tmp = $this->plInfo();
117 foreach($tmp['plProvidedAcls'] as $name => $translation){
118 $smarty->assign($name."ACL",$this->getacl($name));
119 }
121 /* Do we represent a valid group? */
122 if (!$this->is_account && $this->parent === NULL){
123 $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
124 msgPool::noValidExtension(_("application"))."</b>";
125 return ($display);
126 }
128 /* Download requested */
129 foreach($_POST as $name => $value){
130 if(preg_match("/^downloadScript/",$name)){
131 session::set('binary',$this->gotoLogonScript);
132 session::set('binarytype',"octet-stream");
133 session::set('binaryfile',$this->cn.".gosaApplication");
134 header("location: getbin.php ");
135 exit();
136 }
137 }
139 /* Reassign picture data, sometimes its corrupt cause we started a download of application scripts */
140 session::set('binary',$this->iconData);
141 session::set('binarytype',"image/jpeg");
143 $smarty->assign("rand", rand(0, 10000));
144 $head = $this->generateTemplate();
145 $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
147 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
148 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
149 $this->gotoLogonScript = $str;
150 }
152 /* Fill templating stuff */
153 $smarty->assign("cn", $this->cn);
154 if(!$this->is_release()){
155 $smarty->assign("bases", $this->get_allowed_bases());
156 }else{
157 $smarty->assign("bases", array());
158 }
159 if ($this->dn == "new"){
160 $smarty->assign("selectmode", "");
161 $smarty->assign("namemode", "");
162 } else {
163 $smarty->assign("namemode", "readonly");
164 $smarty->assign("selectmode", "disabled");
165 }
167 /* Base select dialog */
168 $once = true;
169 foreach($_POST as $name => $value){
170 if(preg_match("/^chooseBase/",$name) && $once){
171 $once = false;
172 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
173 $this->dialog->setCurrentBase($this->base);
174 }
175 }
177 /* Dialog handling */
178 if(is_object($this->dialog)){
179 /* Must be called before save_object */
180 $this->dialog->save_object();
182 if($this->dialog->isClosed()){
183 $this->dialog = false;
184 }elseif($this->dialog->isSelected()){
186 /* Just allow selection valid bases */
187 $tmp = $this->get_allowed_bases();
188 if(isset($tmp[$this->dialog->isSelected()])){
189 $this->base = $this->dialog->isSelected();
190 }
191 $this->dialog= false;
192 }else{
193 return($this->dialog->execute());
194 }
195 }
197 /* Get random number for pictures */
198 srand((double)microtime()*1000000);
199 $smarty->assign("rand", rand(0, 10000));
201 /* Variables */
202 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
203 $smarty->assign($val, $this->$val);
204 }
206 /* Checkboxes */
207 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
208 "L" => "place_on_kicker",
209 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
210 if (preg_match("/$key/", $this->gosaApplicationFlags)){
211 $smarty->assign("$val", "checked");
212 } else {
213 $smarty->assign("$val", "");
214 }
215 }
217 $smarty->assign("isReleaseApplikation" ,$this->is_release());
218 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
219 $smarty->assign("base_select", $this->base);
220 /* Show main page */
221 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
222 }
225 function remove_from_parent()
226 {
227 restore_error_handler();
229 /* Parse release out of object dn */
230 $release = preg_replace("/".get_ou("applicationou").normalizePreg($this->base)."$/","",$this->dn);
231 $release = preg_replace("/^cn=[^,]+,/","",$release);
233 /* Get a list of all groups
234 */
235 $groups = array();
236 $ldap= $this->config->get_ldap_link();
237 $ldap->cd($this->config->current['BASE']);
238 $ldap->search("(objectClass=posixGroup)",array("dn"));
239 while($attrs = $ldap->fetch()){
240 $groups[$attrs['dn']] = array();
241 }
243 /* Check if there are groups, useing this application
244 */
245 $found = array();
246 foreach($groups as $group => $data){
247 $ldap->cd($release.$group);
248 $ldap->search("(objectClass=gotoMenuEntry)",array("dn"));
249 while($attrs = $ldap->fetch()){
250 $info = preg_replace("/".normalizePreg($release.$group)."$/","",$attrs['dn']);
251 if(preg_match("/^cn=".$this->cn."/",$info) && !preg_match("/ou=[^,]+,/",$info)){
252 $found[] = $attrs['dn'];
253 }
254 }
255 }
257 /* Create an error message an skip remove, if
258 this application is still in use.
259 */
260 if(count($found)){
261 msg_dialog::display(_("Error"), sprintf(_("Cannot remove application - it is still in use by these objects: %s"), "<br>".msgPool::buildList($found)), ERROR_DIALOG);
262 return(FALSE);
263 }
265 $ldap->rmDir($this->dn);
266 new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
267 if (!$ldap->success()){
268 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()), ERROR_DIALOG);
269 }
271 /* Optionally execute a command after we're done */
272 $this->handle_post_events("remove");
274 /* Delete references to object groups */
275 $ldap->cd ($this->config->current['BASE']);
276 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
277 while ($ldap->fetch()){
278 $og= new ogroup($this->config, $ldap->getDN());
279 unset($og->member[$this->dn]);
280 $og->save ();
281 if (!$ldap->success()){
282 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()), ERROR_DIALOG);
283 }
284 }
285 }
288 /* Save data to object */
289 function save_object()
290 {
291 if (isset($_POST['cn'])){
293 /* Create a base backup and reset the
294 base directly after calling plugin::save_object();
295 Base will be set seperatly a few lines below */
296 $base_tmp = $this->base;
297 plugin::save_object();
298 $this->base = $base_tmp;
300 /* Save attributes */
301 parent::save_object();
303 /* Save application flags */
304 $flag= "";
305 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
306 $flag.= "G";
307 }
308 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
309 $flag.= "D";
310 }
311 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
312 $flag.= "L";
313 }
314 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
315 $flag.= "M";
316 }
317 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
318 $flag.= "O";
319 }
320 if ($this->acl_is_writeable("gosaApplicationFlags")){
321 $this->gosaApplicationFlags= "[$flag]";
322 }
324 /* Remove current picture */
325 if(isset($_POST['remove_picture'])){
326 $this->set_picture("");
327 }
329 /* Check for picture upload */
330 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
332 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
333 msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
334 }
336 if (!function_exists("imagick_blob2image")){
337 /* Get temporary file name for conversation */
338 $fname = tempnam (TEMP_DIR, "GOsa");
340 /* Open file and write out photoData */
341 $fp = fopen ($fname, "w");
342 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
343 fclose ($fp);
345 /* Build conversation query. Filename is generated automatically, so
346 we do not need any special security checks. Exec command and save
347 output. For PHP safe mode, you'll need a configuration which respects
348 image magick as executable... */
349 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
350 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
352 /* Read data written by convert */
353 $output= "";
354 $sh= popen($query, 'r');
355 while (!feof($sh)){
356 $output.= fread($sh, 4096);
357 }
358 pclose($sh);
360 unlink($fname);
361 } else {
363 /* Load the new uploaded Photo */
364 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
365 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("no read permission")), ERROR_DIALOG);
366 }
368 /* Resizing image to 147x200 and blur */
369 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
370 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("cannot resize image")), ERROR_DIALOG);
371 }
373 /* Converting image to JPEG */
374 if(!imagick_convert($handle,"PNG")) {
375 msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("cannot convert image")), ERROR_DIALOG);
376 }
378 if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
379 msg_dialog::display(_("Error"), msgPool::incorrectUpload(sprintf(_("cannot save image to '%s'"), $_FILES['picture_file']['tmp_name'])), ERROR_DIALOG);
380 }
382 imagick_free($handle);
383 }
385 /* Activate new picture */
386 $this->set_picture($_FILES['picture_file']['tmp_name']);
387 }
389 if(!$this->is_release()){
390 $tmp = $this->get_allowed_bases();
391 if(isset($_POST['base'])){
392 if(isset($tmp[$_POST['base']])){
393 $this->base= $_POST['base'];
394 }
395 }
396 }
397 }
398 }
401 /* Check values */
402 function check()
403 {
404 /* Call common method to give check the hook */
405 $message= plugin::check();
407 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
408 $message[]=msgPool::invalid(_("Execute path"),"","","/some/path");
409 }
411 /* Permissions for that base? */
412 if ($this->base != ""){
413 $new_dn= "cn=".$this->cn.",".get_ou('applicationou').$this->base;
414 } else {
415 $new_dn= $this->dn;
416 }
419 if($this->dn == "new"){
420 $this->set_acl_base($this->base);
421 }
423 /* All required fields are set? */
424 if ($this->cn == ""){
425 $message[]= msgPool::required(_("Name"));
426 }
428 if(preg_match("/[^a-z0-9]/",$this->cn)) {
429 $message[]=msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/");
430 }
432 if ($this->gosaApplicationExecute == ""){
433 $message[]= msgPool::required(_("Execute"));
434 }
436 /* Check for existing application */
437 $ldap= $this->config->get_ldap_link();
438 $ldap->cd($this->config->current["BASE"]);
440 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
442 if($this->is_release()){
443 $base = $this->parent->parent->app_release;
444 }else{
445 $base = get_ou('applicationou').$this->base;
446 }
448 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$base,array("cn"));
449 if($ldap->count()){
450 $attrs = $ldap->fetch();
451 if($this->dn != $attrs['dn']) {
452 $message[]= msgPool::duplicated("cn");
453 }
454 }
456 /* Check if we are allowed to create or move this object
457 */
458 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
459 $message[] = msgPool::permCreate();
460 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
461 $message[] = msgPool::permMove();
462 }
464 return $message;
465 }
468 /* Save to LDAP */
469 function save()
470 {
471 /* Get application script without header part, to check if we must save the script itself */
472 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
474 plugin::save();
475 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
477 /* Write back to ldap */
478 $ldap= $this->config->get_ldap_link();
479 $ldap->cat($this->dn, array('dn'));
481 $a= $ldap->fetch();
482 if (count($a)){
484 /* Remove gotoLogonScript if it is empty */
485 if(empty($script)) {
486 $this->attrs['gotoLogonScript'] = array();
487 }
489 $ldap->cd($this->dn);
490 $this->cleanup();
491 $ldap->modify ($this->attrs);
492 $this->handle_post_events("modify");
493 new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
494 } else {
496 /* Remove gotoLogonScript if it is empty */
497 if(empty($script)) {
498 unset($this->attrs['gotoLogonScript']);
499 }
501 $ldap->cd($this->config->current['BASE']);
502 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
503 $ldap->cd($this->dn);
504 $ldap->add($this->attrs);
505 new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
506 $this->handle_post_events("add");
507 }
508 if (!$ldap->success()){
509 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()), ERROR_DIALOG);
510 }
511 }
514 function is_release()
515 {
516 if(isset($this->parent->parent)){
517 return($this->parent->parent->IsReleaseManagementActivated());
518 }else{
519 /* Check if we should enable the release selection */
520 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
521 if(!empty($tmp)){
522 return(true);
523 }
524 }
525 return(FALSE);
526 }
529 function set_picture($filename)
530 {
531 if (!is_file($filename)){
532 $filename= "./images/default_icon.png";
533 $this->gosaApplicationIcon= "*removed*";
534 }
536 if (file_exists($filename)){
537 $fd = fopen ($filename, "rb");
538 $this->iconData= fread ($fd, filesize ($filename));
539 session::set('binary',$this->iconData);
540 session::set('binarytype',"image/jpeg");
541 $this->gosaApplicationIcon= $this->iconData;
543 fclose ($fd);
544 }
545 }
547 function getCopyDialog()
548 {
549 $vars = array("cn");
551 $str ="<h2>"._("Application settings")."</h2>
552 <table>
553 <tr>
554 <td>".
555 _("Application name").
556 "</td>
557 <td>
558 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
559 value='".$this->cn."'
560 title='"._("Application name to be displayed (i.e. below icons)")."'>
561 </td>
562 </tr>
563 </table>";
564 $ret = array();
565 $ret['status'] = "";
566 $ret['string'] = $str;
567 return($ret);
568 }
570 function saveCopyDialog()
571 {
572 if(isset($_POST['cn'])){
573 $this->cn = $_POST['cn'];
574 }
575 }
578 function PrepareForCopyPaste($source)
579 {
580 plugin::PrepareForCopyPaste($source);
581 $source_o = new application($this->config,$source['dn']);
582 $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;
583 }
586 /* Return plugin informations for acl handling
587 #FIXME FAIscript seams to ununsed within this class... */
588 static function plInfo()
589 {
590 return (array(
591 "plShortName" => _("Generic"),
592 "plDescription" => _("Application generic"),
593 "plSelfModify" => FALSE,
594 "plDepends" => array(),
595 "plPriority" => 0,
596 "plSection" => array("administration"),
597 "plCategory" => array("application" => array("description" => _("Application"),
598 "objectClass" => "gosaApplication")),
599 "plProvidedAcls"=> array(
600 "cn" => _("Name"),
601 "base" => _("Base"),
602 "description" => _("Description"),
603 "gosaApplicationExecute" => _("Execute"),
604 "gosaApplicationName" => _("Name"),
605 "gosaApplicationIcon" => _("Icon"),
606 "gosaApplicationFlags" => _("Flag"),
607 "gotoLogonScript" => _("Script content"),
609 "exec_for_groupmembers" => _("Only executable for members"), // G
610 "place_on_desktop" => _("Place icon on members desktop"), // D
611 "place_on_kicker" => _("Place entry in members launch bar"), // L
612 "place_in_startmenu" => _("Place entry in members startmenu"), // M
613 "overwrite_config" => _("Replace user configuration on startup")) // O
614 ));
615 }
616 }
617 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
618 ?>