<?php
/*
   This code is part of GOsa (https://gosa.gonicus.de)
   Copyright (C) 2006  Gina Haeussge <osd@foosel.net>
   Copyright (C) 2006  Bernd Zeimetz <bernd@zeimetz.de>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
/*! \brief   netatalk plugin
  \author  Gina Haeussge <osd@foosel.net>
  \author  Bernd Zeimetz <bernd@zeimetz.de>
  \version 0.1
  \date    21.3.2006

  This class provides the functionality to read and write all attributes
  relevant for netatalk from/to the LDAP. It does syntax checking
  and displays the formulars required.
 */

class netatalk extends plugin {

  /* Definitions */
  var $plHeadline = "Netatalk";
  var $plDescription = "Manage netatalk account";

  /* CLI vars */
  var $cli_summary = "Manage netatalk account";
  var $cli_description = "Manage Account \nfor netatalk";
  var $cli_parameters = array ("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");

  /* Plugin specific values */
  var $apple_user_homepath_raw   = "";
  var $apple_user_homeurl_raw     = "";
  var $apple_user_homeurl_xml     = "";
  var $apple_user_homeurl       = "";
  var $apple_user_homeDirectory   = "";
  var $apple_user_share       = "";
  var $shares             = array();
  var $shares_settings        = array();
  var $selectedshare        = "";
  var $mountDirectory         = "/Network/Servers";
  
  /* Attributes to save to LDAP */
  var $attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");

  /* Attributes to use in smarty template */
  var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
  
  /* Attributes to save from $_POST */
  var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");

  /* Objectclasses */
  var $objectclasses = array ("apple-user");

  /* Checkboxes */
  var $is_chk_box = array ();
  

  /* The constructor just saves a copy of the config. You may add what ever you need. */
  function netatalk($config, $dn = NULL) {

    /* Include config object */
    $this->config = $config;
    plugin :: plugin($config, $dn);

    /* Copy needed attributes */
    foreach($this->attributes as $val) {
      if (isset($this->attrs["$val"][0])) {
        $name = str_replace('-', '_', $val);
        $this->$name = $this->attrs["$val"][0];
      }
    }
      
    if (strlen($this->apple_user_homeDirectory) >0) {
      $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
    }
    
    /* Save initial account state */
    $this->initially_was_account = $this->is_account;
  }

  /* Execute the plugin, produce the output. */
  function execute() {
    plugin :: execute();

    /* Use the smarty templating engine here... */
    $smarty = get_smarty();
    $display = "";

    /* Do we need to flip is_account state? */
    if (isset ($_POST['modify_state'])) {
      $this->is_account = !$this->is_account;
    }

    /* Do we represent a valid account? */
    if (!$this->is_account && $this->parent == NULL) {
      $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>"._("This account has no netatalk extensions.")."</b>";

      $display .= back_to_main();
      return ($display);
    }
    
    
    /* Get netatalk shares */
    $this->shares = array();
    $ldap = $this->config->get_ldap_link();

    if($this->dn === "new" || $this->dn == NULL) {
      $ldap->cd($this->parent->by_object['user']->base);
    } else {
      $ldap->cd ($this->dn);
      $ldap->cd ('..'); $ldap->cd ('..');
    }
    $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");


    /* Show tab dialog headers */
    if ($this->parent != NULL) {
      if ($this->is_account) {
        $display = $this->show_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
      } else {
        $errmsg="";
        $obj = $this->parent->by_object['posixAccount'];
        if  (!($obj->is_account) ) {
          $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
        }
        if ($ldap->count() == 0) {
          $errmsg.="At least one share with netatalk or NFS mount entry needed.";
        }
        if($errmsg==""){
          $display = $this->show_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
        } else {
          $display = $this->show_header(_("Create netatalk account"), _($errmsg), TRUE);  
        }
        return ($display);
      }
    }
    
    
    while ($attrs = $ldap->fetch()){
      $tmp = split(":", $attrs["cn"][0]);
      $host = trim($tmp[0]);
      $dir = trim($tmp[1]);
      $mountType = trim($attrs["mountType"][0]);
      if ($mountType == "url") {
        $mountTypeReal = "netatalk";
      } else {
        $mountTypeReal = $mountType;
      } 
      $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
      $this->shares[$share] = $share;
      $this->shares_settings[$share]["mountType"]=$mountType;
      $this->shares_settings[$share]["dir"]=$dir;
      $this->shares_settings[$share]["host"]=$host;

      $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
      $newShare=($this->mountDirectory . "/". $host . $dir );
      if (strcmp($oldShare, $newShare)==0) {
            $this->selectedshare = $share;
      }
    }
    asort($this->shares);
    /* Assign attributes and ACL to smarty */
    
    $smarty->assign("netatalkShareACL", chkacl($this->acl, "netatalkShare"));
    $smarty->assign("netatalkUserHomepathACL", chkacl($this->acl, "netatalkUserHomepath"));
    foreach ($this->smarty_attributes as $val) {
      $smarty->assign("$val", $this-> $val);
      if (in_array($val, $this->is_chk_box)) {
        if ($this-> $val == "checked") {
          $smarty->assign($val."CHK", " checked ");
        } else {
          $smarty->assign($val."CHK", "");
        }
      }
    }

    /* Let smarty fetch and process the page. */
    $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
    return ($display);
  }
  
  
  /* Check if we have correct data */
  function check() {
    $message = array ();

    if (strlen($this->apple_user_share) == 0) {
      $message[] = _("You must select a share to use.");
    }

    return ($message);
  }

