gnusocial.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * GNU Social Hook
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to AGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/licenses/agpl.html
  9. * @author Omar Vega Ramos <ovruni@gnu.org.pe>
  10. * @copyright (c) 2012 - 2016 Omar Vega Ramos
  11. * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License (AGPL)
  12. */
  13. class gnusocial {
  14. protected $username;
  15. protected $password;
  16. protected $endpoint;
  17. /**
  18. * Registers the main event add method
  19. */
  20. public function __construct()
  21. {
  22. // Hook into routing
  23. Event::add('system.pre_controller', array($this, 'add'));
  24. // Set variables
  25. $this->username = Kohana::config('gnusocial.username');
  26. $this->password = Kohana::config('gnusocial.password');
  27. $this->endpoint = Kohana::config('gnusocial.endpoint');
  28. }
  29. /**
  30. * Adds all the events to the main Ushahidi application
  31. */
  32. public function add()
  33. {
  34. // Only add the events if we are on that controller
  35. if (Router::$controller == 'reports' AND Router::$method == 'index') {
  36. Event::add('ushahidi_action.report_approve', array($this, '_update_gnusocial'));
  37. }
  38. if (Router::$controller == 'reports' AND Router::$method == 'edit') {
  39. Event::add('ushahidi_action.report_edit', array($this, '_update_gnusocial'));
  40. }
  41. }
  42. public function _update_gnusocial()
  43. {
  44. // Access the report approved
  45. $update = Event::$data;
  46. if ($update->incident_active == 1) {
  47. $title = $this->_truncate(urldecode($update->incident_title));
  48. $link = url::base().'reports/view/'.$update->id;
  49. $short_link = $this->_ur1Shorten($link);
  50. // If we can't get ur1.ca to work, try again later
  51. if (empty($short_link)) {
  52. print "\nUnable to get short url for '$title' from ur1.ca. Skipping for now.\n\n";
  53. continue;
  54. }
  55. $status = "$title $short_link";
  56. $this->_post($status);
  57. }
  58. }
  59. /**
  60. * Truncated the title
  61. *
  62. * @param string title
  63. * @return string title truncated to 100 characters
  64. */
  65. private function _truncate($str)
  66. {
  67. if (strlen($str) > 100) {
  68. // truncate at 100 chars
  69. $str = substr($str, 0, 100) . '...';
  70. }
  71. return trim(htmlspecialchars_decode($str));
  72. }
  73. /**
  74. * Shortening the url
  75. *
  76. * @param string url
  77. * @return string url short
  78. */
  79. private function _ur1Shorten($url)
  80. {
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_USERAGENT, "ur1shorten");
  83. curl_setopt($ch, CURLOPT_URL,"http://ur1.ca");
  84. curl_setopt($ch, CURLOPT_POST, 1);
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, "longurl=$url");
  86. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  87. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  88. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  89. $html = curl_exec($ch);
  90. if (!$html) {
  91. printf("url1shorten - cURL error: %s\n", curl_error($ch));
  92. curl_close($ch);
  93. return NULL;
  94. }
  95. curl_close($ch);
  96. $dom = new DOMDocument();
  97. @$dom->loadHTML($html);
  98. $xpath = new DOMXPath($dom);
  99. $hrefs = $xpath->evaluate("/html/body/p[@class='success']/a");
  100. return $hrefs->item(0)->getAttribute('href');
  101. }
  102. /**
  103. * Send report to GNU social instance
  104. *
  105. * @param string text to status
  106. * @return true
  107. */
  108. private function _post($status)
  109. {
  110. $ch = curl_init();
  111. curl_setopt($ch, CURLOPT_USERAGENT, "ushahidi");
  112. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
  113. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  114. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  115. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  116. curl_setopt($ch, CURLOPT_URL, $this->endpoint);
  117. curl_setopt($ch, CURLOPT_POST, 1);
  118. curl_setopt($ch, CURLOPT_POSTFIELDS,
  119. array('status' => $status, 'source' => 'ushahidi'));
  120. curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
  121. $buffer = curl_exec($ch);
  122. if (!$buffer) {
  123. printf("cURL error: %s\n", curl_error($ch));
  124. curl_close($ch);
  125. return false;
  126. }
  127. curl_close($ch);
  128. return true;
  129. }
  130. }
  131. new gnusocial;