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.

255 lines
7.9KB

  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\Config;
  9. require_once 'include/plugin.php';
  10. function nodeinfo_wellknown(App $a) {
  11. $nodeinfo = array('links' => array(array('rel' => 'http://nodeinfo.diaspora.software/ns/schema/1.0',
  12. 'href' => App::get_baseurl().'/nodeinfo/1.0')));
  13. header('Content-type: application/json; charset=utf-8');
  14. echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  15. exit;
  16. }
  17. function nodeinfo_init(App $a) {
  18. if (!Config::get('system', 'nodeinfo')) {
  19. http_status_exit(404);
  20. killme();
  21. }
  22. if (($a->argc != 2) OR ($a->argv[1] != '1.0')) {
  23. http_status_exit(404);
  24. killme();
  25. }
  26. $smtp = (function_exists('imap_open') AND !Config::get('system', 'imap_disabled') AND !Config::get('system', 'dfrn_only'));
  27. $nodeinfo = array();
  28. $nodeinfo['version'] = '1.0';
  29. $nodeinfo['software'] = array('name' => 'friendica', 'version' => FRIENDICA_VERSION.'-'.DB_UPDATE_VERSION);
  30. $nodeinfo['protocols'] = array();
  31. $nodeinfo['protocols']['inbound'] = array();
  32. $nodeinfo['protocols']['outbound'] = array();
  33. if (Config::get('system', 'diaspora_enabled')) {
  34. $nodeinfo['protocols']['inbound'][] = 'diaspora';
  35. $nodeinfo['protocols']['outbound'][] = 'diaspora';
  36. }
  37. $nodeinfo['protocols']['inbound'][] = 'friendica';
  38. $nodeinfo['protocols']['outbound'][] = 'friendica';
  39. if (!Config::get('system', 'ostatus_disabled')) {
  40. $nodeinfo['protocols']['inbound'][] = 'gnusocial';
  41. $nodeinfo['protocols']['outbound'][] = 'gnusocial';
  42. }
  43. $nodeinfo['services'] = array();
  44. $nodeinfo['services']['inbound'] = array();
  45. $nodeinfo['services']['outbound'] = array();
  46. $nodeinfo['usage'] = array();
  47. $nodeinfo['openRegistrations'] = ($a->config['register_policy'] != 0);
  48. $nodeinfo['metadata'] = array('nodeName' => $a->config['sitename']);
  49. if (Config::get('system', 'nodeinfo')) {
  50. $nodeinfo['usage']['users'] = array('total' => (int)Config::get('nodeinfo', 'total_users'),
  51. 'activeHalfyear' => (int)Config::get('nodeinfo', 'active_users_halfyear'),
  52. 'activeMonth' => (int)Config::get('nodeinfo', 'active_users_monthly'));
  53. $nodeinfo['usage']['localPosts'] = (int)Config::get('nodeinfo', 'local_posts');
  54. $nodeinfo['usage']['localComments'] = (int)Config::get('nodeinfo', 'local_comments');
  55. if (plugin_enabled('appnet')) {
  56. $nodeinfo['services']['inbound'][] = 'appnet';
  57. }
  58. if (plugin_enabled('appnet') OR plugin_enabled('buffer')) {
  59. $nodeinfo['services']['outbound'][] = 'appnet';
  60. }
  61. if (plugin_enabled('blogger')) {
  62. $nodeinfo['services']['outbound'][] = 'blogger';
  63. }
  64. if (plugin_enabled('dwpost')) {
  65. $nodeinfo['services']['outbound'][] = 'dreamwidth';
  66. }
  67. if (plugin_enabled('fbpost') OR plugin_enabled('buffer')) {
  68. $nodeinfo['services']['outbound'][] = 'facebook';
  69. }
  70. if (plugin_enabled('statusnet')) {
  71. $nodeinfo['services']['inbound'][] = 'gnusocial';
  72. $nodeinfo['services']['outbound'][] = 'gnusocial';
  73. }
  74. if (plugin_enabled('gpluspost') OR plugin_enabled('buffer')) {
  75. $nodeinfo['services']['outbound'][] = 'google';
  76. }
  77. if (plugin_enabled('ijpost')) {
  78. $nodeinfo['services']['outbound'][] = 'insanejournal';
  79. }
  80. if (plugin_enabled('libertree')) {
  81. $nodeinfo['services']['outbound'][] = 'libertree';
  82. }
  83. if (plugin_enabled('buffer')) {
  84. $nodeinfo['services']['outbound'][] = 'linkedin';
  85. }
  86. if (plugin_enabled('ljpost')) {
  87. $nodeinfo['services']['outbound'][] = 'livejournal';
  88. }
  89. if (plugin_enabled('buffer')) {
  90. $nodeinfo['services']['outbound'][] = 'pinterest';
  91. }
  92. if (plugin_enabled('posterous')) {
  93. $nodeinfo['services']['outbound'][] = 'posterous';
  94. }
  95. if (plugin_enabled('pumpio')) {
  96. $nodeinfo['services']['inbound'][] = 'pumpio';
  97. $nodeinfo['services']['outbound'][] = 'pumpio';
  98. }
  99. if ($smtp) {
  100. $nodeinfo['services']['outbound'][] = 'smtp';
  101. }
  102. if (plugin_enabled('tumblr')) {
  103. $nodeinfo['services']['outbound'][] = 'tumblr';
  104. }
  105. if (plugin_enabled('twitter') OR plugin_enabled('buffer')) {
  106. $nodeinfo['services']['outbound'][] = 'twitter';
  107. }
  108. if (plugin_enabled('wppost')) {
  109. $nodeinfo['services']['outbound'][] = 'wordpress';
  110. }
  111. $nodeinfo['metadata']['protocols'] = $nodeinfo['protocols'];
  112. $nodeinfo['metadata']['protocols']['outbound'][] = 'atom1.0';
  113. $nodeinfo['metadata']['protocols']['inbound'][] = 'atom1.0';
  114. $nodeinfo['metadata']['protocols']['inbound'][] = 'rss2.0';
  115. $nodeinfo['metadata']['services'] = $nodeinfo['services'];
  116. if (plugin_enabled('twitter')) {
  117. $nodeinfo['metadata']['services']['inbound'][] = 'twitter';
  118. }
  119. }
  120. header('Content-type: application/json; charset=utf-8');
  121. echo json_encode($nodeinfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  122. exit;
  123. }
  124. function nodeinfo_cron() {
  125. $a = get_app();
  126. // If the plugin 'statistics_json' is enabled then disable it and actrivate nodeinfo.
  127. if (plugin_enabled('statistics_json')) {
  128. Config::set('system', 'nodeinfo', true);
  129. $plugin = 'statistics_json';
  130. $plugins = Config::get('system', 'addon');
  131. $plugins_arr = array();
  132. if ($plugins) {
  133. $plugins_arr = explode(',',str_replace(' ', '',$plugins));
  134. $idx = array_search($plugin, $plugins_arr);
  135. if ($idx !== false) {
  136. unset($plugins_arr[$idx]);
  137. uninstall_plugin($plugin);
  138. Config::set('system', 'addon', implode(', ',$plugins_arr));
  139. }
  140. }
  141. }
  142. if (!Config::get('system', 'nodeinfo')) {
  143. return;
  144. }
  145. $last = Config::get('nodeinfo', 'last_calucation');
  146. if ($last) {
  147. // Calculate every 24 hours
  148. $next = $last + (24 * 60 * 60);
  149. if ($next > time()) {
  150. logger('calculation intervall not reached');
  151. return;
  152. }
  153. }
  154. logger('cron_start');
  155. $users = qu("SELECT `user`.`uid`, `user`.`login_date`, `contact`.`last-item`
  156. FROM `user`
  157. INNER JOIN `profile` ON `profile`.`uid` = `user`.`uid` AND `profile`.`is-default`
  158. INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
  159. WHERE (`profile`.`publish` OR `profile`.`net-publish`) AND `user`.`verified`
  160. AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
  161. AND NOT `user`.`account_expired`");
  162. if (is_array($users)) {
  163. $total_users = count($users);
  164. $active_users_halfyear = 0;
  165. $active_users_monthly = 0;
  166. $halfyear = time() - (180 * 24 * 60 * 60);
  167. $month = time() - (30 * 24 * 60 * 60);
  168. foreach ($users AS $user) {
  169. if ((strtotime($user['login_date']) > $halfyear) OR
  170. (strtotime($user['last-item']) > $halfyear)) {
  171. ++$active_users_halfyear;
  172. }
  173. if ((strtotime($user['login_date']) > $month) OR
  174. (strtotime($user['last-item']) > $month)) {
  175. ++$active_users_monthly;
  176. }
  177. }
  178. Config::set('nodeinfo', 'total_users', $total_users);
  179. logger('total_users: '.$total_users, LOGGER_DEBUG);
  180. Config::set('nodeinfo', 'active_users_halfyear', $active_users_halfyear);
  181. Config::set('nodeinfo', 'active_users_monthly', $active_users_monthly);
  182. }
  183. $posts = qu("SELECT COUNT(*) AS local_posts FROM `thread` WHERE `thread`.`wall` AND `thread`.`uid` != 0");
  184. if (!is_array($posts)) {
  185. $local_posts = -1;
  186. } else {
  187. $local_posts = $posts[0]['local_posts'];
  188. }
  189. Config::set('nodeinfo', 'local_posts', $local_posts);
  190. logger('local_posts: '.$local_posts, LOGGER_DEBUG);
  191. $posts = qu("SELECT COUNT(*) FROM `contact`
  192. INNER JOIN `item` ON `item`.`contact-id` = `contact`.`id` AND `item`.`uid` = `contact`.`uid` AND
  193. `item`.`id` != `item`.`parent` AND `item`.`network` IN ('%s', '%s', '%s')
  194. WHERE `contact`.`self`",
  195. dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_DFRN));
  196. if (!is_array($posts)) {
  197. $local_comments = -1;
  198. } else {
  199. $local_comments = $posts[0]['local_comments'];
  200. }
  201. Config::set('nodeinfo', 'local_comments', $local_comments);
  202. // Now trying to register
  203. $url = 'http://the-federation.info/register/'.$a->get_hostname();
  204. logger('registering url: '.$url, LOGGER_DEBUG);
  205. $ret = fetch_url($url);
  206. logger('registering answer: '.$ret, LOGGER_DEBUG);
  207. logger('cron_end');
  208. Config::set('nodeinfo', 'last_calucation', time());
  209. }