NoGafamPlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * GNU Social - a federating social network
  4. * Copyright (C) 2014, Free Software Foundation, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * @package Plugin
  22. * @maintainer kim <kim@surtdelcercle.cat>
  23. */
  24. class NoGafamPlugin extends Plugin
  25. {
  26. const PLUGIN_VERSION = '1.0.0';
  27. static function settings($setting)
  28. {
  29. // Invidious instance
  30. $settings['invidious'] = 'https://invidio.us';
  31. // Nitter url
  32. $settings['nitter'] = 'https://nitter.net';
  33. // config.php settings override the settings in this file
  34. $configsettings = common_config('site','gafam') ?: array();
  35. foreach($configsettings as $configsetting => $value) {
  36. $settings[$configsetting] = $value;
  37. }
  38. if(isset($settings[$setting])) {
  39. return $settings[$setting];
  40. }
  41. else {
  42. return false;
  43. }
  44. }
  45. public function initialize()
  46. {
  47. return true;
  48. }
  49. /**
  50. * Hook notice save
  51. *
  52. * @param Notice &$notice Notice being saved
  53. *
  54. * @return boolean hook value
  55. */
  56. function onStartNoticeSaveWeb(Action $action, Profile $author, &$content, &$options)
  57. {
  58. $content = $this->checkGafamUrls($content);
  59. return true;
  60. }
  61. function checkGafamUrls($content)
  62. {
  63. $invidious = static::settings('invidious');
  64. $nitter = static::settings('nitter');
  65. //possible youtube urls
  66. $content = str_replace('https://youtube.com', $invidious, $content);
  67. $content = str_replace('https://www.youtube.com', $invidious, $content);
  68. $content = str_replace('https://m.youtube.com', $invidious, $content);
  69. //possible twitter urls
  70. $content = str_replace('https://twitter.com', $nitter, $content);
  71. $content = str_replace('https://www.twitter.com', $nitter, $content);
  72. $content = str_replace('https://mobile.twitter.com', $nitter, $content);
  73. $content = str_replace('https://t.co', $nitter, $content);
  74. return $content;
  75. }
  76. public function onPluginVersion(array &$versions)
  77. {
  78. $versions[] = array('name' => 'NoGafam',
  79. 'version' => self::PLUGIN_VERSION,
  80. 'author' => 'Kim',
  81. 'homepage' => 'https://social.surtdelcercle.cat',
  82. 'rawdescription' =>
  83. // TRANS: Plugin description.
  84. _m('A simple script to replace GAFAM urls.'));
  85. return true;
  86. }
  87. }