ShlinkProxy.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php declare(strict_types=1);
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. */
  11. namespace PrivateBin\Proxy;
  12. use PrivateBin\Configuration;
  13. use PrivateBin\Json;
  14. /**
  15. * ShlinkProxy
  16. *
  17. * Forwards a URL for shortening to shlink and stores the result.
  18. */
  19. class ShlinkProxy extends AbstractProxy
  20. {
  21. /**
  22. * Overrides the abstract parent function to get the proxy URL.
  23. *
  24. * @param Configuration $conf
  25. * @return string
  26. */
  27. protected function _getProxyUrl(Configuration $conf): string
  28. {
  29. return $conf->getKey('apiurl', 'shlink');
  30. }
  31. /**
  32. * Overrides the abstract parent function to get contents from Shlink API.
  33. *
  34. * @access protected
  35. * @param Configuration $conf
  36. * @param string $link
  37. * @return array
  38. */
  39. protected function _getProxyPayload(Configuration $conf, string $link): array
  40. {
  41. $shlink_api_key = $conf->getKey('apikey', 'shlink');
  42. $body = array(
  43. 'longUrl' => $link,
  44. );
  45. return array(
  46. 'method' => 'POST',
  47. 'header' => "Content-Type: application/json\r\n" .
  48. 'X-Api-Key: ' . $shlink_api_key . "\r\n",
  49. 'content' => Json::encode($body),
  50. );
  51. }
  52. /**
  53. * Extracts the short URL from the shlink API response.
  54. *
  55. * @access protected
  56. * @param array $data
  57. * @return ?string
  58. */
  59. protected function _extractShortUrl(array $data): ?string
  60. {
  61. if (
  62. array_key_exists('shortUrl', $data)
  63. ) {
  64. return $data['shortUrl'];
  65. }
  66. return null;
  67. }
  68. }