nodeinfo_2_0.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. if (!defined('GNUSOCIAL')) {
  3. exit(1);
  4. }
  5. class Nodeinfo_2_0Action extends ApiAction
  6. {
  7. private $plugins;
  8. protected function handle()
  9. {
  10. parent::handle();
  11. $this->plugins = $this->getActivePluginList();
  12. $this->showNodeInfo();
  13. }
  14. public function getActivePluginList()
  15. {
  16. $pluginversions = array();
  17. $plugins = array();
  18. Event::handle('PluginVersion', array(&$pluginversions));
  19. foreach ($pluginversions as $plugin) {
  20. $plugins[strtolower($plugin['name'])] = 1;
  21. }
  22. return $plugins;
  23. }
  24. /*
  25. * Technically, the NodeInfo spec defines 'active' as 'signed in at least once',
  26. * but GNU social doesn't keep track of when users last logged in, so let's return
  27. * the number of users that 'posted at least once', I guess.
  28. */
  29. public function showNodeInfo()
  30. {
  31. $openRegistrations = $this->getRegistrationsStatus();
  32. $userCount = $this->getUserCount();
  33. $postCount = $this->getPostCount();
  34. $commentCount = $this->getCommentCount();
  35. $usersActiveHalfyear = $this->getActiveUsers(180);
  36. $usersActiveMonth = $this->getActiveUsers(30);
  37. $protocols = $this->getProtocols();
  38. $inboundServices = $this->getInboundServices();
  39. $outboundServices = $this->getOutboundServices();
  40. $json = json_encode([
  41. 'version' => '2.0',
  42. 'software' => [
  43. 'name' => 'gnusocial',
  44. 'version' => GNUSOCIAL_VERSION
  45. ],
  46. 'protocols' => $protocols,
  47. // TODO: Have plugins register services
  48. 'services' => [
  49. 'inbound' => $inboundServices,
  50. 'outbound' => $outboundServices
  51. ],
  52. 'openRegistrations' => $openRegistrations,
  53. 'usage' => [
  54. 'users' => [
  55. 'total' => $userCount,
  56. 'activeHalfyear' => $usersActiveHalfyear,
  57. 'activeMonth' => $usersActiveMonth
  58. ],
  59. 'localPosts' => $postCount,
  60. 'localComments' => $commentCount
  61. ],
  62. 'metadata' => new stdClass()
  63. ]);
  64. $this->initDocument('json');
  65. print $json;
  66. $this->endDocument('json');
  67. }
  68. public function getRegistrationsStatus()
  69. {
  70. $areRegistrationsClosed = (common_config('site', 'closed')) ? true : false;
  71. $isSiteInviteOnly = (common_config('site', 'inviteonly')) ? true : false;
  72. return !($areRegistrationsClosed || $isSiteInviteOnly);
  73. }
  74. public function getUserCount()
  75. {
  76. $users = new Usage_stats();
  77. $userCount = $users->getUserCount();
  78. return $userCount;
  79. }
  80. public function getPostCount()
  81. {
  82. $posts = new Usage_stats();
  83. $postCount = $posts->getPostCount();
  84. return $postCount;
  85. }
  86. public function getCommentCount()
  87. {
  88. $comments = new Usage_stats();
  89. $commentCount = $comments->getCommentCount();
  90. return $commentCount;
  91. }
  92. public function getActiveUsers($days)
  93. {
  94. $notices = new Notice();
  95. $notices->joinAdd(array('profile_id', 'user:id'));
  96. $notices->whereAdd('notice.created >= NOW() - INTERVAL ' . $days . ' DAY');
  97. $activeUsersCount = $notices->count('distinct profile_id');
  98. return $activeUsersCount;
  99. }
  100. public function getProtocols()
  101. {
  102. $protocols = [];
  103. Event::handle('NodeInfoProtocols', array(&$protocols));
  104. return $protocols;
  105. }
  106. public function getInboundServices()
  107. {
  108. // FIXME: Are those always on?
  109. $inboundServices = array('atom1.0', 'rss2.0');
  110. if (array_key_exists('twitterbridge', $this->plugins) && common_config('twitterimport', 'enabled')) {
  111. $inboundServices[] = 'twitter';
  112. }
  113. if (array_key_exists('ostatus', $this->plugins)) {
  114. $inboundServices[] = 'gnusocial';
  115. }
  116. return $inboundServices;
  117. }
  118. public function getOutboundServices()
  119. {
  120. $xmppEnabled = (array_key_exists('xmpp', $this->plugins) && common_config('xmpp', 'enabled')) ? true : false;
  121. // FIXME: Are those always on?
  122. $outboundServices = array('atom1.0', 'rss2.0');
  123. if (array_key_exists('twitterbridge', $this->plugins)) {
  124. $outboundServices[] = 'twitter';
  125. }
  126. if (array_key_exists('ostatus', $this->plugins)) {
  127. $outboundServices[] = 'gnusocial';
  128. }
  129. if ($xmppEnabled) {
  130. $outboundServices[] = 'xmpp';
  131. }
  132. return $outboundServices;
  133. }
  134. }