17 if (!defined(
"PSF_ENTRY_POINT"))
18 die(
"Not a valid psf entry point");
20 require_once (dirname(__FILE__) .
"/../object.php");
21 require_once (dirname(__FILE__) .
"/../html_stack.php");
32 public static function ToString($type)
34 if ($type === PsfApiParameterType::NULL)
36 if ($type === PsfApiParameterType::Variant)
38 if ($type === PsfApiParameterType::Number)
40 if ($type === PsfApiParameterType::String)
42 if ($type === PsfApiParameterType::Boolean)
44 return "invalid type";
55 public function __construct($_name, $_type = PsfApiParameterType::Variant, $_description = NULL)
59 $this->Description = $_description;
69 public $ParametersRequired = [];
70 public $ParametersOptional = [];
71 public $ShortDescription = NULL;
72 public $LongDescription = NULL;
73 public $Example = NULL;
74 public $Callback = NULL;
75 public $RequiresAuthentication =
false;
78 public $POSTOnly =
false;
80 public function __construct($_name, $_callback = NULL, $short_description = NULL, $long_description = NULL, $params_req = NULL, $params_opt = NULL)
83 $this->LongDescription = $long_description;
84 $this->ShortDescription = $short_description;
85 $this->Callback = $_callback;
86 if ($params_req !== NULL)
87 $this->ParametersRequired = $params_req;
88 if ($params_opt !== NULL)
89 $this->ParametersOptional = $params_opt;
92 public function Process()
94 if ($this->Callback !== NULL)
96 return call_user_func($this->Callback, $this);
101 public function GetParameterCount()
103 return count($this->ParametersOptional) + count($this->ParametersRequired);
109 public $ApiList_GET = [];
110 public $ApiList_POST = [];
111 public $ApiList_DELETE = [];
112 public $ApiList_PUT = [];
113 public $ApiList_Action = [];
114 public $ShowHelpOnNoAction =
true;
115 public $ApiBaseName =
"API";
116 public $ApiBaseIntro =
"Welcome to web API. These APIs can be used to perform various actions on the website.";
117 public $TreatDocsAsHTML =
false;
119 public $ExamplePrefix =
"";
121 public $AuthenticationBackend = NULL;
123 public function RegisterAPI_Action($api, $name = NULL)
127 $name = strtolower($name);
128 $api->Parent = $this;
129 $this->ApiList_Action[$name] = $api;
132 public function RegisterAPI_GET($api)
134 $api->Parent = $this;
135 array_push ($this->ApiList_GET, $api);
138 public function RegisterAPI_PUT($api)
140 $api->Parent = $this;
141 array_push ($this->ApiList_PUT, $api);
144 public function RegisterAPI_POST($api)
146 $api->Parent = $this;
147 array_push ($this->ApiList_POST, $api);
150 public function RegisterAPI_DELETE($api)
152 $api->Parent = $this;
153 array_push ($this->ApiList_DELETE, $api);
156 public function ProcessAction()
159 if (isset($_GET[
'action']))
162 $action = strtolower($_GET[
'action']);
163 }
else if (isset($_POST[
'action']))
166 $action = strtolower($_POST[
'action']);
172 if (!array_key_exists($action, $this->ApiList_Action))
175 if ($method !==
'POST' && $this->ApiList_Action[$action]->POSTOnly)
176 $this->ThrowError(
"POST only",
"This call must be submitted via POST method", 403);
178 if ($this->ApiList_Action[$action]->RequiresAuthentication && !$this->AuthenticationBackend->IsAuthenticated())
179 $this->ThrowError(
"Not authenticated",
"This call requires authentication, but you are not logged in", 403);
181 return $this->ApiList_Action[$action]->Process();
184 public function ProcessGET()
189 public function ProcessPOST()
194 public function ProcessDELETE()
199 public function ProcessPUT()
204 public function Process()
207 if ($this->ProcessAction() ||
208 $this->ProcessGET() ||
209 $this->ProcessPOST() ||
210 $this->ProcessDELETE() ||
214 if ($this->ShowHelpOnNoAction && !$result)
215 $this->PrintHelpAsHtml();
218 private function appendDocs($c, $t)
220 if (!$this->TreatDocsAsHTML)
221 $c->AppendParagraph($t);
226 public function ThrowError($error, $message = NULL, $code = -1)
228 print(
"Error: " . $error .
"\n");
229 if ($message !== NULL)
230 print(
"Details: " . $message .
"\n");
236 public function PrintHelpAsHtml()
238 global $psf_containers_auto_insert_child;
239 $def_psf_containers_auto_insert_child = $psf_containers_auto_insert_child;
240 $psf_containers_auto_insert_child =
true;
241 $help =
new HtmlPage($this->ApiBaseName .
": help");
242 bootstrap_init($help);
243 $help->Style->items[
"blockquote"][
"font-size"] =
"14px";
245 $c->AppendHeader($this->ApiBaseName .
" documentation");
246 $this->appendDocs($c, $this->ApiBaseIntro);
248 if (!empty($this->ApiList_Action))
250 $c->AppendHeader(
"Action APIs", 2);
251 $c->AppendHtmlLine(
'<p>These APIs can be called using standard GET or POST web request with parameter <code>action</code> (for example: <code>' . $this->ExamplePrefix .
'?action=test</code>) where action is one of these:</p>');
252 foreach ($this->ApiList_Action as $key => $value)
254 $c->AppendHeader($key, 3);
255 if ($value->ShortDescription !== NULL)
256 $this->appendDocs($c, $value->ShortDescription);
257 $c->AppendHtmlLine(
'<blockquote>');
258 if ($value->RequiresAuthentication)
259 $c->AppendHtmlLine(
'<p class="text-danger"><span class="glyphicon glyphicon-exclamation-sign"></span> This action requires authentication</p>');
260 if ($value->POSTOnly)
261 $c->AppendHtmlLine(
'<p class="text-danger"><span class="glyphicon glyphicon-exclamation-sign"></span> This action must be called via POST method</p>');
262 if ($value->GetParameterCount() === 0)
264 $c->AppendParagraph(
"This action has no parameters",
"text-info");
267 if (count($value->ParametersRequired) > 0)
269 $c->AppendHeader(
"Parameters (required)", 4);
270 foreach ($value->ParametersRequired as $reqp)
272 $c->AppendHtmlLine(
'<p><b>' . $reqp->Name .
'</b> (<code>' . PsfApiParameterType::ToString($reqp->Type) .
'</code>): ' . $reqp->Description .
'</p>');
275 if (count($value->ParametersOptional) > 0)
277 $c->AppendHeader(
"Parameters (optional)", 4);
278 foreach ($value->ParametersOptional as $optp)
280 $c->AppendHtmlLine(
'<p><b>' . $optp->Name .
'</b> (<code>' . PsfApiParameterType::ToString($optp->Type) .
'</code>): ' . $optp->Description .
'</p>');
284 if ($value->LongDescription !== NULL)
285 $this->appendDocs($c, $value->LongDescription);
286 if ($value->Example !== NULL)
287 $c->AppendHtmlLine(
'<p><b>Example:</b> <code>' . htmlspecialchars($this->ExamplePrefix . $value->Example) .
'</code></p>');
288 $c->AppendHtmlLine(
'</blockquote>');
292 $psf_containers_auto_insert_child = $def_psf_containers_auto_insert_child;
Represent a single Html page.
Base class used for every single PSF object.
This class represents a single API PSF supports quite flexible API framework which can be used to cre...
Data types supported by API parameters.