Code

Updated image
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiTemplateEntry.inc
1 <?php
3 class faiTemplateEntry extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   var $attributes   = array("cn","description","FAItemplateFile","FAItemplatePath","FAImode","user","group","binary","FAIowner");
8   var $objectclasses= array();
10   var $orig_cn              = "";
12   var $dn            = "";
13   var $cn            = "";
14   var $FAItemplateFile   = "";
15   var $FAItemplatePath   = "";
16   var $description   = "";
17   var $status        = "new";
18   var $FAImode       = "0640";
19   var $FAIowner      = "root.root";
20   var $user          = "root";
21   var $group         = "root";
22   var $binary        = false;
23   var $parent        = NULL;
24   var $FAIstate      = "";
25   
26   function faiTemplateEntry (&$config, $dn= NULL,$object=false)
27   {
28     plugin::plugin ($config, $dn);
30     if((isset($object['cn'])) && (!empty($object['cn']))){
31       $this->orig_cn= $object['cn'];
32       $this->dn=$object['dn'];
33       foreach($object as $name=>$value){
34         $oname = $name;
35         $this->$oname=$value;
36       }
37     }else{
38       $this->status = "new";
39       $this->orig_cn= false;
40     }
42     $this->user = explode( '.', $this->FAIowner );
43     $this->group = $this->user[1];
44     $this->user = $this->user[0];
46     session::set('binary',$this->FAItemplateFile);
47     session::set('binarytype','octet-stream');
48     session::set('binaryfile',basename($this->FAItemplatePath));
50     if(!empty($this->dn) && $this->dn != "new"){
51       $ldap = $this->config->get_ldap_link();
52       session::set('binary',$ldap->get_attribute($this->dn,"FAItemplateFile"));
53       $this->FAItemplateFile  = session::get('binary');
54     }
55     
56     $this->FAImode= sprintf("%0.4s", $this->FAImode)." ";
57   }
60   function execute()
61   {
62     /* Call parent execute */
63     plugin::execute();
65     /* Fill templating stuff */
66     $smarty     = get_smarty();
67     $smarty->assign("rand", rand(0, 10000));
68     $display = "";
70     if(isset($_POST['TmpFileUpload']) && $this->acl_is_writeable("FAItemplateFile")){
71       if($str=file_get_contents($_FILES['FAItemplateFile']['tmp_name'])){
72         $this->FAItemplateFile = $str;
74         /* If we don't have a filename set it from upload filename. */
75         if( 0 == strlen( $this->FAItemplatePath )){
76           $this->FAItemplatePath = $_FILES['FAItemplateFile']['name'];
77         }
79         session::set('binary',$this->FAItemplateFile);
80         session::set('binarytype','octet-stream');
81         session::set('binaryfile',basename($this->FAItemplatePath));
82       }
83     }
84    
85     /* File download requested */
86     if(isset($_GET['getFAItemplate'])){
87       send_binary_content($this->FAItemplateFile,$this->cn.".FAItemplate");
88     }
89  
90     $status= _("no file uploaded yet");
92     $bStatus = false; // Hide download icon on default 
93     
94     if(strlen($this->FAItemplateFile)){
96       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
97       $bStatus = true;  // Display download icon 
98     }
99     $smarty->assign("status",$status);
100     $smarty->assign("bStatus",$bStatus);
102     /* Magic quotes GPC, escapes every ' " \, to solve some security risks 
103      * If we post the escaped strings they will be escaped again
104      */
105     foreach($this->attributes as $attrs){
106       if(get_magic_quotes_gpc()){
107         $smarty->assign($attrs,stripslashes($this->$attrs));
108       }else{
109         $smarty->assign($attrs,($this->$attrs));
110       }
111     }
113     /* Assign file modes */
114     $tmode= "$this->FAImode ";
115     foreach (array("s", "u", "g", "o") as $type){
116       $current= substr($tmode, 0, 1);
117       $tmode=   preg_replace("/^./", "", $tmode);
118       $nr= 1;
119       while ($nr < 5){
120         if ($current & $nr){
121           $smarty->assign($type.$nr, "checked");
122         } else {
123           $smarty->assign($type.$nr, "");
124         }
125         $nr+= $nr;
126       }
127     }
129     $smarty->assign("FAItemplateFile","");
131     foreach($this->attributes as $attr){
132       $smarty->assign($attr."ACL",$this->getacl($attr,preg_match("/freeze/",$this->FAIstate)));
133     }
135     /* We now split cn/FAItemplatePath to make things more clear... */
136     $smarty->assign("cn", basename($this->FAItemplatePath));
137     $smarty->assign("templatePath", dirname($this->FAItemplatePath));
138     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));;
140     $display.=  $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
141     return($display);
142   }
144   /* Save data to object */
145   function save_object()
146   {
147     /* Check if form is posted and we are not freezed */
148     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
150       plugin::save_object();
152       /* Set user.group (FAIowner) attribute */  
153       if(isset($_POST['group']) && isset($_POST["user"]) && $this->acl_is_writeable("FAIowner")){
154         $this->FAIowner = $_POST["user"].'.'.$_POST["group"];
155         $this->user = $_POST['user'];
156         $this->group= $_POST['group'];
157       }
159       /* Check if permissions have changed */
160       if($this->acl_is_writeable("FAImode")){
162         /* Save mode */
163         $tmode= "";
164         foreach (array("s", "u", "g", "o") as $type){
165           $nr= 1;
166           $dest= 0;
167           while ($nr < 5){
168             if (isset($_POST["$type$nr"])){
169               $dest+= $nr;
170             }
171             $nr+= $nr;
172           }
173           $tmode= $tmode.$dest;
174         }
175         $this->FAImode= $tmode;
177         /* Assemble cn/FAItemplatePath */
178         $this->cn= preg_replace('/\/+/', '/', $this->FAItemplatePath.'/'.$this->cn);
179         $this->FAItemplatePath= $this->cn;
180       }
181     }
182   }
185   /* Check supplied data */
186   function check()
187   {
188     /* Call common method to give check the hook */
189     $message= plugin::check();
191     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
192       $message[] = msgPool::duplicated(_("Name"));
193     }
195     if(empty($this->FAItemplateFile)){
196       $message[]= msgPool::required(_("File"));
197     } 
199     if(!preg_match('/^\//', $this->FAItemplatePath)){
200       $message[]= msgPool::invalid(_("Destination path"),"","","/path");
201     } 
202  
203     $b = basename($this->FAItemplatePath); 
204     if(empty($b)){
205       $message[] = msgPool::required(_("File name"));
206     }
208     if(empty($this->user)){
209       $message[] = msgPool::required(_("User"));
210     }
211     elseif(preg_match("/[^0-9a-z]/i",$this->user)){
212       $message[] = msgPool::invalid(_("User"),$this->user,"/[0-9a-z]/");
213     }
215     if(empty($this->group)){
216       $message[] = msgPool::required(_("Group"));
217     }
218     elseif(preg_match("/[^0-9a-z]/i",$this->group)){
219       $message[] = msgPool::invalid(_("Group"),$this->group,"/[0-9a-z]/");
220     }
222     return ($message);
223   }
224  
225   function save()
226   {
227     $tmp=array();
228     foreach($this->attributes as $attrs){ 
229       $tmp[$attrs] = $this->$attrs;
230     }
232     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
233       $tmp['remove']['from']  = $this->orig_cn;
234       $tmp['remove']['to']    = $tmp['cn'];
235     }
236   
237     $tmp['dn']      = $this->dn;  
238     $tmp['status']  = $this->status;  
240     return($tmp);
241   }
243   
244   /* Return plugin informations for acl handling */
245   static function plInfo()
246   {
247     return (array(
248           "plShortName" => _("Template entry"),
249           "plDescription" => _("FAI template entry"),
250           "plSelfModify"  => FALSE,
251           "plDepends"     => array(),
252           "plPriority"    => 25,
253           "plSection"     => array("administration"),
254           "plCategory"    => array("fai"),
255           "plProvidedAcls" => array(
256             "cn"                => _("Name"),
257             "description"       => _("Description"),
258             "FAItemplateFile"   => _("Template file"),
259             "FAItemplatePath"   => _("Template path"),
260             "FAIowner"          => _("File owner"),
261             "FAImode"           => _("File permissions"))
262           ));
263   }
266 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
267 ?>