Code

Backport from trunk
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiHookEntry.inc
1 <?php
3 class faiHookEntry extends plugin
4 {
6   /* attribute list for save action */
7   var $ignore_account= TRUE;
8   var $attributes   = array("cn","description","FAIscript","FAItask");
9   var $objectclasses= array();
11   var $orig_cn              = "";
12   var $tasks                = array("chboot", "configure", "debconf", "extrbase", "faiend", "finish",
13                                     "install", "instsoft", "mirror", "mountdisks", "partition", "prepareapt",
14                                     "savelog", "softupdate", "sysinfo","updatebase", "error");
15   var $dn            = "";
16   var $cn            = "";
17   var $FAItask       = "chboot";
18   var $FAIscript     = "";
19   var $description   = "";
20   var $status        = "new";
21   var $parent        = NULL;
22   var $FAIstate      = "";
24   // Encoding validation
25   var $enc_before_edit = "";
26   var $enc_after_edit = "";
27   var $write_protect = FALSE;
29   
30   function faiHookEntry (&$config, $dn= NULL,$object=false)
31   {
32     plugin::plugin ($config, NULL);
33     if($dn != "new"){
34       $this->orig_cn= $object['cn'];
35       $this->dn=$object['dn'];
36       foreach($object as $name=>$value){
37         $oname = $name;
38         $this->$oname=$value;
39       }
40     }elseif(is_array($object)){
41       if(count($object)){
42         $this->orig_cn= $object['cn'];
43         $this->dn=$object['dn'];
44         foreach($object as $name=>$value){
45           $oname = $name;
46           $this->$oname=$value;
47         }
48       }else{
50         $this->status = "new";
51         $this->orig_cn       = false;
52       }
53     }
55     // Keep an eye on dangerous encodings, we may break scripts while editing.
56     $this->mb_extension = function_exists("mb_detect_encoding");
57     if($this->mb_extension){
58         $this->enc_before_edit = mb_detect_encoding($this->FAIscript);
59         if($this->enc_before_edit != "ASCII"){
60             $this->write_protect = TRUE;
61         }
62     }
63   }
66   function execute()
67   {
68     /* Call parent execute */
69     plugin::execute();
71     /* Fill templating stuff */
72     $smarty     = get_smarty();
73     $display = "";
74         
75     if(isset($_POST['ImportUpload'])){
76       if(($_FILES['ImportFile']['error']!=0)){
77         msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
78       }else
79       if(($_FILES['ImportFile']['size']==0)){
80         msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG);
81       }else{
82         $str = file_get_contents(gosa_file_name($_FILES['ImportFile']['tmp_name']));
83         $this->FAIscript = $str;
85         // Check encoding again
86         if($this->mb_extension){
87             $this->enc_before_edit = mb_detect_encoding($this->FAIscript);
88             if($this->enc_before_edit != "ASCII"){
89                 $this->write_protect = TRUE;
90             }
91         }
92       }
93     }
95     // Assign encoding related variables.
96     $smarty->assign("write_protect",$this->write_protect);
99     /* File download requested */
100     if(isset($_POST['download'])){
101       send_binary_content($this->FAIscript,$this->cn.".FAIhook");
102     }
104     /* Create download button*/
105     if($this->dn != "new" && $this->acl_is_readable("FAIscript")){
106       $smarty->assign("DownMe", image('images/save.png', "download"));          
107     }else{
108       $smarty->assign("DownMe","");  
109     }
111     $used_tasks = $this->parent->getUsedFAItask($this->cn);
112     $tasks = $this->tasks;
113     foreach($this->tasks as $id => $task){
114       if(in_array_strict($task,$used_tasks)){
115         unset($tasks[$id]);
116       }
117     }
118     $smarty->assign("tasks", $tasks);
120      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
121      * If we post the escaped strings they will be escaped again
122      */
123     foreach($this->attributes as $attrs){
124         $smarty->assign($attrs,set_post($this->$attrs));
125     }
127     $tmp = $this->plInfo();
128     foreach($tmp['plProvidedAcls'] as $name => $translated){
129       $acl = $this->getacl($name, preg_match("/freeze/",$this->FAIstate));
130       $smarty->assign($name."ACL",$acl);
131     }
132    
133     $smarty->assign("FAIscript" , set_post($this->FAIscript));
134     $smarty->assign("freeze" , preg_match("/freeze/",$this->FAIstate));
135     $display.= $smarty->fetch(get_template_path('faiHookEntry.tpl', TRUE));
136     return($display);
137   }
139   /* Save data to object */
140   function save_object()
141   {
142     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/^freeze/", $this->FAIstate)){
143       foreach($this->attributes as $attrs){
145         // Do not update the hook content, here, we'll do this a few lines below.
146         if($attrs == 'FAIscript') continue;
148         if($this->acl_is_writeable($attrs)){
149           if(isset($_POST[$attrs])){
150             $this->$attrs = get_post($attrs);
151           }else{
152             $this->$attrs = "";
153           }
154         }
155       }
157       // Get FAIscript changes
158       if(isset($_POST['FAIscript']) &&
159               $this->acl_is_writeable('FAIscript') &&
160               !$this->write_protect){
162           // Check encoding again.
163           $this->FAIscript = get_post('FAIscript');
164           $this->enc_after_edit = mb_detect_encoding($this->FAIscript);
165       }
166     }
167     if(isset($_POST['editAnyway'])) $this->write_protect = FALSE;
169   }
171   /* Check supplied data */
172   function check()
173   {
174     /* Call common method to give check the hook */
175     $message= plugin::check();
177     if($this->mb_extension && !$this->write_protect && $this->enc_after_edit !== $this->enc_before_edit ){
178         $msg = sprintf(_("The script encodig has changed from '%s' to '%s'. Do you really want to save?"),
179                 "<i>".$this->enc_before_edit."</i>","<i>".$this->enc_after_edit."</i>");
180         $message[] = $msg;
181         $this->enc_before_edit = $this->enc_after_edit;
182     }
184     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn && 
185         $this->parent->SubObjects[$this->cn]['status'] != 'delete'){
186       $message[]= msgPool::duplicated(_("Name"));
187     }
189     $c = trim($this->cn);
190     if($c == ""){
191       $message[] = msgPool::required(_("Name"));
192     }
193     if(preg_match("/[^a-z0-9_\-]/i",$c)){
194       $message[] = msgPool::invalid(_("Name"),$c,"/[a-z0-9_\-]/i");
195     }
197     $s = trim($this->FAIscript);
198     if($s == ""){
199       $message[]= msgPool::required(_("Script"));
200     }
202     return ($message);
203   }
204  
205   function save()
206   {
207     $tmp=array();
208     foreach($this->attributes as $attrs){ 
209       $tmp[$attrs] = $this->$attrs;
210     }
212     /* Strip out dos newlines */
213     $tmp['FAIscript']= strtr($this->FAIscript, array("\x0D" => ""));
215     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
216       $tmp['remove']['from']  = $this->orig_cn;
217       $tmp['remove']['to']    = $tmp['cn'];
218     }
219   
220     $tmp['dn']      = $this->dn;  
221     $tmp['status']  = $this->status;  
222     return($tmp);
223   }
225     /* Return plugin informations for acl handling */
226   static function plInfo()
227   {
228     return (array(
229           "plShortName" => _("Hook entry"),
230           "plDescription" => _("FAI hook entry"),
231           "plSelfModify"  => FALSE,
232           "plDepends"     => array(),
233           "plPriority"    => 21,
234           "plSection"     => array("administration"),
235           "plCategory"    => array("fai"),
236           "plProvidedAcls" => array(
237             "cn"                => _("Name"),
238             "description"       => _("Description"),
239             "FAItask"           => _("Task"),
240             "FAIscript"         => _("FAI script"))
241           ));
242   }
245 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
246 ?>