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);
35 if($dn != "new"){
36 $this->orig_cn= $object['cn'];
37 $this->dn=$object['dn'];
38 foreach($object as $name=>$value){
39 $oname = $name;
40 $this->$oname=$value;
41 }
43 if(isset($this->attrs['FAIstate'][0])){
44 $this->FAIstate = $this->attrs['FAIstate'][0];
45 }
47 }else{
48 $this->status = "new";
49 $this->orig_cn = false;
50 }
52 $this->user = explode( '.', $this->FAIowner );
53 $this->group = $this->user[1];
54 $this->user = $this->user[0];
55 $_SESSION['binary'] = $this->FAItemplateFile;
56 $_SESSION['binarytype'] = 'octet-stream';
57 $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
59 $this->FAImode= sprintf("%0.4s", $this->FAImode)." ";
60 }
63 function execute()
64 {
65 /* Call parent execute */
66 plugin::execute();
68 /* Fill templating stuff */
69 $smarty = get_smarty();
70 $smarty->assign("rand", rand(0, 10000));
71 $display = "";
73 if(isset($_POST['TmpFileUpload'])){
74 if($str=file_get_contents($_FILES['FAItemplateFile']['tmp_name'])){
75 $this->FAItemplateFile = $str;
77 /* If we don't have a filename set it from upload filename. */
78 if( 0 == strlen( $this->FAItemplatePath ) )
79 $this->FAItemplatePath = $_FILES['FAItemplateFile']['name'];
80 }
82 }
84 $status= _("no file uploaded yet");
85 if(strlen($this->FAItemplateFile)){
86 $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
87 }
88 $smarty->assign("status",$status);
90 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
91 * If we post the escaped strings they will be escaped again
92 */
93 foreach($this->attributes as $attrs){
94 if(get_magic_quotes_gpc()){
95 $smarty->assign($attrs,stripslashes($this->$attrs));
96 }else{
97 $smarty->assign($attrs,($this->$attrs));
98 }
99 }
101 /* Assign file modes */
102 $tmode= "$this->FAImode ";
103 foreach (array("s", "u", "g", "o") as $type){
104 $current= substr($tmode, 0, 1);
105 $tmode= preg_replace("/^./", "", $tmode);
106 $nr= 1;
107 while ($nr < 5){
108 if ($current & $nr){
109 $smarty->assign($type.$nr, "checked");
110 } else {
111 $smarty->assign($type.$nr, "");
112 }
113 $nr+= $nr;
114 }
115 }
117 $smarty->assign("FAItemplateFile","");
119 foreach($this->attributes as $attr){
120 if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
121 $smarty->assign($attr."ACL"," disabled ");
122 }else{
123 $smarty->assign($attr."ACL"," ");
124 }
125 }
127 $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
128 return($display);
129 }
131 /* Save data to object */
132 function save_object()
133 {
134 if (!isset($_POST['FAItemplatePath'])){
135 return;
136 }
137 if((isset($_POST['SubObjectFormSubmitted'])) && ($this->FAIstate != "freeze")){
138 foreach($this->attributes as $attrs){
139 if($attrs == "FAItemplateFile")
140 continue;
141 if($attrs == "FAIowner") {
142 $this->$attrs = $_POST["user"] . '.' . $_POST["group"];
143 continue;
144 }
145 if(isset($_POST[$attrs])){
146 $this->$attrs = $_POST[$attrs];
147 }else{
148 $this->$attrs = "";
149 }
150 }
151 /* Save mode */
152 $tmode= "";
153 foreach (array("s", "u", "g", "o") as $type){
154 $nr= 1;
155 $dest= 0;
156 while ($nr < 5){
157 if (isset($_POST["$type$nr"])){
158 $dest+= $nr;
159 }
160 $nr+= $nr;
161 }
162 $tmode= $tmode.$dest;
163 }
164 $this->FAImode= $tmode;
165 }
166 }
168 /* Check supplied data */
169 function check()
170 {
171 /* Call common method to give check the hook */
172 $message= plugin::check();
174 if(empty($this->FAItemplateFile)){
175 $message[]=_("Please specify a value for attribute 'file'.");
176 }
178 if(empty($this->FAItemplatePath)){
179 $message[]=_("Please specify a value for attribute 'path'.");
180 }
182 if(empty($this->cn)){
183 $message[] = _("Please enter a name.");
184 }
186 if(empty($this->user)){
187 $message[] = _("Please enter a user.");
188 }
189 elseif(preg_match("/[^0-9a-z]/i",$this->user)){
190 $message[] = _("Please enter a valid user. Only a-z/0-9 are allowed.");
191 }
193 if(empty($this->group)){
194 $message[] = _("Please enter a group.");
195 }
196 elseif(preg_match("/[^0-9a-z]/i",$this->group)){
197 $message[] = _("Please enter a valid group. Only a-z/0-9 are allowed.");
198 }
200 return ($message);
201 }
203 function save()
204 {
205 $tmp=array();
206 foreach($this->attributes as $attrs){
207 $tmp[$attrs] = $this->$attrs;
208 }
210 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
211 $tmp['remove']['from'] = $this->orig_cn;
212 $tmp['remove']['to'] = $tmp['cn'];
213 }
215 $tmp['dn'] = $this->dn;
216 $tmp['status'] = $this->status;
218 return($tmp);
219 }
220 }
221 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
222 ?>