Code

Fixed application acls
[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)
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     /* Do we represent a valid group? */
114     if (!$this->is_account && $this->parent == NULL){
115       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
116         _("This 'dn' is no application.")."</b>";
117       return ($display);
118     }
120     $_SESSION['binary']       = $this->gotoLogonScript;
121     $_SESSION['binarytype']   = "octet-stream";
122     $_SESSION['binaryfile']   = $this->cn.".gosaApplication";
123     $smarty->assign("rand", rand(0, 10000));
124     $head = $this->generateTemplate();
125     $this->gotoLogonScript= $this->generateTemplate().preg_replace('/.*### END HEADER ###/s', '', $this->gotoLogonScript);
127     if((isset($_POST['upLoad']))&&(isset($_FILES['ScriptFile']))){
128       $str = file_get_contents($_FILES['ScriptFile']['tmp_name']);
129       $this->gotoLogonScript = $str;
130     }
132     /* Fill templating stuff */
133     $smarty->assign("cn", $this->cn);
134     $smarty->assign("bases", $this->config->idepartments);
135     if ($this->dn == "new"){
136       $smarty->assign("selectmode", "");
137       $smarty->assign("namemode", "");
138     } else {
139       $smarty->assign("namemode", "readonly");
140       $smarty->assign("selectmode", "disabled");
141     }
142     
143     /* Base select dialog */
144     $once = true;
145     foreach($_POST as $name => $value){
146       if(preg_match("/^chooseBase/",$name) && $once){
147         $once = false;
148         $this->dialog = new baseSelectDialog($this->config,$this);
149         $this->dialog->setCurrentBase($this->base);
150       }
151     }
153     /* Dialog handling */
154     if(is_object($this->dialog)){
155       /* Must be called before save_object */
156       $this->dialog->save_object();
158       if($this->dialog->isClosed()){
159         $this->dialog = false;
160       }elseif($this->dialog->isSelected()){
161         $this->base = $this->dialog->isSelected();
162         $this->dialog= false;
163       }else{
164         return($this->dialog->execute());
165       }
166     }
168     /* Get random number for pictures */
169     srand((double)microtime()*1000000);
170     $smarty->assign("rand", rand(0, 10000));
172     /* Variables */
173     foreach(array("description", "gosaApplicationExecute", "gosaApplicationName","cn") as $val){
174       $smarty->assign($val, $this->$val);
175       $smarty->assign($val."ACL", chkacl($this->acl, $val));
176     }
177     $smarty->assign("baseACL", chkacl($this->acl,"base"));
179     /* Checkboxes */
180     foreach (array("G" => "exec_for_groupmembers", "O" => "overwrite_config",
181           "L" => "place_on_kicker",
182           "D" => "place_on_desktop", "M" => "place_in_startmenu") as $key => $val){
183       if (preg_match("/$key/", $this->gosaApplicationFlags)){
184         $smarty->assign("$val", "checked");
185       } else {
186         $smarty->assign("$val", "");
187       }
188     }
190     $smarty->assign("isReleaseApplikation" , $this->isReleaseApplikation);
191     $smarty->assign("gotoLogonScript",htmlentities($this->gotoLogonScript, ENT_COMPAT, 'UTF-8'));
192     $smarty->assign("base_select", $this->base);
193     $smarty->assign("gosaApplicationFlagsACL", chkacl($this->acl, "gosaApplicationFlags"));
194     /* Show main page */
195     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
196   }
199   function remove_from_parent()
200   {
201     $ldap= $this->config->get_ldap_link();
202     $ldap->rmDir($this->dn);
203     show_ldap_error($ldap->get_error(), sprintf(_("Removing of application with dn '%s' failed."),$this->dn));
205     /* Optionally execute a command after we're done */
206     $this->handle_post_events("remove");
208     /* Delete references to object groups */
209     $ldap->cd ($this->config->current['BASE']);
210     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
211     while ($ldap->fetch()){
212       $og= new ogroup($this->config, $ldap->getDN());
213       unset($og->member[$this->dn]);
214       $og->save ();
215       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from objectgroup '%s' failed"), $og->dn));
216     }
217     $ldap->search ("(&(objectClass=posixGroup)(gosaMemberApplication=".$this->cn."))", array("cn"));
218     while ($attrs= $ldap->fetch()){
219       $ag= new appgroup($this->config, $ldap->getDN());
220       $ag->removeApp($this->cn);
221       $ag->save ();
222       show_ldap_error($ldap->get_error(), sprintf(_("Removing application from group '%s' failed"), $ag->dn));
223     }
225   }
228   /* Save data to object */
229   function save_object()
230   {
231     if($this->isReleaseApplikation){
232       $tmpBase = $this->base;
233     } 
234  
235     if (isset($_POST['cn'])){
237       /* Save attributes */
238       parent::save_object();
240       /* Save application flags */
241       $flag= "";
242       if (isset($_POST['exec_for_groupmembers']) && $_POST['exec_for_groupmembers'] == 1){
243         $flag.= "G";
244       }
245       if (isset($_POST['place_on_desktop']) && $_POST['place_on_desktop'] == 1){
246         $flag.= "D";
247       }
248       if (isset($_POST['place_on_kicker']) && $_POST['place_on_kicker'] == 1){
249         $flag.= "L";
250       }
251       if (isset($_POST['place_in_startmenu']) && $_POST['place_in_startmenu'] == 1){
252         $flag.= "M";
253       }
254       if (isset($_POST['overwrite_config']) && $_POST['overwrite_config'] == 1){
255         $flag.= "O";
256       }
257       if (chkacl ($this->acl, "gosaApplicationFlags") ==""){
258         $this->gosaApplicationFlags= "[$flag]";
259       }
261       /* Check for picture upload */
262       if (isset($_FILES['picture_file']['name']) && $_FILES['picture_file']['name'] != ""){
263         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
264           print_red (_("The specified picture has not been uploaded correctly."));
265         }
267         if (!function_exists("imagick_blob2image")){
268           /* Get temporary file name for conversation */
269           $fname = tempnam ("/tmp", "GOsa");
271           /* Open file and write out photoData */
272           $fp = fopen ($fname, "w");
273           fwrite ($fp, $_FILES['picture_file']['tmp_name']);
274           fclose ($fp);
276           /* Build conversation query. Filename is generated automatically, so
277              we do not need any special security checks. Exec command and save
278              output. For PHP safe mode, you'll need a configuration which respects
279              image magick as executable... */
280           $query= "convert -size 48x48 $fname -resize 48x48 +profile \"*\" -";
281           @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__, $query, "Execute");
283           /* Read data written by convert */
284           $output= "";
285           $sh= popen($query, 'r');
286           while (!feof($sh)){
287             $output.= fread($sh, 4096);
288           }
289           pclose($sh);
291           unlink($fname);       
292         } else {
294           /* Load the new uploaded Photo */
295           if(!$handle  =  imagick_ReadImage($_FILES['picture_file']['tmp_name'])){
296             gosa_log("Can't Load image");
297           }
299           /* Resizing image to 147x200 and blur */
300           if(!imagick_resize($handle,48,48,IMAGICK_FILTER_GAUSSIAN,0)){
301             gosa_log("imagick_resize failed");
302           }
304           /* Converting image to JPEG */
305           if(!imagick_convert($handle,"PNG")) {
306             gosa_log("Can't Convert to PNG");
307           }
309           if(imagick_writeimage($handle,$_FILES['picture_file']['tmp_name'])){
310             gosa_log("can't write to specified folder");
311           }
313           imagick_free($handle);
314         }
316         /* Activate new picture */
317         $this->set_picture($_FILES['picture_file']['tmp_name']);
318       } 
321       /* Save base, since this is no LDAP attribute */
322       if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
323         $this->base= $_POST['base'];
324       }
325     }
327     if($this->isReleaseApplikation){
328       $this->base = $tmpBase;
329     }
330   }
333   /* Check values */
334   function check()
335   {
336     /* Call common method to give check the hook */
337     $message= plugin::check();
339     if(!preg_match("#^/#",$this->gosaApplicationExecute)){
340       $message[]=(_("Specified execute path must start with '/'."));
341     }
343     /* Permissions for that base? */
344     if ($this->base != ""){
345       $new_dn= "cn=".$this->cn.",ou=apps,".$this->base;
346     } else {
347       $new_dn= $this->dn;
348     }
350     $ui= get_userinfo();
351     $acl= get_permissions ($new_dn, $ui->subtreeACL);
352     $acl= get_module_permission($acl, "application", $new_dn);
353     if (chkacl($acl, "create") != ""){
354       $message[]= _("You have no permissions to create a application on this 'Base'.");
355     }
357     /* All required fields are set? */
358     if ($this->cn == ""){
359       $message[]= _("Required field 'Name' is not filled.");
360     }
362     if(preg_match("/[^a-z0-9]/",$this->cn))     {
363       $message[]=_("Invalid character in application name. Only a-z 0-9 are allowed.");
364     }
366     if ($this->gosaApplicationExecute == ""){
367       $message[]= _("Required field 'Execute' is not filled.");
368     }
370     /* Check for existing application */
371     $ldap= $this->config->get_ldap_link();
372     $ldap->cd($this->config->current["BASE"]);
374     $tmp = search_config($this->config->data,"faiManagement","CLASS");
375     if((!empty($tmp)) && (isset($_SESSION['appfilter']['release']))){
376       $baseDn = str_replace($this->config->current['BASE'],$this->base,$_SESSION['appfilter']['release']);
377       $baseDn = preg_replace("/ou=apps,.*/","ou=apps,".$this->base,$_SESSION['appfilter']['release']);
378       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))",$baseDn,array("cn"));
379       if($ldap->count()){
380         $attrs = $ldap->fetch();
381         if($this->dn != $attrs['dn']) {
382           $message[]= _("There's already an application with this 'Name'.");
383         }
384       }
385     }else{
386       $ldap->ls("(&(objectClass=gosaApplication)(cn=".$this->cn."))","ou=apps,".$this->base,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     }
394     return $message;
395   }
398   /* Save to LDAP */
399   function save()
400   {
401     plugin::save();
402     $this->attrs["gosaApplicationIcon"]= $this->gosaApplicationIcon;
404     /* Write back to ldap */
405     $ldap= $this->config->get_ldap_link();
406     $ldap->cat($this->dn, array('dn'));
408     $a= $ldap->fetch();
409     if (count($a)){
410       $ldap->cd($this->dn);
411       $this->cleanup();
412       $ldap->modify ($this->attrs); 
413       $this->handle_post_events("modify");
414     } else {
415       $ldap->cd($this->config->current['BASE']);
416       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
417       $ldap->cd($this->dn);
418       $ldap->add($this->attrs);
419       $this->handle_post_events("add");
420     }
421     show_ldap_error($ldap->get_error(), sprintf(_("Saving of application with dn '%s' failed."),$this->dn));
422   }
424   function set_picture($filename)
425   {
426     if (!is_file($filename)){
427       $filename= "./images/default_icon.png";
428       $this->gosaApplicationIcon= "*removed*";
429     }
431     if (file_exists($filename)){
432       $fd = fopen ($filename, "rb");
433       $this->iconData= fread ($fd, filesize ($filename));
434       $_SESSION['binary']= $this->iconData;
435       $_SESSION['binarytype']= "image/jpeg";
436       $this->gosaApplicationIcon= $this->iconData;
438       fclose ($fd);
439     }
440   }
442   function getCopyDialog()
443   {
444     $vars = array("cn");
446     $str ="<h2>"._("Application settings")."</h2>
447       <table>
448       <tr>
449       <td>".
450       _("Application name"). 
451       "</td>  
452       <td>
453       <input id='gosaApplicationName' name='cn' size='35' maxlength='60' 
454       value='".$this->cn."' 
455       title='"._("Application name to be displayed (i.e. below icons)")."'>                     
456       </td>
457       </tr>
458       </table>";
459     $ret = array();
460     $ret['status'] = "";
461     $ret['string'] = $str;
462     return($ret);
463   }
465   function saveCopyDialog()
466   {
467     if(isset($_POST['cn'])){
468       $this->cn = $_POST['cn'];
469     }
470   }
473   /* Return plugin informations for acl handling
474       #FIXME FAIscript seams to ununsed within this class... */
475   function plInfo()
476   {
477     return (array(
478           "plShortName"   => _("Generic"),
479           "plDescription" => _("Application generic"),
480           "plSelfModify"  => FALSE,
481           "plDepends"     => array(),
482           "plPriority"    => 0,
483           "plSection"     => array("administration"),
484           "plCategory"    => array("application" => array("description"  => _("Application"),
485                                                           "objectClass"  => "gosaApplication")),
486           "plProvidedAcls"=> array(
487             "cn"                => _("Name"),
488             "description"       => _("Description"),
489             "gosaApplicationExecute"  => _("Execute"),
490             "gosaApplicationName"     => _("Name"),
491             "gosaApplicationIcon"     => _("Icon"),
492             "gosaApplicationFlags"    => _("Flag"),
493             "gotoLogonScript"         => _("Script content"),
494             "gosaApplicationParameter"=> _("Application parameter"),
496             "exec_for_groupmembers" => _("Only executable for members"),              // G
497             "place_on_desktop"      => _("Place icon on members desktop"),            // D
498             "place_on_kicker"       => _("Place entry in members launch bar"),        // L
499             "place_in_startmenu"    => _("Place entry in members startmenu"),         // M
500             "overwrite_config"      => _("Replace user configuration on startup"))  // O
501             ));
502   }
504 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
505 ?>