Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.1KB

  1. <?php
  2. /**
  3. * @file mod/nodeinfo.php
  4. *
  5. * Documentation: http://nodeinfo.diaspora.software/schema.html
  6. */
  7. use Friendica\App;
  8. use Friendica\Core\Addon;
  9. use Friendica\Core\Config;
  10. use Friendica\Core\Logger;
  11. use Friendica\Core\System;
  12. use Friendica\Database\DBA;
  13. use Friendica\Util\Network;
  14. function nodeinfo_wellknown(App $a) {
  15. $nodeinfo = ['links' => [['rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
  16. 'href' => System::baseUrl().'/nodeinfo/1.0']]];
  17. header('Content-type: application/json; charset=utf-8');
  18. echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  19. exit;
  20. }
  21. function nodeinfo_init(App $a) {
  22. if (!Config::get('system', 'nodeinfo')) {
  23. System::httpExit(404);
  24. killme();
  25. }
  26. if (($a->argc != 2) || ($a->argv[1] != '1.0')) {
  27. System::httpExit(404);
  28. killme();
  29. }
  30. $smtp = (function_exists('imap_open') && !Config::get('system', 'imap_disabled') && !Config::get('system', 'dfrn_only'));
  31. $nodeinfo = [];
  32. $nodeinfo['version'] = '1.0';
  33. $nodeinfo['software'] = ['name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION];
  34. $nodeinfo['protocols'] = [];
  35. $nodeinfo['protocols']['inbound'] = [];
  36. $nodeinfo['protocols']['outbound'] = [];
  37. if (Config::get('system', 'diaspora_enabled')) {
  38. $nodeinfo['protocols']['inbound'][] = 'diaspora';
  39. $nodeinfo['protocols']['outbound'][] = 'diaspora';
  40. }
  41. $nodeinfo['protocols']['inbound'][] = 'friendica';
  42. $nodeinfo['protocols']['outbound'][] = 'friendica';
  43. if (!Config::get('system', 'ostatus_disabled')) {
  44. $nodeinfo['protocols']['inbound'][] = 'gnusocial';
  45. $nodeinfo['protocols']['outbound'][] = 'gnusocial';
  46. }
  47. $nodeinfo['services'] = [];
  48. $nodeinfo['services']['inbound'] = [];
  49. $nodeinfo['services']['outbound'] = [];
  50. $nodeinfo['usage'] = [];
  51. $nodeinfo['openRegistrations'] = intval(Config::get('config', 'register_policy')) !== REGISTER_CLOSED;
  52. $nodeinfo['metadata'] = ['nodeName' => Config::get('config', 'sitename')];
  53. if (Config::get('system', 'nodeinfo')) {
  54. $nodeinfo['usage']['users'] = ['total' => (int)Config::get('nodeinfo', 'total_users'),
  55. 'activeHalfyear' => (int)Config::get('nodeinfo', 'active_users_halfyear'),
  56. 'activeMonth' => (int)Config::get('nodeinfo', 'active_users_monthly')];
  57. $nodeinfo['usage']['localPosts'] = (int)Config::get('nodeinfo', 'local_posts');
  58. $nodeinfo['usage']['localComments'] = (int)Config::get('nodeinfo', 'local_comments');
  59. if (Addon::isEnabled('blogger')) {
  60. $nodeinfo['services']['outbound'][] = 'blogger';
  61. }
  62. if (Addon::isEnabled('dwpost')) {
  63. $nodeinfo['services']['outbound'][] = 'dreamwidth';
  64. }
  65. if (Addon::isEnabled('statusnet')) {
  66. $nodeinfo['services']['inbound'][] = 'gnusocial';
  67. $nodeinfo['services']['outbound'][] = 'gnusocial';
  68. }
  69. if (Addon::isEnabled('gpluspost') || Addon::isEnabled('buffer')) {
  70. $nodeinfo['services']['outbound'][] = 'google';
  71. }
  72. if (Addon::isEnabled('ijpost')) {
  73. $nodeinfo['services']['outbound'][] = 'insanejournal';
  74. }
  75. if (Addon::isEnabled('libertree')) {
  76. $nodeinfo['services']['outbound'][] = 'libertree';
  77. }
  78. if (Addon::isEnabled('buffer')) {
  79. $nodeinfo['services']['outbound'][] = 'linkedin';
  80. }
  81. if (Addon::isEnabled('ljpost')) {
  82. $nodeinfo['services']['outbound'][] = 'livejournal';
  83. }
  84. if (Addon::isEnabled('buffer')) {
  85. $nodeinfo['services']['outbound'][] = 'pinterest';
  86. }
  87. if (Addon::isEnabled('posterous')) {
  88. $nodeinfo['services']['outbound'][] = 'posterous';
  89. }
  90. if (Addon::isEnabled('pumpio')) {
  91. $nodeinfo['services']['inbound'][] = 'pumpio';
  92. $nodeinfo['services']['outbound'][] = 'pumpio';
  93. }
  94. if ($smtp) {
  95. $nodeinfo['services']['outbound'][] = 'smtp';
  96. }
  97. if (Addon::isEnabled('tumblr')) {
  98. $nodeinfo['services']['outbound'][] = 'tumblr';
  99. }
  100. if (Addon::isEnabled('twitter') || Addon::isEnabled('buffer')) {
  101. $nodeinfo['services']['outbound'][] = 'twitter';
  102. }
  103. if (Addon::isEnabled('wppost')) {
  104. $nodeinfo['services']['outbound'][] = 'wordpress';
  105. }
  106. $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
  107. $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
  108. $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
  109. $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
  110. $nodeinfo['metadata']['services'] = $nodeinfo['services'];
  111. if (Addon::isEnabled('twitter')) {
  112. $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
  113. }
  114. $nodeinfo['metadata']['explicitContent'] = Config::get('system', 'explicit_content', false) == true;
  115. }
  116. header('Content-type: application/json; charset=utf-8');
  117. echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  118. exit;
  119. }
  120. function nodeinfo_cron() {
  121. $a = get_app();
  122. // If the addon 'statistics_json' is enabled then disable it and activate nodeinfo.
  123. if (Addon::isEnabled('statistics_json')) {
  124. Config::set('system', 'nodeinfo', true);
  125. $addon = 'statistics_json';
  126. $addons = Config::get('system', 'addon');
  127. $addons_arr = [];
  128. if ($addons) {
  129. $addons_arr = explode(',',str_replace(' ', '',$addons));
  130. $idx = array_search($addon, $addons_arr);
  131. if ($idx !== false) {
  132. unset($addons_arr[$idx]);
  133. Addon::uninstall($addon);
  134. Config::set('system', 'addon', implode(', ',$addons_arr));
  135. }
  136. }
  137. }
  138. if (!Config::get('system', 'nodeinfo')) {
  139. return;
  140. }
  141. Logger::log('cron_start');
  142. $users = q("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
  143. FROM `user`
  144. INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
  145. INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
  146. WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
  147. AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
  148. AND NOT `user`.`account_expired`");
  149. if (is_array($users)) {
  150. $total_users = count($users);
  151. $active_users_halfyear = 0;
  152. $active_users_monthly = 0;
  153. $halfyear = time() - (180 * 24 * 60 * 60);
  154. $month = time() - (30 * 24 * 60 * 60);
  155. foreach ($users AS $user) {
  156. if ((strtotime($user['login_date']) > $halfyear) ||
  157. (strtotime($user['last-item']) > $halfyear)) {
  158. ++$active_users_halfyear;
  159. }
  160. if ((strtotime($user['login_date']) > $month) ||
  161. (strtotime($user['last-item']) > $month)) {
  162. ++$active_users_monthly;
  163. }
  164. }
  165. Config::set('nodeinfo', 'total_users', $total_users);
  166. Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
  167. Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
  168. Logger::log('total_users: ' . $total_users . '/' . $active_users_halfyear. '/' . $active_users_monthly, Logger::DEBUG);
  169. }
  170. $local_posts = DBA::count('thread', ["`wall` AND NOT `deleted` AND `uid` != 0"]);
  171. Config::set('nodeinfo', 'local_posts', $local_posts);
  172. Logger::log('local_posts: ' . $local_posts, Logger::DEBUG);
  173. $local_comments = DBA::count('item', ["`origin` AND `id` != `parent` AND NOT `deleted` AND `uid` != 0"]);
  174. Config::set('nodeinfo', 'local_comments', $local_comments);
  175. Logger::log('local_comments: ' . $local_comments, Logger::DEBUG);
  176. // Now trying to register
  177. $url = 'http://the-federation.info/register/'.$a->getHostName();
  178. Logger::log('registering url: '.$url, Logger::DEBUG);
  179. $ret = Network::fetchUrl($url);
  180. Logger::log('registering answer: '.$ret, Logger::DEBUG);
  181. Logger::log('cron_end');
  182. }