dolibarr 23.0.3
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 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
107/*
108 * Action
109 */
110if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
111 $search_user = '';
112 $search_entity = '';
113 $search_datec_startday = '';
114 $search_datec_startmonth = '';
115 $search_datec_startyear = '';
116 $search_datec_endday = '';
117 $search_datec_endmonth = '';
118 $search_datec_endyear = '';
119 $search_datec_start = '';
120 $search_datec_end = '';
121 $search_tms_startday = '';
122 $search_tms_startmonth = '';
123 $search_tms_startyear = '';
124 $search_tms_endday = '';
125 $search_tms_endmonth = '';
126 $search_tms_endyear = '';
127 $search_tms_start = '';
128 $search_tms_end = '';
129
130 $toselect = array();
131}
132if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')
133 || GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')) {
134 $massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation
135}
136if (($action == 'delete' && $confirm == 'yes')) {
137 $db->begin();
138
139 $nbok = 0;
140 $TMsg = array();
141
142 //$toselect could contain duplicate entries, cf https://github.com/Dolibarr/dolibarr/issues/26244
143 $unique_arr = array_unique($toselect);
144 foreach ($unique_arr as $toselectid) {
145 $sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token";
146 $sql .= " WHERE rowid = ".((int) $toselectid);
147 $sql .= " AND service = 'dolibarr_rest_api'";
148
149 $result = $db->query($sql);
150
151 if ($result > 0) {
152 $nbok++;
153 } else {
154 setEventMessages($db->error(), null, 'errors');
155 $error++;
156 break;
157 }
158 }
159
160 if (empty($error)) {
161 // Message for elements well deleted
162 if ($nbok > 1) {
163 setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs');
164 } elseif ($nbok > 0) {
165 setEventMessages($langs->trans("RecordDeleted", $nbok), null, 'mesgs');
166 } else {
167 setEventMessages($langs->trans("NoRecordDeleted"), null, 'mesgs');
168 }
169 $db->commit();
170 } else {
171 $db->rollback();
172 }
173
174 //var_dump($listofobjectthirdparties);exit;
175}
176
177/*
178 * View
179 */
180
181$nbtotalofrecords = '';
182if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
183 /* The fast and low memory method to get and count full list converts the sql into a sql count */
184 $sqlforcount = 'SELECT COUNT(*) as nbtotalofrecords';
185 $sqlforcount .= " FROM ".MAIN_DB_PREFIX."oauth_token as oat";
186 $sqlforcount .= " WHERE entity IN (0, ".((int) $conf->entity).")";
187 $sqlforcount .= " AND service = 'dolibarr_rest_api'";
188 $resql = $db->query($sqlforcount);
189 if ($resql) {
190 $objforcount = $db->fetch_object($resql);
191 $nbtotalofrecords = $objforcount->nbtotalofrecords;
192 } else {
193 dol_print_error($db);
194 }
195
196 if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
197 $page = 0;
198 $offset = 0;
199 }
200 $db->free($resql);
201}
202
203$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,";
204$sql .= " oat.lastaccess, oat.apicount_total";
205$sql .= " FROM ".MAIN_DB_PREFIX."oauth_token as oat";
206$sql .= " WHERE service = 'dolibarr_rest_api'";
207$sql .= " AND EXISTS(SELECT 'exist' FROM llx_user as u WHERE u.api_key IS NOT NULL AND u.rowid = oat.fk_user)";
208if ($search_user) {
209 $sql .= " AND EXISTS (SELECT 'exist' FROM ".MAIN_DB_PREFIX."user u";
210 $sql .= " WHERE (u.lastname LIKE '%".$db->escape($search_user)."%'";
211 $sql .= " OR u.firstname LIKE '%".$db->escape($search_user)."%')";
212 $sql .= " AND oat.fk_user = u.rowid))";
213}
214if ($search_datec_start) {
215 $sql .= " AND oat.datec >= '".$db->idate($search_datec_start)."'";
216}
217if ($search_datec_end) {
218 $sql .= " AND oat.datec <= '".$db->idate($search_datec_end)."'";
219}
220if ($search_tms_start) {
221 $sql .= " AND oat.tms >= '".$db->idate($search_tms_start)."'";
222}
223if ($search_tms_end) {
224 $sql .= " AND oat.tms <= '".$db->idate($search_tms_end)."'";
225}
226$sql .= $db->order($sortfield, $sortorder);
227if ($limit) {
228 $sql .= $db->plimit($limit + 1, $offset);
229}
230
231$resql = $db->query($sql);
232
233$num = $db->num_rows($resql);
234
235llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-api page-admin-index');
236
237$param = '';
238if ($limit > 0 && $limit != $conf->liste_limit) {
239 $param .= '&limit='.((int) $limit);
240}
241if ($search_datec_startday) {
242 $param .= '&search_date_startday='.urlencode((string) ($search_datec_startday));
243}
244if ($search_datec_startmonth) {
245 $param .= '&search_date_startmonth='.urlencode((string) ($search_datec_startmonth));
246}
247if ($search_datec_startyear) {
248 $param .= '&search_date_startyear='.urlencode((string) ($search_datec_startyear));
249}
250if ($search_datec_endday) {
251 $param .= '&search_date_endday='.urlencode((string) ($search_datec_endday));
252}
253if ($search_datec_endmonth) {
254 $param .= '&search_date_endmonth='.urlencode((string) ($search_datec_endmonth));
255}
256if ($search_datec_endyear) {
257 $param .= '&search_date_endyear='.urlencode((string) ($search_datec_endyear));
258}
259if ($search_tms_startday) {
260 $param .= '&search_date_startday='.urlencode((string) ($search_tms_startday));
261}
262if ($search_tms_startmonth) {
263 $param .= '&search_date_startmonth='.urlencode((string) ($search_tms_startmonth));
264}
265if ($search_tms_startyear) {
266 $param .= '&search_date_startyear='.urlencode((string) ($search_tms_startyear));
267}
268if ($search_tms_endday) {
269 $param .= '&search_date_endday='.urlencode((string) ($search_tms_endday));
270}
271if ($search_tms_endmonth) {
272 $param .= '&search_date_endmonth='.urlencode((string) ($search_tms_endmonth));
273}
274if ($search_tms_endyear) {
275 $param .= '&search_date_endyear='.urlencode((string) ($search_tms_endyear));
276}
277
278$arrayofselected = is_array($toselect) ? $toselect : array();
279
280$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
281print load_fiche_titre($langs->trans("ApiSetup"), $linkback, 'title_setup');
282
283$head = api_admin_prepare_head();
284
285print dol_get_fiche_head($head, 'token_list', '', -1);
286
287$arrayofmassactions = array(
288 'predelete' => img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete")
289);
290
291if (GETPOSTINT('nomassaction') || in_array($massaction, array('presend', 'predelete'))) {
292 $arrayofmassactions = array();
293}
294$massactionbutton = $form->selectMassAction('', $arrayofmassactions);
295
296$morehtmlright = '';
297$tmpurlforbutton = DOL_URL_ROOT.'/user/api_token/card.php?action=create&backtopage='.urlencode(DOL_URL_ROOT.'/api/admin/token_list.php');
298$morehtmlright .= dolGetButtonTitle($langs->trans('New'), '', 'fa fa-plus-circle', $tmpurlforbutton);
299
300print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
301print '<input type="hidden" name="token" value="'.newToken().'">';
302print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
303print '<input type="hidden" name="action" value="list">';
304print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
305print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
306
307// @phan-suppress-next-line PhanPluginSuspiciousParamOrder
308print_barre_liste($langs->trans("ListOfTokensForAllUsers"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'fa-at', 0, $morehtmlright, '', $limit, 0, 0, 1);
309
310include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
311
312$colspan = 6; // Base colspan for empty list
313
314include DOL_DOCUMENT_ROOT.'/core/tpl/apitoken_list.tpl.php';
315
316print '</form>';
317
318llxFooter();
319$db->close();
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
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...
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)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.