94 global
$conf, $langs, $user;
99 $userClass = Defaults::$userIdentifierClass;
108 if (isset($_GET[
'api_key'])) {
110 $api_key = $_GET[
'api_key'];
112 if (isset($_GET[
'DOLAPIKEY'])) {
114 $api_key = $_GET[
'DOLAPIKEY'];
120 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
121 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
122 } elseif (empty($api_key)) {
123 $headers = getallheaders();
124 $api_key = preg_replace(
'/^Bearer\s+/i',
'', empty($headers[
'Authorization']) ?
'' : $headers[
'Authorization']);
129 if (preg_match(
'/^dolcrypt:/i', $api_key)) {
130 throw new RestException(503,
'Bad value for the API key. An API key should not start with dolcrypt:');
139 $sql =
"SELECT u.login, u.datec, u.api_key as use_api, u.api_key as api_key, 0 as token_rowid,";
140 $sql .=
" u.tms as date_modification,";
141 $sql .=
" gu.entity, gu.entity as token_entity";
142 $sql .=
" FROM ".$this->db->prefix().
"user as u";
143 $sql .=
" JOIN ".$this->db->prefix().
"usergroup_user as gu ON u.rowid = gu.fk_user AND gu.entity = ".((int)
$conf->entity);
144 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
146 $sql =
"SELECT u.login, u.datec, u.api_key as use_api, u.entity, u.api_key as api_key, u.entity as token_entity, 0 as token_rowid,";
147 $sql .=
" u.tms as date_modification";
148 $sql .=
" FROM ".$this->db->prefix().
"user as u";
149 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
153 $sql =
"SELECT u.login, u.datec, u.api_key as use_api, oat.tokenstring as api_key, oat.entity as token_entity, oat.rowid as token_rowid,";
154 $sql .=
" oat.tms as date_modification,";
155 $sql .=
" gu.entity";
156 $sql .=
" FROM ".$this->db->prefix().
"oauth_token AS oat";
157 $sql .=
" JOIN ".$this->db->prefix().
"user AS u ON u.rowid = oat.fk_user";
158 $sql .=
" JOIN ".$this->db->prefix().
"usergroup_user as gu ON u.rowid = gu.fk_user AND gu.entity = ".((int)
$conf->entity);
159 $sql .=
" WHERE (oat.tokenstring = '".$this->db->escape($api_key).
"'";
160 $sql .=
" OR oat.tokenstring = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"')";
161 $sql .=
" AND gu.entity = oat.entity";
162 $sql .=
" AND oat.service = 'dolibarr_rest_api'";
164 $sql =
"SELECT u.login, u.datec, u.api_key as use_api, u.entity, oat.tokenstring as api_key, oat.entity as token_entity, oat.rowid as token_rowid,";
165 $sql .=
" oat.tms as date_modification";
166 $sql .=
" FROM ".$this->db->prefix().
"oauth_token AS oat";
167 $sql .=
" JOIN ".$this->db->prefix().
"user AS u ON u.rowid = oat.fk_user";
168 $sql .=
" WHERE (oat.tokenstring = '".$this->db->escape($api_key).
"'";
169 $sql .=
" OR oat.tokenstring = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"')";
170 $sql .=
" AND oat.service = 'dolibarr_rest_api'";
174 $result = $this->db->query($sql);
176 $nbrows = $this->db->num_rows($result);
178 $obj = $this->db->fetch_object($result);
180 $login = $obj->login;
182 $userentity = $obj->entity;
183 $token_entity = $obj->token_entity;
184 $token_rowid = $obj->token_rowid;
186 if (!defined(
"DOLENTITY") &&
$conf->entity != ($obj->entity ? $obj->entity : 1)) {
187 $conf->entity = ($obj->entity ? $obj->entity : 1);
189 dol_syslog(
"Entity was not set on http header with HTTP_DOLAPIENTITY (recommended for performance purpose), so we switch now on entity of user (".
$conf->entity.
") and we have to reload configuration.", LOG_WARNING);
190 $conf->setValues($this->db);
194 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
196 $fmysoc =
new Societe($this->db);
197 $fmysoc->setMysoc(
$conf);
200 if ($fmysoc->country_code ==
'DE' && !isset(
$conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
202 $conf->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
204 if ($fmysoc->country_code ==
'FR' && !isset(
$conf->global->INVOICE_CATEGORY_OF_OPERATION)) {
206 $conf->global->INVOICE_CATEGORY_OF_OPERATION = 1;
208 if ($fmysoc->country_code ==
'FR' && !isset(
$conf->global->INVOICE_DISABLE_REPLACEMENT)) {
212 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
214 if ($fmysoc->country_code ==
'GR' && !isset(
$conf->global->INVOICE_DISABLE_REPLACEMENT)) {
216 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
218 if ($fmysoc->country_code ==
'GR' && !isset(
$conf->global->INVOICE_DISABLE_DEPOSIT)) {
220 $conf->global->INVOICE_DISABLE_DEPOSIT = 1;
223 if (($fmysoc->localtax1_assuj || $fmysoc->localtax2_assuj) && !isset(
$conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX)) {
226 $conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX = 1;
234 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
235 $langcode = $user->conf->MAIN_LANG_DEFAULT;
237 if ($langs->getDefaultLang() != $langcode) {
238 $langs->setDefaultLang($langcode);
239 $langs->tab_translate = array();
240 $langs->loadLangs(array(
'main'));
244 if (
$conf->entity != ($token_entity ? $token_entity : 1)) {
245 throw new RestException(401,
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Token not valid (may be a typo or a wrong entity)");
247 } elseif ($nbrows > 1) {
248 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
251 throw new RestException(503,
'Error when fetching user api_key :'.$this->db->error);
254 if ($login && $stored_key != $api_key) {
255 $userClass::setCacheIdentifier($api_key);
259 $genericmessageerroruser =
'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity='.$conf->entity.
')';
262 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE);
264 throw new RestException(401, $genericmessageerroruser);
267 $fuser =
new User($this->db);
268 $result = $fuser->fetch(0, $login,
'', 0, (empty($userentity) ? -1 :
$conf->entity));
270 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Failed to fetch on entity", LOG_NOTICE);
272 throw new RestException(401, $genericmessageerroruser);
276 if ($fuser->status != $fuser::STATUS_ENABLED) {
278 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has been disabled", LOG_NOTICE);
280 throw new RestException(401, $genericmessageerroruser);
284 if (($fuser->flagdelsessionsbefore && !empty($_SESSION[
"dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION[
"dol_logindate"])) {
286 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has a date for session invalidation = ".$fuser->flagdelsessionsbefore.
" and a session date = ".$_SESSION[
"dol_logindate"].
". We must invalidate its sessions.");
288 throw new RestException(401, $genericmessageerroruser);
292 if ($fuser->isNotIntoValidityDateRange()) {
294 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user login has a validity between [".$fuser->datestartvalidity.
" and ".$fuser->dateendvalidity.
"], current date is ".
dol_now());
296 throw new RestException(401, $genericmessageerroruser);
303 include DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
310 $sqlforcounter =
"UPDATE ".$this->db->prefix().
"oauth_token SET ";
311 $sqlforcounter .=
" apicount_total = apicount_total + 1,";
312 $sqlforcounter .=
" apicount_month = apicount_month + 1,";
314 $sqlforcounter .=
" pageviews_previous_month = ".$this->db->ifsql(
"lastaccess < '".$this->db->idate(
dol_mktime(0, 0, 0, $tmpnow[
'mon'], 1, $tmpnow[
'year'],
'gmt', 0),
'gmt').
"'",
'apicount_month',
'apicount_previous_month').
",";
315 $sqlforcounter .=
" lastaccess = '".$this->db->idate(
dol_now(
'gmt'),
'gmt').
"'";
316 $sqlforcounter .=
" WHERE rowid = ".((int) $token_rowid);
318 $this->db->query($sqlforcounter);
323 $fuser->loadRights();
326 static::$user = $fuser;
332 static::$role =
'external';
336 static::$role =
'admin';
339 throw new RestException(401,
"Failed to login to API. Neither parameter 'HTTP_DOLAPIKEY' nor 'Authentication: Bearer' found on HTTP header (and no parameter DOLAPIKEY in URL).");
342 $userClass::setCacheIdentifier(static::$role);
343 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
344 $requirefortest = static::$requires;
345 if (!is_array($requirefortest)) {
346 $requirefortest = explode(
',', $requirefortest);
348 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';