Code

Updated msgPool usage in fai.
[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       }
38       if(isset($this->attrs['FAIstate'][0])){
39         $this->FAIstate = $this->attrs['FAIstate'][0];
40       }
42     }else{
43       $this->status = "new";
44       $this->orig_cn= false;
45     }
47     $this->user = explode( '.', $this->FAIowner );
48     $this->group = $this->user[1];
49     $this->user = $this->user[0];
51     session::set('binary',$this->FAItemplateFile);
52     session::set('binarytype','octet-stream');
53     session::set('binaryfile',basename($this->FAItemplatePath));
55     if(!empty($this->dn) && $this->dn != "new"){
56       $ldap = $this->config->get_ldap_link();
57       session::set('binary',$ldap->get_attribute($this->dn,"FAItemplateFile"));
58       $this->FAItemplateFile  = session::get('binary');
59     }
60     
61     $this->FAImode= sprintf("%0.4s", $this->FAImode)." ";
62   }
65   function execute()
66   {
67     /* Call parent execute */
68     plugin::execute();
70     /* Fill templating stuff */
71     $smarty     = get_smarty();
72     $smarty->assign("rand", rand(0, 10000));
73     $display = "";
75     if(isset($_POST['TmpFileUpload']) && $this->acl_is_writeable("FAItemplateFile")){
76       if($str=file_get_contents($_FILES['FAItemplateFile']['tmp_name'])){
77         $this->FAItemplateFile = $str;
79         /* If we don't have a filename set it from upload filename. */
80         if( 0 == strlen( $this->FAItemplatePath )){
81           $this->FAItemplatePath = $_FILES['FAItemplateFile']['name'];
82         }
84         session::set('binary',$this->FAItemplateFile);
85         session::set('binarytype','octet-stream');
86         session::set('binaryfile',basename($this->FAItemplatePath));
87       }
88     }
89    
90     /* File download requested */
91     if(isset($_GET['getFAItemplate'])){
92       send_binary_content($this->FAItemplateFile,$this->cn.".FAItemplate");
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     /* We now split cn/FAItemplatePath to make things more clear... */
141     $smarty->assign("cn", basename($this->FAItemplatePath));
142     $smarty->assign("templatePath", dirname($this->FAItemplatePath));
144     $display.=  $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
145     return($display);
146   }
148   /* Save data to object */
149   function save_object()
150   {
151     /* Check if form is posted and we are not freezed */
152     if((isset($_POST['SubObjectFormSubmitted'])) && ($this->FAIstate != "freeze")){
154       plugin::save_object();
156       /* Set user.group (FAIowner) attribute */  
157       if(isset($_POST['group']) && isset($_POST["user"]) && $this->acl_is_writeable("FAIowner")){
158         $this->FAIowner = $_POST["user"].'.'.$_POST["group"];
159         $this->user = $_POST['user'];
160         $this->group= $_POST['group'];
161       }
163       /* Check if permissions have changed */
164       if($this->acl_is_writeable("FAImode")){
166         /* Save mode */
167         $tmode= "";
168         foreach (array("s", "u", "g", "o") as $type){
169           $nr= 1;
170           $dest= 0;
171           while ($nr < 5){
172             if (isset($_POST["$type$nr"])){
173               $dest+= $nr;
174             }
175             $nr+= $nr;
176           }
177           $tmode= $tmode.$dest;
178         }
179         $this->FAImode= $tmode;
181         /* Assemble cn/FAItemplatePath */
182         $this->cn= preg_replace('/\/+/', '/', $this->FAItemplatePath.'/'.$this->cn);
183         $this->FAItemplatePath= $this->cn;
184       }
185     }
186   }
189   /* Check supplied data */
190   function check()
191   {
192     /* Call common method to give check the hook */
193     $message= plugin::check();
195     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
196       $message[] = msgPool::duplicated(_("Name"));
197     }
199     if(empty($this->FAItemplateFile)){
200       $message[]= msgPool::required(_("File"));
201     } 
203     if(!preg_match('/^\//', $this->FAItemplatePath)){
204       $message[]= msgPool::invalid(_("Destination path"),$this->FAItemplatePath,'/^\//');
205     } 
206  
207     $b = basename($this->FAItemplatePath); 
208     if(empty($b)){
209       $message[] = msgPool::required(_("File name"));
210     }
212     if(empty($this->user)){
213       $message[] = msgPool::required(_("User"));
214     }
215     elseif(preg_match("/[^0-9a-z]/i",$this->user)){
216       $message[] = msgPool::invalid(_("User"),$this->user,"/[0-9a-z]/");
217     }
219     if(empty($this->group)){
220       $message[] = msgPool::required(_("Group"));
221     }
222     elseif(preg_match("/[^0-9a-z]/i",$this->group)){
223       $message[] = msgPool::invalid(_("Group"),$this->group,"/[0-9a-z]/");
224     }
226     return ($message);
227   }
228  
229   function save()
230   {
231     $tmp=array();
232     foreach($this->attributes as $attrs){ 
233       $tmp[$attrs] = $this->$attrs;
234     }
236     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
237       $tmp['remove']['from']  = $this->orig_cn;
238       $tmp['remove']['to']    = $tmp['cn'];
239     }
240   
241     $tmp['dn']      = $this->dn;  
242     $tmp['status']  = $this->status;  
244     return($tmp);
245   }
247   
248   /* Return plugin informations for acl handling */
249   static function plInfo()
250   {
251     return (array(
252           "plShortName" => _("Template entry"),
253           "plDescription" => _("FAI template entry"),
254           "plSelfModify"  => FALSE,
255           "plDepends"     => array(),
256           "plPriority"    => 25,
257           "plSection"     => array("administration"),
258           "plCategory"    => array("fai"),
259           "plProvidedAcls" => array(
260             "cn"                => _("Name"),
261             "description"       => _("Description"),
262             "FAItemplateFile"   => _("Template file"),
263             "FAItemplatePath"   => _("Template path"),
264             "FAIowner"          => _("File owner"),
265             "FAImode"           => _("File permissions"))
266           ));
267   }
270 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
271 ?>