Code

Added a first set of reference changes - nearly untested
[gosa.git] / plugins / admin / applications / class_applicationGeneric.inc
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     $tmp = search_config($this->config->data,"faiManagement","CLASS");
37     if(!empty($tmp)) {
38       if(!preg_match("/^ou=apps,/",$_SESSION['appfilter']['release'])){
39         $this->isReleaseApplikation = true;  
40       }
41     }
43     /* Load icon */
44     $ldap= $config->get_ldap_link();
45     if ($dn != 'new'){
46       $this->iconData= $ldap->get_attribute($dn, "gosaApplicationIcon");
47       $this->saved_attributes['gosaApplicationIcon'] = $this->iconData;
48     }
49     if ($this->iconData == ""){
50       $this->set_picture("");
51     }
52     $_SESSION['binary']= $this->iconData;
53     $_SESSION['binarytype']= "image/jpeg";
54     $this->gosaApplicationIcon= $this->iconData;
56     /* This is always an account */
57     $this->is_account= TRUE;
59     if ($this->dn == "new"){
60       if(isset($_SESSION['CurrentMainBase'])){
61         $this->base= $_SESSION['CurrentMainBase'];
62       }else{
63         $ui= get_userinfo();
64         $this->base= dn2base($ui->dn);
65       }
66     } else {
68       if($this->isReleaseApplikation){
69         $this->base = preg_replace("/^.*,ou=apps,/","",$this->dn);
70       }else{
71         $this->base= preg_replace ("/^[^,]+,[^,]+,/", "", $this->dn);
72       }
73     }
74   }
77   function generateTemplate()
78   {
79     $str= "# This code is part of GOsa (https://gosa.gonicus.de)\n#\n";
81     $values = array();
82     $names      = array();
83     if($this->parent->by_object['applicationParameters']->is_account){
84       $names = $this->parent->by_object['applicationParameters']->option_name;
85       $values = $this->parent->by_object['applicationParameters']->option_value;
86     }
88     if (count($names)){
89       $str .="# This plugin handles these environment variables:\n";
90     } else {
91       $str .="# This plugin handles no environment variables.\n";
92     }
94     foreach($names as $index => $name){
96       // Fix length
97       for($i = strlen($name) ; $i < 30 ; $i++){
98         $name= $name." ";
99       }
100       if((isset($values[$index]))&&(!empty($values[$index]))){
101         $str.= "# ".$name."\t(e.g. '".$values[$index]."')\n";
102       }else{
103         $str.= "# ".$name."\t("._("no example").")\n";
104       }
105     }
106     $str .= "#\n".
107       "# Don't remove the following tag, it is used for header update.\n".
108       "### END HEADER ###";
110     return($str);
111   }
113   function execute()
114   {
115     /* Call parent execute */
116     plugin::execute();
118     /* Log view */
119     if($this->is_account && !$this->view_logged){
120       $this->view_logged = TRUE;
121       new log("view","application/".get_class($this),$this->dn);
122     }
124     $smarty= get_smarty();
126     $tmp = $this->plInfo();
127     foreach($tmp['plProvidedAcls'] as $name => $translation){
128       $smarty->assign($name."ACL",$this->getacl($name));
129     }
130  
131     /* Do we represent a valid group? */
132     if (!$this->is_account && $this->parent == NULL){
133       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
134         _("This 'dn' is no application.")."</b>";
135       return ($display);
136     }
138     /* Download requested */
139     foreach($_POST as $name => $value){
140       if(preg_match("/^downloadScript/",$name)){
141         $_SESSION['binary']       = $this->gotoLogonScript;
142         $_SESSION['binarytype']   = "octet-stream";
143         $_SESSION['binaryfile']   = $this->cn.".gosaApplication";
144         header("location: getbin.php ");
145         exit();
146       }
147     }
149     /* Reassign picture data, sometimes its corrupt cause we started a download of application scripts */
150     $_SESSION['binary']     = $this->iconData;
151     $_SESSION['binarytype'] = "image/jpeg";
152     
153     $smarty->assign("rand", rand(0, 10000));
154     $head = $this->generateTemplate();
155     $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
157     if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
158       $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
159       $this->gotoLogonScript = $str;
160     }
162     /* Fill templating stuff */
163     $smarty->assign("cn", $this->cn);
164     $smarty->assign("bases", $this->get_allowed_bases());
165     if ($this->dn == "new"){
166       $smarty->assign("selectmode", "");
167       $smarty->assign("namemode", "");
168     } else {
169       $smarty->assign("namemode", "readonly");
170       $smarty->assign("selectmode", "disabled");
171     }
172     
173     /* Base select dialog */
174     $once = true;
175     foreach($_POST as $name => $value){
176       if(preg_match("/^chooseBase/",$name) && $once){
177         $once = false;
178         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
179         $this->dialog->setCurrentBase($this->base);
180       }
181     }
183     /* Dialog handling */
184     if(is_object($this->dialog)){
185       /* Must be called before save_object */
186       $this->dialog->save_object();
188       if($this->dialog->isClosed()){
189         $this->dialog = false;
190       }elseif($this->dialog->isSelected()){
191  
192         /* Just allow selection valid bases */ 
193         $tmp = $this->get_allowed_bases();
194         if(isset($tmp[$this->dialog->isSelected()])){
195           $this->base = $this->dialog->isSelected();
196         }
197         $this->dialog= false;
198       }else{
199         return($this->dialog->execute());
200       }
201     }
203     /* Get random number for pictures */
204     srand((double)microtime()*1000000);
205     $smarty->assign("rand", rand(0, 10000));
207     /* Variables */
208     foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
209       $smarty->assign($val, $this->$val);
210     }
212     /* Checkboxes */
213     foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
214           "L" => "place_on_kicker",
215           "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
216       if (preg_match("/$key/", $this->gosaApplicationFlags)){
217         $smarty->assign("$val", "checked");
218       } else {
219         $smarty->assign("$val", "");
220       }
221     }
223     $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
224     $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
225     $smarty->assign("base_select", $this->base);
226     /* Show main page */
227     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
228   }
231   function remove_from_parent()
232   {
233     $ldap= $this->config->get_ldap_link();
234     $ldap->rmDir($this->dn);
235     new log("remove","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
236     show_ldap_error($ldap->get_error(), sprintf(_("Removing of application with dn '%s' failed."),$this->dn));
238     /* Optionally execute a command after we're done */
239     $this->handle_post_events("remove");
241     /* Delete references to object groups */
242     $ldap->cd ($this->config->current['BASE']);
243     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
244     while ($ldap->fetch()){
245       $og= new ogroup($this->config, $ldap->getDN());
246       unset($og->member[$this->dn]);
247       $og->save ();
248       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
249     }
250     $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
251     while ($attrs= $ldap->fetch()){
252       $ag= new appgroup($this->config, $ldap->getDN());
253       $ag->removeApp($this->cn);
254       $ag->save ();
255       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
256     }
258   }
261   /* Save data to object */
262   function save_object()
263   {
264     if (isset($_POST['cn'])){
266       /* Create a base backup and reset the
267          base directly after calling plugin::save_object();
268          Base will be set seperatly a few lines below */
269       $base_tmp = $this->base;
270       plugin::save_object();
271       $this->base = $base_tmp;
273       /* Save attributes */
274       parent::save_object();
276       /* Save application flags */
277       $flag= "";
278       if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
279         $flag.= "G";
280       }
281       if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
282         $flag.= "D";
283       }
284       if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
285         $flag.= "L";
286       }
287       if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
288         $flag.= "M";
289       }
290       if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
291         $flag.= "O";
292       }
293       if ($this->acl_is_writeable("gosaApplicationFlags")){
294         $this->gosaApplicationFlags= "[$flag]";
295       }
297       /* Remove current picture */
298       if(isset($_POST['remove_picture'])){
299         $this->set_picture("");
300       }
302       /* Check for picture upload */
303       if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
305         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
306           print_red (_("The specified picture has not been uploaded correctly."));
307         }
309         if (!function_exists("imagick_blob2image")){
310           /* Get temporary file name for conversation */
311           $fname = tempnam ("/tmp", "GOsa");
313           /* Open file and write out photoData */
314           $fp = fopen ($fname, "w");
315           fwrite ($fp, $_FILES['picture_file']['tmp_name']);
316           fclose ($fp);
318           /* Build conversation query. Filename is generated automatically, so
319              we do not need any special security checks. Exec command and save
320              output. For PHP safe mode, you'll need a configuration which respects
321              image magick as executable... */
322           $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
323           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
325           /* Read data written by convert */
326           $output= "";
327           $sh= popen($query, 'r');
328           while (!feof($sh)){
329             $output.= fread($sh, 4096);
330           }
331           pclose($sh);
333           unlink($fname);       
334         } else {
336           /* Load the new uploaded Photo */
337           if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
338             print_red(_("Can't access uploaded image."));
339           }
341           /* Resizing image to 147x200 and blur */
342           if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
343             print_red(_("Uploaded image could not be resized, possilby the image magick extension is missing."));
344           }
346           /* Converting image to JPEG */
347           if(!imagick_convert($handle,"PNG")) {
348             print_red(_("Could not convert image to png, possilby the image magick extension is missing."));
349           }
351           if(!imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
352             print_red(sprintf(_("Could not save uploaded image to %s."),$_FILES['picture_file']['tmp_name']));
353           }
355           imagick_free($handle);
356         }
358         /* Activate new picture */
359         $this->set_picture($_FILES['picture_file']['tmp_name']);
360       } 
362       if(!$this->isReleaseApplikation){
363         $tmp = $this->get_allowed_bases();
364         if(isset($_POST['base'])){
365           if(isset($tmp[$_POST['base']])){
366             $this->base= $_POST['base'];
367           }
368         }
369       }
370     }
371   }
374   /* Check values */
375   function check()
376   {
377     /* Call common method to give check the hook */
378     $message= plugin::check();
380     if(!preg_match("#^/#",$this->gosaApplicationExecute)){
381       $message[]=(_("Specified execute path must start with '/'."));
382     }
384     /* Permissions for that base? */
385     if ($this->base != ""){
386       $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
387     } else {
388       $new_dn= $this->dn;
389     }
392     if($this->dn == "new"){
393       $this->set_acl_base($this->base);
394     }
396     /* All required fields are set? */
397     if ($this->cn == ""){
398       $message[]= _("Required field 'Name' is not filled.");
399     }
401     if(preg_match("/[^a-z0-9]/",$this->cn))     {
402       $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
403     }
405     if ($this->gosaApplicationExecute == ""){
406       $message[]= _("Required field 'Execute' is not filled.");
407     }
409     /* Check for existing application */
410     $ldap= $this->config->get_ldap_link();
411     $ldap->cd($this->config->current["BASE"]);
413     $tmp = search_config($this->config->data,"faiManagement","CLASS");
414     if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
415       $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
416       $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
417       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
418       if($ldap->count()){
419         $attrs = $ldap->fetch();
420         if($this->dn != $attrs['dn']) {
421           $message[]= _("There's already an application with this 'Name'.");
422         }
423       }
424     }else{
425       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,array("cn"));
426       if ($ldap->count()){
427         $attrs = $ldap->fetch();
428         if($this->dn != $attrs['dn']) {
429           $message[]= _("There's already an application with this 'Name'.");
430         }
431       }
432     }
433     return $message;
434   }
437   /* Save to LDAP */
438   function save()
439   {
440     /* Get application script without header part, to check if we must save the script itself */
441     $script = preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
443     plugin::save();
444     $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
446     /* Write back to ldap */
447     $ldap= $this->config->get_ldap_link();
448     $ldap->cat($this->dn, array('dn'));
450     $a= $ldap->fetch();
451     if (count($a)){
453       /* Remove gotoLogonScript if it is empty */
454       if(empty($script))    {
455         $this->attrs['gotoLogonScript'] = array();
456       }
458       $ldap->cd($this->dn);
459       $this->cleanup();
460       $ldap->modify ($this->attrs); 
461       $this->handle_post_events("modify");
462       new log("modify","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
463     } else {
464       
465       /* Remove gotoLogonScript if it is empty */
466       if(empty($script))    {
467         unset($this->attrs['gotoLogonScript']);
468       }
470       $ldap->cd($this->config->current['BASE']);
471       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
472       $ldap->cd($this->dn);
473       $ldap->add($this->attrs);
474       new log("create","application/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
475       $this->handle_post_events("add");
476     }
477     show_ldap_error($ldap->get_error(), sprintf(_("Saving of application with dn '%s' failed."),$this->dn));
478   }
480   function set_picture($filename)
481   {
482     if (!is_file($filename)){
483       $filename= "./images/default_icon.png";
484       $this->gosaApplicationIcon= "*removed*";
485     }
487     if (file_exists($filename)){
488       $fd = fopen ($filename, "rb");
489       $this->iconData= fread ($fd, filesize ($filename));
490       $_SESSION['binary']= $this->iconData;
491       $_SESSION['binarytype']= "image/jpeg";
492       $this->gosaApplicationIcon= $this->iconData;
494       fclose ($fd);
495     }
496   }
498   function getCopyDialog()
499   {
500     $vars = array("cn");
502     $str ="<h2>"._("Application settings")."</h2>
503       <table>
504       <tr>
505       <td>".
506       _("Application name"). 
507       "</td>  
508       <td>
509       <input id='gosaApplicationName' name='cn' size='35' maxlength='60' 
510       value='".$this->cn."' 
511       title='"._("Application name to be displayed (i.e. below icons)")."'>                     
512       </td>
513       </tr>
514       </table>";
515     $ret = array();
516     $ret['status'] = "";
517     $ret['string'] = $str;
518     return($ret);
519   }
521   function saveCopyDialog()
522   {
523     if(isset($_POST['cn'])){
524       $this->cn = $_POST['cn'];
525     }
526   }
529   function PrepareForCopyPaste($source)
530   {
531     plugin::PrepareForCopyPaste($source);
532     $source_o = new application($this->config,$source['dn']);
533     $this->gosaApplicationIcon = $source_o->gosaApplicationIcon;     
534   }
537   /* Return plugin informations for acl handling
538       #FIXME FAIscript seams to ununsed within this class... */
539   function plInfo()
540   {
541     return (array(
542           "plShortName"   => _("Generic"),
543           "plDescription" => _("Application generic"),
544           "plSelfModify"  => FALSE,
545           "plDepends"     => array(),
546           "plPriority"    => 0,
547           "plSection"     => array("administration"),
548           "plCategory"    => array("application" => array("description"  => _("Application"),
549                                                           "objectClass"  => "gosaApplication")),
550           "plProvidedAcls"=> array(
551             "cn"                => _("Name"),
552             "base"              => _("Base"),
553             "description"       => _("Description"),
554             "gosaApplicationExecute"  => _("Execute"),
555             "gosaApplicationName"     => _("Name"),
556             "gosaApplicationIcon"     => _("Icon"),
557             "gosaApplicationFlags"    => _("Flag"),
558             "gotoLogonScript"         => _("Script content"),
560             "exec_for_groupmembers" => _("Only executable for members"),              // G
561             "place_on_desktop"      => _("Place icon on members desktop"),            // D
562             "place_on_kicker"       => _("Place entry in members launch bar"),        // L
563             "place_in_startmenu"    => _("Place entry in members startmenu"),         // M
564             "overwrite_config"      => _("Replace user configuration on startup"))  // O
565             ));
566   }
568 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
569 ?>