  /* Save to LDAP */
  function save() {
    /* remove a / at the end of the homepath, we neither need it there nor
      * do we want to check for it later.
      */
    if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
      $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
    }

    $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
    $dir=$this->shares_settings[$this->apple_user_share]["dir"];
    $host=$this->shares_settings[$this->apple_user_share]["host"];
    
    /* Convert raw data to wished format */
    if ($this->is_account) {
      if($mountType=="url") {
        $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
        $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
      } else {
        $this->apple_user_homeurl = "";
      }
      $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
    } else {
      $this->apple_user_homeurl = "";
      $this->apple_user_homeDirectory = "";
    }

    $ldap = $this->config->get_ldap_link();

    /* Call parents save to prepare $this->attrs */
    plugin :: save();

    /* Do attribute conversion */
    foreach ($this->attributes as $val) {
      $name = str_replace('-', '_', $val);
      if ($this->$name != "") {
        $this->attrs[$val] = $this->$name;
      } else {
        $this->attrs[$val] = array();
      }
      unset ($this->attrs[$name]);
    }
      
    /* Write back to ldap */
    $ldap->cd($this->dn);
    $this->cleanup();
    $ldap->modify($this->attrs);

    show_ldap_error($ldap->get_error(), _("Saving Netatalk account failed"));

    /* Optionally execute a command after we're done */
    if ($this->initially_was_account == $this->is_account) {
      if ($this->is_modified) {
        $this->handle_post_events("modify");
      }
    } else {
      $this->handle_post_events("add");
    }
  }

  /* Use Save_object for every Post handling */
  function save_object() {
    if (isset ($_POST['netatalkTab'])) {
      /* Save ldap attributes */
      plugin :: save_object();
      
      foreach($this->post_attributes as $val) {
        if (isset ($_POST[$val])) {
          $this->$val = $_POST[$val];
        } else {
          $this->$val = "";
        }
      }

      /* Specialhandling for checkboxes */
      foreach ($this->is_chk_box as $val) {
        if (isset ($_POST[$val])) {
          $this-> $val = "checked";
        } else {
          $this-> $val = "unchecked";
        }
      }
      
      $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
    }
  }

  function remove_from_parent() {
    /* Cancel if there's nothing to do here */
    if (!$this->initially_was_account) {
      return;
    }

    /* include global link_info */
    $ldap = $this->config->get_ldap_link();

    /* Remove and write to LDAP */
    plugin :: remove_from_parent();

    /* Adapt attributes if needed */
    //     $method= new $this->method($this->config);
    //     $method->fixAttributesOnRemove($this);

    @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
    $ldap->cd($this->dn);
    $this->cleanup();
    $ldap->modify($this->attrs);

    show_ldap_error($ldap->get_error(), _("Removing Netatalk account failed"));

    /* remove the entry from LDAP */
    unset ($this->attrs['uid']);

    /* Optionally execute a command after we're done */
    $this->handle_post_events('remove');
  }

}

// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>
