plugin.php 11 KB
Newer Older
Martin's avatar
Martin committed
1 2 3 4
<?php

class NSFWPlugin extends KokenPlugin
{
Martin's avatar
Martin committed
5 6 7 8 9 10
  public $pattern_find_caption_first;
  public $pattern_find_caption_last;
  public $pattern_get_tags;
  public $pattern_tag_replace;
  public $tag_replace_subst;
  public $pattern_contents;
Martin's avatar
Martin committed
11 12 13 14
  public $img_pattern;
  public $img_tag_pattern;
  public $img_tag_subst;

15 16
  public $images_to_hide;

Martin's avatar
Martin committed
17 18
  function __construct()
  {
19
    $this->require_setup = true;
20 21
    $this->images_to_hide = array();

Martin's avatar
Martin committed
22 23 24 25 26
    $this->pattern_find_caption_first = '/[\S\n\s]*<figcaption>/';
    $this->pattern_find_caption_last = '/[\S\n\s]*<\/figcaption>/';
    $this->pattern_tag_replace = '';
    $this->tag_replace_subst = '';
    $this->pattern_get_tags = '/\[nsfw\](.*)\[\/nsfw\]/';
Martin's avatar
Martin committed
27 28
    $this->img_pattern = '/[\D\d\n]*"figimg"[\D\d\n]*/';
    $this->img_tag_pattern = '/<img class="content_main/';
Martin's avatar
Martin committed
29
    $this->img_tag_subst = '<img style="/*display: none;*/" id="main_img" class="content_main';
30 31

    $this->register_filter('api.content', 'filter_me');
Martin's avatar
Martin committed
32
    $this->register_filter('site.output', 'output_filter_2');
33
//    $this->register_filter('site.output', 'output_filter');
Martin's avatar
Martin committed
34 35
  }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  function filter_me($filter)
  {
    file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/filter.json", json_encode($filter));
    $tags = explode(",", strtolower($this->data->tags));
    $nsfw = "";
    foreach ($filter['tags'] as $tag)
    {
      if (in_array(strtolower($tag['title']), $tags))
        $nsfw .= $tag['title'] . ", ";
    }
    $nsfw = trim($nsfw, ", ");
    $this->images_to_hide[$filter['__koken_url']] = $nsfw;
    file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/images_to_hide.json", json_encode($this->images_to_hide));
    return $filter;
  }
Martin's avatar
Martin committed
51

Martin's avatar
Martin committed
52
    function output_filter_2($contents)
Martin's avatar
Martin committed
53
    {
54
      $images_to_hide = file_get_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/images_to_hide.json");
Martin's avatar
Martin committed
55 56 57
      if (!$this->data->enabled)
        return $contents;
      $tags = json_encode(explode(",", strtolower($this->data->tags)));
58 59 60
      $conceal_thumbnails = (($this->data->conceal_thumbnails) ? "true" : "false");
      $display_tags = (($this->data->display_tags) ? "true" : "false");
      $allow_thumbnail_reveal = (($this->data->allow_thumbnail_reveal) ? "true" : "false");
Martin's avatar
Martin committed
61 62 63
      $script = "
        <script>
          var tags = JSON.parse('$tags');
64
          var images_to_hide = JSON.parse('$images_to_hide');
65 66 67
          var conceal_thumbnails = $conceal_thumbnails;
          var display_tags = $display_tags;
          var allow_thumbnail_reveal = $allow_thumbnail_reveal;
Martin's avatar
Martin committed
68 69
          console.log(tags);
          var img_container = document.getElementsByClassName('figimg')[0];
70
          var img_entries = document.getElementsByClassName('content');
Martin's avatar
Martin committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
          if (typeof img_container != 'undefined')
          {
            img_container.style.position = 'relative';
            var child = document.createElement('div');
            child.id = \"cover\";
            child.style.width = \"100%\";
            child.style.height = \"100%\";
            child.style.top = '0';
            child.style.left = '0';
            child.style.backgroundColor = \"rgba(0,0,0,.99)\";
            child.style.position = \"absolute\";
            child.style.cursor = 'pointer';
            var text_container = document.createElement('div');
            text_container.innerHTML = '<div>This piece is NSFW (Click here to dismiss)</div>';
            text_container.style.marginTop = '40%';
            text_container.style.fontWeight = 'bold';
            text_container.style.color = 'red';
            text_container.style.textAlign = 'center';
            text_container.onclick = function(event) { event.target.parentElement.click(); event.stopPropagation(); };
            var tag_text = document.createElement('div');
            text_container.appendChild(tag_text);
            child.appendChild(text_container);
            img_container.appendChild(child);
            child.onclick = function(event) { event.target.parentElement.removeChild(event.target) };
            
            var sections = document.getElementsByTagName('section');
            
            var tag_section = null;
            for (var i = 0; i < sections.length; i++)
            {
              if (sections[i].children[0].innerText == 'Tags')
                tag_section = sections[i];
            }
            
105
            if (display_tags)
Martin's avatar
Martin committed
106
            {
107 108 109 110 111 112 113 114
              var img_tags = [];
              for (var i = 0; i < tag_section.children.length; i++)
              {
                if (tags.indexOf(tag_section.children[i].innerText.toLowerCase()) != -1)
                  img_tags.push(tag_section.children[i].innerText);
              }
              
              tag_text.innerHTML = '<span style=\'text-transform: capitalize;\'>' + img_tags.join(', ') + '</span>';
Martin's avatar
Martin committed
115 116 117 118 119 120
            }
            
            console.info(img_tags);
            if (img_tags.length == 0)
              child.click();
          }
121
          else if (img_entries.length > 0 && conceal_thumbnails)
122 123 124 125 126 127 128 129 130 131
          {
            console.log(img_entries);
            var reg = /\/content\/.*\//g;
            for (var i = 0; i < img_entries.length; i++)
            {
              var match = img_entries[i].children[0].children[0].href.match(reg);
              
              if (typeof images_to_hide[match] != 'undefined' && images_to_hide[match] != '')
              {
                console.log('Entry ' + i + ' ' + match + ' has the following tags: ' + images_to_hide[match]);
132 133 134 135 136 137 138 139 140 141 142 143 144
                var figure = img_entries[i].children[0];
                var a = figure.children[0];
                var img = a.children[0];
                
                a.style.position = 'relative';
                
                var div = document.createElement('div');
                div.style.position = 'absolute';
                div.style.top = 0;
                div.style.left = 0;
                div.style.width = '100%';
                div.style.height = '100%';
                div.style.backgroundColor = 'black';
145 146 147
                div.style.transitionProperty = 'opacity';
                div.style.transitionDuration = '10s';
                div.style.transitionDelay = '2s';
148 149 150 151 152 153
                div.className = 'cover';
                
                var text = document.createElement('div');
                text.style.marginTop = '20px';
                text.innerHTML = 'NSFW<br/>';
                
154 155
                if (display_tags)
                  text.innerHTML += '<span style=\'text-transform: capitalize;\'>' + images_to_hide[match] + '</span>';
156 157 158
                
                div.appendChild(text);
                a.appendChild(div);
159 160
              }
            }
161 162
            
            var style = document.createElement('style');
163 164
            if (allow_thumbnail_reveal)
              style.innerHTML = '.cover:hover { opacity: .5; }';
165
            document.head.appendChild(style);
166 167 168
          }
          else
            console.warn('No operations to perform')
Martin's avatar
Martin committed
169 170
        </script>";
      return $contents . $script;
Martin's avatar
Martin committed
171 172
    }

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  function output_filter($contents)
  {
    $split_length = 1000;
    file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/input_page.html", $contents);
    $data = array(
      "tags" => "",
      "caption_first_chunk" => null,
      "caption_last_chunk" => null,
      "img_first_chunk" => null,
    );
    $chunks = str_split($contents, $split_length);
    foreach ($chunks as $key => $chunk)
      file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/chunk_" . $key . ".html", $chunk);
    foreach ($chunks as $key => $chunk)
    {
      $result = preg_match_all($this->pattern_find_caption_first, $chunk, $matches, PREG_SET_ORDER, 0);
      if ($result)
        $data['caption_first_chunk'] = $key;
      $result = preg_match_all($this->pattern_find_caption_last, $chunk, $matches, PREG_SET_ORDER, 0);
      if ($result)
        $data['caption_last_chunk'] = $key;
      $result = preg_match_all($this->img_pattern, $chunk, $matches, PREG_SET_ORDER, 0);
      if ($result)
        $data['img_first_chunk'] = $key;
    }

//    $chunks[$data['nsfw_chunk']] = preg_replace($this->tag_replace_pattern, $this->tag_replace_subst, $chunks[$data['nsfw_chunk']]);

    if (isset($data['caption_first_chunk']) && isset($data['caption_last_chunk']))
    {
      $string = "";
      for ($i = $data['caption_first_chunk']; $i < count($chunks) && $i <= $data['caption_last_chunk']; $i++)
        $string .= $chunks[$i];
      file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/caption_string.html", $string);
      preg_match($this->pattern_get_tags, $string, $matches);
      file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/tag_matches.html", json_encode($matches));
      $data['tags'] = implode(", ", explode(",", $matches[1]));

//      $string = preg_replace($this->pattern_tag_replace, $this->tag_replace_subst, $string);
Martin's avatar
Martin committed
212 213
//
//
214 215 216 217 218 219 220 221 222 223 224
//      if ($data['caption_first_chunk'] == $data['caption_last_chunk'])
//        $chunks[$data['caption_first_chunk']] = $string;
//      else
//      {
//        $string = str_split($string, $split_length);
//        foreach ($string as $key => $str_chunk)
//          $chunks[$data['caption_first_chunk'] + $key] = $str_chunk;
//      }
    }

//    if (isset($data['img_first_chunk']))
Martin's avatar
Martin committed
225
//    {
226 227 228 229
//      $result = preg_replace($this->img_tag_pattern, $this->img_tag_subst, $chunks[$data['img_first_chunk']] . $chunks[$data['img_first_chunk'] + 1]);
//      $result = str_split($result, $split_length);
//      $chunks[$data['img_first_chunk']] = $result[0];
//      $chunks[$data['img_first_chunk'] + 1] = $result[1];
Martin's avatar
Martin committed
230
//    }
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

    $stuff = implode("", $chunks);
    file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/final_page.html", $stuff);

    $script = "";

    if ($data['tags'] != "")
    {
      $script = "
    <script>
      var img_container = document.getElementsByClassName('figimg')[0];
      img_container.style.position = 'relative';
      var child = document.createElement('div');
      child.id = \"cover\";
      child.style.width = \"100%\";
      child.style.height = \"100%\";
      child.style.top = '0';
      child.style.left = '0';
      child.style.backgroundColor = \"rgba(0,0,0,.99)\";
      child.style.position = \"absolute\";
      child.style.cursor = 'pointer';
      var text = document.createElement('div');
      text.innerHTML = '<div>This piece is NSFW</div>' +
       '<div>{tags}</div>';
      text.style.marginTop = '40%';
      text.style.fontWeight = 'bold';
      text.style.color = 'red';
      text.style.textAlign = 'center';
      text.onclick = function(event) { event.target.parentElement.click(); event.stopPropagation(); };
      child.appendChild(text);
      img_container.appendChild(child);
      child.onclick = function(event) { event.target.parentElement.removeChild(event.target) };
    </script>
    ";

      $script = str_replace("{tags}", $data['tags'], $script);
    }

    file_put_contents("/var/www/towerofawesome/art/storage/plugins/nsfw-plugin/data.json", json_encode($data));

    return $stuff . $script;
  }
Martin's avatar
Martin committed
273
}