1 <?php
3 class faiTemplateEntry extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account= TRUE;
12 var $attributes = array("cn","description","FAItemplateFile","FAItemplatePath","FAImode","user","group","binary","FAIowner");
13 var $objectclasses= array();
15 var $orig_cn = "";
17 var $dn = "";
18 var $cn = "";
19 var $FAItemplateFile = "";
20 var $FAItemplatePath = "";
21 var $description = "";
22 var $status = "new";
23 var $FAImode = "0640";
24 var $FAIowner = "root.root";
25 var $user = "root";
26 var $group = "root";
27 var $binary = false;
29 var $FAIstate = "";
31 function faiTemplateEntry ($config, $dn= NULL,$object=false)
32 {
33 plugin::plugin ($config, $dn);
34 if($dn != "new"){
35 $this->orig_cn= $object['cn'];
36 $this->dn=$object['dn'];
37 foreach($object as $name=>$value){
38 $oname = $name;
39 $this->$oname=$value;
40 }
42 if(isset($this->attrs['FAIstate'][0])){
43 $this->FAIstate = $this->attrs['FAIstate'][0];
44 }
46 }else{
47 $this->status = "new";
48 $this->orig_cn = false;
49 }
50 $this->user = explode( '.', $this->FAIowner );
51 $this->group = $this->user[1];
52 $this->user = $this->user[0];
53 $_SESSION['binary'] = $this->FAItemplateFile;
54 $_SESSION['binarytype'] = 'octet-stream';
55 $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
57 $this->FAImode= sprintf("%0.4s", $this->FAImode)." ";
58 }
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'])){
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 }
81 $status= _("no file uploaded yet");
82 if(strlen($this->FAItemplateFile)){
83 $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
84 }
85 $smarty->assign("status",$status);
87 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
88 * If we post the escaped strings they will be escaped again
89 */
90 foreach($this->attributes as $attrs){
91 if(get_magic_quotes_gpc()){
92 $smarty->assign($attrs,stripslashes($this->$attrs));
93 }else{
94 $smarty->assign($attrs,($this->$attrs));
95 }
96 }
98 /* Assign file modes */
99 $tmode= "$this->FAImode ";
100 foreach (array("s", "u", "g", "o") as $type){
101 $current= substr($tmode, 0, 1);
102 $tmode= preg_replace("/^./", "", $tmode);
103 $nr= 1;
104 while ($nr < 5){
105 if ($current & $nr){
106 $smarty->assign($type.$nr, "checked");
107 } else {
108 $smarty->assign($type.$nr, "");
109 }
110 $nr+= $nr;
111 }
112 }
114 $smarty->assign("FAItemplateFile","");
116 foreach($this->attributes as $attr){
117 if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
118 $smarty->assign($attr."ACL"," disabled ");
119 }else{
120 $smarty->assign($attr."ACL"," ");
121 }
122 }
124 $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
125 return($display);
126 }
128 /* Save data to object */
129 function save_object()
130 {
131 if (!isset($_POST['FAItemplatePath'])){
132 return;
133 }
134 if((isset($_POST['SubObjectFormSubmitted'])) && ($this->FAIstate != "freeze")){
135 foreach($this->attributes as $attrs){
136 if($attrs == "FAItemplateFile")
137 continue;
138 if($attrs == "FAIowner") {
139 $this->$attrs = $_POST["user"] . '.' . $_POST["group"];
140 continue;
141 }
142 if(isset($_POST[$attrs])){
143 $this->$attrs = $_POST[$attrs];
144 }else{
145 $this->$attrs = "";
146 }
147 }
148 /* Save mode */
149 $tmode= "";
150 foreach (array("s", "u", "g", "o") as $type){
151 $nr= 1;
152 $dest= 0;
153 while ($nr < 5){
154 if (isset($_POST["$type$nr"])){
155 $dest+= $nr;
156 }
157 $nr+= $nr;
158 }
159 $tmode= $tmode.$dest;
160 }
161 $this->FAImode= $tmode;
162 }
163 }
165 /* Check supplied data */
166 function check()
167 {
168 $message= array();
169 if(empty($this->FAItemplateFile)){
170 $message[]=_("Please specify a value for attribute 'file'.");
171 }
173 if(empty($this->FAItemplatePath)){
174 $message[]=_("Please specify a value for attribute 'path'.");
175 }
177 if(empty($this->cn)){
178 $message[] = _("Please enter a name.");
179 }
181 if(empty($this->user)){
182 $message[] = _("Please enter a user.");
183 }
184 elseif(preg_match("/[^0-9a-z]/i",$this->user)){
185 $message[] = _("Please enter a valid user. Only a-z/0-9 are allowed.");
186 }
188 if(empty($this->group)){
189 $message[] = _("Please enter a group.");
190 }
191 elseif(preg_match("/[^0-9a-z]/i",$this->group)){
192 $message[] = _("Please enter a valid group. Only a-z/0-9 are allowed.");
193 }
195 return ($message);
196 }
198 function save()
199 {
200 $tmp=array();
201 foreach($this->attributes as $attrs){
202 $tmp[$attrs] = $this->$attrs;
203 }
205 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
206 $tmp['remove']['from'] = $this->orig_cn;
207 $tmp['remove']['to'] = $tmp['cn'];
208 }
210 $tmp['dn'] = $this->dn;
211 $tmp['status'] = $this->status;
213 return($tmp);
214 }
215 }
216 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
217 ?>