Skip to content

Commit

Permalink
Eoxia#74 [Admin] add: check api credentials when setup
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-kilyan committed Sep 20, 2024
1 parent 1a3ac0c commit e61fea6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
26 changes: 26 additions & 0 deletions admin/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@
if (dol_strlen($signatureTokenYourlsAPI) > 0) {
dolibarr_set_const($db, 'EASYURL_SIGNATURE_TOKEN_YOURLS_API', $signatureTokenYourlsAPI, 'chaine', 0, '', $conf->entity);
}

if (dol_strlen($URLYourlsAPI) > 0 && dol_strlen($signatureTokenYourlsAPI) > 0) {

// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, GETPOST('url_yourls_api'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request

curl_setopt($ch, CURLOPT_POSTFIELDS, [ // Data to POST
'signature' => getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'),
'format' => 'json'
]);

$data = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// 400 when no actions set and credentials ok
if ($statusCode != 400) {
setEventMessage('WarningApiCredentialsIncorrect', 'warnings');
}
}

dolibarr_set_const($db, 'EASYURL_DEFAULT_ORIGINAL_URL', $defaultOriginalURL, 'chaine', 0, '', $conf->entity);

setEventMessage('SavedConfig');
Expand Down
2 changes: 2 additions & 0 deletions langs/fr_FR/easyurl.lang
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ AutomaticEasyUrlGeneration = Génération automatique des URLs raccou
AutomaticEasyUrlGenerationDescription = Crée automatiquement l'URL raccourcie lors de la validation d'un objet <br> (proposition commerciale, commande, facture)
ManualEasyUrlGeneration = Génération manuelle des URLs raccourcies
ManualEasyUrlGenerationDescription = Ajout d'un bouton pour créer une URL raccourcie sur un objet <br> (proposition commerciale, commande, facture)
WarningApiCredentialsIncorrect = Les informations d'API sont incorrects



Expand Down Expand Up @@ -81,6 +82,7 @@ UrlMethodeDescription = Cette option permet de choisir la méthode d'A
GenerateUrlSuccess = Génération réussie des %d raccourcissements d'URLs
OriginalUrlFail = Erreur(s) possible(s) : <br> - Nombre d'URL est vide <br> - l'URL d'origine est vide <br> - l'URL d'origine par défaut n'as pas été configurée
DefaultOriginalUrlConfiguration = Vous n'avez pas configuré d'URL d'origine par défaut, veuillez cliquer ici pour vous rendre sur la page de configuration
ApiCredentialsNotCongirate = Vous n'avez pas configuré d'API, veuillez cliquer ici pour vous rendre sur la page de configuration
Success = Succès
ExportId = Id Export
ExportNumber = Nombre d'URL générées
Expand Down
13 changes: 6 additions & 7 deletions lib/easyurl_function.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function set_easy_url_link(CommonObject $object, string $urlType, string $urlMet
// Do something with the result
$data = json_decode($data);

if ($data->status == 'success') {
if ($data != null && $data->status == 'success') {
if ($urlType != 'none') {
$object->array_options['options_easy_url_' . $urlType . '_link'] = $data->shorturl;
$object->updateExtraField('easy_url_' . $urlType . '_link');
Expand Down Expand Up @@ -156,25 +156,24 @@ function get_easy_url_link(CommonObject $object, string $urlType): int

// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $conf->global->EASYURL_URL_YOURLS_API);
curl_setopt($ch, CURLOPT_URL, getDolGlobalString('EASYURL_URL_YOURLS_API'));
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, [ // Data to POST
'action' => 'url-stats',
'signature' => $conf->global->EASYURL_SIGNATURE_TOKEN_YOURLS_API,
'signature' => getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API'),
'format' => 'json',
'shorturl' => $object->array_options['options_easy_url_' . $urlType . '_link']
]);

// Fetch and return content
$data = curl_exec($ch);
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Do something with the result
$data = json_decode($data);
return $data->statusCode == 200 ? 1 : 0;
return $statusCode == 200;
} else {
return -1;
}
Expand Down
9 changes: 9 additions & 0 deletions view/easyurltools.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@
</div>
</div>
<?php endif;
if (!getDolGlobalString('EASYURL_URL_YOURLS_API') || !getDolGlobalString('EASYURL_SIGNATURE_TOKEN_YOURLS_API')) : ?>
<div class="wpeo-notice notice-warning">
<div class="notice-content">
<div class="notice-title">
<a href="<?php echo dol_buildpath('/custom/easyurl/admin/setup.php', 1); ?>"><strong><?php echo $langs->trans('ApiCredentialsNotCongirate'); ?></strong></a>
</div>
</div>
</div>
<?php endif;

$translations = [
'ExportGenerating' => $langs->transnoentities('ExportGenerating'),
Expand Down

0 comments on commit e61fea6

Please sign in to comment.