Code

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