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 }
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 }
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 }
90 /* File download requested */
91 if(isset($_GET['getFAItemplate'])){
92 send_binary_content($this->FAItemplateFile,$this->cn.".FAItemplate");
93 }
95 $status= _("no file uploaded yet");
97 $bStatus = false; // Hide download icon on default
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[] =_("There is already a template with the given name.");
197 }
199 if(empty($this->FAItemplateFile)){
200 $message[]=_("Please specify a value for attribute 'file'.");
201 }
203 if(!preg_match('/^\//', $this->FAItemplatePath)){
204 $message[]=_("Please specify a valid value for attribute 'Destination path'.");
205 }
207 if(empty($this->cn)){
208 $message[] = _("Please enter a file name.");
209 }
211 # echo $this->cn;
212 # if(preg_match('/\//', $this->cn)){
213 # $message[] = _("The file name should not contain / characters.");
214 # # }
216 if(empty($this->user)){
217 $message[] = _("Please enter a user.");
218 }
219 elseif(preg_match("/[^0-9a-z]/i",$this->user)){
220 $message[] = _("Please enter a valid user. Only a-z/0-9 are allowed.");
221 }
223 if(empty($this->group)){
224 $message[] = _("Please enter a group.");
225 }
226 elseif(preg_match("/[^0-9a-z]/i",$this->group)){
227 $message[] = _("Please enter a valid group. Only a-z/0-9 are allowed.");
228 }
230 return ($message);
231 }
233 function save()
234 {
235 $tmp=array();
236 foreach($this->attributes as $attrs){
237 $tmp[$attrs] = $this->$attrs;
238 }
240 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
241 $tmp['remove']['from'] = $this->orig_cn;
242 $tmp['remove']['to'] = $tmp['cn'];
243 }
245 $tmp['dn'] = $this->dn;
246 $tmp['status'] = $this->status;
248 return($tmp);
249 }
252 /* Return plugin informations for acl handling */
253 static function plInfo()
254 {
255 return (array(
256 "plShortName" => _("Template entry"),
257 "plDescription" => _("FAI template entry"),
258 "plSelfModify" => FALSE,
259 "plDepends" => array(),
260 "plPriority" => 25,
261 "plSection" => array("administration"),
262 "plCategory" => array("fai"),
263 "plProvidedAcls" => array(
264 "cn" => _("Name"),
265 "description" => _("Description"),
266 "FAItemplateFile" => _("Template file"),
267 "FAItemplatePath" => _("Template path"),
268 "FAIowner" => _("File owner"),
269 "FAImode" => _("File permissions"))
270 ));
271 }
273 }
274 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
275 ?>