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 = "";
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 }
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 }
85 /* File download requested */
86 if(isset($_GET['getFAItemplate'])){
87 send_binary_content($this->FAItemplateFile,$this->cn.".FAItemplate");
88 }
90 $status= _("no file uploaded yet");
92 $bStatus = false; // Hide download icon on default
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 foreach($this->attributes as $attr){
130 $smarty->assign($attr."ACL",$this->getacl($attr,preg_match("/freeze/",$this->FAIstate)));
131 }
133 /* We now split cn/FAItemplatePath to make things more clear... */
134 $smarty->assign("templateFile", preg_replace("/^.*\//","",$this->FAItemplatePath));
135 $smarty->assign("templatePath", preg_replace("/[^\/]*$/","",$this->FAItemplatePath));
136 $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));;
138 $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
139 return($display);
140 }
142 /* Save data to object */
143 function save_object()
144 {
145 /* Check if form is posted and we are not freezed */
146 if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
148 /* Remember destination current path
149 depending on the ACLs we will assemble a new one later.
150 */
151 $cur_path = $this->FAItemplatePath;
152 plugin::save_object();
154 /* Set user.group (FAIowner) attribute */
155 if(isset($_POST['group']) && isset($_POST["user"]) && $this->acl_is_writeable("FAIowner")){
156 $this->FAIowner = $_POST["user"].'.'.$_POST["group"];
157 $this->user = $_POST['user'];
158 $this->group= $_POST['group'];
159 }
161 /* Check if permissions have changed */
162 if($this->acl_is_writeable("FAImode")){
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 }
180 /* Check if we are allowed to change the destination directory
181 */
182 if($this->acl_is_writeable("FAItemplatePath")){
183 $cur_path = get_post('templatePath').'/'.basename($cur_path);
184 }
186 /* Check if we are allowed to change the destination directory
187 */
188 if($this->acl_is_writeable("cn")){
189 $cur_path = preg_replace("/[^\/]*$/","",$cur_path).get_post('templateFile');
190 }
191 $cur_path= str_replace("//","/",$cur_path);
192 if(trim($cur_path,"/") == ""){
193 $cur_path= "";
194 }
195 $this->FAItemplatePath = $this->cn= $cur_path;
196 }
197 }
200 /* Check supplied data */
201 function check()
202 {
203 /* Call common method to give check the hook */
204 $message= plugin::check();
206 if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
207 $message[] = msgPool::duplicated(_("Name"));
208 }
210 if(empty($this->FAItemplateFile)){
211 $message[]= msgPool::required(_("File"));
212 }
214 if(!preg_match('/^\//', $this->FAItemplatePath)){
215 $message[]= msgPool::invalid(_("Destination path"),"","","/path");
216 }
218 $b = trim(basename($this->FAItemplatePath));
219 if($b == ""){
220 $message[] = msgPool::required(_("File name"));
221 }
223 if($this->user == ""){
224 $message[] = msgPool::required(_("User"));
225 }elseif(preg_match("/[^0-9a-z]/i",$this->user)){
226 $message[] = msgPool::invalid(_("User"),$this->user,"/[0-9a-z]/");
227 }
229 if($this->group == ""){
230 $message[] = msgPool::required(_("Group"));
231 }elseif (!tests::is_uid($this->group)){
232 if (strict_uid_mode()){
233 $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/");
234 } else {
235 $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/i");
236 }
237 }
239 return ($message);
240 }
242 function save()
243 {
244 $tmp=array();
245 foreach($this->attributes as $attrs){
246 $tmp[$attrs] = $this->$attrs;
247 }
249 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
250 $tmp['remove']['from'] = $this->orig_cn;
251 $tmp['remove']['to'] = $tmp['cn'];
252 }
254 $tmp['dn'] = $this->dn;
255 $tmp['status'] = $this->status;
257 return($tmp);
258 }
261 /* Return plugin informations for acl handling */
262 static function plInfo()
263 {
264 return (array(
265 "plShortName" => _("Template entry"),
266 "plDescription" => _("FAI template entry"),
267 "plSelfModify" => FALSE,
268 "plDepends" => array(),
269 "plPriority" => 25,
270 "plSection" => array("administration"),
271 "plCategory" => array("fai"),
272 "plProvidedAcls" => array(
273 "cn" => _("Name"),
274 "description" => _("Description"),
275 "FAItemplateFile" => _("Template file"),
276 "FAItemplatePath" => _("Template path"),
277 "FAIowner" => _("File owner"),
278 "FAImode" => _("File permissions"))
279 ));
280 }
282 }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>