Code

Removed a couple of print_red calls
[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                         "harddisk" => _("Harddisk"),
30                         "stick" => _("USB stick"),
31                         "cd" => _("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->dev_id     = $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   }
63   public function execute()
64   {
65     $smarty = get_smarty();
66     $smarty->assign("base",$this->base);
67     $smarty->assign("bases",$this->get_allowed_bases());
68     foreach($this->attributes as $attr){
69       $smarty->assign($attr,$this->$attr);
70     }
71     foreach($this->posts as $attr){
72       $smarty->assign($attr,$this->$attr);
73     }
75     $smarty->assign("type",$this->type);
76     $smarty->assign ("types", $this->types);
78     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
79   }
81   
82   public function check()
83   {
84     $message = plugin::check();
86     if(empty($this->cn)||(preg_match("/[^a-z0-9]/i",$this->cn))){
87       $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
88     }
89     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
90       $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
91     }
93     /* Skip serial check if vendor and product id are given */
94     if(preg_match("/^\s+$/i",$this->dev_id)){
95       $message[]= msgPool::invalid(_("iSerial"),"","01234");
96     }
97     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
98       $message[]= msgPool::invalid(_("Serial"),"","","0x1234");
99     }
100     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
101       $message[]= msgPool::invalid(_("Vender ID"),"","","0x1234");
102     }
103    
104     /* Check if entry already exists */ 
105     if($this->cn != $this->orig_cn){
106       $ldap = $this->config->get_ldap_link();
107       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
108       if($ldap->count()){
109         $message[]= msgPool::duplicate(_("Name"));
110       }
111     }
113     return($message);
114   }
117   public function save_object()
118   {
119     if(isset($_POST['deviceGeneric_posted'])){
120       plugin::save_object();
121   
122       if(isset($_POST['base'])){
123         $tmp = $this->get_allowed_bases();
124         if(isset($tmp[get_post("base")])){
125           $this->base = get_post("base");
126         }
127       }
128     
129       foreach($this->posts as $post){
130         if(isset($_POST[$post])){
131           $this->$post = get_post($post);
132         }
133       }
134     }
135   }
137   
138   public function remove_from_parent()
139   {
140     plugin::remove_from_parent();
141     $ldap = $this->config->get_ldap_link();
142     $ldap->cd($this->config->current['BASE']);
144     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
145     $skip = FALSE;
146     $obj = array();
147     while($cnt && $attrs = $ldap->fetch()){
148       $obj[$ldap->getDN()]= $attrs['cn'][0];
149       $skip =TRUE;
150     }
151     if($skip){
152       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
153     }else{
154       $ldap->rmdir_recursive($this->dn);
155     }
156   }
159   public function save()
160   {
161     plugin::save();
163     $this->attrs['gotoHotplugDevice'] = "";
164     foreach($this->posts as $post){
165       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
166     }
167     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
169     $ldap = $this->config->get_ldap_link();
170     $ldap->cd($this->config->current['BASE']);
171     $ldap->cat($this->dn);
172     if($ldap->count()){
173       $ldap->cd($this->dn);
174       $ldap->modify($this->attrs);
175     }else{
176       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
177       $ldap->cd($this->dn);
178       $ldap->add($this->attrs);
179     }
180     if (!$ldap->success()){
181       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
182     }
183   }
186   /* check if given str in like this 0xffff*/
187   function is_2byteHex($str)
188   {
189     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
190   }
193   function PrepareForCopyPaste($source)
194   {
195     plugin::PrepareForCopyPaste($source);
196     $source_o = new deviceGeneric($this->config,$source['dn']);
197     foreach($this->CopyPasteVars as $post){
198       $this->$post = $source_o->$post;
199     }
200   }
203   /* Return a dialog with all fields that must be changed,
204      if we want to copy this entry */
205   function getCopyDialog()
206   {
207     $str = "";
208     $smarty = get_smarty();
209     $smarty->assign("cn",         $this->cn);
210     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
212     $ret = array();
213     $ret['string'] = $str;
214     $ret['status'] = "";
215     return($ret);
216   }
219   /* Save all */
220   function saveCopyDialog()
221   {
222     $attrs = array("cn");
223     foreach($attrs as $attr){
224       if(isset($_POST[$attr])){
225         $this->$attr = $_POST[$attr];
226       }
227     }
228   }
232   /* Return plugin informations for acl handling  */
233   public static function plInfo()
234   {
235     return (array(
236           "plShortName"   => _("Generic"),
237           "plDescription" => _("Device generic"),
238           "plSelfModify"  => FALSE,
239           "plDepends"     => array(),
240           "plPriority"    => 0,
241           "plSection"     => array("administration"),
242           "plCategory"    => array("devices" => array("description"  => _("Devices"),
243                                                         "objectClass"  => "gotoHotplugDevice")),
244           "plProvidedAcls"=> array(
245             "cn"                          => _("Name"))
246           ));
248   }
249         
251 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
252 ?>