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