44 if (isset($options[
"databaseAdapter"]) && $options[
"databaseAdapter"] instanceof PDO) {
45 $this->db = $options[
"databaseAdapter"];
47 throw new \Exception(
"Missing required parameter databaseAdapter", self::ERR_INVALID_ARGUMENT);
51 $this->allowedAuth = array();
52 if (isset($options[
"loginAdapterList"]) && is_array($options[
"loginAdapterList"])) {
53 foreach ($options[
"loginAdapterList"] as $lal) {
54 $this->allowedAuth[] = (string) $lal;
59 if (isset($options[
"loggerAdapter"])) {
60 if (!is_array($options[
"loggerAdapter"]))
61 $options[
"loggerAdapter"] = array($options[
"loggerAdapter"]);
62 foreach ($options[
"loggerAdapter"] as $la) {
63 if ($la instanceof Psr\Log\LoggerInterface)
64 $this->loggerList[] = $la;
79 if (!isset($auth[
"method"]) || !is_string($auth[
"method"])) {
80 throw new \Exception(
"Missing required parameter method", self::ERR_INVALID_ARGUMENT);
82 if (!isset($auth[
"options"]) || !is_array($auth[
"options"])) {
83 throw new \Exception(
"Missing required parameter options", self::ERR_INVALID_ARGUMENT);
86 if (count($this->allowedAuth) && !in_array($auth[
"method"], $this->allowedAuth)) {
87 throw new \Exception(
"Method not allowed", self::ERR_METHOD_DENIED);
89 if (isset($auth[
"options"][
"uid"]) && !intval($auth[
"options"][
"uid"])) {
90 throw new \Exception(
"Invalid UID", self::ERR_INVALID_ARGUMENT);
93 $adapterName =
"Alternc_Api_Auth_" . ucfirst(strtolower($auth[
"method"]));
95 $authAdapter =
new $adapterName($this);
97 $token = $authAdapter->auth($auth[
"options"]);
104 throw new \Exception(
"Invalid answer from Api_Auth_Interface", self::ERR_INVALID_ANSWER);
106 if (isset($auth[
"options"][
"uid"])) {
109 return new Alternc_Api_Response(array(
"code" => self::ERR_SETUID_FORBIDDEN,
"message" =>
"This user is not allowed to set his uid"));
112 foreach ($this->db->query(
"SELECT uid FROM membres WHERE uid=" . intval($auth[
"options"][
"uid"])) as $setuid) {
113 $token->uid = intval($setuid[
'uid']);
114 $stmt = $this->db->prepare(
"UPDATE token SET data=? WHERE token=?");
118 return new Alternc_Api_Response(array(
"code" => self::ERR_SETUID_USER_NOT_FOUND,
"message" =>
"Can't find the user you want to setuid to"));
132 throw new \Exception(
"request must be an Alternc_Api_Request object", self::ERR_INVALID_ARGUMENT);
139 $className =
"Alternc_Api_Object_" . ucfirst(strtolower(
$request->object));
140 if (!class_exists($className))
141 return new Alternc_Api_Response(array(
"code" => self::ERR_OBJECT_NOT_FOUND,
"message" =>
"Object not found in this AlternC's instance"));
143 $object =
new $className($this);
147 if (strpos($action,
"-") !==
false) {
149 $action = lcfirst(str_replace(
" ",
"", implode(
"", array_map(
"ucfirst", explode(
"-", $action)))));
151 if (!method_exists($object, $action))
152 return new Alternc_Api_Response(array(
"code" => self::ERR_ACTION_NOT_FOUND,
"message" =>
"Action not found for this object in this AlternC's instance"));
156 return $object->$action(
$request->options);
166 if (substr($element, 0, 5) ==
"auth/") {
167 $adapterName =
"Alternc_Api_Auth_" . ucfirst(strtolower(substr($element, 5)));
168 if (!class_exists($adapterName))
170 $authAdapter =
new $adapterName($this);
171 return $authAdapter->documentation();
173 list($class, $action) = explode(
"/", $element);
174 $className =
"Alternc_Api_Object_" . ucfirst(strtolower($class));
175 if (!class_exists($className))
177 $object =
new $className($this);
179 return $authAdapter->documentation();
181 return $authAdapter->documentation($action);
Standard Request object for the AlternC API.
Standard Response object for the AlternC API.
Service API used by server to export API methods this class can be used to implement an API service /...
const ERR_ACTION_NOT_FOUND
const ERR_OBJECT_NOT_FOUND
getDb()
Getter for the databaseAdapter (used by authAdapter)
__construct($options)
Constructor of the Api Service Wrapper.
const ERR_SETUID_FORBIDDEN
const ERR_SETUID_USER_NOT_FOUND
call($request)
Manage an API Call.
const ERR_INVALID_ARGUMENT
auth($auth)
Authenticate into an AlternC server.
doc($element)
Return documentation of the API, either general (no parameters) or for a specific action or auth clas...
Standard Token object for the AlternC API.
static tokenGet($token, $db)
Check and return a token.