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, $parent= NULL)
32 {
33 plugin::plugin ($config, $dn, $parent);
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 if(isset($_POST['download'])){
114 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
115 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
116 header("Cache-Control: no-cache");
117 header("Pragma: no-cache");
118 header("Cache-Control: post-check=0, pre-check=0");
119 header("Content-type: application/octet-stream");
120 header( "Content-disposition: attachment; filename=".$this->cn.".gotoLogonScript" );
121 echo $this->gotoLogonScript;
122 exit();
123 }
125 /* Do we represent a valid group? */
126 if (!$this->is_account && $this->parent == NULL){
127 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
128 _("This 'dn' is no application.")."</b>";
129 return ($display);
130 }
132 $head = $this->generateTemplate();
133 $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
135 if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
136 $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
137 $this->gotoLogonScript = $str;
138 }
140 /* Fill templating stuff */
141 $smarty->assign("cn", $this->cn);
142 $smarty->assign("bases", $this->config->idepartments);
143 if ($this->dn == "new"){
144 $smarty->assign("selectmode", "");
145 $smarty->assign("namemode", "");
146 } else {
147 $smarty->assign("namemode", "readonly");
148 $smarty->assign("selectmode", "disabled");
149 }
151 /* Base select dialog */
152 $once = true;
153 foreach($_POST as $name => $value){
154 if(preg_match("/^chooseBase/",$name) && $once){
155 $once = false;
156 $this->dialog = new baseSelectDialog($this->config);
157 $this->dialog->setCurrentBase($this->base);
158 }
159 }
161 /* Dialog handling */
162 if(is_object($this->dialog)){
163 /* Must be called before save_object */
164 $this->dialog->save_object();
166 if($this->dialog->isClosed()){
167 $this->dialog = false;
168 }elseif($this->dialog->isSelected()){
169 $this->base = $this->dialog->isSelected();
170 $this->dialog= false;
171 }else{
172 return($this->dialog->execute());
173 }
174 }
176 /* Get random number for pictures */
177 srand((double)microtime()*1000000);
178 $smarty->assign("rand", rand(0, 10000));
180 /* Variables */
181 foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn","gotoLogonScript","gosaApplicationIcon") as $val){
182 $smarty->assign($val, $this->$val);
183 $smarty->assign($val."ACL", chkacl($this->acl, $val));
184 }
185 $smarty->assign("baseACL", chkacl($this->acl,"base"));
187 /* Checkboxes */
188 foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
189 "L" => "place_on_kicker",
190 "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
191 if (preg_match("/$key/", $this->gosaApplicationFlags)){
192 $smarty->assign("$val", "checked");
193 } else {
194 $smarty->assign("$val", "");
195 }
196 }
198 $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
199 $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
200 $smarty->assign("base_select", $this->base);
201 $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
202 /* Show main page */
203 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
204 }
207 function remove_from_parent()
208 {
209 $ldap= $this->config->get_ldap_link();
210 $ldap->rmDir($this->dn);
211 show_ldap_error($ldap->get_error(), _("Removing application failed"));
213 /* Optionally execute a command after we're done */
214 $this->handle_post_events("remove");
216 /* Delete references to object groups */
217 $ldap->cd ($this->config->current['BASE']);
218 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
219 while ($ldap->fetch()){
220 $og= new ogroup($this->config, $ldap->getDN());
221 unset($og->member[$this->dn]);
222 $og->save ();
223 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
224 }
225 $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
226 while ($attrs= $ldap->fetch()){
227 $ag= new appgroup($this->config, $ldap->getDN());
228 $ag->removeApp($this->cn);
229 $ag->save ();
230 show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
231 }
233 }
236 /* Save data to object */
237 function save_object()
238 {
239 if($this->isReleaseApplikation){
240 $tmpBase = $this->base;
241 }
243 if (isset($_POST['cn'])){
245 /* Save attributes */
246 parent::save_object();
248 /* Save application flags */
249 $flag= "";
250 if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
251 $flag.= "G";
252 }
253 if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
254 $flag.= "D";
255 }
256 if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
257 $flag.= "L";
258 }
259 if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
260 $flag.= "M";
261 }
262 if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
263 $flag.= "O";
264 }
265 if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
266 $this->gosaApplicationFlags= "[$flag]";
267 }
269 /* Check for picture upload */
270 if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
271 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
272 print_red (_("The specified picture has not been uploaded correctly."));
273 }
275 if (!function_exists("imagick_blob2image")){
276 /* Get temporary file name for conversation */
277 $fname = tempnam ("/tmp", "GOsa");
279 /* Open file and write out photoData */
280 $fp = fopen ($fname, "w");
281 fwrite ($fp, $_FILES['picture_file']['tmp_name']);
282 fclose ($fp);
284 /* Build conversation query. Filename is generated automatically, so
285 we do not need any special security checks. Exec command and save
286 output. For PHP safe mode, you'll need a configuration which respects
287 image magick as executable... */
288 $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
289 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
291 /* Read data written by convert */
292 $output= "";
293 $sh= popen($query, 'r');
294 while (!feof($sh)){
295 $output.= fread($sh, 4096);
296 }
297 pclose($sh);
299 unlink($fname);
300 } else {
302 /* Load the new uploaded Photo */
303 if(!$handle = imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
304 gosa_log("Can't Load image");
305 }
307 /* Resizing image to 147x200 and blur */
308 if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
309 gosa_log("imagick_resize failed");
310 }
312 /* Converting image to JPEG */
313 if(!imagick_convert($handle,"PNG")) {
314 gosa_log("Can't Convert to PNG");
315 }
317 if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
318 gosa_log("can't write to specified folder");
319 }
321 imagick_free($handle);
322 }
324 /* Activate new picture */
325 $this->set_picture($_FILES['picture_file']['tmp_name']);
326 }
329 /* Save base, since this is no LDAP attribute */
330 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
331 $this->base= $_POST['base'];
332 }
333 }
335 if($this->isReleaseApplikation){
336 $this->base = $tmpBase;
337 }
338 }
341 /* Check values */
342 function check()
343 {
344 /* Call common method to give check the hook */
345 $message= plugin::check();
347 if(!preg_match("#^/#",$this->gosaApplicationExecute)){
348 $message[]=(_("Specified execute path must start with '/'."));
349 }
351 /* Permissions for that base? */
352 if ($this->base != ""){
353 $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
354 } else {
355 $new_dn= $this->dn;
356 }
358 $ui= get_userinfo();
359 $acl= get_permissions ($new_dn, $ui->subtreeACL);
360 $acl= get_module_permission($acl, "application", $new_dn);
361 if (chkacl($acl, "create") != ""){
362 $message[]= _("You have no permissions to create a application on this 'Base'.");
363 }
365 /* All required fields are set? */
366 if ($this->cn == ""){
367 $message[]= _("Required field 'Name' is not filled.");
368 }
370 if(preg_match("/[^a-z0-9]/",$this->cn)) {
371 $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
372 }
374 if ($this->gosaApplicationExecute == ""){
375 $message[]= _("Required field 'Execute' is not filled.");
376 }
378 /* Check for existing application */
379 $ldap= $this->config->get_ldap_link();
380 $ldap->cd($this->config->current["BASE"]);
382 $tmp = search_config($this->config->data,"faiManagement","CLASS");
383 if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
384 $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
385 $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
386 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
387 if($ldap->count()){
388 $attrs = $ldap->fetch();
389 if($this->dn != $attrs['dn']) {
390 $message[]= _("There's already an application with this 'Name'.");
391 }
392 }
393 }else{
394 $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,array("cn"));
395 if ($ldap->count()){
396 $attrs = $ldap->fetch();
397 if($this->dn != $attrs['dn']) {
398 $message[]= _("There's already an application with this 'Name'.");
399 }
400 }
401 }
402 return $message;
403 }
406 /* Save to LDAP */
407 function save()
408 {
409 /* Get application script without header part, to check if we must save the script itself */
410 $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
412 plugin::save();
413 $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
415 /* Write back to ldap */
416 $ldap= $this->config->get_ldap_link();
417 $ldap->cat($this->dn, array('dn'));
419 $a= $ldap->fetch();
420 if (count($a)){
422 /* Remove gotoLogonScript if it is empty */
423 if(empty($script)) {
424 $this->attrs['gotoLogonScript'] = array();
425 }
427 $ldap->cd($this->dn);
428 $this->cleanup();
429 $ldap->modify ($this->attrs);
430 $this->handle_post_events("modify");
431 } else {
433 /* Remove gotoLogonScript if it is empty */
434 if(empty($script)) {
435 unset($this->attrs['gotoLogonScript']);
436 }
438 $ldap->cd($this->config->current['BASE']);
439 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
440 $ldap->cd($this->dn);
441 $ldap->add($this->attrs);
442 $this->handle_post_events("add");
443 }
444 show_ldap_error($ldap->get_error(), _("Saving application failed"));
445 }
447 function set_picture($filename)
448 {
449 if (!is_file($filename)){
450 $filename= "./images/default_icon.png";
451 $this->gosaApplicationIcon= "*removed*";
452 }
454 if (file_exists($filename)){
455 $fd = fopen ($filename, "rb");
456 $this->iconData= fread ($fd, filesize ($filename));
457 $_SESSION['binary']= $this->iconData;
458 $_SESSION['binarytype']= "image/jpeg";
459 $this->gosaApplicationIcon= $this->iconData;
461 fclose ($fd);
462 }
463 }
465 function getCopyDialog()
466 {
467 $vars = array("cn");
469 $str ="<h2>"._("Application settings")."</h2>
470 <table>
471 <tr>
472 <td>".
473 _("Application name").
474 "</td>
475 <td>
476 <input id='gosaApplicationName' name='cn' size='35' maxlength='60'
477 value='".$this->cn."'
478 title='"._("Application name to be displayed (i.e. below icons)")."'>
479 </td>
480 </tr>
481 </table>";
482 $ret = array();
483 $ret['status'] = "";
484 $ret['string'] = $str;
485 return($ret);
486 }
488 function saveCopyDialog()
489 {
490 if(isset($_POST['cn'])){
491 $this->cn = $_POST['cn'];
492 }
493 }
494 }
495 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
496 ?>