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_id) || preg_match("/[\|]/i",$this->HOT_id)){
127 $message[]=_("Please specify a valid id.");
128 }
129 if(empty($this->HOT_vendor) || !$this->is_2byteHex($this->HOT_vendor)){
130 $message[]=_("Please specify a valid vendor id. (2 byte hex like '0xFFFF')");
131 }
132 if(empty($this->HOT_produkt) || !$this->is_2byteHex($this->HOT_produkt)){
133 $message[]=_("Please specify a valid product id. (2 byte hex like '0xFFFF')");
134 }
136 $ldap = $this->config->get_ldap_link();
137 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->HOT_name."*))",array("gotoHotplugDevice"));
138 if($ldap->count()){
139 $message[]=_("An Entry with this name already exists.");
140 }
142 }else{
143 if((!isset($_POST['hotplugName']))||(empty($_POST['hotplugName']))){
144 $message[] = _("Please select an entry or press cancel.");
145 }
146 }
148 return ($message);
149 }
151 function is_2byteHex($str)
152 {
153 return empty($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
154 }
159 /* Save to LDAP */
160 function save()
161 {
162 /* return generated entry from input fields*/
163 if(!$this->use_existing){
164 $a_return=array();
165 $a_return[$this->HOT_name]['name'] = $this->HOT_name;
166 $a_return[$this->HOT_name]['description'] = $this->HOT_description;
167 $a_return[$this->HOT_name]['id'] = $this->HOT_id;
168 $a_return[$this->HOT_name]['produkt'] = $this->HOT_produkt;
169 $a_return[$this->HOT_name]['vendor'] = $this->HOT_vendor;
170 return($a_return);
171 }else{
172 $entries = array();
173 foreach($_POST['hotplugName'] as $name){
174 $entries[$name] = $this->hotplugDeviceList[$name];
175 }
176 return $entries;
177 }
178 }
180 function getHotplugs(){
181 $ldap= $this->config->get_ldap_link();
182 $ldap->cd($this->depselect);
184 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->regex."))",array("gotoHotplugDevice"));
186 $a_return = array();
187 $this->hotplugDeviceList = array();
188 while($attr = $ldap->fetch()){
189 if(isset($attr['gotoHotplugDevice'])){
190 unset($attr['gotoHotplugDevice']['count']);
191 foreach($attr['gotoHotplugDevice'] as $device){
192 $tmp =$tmp2= array();
193 $tmp = split("\|",$device);
195 if(preg_match("/^".str_replace("*","",$this->regex).".*/i",$tmp[0])){
197 if(in_array($tmp[0],$this->skipThese)) continue;
199 $a_return[$tmp[0]]= $tmp[0]." [".$tmp[1]."] ".$tmp[2];
201 $tmp2['name'] = $tmp[0];
202 $tmp2['description'] = $tmp[1];
203 $tmp2['id'] = $tmp[2];
205 /* Produkt ID */
206 if(!isset($tmp[3])){
207 $tmp[3] = "";
208 }
209 /* Vendor ID */
210 if(!isset($tmp[4])){
211 $tmp[4] = "";
212 }
214 $tmp2['produkt'] = $tmp[3];
215 $tmp2['vendor'] = $tmp[4];
217 $this->hotplugDeviceList[$tmp[0]]=$tmp2;
218 }
219 }
220 }
221 }
222 return($a_return);
223 }
225 }
227 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
228 ?>