explorer.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \defgroup api Module DolibarrApi
  20. * \brief API loader
  21. * Search files htdocs/<module>/class/api_<module>.class.php
  22. * \file htdocs/api/admin/explorer.php
  23. */
  24. require_once '../../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/api/class/api_access.class.php';
  28. $langs->load("admin");
  29. /*
  30. * View
  31. */
  32. // Enable and test if module Api is enabled
  33. if (empty($conf->global->MAIN_MODULE_API))
  34. {
  35. dol_syslog("Call Dolibarr API interfaces with module REST disabled");
  36. print $langs->trans("WarningModuleNotActive",'Api').'.<br><br>';
  37. print $langs->trans("ToActivateModule");
  38. exit;
  39. }
  40. $api = new DolibarrApi($db);
  41. $api->r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
  42. $api->r->setSupportedFormats('JsonFormat', 'XmlFormat');
  43. $api->r->addAuthenticationClass('DolibarrApiAccess','');
  44. $listofapis = array();
  45. $modulesdir = dolGetModulesDirs();
  46. foreach ($modulesdir as $dir)
  47. {
  48. /*
  49. * Search available module
  50. */
  51. dol_syslog("Scan directory ".$dir." for API modules");
  52. $handle=@opendir(dol_osencode($dir));
  53. if (is_resource($handle))
  54. {
  55. while (($file = readdir($handle))!==false)
  56. {
  57. if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg))
  58. {
  59. $modulename=$reg[1];
  60. // Defined if module is enabled
  61. $enabled=true;
  62. $module=$part=$obj=strtolower(preg_replace('/^mod/i','',$modulename));
  63. //if ($part == 'propale') $part='propal';
  64. if ($module == 'societe') {
  65. $obj = 'thirdparty';
  66. }
  67. if ($module == 'categorie') {
  68. $part = 'categories';
  69. $obj = 'category';
  70. }
  71. if ($module == 'facture') {
  72. $part = 'compta/facture';
  73. $obj = 'facture';
  74. }
  75. if (empty($conf->$module->enabled)) $enabled=false;
  76. if ($enabled)
  77. {
  78. /*
  79. * If exists, load the API class for enable module
  80. *
  81. * Search files named api_<object>.class.php into /htdocs/<module>/class directory
  82. *
  83. * @todo : take care of externals module!
  84. * @todo : use getElementProperties() function ?
  85. */
  86. $dir_part = DOL_DOCUMENT_ROOT.'/'.$part.'/class/';
  87. $handle_part=@opendir(dol_osencode($dir_part));
  88. if (is_resource($handle_part))
  89. {
  90. while (($file_searched = readdir($handle_part))!==false)
  91. {
  92. if (is_readable($dir_part.$file_searched) && preg_match("/^(api_.*)\.class\.php$/i",$file_searched,$reg))
  93. {
  94. $classname=$reg[1];
  95. $classname = str_replace('Api_','',ucwords($reg[1])).'Api';
  96. $classname = ucfirst($classname);
  97. require_once $dir_part.$file_searched;
  98. if (class_exists($classname))
  99. {
  100. dol_syslog("Found API classname=".$classname);
  101. $api->r->addAPIClass($classname,'');
  102. /*
  103. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Routes.php';
  104. $tmpclass = new ReflectionClass($classname);
  105. try {
  106. $classMetadata = CommentParser::parse($tmpclass->getDocComment());
  107. } catch (Exception $e) {
  108. throw new RestException(500, "Error while parsing comments of `$classname` class. " . $e->getMessage());
  109. }*/
  110. //$listofapis[]=array('classname'=>$classname, 'fullpath'=>$file_searched);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. $listofapis=Routes::toArray();
  121. //var_dump($listofapis);
  122. llxHeader();
  123. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
  124. print load_fiche_titre($langs->trans("ApiSetup"),$linkback,'title_setup');
  125. // Define $urlwithroot
  126. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  127. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  128. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  129. // Show message
  130. print '<br>';
  131. $message='';
  132. $url='<a href="'.$urlwithroot.'/api/index.php/login?login='.urlencode($user->login).'&password=yourpassword" target="_blank">'.$urlwithroot.'/api/index.php/login?login='.urlencode($user->login).'&password=yourpassword[&reset=1]</a>';
  133. $message.=$langs->trans("UrlToGetKeyToUseAPIs").':<br>';
  134. $message.=img_picto('','object_globe.png').' '.$url;
  135. print $message;
  136. print '<br>';
  137. print '<br>';
  138. $oldclass='';
  139. print $langs->trans("ListOfAvailableAPIs").':<br>';
  140. foreach($listofapis['v1'] as $key => $val)
  141. {
  142. if ($key == 'login') continue;
  143. if ($key)
  144. {
  145. foreach($val as $method => $val2)
  146. {
  147. $newclass=$val2['className'];
  148. if (preg_match('/restler/i', $newclass)) continue;
  149. if ($oldclass != $newclass)
  150. {
  151. print "\n<br>\n".$langs->trans("Class").': '.$newclass.'<br>'."\n";
  152. $oldclass = $newclass;
  153. }
  154. //print $key.' - '.$val['classname'].' - '.$val['fullpath']." - ".DOL_MAIN_URL_ROOT.'/api/index.php/'.strtolower(preg_replace('/Api$/','',$val['classname']))."/xxx<br>\n";
  155. $url=$urlwithroot.'/api/index.php/'.$key;
  156. $url.='?api_key=token';
  157. print img_picto('','object_globe.png').' '.$method.' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
  158. }
  159. }
  160. }
  161. print '<br>';
  162. print '<br>';
  163. print $langs->trans("OnlyActiveElementsAreExposed", DOL_URL_ROOT.'/admin/modules.php');
  164. llxFooter();
  165. $db->close();