Code

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