Code

09b5d8dbfb4cb528ea11ee4d4f0882e639643de8
[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      = "";
23   
24   function faiHookEntry (&$config, $dn= NULL,$object=false)
25   {
26     plugin::plugin ($config, NULL);
27     if($dn != "new"){
28       $this->orig_cn= $object['cn'];
29       $this->dn=$object['dn'];
30       foreach($object as $name=>$value){
31         $oname = $name;
32         $this->$oname=$value;
33       }
34     }elseif(is_array($object)){
35       if(count($object)){
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         }
42       }else{
44         $this->status = "new";
45         $this->orig_cn       = false;
46       }
47     }
48   }
51   function execute()
52   {
53     /* Call parent execute */
54     plugin::execute();
56     /* Fill templating stuff */
57     $smarty     = get_smarty();
58     $display = "";
59         
60     if(isset($_POST['ImportUpload'])){
61       if(($_FILES['ImportFile']['error']!=0)){
62         msg_dialog::display(_("Error"), msgPool::incorrectUpload(), ERROR_DIALOG);
63       }else
64       if(($_FILES['ImportFile']['size']==0)){
65         msg_dialog::display(_("Error"), msgPool::incorrectUpload(_("file is empty")), ERROR_DIALOG);
66       }else{
67         $str = utf8_encode(file_get_contents($_FILES['ImportFile']['tmp_name']));
68         $this->FAIscript = addslashes ($str);
69       }
70     }
72     /* File download requested */
73     if(isset($_GET['getFAIHook'])){
74       send_binary_content(stripslashes($this->FAIscript),$this->cn.".FAIhook");
75     }
77     /* Create download button*/
78     if($this->dn != "new" && $this->acl_is_readable("FAIscript")){
79       $smarty->assign("DownMe","<a href='?plug=".$_GET['plug']."&getFAIHook'>
80           <img src='images/save.png' alt='"._("Download")."' title='"._("Download")."' border=0 class='center'>
81         </a>");
82     }else{
83       $smarty->assign("DownMe","");  
84     }
86     $used_tasks = $this->parent->getUsedFAItask($this->cn);
87     $tasks = $this->tasks;
88     foreach($this->tasks as $id => $task){
89       if(in_array($task,$used_tasks)){
90         unset($tasks[$id]);
91       }
92     }
93     $smarty->assign("tasks", $tasks);
95      /* Magic quotes GPC, escapes every ' " \, to solve some security risks
96      * If we post the escaped strings they will be escaped again
97      */
98     foreach($this->attributes as $attrs){
99       if(get_magic_quotes_gpc()){
100         $smarty->assign($attrs,(stripslashes($this->$attrs)));
101       }else{
102         $smarty->assign($attrs,($this->$attrs));
103       }
104     }
106     $tmp = $this->plInfo();
107     foreach($tmp['plProvidedAcls'] as $name => $translated){
108       $acl = $this->getacl($name, preg_match("/freeze/",$this->FAIstate));
109       $smarty->assign($name."ACL",$acl);
110     }
111    
112     if(get_magic_quotes_gpc()){
113       $smarty->assign("FAIscript" , htmlentities(stripslashes($this->FAIscript)));
114     }else{
115       $smarty->assign("FAIscript" , htmlentities($this->FAIscript));
116     }
117     $smarty->assign("freeze" , preg_match("/freeze/",$this->FAIstate));
118     $display.= $smarty->fetch(get_template_path('faiHookEntry.tpl', TRUE));
119     return($display);
120   }
122   /* Save data to object */
123   function save_object()
124   {
125     if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/^freeze/", $this->FAIstate)){
126       foreach($this->attributes as $attrs){
127         if($this->acl_is_writeable($attrs)){
128           if(isset($_POST[$attrs])){
129             $this->$attrs = $_POST[$attrs];
130           }else{
131             $this->$attrs = "";
132           }
133         }
134       }
135     }
136   }
138   /* Check supplied data */
139   function check()
140   {
141     /* Call common method to give check the hook */
142     $message= plugin::check();
144     if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
145       $message[]= msgPool::duplicated(_("Name"));
146     }
148     $c = trim($this->cn);
149     if($c == ""){
150       $message[] = msgPool::required(_("Name"));
151     }
152     if(preg_match("/[^a-z0-9_\-]/i",$c)){
153       $message[] = msgPool::invalid(_("Name"),$c,"/[a-z0-9_\-]/i");
154     }
156     $s = trim($this->FAIscript);
157     if($s == ""){
158       $message[]= msgPool::required(_("Script"));
159     }
161     return ($message);
162   }
163  
164   function save()
165   {
166     $tmp=array();
167     foreach($this->attributes as $attrs){ 
168       $tmp[$attrs] = $this->$attrs;
169     }
171     /* Strip out dos newlines */
172     $tmp['FAIscript']= strtr($this->FAIscript, array("\x0D" => ""));
174     if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
175       $tmp['remove']['from']  = $this->orig_cn;
176       $tmp['remove']['to']    = $tmp['cn'];
177     }
178   
179     $tmp['dn']      = $this->dn;  
180     $tmp['status']  = $this->status;  
181     return($tmp);
182   }
184     /* Return plugin informations for acl handling */
185   static function plInfo()
186   {
187     return (array(
188           "plShortName" => _("Hook entry"),
189           "plDescription" => _("FAI hook entry"),
190           "plSelfModify"  => FALSE,
191           "plDepends"     => array(),
192           "plPriority"    => 21,
193           "plSection"     => array("administration"),
194           "plCategory"    => array("fai"),
195           "plProvidedAcls" => array(
196             "cn"                => _("Name"),
197             "description"       => _("Description"),
198             "FAItask"           => _("Task"),
199             "FAIscript"         => _("FAI script"))
200           ));
201   }
204 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
205 ?>