Code

Added acls to fai template entry
[gosa.git] / plugins / admin / fai / class_faiTemplateEntry.inc
1 <?php
3 class faiTemplateEntry extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account= TRUE;
12   var $attributes   = array("cn","description","FAItemplateFile","FAItemplatePath","FAImode","user","group","binary","FAIowner");
13   var $objectclasses= array();
15   var $orig_cn              = "";
17   var $dn            = "";
18   var $cn            = "";
19   var $FAItemplateFile   = "";
20   var $FAItemplatePath   = "";
21   var $description   = "";
22   var $status        = "new";
23   var $FAImode       = "0640";
24   var $FAIowner      = "root.root";
25   var $user          = "root";
26   var $group         = "root";
27   var $binary        = false;
29   var $FAIstate      = "";
30   
31   function faiTemplateEntry ($config, $dn= NULL,$object=false)
32   {
33     plugin::plugin ($config, $dn);
35     if((isset($object['cn'])) && (!empty($object['cn']))){
36       $this->orig_cn= $object['cn'];
37       $this->dn=$object['dn'];
38       foreach($object as $name=>$value){
39         $oname = $name;
40         $this->$oname=$value;
41       }
43       if(isset($this->attrs['FAIstate'][0])){
44         $this->FAIstate = $this->attrs['FAIstate'][0];
45       }
47     }else{
48       $this->status = "new";
49       $this->orig_cn= false;
50     }
52     $this->user = explode( '.', $this->FAIowner );
53     $this->group = $this->user[1];
54     $this->user = $this->user[0];
56     $_SESSION['binary'] = $this->FAItemplateFile;
57     $_SESSION['binarytype'] = 'octet-stream';
58     $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
60     if(!empty($this->dn) && $this->dn != "new"){
61       $ldap = $this->config->get_ldap_link();
62       $_SESSION['binary'] =$ldap->get_attribute($this->dn,"FAItemplateFile");
63       $this->FAItemplateFile = $_SESSION['binary'];
64     }
65     
66     $this->FAImode= sprintf("%0.4s", $this->FAImode)." ";
67   }
70   function execute()
71   {
72     /* Call parent execute */
73     plugin::execute();
75     /* Fill templating stuff */
76     $smarty     = get_smarty();
77     $smarty->assign("rand", rand(0, 10000));
78     $display = "";
80     if(isset($_POST['TmpFileUpload']) && $this->acl_is_writeable("FAItemplateFile")){
81       if($str=file_get_contents($_FILES['FAItemplateFile']['tmp_name'])){
82         $this->FAItemplateFile = $str;
84         /* If we don't have a filename set it from upload filename. */
85         if( 0 == strlen( $this->FAItemplatePath )){
86           $this->FAItemplatePath = $_FILES['FAItemplateFile']['name'];
87         }
89         $_SESSION['binary']     = $this->FAItemplateFile;
90         $_SESSION['binarytype'] = 'octet-stream';
91         $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
92       }
93     }
94     
95     $status= _("no file uploaded yet");
97     $bStatus = false; // Hide download icon on default 
98     
99     if(strlen($this->FAItemplateFile)){
101       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
102       $bStatus = true;  // Display download icon 
103     }
104     $smarty->assign("status",$status);
105     $smarty->assign("bStatus",$bStatus);
107     /* Magic quotes GPC, escapes every ' " \, to solve some security risks 
108      * If we post the escaped strings they will be escaped again
109      */
110     foreach($this->attributes as $attrs){
111       if(get_magic_quotes_gpc()){
112         $smarty->assign($attrs,stripslashes($this->$attrs));
113       }else{
114         $smarty->assign($attrs,($this->$attrs));
115       }
116     }
118     /* Assign file modes */
119     $tmode= "$this->FAImode ";
120     foreach (array("s", "u", "g", "o") as $type){
121       $current= substr($tmode, 0, 1);
122       $tmode=   preg_replace("/^./", "", $tmode);
123       $nr= 1;
124       while ($nr < 5){
125         if ($current & $nr){
126           $smarty->assign($type.$nr, "checked");
127         } else {
128           $smarty->assign($type.$nr, "");
129         }
130         $nr+= $nr;
131       }
132     }
134     $smarty->assign("FAItemplateFile","");
136     foreach($this->attributes as $attr){
137       $smarty->assign($attr."ACL",$this->getacl($attr,preg_match("/freeze/",$this->FAIstate)));
138     }
140     $display.= "<h2><font color='red'>Fix / problen and ensure that size and content are correct if you download this template. Revision > 4582 (gosa-2.5 is already patched)</font></h2>".$smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
141     return($display);
142   }
144   /* Save data to object */
145   function save_object()
146   {
147     if (!isset($_POST['FAItemplatePath'])){
148       return;
149     }
150     if((isset($_POST['SubObjectFormSubmitted'])) && ($this->FAIstate != "freeze")){
151       foreach($this->attributes as $attrs){
152         if($attrs == "FAItemplateFile") 
153           continue;
154         if($attrs == "FAIowner") {
155           $this->$attrs = $_POST["user"] . '.' . $_POST["group"];
156           continue;
157         }
158         if(isset($_POST[$attrs])){
159           $this->$attrs = $_POST[$attrs];
160         }else{
161           $this->$attrs = "";
162         }
163       }
164       /* Save mode */
165       $tmode= "";
166       foreach (array("s", "u", "g", "o") as $type){
167         $nr= 1;
168         $dest= 0;
169         while ($nr < 5){
170           if (isset($_POST["$type$nr"])){
171             $dest+= $nr;
172           }
173           $nr+= $nr;
174         }
175         $tmode= $tmode.$dest;
176       }
177       $this->FAImode= $tmode;
178     }
179   }
181   /* Check supplied data */
182   function check()
183   {
184     /* Call common method to give check the hook */
185     $message= plugin::check();
187     if(empty($this->FAItemplateFile)){
188       $message[]=_("Please specify a value for attribute 'file'.");
189     } 
191     if(empty($this->FAItemplatePath)){
192       $message[]=_("Please specify a value for attribute 'path'.");
193     } 
194   
195     if(empty($this->cn)){
196       $message[] = _("Please enter a name.");
197     }
199     if(empty($this->user)){
200       $message[] = _("Please enter a user.");
201     }
202     elseif(preg_match("/[^0-9a-z]/i",$this->user)){
203       $message[] = _("Please enter a valid user. Only a-z/0-9 are allowed.");
204     }
206     if(empty($this->group)){
207       $message[] = _("Please enter a group.");
208     }
209     elseif(preg_match("/[^0-9a-z]/i",$this->group)){
210       $message[] = _("Please enter a valid group. Only a-z/0-9 are allowed.");
211     }
213     return ($message);
214   }
215  
216   function save()
217   {
218     $tmp=array();
219     foreach($this->attributes as $attrs){ 
220       $tmp[$attrs] = $this->$attrs;
221     }
223     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
224       $tmp['remove']['from']  = $this->orig_cn;
225       $tmp['remove']['to']    = $tmp['cn'];
226     }
227   
228     $tmp['dn']      = $this->dn;  
229     $tmp['status']  = $this->status;  
231     return($tmp);
232   }
234   
235   /* Return plugin informations for acl handling */
236   function plInfo()
237   {
238     return (array(
239           "plShortName" => _("Template entry"),
240           "plDescription" => _("FAI template entry"),
241           "plSelfModify"  => FALSE,
242           "plDepends"     => array(),
243           "plPriority"    => 25,
244           "plSection"     => array("administration"),
245           "plCategory"    => array("fai"),
246           "plProvidedAcls" => array(
247             "cn"                => _("Name"),
248             "description"       => _("Description"),
249             "FAItemplateFile"   => _("Template file"),
250             "FAItemplatePath"   => _("Template path"),
251             "FAIowner"          => _("File owner"),
252             "FAImode"           => _("File permissions"))
253           ));
254   }
257 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
258 ?>