1 <?php
3 class deviceGeneric extends plugin
4 {
5 public $dn = "";
6 public $cn = "";
7 public $orig_cn = "";
8 public $orig_base = "";
9 public $orig_dn = "";
10 public $description = "";
11 public $vendor = "";
12 public $devID = "";
13 public $serial = "";
14 public $base = "";
15 public $types;
16 public $type;
17 public $baseSelector;
19 public $attributes = array("cn","description","devID","serial","vendor", "type");
20 public $objectclasses = array("top","gotoDevice");
22 public $CopyPasteVars = array("orig_cn","description","vendor","devID","serial","base");
24 public function deviceGeneric(&$config,$dn = NULL)
25 {
26 plugin::plugin($config,$dn);
28 $this->is_account = TRUE;
30 $this->types= array("camera" => _("Digital camera"),
31 "disc" => _("USB stick"),
32 "cdrom" => _("CD/DVD drive"),);
33 asort($this->types);
35 /* Set class values */
36 if(isset($this->attrs['gotoHotplugDevice'][0])){
37 $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
38 $this->cn = $this->attrs['cn'][0];
39 $this->description= $tmp[0];
40 $this->devID = $tmp[1];
41 $this->serial = $tmp[2];
42 $this->vendor = $tmp[3];
43 if (isset($tmp[4])){
44 $this->type= $tmp[4];
45 }
46 }
48 $this->orig_cn = $this->cn;
50 /* Set Base */
51 if ($this->dn == "new"){
52 if(session::is_set('CurrentMainBase')){
53 $this->base = session::get('CurrentMainBase');
54 }else{
55 $ui= get_userinfo();
56 $this->base= dn2base($ui->dn);
57 }
58 } else {
59 $this->base =preg_replace ("/^[^,]+,".preg_quote(get_ou('deviceRDN'), '/')."/i","",$this->dn);
60 }
61 $this->orig_base = $this->base;
62 $this->orig_dn = $this->dn;
64 /* Instanciate base selector */
65 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
66 $this->baseSelector->setSubmitButton(false);
67 $this->baseSelector->setHeight(300);
68 $this->baseSelector->update(true);
69 }
72 public function execute()
73 {
74 $smarty = get_smarty();
75 $smarty->assign("usePrototype", "true");
76 $smarty->assign("base", $this->baseSelector->render());
77 foreach($this->attributes as $attr){
78 $smarty->assign($attr,$this->$attr);
79 }
81 $tmp = $this->plInfo();
82 foreach($tmp['plProvidedAcls'] as $attr => $desc){
83 $smarty->assign($attr."ACL", $this->getacl($attr));
84 }
86 $smarty->assign("type",$this->type);
87 $smarty->assign ("types", $this->types);
88 return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
89 }
92 public function check()
93 {
94 $message = plugin::check();
96 if($this->cn == "" ||(preg_match("/[^a-z0-9]/i",$this->cn))){
97 $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
98 }
99 if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
100 $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
101 }
103 /* Skip serial check if vendor and product id are given */
104 if(preg_match("/^\s+$/i",$this->devID)){
105 $message[]= msgPool::invalid(_("iSerial"),"","01234");
106 }elseif(empty($this->devID) && $this->devID!="0"){
107 $message[]= msgPool::required(_("iSerial"));
108 }
109 if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
110 $message[]= msgPool::invalid(_("Product-ID"),"","","0x1234");
111 }
112 if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
113 $message[]= msgPool::invalid(_("Vendor-ID"),"","","0x1234");
114 }
116 /* Check if entry already exists */
117 if($this->cn != $this->orig_cn || $this->dn == "new"){
118 $ldap = $this->config->get_ldap_link();
119 $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
120 if($ldap->count()){
121 $message[]= msgPool::duplicated(_("Name"));
122 }
123 }
125 /* Check if we are allowed to create or move this object
126 */
127 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
128 $message[] = msgPool::permCreate();
129 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
130 $message[] = msgPool::permMove();
131 }
133 return($message);
134 }
137 public function save_object()
138 {
139 if(isset($_POST['deviceGeneric_posted'])){
140 plugin::save_object();
142 /* Refresh base */
143 if ($this->acl_is_moveable($this->base)){
144 if (!$this->baseSelector->update()) {
145 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
146 }
147 if ($this->base != $this->baseSelector->getBase()) {
148 $this->base= $this->baseSelector->getBase();
149 $this->is_modified= TRUE;
150 }
151 }
153 }
154 }
157 public function remove_from_parent()
158 {
159 plugin::remove_from_parent();
160 $ldap = $this->config->get_ldap_link();
161 $ldap->cd($this->config->current['BASE']);
163 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
164 $skip = FALSE;
165 $obj = array();
166 while($attrs = $ldap->fetch()){
167 $obj[$ldap->getDN()]= $attrs['cn'][0];
168 $skip =TRUE;
169 }
170 if($skip){
171 msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
172 }else{
173 $ldap->rmdir_recursive($this->dn);
174 }
175 }
178 public function save()
179 {
180 plugin::save();
182 /* Unset non ldap attributes
183 */
184 foreach(array("devID","serial","vendor", "type", "description") as $name){
185 if(isset($this->attrs[$name])){
186 unset($this->attrs[$name]);
187 }
188 }
190 $this->attrs['gotoHotplugDevice'] = "";
191 foreach(array("description","devID","serial","vendor", "type") as $post){
192 $this->attrs['gotoHotplugDevice'] .= $this->$post."|";
193 }
194 $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
196 $ldap = $this->config->get_ldap_link();
197 $ldap->cd($this->config->current['BASE']);
198 $ldap->cat($this->dn);
199 if($ldap->count()){
200 $ldap->cd($this->dn);
201 $ldap->modify($this->attrs);
202 }else{
203 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
204 $ldap->cd($this->dn);
205 $ldap->add($this->attrs);
206 }
207 if (!$ldap->success()){
208 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
209 }
210 }
213 /* check if given str in like this 0xffff*/
214 function is_2byteHex($str)
215 {
216 return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9]{4}$/i",$str);
217 }
220 function PrepareForCopyPaste($source)
221 {
222 plugin::PrepareForCopyPaste($source);
223 $source_o = new deviceGeneric($this->config,$source['dn']);
224 foreach($this->CopyPasteVars as $post){
225 $this->$post = $source_o->$post;
226 }
227 }
230 /* Return a dialog with all fields that must be changed,
231 if we want to copy this entry */
232 function getCopyDialog()
233 {
234 $str = "";
235 $smarty = get_smarty();
236 $smarty->assign("cn", $this->cn);
237 $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
239 $ret = array();
240 $ret['string'] = $str;
241 $ret['status'] = "";
242 return($ret);
243 }
246 /* Save all */
247 function saveCopyDialog()
248 {
249 $attrs = array("cn");
250 foreach($attrs as $attr){
251 if(isset($_POST[$attr])){
252 $this->$attr = $_POST[$attr];
253 }
254 }
255 }
259 /* Return plugin informations for acl handling */
260 public static function plInfo()
261 {
262 return (array(
263 "plShortName" => _("Generic"),
264 "plDescription" => _("Device generic")." ("._("Hotplug").")",
265 "plSelfModify" => FALSE,
266 "plDepends" => array(),
267 "plPriority" => 0,
268 "plSection" => array("administration"),
269 "plCategory" => array("devices" => array("description" => _("Devices"),
270 "objectClass" => "gotoHotplugDevice")),
271 "plProvidedAcls"=> array(
272 "cn" => _("Name"),
273 "base" => _("Base"),
274 "description" => _("Description"),
275 "type" => _("Type"),
276 "serial" => _("Serial"),
277 "vendor" => _("Vendor"),
278 "devID" => _("Device ID"))
279 ));
281 }
283 }
284 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
285 ?>