webmention.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. if (!defined('STATUSNET')) {
  17. exit(1);
  18. }
  19. class WebmentionAction extends Action
  20. {
  21. protected function handle()
  22. {
  23. GNUsocial::setApi(true); // Minimize error messages to aid in debugging
  24. parent::handle();
  25. if ($this->isPost()) {
  26. return $this->handlePost();
  27. }
  28. return false;
  29. }
  30. function handlePost()
  31. {
  32. $source = $this->arg('source');
  33. $target = $this->arg('target');
  34. header('Content-Type: text/plain; charset=utf-8');
  35. if(!$source) {
  36. echo _m('"source" is missing')."\n";
  37. throw new ClientException(_m('"source" is missing'), 400);
  38. }
  39. if(!$target) {
  40. echo _m('"target" is missing')."\n";
  41. throw new ClientException(_m('"target" is missing'), 400);
  42. }
  43. $response = linkback_get_source($source, $target);
  44. if(!$response) {
  45. echo _m('Source does not link to target.')."\n";
  46. throw new ClientException(_m('Source does not link to target.'), 400);
  47. }
  48. $notice = linkback_get_target($target);
  49. if(!$notice) {
  50. echo _m('Target not found')."\n";
  51. throw new ClientException(_m('Target not found'), 404);
  52. }
  53. $url = linkback_save($source, $target, $response, $notice);
  54. if(!$url) {
  55. echo _m('An error occured while saving.')."\n";
  56. throw new ClientException(_m('An error occured while saving.'), 500);
  57. }
  58. echo $url."\n";
  59. return true;
  60. }
  61. }