diff --git a/htdocs/api/index.php b/htdocs/api/index.php index a124f42e83b..46b63b994fe 100644 --- a/htdocs/api/index.php +++ b/htdocs/api/index.php @@ -305,14 +305,6 @@ if (!empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' && $moduleobject = $reg[1]; if ($moduleobject == 'explorer') { // If we call page to explore details of a service $moduleobject = $regbis[2]; - } else { - // TODO - // Increase counter of API access - if (getDolGlobalString('API_COUNTER_ENABLED')) { - include DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; - dolibarr_set_const($db, 'API_COUNTER_COUNT', getDolGlobalInt('API_COUNTER_COUNT') + 1, 'chaine', 1); - //var_dump('eeee');exit; - } } $moduleobject = strtolower($moduleobject); @@ -442,4 +434,50 @@ if (Luracast\Restler\Defaults::$returnResponse) { echo $result; } +if (getDolGlobalInt("API_ENABLE_COUNT_CALLS") && $api->r->responseCode == 200) { + $error = 0; + $db->begin(); + $userid = DolibarrApiAccess::$user->id; + + $sql = "SELECT up.value"; + $sql .= " FROM ".MAIN_DB_PREFIX."user_param as up"; + $sql .= " WHERE up.param = 'API_COUNT_CALL'"; + $sql .= " AND up.fk_user = ".((int) $userid); + $sql .= " AND up.entity = ".((int) $conf->entity); + + $result = $db->query($sql); + if ($result) { + $updateapi = false; + $nbrows = $db->num_rows($result); + if ($nbrows == 0) { + $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."user_param"; + $sql2 .= " (fk_user, entity, param, value)"; + $sql2 .= " VALUES (".((int) $userid).", ".((int) $conf->entity).", 'API_COUNT_CALL', 1)"; + } else { + $updateapi = true; + $sql2 = "UPDATE ".MAIN_DB_PREFIX."user_param as up"; + $sql2 .= " SET up.value = up.value + 1"; + $sql2 .= " WHERE up.param = 'API_COUNT_CALL'"; + $sql2 .= " AND up.fk_user = ".((int) $userid); + $sql2 .= " AND up.entity = ".((int) $conf->entity); + } + + $result2 = $db->query($sql2); + if (!$result2) { + $modeapicall = $updateapi ? 'updating' : 'inserting'; + dol_syslog('Error while '.$modeapicall. ' API_COUNT_CALL for user '.$userid, LOG_ERR); + $error++; + } + } else { + dol_syslog('Error on select API_COUNT_CALL for user '.$userid, LOG_ERR); + $error++; + } + + if ($error) { + $db->rollback(); + } else { + $db->commit(); + } +} + //session_destroy();