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 if given str in like this 0xffff*/
112 function is_2byteHex($str)
113 {
114 return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
115 }
118 /* Check supplied data */
119 function check()
120 {
121 /* Call common method to give check the hook */
122 $message= plugin::check();
124 if(!$this->use_existing){
126 if(empty($this->HOT_name)||(preg_match("/[^a-z0-9]/i",$this->HOT_name))){
127 $message[]=_("Please specify a valid name. Only 0-9 a-Z is allowed.");
128 }
129 if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->HOT_description)){
130 $message[]=_("Invalid character in description. Please specify a valid description.");
131 }
133 if(preg_match("/[\|]/i",$this->HOT_id)){
134 $message[]=_("Please specify a valid id.");
135 }
136 if(!$this->is_2byteHex($this->HOT_vendor)){
137 $message[]=_("Please specify a valid vendor id. (2 byte hex like '0xFFFF')");
138 }
139 if(!$this->is_2byteHex($this->HOT_produkt)){
140 $message[]=_("Please specify a valid product id. (2 byte hex like '0xFFFF')");
141 }
142 $ldap = $this->config->get_ldap_link();
143 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->HOT_name."*))",array("gotoHotplugDevice"));
144 if($ldap->count()){
145 $message[]=_("An Entry with this name already exists.");
146 }
148 }else{
149 if((!isset($_POST['hotplugName']))||(empty($_POST['hotplugName']))){
150 $message[] = _("Please select an entry or press cancel.");
151 }
152 }
154 return ($message);
155 }
158 /* Save to LDAP */
159 function save()
160 {
161 /* return generated entry from input fields*/
162 if(!$this->use_existing){
163 $a_return=array();
164 $a_return[$this->HOT_name]['name'] = $this->HOT_name;
165 $a_return[$this->HOT_name]['description'] = $this->HOT_description;
166 $a_return[$this->HOT_name]['id'] = $this->HOT_id;
167 $a_return[$this->HOT_name]['produkt'] = $this->HOT_produkt;
168 $a_return[$this->HOT_name]['vendor'] = $this->HOT_vendor;
169 return($a_return);
170 }else{
171 $entries = array();
172 foreach($_POST['hotplugName'] as $name){
173 $entries[$name] = $this->hotplugDeviceList[$name];
174 }
175 return $entries;
176 }
177 }
179 function getHotplugs(){
180 $ldap= $this->config->get_ldap_link();
181 $ldap->cd($this->depselect);
183 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->regex."))",array("gotoHotplugDevice"));
185 $a_return = array();
186 $this->hotplugDeviceList = array();
187 while($attr = $ldap->fetch()){
188 if(isset($attr['gotoHotplugDevice'])){
189 unset($attr['gotoHotplugDevice']['count']);
190 foreach($attr['gotoHotplugDevice'] as $device){
191 $tmp =$tmp2= array();
192 $tmp = split("\|",$device);
194 if(preg_match("/^".str_replace("*","",$this->regex).".*/i",$tmp[0])){
196 if(in_array($tmp[0],$this->skipThese)) continue;
198 $a_return[$tmp[0]]= $tmp[0]." [".$tmp[1]."] ".$tmp[2];
200 $tmp2['name'] = $tmp[0];
201 $tmp2['description'] = $tmp[1];
202 $tmp2['id'] = $tmp[2];
204 /* Produkt ID */
205 if(!isset($tmp[3])){
206 $tmp[3] = "";
207 }
208 /* Vendor ID */
209 if(!isset($tmp[4])){
210 $tmp[4] = "";
211 }
213 $tmp2['produkt'] = $tmp[3];
214 $tmp2['vendor'] = $tmp[4];
216 $this->hotplugDeviceList[$tmp[0]]=$tmp2;
217 }
218 }
219 }
220 }
221 return($a_return);
222 }
224 }
226 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
227 ?>