Code

3f5364e1eaa434f759f645d363ffaba8897f5a56
[gosa.git] / trunk / gosa-plugins / goto / admin / devices / class_deviceGeneric.inc
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;
18   public $attributes      = array("cn","description","devID","serial","vendor", "type");
19   public $objectclasses   = array("top","gotoDevice");
21   public $CopyPasteVars   = array("orig_cn","description","vendor","devID","serial","base");
23   public function deviceGeneric(&$config,$dn = NULL)
24   {
25     plugin::plugin($config,$dn);
27     $this->is_account = TRUE;
29     $this->types= array("camera" => _("Digital camera"),
30                         "disc" => _("USB stick"),
31                         "cdrom" => _("CD/DVD drive"),);
32     asort($this->types);
34     /* Set class values */
35     if(isset($this->attrs['gotoHotplugDevice'][0])){
36       $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
37       $this->cn         = $this->attrs['cn'][0];
38       $this->description= $tmp[0];
39       $this->devID     = $tmp[1];
40       $this->serial     = $tmp[2];
41       $this->vendor     = $tmp[3];
42       if (isset($tmp[4])){
43         $this->type= $tmp[4];
44       }
45     }     
47     $this->orig_cn = $this->cn;
49     /* Set Base */
50     if ($this->dn == "new"){
51       if(session::is_set('CurrentMainBase')){
52         $this->base = session::get('CurrentMainBase');
53       }else{
54         $ui= get_userinfo();
55         $this->base= dn2base($ui->dn);
56       }
57     } else {
58       $this->base =preg_replace ("/^[^,]+,".get_ou('deviceRDN')."/","",$this->dn);
59     }
60     $this->orig_base = $this->base;
61     $this->orig_dn   = $this->dn;
62   }
65   public function execute()
66   {
67     $smarty = get_smarty();
68     $smarty->assign("base",$this->base);
69     $smarty->assign("bases",$this->get_allowed_bases());
70     foreach($this->attributes as $attr){
71       $smarty->assign($attr,$this->$attr);
72     }
73   
74     $tmp = $this->plInfo();
75     foreach($tmp['plProvidedAcls'] as $attr => $desc){
76       $smarty->assign($attr."ACL", $this->getacl($attr));
77     }
79     $smarty->assign("type",$this->type);
80     $smarty->assign ("types", $this->types);
81     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
82   }
84   
85   public function check()
86   {
87     $message = plugin::check();
89     if($this->cn == "" ||(preg_match("/[^a-z0-9]/i",$this->cn))){
90       $message[]= msgPool::invalid(_("Device name"),$this->cn,"/^[a-z0-9]*$/i");
91     }
92     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
93       $message[]= msgPool::invalid(_("Description"),$this->description,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
94     }
96     /* Skip serial check if vendor and product id are given */
97     if(preg_match("/^\s+$/i",$this->devID)){
98       $message[]= msgPool::invalid(_("iSerial"),"","01234");
99     }elseif(empty($this->devID) && $this->devID!="0"){
100       $message[]= msgPool::required(_("iSerial"));
101     }
102     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
103       $message[]= msgPool::invalid(_("Product-ID"),"","","0x1234");
104     }
105     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
106       $message[]= msgPool::invalid(_("Vendor-ID"),"","","0x1234");
107     }
108     /* Convert vendorId and productId to lower case */
109     $this->serial = strtolower($this->serial);
110     $this->vendor = strtolower($this->vendor);
112     /* Check if entry already exists */ 
113     if($this->cn != $this->orig_cn || $this->dn == "new"){
114       $ldap = $this->config->get_ldap_link();
115       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
116       if($ldap->count()){
117         $message[]= msgPool::duplicated(_("Name"));
118       }
119     }
121     /* Check if we are allowed to create or move this object
122      */
123     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
124       $message[] = msgPool::permCreate();
125     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
126       $message[] = msgPool::permMove();
127     }
129     if ($this->cn != $this->orig_cn || $this->base != $this->orig_base) { 
131       $ldap = $this->config->get_ldap_link();
132       $ldap->cd($this->config->current['BASE']);
133       $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".LDAP::prepare4filter($this->orig_dn)."))",
134                     array("cn","uid"));
135       $obj = array();
136       while($attrs = $ldap->fetch()){
137         $obj[$ldap->getDN()]= $attrs['cn'][0];
138       }
139       if ($obj != array()) {
140         $message[] = msgPool::stillInUse(_("Device"), $obj);
141       }
142     }
143      
144     return($message);
145   }
148   public function save_object()
149   {
150     if(isset($_POST['deviceGeneric_posted'])){
151       plugin::save_object();
152   
153       if(isset($_POST['base'])){
154         $tmp = $this->get_allowed_bases();
155         if(isset($tmp[get_post("base")])){
156           $this->base = get_post("base");
157         }
158       }
159     }
160   }
162   
163   public function remove_from_parent()
164   {
165     plugin::remove_from_parent();
166     $ldap = $this->config->get_ldap_link();
167     $ldap->cd($this->config->current['BASE']);
169     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
170     $skip = FALSE;
171     $obj = array();
172     while($attrs = $ldap->fetch()){
173       $obj[$ldap->getDN()]= $attrs['cn'][0];
174       $skip =TRUE;
175     }
176     if($skip){
177       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
178     }else{
179       $ldap->rmdir_recursive($this->dn);
180     }
181   }
184   public function save()
185   {
186     plugin::save();
188     /* Unset non ldap attributes 
189      */
190     foreach(array("devID","serial","vendor", "type", "description") as $name){
191       if(isset($this->attrs[$name])){
192         unset($this->attrs[$name]);
193       }
194     }
196     $this->attrs['gotoHotplugDevice'] = "";
197     foreach(array("description","devID","serial","vendor", "type") as $post){
198       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
199     }
200     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
202     $ldap = $this->config->get_ldap_link();
203     $ldap->cd($this->config->current['BASE']);
204     $ldap->cat($this->dn);
205     if($ldap->count()){
206       $ldap->cd($this->dn);
207       $ldap->modify($this->attrs);
208     }else{
209       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
210       $ldap->cd($this->dn);
211       $ldap->add($this->attrs);
212     }
213     if (!$ldap->success()){
214       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
215     }
216   }
219   /* check if given str in like this 0xffff*/
220   function is_2byteHex($str)
221   {
222     return !strlen($str) || preg_match("/^(0x|)[A-Fa-f0-9]{4}$/i",$str);
223   }
226   function PrepareForCopyPaste($source)
227   {
228     plugin::PrepareForCopyPaste($source);
229     $source_o = new deviceGeneric($this->config,$source['dn']);
230     foreach($this->CopyPasteVars as $post){
231       $this->$post = $source_o->$post;
232     }
233   }
236   /* Return a dialog with all fields that must be changed,
237      if we want to copy this entry */
238   function getCopyDialog()
239   {
240     $str = "";
241     $smarty = get_smarty();
242     $smarty->assign("cn",         $this->cn);
243     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
245     $ret = array();
246     $ret['string'] = $str;
247     $ret['status'] = "";
248     return($ret);
249   }
252   /* Save all */
253   function saveCopyDialog()
254   {
255     $attrs = array("cn");
256     foreach($attrs as $attr){
257       if(isset($_POST[$attr])){
258         $this->$attr = $_POST[$attr];
259       }
260     }
261   }
265   /* Return plugin informations for acl handling  */
266   public static function plInfo()
267   {
268     return (array(
269           "plShortName"   => _("Generic"),
270           "plDescription" => _("Device generic")." ("._("Hotplug").")",
271           "plSelfModify"  => FALSE,
272           "plDepends"     => array(),
273           "plPriority"    => 0,
274           "plSection"     => array("administration"),
275           "plCategory"    => array("devices" => array("description"  => _("Devices"),
276                                                         "objectClass"  => "gotoHotplugDevice")),
277           "plProvidedAcls"=> array(
278             "cn"            => _("Name"),
279             "base"          => _("Base"),
280             "description"   => _("Description"),
281             "type"          => _("Type"),
282             "serial"        => _("Serial"),
283             "vendor"        => _("Vendor"), 
284             "devID"        => _("Device ID"))
285           ));
287   }
288         
290 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
291 ?>