Code

160047b5dd2efd42b8c1cfaa3f947e1cadae0c1a
[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     /* Fill templating stuff */
56     $smarty     = get_smarty();
57     $smarty->assign("rand", rand(0, 10000));
58     $display = "";
60     if(isset($_POST['TmpFileUpload'])){
61       if($str=file_get_contents($_FILES['FAItemplateFile']['tmp_name'])){
62         $this->FAItemplateFile = $str;
64         /* If we don't have a filename set it from upload filename. */
65         if( 0 == strlen( $this->FAItemplatePath ) )
66           $this->FAItemplatePath = $_FILES['FAItemplateFile']['name'];
67       }
68     
69     }
70     
71     $status= _("no file uploaded yet");
72     if(strlen($this->FAItemplateFile)){
73       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
74     }
75     $smarty->assign("status",$status);
77     /* Magic quotes GPC, escapes every ' " \, to solve some security risks 
78      * If we post the escaped strings they will be escaped again
79      */
80     foreach($this->attributes as $attrs){
81       if(get_magic_quotes_gpc()){
82         $smarty->assign($attrs,stripslashes($this->$attrs));
83       }else{
84         $smarty->assign($attrs,($this->$attrs));
85       }
86     }
88     /* Assign file modes */
89     $tmode= "$this->FAImode ";
90     foreach (array("s", "u", "g", "o") as $type){
91       $current= substr($tmode, 0, 1);
92       $tmode=   preg_replace("/^./", "", $tmode);
93       $nr= 1;
94       while ($nr < 5){
95         if ($current & $nr){
96           $smarty->assign($type.$nr, "checked");
97         } else {
98           $smarty->assign($type.$nr, "");
99         }
100         $nr+= $nr;
101       }
102     }
104     $smarty->assign("FAItemplateFile","");
106     $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
107     return($display);
108   }
110   /* Save data to object */
111   function save_object()
112   {
113     if (!isset($_POST['FAItemplatePath'])){
114       return;
115     }
116     if(isset($_POST['SubObjectFormSubmitted'])){
117       foreach($this->attributes as $attrs){
118         if($attrs == "FAItemplateFile") 
119           continue;
120         if($attrs == "FAIowner") {
121           $this->$attrs = $_POST["user"] . '.' . $_POST["group"];
122           continue;
123         }
124         if(isset($_POST[$attrs])){
125           $this->$attrs = $_POST[$attrs];
126         }else{
127           $this->$attrs = "";
128         }
129       }
130     }
132     /* Save mode */
133     $tmode= "";
134     foreach (array("s", "u", "g", "o") as $type){
135       $nr= 1;
136       $dest= 0;
137       while ($nr < 5){
138         if (isset($_POST["$type$nr"])){
139           $dest+= $nr;
140         }
141         $nr+= $nr;
142       }
143       $tmode= $tmode.$dest;
144     }
145     $this->FAImode= $tmode;
146   }
148   /* Check supplied data */
149   function check()
150   {
151     $message= array();
152     if(empty($this->FAItemplateFile)){
153       $message[]=_("Please specify a value for attribute 'file'.");
154     } 
156     if(empty($this->FAItemplatePath)){
157       $message[]=_("Please specify a value for attribute 'path'.");
158     } 
159   
160     if(empty($this->cn)){
161       $message[] = _("Please enter a name.");
162     }
164     if(empty($this->user)){
165       $message[] = _("Please enter a user.");
166     }
167     elseif(preg_match("/[^0-9a-z]/i",$this->user)){
168       $message[] = _("Please enter a valid user. Only a-Z 0-9 are allowed.");
169     }
171     if(empty($this->group)){
172       $message[] = _("Please enter a group.");
173     }
174     elseif(preg_match("/[^0-9a-z]/i",$this->group)){
175       $message[] = _("Please enter a valid group. Only a-Z 0-9 are allowed.");
176     }
178     return ($message);
179   }
180  
181   function save()
182   {
183     $tmp=array();
184     foreach($this->attributes as $attrs){ 
185       $tmp[$attrs] = $this->$attrs;
186     }
188     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
189       $tmp['remove']['from']  = $this->orig_cn;
190       $tmp['remove']['to']    = $tmp['cn'];
191     }
192   
193     $tmp['dn']      = $this->dn;  
194     $tmp['status']  = $this->status;  
196     return($tmp);
197   }
199 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
200 ?>