Code

ebe89524de64e879ce722e77f8189aa962393e3d
[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("/^[^\$]+[\$]*$/", $this->path)) {
188         $message[] = _("'$' is only allowed at the end of the share name for hidden shares");
189       }
190     } else {
191       if (!tests::is_path($this->path)){
192         $message[]= _("'Path / Volume' must contain at least one '/' and no special characters!");
193       }
194     }
196     if(!in_array($this->type,$this->types)){
197       $message[]= msgPool::required(_("Type"));
198     }
200     // only 0-9a-z
201     if(!$this->is_edit){
202       if(empty($this->name)){
203         $message[]= msgPool::required(_("Name"));
204       }
205       if(!preg_match("/^[a-z0-9\._äüö]*$/ui",$this->name)){
206         $message[]= msgPool::invalid(_("Name"),$this->name,"/[a-z0-9\._äüö]/ui");
207       }
208     }
210     if(!empty($this->description) && preg_match("/^[^a-z0-9\._äüö\+ -]*$/ui",$this->description)){
211       $message[]= msgPool::invalid(_("Description"),$this->description,"/^[a-z0-9\._äüö\+ -]*$/ui");
212     }
214     if(!empty($this->volume) && preg_match("/^[^a-z0-9\._äüö\+ -]*$/ui",$this->volume)){
215       $message[]= msgPool::invalid(_("Volume"),$this->volume,"/^[a-z0-9\._äüö\+ -]*$/ui");
216     }
218     if(!empty($this->option) && preg_match("/^[^a-z0-9\._äüö,=\+ -]*$/ui",$this->option)){
219       $message[]= msgPool::invalid(_("Option"),$this->option,"/^[a-z0-9\._äüö\+ -]*$/ui");
220     }
222     /* remove a / at the end of the path, we neither need it there nor
223      * do we want to check for it later.
224      */
225     if(substr($this->path, -1, 1) == '/') {
226       $this->path=substr($this->path, 0, -1);
227     }
229     $this_cn = preg_replace("/,ou=.*$/","",$this->dn);
230     $this_cn = preg_replace("/cn=/","",$this_cn);
231     $ldap= $this->config->get_ldap_link();
232     $ldap->cd($this->config->current['BASE']);
233     $ldap->search("(objectClass=goShareServer)", array("goExportEntry", "cn", "gosaUnitTag"));
234     while($test = $ldap->fetch()){
235       if($test['dn']==$this->dn)
236         continue;
237       if(isset($test['goExportEntry'])){
238         foreach($test['goExportEntry'] as $entry){
239           $tmp = split("\|",$entry);
240           if($tmp[0] == $this->name && $test['cn'][0] == $this_cn){
241             /* The selected share name is identical to another share name.  Furthermore, that other share is
242                on a server with the same name. Reject this unless the other server is in a different
243                adminstrative domain. */
244             if (!isset($test['gosaUnitTag']) || $test['gosaUnitTag'][0] == $this->gosaUnitTag){
245               $message[]= sprintf(_("The share '%s' on another server '%s' already exists."), $this->name, $this_cn);
246             }
247           }
248         }
249       }
250     }
251     return ($message);
252   }
255   /* Save to LDAP */
256   function save()
257   {
258     /* Everything seems perfect, lets 
259        generate an new export Entry 
260      */
262     $s_return = "";
264     $s_return.= $this->name."|";     
265     $s_return.= $this->description."|";     
266     $s_return.= $this->type."|";     
267     $s_return.= $this->charset."|";     
268     $s_return.= $this->path."|";     
269     $s_return.= $this->option."|";     
270     $s_return.= $this->volume;     
272     return(array($this->name=>$s_return));
273   }
275   function should_create_mount() {
276     return $this->create_mount;
277   }
282 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
283 ?>