oauth_post_notice.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(__DIR__ . '/../..'));
  21. require_once INSTALLDIR . '/extlib/OAuth.php';
  22. $shortoptions = 't:s:u:';
  23. $longoptions = ['oauth_token=', 'oauth_token_secret=', 'update='];
  24. $helptext = <<<END_OF_VERIFY_HELP
  25. oauth_post_notice.php [options]
  26. Update your status via OAuth
  27. -t --oauth_token access token
  28. -s --oauth_token_secret access token secret
  29. -u --update status update
  30. END_OF_VERIFY_HELP;
  31. $token = null;
  32. $token_secret = null;
  33. $update = null;
  34. require_once INSTALLDIR . '/scripts/commandline.inc';
  35. if (have_option('t', 'oauth_token')) {
  36. $token = get_option_value('t', 'oauth_token');
  37. }
  38. if (have_option('s', 'oauth_token_secret')) {
  39. $token_secret = get_option_value('s', 'oauth_token_secret');
  40. }
  41. if (have_option('u', 'update')) {
  42. $update = get_option_value('u', 'update');
  43. }
  44. if (empty($token)) {
  45. echo "Please specify an access token.\n";
  46. exit(1);
  47. }
  48. if (empty($token_secret)) {
  49. echo "Please specify an access token secret.\n";
  50. exit(1);
  51. }
  52. if (empty($update)) {
  53. echo "You forgot to update your status!\n";
  54. exit(1);
  55. }
  56. $ini = parse_ini_file('oauth.ini');
  57. $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
  58. $endpoint = $ini['apiroot'] . '/statuses/update.xml';
  59. $atok = new OAuthToken($token, $token_secret);
  60. $parsed = parse_url($endpoint);
  61. parse_str($parsed['query'], $params);
  62. $params['status'] = $update;
  63. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  64. try {
  65. $oauthReq = OAuthRequest::from_consumer_and_token(
  66. $consumer,
  67. $atok,
  68. 'POST',
  69. $endpoint,
  70. $params
  71. );
  72. $oauthReq->sign_request($hmac_method, $consumer, $atok);
  73. $httpReq = httpRequest($endpoint, $oauthReq->to_postdata());
  74. echo $httpReq->getBody();
  75. } catch (Exception $e) {
  76. echo "Error! . {$e->getMessage}() . 'HTTP reponse body: " . $httpReq->getBody();
  77. exit(1);
  78. }
  79. function httpRequest($endpoint, $poststr)
  80. {
  81. $request = HTTPClient::start();
  82. $request->setConfig(
  83. [
  84. 'follow_redirects' => true,
  85. 'connect_timeout' => 120,
  86. 'timeout' => 120,
  87. 'ssl_verify_peer' => false,
  88. 'ssl_verify_host' => false,
  89. ]
  90. );
  91. // Turn signed request query string back into an array
  92. parse_str($poststr, $postdata);
  93. return $request->post($endpoint, null, $postdata);
  94. }