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