Code

68fb8972ce65ba8bb1a4be79273083b6f70dedb0
[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(preg_replace("/^.*\//","",$this->FAItemplatePath))){
76           $this->FAItemplatePath = preg_replace("/[^\/]*$/","",$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($_POST['getFAItemplate'])){
87       send_binary_content($this->FAItemplateFile,$this->cn.".FAItemplate");
88     }
90     /* File edit requested */
91     if(isset($_POST['editFAItemplate'])){
92       $this->dialog = new faiTemplateEdit($this->config,$this->dn,$this->FAItemplateFile);
93     }
95     /* File edit requested, was canceled  */
96     if(isset($_POST['templateEditCancel'])){
97       $this->dialog = null;
98     }
100     /* File edit requested, was canceled  */
101     if($this->dialog instanceOf faiTemplateEdit && isset($_POST['templateEditSave'])){
102       $this->dialog->save_object();
103       $msgs = $this->dialog->check();
104       if(count($msgs)){
105         msg_dialog::displayChecks($msgs);
106       }else{
107         $this->FAItemplateFile = $this->dialog->save();
108         $this->dialog = null;
109       }
110     }
112     /* Display opened dialog */
113     if($this->dialog){
114       $this->dialog->save_object();
115       return($this->dialog->execute());
116     }
118     $status= _("no file uploaded yet");
119     $bStatus = false; // Hide download icon on default 
120     if(strlen($this->FAItemplateFile)){
121       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
122       $bStatus = true;  // Display download icon 
123     }
125     $smarty->assign("status",$status);
126     $smarty->assign("bStatus",$bStatus);
128     /* Magic quotes GPC, escapes every ' " \, to solve some security risks 
129      * If we post the escaped strings they will be escaped again
130      */
131     foreach($this->attributes as $attrs){
132         $smarty->assign($attrs,set_post($this->$attrs));
133     }
135     /* Assign file modes */
136     $tmode= "$this->FAImode ";
137     foreach (array("s", "u", "g", "o") as $type){
138       $current= substr($tmode, 0, 1);
139       $tmode=   preg_replace("/^./", "", $tmode);
140       $nr= 1;
141       while ($nr < 5){
142         if ($current & $nr){
143           $smarty->assign($type.$nr, "checked");
144         } else {
145           $smarty->assign($type.$nr, "");
146         }
147         $nr+= $nr;
148       }
149     }
151     $tmp = $this->plInfo();
152     foreach($tmp['plProvidedAcls'] as $attr => $desc){
153       $smarty->assign($attr."ACL",$this->getacl($attr,preg_match("/freeze/",$this->FAIstate)));
154     }
156     /* We now split cn/FAItemplatePath to make things more clear... */
157     $smarty->assign("templateFile", set_post(preg_replace("/^.*\//","",$this->FAItemplatePath)));
158     $smarty->assign("templatePath", set_post(preg_replace("/[^\/]*$/","",$this->FAItemplatePath)));
159     $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));;
161     $display.=  $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
162     return($display);
163   }
165   /* Save data to object */
166   function save_object()
167   {
168     /* Check if form is posted and we are not freezed */
169     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
171       /* Remember destination current path 
172           depending on the ACLs we will assemble a new one later.
173        */
174       $cur_path = $this->FAItemplatePath;
175       plugin::save_object();
177       /* Set user.group (FAIowner) attribute */  
178       if(isset($_POST['group']) && isset($_POST["user"]) && $this->acl_is_writeable("FAIowner")){
179         $this->FAIowner = get_post("user").'.'.get_post("group");
180         $this->user = get_post('user');
181         $this->group= get_post('group');
182       }
184       /* Check if permissions have changed */
185       if($this->acl_is_writeable("FAImode")){
187         /* Save mode */
188         $tmode= "";
189         foreach (array("s", "u", "g", "o") as $type){
190           $nr= 1;
191           $dest= 0;
192           while ($nr < 5){
193             if (isset($_POST["$type$nr"])){
194               $dest+= $nr;
195             }
196             $nr+= $nr;
197           }
198           $tmode= $tmode.$dest;
199         }
200         $this->FAImode= $tmode;
201       }
203       /* Check if we are allowed to change the destination directory 
204        */
205       if($this->acl_is_writeable("FAItemplatePath")){
206         $cur_path = get_post('templatePath').'/'.basename($cur_path);
207       }
209       /* Check if we are allowed to change the destination directory 
210        */
211       if($this->acl_is_writeable("cn")){
212         $cur_path = preg_replace("/[^\/]*$/","",$cur_path).get_post('templateFile');
213       }
214       $cur_path= str_replace("//","/",$cur_path);
215       if(trim($cur_path,"/") == ""){
216         $cur_path= "";
217       }
218       $this->FAItemplatePath = $this->cn= $cur_path;
219     }
220   }
223   /* Check supplied data */
224   function check()
225   {
226     /* Call common method to give check the hook */
227     $message= plugin::check();
229     if(isset($this->parent->SubObjects[$this->cn]) && 
230             $this->parent->SubObjects[$this->cn]['status'] != 'delete' &&
231             $this->cn != $this->orig_cn){
232       $message[] = msgPool::duplicated(_("Name"));
233     }
235     if(empty($this->FAItemplateFile)){
236       $message[]= msgPool::required(_("File"));
237     } 
239     if(!preg_match('/^\//', $this->FAItemplatePath)){
240       $message[]= msgPool::invalid(_("Destination path"),"","","/path");
241     } 
242  
243     $b = trim(basename($this->FAItemplatePath)); 
244     if($b == ""){
245       $message[] = msgPool::required(_("File name"));
246     }
248     if($this->user == ""){
249       $message[] = msgPool::required(_("User"));
250     }elseif(preg_match("/[^0-9a-z]/i",$this->user)){
251       $message[] = msgPool::invalid(_("User"),$this->user,"/[0-9a-z]/");
252     }
254     if($this->group == ""){
255       $message[] = msgPool::required(_("Group"));
256     }elseif (!tests::is_uid($this->group)){
257       if (strict_uid_mode()){
258         $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/");
259       } else {
260         $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/i");
261       }
262     }
264     return ($message);
265   }
266  
267   function save()
268   {
269     $tmp=array();
270     foreach($this->attributes as $attrs){ 
271       $tmp[$attrs] = $this->$attrs;
272     }
274     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
275       $tmp['remove']['from']  = $this->orig_cn;
276       $tmp['remove']['to']    = $tmp['cn'];
277     }
278   
279     $tmp['dn']      = $this->dn;  
280     $tmp['status']  = $this->status;  
282     return($tmp);
283   }
285   
286   /* Return plugin informations for acl handling */
287   static function plInfo()
288   {
289     return (array(
290           "plShortName" => _("Template entry"),
291           "plDescription" => _("FAI template entry"),
292           "plSelfModify"  => FALSE,
293           "plDepends"     => array(),
294           "plPriority"    => 25,
295           "plSection"     => array("administration"),
296           "plCategory"    => array("fai"),
297           "plProvidedAcls" => array(
298             "cn"                => _("Name"),
299             "description"       => _("Description"),
300             "FAItemplateFile"   => _("Template file"),
301             "FAItemplatePath"   => _("Template path"),
302             "FAIowner"          => _("File owner"),
303             "FAImode"           => _("File permissions"))
304           ));
305   }
308 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
309 ?>