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 /* Refresh base */
140 if ($this->acl_is_moveable($this->base)){
141 if (!$this->baseSelector->update()) {
142 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
143 }
144 if ($this->base != $this->baseSelector->getBase()) {
145 $this->base= $this->baseSelector->getBase();
146 $this->is_modified= TRUE;
147 }
148 }
150 if(isset($_POST['deviceGeneric_posted'])){
151 plugin::save_object();
152 }
153 }
156 public function remove_from_parent()
157 {
158 plugin::remove_from_parent();
159 $ldap = $this->config->get_ldap_link();
160 $ldap->cd($this->config->current['BASE']);
162 $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
163 $skip = FALSE;
164 $obj = array();
165 while($attrs = $ldap->fetch()){
166 $obj[$ldap->getDN()]= $attrs['cn'][0];
167 $skip =TRUE;
168 }
169 if($skip){
170 msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
171 }else{
172 $ldap->rmdir_recursive($this->dn);
173 }
174 }
177 public function save()
178 {
179 plugin::save();
181 /* Unset non ldap attributes
182 */
183 foreach(array("devID","serial","vendor", "type", "description") as $name){
184 if(isset($this->attrs[$name])){
185 unset($this->attrs[$name]);
186 }
187 }
189 $this->attrs['gotoHotplugDevice'] = "";
190 foreach(array("description","devID","serial","vendor", "type") as $post){
191 $this->attrs['gotoHotplugDevice'] .= $this->$post."|";
192 }
193 $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
195 $ldap = $this->config->get_ldap_link();
196 $ldap->cd($this->config->current['BASE']);
197 $ldap->cat($this->dn);
198 if($ldap->count()){
199 $ldap->cd($this->dn);
200 $ldap->modify($this->attrs);
201 }else{
202 $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
203 $ldap->cd($this->dn);
204 $ldap->add($this->attrs);
205 }
206 if (!$ldap->success()){
207 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
208 }
209 }
212 /* check if given str in like this 0xffff*/
213 function is_2byteHex($str)
214 {
215 return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9]{4}$/i",$str);
216 }
219 function PrepareForCopyPaste($source)
220 {
221 plugin::PrepareForCopyPaste($source);
222 $source_o = new deviceGeneric($this->config,$source['dn']);
223 foreach($this->CopyPasteVars as $post){
224 $this->$post = $source_o->$post;
225 }
226 }
229 /* Return a dialog with all fields that must be changed,
230 if we want to copy this entry */
231 function getCopyDialog()
232 {
233 $str = "";
234 $smarty = get_smarty();
235 $smarty->assign("cn", $this->cn);
236 $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
238 $ret = array();
239 $ret['string'] = $str;
240 $ret['status'] = "";
241 return($ret);
242 }
245 /* Save all */
246 function saveCopyDialog()
247 {
248 $attrs = array("cn");
249 foreach($attrs as $attr){
250 if(isset($_POST[$attr])){
251 $this->$attr = $_POST[$attr];
252 }
253 }
254 }
258 /* Return plugin informations for acl handling */
259 public static function plInfo()
260 {
261 return (array(
262 "plShortName" => _("Generic"),
263 "plDescription" => _("Device generic")." ("._("Hotplug").")",
264 "plSelfModify" => FALSE,
265 "plDepends" => array(),
266 "plPriority" => 0,
267 "plSection" => array("administration"),
268 "plCategory" => array("devices" => array("description" => _("Devices"),
269 "objectClass" => "gotoHotplugDevice")),
270 "plProvidedAcls"=> array(
271 "cn" => _("Name"),
272 "base" => _("Base"),
273 "description" => _("Description"),
274 "type" => _("Type"),
275 "serial" => _("Serial"),
276 "vendor" => _("Vendor"),
277 "devID" => _("Device ID"))
278 ));
280 }
282 }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>