oembedproxy.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * StatusNet-only extensions to the Twitter-like API
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @package StatusNet
  23. * @author Brion Vibber <brion@status.net>
  24. * @copyright 2010 StatusNet, Inc.
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://status.net/
  27. */
  28. if (!defined('STATUSNET') && !defined('LACONICA')) {
  29. exit(1);
  30. }
  31. /**
  32. * Oembed proxy implementation
  33. *
  34. * This class provides an interface for our JS-side code to pull info on
  35. * links from other sites, using either native oEmbed, our own custom
  36. * handlers, or the noembed.com offsite proxy service as configured.
  37. *
  38. * @category oEmbed
  39. * @package StatusNet
  40. * @author Brion Vibber <brion@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  43. * @link http://status.net/
  44. */
  45. class OembedproxyAction extends OembedAction
  46. {
  47. function handle($args)
  48. {
  49. // Trigger short error responses; not a human-readable web page.
  50. GNUsocial::setApi(true);
  51. // We're not a general oEmbed proxy service; limit to valid sessions.
  52. $token = $this->trimmed('token');
  53. if (!$token || $token != common_session_token()) {
  54. // TRANS: Client error displayed when the session token does not match or is not given.
  55. $this->clientError(_m('There was a problem with your session token. '.
  56. 'Try again, please.'));
  57. }
  58. $format = $this->arg('format');
  59. if ($format && $format != 'json') {
  60. // TRANS: Client exception thrown when requesting a different format than JSON.
  61. throw new ClientException(_m('Invalid format; only JSON supported.'));
  62. }
  63. $url = $this->arg('url');
  64. if (!common_valid_http_url($url)) {
  65. // TRANS: Client exception thrown when not providing a valid URL.
  66. throw new ClientException(_m('Invalid URL.'));
  67. }
  68. $params = array();
  69. if ($this->arg('maxwidth')) {
  70. $params['maxwidth'] = $this->arg('maxwidth');
  71. }
  72. if ($this->arg('maxheight')) {
  73. $params['maxheight'] = $this->arg('maxheight');
  74. }
  75. $data = oEmbedHelper::getObject($url, $params);
  76. $this->init_document('json');
  77. print json_encode($data);
  78. }
  79. }