PageRenderTime 104ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/nsfw/nsfw.php

https://github.com/chiefdome/friendica-addons
PHP | 159 lines | 105 code | 42 blank | 12 comment | 17 complexity | ba1f90fea608d94b7a03f6d26e65f5e7 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0
  1. <?php
  2. /**
  3. * Name: NSFW
  4. * Description: Collapse posts with inappropriate content
  5. * Version: 1.0
  6. * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
  7. *
  8. */
  9. function nsfw_install() {
  10. register_hook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body', 10);
  11. register_hook('plugin_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
  12. register_hook('plugin_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
  13. }
  14. function nsfw_uninstall() {
  15. unregister_hook('prepare_body', 'addon/nsfw/nsfw.php', 'nsfw_prepare_body');
  16. unregister_hook('plugin_settings', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings');
  17. unregister_hook('plugin_settings_post', 'addon/nsfw/nsfw.php', 'nsfw_addon_settings_post');
  18. }
  19. // This function isn't perfect and isn't trying to preserve the html structure - it's just a
  20. // quick and dirty filter to pull out embedded photo blobs because 'nsfw' seems to come up
  21. // inside them quite often. We don't need anything fancy, just pull out the data blob so we can
  22. // check against the rest of the body.
  23. function nsfw_extract_photos($body) {
  24. $new_body = '';
  25. $img_start = strpos($body,'src="data:');
  26. $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false);
  27. $cnt = 0;
  28. while($img_end !== false) {
  29. $img_end += $img_start;
  30. $new_body = $new_body . substr($body,0,$img_start);
  31. $cnt ++;
  32. $body = substr($body,0,$img_end);
  33. $img_start = strpos($body,'src="data:');
  34. $img_end = (($img_start !== false) ? strpos(substr($body,$img_start),'>') : false);
  35. }
  36. if(! $cnt)
  37. return $body;
  38. return $new_body;
  39. }
  40. function nsfw_addon_settings(&$a,&$s) {
  41. if(! local_user())
  42. return;
  43. /* Add our stylesheet to the page so we can make our settings look nice */
  44. $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/nsfw/nsfw.css' . '" media="all" />' . "\r\n";
  45. $enable_checked = (intval(get_pconfig(local_user(),'nsfw','disable')) ? '' : ' checked="checked" ');
  46. $words = get_pconfig(local_user(),'nsfw','words');
  47. if(! $words)
  48. $words = 'nsfw,';
  49. $s .= '<div class="settings-block">';
  50. $s .= '<h3>' . t('Not Safe For Work (General Purpose Content Filter) settings') . '</h3>';
  51. $s .= '<div id="nsfw-wrapper">';
  52. $s .= '<p>' . t ('This plugin looks in posts for the words/text you specify below, and collapses any content containing those keywords so it is not displayed at inappropriate times, such as sexual innuendo that may be improper in a work setting. It is polite and recommended to tag any content containing nudity with #NSFW. This filter can also match any other word/text you specify, and can thereby be used as a general purpose content filter.') . '</p>';
  53. $s .= '<label id="nsfw-enable-label" for="nsfw-enable">' . t('Enable Content filter') . ' </label>';
  54. $s .= '<input id="nsfw-enable" type="checkbox" name="nsfw-enable" value="1"' . $enable_checked . ' />';
  55. $s .= '<div class="clear"></div>';
  56. $s .= '<label id="nsfw-label" for="nsfw-words">' . t('Comma separated list of keywords to hide') . ' </label>';
  57. $s .= '<input id="nsfw-words" type="text" name="nsfw-words" value="' . $words .'" />';
  58. $s .= '</div><div class="clear"></div>';
  59. $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="nsfw-submit" name="nsfw-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
  60. $s .= '<div class="nsfw-desc">' . t('Use /expression/ to provide regular expressions') . '</div></div>';
  61. return;
  62. }
  63. function nsfw_addon_settings_post(&$a,&$b) {
  64. if(! local_user())
  65. return;
  66. if($_POST['nsfw-submit']) {
  67. set_pconfig(local_user(),'nsfw','words',trim($_POST['nsfw-words']));
  68. $enable = ((x($_POST,'nsfw-enable')) ? intval($_POST['nsfw-enable']) : 0);
  69. $disable = 1-$enable;
  70. set_pconfig(local_user(),'nsfw','disable', $disable);
  71. info( t('NSFW Settings saved.') . EOL);
  72. }
  73. }
  74. function nsfw_prepare_body(&$a,&$b) {
  75. $words = null;
  76. if(get_pconfig(local_user(),'nsfw','disable'))
  77. return;
  78. if(local_user()) {
  79. $words = get_pconfig(local_user(),'nsfw','words');
  80. }
  81. if($words) {
  82. $arr = explode(',',$words);
  83. }
  84. else {
  85. $arr = array('nsfw');
  86. }
  87. $found = false;
  88. if(count($arr)) {
  89. $body = nsfw_extract_photos($b['html']);
  90. foreach($arr as $word) {
  91. $word = trim($word);
  92. if(! strlen($word)) {
  93. continue;
  94. }
  95. if(strpos($word,'/') === 0) {
  96. if(preg_match($word,$body)) {
  97. $found = true;
  98. break;
  99. }
  100. }
  101. else {
  102. if(stristr($body,$word)) {
  103. $found = true;
  104. break;
  105. }
  106. if(stristr($b['item']['tag'], ']' . $word . '[' )) {
  107. $found = true;
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. if($found) {
  114. $rnd = random_string(8);
  115. $b['html'] = '<div id="nsfw-wrap-' . $rnd . '" class="fakelink" onclick=openClose(\'nsfw-' . $rnd . '\'); >' . sprintf( t('%s - Click to open/close'),$word ) . '</div><div id="nsfw-' . $rnd . '" style="display: none; " >' . $b['html'] . '</div>';
  116. }
  117. }