dolibarr  13.0.2
explorer.php
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/>.
17  *
18  * @deprecated Old explorer. Not using Swagger. See instead explorer in htdocs/api/index.php.
19  */
20 
28 use Luracast\Restler\Routes;
29 
30 require_once '../../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/api/class/api_access.class.php';
34 
35 // Load translation files required by the page
36 $langs->load("admin");
37 
38 
39 /*
40  * View
41  */
42 
43 // Enable and test if module Api is enabled
44 if (empty($conf->global->MAIN_MODULE_API))
45 {
46  dol_syslog("Call Dolibarr API interfaces with module REST disabled");
47  print $langs->trans("WarningModuleNotActive", 'Api').'.<br><br>';
48  print $langs->trans("ToActivateModule");
49  exit;
50 }
51 
52 
53 $api = new DolibarrApi($db);
54 
55 $api->r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
56 $api->r->setSupportedFormats('JsonFormat', 'XmlFormat');
57 $api->r->addAuthenticationClass('DolibarrApiAccess', '');
58 
59 $listofapis = array();
60 
61 $modulesdir = dolGetModulesDirs();
62 foreach ($modulesdir as $dir)
63 {
64  /*
65  * Search available module
66  */
67  //dol_syslog("Scan directory ".$dir." for API modules");
68 
69  $handle = @opendir(dol_osencode($dir));
70  if (is_resource($handle))
71  {
72  while (($file = readdir($handle)) !== false)
73  {
74  if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i", $file, $reg))
75  {
76  $modulename = $reg[1];
77 
78  // Defined if module is enabled
79  $enabled = true;
80  $module = $part = $obj = strtolower(preg_replace('/^mod/i', '', $modulename));
81  //if ($part == 'propale') $part='propal';
82  if ($module == 'societe') {
83  $obj = 'thirdparty';
84  }
85  if ($module == 'categorie') {
86  $part = 'categories';
87  $obj = 'category';
88  }
89  if ($module == 'facture') {
90  $part = 'compta/facture';
91  $obj = 'facture';
92  }
93  if ($module == 'ficheinter') {
94  $obj = 'fichinter';
95  $part = 'fichinter';
96  $module = 'fichinter';
97  }
98 
99  if (empty($conf->$module->enabled)) $enabled = false;
100 
101  if ($enabled) {
102  /*
103  * If exists, load the API class for enable module
104  *
105  * Search files named api_<object>.class.php into /htdocs/<module>/class directory
106  *
107  * @todo : take care of externals module!
108  * @todo : use getElementProperties() function ?
109  */
110  $dir_part = DOL_DOCUMENT_ROOT.'/'.$part.'/class/';
111 
112  $handle_part = @opendir(dol_osencode($dir_part));
113  if (is_resource($handle_part))
114  {
115  while (($file_searched = readdir($handle_part)) !== false)
116  {
117  if (is_readable($dir_part.$file_searched) && preg_match("/^api_(.*)\.class\.php$/i", $file_searched, $reg))
118  {
119  $classname = ucwords($reg[1]);
120  require_once $dir_part.$file_searched;
121  if (class_exists($classname))
122  {
123  dol_syslog("Found API classname=".$classname." into ".$dir);
124  $listofapis[] = $classname;
125  }
126  }
127 
128  /*
129  if (is_readable($dir_part.$file_searched) && preg_match("/^(api_.*)\.class\.php$/i",$file_searched,$reg))
130  {
131  $classname=$reg[1];
132  $classname = str_replace('Api_','',ucwords($reg[1])).'Api';
133  //$classname = str_replace('Api_','',ucwords($reg[1]));
134  $classname = ucfirst($classname);
135  require_once $dir_part.$file_searched;
136 
137  // if (class_exists($classname))
138  // {
139  // dol_syslog("Found API classname=".$classname);
140  // $api->r->addAPIClass($classname,'');
141 
142  // require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Routes.php';
143  // $tmpclass = new ReflectionClass($classname);
144  // try {
145  // $classMetadata = CommentParser::parse($tmpclass->getDocComment());
146  // } catch (Exception $e) {
147  // throw new RestException(500, "Error while parsing comments of `$classname` class. " . $e->getMessage());
148  // }
149 
150  // //$listofapis[]=array('classname'=>$classname, 'fullpath'=>$file_searched);
151  // }
152  }*/
153  }
154  }
155  }
156  }
157  }
158  }
159 }
160 
161 //var_dump($listofapis);
162 $listofapis = Routes::toArray(); // @todo api for "status" is lost here
163 //var_dump($listofapis);
164 
165 
166 llxHeader();
167 
168 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
169 print load_fiche_titre($langs->trans("ApiSetup"), $linkback, 'title_setup');
170 
171 // Define $urlwithroot
172 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
173 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
174 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
175 
176 // Show message
177 print '<br>';
178 $message = '';
179 $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>';
180 $message .= $langs->trans("UrlToGetKeyToUseAPIs").':<br>';
181 $message .= img_picto('', 'globe').' '.$url;
182 print $message;
183 print '<br>';
184 print '<br>';
185 
186 $oldclass = '';
187 
188 print $langs->trans("ListOfAvailableAPIs").':<br>';
189 foreach ($listofapis['v1'] as $key => $val)
190 {
191  if ($key == 'login') continue;
192  if ($key == 'index') continue;
193 
194  if ($key)
195  {
196  foreach ($val as $method => $val2)
197  {
198  $newclass = $val2['className'];
199 
200  if (preg_match('/restler/i', $newclass)) continue;
201 
202  if ($oldclass != $newclass)
203  {
204  print "\n<br>\n".$langs->trans("Class").': '.$newclass.'<br>'."\n";
205  $oldclass = $newclass;
206  }
207  //print $key.' - '.$val['classname'].' - '.$val['fullpath']." - ".DOL_MAIN_URL_ROOT.'/api/index.php/'.strtolower(preg_replace('/Api$/','',$val['classname']))."/xxx<br>\n";
208  $url = $urlwithroot.'/api/index.php/'.$key;
209  $url .= '?api_key=token';
210  print img_picto('', 'globe').' '.$method.' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
211  }
212  }
213 }
214 
215 print '<br>';
216 print '<br>';
217 print $langs->trans("OnlyActiveElementsAreExposed", DOL_URL_ROOT.'/admin/modules.php');
218 
219 
220 llxFooter();
221 $db->close();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dolGetModulesDirs($subdir= '')
Return list of modules directories.
llxHeader()
Empty header.
Definition: wrapper.php:45
Class for API REST v1.
Definition: api.class.php:30
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print
Draft customers invoices.
Definition: index.php:89
llxFooter()
Empty footer.
Definition: wrapper.php:59