Skip to content

Commit

Permalink
#12 #13 [Hook] add: check url and error code
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Aug 7, 2023
1 parent 9e66ec2 commit 07b49d9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 15 deletions.
26 changes: 15 additions & 11 deletions class/actions_tinyurl.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* \brief TinyURL hook overload
*/

// Load TinyURL libraries
require_once __DIR__ . '/../lib/tinyurl_function.lib.php';

/**
* Class ActionsTinyurl
*/
Expand Down Expand Up @@ -74,14 +77,18 @@ public function printCommonFooter(array $parameters): int

if (in_array($parameters['currentcontext'], ['propalcard', 'ordercard', 'invoicecard'])) {
if ($object->status > $object::STATUS_DRAFT) {
$jQueryElement = '.' . $object->element . '_extras_tiny_url_link';
$output = '<a class="reposition editfielda" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=set_tiny_url&token=' . newToken() . '">';
$output .= img_picto($langs->trans('SetTinyURLLink'), 'fontawesome_fa-redo_fas_#444', 'class="paddingright pictofixedwidth valignmiddle"') . '</a>'; ?>
<script>
let objectElement = <?php echo "'" . $jQueryElement . "'"; ?>;
jQuery(objectElement).prepend(<?php echo json_encode($output); ?>);
</script>
<?php
$checkTinyUrlLink = get_tiny_url_link($object);
if ($checkTinyUrlLink == 0) {
$jQueryElement = '.' . $object->element . '_extras_tiny_url_link';
$output = '<a class="reposition editfielda" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=set_tiny_url&token=' . newToken() . '">';
$output .= img_picto($langs->trans('SetTinyURLLink'), 'fontawesome_fa-redo_fas_#444', 'class="paddingright pictofixedwidth valignmiddle"') . '</a>';
$output .= '</span>' . img_picto($langs->trans('GetTinyURLErrors'), 'fontawesome_fa-exclamation-triangle_fas_#bc9526') . '</span>'; ?>
<script>
let objectElement = <?php echo "'" . $jQueryElement . "'"; ?>;
jQuery(objectElement).prepend(<?php echo json_encode($output); ?>);
</script>
<?php
}
}
}

Expand All @@ -100,9 +107,6 @@ public function doActions(array $parameters, $object, string $action): int
{
if (in_array($parameters['currentcontext'], ['propalcard', 'ordercard', 'invoicecard'])) {
if ($action == 'set_tiny_url') {
// Load TinyURL libraries
require_once __DIR__ . '/../lib/tinyurl_function.lib.php';

set_tiny_url_link($object);

header('Location: ' . $_SERVER['PHP_SELF'] . '?id=' . $object->id);
Expand Down
2 changes: 2 additions & 0 deletions langs/fr_FR/tinyurl.lang
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ TinyUrlLinkHelp = Transformation d'une URL en short URL (Lien de paiement)
SetTinyURLLink = Créer short URL
SetTinyURLSuccess = Création réussie d'une short URL
SetTinyURLErrors = Erreur : création d'une short URL
GetTinyURLErrors = Erreur : short URL non trouvée

59 changes: 55 additions & 4 deletions lib/tinyurl_function.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,20 @@ function set_tiny_url_link(CommonObject $object) {
require_once DOL_DOCUMENT_ROOT . '/core/lib/ticket.lib.php';

$object->fetch($object->id);
$onlinePaymentURL = getOnlinePaymentUrl(0, 'invoice', $object->ref);
switch ($object->element) {
case 'commande' :
$type = 'order';
break;
case 'facture' :
$type = 'invoice';
break;
default :
$type = $object->element;
break;
}
$onlinePaymentURL = getOnlinePaymentUrl(0, $type, $object->ref);

$title = dol_sanitizeFileName(strtolower($conf->global->MAIN_INFO_SOCIETE_NOM) . '-' . strtolower($object->ref) . (getDolGlobalInt('TINYURL_USE_SHA_URL') ? '-' . generate_random_id(8) : ''));
$title = dol_sanitizeFileName(dol_strtolower($conf->global->MAIN_INFO_SOCIETE_NOM) . '-' . strtolower($object->ref) . (getDolGlobalInt('TINYURL_USE_SHA_URL') ? '-' . generate_random_id(8) : ''));

// Init the CURL session
$ch = curl_init();
Expand All @@ -63,12 +74,52 @@ function set_tiny_url_link(CommonObject $object) {

// Do something with the result
$data = json_decode($data);
$object->array_options['options_tiny_url_link'] = $data->shorturl;
$object->update($user, false);
if ($data->status == 'success') {
$object->array_options['options_tiny_url_link'] = $data->shorturl;
$object->update($user, false);
setEventMessage($langs->trans('SetTinyURLSuccess'));
} else {
setEventMessage($langs->trans('SetTinyURLErrors'), 'errors');
}
}
}

/**
* get tiny url link
*
* @param CommonObject $object Object
* @return int 0 < on error, 1 = statusCode 200, 0 = other statusCode (ex : 404)
*/
function get_tiny_url_link(CommonObject $object) {
global $conf;

$useOnlinePayment = (isModEnabled('paypal') || isModEnabled('stripe') || isModEnabled('paybox'));
$checkConf = getDolGlobalString('TINYURL_URL_YOURLS_API') && getDolGlobalString('TINYURL_SIGNATURE_TOKEN_YOURLS_API');
if ($useOnlinePayment && $checkConf) {
$object->fetch($object->id);

// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $conf->global->TINYURL_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->TINYURL_SIGNATURE_TOKEN_YOURLS_API,
'format' => 'json',
'shorturl' => $object->array_options['options_tiny_url_link']
]);

// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);

// Do something with the result
$data = json_decode($data);
return $data->statusCode == 200 ? 1 : 0;
} else {
return -1;
}
}

0 comments on commit 07b49d9

Please sign in to comment.