1
0

YourlsProxy.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /**
  14. * YourlsProxy
  15. *
  16. * Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
  17. * the result.
  18. */
  19. class YourlsProxy 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', 'yourls');
  30. }
  31. /**
  32. * Overrides the abstract parent function to get contents from YOURLS 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. return [
  42. 'method' => 'POST',
  43. 'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
  44. 'content' => http_build_query(
  45. [
  46. 'signature' => $conf->getKey('signature', 'yourls'),
  47. 'format' => 'json',
  48. 'action' => 'shorturl',
  49. 'url' => $link,
  50. ]
  51. ),
  52. ];
  53. }
  54. /**
  55. * Extracts the short URL from the YOURLS API response.
  56. *
  57. * @access protected
  58. * @param array $data
  59. * @return ?string
  60. */
  61. protected function _extractShortUrl(array $data): ?string
  62. {
  63. if ((int) ($data['statusCode'] ?? 0) === 200) {
  64. return $data['shorturl'] ?? null;
  65. }
  66. return null;
  67. }
  68. }