Passed
Push — master ( 4829e1...2ca2cb )
by Dongxin
01:44
created

index.js (2 issues)

Severity
1
/* global hexo, __dirname */
2
3
'use strict';
4
5
const fs = require('hexo-fs');
6
const path = require('path');
7
const CryptoJS = require('crypto-js');
8
const log = require('hexo-log')({
9
  'debug': false,
10
  'silent': false,
11
});
12
13
hexo.extend.filter.register('after_post_render', function encrypt (data) {
14
15
  // Close the encrypt function
16
  if (!('encrypt' in hexo.config && hexo.config.encrypt && 'enable' in hexo.config.encrypt && hexo.config.encrypt.enable)) {
17
18
    return data;
19
20
  }
21
  if (!('default_template' in hexo.config.encrypt && hexo.config.encrypt.default_template)) { // No such template
22
23
    hexo.config.encrypt.default_template = fs.readFileSync(path.resolve(__dirname, './template.html'));
24
25
  }
26
  if (!('default_abstract' in hexo.config.encrypt && hexo.config.encrypt.default_abstract)) { // No read more info
27
28
    hexo.config.encrypt.default_abstract = 'The article has been encrypted, please enter your password to view.<br>';
29
30
  }
31
  if (!('default_message' in hexo.config.encrypt && hexo.config.encrypt.default_message)) { // No message
32
33
    hexo.config.encrypt.default_message = 'Please enter the password to read the blog.';
34
35
  }
36
  if (!('default_decryption_error' in hexo.config.encrypt && hexo.config.encrypt.default_decryption_error)) { // Wrong password
37
38
    hexo.config.encrypt.default_decryption_error = 'Incorrect Password!';
39
40
  }
41
  if (!('default_no_content_error' in hexo.config.encrypt && hexo.config.encrypt.default_no_content_error)) { // No content
42
43
    hexo.config.encrypt.default_no_content_error = 'No content to display!';
44
45
  }
46
  
47
  var currentTag, needToEncryptTag;
48
  if (!('password' in data && data.password) && ('tags' in hexo.config.encrypt)) {
49
    outer:
50
    for (var i in data.tags.data) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
51
      currentTag = data.tags.data[i].name
52
      for (var j in hexo.config.encrypt.tags) {
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
53
        needToEncryptTag = hexo.config.encrypt.tags[j].name
54
        if (currentTag == needToEncryptTag) {
55
          data.password = hexo.config.encrypt.tags[j].password;
56
          break outer;
57
        }
58
      }
59
    }
60
  }
61
62
  if ('password' in data && data.password) {
63
64
    // Use the blog's config first
65
    log.info(`Encrypted the blog: ${ data.title.trim() }`);
66
67
    // Store the origin data
68
    data.origin = data.content;
69
    data.encrypt = true;
70
71
    if (!('abstract' in data && data.abstract)) {
72
73
      data.abstract = hexo.config.encrypt.default_abstract;
74
75
    }
76
    if (!('template' in data && data.template)) {
77
78
      data.template = hexo.config.encrypt.default_template;
79
80
    }
81
    if (!('message' in data && data.message)) {
82
83
      data.message = hexo.config.encrypt.default_message;
84
85
    }
86
    if (!('decryptionError' in data && data.decryptionError)) {
87
88
      data.decryptionError = hexo.config.encrypt.default_decryption_error;
89
90
    }
91
    if (!('noContentError' in data && data.noContentError)) {
92
93
      data.noContentError = hexo.config.encrypt.default_no_content_error;
94
95
    }
96
97
    if (data.content.trim() === '') {
98
99
      log.warn('Warning: Your blog has no content, it may cause error when decrypting.');
100
101
    }
102
103
    data.content = escape(data.content);
104
    data.content = CryptoJS.enc.Utf8.parse(data.content);
105
    data.content = CryptoJS.enc.Base64.stringify(data.content);
106
    data.content = CryptoJS.AES.encrypt(data.content, String(data.password)).toString();
107
108
    data.template = data.template.replace('{{content}}', data.content);
109
    data.template = data.template.replace('{{message}}', data.message);
110
    data.template = data.template.replace('{{message}}', data.message);
111
    data.template = data.template.replace('{{decryptionError}}', data.decryptionError);
112
    data.template = data.template.replace('{{noContentError}}', data.noContentError);
113
114
    data.content = data.template;
115
    data.content += `<script src="${hexo.config.root}lib/crypto-js.js"></script>`;
116
    data.content += `<script src="${hexo.config.root}lib/blog-encrypt.js"></script>`;
117
    data.content += `<link href="${hexo.config.root}css/blog-encrypt.css" rel="stylesheet" type="text/css">`;
118
119
    data.more = data.abstract;
120
    data.excerpt = data.more;
121
122
  }
123
124
  return data;
125
126
});
127
128
hexo.extend.generator.register('blog-encrypt', () => [
129
  {
130
    'data': () => fs.createReadStream(path.resolve(path.dirname(require.resolve('crypto-js')), 'crypto-js.js')),
131
    'path': 'lib/crypto-js.js',
132
  }, {
133
    'data': function () {
134
135
      const Readable = require('stream').Readable;
136
      const stream = new Readable();
137
      stream.push(fs.readFileSync(path.resolve(__dirname, 'lib/blog-encrypt.js'))
138
        .replace('callBackReplaceHere', hexo.config.encrypt && hexo.config.encrypt.enable && hexo.config.encrypt.callback ? hexo.config.encrypt.callback : ''));
139
      stream.push(null); // Indicates the end of the stream
140
      return stream;
141
142
    },
143
    'path': 'lib/blog-encrypt.js',
144
  }, {
145
    'data': () => fs.createReadStream(path.resolve(__dirname, 'lib/blog-encrypt.css')),
146
    'path': 'css/blog-encrypt.css',
147
  },
148
]);
149