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 /* File edit requested */
91 if(isset($_GET['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 if(get_magic_quotes_gpc()){
133 $smarty->assign($attrs,stripslashes($this->$attrs));
134 }else{
135 $smarty->assign($attrs,($this->$attrs));
136 }
137 }
139 /* Assign file modes */
140 $tmode= "$this->FAImode ";
141 foreach (array("s", "u", "g", "o") as $type){
142 $current= substr($tmode, 0, 1);
143 $tmode= preg_replace("/^./", "", $tmode);
144 $nr= 1;
145 while ($nr < 5){
146 if ($current & $nr){
147 $smarty->assign($type.$nr, "checked");
148 } else {
149 $smarty->assign($type.$nr, "");
150 }
151 $nr+= $nr;
152 }
153 }
155 foreach($this->attributes as $attr){
156 $smarty->assign($attr."ACL",$this->getacl($attr,preg_match("/freeze/",$this->FAIstate)));
157 }
159 /* We now split cn/FAItemplatePath to make things more clear... */
160 $smarty->assign("templateFile", preg_replace("/^.*\//","",$this->FAItemplatePath));
161 $smarty->assign("templatePath", preg_replace("/[^\/]*$/","",$this->FAItemplatePath));
162 $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));;
164 $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
165 return($display);
166 }
168 /* Save data to object */
169 function save_object()
170 {
171 /* Check if form is posted and we are not freezed */
172 if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
174 /* Remember destination current path
175 depending on the ACLs we will assemble a new one later.
176 */
177 $cur_path = $this->FAItemplatePath;
178 plugin::save_object();
180 /* Set user.group (FAIowner) attribute */
181 if(isset($_POST['group']) && isset($_POST["user"]) && $this->acl_is_writeable("FAIowner")){
182 $this->FAIowner = $_POST["user"].'.'.$_POST["group"];
183 $this->user = $_POST['user'];
184 $this->group= $_POST['group'];
185 }
187 /* Check if permissions have changed */
188 if($this->acl_is_writeable("FAImode")){
190 /* Save mode */
191 $tmode= "";
192 foreach (array("s", "u", "g", "o") as $type){
193 $nr= 1;
194 $dest= 0;
195 while ($nr < 5){
196 if (isset($_POST["$type$nr"])){
197 $dest+= $nr;
198 }
199 $nr+= $nr;
200 }
201 $tmode= $tmode.$dest;
202 }
203 $this->FAImode= $tmode;
204 }
206 /* Check if we are allowed to change the destination directory
207 */
208 if($this->acl_is_writeable("FAItemplatePath")){
209 $cur_path = get_post('templatePath').'/'.basename($cur_path);
210 }
212 /* Check if we are allowed to change the destination directory
213 */
214 if($this->acl_is_writeable("cn")){
215 $cur_path = preg_replace("/[^\/]*$/","",$cur_path).get_post('templateFile');
216 }
217 $cur_path= str_replace("//","/",$cur_path);
218 if(trim($cur_path,"/") == ""){
219 $cur_path= "";
220 }
221 $this->FAItemplatePath = $this->cn= $cur_path;
222 }
223 }
226 /* Check supplied data */
227 function check()
228 {
229 /* Call common method to give check the hook */
230 $message= plugin::check();
232 if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
233 $message[] = msgPool::duplicated(_("Name"));
234 }
236 if(empty($this->FAItemplateFile)){
237 $message[]= msgPool::required(_("File"));
238 }
240 if(!preg_match('/^\//', $this->FAItemplatePath)){
241 $message[]= msgPool::invalid(_("Destination path"),"","","/path");
242 }
244 $b = trim(basename($this->FAItemplatePath));
245 if($b == ""){
246 $message[] = msgPool::required(_("File name"));
247 }
249 if($this->user == ""){
250 $message[] = msgPool::required(_("User"));
251 }elseif(preg_match("/[^0-9a-z]/i",$this->user)){
252 $message[] = msgPool::invalid(_("User"),$this->user,"/[0-9a-z]/");
253 }
255 if($this->group == ""){
256 $message[] = msgPool::required(_("Group"));
257 }elseif (!tests::is_uid($this->group)){
258 if (strict_uid_mode()){
259 $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/");
260 } else {
261 $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/i");
262 }
263 }
265 return ($message);
266 }
268 function save()
269 {
270 $tmp=array();
271 foreach($this->attributes as $attrs){
272 $tmp[$attrs] = $this->$attrs;
273 }
275 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
276 $tmp['remove']['from'] = $this->orig_cn;
277 $tmp['remove']['to'] = $tmp['cn'];
278 }
280 $tmp['dn'] = $this->dn;
281 $tmp['status'] = $this->status;
283 return($tmp);
284 }
287 /* Return plugin informations for acl handling */
288 static function plInfo()
289 {
290 return (array(
291 "plShortName" => _("Template entry"),
292 "plDescription" => _("FAI template entry"),
293 "plSelfModify" => FALSE,
294 "plDepends" => array(),
295 "plPriority" => 25,
296 "plSection" => array("administration"),
297 "plCategory" => array("fai"),
298 "plProvidedAcls" => array(
299 "cn" => _("Name"),
300 "description" => _("Description"),
301 "FAItemplateFile" => _("Template file"),
302 "FAItemplatePath" => _("Template path"),
303 "FAIowner" => _("File owner"),
304 "FAImode" => _("File permissions"))
305 ));
306 }
308 }
309 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
310 ?>