Code

Updated devices
[gosa.git] / 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('deviceou')."/","",$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     }
100     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
101       $message[]= msgPool::invalid(_("Serial"),"","","0x1234");
102     }
103     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
104       $message[]= msgPool::invalid(_("Vendor ID"),"","","0x1234");
105     }
106   
107     /* Check if entry already exists */ 
108     if($this->cn != $this->orig_cn || $this->dn == "new"){
109       $ldap = $this->config->get_ldap_link();
110       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
111       if($ldap->count()){
112         $message[]= msgPool::duplicated(_("Name"));
113       }
114     }
116     /* Check if we are allowed to create or move this object
117      */
118     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
119       $message[] = msgPool::permCreate();
120     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
121       $message[] = msgPool::permMove();
122     }
124     return($message);
125   }
128   public function save_object()
129   {
130     if(isset($_POST['deviceGeneric_posted'])){
131       plugin::save_object();
132   
133       if(isset($_POST['base'])){
134         $tmp = $this->get_allowed_bases();
135         if(isset($tmp[get_post("base")])){
136           $this->base = get_post("base");
137         }
138       }
139     }
140   }
142   
143   public function remove_from_parent()
144   {
145     plugin::remove_from_parent();
146     $ldap = $this->config->get_ldap_link();
147     $ldap->cd($this->config->current['BASE']);
149     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
150     $skip = FALSE;
151     $obj = array();
152     while($attrs = $ldap->fetch()){
153       $obj[$ldap->getDN()]= $attrs['cn'][0];
154       $skip =TRUE;
155     }
156     if($skip){
157       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
158     }else{
159       $ldap->rmdir_recursive($this->dn);
160     }
161   }
164   public function save()
165   {
166     plugin::save();
168     /* Unset non ldap attributes 
169      */
170     foreach(array("devID","serial","vendor", "type", "description") as $name){
171       if(isset($this->attrs[$name])){
172         unset($this->attrs[$name]);
173       }
174     }
176     $this->attrs['gotoHotplugDevice'] = "";
177     foreach(array("description","devID","serial","vendor", "type") as $post){
178       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
179     }
180     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
182     $ldap = $this->config->get_ldap_link();
183     $ldap->cd($this->config->current['BASE']);
184     $ldap->cat($this->dn);
185     if($ldap->count()){
186       $ldap->cd($this->dn);
187       $ldap->modify($this->attrs);
188     }else{
189       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
190       $ldap->cd($this->dn);
191       $ldap->add($this->attrs);
192     }
193     if (!$ldap->success()){
194       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
195     }
196   }
199   /* check if given str in like this 0xffff*/
200   function is_2byteHex($str)
201   {
202     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
203   }
206   function PrepareForCopyPaste($source)
207   {
208     plugin::PrepareForCopyPaste($source);
209     $source_o = new deviceGeneric($this->config,$source['dn']);
210     foreach($this->CopyPasteVars as $post){
211       $this->$post = $source_o->$post;
212     }
213   }
216   /* Return a dialog with all fields that must be changed,
217      if we want to copy this entry */
218   function getCopyDialog()
219   {
220     $str = "";
221     $smarty = get_smarty();
222     $smarty->assign("cn",         $this->cn);
223     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
225     $ret = array();
226     $ret['string'] = $str;
227     $ret['status'] = "";
228     return($ret);
229   }
232   /* Save all */
233   function saveCopyDialog()
234   {
235     $attrs = array("cn");
236     foreach($attrs as $attr){
237       if(isset($_POST[$attr])){
238         $this->$attr = $_POST[$attr];
239       }
240     }
241   }
245   /* Return plugin informations for acl handling  */
246   public static function plInfo()
247   {
248     return (array(
249           "plShortName"   => _("Generic"),
250           "plDescription" => _("Device generic")." ("._("Hotplug").")",
251           "plSelfModify"  => FALSE,
252           "plDepends"     => array(),
253           "plPriority"    => 0,
254           "plSection"     => array("administration"),
255           "plCategory"    => array("devices" => array("description"  => _("Devices"),
256                                                         "objectClass"  => "gotoHotplugDevice")),
257           "plProvidedAcls"=> array(
258             "cn"            => _("Name"),
259             "base"          => _("Base"),
260             "description"   => _("Description"),
261             "type"          => _("Type"),
262             "serial"        => _("Serial"),
263             "vendor"        => _("Vendor"), 
264             "devID"        => _("Device ID"))
265           ));
267   }
268         
270 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
271 ?>