1 <?php
3 class hotplugDialog 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("HOT_name","HOT_description","HOT_id","HOT_produkt","HOT_vendor");
13 var $objectclasses = array("whatever");
14 var $use_existing = false;
16 var $HOT_name = "";
17 var $HOT_description = "";
18 var $HOT_id = "";
19 var $HOT_produkt = "";
20 var $HOT_vendor = "";
22 var $hotplugDevices = "";
23 var $hotplugDeviceList = array();
24 var $regex = "*";
25 var $depselect = "/";
26 var $deplist = "/";
27 var $skipThese = array();
29 function hotplugDialog ($config, $dn= NULL,$use_existing=false ,$skipThese = array())
30 {
31 $this->use_existing = $use_existing;
32 $this->skipThese = $skipThese;
33 plugin::plugin ($config, $dn);
34 $this->depselect = $this->config->current['BASE'];
35 }
37 function execute()
38 {
39 /* Call parent execute */
40 plugin::execute();
42 /* Fill templating stuff */
43 $smarty= get_smarty();
44 $display= "";
46 foreach($this->attributes as $s_attr){
47 $smarty->assign($s_attr,$this->$s_attr);
48 }
50 if($this->use_existing){
52 if(isset($_POST['dialogissubmitted'])){
53 $this->regex=$_POST['regexHot'];
54 $this->depselect = $_POST['depselectHot'];
55 }
57 if((isset($_GET['search']))&&(!empty($_GET['search']))){
58 $this->regex=$_GET['search']."*";
59 $this->regex=preg_replace("/\*\*/","*",$this->regex);
60 }
62 $this->deplist=$this->config->idepartments;
64 $this->hotplugDevices = $this->getHotplugs();
65 $smarty->assign("regexHot" ,$this->regex);
66 $smarty->assign("deplistHot" ,$this->deplist);
67 $smarty->assign("depselectHot",$this->depselect);
68 $smarty->assign("apply", apply_filter());
69 $smarty->assign("alphabet", generate_alphabet());
70 $smarty->assign("search_image", get_template_path('images/search.png'));
71 $smarty->assign("tree_image", get_template_path('images/tree.png'));
72 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
73 $smarty->assign("deplist", $this->config->idepartments);
76 $smarty->assign("hotplugDevices",$this->hotplugDevices);
77 $smarty->assign("hotplugDeviceKeys",array_flip($this->hotplugDevices));
78 $display.= $smarty->fetch(get_template_path('hotplugDialog.tpl', TRUE,dirname(__FILE__)));
79 }else{
80 $display.= $smarty->fetch(get_template_path('hotplugDialogNew.tpl', TRUE,dirname(__FILE__)));
81 }
82 return($display);
83 }
85 function remove_from_parent()
86 {
87 /* This cannot be removed... */
88 }
91 /* Save data to object */
92 function save_object()
93 {
94 if(isset($_POST['dialogissubmitted'])){
95 plugin::save_object();
96 foreach($this->attributes as $s_attr){
97 if(isset($_POST[$s_attr])){
98 if(in_array($s_attr,array("HOT_produkt","HOT_vendor"))){
99 $this->$s_attr = strtolower(stripslashes($_POST[$s_attr]));
100 }else{
101 $this->$s_attr = stripslashes($_POST[$s_attr]);
102 }
103 }else{
104 $this->$s_attr = false;
105 }
106 }
107 }
108 }
111 /* Check supplied data */
112 function check()
113 {
114 /* Call common method to give check the hook */
115 $message= plugin::check();
117 if(!$this->use_existing){
119 if(empty($this->HOT_name)||(preg_match("/[^a-z0-9]/i",$this->HOT_name))){
120 $message[]=_("Please specify a valid name. Only 0-9 a-Z is allowed.");
121 }
122 if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->HOT_description)){
123 $message[]=_("Invalid character in description. Please specify a valid description.");
124 }
126 if((empty($this->HOT_vendor)) || (empty($this->HOT_produkt))){
127 if(empty($this->HOT_id) || preg_match("/[\|\*]/i",$this->HOT_id)){
128 $message[]=_("Please specify a valid iSerial.");
129 }
130 }
131 if(empty($this->HOT_vendor) || !$this->is_2byteHex($this->HOT_vendor)){
132 $message[]=_("Please specify a valid vendor ID. (2 byte hex like '0xFFFF')");
133 }
134 if(empty($this->HOT_produkt) || !$this->is_2byteHex($this->HOT_produkt)){
135 $message[]=_("Please specify a valid product ID. (2 byte hex like '0xFFFF')");
136 }
138 $ldap = $this->config->get_ldap_link();
139 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->HOT_name."*))",array("gotoHotplugDevice"));
140 if($ldap->count()){
141 $message[]=_("An Entry with this name already exists.");
142 }
144 }else{
145 if((!isset($_POST['hotplugName']))||(empty($_POST['hotplugName']))){
146 $message[] = _("Please select an entry or press cancel.");
147 }
148 }
150 return ($message);
151 }
153 function is_2byteHex($str)
154 {
155 return empty($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
156 }
161 /* Save to LDAP */
162 function save()
163 {
164 /* return generated entry from input fields*/
165 if(!$this->use_existing){
166 $a_return=array();
167 $a_return[$this->HOT_name]['name'] = $this->HOT_name;
168 $a_return[$this->HOT_name]['description'] = $this->HOT_description;
169 $a_return[$this->HOT_name]['id'] = $this->HOT_id;
170 $a_return[$this->HOT_name]['produkt'] = $this->HOT_produkt;
171 $a_return[$this->HOT_name]['vendor'] = $this->HOT_vendor;
172 return($a_return);
173 }else{
174 $entries = array();
175 foreach($_POST['hotplugName'] as $name){
176 $entries[$name] = $this->hotplugDeviceList[$name];
177 }
178 return $entries;
179 }
180 }
182 function getHotplugs(){
183 $ldap= $this->config->get_ldap_link();
184 $ldap->cd($this->depselect);
186 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->regex."))",array("gotoHotplugDevice"));
188 $a_return = array();
189 $this->hotplugDeviceList = array();
190 while($attr = $ldap->fetch()){
191 if(isset($attr['gotoHotplugDevice'])){
192 unset($attr['gotoHotplugDevice']['count']);
193 foreach($attr['gotoHotplugDevice'] as $device){
194 $tmp =$tmp2= array();
195 $tmp = split("\|",$device);
197 if(preg_match("/^".str_replace("*","",$this->regex).".*/i",$tmp[0])){
199 if(in_array($tmp[0],$this->skipThese)) continue;
201 $a_return[$tmp[0]]= $tmp[0]." [".$tmp[1]."] ".$tmp[2];
203 $tmp2['name'] = $tmp[0];
204 $tmp2['description'] = $tmp[1];
205 $tmp2['id'] = $tmp[2];
207 /* Produkt ID */
208 if(!isset($tmp[3])){
209 $tmp[3] = "";
210 }
211 /* Vendor ID */
212 if(!isset($tmp[4])){
213 $tmp[4] = "";
214 }
216 $tmp2['produkt'] = $tmp[3];
217 $tmp2['vendor'] = $tmp[4];
219 $this->hotplugDeviceList[$tmp[0]]=$tmp2;
220 }
221 }
222 }
223 }
224 return($a_return);
225 }
227 }
229 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
230 ?>