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 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 = "";
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($_POST['download'])){
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", image('images/save.png', "download"));
80 }else{
81 $smarty->assign("DownMe","");
82 }
84 $used_tasks = $this->parent->getUsedFAItask($this->cn);
85 $tasks = $this->tasks;
86 foreach($this->tasks as $id => $task){
87 if(in_array($task,$used_tasks)){
88 unset($tasks[$id]);
89 }
90 }
91 $smarty->assign("tasks", $tasks);
93 /* Magic quotes GPC, escapes every ' " \, to solve some security risks
94 * If we post the escaped strings they will be escaped again
95 */
96 foreach($this->attributes as $attrs){
97 if(get_magic_quotes_gpc()){
98 $smarty->assign($attrs,(stripslashes($this->$attrs)));
99 }else{
100 $smarty->assign($attrs,($this->$attrs));
101 }
102 }
104 $tmp = $this->plInfo();
105 foreach($tmp['plProvidedAcls'] as $name => $translated){
106 $acl = $this->getacl($name, preg_match("/freeze/",$this->FAIstate));
107 $smarty->assign($name."ACL",$acl);
108 }
110 if(get_magic_quotes_gpc()){
111 $smarty->assign("FAIscript" , htmlentities(stripslashes($this->FAIscript)));
112 }else{
113 $smarty->assign("FAIscript" , htmlentities($this->FAIscript));
114 }
115 $smarty->assign("freeze" , preg_match("/freeze/",$this->FAIstate));
116 $display.= $smarty->fetch(get_template_path('faiHookEntry.tpl', TRUE));
117 return($display);
118 }
120 /* Save data to object */
121 function save_object()
122 {
123 if((isset($_POST['SubObjectFormSubmitted'])) && !preg_match("/^freeze/", $this->FAIstate)){
124 foreach($this->attributes as $attrs){
125 if($this->acl_is_writeable($attrs)){
126 if(isset($_POST[$attrs])){
127 $this->$attrs = $_POST[$attrs];
128 }else{
129 $this->$attrs = "";
130 }
131 }
132 }
133 }
134 }
136 /* Check supplied data */
137 function check()
138 {
139 /* Call common method to give check the hook */
140 $message= plugin::check();
142 if(isset($this->parent->SubObjects[$this->cn]) && $this->cn != $this->orig_cn){
143 $message[]= msgPool::duplicated(_("Name"));
144 }
146 $c = trim($this->cn);
147 if($c == ""){
148 $message[] = msgPool::required(_("Name"));
149 }
150 if(preg_match("/[^a-z0-9_\-]/i",$c)){
151 $message[] = msgPool::invalid(_("Name"),$c,"/[a-z0-9_\-]/i");
152 }
154 $s = trim($this->FAIscript);
155 if($s == ""){
156 $message[]= msgPool::required(_("Script"));
157 }
159 return ($message);
160 }
162 function save()
163 {
164 $tmp=array();
165 foreach($this->attributes as $attrs){
166 $tmp[$attrs] = $this->$attrs;
167 }
169 /* Strip out dos newlines */
170 $tmp['FAIscript']= strtr($this->FAIscript, array("\x0D" => ""));
172 if(($this->orig_cn)&&($tmp['cn']!=$this->orig_cn)){
173 $tmp['remove']['from'] = $this->orig_cn;
174 $tmp['remove']['to'] = $tmp['cn'];
175 }
177 $tmp['dn'] = $this->dn;
178 $tmp['status'] = $this->status;
179 return($tmp);
180 }
182 /* Return plugin informations for acl handling */
183 static function plInfo()
184 {
185 return (array(
186 "plShortName" => _("Hook entry"),
187 "plDescription" => _("FAI hook entry"),
188 "plSelfModify" => FALSE,
189 "plDepends" => array(),
190 "plPriority" => 21,
191 "plSection" => array("administration"),
192 "plCategory" => array("fai"),
193 "plProvidedAcls" => array(
194 "cn" => _("Name"),
195 "description" => _("Description"),
196 "FAItask" => _("Task"),
197 "FAIscript" => _("FAI script"))
198 ));
199 }
201 }
202 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
203 ?>