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