index.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. $eni=0;
  52. foreach($apiPaths as $apiPath) {
  53. $ipos = strripos($apiPath, PHP_SLASH);
  54. if ($ipos !== false) {
  55. $apiPath = substr($apiPath, $ipos);
  56. }
  57. $apiPath = APP_API_PATH . DIRECTORY_SEPARATOR . $apiPath;
  58. $apistr = file_get_contents($apiPath);
  59. // Parsing for the api namespace..
  60. $matches = [];
  61. $regstr = '/namespace\s(?<namespace>[\w\-\\\]+)\;/';
  62. if (preg_match_all($regstr, $apistr, $matches, PREG_SET_ORDER) !== false) {
  63. $env[$eni]['namespace'] = $matches[0]["namespace"];
  64. }
  65. // Parsing for the api classname..
  66. $matches = [];
  67. $regstr = '/class\s(?<classname>[\w\-]+)\s/';
  68. if (preg_match_all($regstr, $apistr, $matches, PREG_SET_ORDER) !== false) {
  69. $env[$eni]['classname'] = $matches[0]["classname"];
  70. }
  71. $matches = [];
  72. $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})?)/';
  73. if (preg_match_all($regstr, $apistr, $matches, PREG_SET_ORDER) !== false) {
  74. foreach($matches as $match) {
  75. $method1=[];
  76. if ($match["visibility"] === "public") {
  77. //var_dump_ifdebug(true, $match);
  78. if ($match["param_defs"]!="") {
  79. $method1['name'] = $match["name"];
  80. echo_ifdebug(true, "debug: <b>".$match["name"]."(</b>");
  81. $method1['doc'] = $match["name"]."(";
  82. for($i=1;$i<=5;$i++) {
  83. if (!empty($match["param_name".$i])) {
  84. $param1 = [];
  85. $param1['name'] = $match["param_name".$i];
  86. $param1['type'] = "variant";
  87. $param1['optional'] = false;
  88. if ($i > 1) {
  89. echo_ifdebug(true, ",&nbsp;");
  90. $method1['doc'].=", ";
  91. }
  92. echo_ifdebug(true, $match["param_name".$i]." (");
  93. $method1['doc'].=$match["param_name".$i]." (";
  94. if (empty($match["param_type".$i])) {
  95. echo_ifdebug(true, "variant");
  96. $method1['doc'].="variant";
  97. } else {
  98. $param1['type'] = $match["param_type".$i];
  99. echo_ifdebug(true, $match["param_type".$i]);
  100. $method1['doc'].=$match["param_type".$i];
  101. }
  102. if ($match["optional_flag".$i] === "?") {
  103. $param1['optional'] = true;
  104. echo_ifdebug(true, ", optional)");
  105. $method1['doc'].=", optional)";
  106. } else {
  107. echo_ifdebug(true, ")");
  108. $method1['doc'].=")";
  109. }
  110. $method1['params'][] = $param1;
  111. } else {
  112. continue;
  113. }
  114. }
  115. if (!isset($match["return_type"])) {
  116. $method1['return_type'] = "variant";
  117. } else {
  118. $method1['return_type'] = $match["return_type"];
  119. }
  120. $method1['doc'].="):".$method1['return_type'];
  121. $method1['namespace'] = $env[$eni]['namespace'];
  122. $method1['classname'] = $env[$eni]['classname'];
  123. $methods[ltrim($match["name"],'&')] = $method1;
  124. echo_ifdebug(true, "<b>):".$method1['return_type']."</b><br>");
  125. } else {
  126. $method1['name'] = $match["name"];
  127. $param1 = [];
  128. $method1['params'] = $param1;
  129. if (!isset($match["return_type"])) {
  130. $method1['return_type'] = "variant";
  131. } else {
  132. $method1['return_type'] = $match["return_type"];
  133. }
  134. $method1['doc'] = $match["name"]."():".$method1['return_type'];
  135. $method1['namespace'] = $env[$eni]['namespace'];
  136. $method1['classname'] = $env[$eni]['classname'];
  137. $methods[ltrim($match["name"],'&')] = $method1;
  138. echo_ifdebug(true, "debug: <b>".$match["name"]."():".$method1['return_type']."</b>"."<br>");
  139. }
  140. }
  141. }
  142. //$methods[] = $method1;
  143. //var_dump_ifdebug(true, $methods);
  144. // LOADING METHODS
  145. //var_dump_ifdebug(true, $matches);
  146. //exit(-1);
  147. }
  148. $eni++;
  149. }
  150. }
  151. //exit(-1);
  152. if (empty($methods)) {
  153. $methods = [];
  154. }
  155. // Caching the array just loaded
  156. $cache->setJ($cacheKey, $methods, 0, CACHE_EXPIRE);
  157. // PARAMETERS VALIDATION
  158. $url = trim(substr(filter_input(INPUT_GET, "url", FILTER_SANITIZE_STRING), 0, 300), "/");
  159. /*
  160. switch ($url) {
  161. case "action":
  162. $scriptPath = APP_AJAX_PATH;
  163. define("SCRIPT_NAME", "action");
  164. define("SCRIPT_FILENAME", "action.php");
  165. break;
  166. case "method":
  167. define("SCRIPT_NAME", "mymethod");
  168. define("SCRIPT_FILENAME", "mymethod.php");
  169. break;
  170. default:
  171. $scriptPath = APP_ERROR_PATH;
  172. define("SCRIPT_NAME", "err-404");
  173. define("SCRIPT_FILENAME", "err-404.php");
  174. }
  175. */
  176. //print_r($methods);
  177. echo_ifdebug(true, "<br>");
  178. if (isset($methods[$url])) {
  179. $userMethod = $url;
  180. $cmd = 'return '.$methods[$url]['namespace'].'\\'.$methods[$url]['classname'].'::'.$url.'(';
  181. $i=0;
  182. foreach($methods[$url]["params"] as $param) {
  183. $userParams[$i] = filter_input(INPUT_GET, ltrim($param['name'],'$'));
  184. if ($param['type']==="string") {
  185. $cmd .= "'$userParams[$i]',";
  186. } else {
  187. $cmd .= "$userParams[$i],";
  188. }
  189. $i++;
  190. }
  191. $cmd=rtrim($cmd,",");
  192. $cmd .= ");";
  193. //echo("cmd=$cmd");
  194. //set_exception_handler('eval_ex_handler');
  195. $ret = eval($cmd);
  196. echo($ret);
  197. } else if ($url === "XMLDOC") {
  198. if (!DEBUG) {
  199. header("Content-Type: text/xml");
  200. echo('<?xml version="1.0" encoding="utf-8"?>');
  201. echo('<API>');
  202. foreach($env as $e) {
  203. $curnamespace = $e['namespace'];
  204. $curclassname = $e['classname'];
  205. foreach($methods as $m) {
  206. //echo($m['namespace']."===".$curnamespace."<br>");
  207. //echo($m['classname']."===".$curclassname."<br>");
  208. if (($m['namespace'] === $curnamespace) && ($m['classname'] === $curclassname)) {
  209. echo("<METHOD>");
  210. echo('<NAMESPACE>'.$m['namespace'].'</NAMESPACE>');
  211. echo('<CLASSNAME>'.$m['classname'].'</CLASSNAME>');
  212. echo('<NAME>'.$m['name'].'</NAME>');
  213. $pi=1;
  214. foreach($m['params'] as $p) {
  215. echo("<PARAM$pi type='".$p['type']."' optional='".$p['optional']."'>".ltrim($p['name'],'$')."</PARAM$pi>");
  216. $pi++;
  217. }
  218. echo('<RETURN_TYPE>'.$m['return_type'].'</RETURN_TYPE>');
  219. echo('<HEADER>'.$m['doc'].'</HEADER>');
  220. echo("</METHOD>");
  221. }
  222. }
  223. }
  224. echo('</API>');
  225. }
  226. } else {
  227. $scriptPath = APP_ERROR_PATH;
  228. define("SCRIPT_NAME", "err-404");
  229. define("SCRIPT_FILENAME", "err-404.php");
  230. if (SCRIPT_NAME==="err-404") {
  231. header("HTTP/1.1 404 Not Found");
  232. }
  233. require $scriptPath . "/" . SCRIPT_FILENAME;
  234. }