[enh] adding FTP object (start, not complete) + adding Legacyobject class used by all

This commit is contained in:
Benjamin Sonntag 2015-01-05 01:32:35 +01:00
parent 66e97ff156
commit ac6d61ae6b
4 changed files with 163 additions and 45 deletions

View File

@ -0,0 +1,42 @@
<?php
/**
* Any Legacy AlternC Api should use this class as a parent
* to be able to handle properly the access rights & error messages
*
* @author benjamin
*/
class Alternc_Api_Legacyobject {
protected $admin; // m_admin instance
protected $cuid; // current user id
protected $isAdmin; // is it an Admin account?
const ERR_INVALID_ARGUMENT = 111201;
const ERR_ALTERNC_FUNCTION = 111202;
function __construct($service) {
global $admin,$cuid;
if (!($service instanceof Alternc_Api_Service)) {
throw new \Exception("Bad argument: service is not an Alternc_Api_Service", self::ERR_INVALID_ARGUMENT);
}
// We store the global $cuid to AlternC legacy classes
$this->cuid=$cuid=$service->token->uid;
$this->isAdmin=$service->token->isAdmin;
// We use the global $admin from AlternC legacy classes
$this->admin=$admin;
// Set the legacy rights:
$this->admin->enabled=$this->isAdmin;
}
/** return a proper Alternc_Api_Response from an error class and error string
* from AlternC legacy class
*/
protected function alterncLegacyErrorManager() {
global $err;
return new Alternc_Api_Response( array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "[".$err->clsid."] ".$err->error) );
}
} /* Aternc_Api_Legacyobject */

View File

