Code

29267dd9245ec8069d73c9b48b6273c4a15cf4a2
[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;
29   var $FAIstate      = "";
30   
31   function faiTemplateEntry ($config, $dn= NULL,$object=false)
32   {
33     plugin::plugin ($config, $dn);
35     if((isset($object['cn'])) && (!empty($object['cn']))){
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'] = stripslashes($this->FAItemplateFile);
56     $_SESSION['binarytype'] = 'octet-stream';
57     $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
58     
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 = addslashes($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         $_SESSION['binary']     = stripslashes($this->FAItemplateFile);
83         $_SESSION['binarytype'] = 'octet-stream';
84         $_SESSION['binaryfile'] = basename( $this->FAItemplatePath );
85       }
86     }
87     
88     $status= _("no file uploaded yet");
90     $bStatus = false; // Hide download icon on default 
91     
92     if(strlen($this->FAItemplateFile)){
93       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->FAItemplateFile));
94       $bStatus = true;  // Display download icon 
95     }
96     $smarty->assign("status",$status);
97     $smarty->assign("bStatus",$bStatus);
99     /* Magic quotes GPC, escapes every ' " \, to solve some security risks 
100      * If we post the escaped strings they will be escaped again
101      */
102     foreach($this->attributes as $attrs){
103       if(get_magic_quotes_gpc()){
104         $smarty->assign($attrs,stripslashes($this->$attrs));
105       }else{
106         $smarty->assign($attrs,($this->$attrs));
107       }
108     }
110     /* Assign file modes */
111     $tmode= "$this->FAImode ";
112     foreach (array("s", "u", "g", "o") as $type){
113       $current= substr($tmode, 0, 1);
114       $tmode=   preg_replace("/^./", "", $tmode);
115       $nr= 1;
116       while ($nr < 5){
117         if ($current & $nr){
118           $smarty->assign($type.$nr, "checked");
119         } else {
120           $smarty->assign($type.$nr, "");
121         }
122         $nr+= $nr;
123       }
124     }
126     $smarty->assign("FAItemplateFile","");
128     foreach($this->attributes as $attr){
129       if(($this->FAIstate == "freeze") || (chkacl($this->acl,$attr)!= "")){
130         $smarty->assign($attr."ACL"," disabled ");
131       }else{
132         $smarty->assign($attr."ACL","  ");
133       }
134     }
136     $display.= $smarty->fetch(get_template_path('faiTemplateEntry.tpl', TRUE));
137     return($display);
138   }
140   /* Save data to object */
141   function save_object()
142   {
143     if (!isset($_POST['FAItemplatePath'])){
144       return;
145     }
146     if((isset($_POST['SubObjectFormSubmitted'])) && ($this->FAIstate != "freeze")){
147       foreach($this->attributes as $attrs){
148         if($attrs == "FAItemplateFile") 
149           continue;
150         if($attrs == "FAIowner") {
151           $this->$attrs = $_POST["user"] . '.' . $_POST["group"];
152           continue;
153         }
154         if(isset($_POST[$attrs])){
155           $this->$attrs = $_POST[$attrs];
156         }else{
157           $this->$attrs = "";
158         }
159       }
160       /* Save mode */
161       $tmode= "";
162       foreach (array("s", "u", "g", "o") as $type){
163         $nr= 1;
164         $dest= 0;
165         while ($nr < 5){
166           if (isset($_POST["$type$nr"])){
167             $dest+= $nr;
168           }
169           $nr+= $nr;
170         }
171         $tmode= $tmode.$dest;
172       }
173       $this->FAImode= $tmode;
174     }
175   }
177   /* Check supplied data */
178   function check()
179   {
180     /* Call common method to give check the hook */
181     $message= plugin::check();
183     if(empty($this->FAItemplateFile)){
184       $message[]=_("Please specify a value for attribute 'file'.");
185     } 
187     if(empty($this->FAItemplatePath)){
188       $message[]=_("Please specify a value for attribute 'path'.");
189     } 
190   
191     if(empty($this->cn)){
192       $message[] = _("Please enter a name.");
193     }
195     if(empty($this->user)){
196       $message[] = _("Please enter a user.");
197     }
198     elseif(preg_match("/[^0-9a-z]/i",$this->user)){
199       $message[] = _("Please enter a valid user. Only a-z/0-9 are allowed.");
200     }
202     if(empty($this->group)){
203       $message[] = _("Please enter a group.");
204     }
205     elseif(preg_match("/[^0-9a-z]/i",$this->group)){
206       $message[] = _("Please enter a valid group. Only a-z/0-9 are allowed.");
207     }
209     return ($message);
210   }
211  
212   function save()
213   {
214     $tmp=array();
215     foreach($this->attributes as $attrs){ 
216       $tmp[$attrs] = $this->$attrs;
217     }
219     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
220       $tmp['remove']['from']  = $this->orig_cn;
221       $tmp['remove']['to']    = $tmp['cn'];
222     }
223   
224     $tmp['dn']      = $this->dn;  
225     $tmp['status']  = $this->status;  
227     return($tmp);
228   }
230 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
231 ?>