index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * Copyright (c) 2016, 2024, 5 Mode
  4. * All rights reserved.
  5. *
  6. * This file is part of ApiGrave.
  7. *
  8. * ApiGrave is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * ApiGrave is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with ApiGrave. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. * index.php
  22. *
  23. * The index file.
  24. *
  25. * @author Daniele Bonini <my25mb@aol.com>
  26. * @copyrights (c) 2016, 2024, the Open Gallery's contributors
  27. * @license https://opensource.org/licenses/BSD-3-Clause
  28. */
  29. require "../Private/core/init.inc";
  30. use fivemode\fivemode\Cache;
  31. // FUNCTION AND VARIABLE DECLARATIONS
  32. //$scriptPath = APP_SCRIPT_PATH;
  33. $env = [];
  34. $methods = [];
  35. function eval_ex_handler(Throwable $exception) {
  36. global $methods;
  37. global $url;
  38. echo("<h1>This is a bit of doc for $url:</h1>");
  39. echo("<br><br>");
  40. echo($methods[$url]["doc"]);
  41. }
  42. // READING METHODS FROM THE API PATH
  43. // Reading from the cache first
  44. $cache = &Cache::getInstance();
  45. $cacheKey = md5("CALL spGetMethods();");
  46. $methods = false; //$cache->getJ($cacheKey);
  47. if (!$methods) {
  48. chdir(APP_API_PATH);
  49. $pattern = "*.inc";
  50. $apiPaths = glob($pattern);
  51. //print_r($apiPaths);
  52. $eni=0;
  53. foreach($apiPaths as $apiPath) {
  54. $ipos = strripos($apiPath, PHP_SLASH);
  55. if ($ipos !== false) {
  56. $apiPath = substr($apiPath, $ipos);
  57. }
  58. $apiPath = APP_API_PATH . DIRECTORY_SEPARATOR . $apiPath;
  59. $apistr = file_get_contents($apiPath);
  60. // Parsing for the api namespace..
  61. $matches = [];
  62. $regstr = '/namespace\s(?<namespace>[\w\-\\\]+)\;/';
  63. if (preg_match_all($regstr, $apistr, $matches, PREG_SET_ORDER) !== false) {
  64. $env[$eni]['namespace'] = (string)$matches[0]["namespace"];
  65. }
  66. if (empty($env[$eni]['namespace'])) {
  67. $env[$eni]['namespace'] = "-";
  68. }
  69. // Parsing for the api classname..
  70. $matches = [];
  71. $regstr = '/class\s(?<classname>[\w\-]+)\s/';
  72. if (preg_match_all($regstr, $apistr, $matches, PREG_SET_ORDER) !== false) {
  73. $env[$eni]['classname'] = (string)$matches[0]["classname"];
  74. }
  75. if (empty($env[$eni]['classname'])) {
  76. $env[$eni]['classname'] = "-";
  77. }
  78. //echo("namespace=".$env[$eni]['namespace']."<br>");
  79. //echo("classname=".$env[$eni]['classname']."<br>");
  80. $matches = [];
  81. $regstr = '/(?<func_header>(?<visibility>private|public)?\s?(?<func_modifier>static)?\s?function\s(?<name>\&?[\w\-]{2,25})\((?<param_defs>(?<param_def1>\s?(?<optional_flag1>\?)?(?<param_type1>[a-z]{3,8})?\s?(?<reference_flag1>[\&]{0,1})?(?<param_name1>\$[\w\-]{1,20})\s?(?<default_value1>\=\s?[\w\-\']{1,128})?\s?\,?\s?)?(?<param_def2>\s?(?<optional_flag2>\?)?(?<param_type2>[a-z]{3,8})?\s?(?<reference_flag2>\&)?(?<param_name2>\$[\w\-]{1,20})\s?(?<default_value2>\=\s?[\w\-\']{1,128})?\s?\,?\s?)?(?<param_def3>\s?(?<optional_flag3>\?)?(?<param_type3>[a-z]{3,8})?\s?(?<reference_flag3>\&)?(?<param_name3>\$[\w\-]{1,20})\s?(?<default_value3>\=\s?[\w\-\']{1,128})?\s?\,?\s?)?(?<param_def4>\s?(?<optional_flag4>\?)?(?<param_type4>[a-z]{3,8})?\s?(?<reference_flag4>\&)?(?<param_name4>\$[\w\-]{1,20})\s?(?<default_value4>\=\s?[\w\-\']{1,128})?\s?\,?\s?)?(?<param_def5>\s?(?<optional_flag5>\?)?(?<param_type5>[a-z]{3,8})?\s?(?<reference_flag5>\&)?(?<param_name5>\$[\w\-]{1,20})\s?(?<default_value5>\=\s?[\w\-\']{1,128})?\s?\,?\s?)?)?\)?\:?\s?(?<return_type>[\w\-]{2,25})?)/';
  82. if (preg_match_all($regstr, $apistr, $matches, PREG_SET_ORDER) !== false) {
  83. foreach($matches as $match) {
  84. $method1=[];
  85. if ($match["visibility"] === "public" || $env[$eni]['namespace'] === "-") {
  86. //var_dump_ifdebug(true, $match);
  87. if ($match["param_defs"]!="") {
  88. $method1['name'] = $match["name"];
  89. echo_ifdebug(true, "debug: <b>".$match["name"]."(</b>");
  90. $method1['doc'] = $match["name"]."(";
  91. for($i=1;$i<=5;$i++) {
  92. if (!empty($match["param_name".$i])) {
  93. $param1 = [];
  94. $param1['name'] = $match["param_name".$i];
  95. $param1['type'] = "variant";
  96. $param1['optional'] = false;
  97. if ($i > 1) {
  98. echo_ifdebug(true, ",&nbsp;");
  99. $method1['doc'].=", ";
  100. }
  101. echo_ifdebug(true, $match["param_name".$i]." (");
  102. $method1['doc'].=$match["param_name".$i]." (";
  103. if (empty($match["param_type".$i])) {
  104. echo_ifdebug(true, "variant");
  105. $method1['doc'].="variant";
  106. } else {
  107. $param1['type'] = $match["param_type".$i];
  108. echo_ifdebug(true, $match["param_type".$i]);
  109. $method1['doc'].=$match["param_type".$i];
  110. }
  111. if ($match["optional_flag".$i] === "?") {
  112. $param1['optional'] = true;
  113. echo_ifdebug(true, ", optional)");
  114. $method1['doc'].=", optional)";
  115. } else {
  116. echo_ifdebug(true, ")");
  117. $method1['doc'].=")";
  118. }
  119. $method1['params'][] = $param1;
  120. } else {
  121. continue;
  122. }
  123. }
  124. if (!isset($match["return_type"])) {
  125. $method1['return_type'] = "variant";
  126. } else {
  127. $method1['return_type'] = $match["return_type"];
  128. }
  129. $method1['doc'].="):".$method1['return_type'];
  130. $method1['namespace'] = $env[$eni]['namespace'];
  131. $method1['classname'] = $env[$eni]['classname'];
  132. $methods[ltrim($match["name"],'&')] = $method1;
  133. echo_ifdebug(true, "<b>):".$method1['return_type']."</b><br>");
  134. } else {
  135. $method1['name'] = $match["name"];
  136. $param1 = [];
  137. $method1['params'] = $param1;
  138. if (!isset($match["return_type"])) {
  139. $method1['return_type'] = "variant";
  140. } else {
  141. $method1['return_type'] = $match["return_type"];
  142. }
  143. $method1['doc'] = $match["name"]."():".$method1['return_type'];
  144. $method1['namespace'] = $env[$eni]['namespace'];
  145. $method1['classname'] = $env[$eni]['classname'];
  146. $methods[ltrim($match["name"],'&')] = $method1;
  147. echo_ifdebug(true, "debug: <b>".$match["name"]."():".$method1['return_type']."</b>"."<br>");
  148. }
  149. }
  150. }
  151. //$methods[] = $method1;
  152. //var_dump_ifdebug(true, $methods);
  153. // LOADING METHODS
  154. //var_dump_ifdebug(true, $matches);
  155. //exit(-1);
  156. }
  157. $eni++;
  158. }
  159. }
  160. //exit(-1);
  161. if (empty($methods)) {
  162. $methods = [];
  163. }
  164. // Caching the array just loaded
  165. $cache->setJ($cacheKey, $methods, 0, CACHE_EXPIRE);
  166. // PARAMETERS VALIDATION
  167. $url = trim(substr(filter_input(INPUT_GET, "url", FILTER_SANITIZE_STRING), 0, 300), "/");
  168. /*
  169. switch ($url) {
  170. case "action":
  171. $scriptPath = APP_AJAX_PATH;
  172. define("SCRIPT_NAME", "action");
  173. define("SCRIPT_FILENAME", "action.php");
  174. break;
  175. case "method":
  176. define("SCRIPT_NAME", "mymethod");
  177. define("SCRIPT_FILENAME", "mymethod.php");
  178. break;
  179. default:
  180. $scriptPath = APP_ERROR_PATH;
  181. define("SCRIPT_NAME", "err-404");
  182. define("SCRIPT_FILENAME", "err-404.php");
  183. }
  184. */
  185. //print_r($methods);
  186. echo_ifdebug(true, "<br>");
  187. if (isset($methods[$url])) {
  188. $userMethod = $url;
  189. if ($methods[$url]['namespace']==="-" || $methods[$url]['classname'] ==="-") {
  190. $cmd = 'return '.$url.'(';
  191. } else {
  192. $cmd = 'return '.$methods[$url]['namespace'].'\\'.$methods[$url]['classname'].'::'.$url.'(';
  193. }
  194. //print_r($methods[$url]["params"]);
  195. $i=0;
  196. foreach($methods[$url]["params"] as $param) {
  197. $userParams[$i] = filter_input(INPUT_GET, $param['name'], FILTER_SANITIZE_STRING);
  198. //print_r($userParams[$i]);
  199. if ($param['type']==="string" && !empty($userParams[$i])) {
  200. $cmd .= "'$userParams[$i]',";
  201. } else {
  202. $cmd .= "$userParams[$i],";
  203. }
  204. $i++;
  205. }
  206. $cmd=rtrim($cmd,",");
  207. $cmd .= ");";
  208. //echo("cmd=$cmd");
  209. set_exception_handler('eval_ex_handler');
  210. $ret = eval($cmd);
  211. if ($methods[$url]["return_type"]==="bool") {
  212. echo(($ret?"true":"false"));
  213. } else {
  214. echo($ret);
  215. }
  216. } else if ($url === "XMLDOC") {
  217. if (!DEBUG) {
  218. header("Content-Type: text/xml");
  219. //print_r($env);
  220. echo('<?xml version="1.0" encoding="utf-8"?>');
  221. echo('<API>');
  222. foreach($env as $e) {
  223. $curnamespace = $e['namespace'];
  224. $curclassname = $e['classname'];
  225. foreach($methods as $m) {
  226. //echo($m['namespace']."===".$curnamespace."<br>");
  227. //echo($m['classname']."===".$curclassname."<br>");
  228. if (($m['namespace'] === $curnamespace) && ($m['classname'] === $curclassname)) {
  229. echo("<METHOD>");
  230. echo('<NAMESPACE>'.$m['namespace'].'</NAMESPACE>');
  231. echo('<CLASSNAME>'.$m['classname'].'</CLASSNAME>');
  232. echo('<NAME>'.$m['name'].'</NAME>');
  233. $pi=1;
  234. foreach($m['params'] as $p) {
  235. echo("<PARAM$pi type='".$p['type']."' optional='".($p['optional']==1?"true":"false")."'>".$p['name']."</PARAM$pi>");
  236. $pi++;
  237. }
  238. echo('<RETURN_TYPE>'.$m['return_type'].'</RETURN_TYPE>');
  239. echo('<HEADER>'.$m['doc'].'</HEADER>');
  240. echo("</METHOD>");
  241. }
  242. }
  243. }
  244. echo('</API>');
  245. }
  246. } else {
  247. $scriptPath = APP_ERROR_PATH;
  248. define("SCRIPT_NAME", "err-404");
  249. define("SCRIPT_FILENAME", "err-404.php");
  250. if (SCRIPT_NAME==="err-404") {
  251. header("HTTP/1.1 404 Not Found");
  252. }
  253. require $scriptPath . "/" . SCRIPT_FILENAME;
  254. }