94 global
$conf, $langs, $user;
99 $userClass = Defaults::$userIdentifierClass;
108 if (isset($_GET[
'api_key']) || isset($_GET[
'DOLAPIKEY'])) {
114 throw new RestException(401,
'The API key must be sent in the DOLAPIKEY header, not in the URL (API_DISABLE_KEY_IN_URL is set)');
117 if (isset($_GET[
'api_key'])) {
118 $api_key = $_GET[
'api_key'];
120 if (isset($_GET[
'DOLAPIKEY'])) {
121 $api_key = $_GET[
'DOLAPIKEY'];
127 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
128 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
129 } elseif (empty($api_key)) {
130 $headers = getallheaders();
131 $api_key = preg_replace(
'/^Bearer\s+/i',
'', empty($headers[
'Authorization']) ?
'' : $headers[
'Authorization']);
136 if (preg_match(
'/^dolcrypt:/i', $api_key)) {
137 throw new RestException(503,
'Bad value for the API key. An API key should not start with dolcrypt:');
146 $sql =
"SELECT DISTINCT u.login, u.datec, u.api_key as use_api, u.api_key as api_key, 0 as token_rowid,";
147 $sql .=
" u.tms as date_modification,";
148 $sql .=
" gu.entity, gu.entity as token_entity";
149 $sql .=
" FROM ".$this->db->prefix().
"user as u";
150 $sql .=
" JOIN ".$this->db->prefix().
"usergroup_user as gu ON u.rowid = gu.fk_user AND gu.entity = ".((int)
$conf->entity);
151 $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, u.entity, u.api_key as api_key, u.entity as token_entity, 0 as token_rowid,";
154 $sql .=
" u.tms as date_modification";
155 $sql .=
" FROM ".$this->db->prefix().
"user as u";
156 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
160 $sql =
"SELECT DISTINCT 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,";
161 $sql .=
" oat.tms as date_modification,";
162 $sql .=
" gu.entity";
163 $sql .=
" FROM ".$this->db->prefix().
"oauth_token AS oat";
164 $sql .=
" JOIN ".$this->db->prefix().
"user AS u ON u.rowid = oat.fk_user";
165 $sql .=
" JOIN ".$this->db->prefix().
"usergroup_user as gu ON u.rowid = gu.fk_user AND gu.entity = ".((int)
$conf->entity);
166 $sql .=
" WHERE (oat.tokenstring = '".$this->db->escape($api_key).
"'";
167 $sql .=
" OR oat.tokenstring = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"')";
168 $sql .=
" AND gu.entity = oat.entity";
169 $sql .=
" AND oat.service = 'dolibarr_rest_api'";
171 $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,";
172 $sql .=
" oat.tms as date_modification";
173 $sql .=
" FROM ".$this->db->prefix().
"oauth_token AS oat";
174 $sql .=
" JOIN ".$this->db->prefix().
"user AS u ON u.rowid = oat.fk_user";
175 $sql .=
" WHERE (oat.tokenstring = '".$this->db->escape($api_key).
"'";
176 $sql .=
" OR oat.tokenstring = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"')";
177 $sql .=
" AND oat.service = 'dolibarr_rest_api'";
181 $result = $this->db->query($sql);
183 $nbrows = $this->db->num_rows($result);
185 $obj = $this->db->fetch_object($result);
187 $login = $obj->login;
189 $userentity = $obj->entity;
190 $token_entity = $obj->token_entity;
191 $token_rowid = $obj->token_rowid;
193 if (!defined(
"DOLENTITY") &&
$conf->entity != ($obj->entity ? $obj->entity : 1)) {
194 $conf->entity = ($obj->entity ? $obj->entity : 1);
196 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);
197 $conf->setValues($this->db);
201 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
203 $fmysoc =
new Societe($this->db);
204 $fmysoc->setMysoc(
$conf);
207 if ($fmysoc->country_code ==
'DE' && !isset(
$conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
209 $conf->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
211 if ($fmysoc->country_code ==
'FR' && !isset(
$conf->global->INVOICE_CATEGORY_OF_OPERATION)) {
213 $conf->global->INVOICE_CATEGORY_OF_OPERATION = 1;
215 if ($fmysoc->country_code ==
'FR' && !isset(
$conf->global->INVOICE_DISABLE_REPLACEMENT)) {
219 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
221 if ($fmysoc->country_code ==
'GR' && !isset(
$conf->global->INVOICE_DISABLE_REPLACEMENT)) {
223 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
225 if ($fmysoc->country_code ==
'GR' && !isset(
$conf->global->INVOICE_DISABLE_DEPOSIT)) {
227 $conf->global->INVOICE_DISABLE_DEPOSIT = 1;
230 if (($fmysoc->localtax1_assuj || $fmysoc->localtax2_assuj) && !isset(
$conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX)) {
233 $conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX = 1;
241 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
242 $langcode = $user->conf->MAIN_LANG_DEFAULT;
244 if ($langs->getDefaultLang() != $langcode) {
245 $langs->setDefaultLang($langcode);
246 $langs->tab_translate = array();
247 $langs->loadLangs(array(
'main'));
251 if (
$conf->entity != ($token_entity ? $token_entity : 1)) {
252 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)");
254 } elseif ($nbrows > 1) {
255 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
258 throw new RestException(503,
'Error when fetching user api_key :'.$this->db->lasterror());
261 if ($login && $stored_key != $api_key) {
262 $userClass::setCacheIdentifier($api_key);
266 $genericmessageerroruser =
'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity='.$conf->entity.
')';
269 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE);
271 throw new RestException(401, $genericmessageerroruser);
274 $fuser =
new User($this->db);
275 $result = $fuser->fetch(0, $login,
'', 0, (empty($userentity) ? -1 :
$conf->entity));
277 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Failed to fetch on entity", LOG_NOTICE);
279 throw new RestException(401, $genericmessageerroruser);
283 if ($fuser->status != $fuser::STATUS_ENABLED) {
285 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has been disabled", LOG_NOTICE);
287 throw new RestException(401, $genericmessageerroruser);
291 if (($fuser->flagdelsessionsbefore && !empty($_SESSION[
"dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION[
"dol_logindate"])) {
293 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.");
295 throw new RestException(401, $genericmessageerroruser);
299 if ($fuser->isNotIntoValidityDateRange()) {
301 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());
303 throw new RestException(401, $genericmessageerroruser);
310 include DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
317 $sqlforcounter =
"UPDATE ".$this->db->prefix().
"oauth_token SET ";
318 $sqlforcounter .=
" apicount_total = apicount_total + 1,";
319 $sqlforcounter .=
" apicount_month = apicount_month + 1,";
321 $sqlforcounter .=
" apicount_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').
",";
322 $sqlforcounter .=
" lastaccess = '".$this->db->idate(
dol_now(
'gmt'),
'gmt').
"'";
323 $sqlforcounter .=
" WHERE rowid = ".((int) $token_rowid);
325 $this->db->query($sqlforcounter);
330 $fuser->loadRights();
333 static::$user = $fuser;
339 static::$role =
'external';
343 static::$role =
'admin';
346 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).");
349 $userClass::setCacheIdentifier(static::$role);
351 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
352 $requirefortest = static::$requires;
353 if (!is_array($requirefortest)) {
354 $requirefortest = explode(
',', $requirefortest);
356 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';