Skip to content

Commit

Permalink
Eoxia#45 [Shortener] add: manage edit action with curl for edit in YO…
Browse files Browse the repository at this point in the history
…URLS
  • Loading branch information
nicolas-eoxia committed Jan 4, 2024
1 parent d0ab3b4 commit 6706e62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$object->update($user, true);
}

if ($object->original_url != GETPOST('previous_original_url')) {
update_easy_url_link($object);
}

$actioncomm->label = $langs->trans('ObjectModifyTrigger', $langs->transnoentities(ucfirst($object->element)), $object->ref);
$actioncomm->create($user);
break;
Expand Down
34 changes: 34 additions & 0 deletions lib/easyurl_function.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,37 @@ function get_easy_url_link(CommonObject $object, string $urlType): int
return -1;
}
}

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

// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $conf->global->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' => 'update',
'signature' => $conf->global->EASYURL_SIGNATURE_TOKEN_YOURLS_API,
'format' => 'json',
'shorturl' => $object->label,
'url' => $object->original_url
]);

// 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;
}
1 change: 1 addition & 0 deletions view/shortener/shortener_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="update">';
print '<input type="hidden" name="id" value="' . $object->id . '">';
print '<input type="hidden" name="previous_original_url" value="' . $object->original_url . '">';
if ($backtopage) {
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
}
Expand Down

0 comments on commit 6706e62

Please sign in to comment.