Code

Make error message more specific on where the error is.
[gosa.git] / trunk / gosa-plugins / goto / admin / systems / services / nfs / class_servNfs.inc
1 <?php
3 class servnfs extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = TRUE;
7   var $attributes       = array("description","type","charset","path","option", "volume");
8   var $objectclasses    = array("whatever");
9   var $is_account       = true;
11   var $name          ="";  // Name of  
12   var $description   ="";  // description
13   var $type          ="";  // Type FS/Samba/NCP
14   var $charset       ="";  // charset
15   var $host          ="";  // hostname
16   var $types         =array();  // Array Types NFS/Samba/NCP/netatalk
17   var $charsets      =array();  // Array with charsets
18   var $path          ="";  // Path
19   var $volume        ="";  // Volume
20   var $option        ="";  // Options
21   var $is_edit           =false;
22   var $create_mount_init = false; //mount entry set for this entry (while editing)?
23   var $create_mount = false; //save mount entry
25   var $parent       = NULL;
26   var $view_logged  = FALSE;
28   function servnfs (&$config, $parent,$entry= false,$mount =false)
29   {
30     $this->parent = $parent;
31     $dn = $parent->dn;
32     plugin::plugin ($config, $dn);
34     $this->types   = array("NBD" => "NBD", "CIFS" => "CIFS", "NFS"=>"NFS","samba"=>"samba","netatalk"=>"netatalk","NCP"=>"NCP");
35     if($dn){
36       $this->host = substr($dn, 3, strpos($dn, ',')-3);
37     }
38     asort($this->types);
40     $this->charsets = array();
42     if(!file_exists(CONFIG_DIR."/encodings")){
43       msg_dialog::display(_("Error"), msgPool::fileDoesNotExist(CONFIG_DIR."/encodings"), ERROR_DIALOG);
44     }else{
45       if(!is_readable(CONFIG_DIR."/encodings")){
46         msg_dialog::display(_("Error"), msgPool::cannotReadFile(CONFIG_DIR."/encodings"), ERROR_DIALOG);
47       }else{
48         $fp = fopen(CONFIG_DIR."/encodings","r");
49         $i = 100;
50         while(!feof($fp)&&$i){
51           $i -- ;
52           $str = trim(fgets($fp,256));
54           /* Skip comments */
55           if(!preg_match("/^#/",$str)){
56             $arr = split("\=",$str);
57             if(count($arr)==2){
58               $this->charsets[$arr[0]]=$arr[1];
59             }
60           }
61         }
62       }
63     }
65     if($entry){
66       list($this->name, $this->description, $this->type, $this->charset,
67           $this->path, $this->option, $this->volume)= split("\|",$entry."|");
68       $this->is_edit          = true;
70     }else{
71       $this->attributes[] = "name";
72     }
75     $this->create_mount_init = $mount;
76   }
78   function execute()
79   {
80     /* Call parent execute */
81     plugin::execute();
83     if($this->is_account && !$this->view_logged){
84       $this->view_logged = TRUE;
85       new log("view","server/".get_class($this),$this->dn);
86     }
88     /* Fill templating stuff */
89     $smarty= get_smarty();
91     $smarty->assign("charsets" ,$this->charsets);
92     $smarty->assign("types"    ,$this->types);
94     /* attrs to smarty*/
95     foreach($this->attributes as $attr){
96       $smarty->assign($attr,$this->$attr);
97     }
99     $tmp = $this->parent->plInfo();
100     foreach($tmp['plProvidedAcls'] as $name => $translation){
101       $smarty->assign($name."ACL",$this->parent->getacl($name));
102     }
104     $smarty->assign("name",$this->name);
105     if($this->is_edit){
106       $smarty->assign("nameACL", preg_replace("/w/","",$this->parent->getacl("name")));
107     } 
108     $smarty->assign("allow_mounts", $this->parent->allow_mounts);
109     $smarty->assign("mount_checked", "");
111     $smarty->assign("appleMountsACL",    $this->getacl("appleMounts"));
113     if (($this->type == "netatalk") || ($this->type == "NFS")) {
114       if ($this->create_mount_init) {
115         $smarty->assign("mount_checked", "checked");
116       } else {
117         $tmp = split(",", $this->dn);
118         $clip = $tmp[0] . ",".get_ou('serverRDN');
119         $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
120         switch ($this->type) {
121           case "netatalk" : {
122             $mountdn = "cn=".$this->host.":/".$this->name.",".$mountsdn;
123             break;
124           }
125           case "NFS" : {
126             $mountdn = "cn=".$this->host.":".$this->path.",".$mountsdn;
127             break;
128           }
129           default : {
130                       continue;
131                     }
132         }
133         $ldap = $this->config->get_ldap_link();
134         $ldap->cat($mountdn, array('dn'));
135         $attrs = $ldap->fetch();
136         if (count($attrs) > 0) {
137           $smarty->assign("mount_checked", "checked");
138         }
139       }        
140     }
142     $display= $smarty->fetch(get_template_path('servnfs.tpl', TRUE, dirname(__FILE__)));
143     return($display);
144   }
146   function remove_from_parent()
147   {
148     /* This cannot be removed... */
149   }
152   /* Save data to object */
153   function save_object()
154   {
155     if(isset($_POST['servnfs_posted'])){
157       foreach($this->attributes as $name){
158         if($this->parent->acl_is_writeable($name) && isset($_POST[$name])){
159           $this->$name = $_POST[$name];
160         }
161       }
163       if ((isset($_POST['netatalk_mount'])) && (($this->type == "netatalk") || ($this->type == "NFS"))) {
164         $this->create_mount = true;
165       } else {
166         $this->create_mount = false;
167       }
168     }
169   }
172   /* Check supplied data */
173   function check()
174   {
175     /* Call common method to give check the hook */
176     $message= plugin::check();
178     if(empty($this->path)){
179       $message[]= msgPool::required(_("Path"));
180     }
182     if($this->type == "NBD") {
183       if (!preg_match('/^[a-z0-9-]+:[0-9]+$/', $this->path)){
184         $message[]= _("'Path / Volume' must be of format 'server:port'!");
185       }
186     } elseif ($this->type == "CIFS" || $this->type == "samba") {
187       if (!preg_match("/^[a-z0-9%\/_.+-]+[\$]*$/i", $this->path)) {
188         if (preg_match("/\$/", $this->path) && !preg_match("/^[^\$]+[\$]*$/", $this->path)) {
189           $message[] = _("'$' is only allowed at the end of the share name for hidden shares");
190         }
191         else {
192           $message[] = msgPool::invalid(_("Path"),$this->path,"/[a-z0-9\$%\/_.+-]+[\$]*$/i");
193         }
194       }
195     } else {
196       if (!tests::is_path($this->path)){
197         $message[]= _("'Path / Volume' must contain at least one '/' and no special characters!");
198       }
199     }
201     if(!in_array($this->type,$this->types)){
202       $message[]= msgPool::required(_("Type"));
203     }
205     // only 0-9a-z
206     if(!$this->is_edit){
207       if(empty($this->name)){
208         $message[]= msgPool::required(_("Name"));
209       }
210       if(!preg_match("/^[a-z0-9\._äüö]*$/ui",$this->name)){
211         $message[]= msgPool::invalid(_("Name"),$this->name,"/[a-z0-9\._äüö]/ui");
212       }
213     }
215     if(!empty($this->description) && preg_match("/^[^a-z0-9\._äüö\+ -]*$/ui",$this->description)){
216       $message[]= msgPool::invalid(_("Description"),$this->description,"/^[a-z0-9\._äüö\+ -]*$/ui");
217     }
219     if(!empty($this->volume) && preg_match("/^[^a-z0-9\._äüö\+ -]*$/ui",$this->volume)){
220       $message[]= msgPool::invalid(_("Volume"),$this->volume,"/^[a-z0-9\._äüö\+ -]*$/ui");
221     }
223     if(!empty($this->option) && preg_match("/^[^a-z0-9\._äüö,=\+ -]*$/ui",$this->option)){
224       $message[]= msgPool::invalid(_("Option"),$this->option,"/^[a-z0-9\._äüö\+ -]*$/ui");
225     }
227     /* remove a / at the end of the path, we neither need it there nor
228      * do we want to check for it later.
229      */
230     if(substr($this->path, -1, 1) == '/') {
231       $this->path=substr($this->path, 0, -1);
232     }
234     $this_cn = preg_replace("/,ou=.*$/","",$this->dn);
235     $this_cn = preg_replace("/cn=/","",$this_cn);
236     $ldap= $this->config->get_ldap_link();
237     $ldap->cd($this->config->current['BASE']);
238     $ldap->search("(objectClass=goShareServer)", array("goExportEntry", "cn", "gosaUnitTag"));
239     while($test = $ldap->fetch()){
240       if($test['dn']==$this->dn)
241         continue;
242       if(isset($test['goExportEntry'])){
243         foreach($test['goExportEntry'] as $entry){
244           $tmp = split("\|",$entry);
245           if($tmp[0] == $this->name && $test['cn'][0] == $this_cn){
246             /* The selected share name is identical to another share name.  Furthermore, that other share is
247                on a server with the same name. Reject this unless the other server is in a different
248                adminstrative domain. */
249             if (!isset($test['gosaUnitTag']) || $test['gosaUnitTag'][0] == $this->gosaUnitTag){
250               $message[]= sprintf(_("The share '%s' on another server '%s' already exists."), $this->name, $this_cn);
251             }
252           }
253         }
254       }
255     }
256     return ($message);
257   }
260   /* Save to LDAP */
261   function save()
262   {
263     /* Everything seems perfect, lets 
264        generate an new export Entry 
265      */
267     $s_return = "";
269     $s_return.= $this->name."|";     
270     $s_return.= $this->description."|";     
271     $s_return.= $this->type."|";     
272     $s_return.= $this->charset."|";     
273     $s_return.= $this->path."|";     
274     $s_return.= $this->option."|";     
275     $s_return.= $this->volume;     
277     return(array($this->name=>$s_return));
278   }
280   function should_create_mount() {
281     return $this->create_mount;
282   }
287 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
288 ?>