diff --git a/htdocs/api/class/api.class.php b/htdocs/api/class/api.class.php new file mode 100644 index 00000000000..298beb685d6 --- /dev/null +++ b/htdocs/api/class/api.class.php @@ -0,0 +1,90 @@ + + * + * 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 3 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, see . + */ + +use Luracast\Restler\Restler; + +/** + * Class for API + */ +class DolibarrApi { + + /** + * @var DoliDb $db Database object + */ + static protected $db; + + /** + * @var Restler $r Restler object + */ + var $r; + + /** + * Constructor + * + * @var DoliDB $db Database handler + */ + function __construct($db) { + $this->db = $db; + $this->r = new Restler(); + } + + /** + * Executed method when API is called without parameter + * + * Display a short message an return a http code 200 + * @return array + */ + function index() + { + return array( + 'success' => array( + 'code' => 200, + 'message' => __class__.' is up and running!' + ) + ); + } + + + /** + * Clean sensible object datas + * @var object $object Object to clean + * @return array Array of cleaned object properties + * + * @todo use an array for properties to clean + * + */ + protected function cleanObjectDatas($object){ + + unset($object->db); + + return array($object); + } + +} + +/** + * API init + * This class exists to show 200 code when request url root /api/ + * + */ +class DolibarrApiInit extends DolibarrApi { + + function __construct() { + + } + +} \ No newline at end of file diff --git a/htdocs/api/index.php b/htdocs/api/index.php new file mode 100644 index 00000000000..3ac00faa65f --- /dev/null +++ b/htdocs/api/index.php @@ -0,0 +1,8 @@ + + * + * 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 3 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, see . + */ + +/** + * \defgroup api Module DolibarrApi + * \brief API loader + * Search files htdocs//class/api_.class.php + * \file htdocs/api/indexphp + * + * @todo User authentication with api_key + * + * + */ +if (! defined("NOLOGIN")) define("NOLOGIN",'1'); + +$res=0; +if (! $res && file_exists("../../main.inc.php")) $res=@include '../../main.inc.php'; +if (! $res && file_exists("../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../dolibarr/htdocs/main.inc.php'; // For custom directory +if (! $res) die("Include of main fails"); + +require_once DOL_DOCUMENT_ROOT.'/includes/restler/vendor/autoload.php'; +require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; + +$api = new DolibarrApi($db); + +$api->r->setSupportedFormats('JsonFormat', 'XmlFormat'); +$api->r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root +$api->r->addAPIClass('DolibarrApiInit',''); // Just for url root page + +$modulesdir = dolGetModulesDirs(); +foreach ($modulesdir as $dir) +{ + /* + * Search available module + */ + dol_syslog("Scan directory ".$dir." for API modules"); + + $handle=@opendir(dol_osencode($dir)); + if (is_resource($handle)) + { + while (($file = readdir($handle))!==false) + { + if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg)) + { + $modulename=$reg[1]; + + // Defined if module is enabled + $enabled=true; + $part=$obj=strtolower(preg_replace('/^mod/i','',$modulename)); + //if ($part == 'propale') $part='propal'; + if ($part == 'societe') { + $obj = 'thirdparty'; + } + if (empty($conf->$part->enabled)) $enabled=false; + + if ($enabled) + { + /* + * If exists, load the API class for enable module + * + * Search a file api_.class.php into /htdocs//class directory + * + * @todo : take care of externals module! + * @todo : use getElementProperties() function + */ + $file = DOL_DOCUMENT_ROOT.'/'.$part."/class/api_".$obj.".class.php"; + + $classname = ucwords($obj).'Api'; + if (file_exists($file)) + { + require_once $file; + $api->r->addAPIClass($classname,''); + } + } + } + } + } +} + +$api->r->handle(); //serve the response \ No newline at end of file diff --git a/htdocs/societe/class/api_thirdparty.class.php b/htdocs/societe/class/api_thirdparty.class.php new file mode 100644 index 00000000000..bd5581ef6a5 --- /dev/null +++ b/htdocs/societe/class/api_thirdparty.class.php @@ -0,0 +1,143 @@ + + * + * 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 3 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, see . + */ + + use Luracast\Restler\RestException; + + +/** + * + * API class for thirdparty object + * + * @smart-auto-routing false + * + */ +class ThirdpartyApi extends DolibarrApi { + + static $FIELDS = array( + 'name', + 'email' + ); + + /** + * @var Societe $company {@type Societe} + */ + public $company; + + /** + * Constructor + * + * @url thirdparty/ + * + */ + function __construct() + { + global $db; + $this->db = $db; + $this->company = new Societe($this->db); + } + + /** + * Get properties of a thirdparty object + * + * Return an array with thirdparty informations + * + * @url GET thirdparty/{id} + * @param int $id ID of thirdparty + * @return array|mixed data without useless information + * @throws RestException + */ + function get($id) + { + $result = $this->company->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + } + + return $this->cleanObjectDatas($this->company); + } + + /** + * Fetch a list of thirdparties + * + * @url GET /thirdparties/list + * + * @return array Array of thirdparty objects + */ + function getList() { + + $result = $this->company->fetch_all($id); + if( ! $result ) { + throw new RestException(404, 'Thirdparties not found'); + } + + return $this->cleanObjectDatas($this->company->lines); + + } + /** + * Create thirdparty object + * + * @url POST thirdparty/ + * @param type $request_data + * @return type + */ + function post($request_data = NULL) + { + return $this->company->create($this->_validate($request_data)); + } + + /** + * Update thirdparty + * + * @url PUT thirdparty/{id} + * @param type $id + * @param type $request_data + * @return type$this->company + */ + function put($id, $request_data = NULL) + { + return $this->company->update($id, $this->_validate($request_data)); + } + + /** + * Delete thirdparty + * + * @url DELETE thirdparty/{id} + * @param type $id + * @return type + */ + function delete($id) + { + return $this->company->delete($id); + } + + /** + * Validate fields before create or update object + * @param type $data + * @return array + * @throws RestException + */ + private function _validate($data) + { + $thirdparty = array(); + foreach (ThirdpartyApi::$FIELDS as $field) { + if (!isset($data[$field])) + throw new RestException(400, "$field field missing"); + $thirdparty[$field] = $data[$field]; + } + return $thirdparty; + } +}