Code

closes #405
[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 $description   = "";
9   public $vendor        = "";
10   public $dev_id        = "";
11   public $serial        = "";
12   public $base          = "";   
13   public $types;
14   public $type;
16   public $posts           = array("description","dev_id","serial","vendor", "type");
17   public $attributes      = array("cn");
18   public $objectclasses   = array("top","gotoDevice");
20   public $CopyPasteVars   = array("orig_cn","description","vendor","dev_id","serial","base");
22   public function deviceGeneric(&$config,$dn = NULL)
23   {
24     plugin::plugin($config,$dn);
26     $this->is_account = TRUE;
28     $this->types= array("camera" => _("Digital camera"),
29                         "disc" => _("USB stick"),
30                         "cdrom" => _("CD/DVD drive"),);
31     asort($this->types);
33     /* Set class values */
34     if(isset($this->attrs['gotoHotplugDevice'][0])){
35       $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
36       $this->cn         = $this->attrs['cn'][0];
37       $this->description= $tmp[0];
38       $this->dev_id     = $tmp[1];
39       $this->serial     = $tmp[2];
40       $this->vendor     = $tmp[3];
41       if (isset($tmp[4])){
42         $this->type= $tmp[4];
43       }
44     }     
46     $this->orig_cn = $this->cn;
48     /* Set Base */
49     if ($this->dn == "new"){
50       if(session::is_set('CurrentMainBase')){
51         $this->base = session::get('CurrentMainBase');
52       }else{
53         $ui= get_userinfo();
54         $this->base= dn2base($ui->dn);
55       }
56     } else {
57       $this->base =preg_replace ("/^[^,]+,".get_ou('deviceou')."/","",$this->dn);
58     }
59   }
62   public function execute()
63   {
64     $smarty = get_smarty();
65     $smarty->assign("base",$this->base);
66     $smarty->assign("bases",$this->get_allowed_bases());
67     foreach($this->attributes as $attr){
68       $smarty->assign($attr,$this->$attr);
69     }
70     foreach($this->posts as $attr){
71       $smarty->assign($attr,$this->$attr);
72     }
74     $smarty->assign("type",$this->type);
75     $smarty->assign ("types", $this->types);
77     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
78   }
80   
81   public function check()
82   {
83     $message = plugin::check();
85     if(empty($this->cn)||(preg_match("/[^a-z0-9]/i",$this->cn))){
86       $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
87     }
88     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
89       $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
90     }
92     /* Skip serial check if vendor and product id are given */
93     if(preg_match("/^\s+$/i",$this->dev_id)){
94       $message[]= msgPool::invalid(_("iSerial"),"","01234");
95     }
96     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
97       $message[]= msgPool::invalid(_("Serial"),"","","0x1234");
98     }
99     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
100       $message[]= msgPool::invalid(_("Vendor ID"),"","","0x1234");
101     }
102    
103     /* Check if entry already exists */ 
104     if($this->cn != $this->orig_cn){
105       $ldap = $this->config->get_ldap_link();
106       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
107       if($ldap->count()){
108         $message[]= msgPool::duplicated(_("Name"));
109       }
110     }
112     return($message);
113   }
116   public function save_object()
117   {
118     if(isset($_POST['deviceGeneric_posted'])){
119       plugin::save_object();
120   
121       if(isset($_POST['base'])){
122         $tmp = $this->get_allowed_bases();
123         if(isset($tmp[get_post("base")])){
124           $this->base = get_post("base");
125         }
126       }
127     
128       foreach($this->posts as $post){
129         if(isset($_POST[$post])){
130           $this->$post = get_post($post);
131         }
132       }
133     }
134   }
136   
137   public function remove_from_parent()
138   {
139     plugin::remove_from_parent();
140     $ldap = $this->config->get_ldap_link();
141     $ldap->cd($this->config->current['BASE']);
143     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
144     $skip = FALSE;
145     $obj = array();
146     while($attrs = $ldap->fetch()){
147       $obj[$ldap->getDN()]= $attrs['cn'][0];
148       $skip =TRUE;
149     }
150     if($skip){
151       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
152     }else{
153       $ldap->rmdir_recursive($this->dn);
154     }
155   }
158   public function save()
159   {
160     plugin::save();
162     $this->attrs['gotoHotplugDevice'] = "";
163     foreach($this->posts as $post){
164       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
165     }
166     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
168     $ldap = $this->config->get_ldap_link();
169     $ldap->cd($this->config->current['BASE']);
170     $ldap->cat($this->dn);
171     if($ldap->count()){
172       $ldap->cd($this->dn);
173       $ldap->modify($this->attrs);
174     }else{
175       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
176       $ldap->cd($this->dn);
177       $ldap->add($this->attrs);
178     }
179     if (!$ldap->success()){
180       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
181     }
182   }
185   /* check if given str in like this 0xffff*/
186   function is_2byteHex($str)
187   {
188     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
189   }
192   function PrepareForCopyPaste($source)
193   {
194     plugin::PrepareForCopyPaste($source);
195     $source_o = new deviceGeneric($this->config,$source['dn']);
196     foreach($this->CopyPasteVars as $post){
197       $this->$post = $source_o->$post;
198     }
199   }
202   /* Return a dialog with all fields that must be changed,
203      if we want to copy this entry */
204   function getCopyDialog()
205   {
206     $str = "";
207     $smarty = get_smarty();
208     $smarty->assign("cn",         $this->cn);
209     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
211     $ret = array();
212     $ret['string'] = $str;
213     $ret['status'] = "";
214     return($ret);
215   }
218   /* Save all */
219   function saveCopyDialog()
220   {
221     $attrs = array("cn");
222     foreach($attrs as $attr){
223       if(isset($_POST[$attr])){
224         $this->$attr = $_POST[$attr];
225       }
226     }
227   }
231   /* Return plugin informations for acl handling  */
232   public static function plInfo()
233   {
234     return (array(
235           "plShortName"   => _("Generic"),
236           "plDescription" => _("Device generic"),
237           "plSelfModify"  => FALSE,
238           "plDepends"     => array(),
239           "plPriority"    => 0,
240           "plSection"     => array("administration"),
241           "plCategory"    => array("devices" => array("description"  => _("Devices"),
242                                                         "objectClass"  => "gotoHotplugDevice")),
243           "plProvidedAcls"=> array(
244             "cn"                          => _("Name"))
245           ));
247   }
248         
250 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
251 ?>