1 <?php
3 class hotplugDialog extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
7 var $attributes = array("HOT_name","HOT_description","HOT_id","HOT_produkt","HOT_vendor");
8 var $objectclasses = array("whatever");
9 var $use_existing = false;
11 var $HOT_name = "";
12 var $HOT_description = "";
13 var $HOT_id = "";
14 var $HOT_produkt = "";
15 var $HOT_vendor = "";
17 var $hotplugDevices = "";
18 var $hotplugDeviceList = array();
19 var $regex = "*";
20 var $depselect = "/";
21 var $deplist = "/";
22 var $skipThese = array();
24 function hotplugDialog ($config, $dn= NULL,$use_existing=false ,$skipThese = array())
25 {
26 $this->use_existing = $use_existing;
27 $this->skipThese = $skipThese;
28 plugin::plugin ($config, $dn);
29 $this->depselect = $this->config->current['BASE'];
30 }
32 function execute()
33 {
34 /* Call parent execute */
35 plugin::execute();
37 /* Fill templating stuff */
38 $smarty= get_smarty();
39 $display= "";
41 foreach($this->attributes as $s_attr){
42 $smarty->assign($s_attr,$this->$s_attr);
43 }
45 if($this->use_existing){
47 if(isset($_POST['dialogissubmitted'])){
48 $this->regex=$_POST['regexHot'];
49 $this->depselect = $_POST['depselectHot'];
50 }
52 if((isset($_GET['search']))&&(!empty($_GET['search']))){
53 $this->regex=$_GET['search']."*";
54 $this->regex=preg_replace("/\*\*/","*",$this->regex);
55 }
57 $this->deplist=$this->config->idepartments;
59 $this->hotplugDevices = $this->getHotplugs();
60 $smarty->assign("regexHot" ,$this->regex);
61 $smarty->assign("deplistHot" ,$this->deplist);
62 $smarty->assign("depselectHot",$this->depselect);
63 $smarty->assign("apply", apply_filter());
64 $smarty->assign("alphabet", generate_alphabet());
65 $smarty->assign("search_image", get_template_path('images/search.png'));
66 $smarty->assign("tree_image", get_template_path('images/tree.png'));
67 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
68 $smarty->assign("deplist", $this->config->idepartments);
71 $smarty->assign("hotplugDevices",$this->hotplugDevices);
72 $smarty->assign("hotplugDeviceKeys",array_flip($this->hotplugDevices));
73 $display.= $smarty->fetch(get_template_path('hotplugDialog.tpl', TRUE,dirname(__FILE__)));
74 }else{
75 $display.= $smarty->fetch(get_template_path('hotplugDialogNew.tpl', TRUE,dirname(__FILE__)));
76 }
77 return($display);
78 }
80 function remove_from_parent()
81 {
82 /* This cannot be removed... */
83 }
86 /* Save data to object */
87 function save_object()
88 {
89 if(isset($_POST['dialogissubmitted'])){
90 plugin::save_object();
91 foreach($this->attributes as $s_attr){
92 if(isset($_POST[$s_attr])){
93 if(in_array($s_attr,array("HOT_produkt","HOT_vendor"))){
94 $this->$s_attr = strtolower(stripslashes($_POST[$s_attr]));
95 }else{
96 $this->$s_attr = stripslashes($_POST[$s_attr]);
97 }
98 }else{
99 $this->$s_attr = false;
100 }
101 }
102 }
103 }
106 /* check if given str in like this 0xffff*/
107 function is_2byteHex($str)
108 {
109 return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
110 }
113 /* Check supplied data */
114 function check()
115 {
116 /* Call common method to give check the hook */
117 $message= plugin::check();
119 if(!$this->use_existing){
121 if(empty($this->HOT_name)||(preg_match("/[^a-z0-9]/i",$this->HOT_name))){
122 $message[]=_("Please specify a valid name. Only 0-9 a-Z is allowed.");
123 }
124 if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->HOT_description)){
125 $message[]=_("Invalid character in description. Please specify a valid description.");
126 }
128 /* Skip serial check if vendor and product id are given */
129 if((empty($this->HOT_vendor)) || (empty($this->HOT_produkt))){
130 if(empty($this->HOT_id) || preg_match("/[\|\*]/i",$this->HOT_id)){
131 $message[]=_("Please specify a valid iSerial.");
132 }
133 }
134 if(empty($this->HOT_vendor) || !$this->is_2byteHex($this->HOT_vendor)){
135 $message[]=_("Please specify a valid vendor ID. (2 byte hex like '0xFFFF')");
136 }
137 if(empty($this->HOT_produkt) || !$this->is_2byteHex($this->HOT_produkt)){
138 $message[]=_("Please specify a valid product ID. (2 byte hex like '0xFFFF')");
139 }
140 $ldap = $this->config->get_ldap_link();
141 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDevice=".$this->HOT_name."*))",array("gotoHotplugDevice"));
142 if($ldap->count()){
143 $message[]=_("An Entry with this name already exists.");
144 }
146 }else{
147 if((!isset($_POST['hotplugName']))||(empty($_POST['hotplugName']))){
148 $message[] = _("Please select an entry or press cancel.");
149 }
150 }
152 return ($message);
153 }
156 /* Save to LDAP */
157 function save()
158 {
159 /* return generated entry from input fields*/
160 if(!$this->use_existing){
161 $a_return=array();
162 $a_return[$this->HOT_name]['name'] = $this->HOT_name;
163 $a_return[$this->HOT_name]['description'] = $this->HOT_description;
164 $a_return[$this->HOT_name]['id'] = $this->HOT_id;
165 $a_return[$this->HOT_name]['produkt'] = $this->HOT_produkt;
166 $a_return[$this->HOT_name]['vendor'] = $this->HOT_vendor;
167 return($a_return);
168 }else{
169 $entries = array();
170 foreach($_POST['hotplugName'] as $name){
171 $entries[$name] = $this->hotplugDeviceList[$name];
172 }
173 return $entries;
174 }
175 }
177 function getHotplugs(){
178 $ldap= $this->config->get_ldap_link();
179 $ldap->cd($this->depselect);
181 /* Set tag attribute if we've tagging activated */
182 $tag= "";
183 $ui= get_userinfo();
184 if ($ui->gosaUnitTag != "" && isset($this->config->current['STRICT_UNITS']) &&
185 preg_match('/TRUE/i', $this->config->current['STRICT_UNITS'])){
186 $tag= "(gosaUnitTag=".$ui->gosaUnitTag.")";
187 }
188 $ldap->search("(&(objectClass=gotoEnvironment)$tag(gotoHotplugDevice=".$this->regex."))",array("gotoHotplugDevice"));
190 $a_return = array();
191 $this->hotplugDeviceList = array();
192 while($attr = $ldap->fetch()){
193 if(isset($attr['gotoHotplugDevice'])){
194 unset($attr['gotoHotplugDevice']['count']);
195 foreach($attr['gotoHotplugDevice'] as $device){
196 $tmp =$tmp2= array();
197 $tmp = split("\|",$device);
199 if(preg_match("/^".str_replace("*","",$this->regex).".*/i",$tmp[0])){
201 if(in_array($tmp[0],$this->skipThese)) continue;
203 $a_return[$tmp[0]]= $tmp[0]." [".$tmp[1]."] ".$tmp[2];
205 $tmp2['name'] = $tmp[0];
206 $tmp2['description'] = $tmp[1];
207 $tmp2['id'] = $tmp[2];
209 /* Produkt ID */
210 if(!isset($tmp[3])){
211 $tmp[3] = "";
212 }
213 /* Vendor ID */
214 if(!isset($tmp[4])){
215 $tmp[4] = "";
216 }
218 $tmp2['produkt'] = $tmp[3];
219 $tmp2['vendor'] = $tmp[4];
221 $this->hotplugDeviceList[$tmp[0]]=$tmp2;
222 }
223 }
224 }
225 }
226 return($a_return);
227 }
229 }
231 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
232 ?>