dolibarr 25.0.0-alpha
token_list.php
1<?php
2/* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.org>
4 * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2012-2018 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Load Dolibarr environment
31require '../../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/api.lib.php';
34
46// Load translation files required by the page
47$langs->loadLangs(array('admin', 'users'));
48$error = 0;
49
50if (!$user->admin) {
52}
53
54// Retrieve needed GETPOSTS for this file
55// Action / Massaction
56$action = GETPOST('action', 'aZ09');
57$massaction = GETPOST('massaction', 'alpha');
58$confirm = GETPOST('confirm', 'alpha');
59$toselect = GETPOST('toselect', 'array');
60
61// List filters
62$search_user = GETPOST('search_user', 'alpha');
63$search_entity = GETPOST('search_entity', 'alpha');
64$search_datec_startday = GETPOSTINT('search_datec_startday');
65$search_datec_startmonth = GETPOSTINT('search_datec_startmonth');
66$search_datec_startyear = GETPOSTINT('search_datec_startyear');
67$search_datec_endday = GETPOSTINT('search_datec_endday');
68$search_datec_endmonth = GETPOSTINT('search_datec_endmonth');
69$search_datec_endyear = GETPOSTINT('search_datec_endyear');
70$search_datec_start = dol_mktime(0, 0, 0, $search_datec_startmonth, $search_datec_startday, $search_datec_startyear);
71$search_datec_end = dol_mktime(23, 59, 59, $search_datec_endmonth, $search_datec_endday, $search_datec_endyear);
72$search_tms_startday = GETPOSTINT('search_tms_startday');
73$search_tms_startmonth = GETPOSTINT('search_tms_startmonth');
74$search_tms_startyear = GETPOSTINT('search_tms_startyear');
75$search_tms_endday = GETPOSTINT('search_tms_endday');
76$search_tms_endmonth = GETPOSTINT('search_tms_endmonth');
77$search_tms_endyear = GETPOSTINT('search_tms_endyear');
78$search_tms_start = dol_mktime(0, 0, 0, $search_tms_startmonth, $search_tms_startday, $search_tms_startyear);
79$search_tms_end = dol_mktime(23, 59, 59, $search_tms_endmonth, $search_tms_endday, $search_tms_endyear);
80
81// Pagination
82$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
83$sortfield = GETPOST('sortfield', 'aZ09comma');
84$sortorder = GETPOST('sortorder', 'aZ09comma');
85$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
86if (empty($page) || $page < 0 || GETPOST('button_search', 'alpha') || GETPOST('button_removefilter', 'alpha')) {
87 $page = 0;
88}
89$offset = $limit * $page;
90$pageprev = $page - 1;
91$pagenext = $page + 1;
92
93if (!$sortfield) {
94 $sortfield = 'oat.tms';
95}
96if (!$sortorder) {
97 $sortorder = 'DESC';
98}
99
100$arrayfields = array(
101 'u.login' => array('label' => "User", 'checked' => '1'),
102 'e.label' => array('label' => "Entity", 'checked' => '1'),
103 'oat.datec' => array('label' => "DateCreation", 'checked' => '1'),
104 'oat.tms' => array('label' => "DateModification", 'checked' => '1'),
105);
106// Add hook to complete $arrayfield
107$parameters = array('arrayfields' => &$arrayfields);
108$reshook = $hookmanager->executeHooks('completeArrayFields', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
109
110/*
111 * Action
112 */
113if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
114 $search_user = '';
115 $search_entity = '';
116 $search_datec_startday = '';
117 $search_datec_startmonth = '';
118 $search_datec_startyear = '';
119 $search_datec_endday = '';
120 $search_datec_endmonth = '';
121 $search_datec_endyear = '';
122 $search_datec_start = '';
123 $search_datec_end = '';
124 $search_tms_startday = '';
125 $search_tms_startmonth = '';
126 $search_tms_startyear = '';
127 $search_tms_endday = '';
128 $search_tms_endmonth = '';
129 $search_tms_endyear = '';
130 $search_tms_start = '';
131 $search_tms_end = '';
132
133 $toselect = array();
134}
135if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')
136 || GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')) {
137 $massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation
138}
139if (($action == 'delete' && $confirm == 'yes')) {
140 $db->begin();
141
142 $nbok = 0;
143 $TMsg = array();
144
145 //$toselect could contain duplicate entries, cf https://github.com/Dolibarr/dolibarr/issues/26244
146 $unique_arr = array_unique($toselect);
147 foreach ($unique_arr as $toselectid) {
148 $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token";
149 $sql .= " WHERE rowid = ".((int) $toselectid);
150 $sql .= " AND service = 'dolibarr_rest_api'";
151
152 $result = $db->query($sql);
153
154 if ($result > 0) {
155 $nbok++;
156 } else {
157 setEventMessages($db->error(), null, 'errors');
158 $error++;
159 break;
160 }
161 }
162
163 if (empty($error)) {
164 // Message for elements well deleted
165 if ($nbok > 1) {
166 setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs');
167 } elseif ($nbok > 0) {
168 setEventMessages($langs->trans("RecordDeleted", $nbok), null, 'mesgs');
169 } else {
170 setEventMessages($langs->trans("NoRecordDeleted"), null, 'mesgs');
171 }
172 $db->commit();
173 } else {
174 $db->rollback();
175 }
176
177 //var_dump($listofobjectthirdparties);exit;
178}
179
180/*
181 * View
182 */
183
184$nbtotalofrecords = '';
185if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
186 /* The fast and low memory method to get and count full list converts the sql into a sql count */
187 $sqlforcount = 'SELECT COUNT(*) as nbtotalofrecords';
188 $sqlforcount .= " FROM ".MAIN_DB_PREFIX."oauth_token as oat";
189 $sqlforcount .= " WHERE entity IN (0, ".((int) $conf->entity).")";
190 $sqlforcount .= " AND service = 'dolibarr_rest_api'";
191 $resql = $db->query($sqlforcount);
192 if ($resql) {
193 $objforcount = $db->fetch_object($resql);
194 $nbtotalofrecords = $objforcount->nbtotalofrecords;
195 } else {
197 }
198
199 if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
200 $page = 0;
201 $offset = 0;
202 }
203 $db->free($resql);
204}
205
206$sql = "SELECT oat.rowid, oat.tokenstring, oat.entity, oat.state as rights, oat.fk_user, oat.datec as date_creation, oat.tms as date_modification,";
207$sql .= " oat.lastaccess, oat.apicount_total";
208$sql .= " FROM ".MAIN_DB_PREFIX."oauth_token as oat";
209$sql .= " WHERE service = 'dolibarr_rest_api'";
210$sql .= " AND EXISTS(SELECT 'exist' FROM ".MAIN_DB_PREFIX."user as u WHERE u.api_key IS NOT NULL AND u.rowid = oat.fk_user)";
211if ($search_user) {
212 $sql .= " AND EXISTS (SELECT 'exist' FROM ".MAIN_DB_PREFIX."user u";
213 $sql .= " WHERE (u.lastname LIKE '%".$db->escape($search_user)."%'";
214 $sql .= " OR u.firstname LIKE '%".$db->escape($search_user)."%')";
215 $sql .= " AND oat.fk_user = u.rowid))";
216}
217if ($search_datec_start) {
218 $sql .= " AND oat.datec >= '".$db->idate($search_datec_start)."'";
219}
220if ($search_datec_end) {
221 $sql .= " AND oat.datec <= '".$db->idate($search_datec_end)."'";
222}
223if ($search_tms_start) {
224 $sql .= " AND oat.tms >= '".$db->idate($search_tms_start)."'";
225}
226if ($search_tms_end) {
227 $sql .= " AND oat.tms <= '".$db->idate($search_tms_end)."'";
228}
229$sql .= $db->order($sortfield, $sortorder);
230if ($limit) {
231 $sql .= $db->plimit($limit + 1, $offset);
232}
233
234$resql = $db->query($sql);
235
236$num = $db->num_rows($resql);
237
238llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-api page-admin-index');
239
240$param = '';
241if ($limit > 0 && $limit != $conf->liste_limit) {
242 $param .= '&limit='.((int) $limit);
243}
244if ($search_datec_startday) {
245 $param .= '&search_date_startday='.urlencode((string) ($search_datec_startday));
246}
247if ($search_datec_startmonth) {
248 $param .= '&search_date_startmonth='.urlencode((string) ($search_datec_startmonth));
249}
250if ($search_datec_startyear) {
251 $param .= '&search_date_startyear='.urlencode((string) ($search_datec_startyear));
252}
253if ($search_datec_endday) {
254 $param .= '&search_date_endday='.urlencode((string) ($search_datec_endday));
255}
256if ($search_datec_endmonth) {
257 $param .= '&search_date_endmonth='.urlencode((string) ($search_datec_endmonth));
258}
259if ($search_datec_endyear) {
260 $param .= '&search_date_endyear='.urlencode((string) ($search_datec_endyear));
261}
262if ($search_tms_startday) {
263 $param .= '&search_date_startday='.urlencode((string) ($search_tms_startday));
264}
265if ($search_tms_startmonth) {
266 $param .= '&search_date_startmonth='.urlencode((string) ($search_tms_startmonth));
267}
268if ($search_tms_startyear) {
269 $param .= '&search_date_startyear='.urlencode((string) ($search_tms_startyear));
270}
271if ($search_tms_endday) {
272 $param .= '&search_date_endday='.urlencode((string) ($search_tms_endday));
273}
274if ($search_tms_endmonth) {
275 $param .= '&search_date_endmonth='.urlencode((string) ($search_tms_endmonth));
276}
277if ($search_tms_endyear) {
278 $param .= '&search_date_endyear='.urlencode((string) ($search_tms_endyear));
279}
280
281$arrayofselected = is_array($toselect) ? $toselect : array();
282
283$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
284print load_fiche_titre($langs->trans("ApiSetup"), $linkback, 'title_setup');
285
286$head = api_admin_prepare_head();
287
288print dol_get_fiche_head($head, 'token_list', '', -1);
289
290$arrayofmassactions = array(
291 'predelete' => img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete")
292);
293
294if (GETPOSTINT('nomassaction') || in_array($massaction, array('presend', 'predelete'))) {
295 $arrayofmassactions = array();
296}
297$massactionbutton = $form->selectMassAction('', $arrayofmassactions);
298
299$morehtmlright = '';
300$tmpurlforbutton = DOL_URL_ROOT.'/user/api_token/card.php?action=create&backtopage='.urlencode(DOL_URL_ROOT.'/api/admin/token_list.php');
301$morehtmlright .= dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', $tmpurlforbutton);
302
303print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
304print '<input type="hidden" name="token" value="'.newToken().'">';
305print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
306print '<input type="hidden" name="action" value="list">';
307print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
308print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
309
310// @phan-suppress-next-line PhanPluginSuspiciousParamOrder
311print_barre_liste($langs->trans("ListOfTokensForAllUsers"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'fa-at', 0, $morehtmlright, '', $limit, 0, 0, 1);
312
313include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
314
315$colspan = 6; // Base colspan for empty list
316
317include DOL_DOCUMENT_ROOT.'/core/tpl/apitoken_list.tpl.php';
318
319print '</form>';
320
321llxFooter();
322$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
api_admin_prepare_head()
Return array head with list of tabs to view object information.
Definition api.lib.php:32
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print_barre_liste($title, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $selectlimitsuffix=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:519
dolGetButtonTitle($label, $helpText='', $iconClass='fa fa-file', $url='', $id='', $status=1, $params=array())
Function dolGetButtonTitle : this kind of buttons are used in title in list.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.