Code

Updated move and create checks
[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 $dev_id        = "";
13   public $serial        = "";
14   public $base          = "";   
15   public $types;
16   public $type;
18   public $posts           = array("description","dev_id","serial","vendor", "type");
19   public $attributes      = array("cn");
20   public $objectclasses   = array("top","gotoDevice");
22   public $CopyPasteVars   = array("orig_cn","description","vendor","dev_id","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->dev_id     = $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 ("/^[^,]+,".get_ou('deviceou')."/","",$this->dn);
60     }
61     $this->orig_base = $this->base;
62     $this->orig_dn   = $this->dn;
63   }
66   public function execute()
67   {
68     $smarty = get_smarty();
69     $smarty->assign("base",$this->base);
70     $smarty->assign("bases",$this->get_allowed_bases());
71     foreach($this->attributes as $attr){
72       $smarty->assign($attr,$this->$attr);
73     }
74     foreach($this->posts as $attr){
75       $smarty->assign($attr,$this->$attr);
76     }
78     $smarty->assign("type",$this->type);
79     $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(empty($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->dev_id)){
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       foreach($this->posts as $post){
141         if(isset($_POST[$post])){
142           $this->$post = get_post($post);
143         }
144       }
145     }
146   }
148   
149   public function remove_from_parent()
150   {
151     plugin::remove_from_parent();
152     $ldap = $this->config->get_ldap_link();
153     $ldap->cd($this->config->current['BASE']);
155     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
156     $skip = FALSE;
157     $obj = array();
158     while($attrs = $ldap->fetch()){
159       $obj[$ldap->getDN()]= $attrs['cn'][0];
160       $skip =TRUE;
161     }
162     if($skip){
163       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
164     }else{
165       $ldap->rmdir_recursive($this->dn);
166     }
167   }
170   public function save()
171   {
172     plugin::save();
174     $this->attrs['gotoHotplugDevice'] = "";
175     foreach($this->posts as $post){
176       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
177     }
178     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
180     $ldap = $this->config->get_ldap_link();
181     $ldap->cd($this->config->current['BASE']);
182     $ldap->cat($this->dn);
183     if($ldap->count()){
184       $ldap->cd($this->dn);
185       $ldap->modify($this->attrs);
186     }else{
187       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
188       $ldap->cd($this->dn);
189       $ldap->add($this->attrs);
190     }
191     if (!$ldap->success()){
192       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
193     }
194   }
197   /* check if given str in like this 0xffff*/
198   function is_2byteHex($str)
199   {
200     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
201   }
204   function PrepareForCopyPaste($source)
205   {
206     plugin::PrepareForCopyPaste($source);
207     $source_o = new deviceGeneric($this->config,$source['dn']);
208     foreach($this->CopyPasteVars as $post){
209       $this->$post = $source_o->$post;
210     }
211   }
214   /* Return a dialog with all fields that must be changed,
215      if we want to copy this entry */
216   function getCopyDialog()
217   {
218     $str = "";
219     $smarty = get_smarty();
220     $smarty->assign("cn",         $this->cn);
221     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
223     $ret = array();
224     $ret['string'] = $str;
225     $ret['status'] = "";
226     return($ret);
227   }
230   /* Save all */
231   function saveCopyDialog()
232   {
233     $attrs = array("cn");
234     foreach($attrs as $attr){
235       if(isset($_POST[$attr])){
236         $this->$attr = $_POST[$attr];
237       }
238     }
239   }
243   /* Return plugin informations for acl handling  */
244   public static function plInfo()
245   {
246     return (array(
247           "plShortName"   => _("Generic"),
248           "plDescription" => _("Device generic"),
249           "plSelfModify"  => FALSE,
250           "plDepends"     => array(),
251           "plPriority"    => 0,
252           "plSection"     => array("administration"),
253           "plCategory"    => array("devices" => array("description"  => _("Devices"),
254                                                         "objectClass"  => "gotoHotplugDevice")),
255           "plProvidedAcls"=> array(
256             "cn"                          => _("Name"))
257           ));
259   }
260         
262 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
263 ?>