Code

this should do it.
[gosa.git] / plugins / admin / fai / class_faiTemplateEntry.inc
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;
28   
29   function faiTemplateEntry ($config, $dn= NULL,$object=false)
30   {
31     plugin::plugin ($config, $dn);
32     if($dn != "new"){
33       $this->orig_cn= $object['cn'];
34       $this->dn=$object['dn'];
35       foreach($object as $name=>$value){
36         $oname = $name;
37         $this->$oname=$value;
38       }
39     }else{
40       $this->status = "new";
41       $this->orig_cn       = false;
42     }
43     $this->user = explode( '.', $this->FAIowner );
44     $this->group = $this->user[1];
45     $this->user = $this->user[0];
46     $_SESSION['binary'] = $this->FAItemplateFile;
47     $_SESSION['binarytype'] = 'octet-stream';
48     $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
49     
50     $this->FAImode= sprintf("%0.4s", $this->FAImode)." ";
51   }
53   function execute()
54   {
55         /* Call parent execute */
56         plugin::execute();
58     /* Fill templating stuff */
59     $smarty     = get_smarty();
60     $smarty->assign("rand", rand(0, 10000));
61     $display = "";
63     if(isset($_POST['TmpFileUpload'])){
64       if($str=file_get_contents($_FILES['FAItemplateFile']['tmp_name'])){
65         $this->FAItemplateFile = $str;
67         /* If we don't have a filename set it from upload filename. */
68         if( 0 == strlen( $this->FAItemplatePath ) )
69           $this->FAItemplatePath = $_FILES['FAItemplateFile']['name'];
70       }
71     
72     }
73     
74     $status= _("no file uploaded yet");
75     if(strlen($this->FAItemplateFile)){
76       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
77     }
78     $smarty->assign("status",$status);
80     /* Magic quotes GPC, escapes every ' " \, to solve some security risks 
81      * If we post the escaped strings they will be escaped again
82      */
83     foreach($this->attributes as $attrs){
84       if(get_magic_quotes_gpc()){
85         $smarty->assign($attrs,stripslashes($this->$attrs));
86       }else{
87         $smarty->assign($attrs,($this->$attrs));
88       }
89     }
91     /* Assign file modes */
92     $tmode= "$this->FAImode ";
93     foreach (array("s", "u", "g", "o") as $type){
94       $current= substr($tmode, 0, 1);
95       $tmode=   preg_replace("/^./", "", $tmode);
96       $nr= 1;
97       while ($nr < 5){
98         if ($current & $nr){
99           $smarty->assign($type.$nr, "checked");
100         } else {
101           $smarty->assign($type.$nr, "");
102         }
103         $nr+= $nr;
104       }
105     }
107     $smarty->assign("FAItemplateFile","");
109     $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
110     return($display);
111   }
113   /* Save data to object */
114   function save_object()
115   {
116     if (!isset($_POST['FAItemplatePath'])){
117       return;
118     }
119     if(isset($_POST['SubObjectFormSubmitted'])){
120       foreach($this->attributes as $attrs){
121         if($attrs == "FAItemplateFile") 
122           continue;
123         if($attrs == "FAIowner") {
124           $this->$attrs = $_POST["user"] . '.' . $_POST["group"];
125           continue;
126         }
127         if(isset($_POST[$attrs])){
128           $this->$attrs = $_POST[$attrs];
129         }else{
130           $this->$attrs = "";
131         }
132       }
133     }
135     /* Save mode */
136     $tmode= "";
137     foreach (array("s", "u", "g", "o") as $type){
138       $nr= 1;
139       $dest= 0;
140       while ($nr < 5){
141         if (isset($_POST["$type$nr"])){
142           $dest+= $nr;
143         }
144         $nr+= $nr;
145       }
146       $tmode= $tmode.$dest;
147     }
148     $this->FAImode= $tmode;
149   }
151   /* Check supplied data */
152   function check()
153   {
154     $message= array();
155     if(empty($this->FAItemplateFile)){
156       $message[]=_("Please specify a value for attribute 'file'.");
157     } 
159     if(empty($this->FAItemplatePath)){
160       $message[]=_("Please specify a value for attribute 'path'.");
161     } 
162   
163     if(empty($this->cn)){
164       $message[] = _("Please enter a name.");
165     }
167     if(empty($this->user)){
168       $message[] = _("Please enter a user.");
169     }
170     elseif(preg_match("/[^0-9a-z]/i",$this->user)){
171       $message[] = _("Please enter a valid user. Only a-z/0-9 are allowed.");
172     }
174     if(empty($this->group)){
175       $message[] = _("Please enter a group.");
176     }
177     elseif(preg_match("/[^0-9a-z]/i",$this->group)){
178       $message[] = _("Please enter a valid group. Only a-z/0-9 are allowed.");
179     }
181     return ($message);
182   }
183  
184   function save()
185   {
186     $tmp=array();
187     foreach($this->attributes as $attrs){ 
188       $tmp[$attrs] = $this->$attrs;
189     }
191     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
192       $tmp['remove']['from']  = $this->orig_cn;
193       $tmp['remove']['to']    = $tmp['cn'];
194     }
195   
196     $tmp['dn']      = $this->dn;  
197     $tmp['status']  = $this->status;  
199     return($tmp);
200   }
202 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
203 ?>