SensitiveContentPlugin.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. if (!defined('GNUSOCIAL')) {
  3. exit(1);
  4. }
  5. class SensitiveContentPlugin extends Plugin
  6. {
  7. const PLUGIN_VERSION = '0.0.1';
  8. function onPluginVersion(array &$versions)
  9. {
  10. $versions[] = array('name' => 'Sensitive Content',
  11. 'version' => self::PLUGIN_VERSION,
  12. 'author' => 'MoonMan',
  13. 'homepage' => 'https://gitgud.io/ShitposterClub/SensitiveContent/',
  14. 'description' =>
  15. _m('Mark, hide/show sensitive notices like on Twitter.'));
  16. return true;
  17. }
  18. static function settings($setting)
  19. {
  20. $settings['blockerimage'] = Plugin::staticPath('SensitiveContent', '').'img/blocker.png';
  21. $configphpsettings = common_config('site','sensitivecontent') ?: array();
  22. foreach($configphpsettings as $configphpsetting=>$value) {
  23. $settings[$configphpsetting] = $value;
  24. }
  25. if(isset($settings[$setting])) {
  26. return $settings[$setting];
  27. }
  28. else FALSE;
  29. }
  30. function onNoticeSimpleStatusArray($notice, &$twitter_status, $scoped)
  31. {
  32. $twitter_status['tags'] = $notice->getTags();
  33. }
  34. function onTwitterUserArray($profile, &$twitter_user, $scoped)
  35. {
  36. if ($scoped instanceof Profile && $scoped->sameAs($profile)) {
  37. $twitter_user['hide_sensitive'] = $this->getHideSensitive($scoped);
  38. }
  39. }
  40. public function onRouterInitialized(URLMapper $m)
  41. {
  42. $m->connect('settings/sensitivecontent',
  43. ['action' => 'sensitivecontentsettings']);
  44. }
  45. function onEndAccountSettingsNav($action)
  46. {
  47. $action->menuItem(common_local_url('sensitivecontentsettings'),
  48. _m('MENU', 'Sensitive Content'),
  49. _m('Settings for display of sensitive content.'));
  50. return true;
  51. }
  52. public function onQvitterEndShowHeadElements(Action $action)
  53. {
  54. $blocker = static::settings('blockerimage');
  55. common_log( LOG_DEBUG, "SENSITIVECONTENT " . $blocker );
  56. $styles = <<<EOB
  57. .sensitive-blocker {
  58. display: none;
  59. }
  60. div.stream-item.notice.sensitive-notice .sensitive-blocker {
  61. display: block;
  62. width: 100%;
  63. height: 100%;
  64. position: absolute;
  65. z-index: 100;
  66. background-color: #d4baba;
  67. background-image: url($blocker);
  68. background-repeat: no-repeat;
  69. background-position: center center;
  70. background-size: contain;
  71. transition: opacity 1s ease-in-out;
  72. }
  73. .sensitive-blocker:hover {
  74. opacity: .5;
  75. }
  76. div.stream-item.notice.expanded.sensitive-notice .sensitive-blocker {
  77. display: none;
  78. background-color: transparent;
  79. background-image: none;
  80. }
  81. EOB;
  82. $action->style($styles);
  83. }
  84. function onQvitterEndShowScripts(Action $action)
  85. {
  86. $action->script( Plugin::staticPath('SensitiveContent', '').'js/sensitivecontent.js' );
  87. }
  88. function onEndShowStyles(Action $action)
  89. {
  90. $blocker = static::settings('blockerimage');
  91. $styles = <<<EOB
  92. /* default no show */
  93. html .tagcontainer > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker {
  94. display: none;
  95. }
  96. html[data-hidesensitive='true'] .tagcontainer.data-tag-nsfw > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker {
  97. display: block;
  98. width: 100%;
  99. height: 100%;
  100. position: absolute;
  101. z-index: 100;
  102. /*background-color: #d4baba;*/
  103. background-color: black;
  104. background-image: url($blocker);
  105. background-repeat: no-repeat;
  106. background-position: center center;
  107. background-size: contain;
  108. transition: opacity 1s ease-in-out;
  109. }
  110. html[data-hidesensitive='true'] .tagcontainer.data-tag-nsfw > footer > .attachments > .inline-attachment > .attachment-wrapper > .sensitive-blocker.reveal {
  111. opacity: 0;
  112. }
  113. EOB;
  114. $action->style($styles);
  115. }
  116. function onStartShowAttachmentRepresentation($out, $file)
  117. {
  118. $profile = Profile::current();
  119. if (!is_null($profile) && $profile instanceof Profile)
  120. {
  121. $hidesensitive = $this->getHideSensitive($profile);
  122. }
  123. else
  124. {
  125. $hidesensitive = false;
  126. }
  127. $classes = "sensitive-blocker"; //'sensitive-blocker';
  128. $out->elementStart('div', array(
  129. 'class'=>'attachment-wrapper',
  130. 'style'=>'height: ' . $file->getThumbnail()->height . 'px; width: ' . $file->getThumbnail()->width . 'px;'
  131. )); /*needs height of thumb*/
  132. $out->elementStart('div', array(
  133. 'class'=>$classes,
  134. 'onclick'=>'toggleSpoiler(event)',
  135. 'style'=>'height: ' . $file->getThumbnail()->height . 'px; width: ' . $file->getThumbnail()->width . 'px;'
  136. ));
  137. $out->raw('&nbsp;');
  138. $out->elementEnd('div');
  139. }
  140. function onEndShowAttachmentRepresentation($out, $file)
  141. {
  142. $out->elementEnd('div');
  143. }
  144. function onEndShowScripts(Action $action)
  145. {
  146. $profile = $action->getScoped();
  147. if (!is_null($profile) && $profile instanceof Profile)
  148. {
  149. $hidesensitive = $this->getHideSensitive($profile) ? "true" : "false";
  150. }
  151. else
  152. {
  153. $hidesensitive = "false";
  154. }
  155. $inline = <<<EOB
  156. window.hidesensitive = $hidesensitive ;
  157. function toggleSpoiler(evt) {
  158. if (window.hidesensitive) evt.target.classList.toggle('reveal');
  159. }
  160. EOB;
  161. $action->inlineScript($inline);
  162. }
  163. function onEndOpenNoticeListItemElement(NoticeListItem $nli)
  164. {
  165. $rawtags = $nli->getNotice()->getTags();
  166. $classes = "tagcontainer";
  167. foreach($rawtags as $tag)
  168. {
  169. $classes = $classes . ' data-tag-' . $tag;
  170. }
  171. $nli->elementStart('span', array('class' => $classes));
  172. //$nli->elementEnd('span');
  173. }
  174. function onStartCloseNoticeListItemElement(NoticeListItem $nli)
  175. {
  176. $nli->elementEnd('span');
  177. }
  178. function onStartHtmlElement($action, &$attrs) {
  179. $profile = Profile::current();
  180. if (!is_null($profile) && $profile instanceof Profile)
  181. {
  182. $hidesensitive = $this->getHideSensitive($profile);
  183. }
  184. else
  185. {
  186. $hidesensitive = false;
  187. }
  188. $attrs = array_merge($attrs,
  189. array('data-hidesensitive' => ($hidesensitive ? "true" : "false"))
  190. );
  191. }
  192. function getHideSensitive($profile) {
  193. $c = Cache::instance();
  194. /*
  195. if (!empty($c)) {
  196. $hidesensitive = $c->get(Cache::key('profile:hide_sensitive:'.$profile->id));
  197. if (is_numeric($hidesensitive)) {
  198. return (boolean) $hidesensitive;
  199. }
  200. else return FALSE;
  201. }
  202. */
  203. $hidesensitive = $profile->getPref('MoonMan', 'hide_sensitive', '0');
  204. if (!empty($c)) {
  205. //not using it yet.
  206. $c->set(Cache::key('profile:hide_sensitive:'.$profile->id), $hidesensitive);
  207. }
  208. //common_log(LOG_DEBUG, "SENSITIVECONTENT hidesensitive? id " . $profile->id . " value " . (boolean)$hidesensitive );
  209. if (is_null($hidesensitive)) {
  210. return FALSE;
  211. } else
  212. if (is_numeric($hidesensitive)) {
  213. return (boolean) $hidesensitive;
  214. }
  215. else return FALSE;
  216. }
  217. }