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;
20 /* Headpage attributes */
21 var $last_sorting= "invalid";
22 var $applications= array();
24 /* attribute list for save action */
25 var $attributes= array("cn", "description", "gosaApplicationExecute", "gosaApplicationName","gosaApplicationIcon",
26 "gosaApplicationFlags","gotoLogonScript");
27 var $objectclasses= array("top", "gosaApplication");
29 var $isReleaseApplikation = false;
31 function application ($config, $dn= NULL)
32 {
33 plugin::plugin ($config, $dn);
35 $tmp = search_config($this->config->data,"faiManagement","CLASS");
36 if(!empty($tmp)) {
37 if(!preg_match("/^ou=apps,/",$_SESSION['appfilter']['release'])){
38 $this->isReleaseApplikation = true;
39 }
40 }
42 /* Load icon */
43 $ldap= $config->get_ldap_link();
44 if ($dn != 'new'){
45 $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
46 $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
47 }
48 if ($this->iconData == ""){
49 $this->set_picture("");
50 }
51 $_SESSION['binary']= $this->iconData;
52 $_SESSION['binarytype']= "image/jpeg";
53 $this->gosaApplicationIcon= $this->iconData;
55 /* This is always an account */
56 $this->is_account= TRUE;
58 if ($this->dn == "new"){
59 if(isset($_SESSION['CurrentMainBase'])){
60 $this->base= $_SESSION['CurrentMainBase'];
61 }else{
62 $ui= get_userinfo();
63 $this->base= dn2base($ui->dn);
64 }
65 } else {
66 $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
67 }
68 }
71 function generateTemplate(){
72 $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
74 $values = array();
75 $names = array();
76 if($this->parent->by_object['applicationParameters']->is_account){
77 $names = $this->parent->by_object['applicationParameters']->option_name;
78 $values = $this->parent->by_object['applicationParameters']->option_value;
79 }
81 if (count($names)){
82 $str .="# This plugin handles these environment variables:\n";
83 } else {
84 $str .="# This plugin handles no environment variables.\n";
85 }
87 foreach($names as $index => $name){
89 // Fix length
90 for($i = strlen($name) ; $i < 30 ; $i++){
91 $name= $name." ";
92 }
93 if((isset($values[$index]))&&(!empty($values[$index]))){
94 $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
95 }else{
96 $str.= "# ".$name."\t("._("no example").")\n";
97 }
98 }
99 $str .= "#\n".
100 "# Don't remove the following tag, it is used for header update.\n".
101 "### END HEADER ###";
103 return($str);
104 }
106 function execute()
107 {
108 /* Call parent execute */
109 plugin::execute();
111 $smarty= get_smarty();
113 $tmp = $this->plInfo();
114 foreach($tmp['plProvidedAcls'] as $name => $translation){
115 $smarty->assign($name."ACL",$this->getacl($name));
116 }
118 $baseACL = $this->getacl("base");
119 if(!$this->acl_is_moveable()) {
120 $baseACL = preg_replace("/w/","",$baseACL);
121 }
122 $smarty->assign("baseACL", $baseACL);
124 /* Do we represent a valid group? */
125 if (!$this->is_account && $this->parent == NULL){
126 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
127 _("This 'dn' is no application.")."</b>";
128 return ($display);
129 }
131 /* Download requested */
132 foreach($_POST as $name => $value){
133 if(preg_match("/^downloadScript/",$name)){
134 $_SESSION['binary'] = $this->gotoLogonScript;
135 $_SESSION['binarytype'] = "octet-stream";
136 $_SESSION['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['binary'] = $this->iconData;
144 $_SESSION['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']))){
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 $smarty->assign("bases", $this->config->idepartments);
158 if ($this->dn == "new"){
159 $smarty->assign("selectmode", "");
160 $smarty->assign("namemode", "");
161 } else {
162 $smarty->assign("namemode", "readonly");
163 $smarty->assign("selectmode", "disabled");
164 }
166 /* Base select dialog */
167 $once = true;
168 foreach($_POST as $name => $value){
169 if(preg_match("/^chooseBase/",$name) && $once){
170 $once = false;
171 $this->dialog = new baseSelectDialog($this->config,$this);
172 $this->dialog->setCurrentBase($this->base);
173 }
174 }
176 /* Dialog handling */
177 if(is_object($this->dialog)){
178 /* Must be called before save_object */
179 $this->dialog->save_object();
181 if($this->dialog->isClosed()){
182 $this->dialog = false;
183 }elseif($this->dialog->isSelected()){
184 $this->base = $this->dialog->isSelected();
185 $this->dialog= false;
186 }else{
187 return($this->dialog->execute());
188 }
189 }
191 /* Get random number for pictures */
192 srand((double)microtime()*1000000);
193 $smarty->assign("rand", rand(0, 10000));
195 /* Variables */
196 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
197 $smarty->assign($val, $this->$val);
198 }
200 /* Checkboxes */
201 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
202 "L" => "place_on_kicker",
203 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
204 if (preg_match("/$key/", $this->gosaApplicationFlags)){
205 $smarty->assign("$val", "checked");
206 } else {
207 $smarty->assign("$val", "");
208 }
209 }
211 $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
212 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
213 $smarty->assign("base_select", $this->base);
214 /* Show main page */
215 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
216 }
219 function remove_from_parent()
220 {
221 $ldap= $this->config->get_ldap_link();
222 $ldap->rmDir($this->dn);
223 show_ldap_error($ldap->get_error(), sprintf(_("Removing of application with dn '%s' failed."),$this->dn));
225 /* Optionally execute a command after we're done */
226 $this->handle_post_events("remove");
228 /* Delete references to object groups */
229 $ldap->cd ($this->config->current['BASE']);
230 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
231 while ($ldap->fetch()){
232 $og= new ogroup($this->config, $ldap->getDN());
233 unset($og->member[$this->dn]);
234 $og->save ();
235 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
236 }
237 $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
238 while ($attrs= $ldap->fetch()){
239 $ag= new appgroup($this->config, $ldap->getDN());
240 $ag->removeApp($this->cn);
241 $ag->save ();
242 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
243 }
245 }
248 /* Save data to object */
249 function save_object()
250 {
251 if($this->isReleaseApplikation){
252 $tmpBase = $this->base;
253 }
255 if (isset($_POST['cn'])){
257 /* Save attributes */
258 parent::save_object();
260 /* Save application flags */
261 $flag= "";
262 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
263 $flag.= "G";
264 }
265 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
266 $flag.= "D";
267 }
268 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
269 $flag.= "L";
270 }
271 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
272 $flag.= "M";
273 }
274 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
275 $flag.= "O";
276 }
277 if ($this->acl_is_writeable("gosaApplicationFlags")){
278 $this->gosaApplicationFlags= "[$flag]";
279 }
281 /* Check for picture upload */
282 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
283 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
284 print_red (_("The specified picture has not been uploaded correctly."));
285 }
287 if (!function_exists("imagick_blob2image")){
288 /* Get temporary file name for conversation */
289 $fname = tempnam ("/tmp", "GOsa");
291 /* Open file and write out photoData */
292 $fp = fopen ($fname, "w");
293 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
294 fclose ($fp);
296 /* Build conversation query. Filename is generated automatically, so
297 we do not need any special security checks. Exec command and save
298 output. For PHP safe mode, you'll need a configuration which respects
299 image magick as executable... */
300 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
301 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
303 /* Read data written by convert */
304 $output= "";
305 $sh= popen($query, 'r');
306 while (!feof($sh)){
307 $output.= fread($sh, 4096);
308 }
309 pclose($sh);
311 unlink($fname);
312 } else {
314 /* Load the new uploaded Photo */
315 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
316 gosa_log("Can't Load image");
317 }
319 /* Resizing image to 147x200 and blur */
320 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
321 gosa_log("imagick_resize failed");
322 }
324 /* Converting image to JPEG */
325 if(!imagick_convert($handle,"PNG")) {
326 gosa_log("Can't Convert to PNG");
327 }
329 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
330 gosa_log("can't write to specified folder");
331 }
333 imagick_free($handle);
334 }
336 /* Activate new picture */
337 $this->set_picture($_FILES['picture_file']['tmp_name']);
338 }
340 $this->base= $_POST['base'];
341 }
343 if($this->isReleaseApplikation){
344 $this->base = $tmpBase;
345 }
346 }
349 /* Check values */
350 function check()
351 {
352 /* Call common method to give check the hook */
353 $message= plugin::check();
355 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
356 $message[]=(_("Specified execute path must start with '/'."));
357 }
359 /* Permissions for that base? */
360 if ($this->base != ""){
361 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
362 } else {
363 $new_dn= $this->dn;
364 }
366 $ui= get_userinfo();
367 if (!$this->acl_is_createable() && $this->dn == "new"){
368 $message[]= _("You have no permissions to create a application on this 'Base'.");
369 }
371 /* All required fields are set? */
372 if ($this->cn == ""){
373 $message[]= _("Required field 'Name' is not filled.");
374 }
376 if(preg_match("/[^a-z0-9]/",$this->cn)) {
377 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
378 }
380 if ($this->gosaApplicationExecute == ""){
381 $message[]= _("Required field 'Execute' is not filled.");
382 }
384 /* Check for existing application */
385 $ldap= $this->config->get_ldap_link();
386 $ldap->cd($this->config->current["BASE"]);
388 $tmp = search_config($this->config->data,"faiManagement","CLASS");
389 if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
390 $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
391 $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
392 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
393 if($ldap->count()){
394 $attrs = $ldap->fetch();
395 if($this->dn != $attrs['dn']) {
396 $message[]= _("There's already an application with this 'Name'.");
397 }
398 }
399 }else{
400 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,array("cn"));
401 if ($ldap->count()){
402 $attrs = $ldap->fetch();
403 if($this->dn != $attrs['dn']) {
404 $message[]= _("There's already an application with this 'Name'.");
405 }
406 }
407 }
408 return $message;
409 }
412 /* Save to LDAP */
413 function save()
414 {
415 /* Get application script without header part, to check if we must save the script itself */
416 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
418 plugin::save();
419 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
421 /* Write back to ldap */
422 $ldap= $this->config->get_ldap_link();
423 $ldap->cat($this->dn, array('dn'));
425 $a= $ldap->fetch();
426 if (count($a)){
428 /* Remove gotoLogonScript if it is empty */
429 if(empty($script)) {
430 $this->attrs['gotoLogonScript'] = array();
431 }
433 $ldap->cd($this->dn);
434 $this->cleanup();
435 $ldap->modify ($this->attrs);
436 $this->handle_post_events("modify");
437 } else {
439 /* Remove gotoLogonScript if it is empty */
440 if(empty($script)) {
441 unset($this->attrs['gotoLogonScript']);
442 }
444 $ldap->cd($this->config->current['BASE']);
445 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
446 $ldap->cd($this->dn);
447 $ldap->add($this->attrs);
448 $this->handle_post_events("add");
449 }
450 show_ldap_error($ldap->get_error(), sprintf(_("Saving of application with dn '%s' failed."),$this->dn));
451 }
453 function set_picture($filename)
454 {
455 if (!is_file($filename)){
456 $filename= "./images/default_icon.png";
457 $this->gosaApplicationIcon= "*removed*";
458 }
460 if (file_exists($filename)){
461 $fd = fopen ($filename, "rb");
462 $this->iconData= fread ($fd, filesize ($filename));
463 $_SESSION['binary']= $this->iconData;
464 $_SESSION['binarytype']= "image/jpeg";
465 $this->gosaApplicationIcon= $this->iconData;
467 fclose ($fd);
468 }
469 }
471 function getCopyDialog()
472 {
473 $vars = array("cn");
475 $str ="<h2>"._("Application settings")."</h2>
476 <table>
477 <tr>
478 <td>".
479 _("Application name").
480 "</td>
481 <td>
482 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
483 value='".$this->cn."'
484 title='"._("Application name to be displayed (i.e. below icons)")."'>
485 </td>
486 </tr>
487 </table>";
488 $ret = array();
489 $ret['status'] = "";
490 $ret['string'] = $str;
491 return($ret);
492 }
494 function saveCopyDialog()
495 {
496 if(isset($_POST['cn'])){
497 $this->cn = $_POST['cn'];
498 }
499 }
502 /* Return plugin informations for acl handling
503 #FIXME FAIscript seams to ununsed within this class... */
504 function plInfo()
505 {
506 return (array(
507 "plShortName" => _("Generic"),
508 "plDescription" => _("Application generic"),
509 "plSelfModify" => FALSE,
510 "plDepends" => array(),
511 "plPriority" => 0,
512 "plSection" => array("administration"),
513 "plCategory" => array("application" => array("description" => _("Application"),
514 "objectClass" => "gosaApplication")),
515 "plProvidedAcls"=> array(
516 "cn" => _("Name"),
517 "base" => _("Base"),
518 "description" => _("Description"),
519 "gosaApplicationExecute" => _("Execute"),
520 "gosaApplicationName" => _("Name"),
521 "gosaApplicationIcon" => _("Icon"),
522 "gosaApplicationFlags" => _("Flag"),
523 "gotoLogonScript" => _("Script content"),
525 "exec_for_groupmembers" => _("Only executable for members"), // G
526 "place_on_desktop" => _("Place icon on members desktop"), // D
527 "place_on_kicker" => _("Place entry in members launch bar"), // L
528 "place_in_startmenu" => _("Place entry in members startmenu"), // M
529 "overwrite_config" => _("Replace user configuration on startup")) // O
530 ));
531 }
532 }
533 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
534 ?>