Code

Apply fix for #3105
[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(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
91     }
92     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
93       $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[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     return($message);
130   }
133   public function save_object()
134   {
135     if(isset($_POST['deviceGeneric_posted'])){
136       plugin::save_object();
137   
138       if(isset($_POST['base'])){
139         $tmp = $this->get_allowed_bases();
140         if(isset($tmp[get_post("base")])){
141           $this->base = get_post("base");
142         }
143       }
144     }
145   }
147   
148   public function remove_from_parent()
149   {
150     plugin::remove_from_parent();
151     $ldap = $this->config->get_ldap_link();
152     $ldap->cd($this->config->current['BASE']);
154     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
155     $skip = FALSE;
156     $obj = array();
157     while($attrs = $ldap->fetch()){
158       $obj[$ldap->getDN()]= $attrs['cn'][0];
159       $skip =TRUE;
160     }
161     if($skip){
162       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
163     }else{
164       $ldap->rmdir_recursive($this->dn);
165     }
166   }
169   public function save()
170   {
171     plugin::save();
173     /* Unset non ldap attributes 
174      */
175     foreach(array("devID","serial","vendor", "type", "description") as $name){
176       if(isset($this->attrs[$name])){
177         unset($this->attrs[$name]);
178       }
179     }
181     $this->attrs['gotoHotplugDevice'] = "";
182     foreach(array("description","devID","serial","vendor", "type") as $post){
183       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
184     }
185     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
187     $ldap = $this->config->get_ldap_link();
188     $ldap->cd($this->config->current['BASE']);
189     $ldap->cat($this->dn);
190     if($ldap->count()){
191       $ldap->cd($this->dn);
192       $ldap->modify($this->attrs);
193     }else{
194       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
195       $ldap->cd($this->dn);
196       $ldap->add($this->attrs);
197     }
198     if (!$ldap->success()){
199       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
200     }
201   }
204   /* check if given str in like this 0xffff*/
205   function is_2byteHex($str)
206   {
207     return !strlen($str) || preg_match("/^(0x|)[A-Fa-f0-9]{4}$/i",$str);
208   }
211   function PrepareForCopyPaste($source)
212   {
213     plugin::PrepareForCopyPaste($source);
214     $source_o = new deviceGeneric($this->config,$source['dn']);
215     foreach($this->CopyPasteVars as $post){
216       $this->$post = $source_o->$post;
217     }
218   }
221   /* Return a dialog with all fields that must be changed,
222      if we want to copy this entry */
223   function getCopyDialog()
224   {
225     $str = "";
226     $smarty = get_smarty();
227     $smarty->assign("cn",         $this->cn);
228     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
230     $ret = array();
231     $ret['string'] = $str;
232     $ret['status'] = "";
233     return($ret);
234   }
237   /* Save all */
238   function saveCopyDialog()
239   {
240     $attrs = array("cn");
241     foreach($attrs as $attr){
242       if(isset($_POST[$attr])){
243         $this->$attr = $_POST[$attr];
244       }
245     }
246   }
250   /* Return plugin informations for acl handling  */
251   public static function plInfo()
252   {
253     return (array(
254           "plShortName"   => _("Generic"),
255           "plDescription" => _("Device generic")." ("._("Hotplug").")",
256           "plSelfModify"  => FALSE,
257           "plDepends"     => array(),
258           "plPriority"    => 0,
259           "plSection"     => array("administration"),
260           "plCategory"    => array("devices" => array("description"  => _("Devices"),
261                                                         "objectClass"  => "gotoHotplugDevice")),
262           "plProvidedAcls"=> array(
263             "cn"            => _("Name"),
264             "base"          => _("Base"),
265             "description"   => _("Description"),
266             "type"          => _("Type"),
267             "serial"        => _("Serial"),
268             "vendor"        => _("Vendor"), 
269             "devID"        => _("Device ID"))
270           ));
272   }
273         
275 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
276 ?>