@ -3,15 +3,10 @@
/**
* Account Api of AlternC, used by alternc-api package
*/
class Alternc_Api_Object_Account {
class Alternc_Api_Object_Account extends Alternc_Api_Legacyobject {
const ERR_INVALID_ARGUMENT = 1115101;
const ERR_ALTERNC_FUNCTION = 1115102;
const ERR_NOT_FOUND = 1115103;
var $admin; // m_admin instance
var $cuid; // current user id
var $isAdmin; // is it an Admin account?
function __construct($service) {
global $admin,$cuid;
@ -74,7 +69,6 @@ class Alternc_Api_Object_Account {
* non-mandatory: pass, canpass, type, duration, notes, force, create_dom, db_server_id
* @return Alternc_Api_Response whose content is the updated UID
*/
// function update_mem($uid, $mail, $nom, $prenom, $pass, $enabled, $canpass, $type='default', $duration=0, $notes = "",$reset_quotas=false) {
function update($options) {
$defaults=array("nom","prenom","mail","canpass", "enabled","type","duration", "notes");
if (!isset($options["uid"])) {
@ -162,14 +156,4 @@ class Alternc_Api_Object_Account {
}
/** return a proper Alternc_Api_Response from an error class and error string
* from AlternC legacy class
*/
private function alterncLegacyErrorManager() {
global $err;
return new Alternc_Api_Response( array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "[".$err->clsid."] ".$err->error) );
}
} // class Alternc_Api_Object_Account

View File

@ -3,30 +3,16 @@
/**
* Domain Api of AlternC, used by alternc-api package
*/
class Alternc_Api_Object_Domain {
class Alternc_Api_Object_Domain extends Alternc_Api_Legacyobject {
const ERR_INVALID_ARGUMENT = 1115401;
const ERR_ALTERNC_FUNCTION = 1115402;
var $admin; // m_admin instance
var $dom; // m_dom instance
var $cuid; // current user id
var $isAdmin; // is it an Admin account?
protected $dom; // m_dom instance
function __construct($service) {
global $admin,$cuid,$dom;
if (!($service instanceof Alternc_Api_Service)) {
throw new \Exception("Bad argument: service is not an Alternc_Api_Service", self::ERR_INVALID_ARGUMENT);
}
// We store the global $cuid to AlternC legacy classes
$this->cuid=$cuid=$service->token->uid;
$this->isAdmin=$service->token->isAdmin;
// We use the global $admin & $dom from AlternC legacy classes
$this->admin=$admin;
global $dom;
parent::__construct($service);
$this->dom=$dom;
// Set the legacy rights:
$this->admin->enabled=$this->isAdmin;
}
}
/** API Method from legacy class method dom->get_domain_list()
@ -159,13 +145,4 @@ class Alternc_Api_Object_Domain {
}
/** return a proper Alternc_Api_Response from an error class and error string
* from AlternC legacy class
*/
private function alterncLegacyErrorManager() {
global $err;
return new Alternc_Api_Response( array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "[".$err->clsid."] ".$err->error) );
}
} // class Alternc_Api_Object_Domain

View File

@ -0,0 +1,115 @@
<?php
/**
* Ftp Api of AlternC, used by alternc-api package
*/
class Alternc_Api_Object_Ftp extends Alternc_Api_Legacyobject {
protected $ftp; // m_ftp instance
function __construct($service) {
global $ftp;
parent::__construct($service);
$this->ftp=$ftp;
}
/** API Method from legacy class method ftp->add_ftp()
* @param $options a hash with parameters transmitted to legacy call
* mandatory parameters: prefix, login, pass, dir
* @return Alternc_Api_Response whose content is the newly created UID
*/
function add($options) {
$mandatory=array("prefix","login","pass","dir");
$missing="";
foreach ($mandatory as $key) {
if (!isset($options[$key])) {
$missing.=$key." ";
}
}
if ($missing) {
return new Alternc_Api_Response( array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: ".$missing) );
}
$ftpid=$this->ftp->add_ftp($options["prefix"],$options["login"], $options["pass"], $options["dir"]);
if (!$ftpid) {
return $this->alterncLegacyErrorManager();
} else {
return new Alternc_Api_Response( array("content" => $ftpid ) );
}
}
/** API Method from legacy class method ftp->put_ftp_details()
* @param $options a hash with parameters transmitted to legacy call
* mandatory parameters: id
* non-mandatory: prefix, login, pass, dir
* @return Alternc_Api_Response whose content is the updated UID
*/
function update($options) {
$defaults=array("prefix","login","pass","dir");
if (!isset($options["id"])) {
return new Alternc_Api_Response( array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: ID") );
}
$id=intval($options["id"]);
$old=$this->ftp->get_ftp_details($id);
if (!$old) {
return new Alternc_Api_Response( array("code" => self::ERR_NOT_FOUND, "message" => "FTP Account not found") );
}
foreach ($defaults as $key) {
if (!isset($options[$key])) {
$options[$key]=$old[$key];
}
}
if (!isset($options["pass"])) $options["pass"]="";
$result=$this->ftp->put_ftp_details($id, $options["prefixe"], $options["login"], $options["pass"], $options["dir"]);
if (!$result) {
return $this->alterncLegacyErrorManager();
} else {
return new Alternc_Api_Response( array("content" => $result ) );
}
}
/** API Method from legacy class method ftp->del_ftp()
* @param $options a hash with parameters transmitted to legacy call
* mandatory parameters: id
* @return Alternc_Api_Response TRUE if the FTP account has been deleted.
*/
function del($options) {
if (!isset($options["id"])) {
return new Alternc_Api_Response( array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: ID") );
}
$result=$this->admin->delete_ftp(intval($options["id"]));
if (!$result) {
return $this->alterncLegacyErrorManager();
} else {
return new Alternc_Api_Response( array("content" => true ) );
}
}
/** API Method from legacy class method ftp->get_list()
* @param $options a hash with parameters transmitted to legacy call
* non-mandatory parameters:
* Any of: offset(int=0), count(int=+inf)
* @return Alternc_Api_Response An array with all matching FTP account informations as hashes
*/
function find($options) {
$result=$this->ftp->get_list();
if (!$result) {
return $this->alterncLegacyErrorManager();
} else {
$offset=-1; $count=-1;
if (isset($options["count"])) $count=intval($options["count"]);
if (isset($options["offset"])) $offset=intval($options["offset"]);
if ($offset!=-1 || $count!=-1) {
if ($offset<0 || $offset>count($result)) $offset=0;
if ($count<0 || $count>1000) $count=1000;
$result= array_slice($result, $offset, $count);
}
return new Alternc_Api_Response( array("content" =>$result) );
}
}
} // class Alternc_Api_Object_Ftp