Alternc  latest
Alternc logiel libre pour l'hébergement
index.php File Reference

Go to the source code of this file.

Functions

 __autoload ($class_name)
 multiple call-mode API for Alternc you can call this HTTP(s) API as follow: from the base url https://panel.example.fr/api/ More...
 
 apicall ($data, $token)
 
 apiauth ($data)
 

Variables

if(preg_match("#^/api/auth/( if [^/\?] *)[/\?]?#", $_SERVER["REQUEST_URI"], $mat)) if ( $_SERVER["REQUEST_URI"]=="/api/post")(preg_match("#^/api/rest/([^/] *)/([^/\?] *)[/\?]?#", $_SERVER["REQUEST_URI"], $mat))
 Main code: either we are authenticating or calling one of the APIs or asking for some documentation. More...
 

Function Documentation

◆ __autoload()

__autoload (   $class_name)

multiple call-mode API for Alternc you can call this HTTP(s) API as follow: from the base url https://panel.example.fr/api/

  1. /api/post use GETted data (?token=xx&object=xx&action=yy&option1=value1&option2=value2
  2. /api/post use POSTED json data using the same keys
  3. use a sub-url (rest-style) of the form /api/rest/object/action?token=xx&option1=value1&option2=value2
  4. the same (REST) but options and value are POSTED

the json-object contains: ->object = the Alternc_Api_Object_<classname> to call ->action = the method to call in this class ->options = an object passed as it is while calling the method.

Authentication is done by asking for /api/auth/<method>?option1=value1&option2=value2 or POSTED data a token is returned for this session Use /api/auth to know which method you can use and what parameter they expect

Todo:
add HTML pages that will self-document this API

Attempts to load a class in multiple path, the PSR-0 or old style way

@staticvar array $srcPathList @staticvar boolean $init

Parameters
string$class_name
Returns
boolean

Definition at line 34 of file index.php.

34  {
35  // Contains (Namespace) => directory
36  static $srcPathList = array();
37  static $init = null;
38 
39  // Attempts to set include path and directories once
40  if (is_null($init)) {
41 
42  // Sets init flag
43  $init = true;
44 
45  // Sets a contextual directory
46  $srcPathList["standard"] = "/usr/share/php";
47 
48  // Updates include_path according to this list
49  $includePathList = explode(PATH_SEPARATOR, get_include_path());
50 
51  foreach ($srcPathList as $path) {
52  if (!in_array($path, $includePathList)) {
53  $includePathList[] = $path;
54  }
55  }
56  // Reverses the path for search efficiency
57  $finalIncludePathList = array_reverse($includePathList);
58 
59  // Sets the updated include_path
60  set_include_path(implode(PATH_SEPARATOR, $finalIncludePathList));
61  }
62 
63  // Accepts old Foo_Bar namespacing
64  if (preg_match("/_/", $class_name)) {
65  $file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
66 
67  // Accepts 5.3 Foo\Bar PSR-0 namespacing
68  } else if (preg_match("/\\/", $class_name)) {
69  $file_name = str_replace('\\', DIRECTORY_SEPARATOR, ltrim($class_name, '\\')) . '.php';
70 
71  // Accepts non namespaced classes
72  } else {
73  $file_name = $class_name . '.php';
74  }
75 
76  // Attempts to find file in namespace
77  foreach ($srcPathList as $namespace => $path) {
78  $file_path = $path . DIRECTORY_SEPARATOR . $file_name;
79  if (is_file($file_path) && is_readable($file_path)) {
80  require $file_path;
81  return true;
82  }
83  }
84 
85  // Failed to find file
86  return false;
87 }

◆ apiauth()

apiauth (   $data)

Definition at line 115 of file index.php.

115  {
116  global $dbh;
117  $options["databaseAdapter"] = $dbh;
118  // TODO (no loggerAdapter PSR3-Interface-compliant class as of now)
119  try {
120  $service = new Alternc_Api_Service($options);
121  $response = $service->auth($data);
122  header("Content-Type: application/json");
123  echo $response->toJson();
124  exit();
125  } catch (Exception $e) {
126  // something went wrong, we spit out the exception as an Api_Response
127  // TODO : Don't do that on production! spit out a generic "fatal error" code and LOG the exception !
128  header("Content-Type: application/json");
129  $response = new Alternc_Api_Response(array("code" => $e->code, "message" => $e->message));
130  echo $response->toJson();
131  exit();
132  }
133 }
134 
135 /**
136  * Main code: either we are authenticating
137  * or calling one of the APIs
138  * or asking for some documentation
139  */
140 // Authentication
141 if (preg_match("#^/api/auth/([^/\?]*)[/\?]?#", $_SERVER["REQUEST_URI"], $mat)) {
142  if ($_SERVER["REQUEST_METHOD"] == "POST") {
143  $data = array("options" => $_POST,
144  "method" => $mat[1]);
145  apiauth($data);
146  exit();
147  } else {
148  $data = array("options" => $_GET,
149  "method" => $mat[1]);
150  apiauth($data);
151  exit();
152  }
153 }
154 
155 // We support 4 api calls methods:
156 if ($_SERVER["REQUEST_URI"] == "/api/post") {
157  // simple ?q or POST of json data
158  if ($_SERVER["REQUEST_METHOD"] == "POST") {
159  $data = array("options" => $_POST,
160  "object" => $_POST["object"],
161  "action" => $_POST["action"],
162  );
163  $token = $_POST["token"];
164  apicall($data, $token);
165  exit();
166  } else {
167  $data = array("options" => $_GET,
168  "object" => $_GET["object"],
169  "action" => $_GET["action"],
170  );
171  $token = $_GET["token"];
172  apicall($data, $token);
173  exit();
174  }
175 }
176 if (preg_match("#^/api/rest/([^/]*)/([^/\?]*)[/\?]?#", $_SERVER["REQUEST_URI"], $mat)) {
177  if ($_SERVER["REQUEST_METHOD"] == "POST") {
178  $data = array("options" => $_POST,
179  "object" => $mat[1],
180  "action" => $mat[2]
181  );
182  $token = $_POST["token"];
183  apicall($data, $token);
184  exit();
185  } else {
186  $data = array("options" => $_GET,
187  "object" => $mat[1],
188  "action" => $mat[2]
189  );
190  $token = $_GET["token"];
191  apicall($data, $token);
192  exit();
193  }
194 }
exit
Definition: adm_doadd.php:70
global $dbh
Definition: bootstrap.php:26
apiauth($data)
Definition: index.php:115
apicall($data, $token)
Definition: index.php:89
Standard Response object for the AlternC API.
Definition: Response.php:7
Service API used by server to export API methods this class can be used to implement an API service /...
Definition: Service.php:11

References $dbh, and exit.

◆ apicall()

apicall (   $data,
  $token 
)

Definition at line 89 of file index.php.

89  {
90  global $dbh;
91  $options["databaseAdapter"] = $dbh;
92  $options["loginAdapterList"] = array("sharedsecret", "login");
93  // TODO (no loggerAdapter PSR3-Interface-compliant class as of now)
94  try {
95  $data["token_hash"] = $token;
96  $service = new Alternc_Api_Service($options);
97 
98  $response = $service->call(
99  new Alternc_Api_Request($data)
100  );
101 
102  header("Content-Type: application/json");
103  echo $response->toJson();
104  exit();
105  } catch (Exception $e) {
106  // something went wrong, we spit out the exception as an Api_Response
107  // TODO : Don't do that on production! spit out a generic "fatal error" code and LOG the exception !
108  header("Content-Type: application/json");
109  $response = new Alternc_Api_Response(array("code" => $e->getCode(), "message" => $e->getMessage()));
110  echo $response->toJson();
111  exit();
112  }
113 }
Standard Request object for the AlternC API.
Definition: Request.php:8

References $dbh, and exit.

Variable Documentation

◆ if

if ( preg_match("#^/api/rest/([^/]*)/([^/\?]*)[/\?]?#", $_SERVER["REQUEST_URI"], $mat)  $oneuser_ok) )[/\?]
Initial value:
{
__("No users currently defined, you must create login with the 'Manage allowed users' accounts' menu.")
__($str)
Definition: functions.php:404

Main code: either we are authenticating or calling one of the APIs or asking for some documentation.

Definition at line 176 of file index.php.