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( $this->FAItemplatePath )){
76 $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 $smarty->assign("FAItemplateFile","");
131 foreach($this->attributes as $attr){
132 $smarty->assign($attr."ACL",$this->getacl($attr,preg_match("/freeze/",$this->FAIstate)));
133 }
135 /* We now split cn/FAItemplatePath to make things more clear... */
136 $smarty->assign("cn", basename($this->FAItemplatePath));
137 $smarty->assign("templatePath", dirname($this->FAItemplatePath));
138 $smarty->assign("freeze", preg_match("/freeze/i",$this->FAIstate));;
140 $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
141 return($display);
142 }
144 /* Save data to object */
145 function save_object()
146 {
147 /* Check if form is posted and we are not freezed */
148 if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/freeze/", $this->FAIstate)){
150 /* Remember destination current path
151 depending on the ACLs we will assemble a new one later.
152 */
153 $cur_path = $this->FAItemplatePath;
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;
180 }
182 /* Check if we are allowed to change the destination directory
183 */
184 if($this->acl_is_writeable("FAItemplatePath")){
185 $cur_path = $this->FAItemplatePath.'/'.basename($cur_path);
186 }
188 /* Check if we are allowed to change the destination directory
189 */
190 if($this->acl_is_writeable("cn")){
191 $cur_path = dirname($cur_path).'/'.$this->cn;
192 }
193 $this->FAItemplatePath = $cur_path;
194 }
195 }
198 /* Check supplied data */
199 function check()
200 {
201 /* Call common method to give check the hook */
202 $message= plugin::check();
204 if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
205 $message[] = msgPool::duplicated(_("Name"));
206 }
208 if(empty($this->FAItemplateFile)){
209 $message[]= msgPool::required(_("File"));
210 }
212 if(!preg_match('/^\//', $this->FAItemplatePath)){
213 $message[]= msgPool::invalid(_("Destination path"),"","","/path");
214 }
216 $b = trim(basename($this->FAItemplatePath));
217 if($b == ""){
218 $message[] = msgPool::required(_("File name"));
219 }
221 if($this->user == ""){
222 $message[] = msgPool::required(_("User"));
223 }elseif(preg_match("/[^0-9a-z]/i",$this->user)){
224 $message[] = msgPool::invalid(_("User"),$this->user,"/[0-9a-z]/");
225 }
227 if($this->group == ""){
228 $message[] = msgPool::required(_("Group"));
229 }elseif (!tests::is_uid($this->group)){
230 if (strict_uid_mode()){
231 $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/");
232 } else {
233 $message[]= msgPool::invalid(_("Group"), $this->group, "/[a-z0-9_-]/i");
234 }
235 }
237 return ($message);
238 }
240 function save()
241 {
242 $tmp=array();
243 foreach($this->attributes as $attrs){
244 $tmp[$attrs] = $this->$attrs;
245 }
247 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
248 $tmp['remove']['from'] = $this->orig_cn;
249 $tmp['remove']['to'] = $tmp['cn'];
250 }
252 $tmp['dn'] = $this->dn;
253 $tmp['status'] = $this->status;
255 return($tmp);
256 }
259 /* Return plugin informations for acl handling */
260 static function plInfo()
261 {
262 return (array(
263 "plShortName" => _("Template entry"),
264 "plDescription" => _("FAI template entry"),
265 "plSelfModify" => FALSE,
266 "plDepends" => array(),
267 "plPriority" => 25,
268 "plSection" => array("administration"),
269 "plCategory" => array("fai"),
270 "plProvidedAcls" => array(
271 "cn" => _("Name"),
272 "description" => _("Description"),
273 "FAItemplateFile" => _("Template file"),
274 "FAItemplatePath" => _("Template path"),
275 "FAIowner" => _("File owner"),
276 "FAImode" => _("File permissions"))
277 ));
278 }
280 }
281 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
282 ?>