Code

44e5e437c44be3fc95e5a43bb049bf387e409a6a
[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("Object_cn","Object_description","Object_FAItemplateFile","Object_FAItemplatePath","Object_FAImode","Object_user","Object_group","Object_binary","Object_FAIowner");
13   var $objectclasses= array();
15   var $orig_cn              = "";
17   var $Object_dn            = "";
18   var $Object_cn            = "";
19   var $Object_FAItemplateFile   = "";
20   var $Object_FAItemplatePath   = "";
21   var $Object_description   = "";
22   var $Object_status        = "new";
23   var $Object_FAImode       = "0640";
24   var $Object_FAIowner      = "root.root";
25   var $Object_user          = "root";
26   var $Object_group         = "root";
27   var $Object_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 = "Object_".$name;
37         $this->$oname=$value;
38       }
39     }else{
40       $this->Object_status = "new";
41       $this->orig_cn       = false;
42     }
43     $this->Object_user = explode( '.', $this->Object_FAIowner );
44     $this->Object_group = $this->Object_user[1];
45     $this->Object_user = $this->Object_user[0];
46     $_SESSION['binary'] = $this->Object_FAItemplateFile;
47     $_SESSION['binarytype'] = 'octet-stream';
48     $_SESSION['binaryfile'] = basename( $this->Object_FAItemplatePath );
49     
50     $this->Object_FAImode= sprintf("%0.4s", $this->Object_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->Object_FAItemplateFile = $str;
64         /* If we don't have a filename set it from upload filename. */
65         if( 0 == strlen( $this->Object_FAItemplatePath ) )
66           $this->Object_FAItemplatePath = $_FILES['FAItemplateFile']['name'];
67       }
68     
69     }
70     
71     $status= _("no file uploaded yet");
72     if(strlen($this->Object_FAItemplateFile)){
73       $status= sprintf(_("exists in database (size: %s bytes)"),strlen($this->Object_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->Object_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("Object_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['Object_FAItemplatePath'])){
114       return;
115     }
116     if(isset($_POST['SubObjectFormSubmitted'])){
117       foreach($this->attributes as $attrs){
118         if($attrs == "Object_FAItemplateFile") 
119           continue;
120         if($attrs == "Object_FAIowner") {
121           $this->$attrs = $_POST["Object_user"] . '.' . $_POST["Object_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->Object_FAImode= $tmode;
146   }
148   /* Check supplied data */
149   function check()
150   {
151     $message= array();
152     if(empty($this->Object_FAItemplateFile)){
153       $message[]=_("Please specify a value for attribute 'file'.");
154     } 
156     if(empty($this->Object_FAItemplatePath)){
157       $message[]=_("Please specify a value for attribute 'path'.");
158     } 
159   
160     if(empty($this->Object_cn)){
161       $message[] = _("Please enter a name.");
162     }
163     elseif(preg_match("/[^0-9a-z]/i",$this->Object_cn)){
164       $message[] = _("Please enter a valid name. Only a-Z 0-9 are allowed.");
165     }
167     if(empty($this->Object_user)){
168       $message[] = _("Please enter a user.");
169     }
170     elseif(preg_match("/[^0-9a-z]/i",$this->Object_user)){
171       $message[] = _("Please enter a valid user. Only a-Z 0-9 are allowed.");
172     }
174     if(empty($this->Object_group)){
175       $message[] = _("Please enter a group.");
176     }
177     elseif(preg_match("/[^0-9a-z]/i",$this->Object_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       $attr = preg_replace("/^Object_/","",$attrs);
189       $tmp[$attr] = $this->$attrs;
190     }
192     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
193       $tmp['remove']['from']  = $this->orig_cn;
194       $tmp['remove']['to']    = $tmp['cn'];
195     }
196   
197     $tmp['dn']      = $this->dn;  
198     $tmp['status']  = $this->Object_status;  
200     return($tmp);
201   }
203 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
204 ?>