Code

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