1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var fs = require('fs-extra'),
|
11 | merge = require('utils-merge'),
|
12 | path = require('path'),
|
13 | logger,
|
14 | appSettings;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var Utilities = function (resources) {
|
28 | logger = resources.logger;
|
29 | appSettings = resources.settings;
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | Utilities.prototype.isValidObjectID = function (str) {
|
38 |
|
39 | str = str + '';
|
40 | var len = str.length,
|
41 | valid = false;
|
42 | if (len === 12 || len === 24) {
|
43 | valid = /^[0-9a-fA-F]+$/.test(str);
|
44 | }
|
45 | return valid;
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | Utilities.prototype.run_cmd = function (cmd, args, callBack) {
|
56 | var spawn = require('child_process').spawn;
|
57 | var child = spawn(cmd, args);
|
58 | var resp = '';
|
59 |
|
60 | child.stdout.on('data', function (buffer) {
|
61 | resp += buffer.toString();
|
62 | });
|
63 | child.stdout.on('end', function () {
|
64 | callBack(resp);
|
65 | });
|
66 |
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | Utilities.prototype.async_run_cmd = function (cmd, args, asynccallback, callback) {
|
78 |
|
79 |
|
80 | var spawn = require('child_process').spawn;
|
81 | var child = spawn(cmd, args);
|
82 |
|
83 |
|
84 | child.stdout.on('error', function (err) {
|
85 | console.log('got error callback');
|
86 | callback(err, null);
|
87 | });
|
88 | child.stdout.on('data', function (buffer) {
|
89 | asynccallback(buffer.toString());
|
90 | });
|
91 | child.stderr.on('data', function (buffer) {
|
92 | asynccallback(buffer.toString());
|
93 | });
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | child.on('exit', function () {
|
103 | if(logger){
|
104 | logger.silly('got exit callback');
|
105 | }
|
106 | callback(null, 'command run: ' + cmd + ' ' + args);
|
107 | });
|
108 | };
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | Utilities.prototype.restart_app = function (options) {
|
115 | var d = new Date(),
|
116 | restartfile = (typeof options === 'object' && options.restartfile)? options.restartfile : path.join(process.cwd(), '/content/config/restart.json');
|
117 |
|
118 | if(logger){
|
119 | logger.silly('application restarted');
|
120 | }
|
121 | fs.outputFile(restartfile, 'restart log ' + d + '- \r\n ', function (err) {
|
122 | if (err) {
|
123 | if(logger){
|
124 | logger.silly('application restarted');
|
125 | }
|
126 | else{
|
127 | console.error(err);
|
128 | }
|
129 | }
|
130 | });
|
131 | };
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Utilities.prototype.sortObject = function (dir, field) {
|
142 | var comparefunction;
|
143 | if (dir === 'desc') {
|
144 | comparefunction = function (a, b) {
|
145 | if (a[field] < b[field]) {
|
146 | return 1;
|
147 | }
|
148 | if (a[field] > b[field]) {
|
149 | return -1;
|
150 | }
|
151 | return 0;
|
152 | };
|
153 | }
|
154 | else {
|
155 | comparefunction = function (a, b) {
|
156 | if (a[field] < b[field]) {
|
157 | return -1;
|
158 | }
|
159 | if (a[field] > b[field]) {
|
160 | return 1;
|
161 | }
|
162 | return 0;
|
163 | };
|
164 | }
|
165 |
|
166 | return comparefunction;
|
167 | };
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | Utilities.prototype.removeEmptyObjectValues = function (obj) {
|
175 | for (var property in obj) {
|
176 | if (typeof obj[property] === 'object') {
|
177 | this.removeEmptyObjectValues(obj[property]);
|
178 | }
|
179 | else {
|
180 | if (obj[property] === '' || obj[property] === ' ' || obj[property] === null || obj[property] === undefined || Object.keys(obj).length === 0) {
|
181 | delete obj[property];
|
182 | }
|
183 | }
|
184 | }
|
185 | return obj;
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | Utilities.prototype.replaceBooleanStringObjectValues = function (obj) {
|
196 | for (var property in obj) {
|
197 | if (typeof obj[property] === 'object') {
|
198 | this.replaceBooleanStringObjectValues(obj[property]);
|
199 | }
|
200 | else {
|
201 | if(obj[property] === 'true'){
|
202 | obj[property] = true;
|
203 | }
|
204 | else if(obj[property] === 'false'){
|
205 | obj[property] = false;
|
206 | }
|
207 | }
|
208 | }
|
209 | return obj;
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | Utilities.prototype.removePrivateInfo = function (obj) {
|
218 |
|
219 | if (typeof obj === 'object') {
|
220 | obj.password = null;
|
221 | obj.apikey = null;
|
222 | obj.random = null;
|
223 | }
|
224 | return obj;
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | Utilities.prototype.stripTags = function (textinput) {
|
233 | if (textinput) {
|
234 | return textinput.replace(/[^a-z0-9@._]/gi, '-').toLowerCase();
|
235 | }
|
236 | else {
|
237 | return false;
|
238 | }
|
239 | };
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | Utilities.prototype.makeNiceName = function (username) {
|
247 | if (username) {
|
248 | return username.replace(/[^a-z0-9]/gi, '-').toLowerCase();
|
249 | }
|
250 | else {
|
251 | return false;
|
252 | }
|
253 | };
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | Utilities.prototype.makeNiceAttribute = function (username) {
|
261 | if (username) {
|
262 | return username.replace(/[^a-z0-9]/gi, '_').toLowerCase();
|
263 | }
|
264 | else {
|
265 | return false;
|
266 | }
|
267 | };
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | Utilities.prototype.getAdminMenu = function (options) {
|
275 | var adminmenu = {
|
276 | menu: {
|
277 | Content: {},
|
278 | Themes: {},
|
279 | Extensions: {},
|
280 | Settings: {},
|
281 | User: {}
|
282 | }
|
283 | };
|
284 | if(logger && logger.silly && options){
|
285 | logger.silly(options);
|
286 | }
|
287 | for (var x in appSettings.extconf.extensions) {
|
288 | if (appSettings.extconf.extensions[x].enabled === true && appSettings.extconf.extensions[x].periodicConfig['periodicjs.ext.admin']) {
|
289 | var extmenudata = appSettings.extconf.extensions[x].periodicConfig['periodicjs.ext.admin'];
|
290 |
|
291 | if (extmenudata.menu.Content) {
|
292 | adminmenu.menu.Content = merge(extmenudata.menu.Content, adminmenu.menu.Content);
|
293 | }
|
294 | if (extmenudata.menu.Themes) {
|
295 | adminmenu.menu.Themes = merge(extmenudata.menu.Themes, adminmenu.menu.Themes);
|
296 | }
|
297 | if (extmenudata.menu.Extensions) {
|
298 | adminmenu.menu.Extensions = merge(extmenudata.menu.Extensions, adminmenu.menu.Extensions);
|
299 | }
|
300 | if (extmenudata.menu.Settings) {
|
301 | adminmenu.menu.Settings = merge(extmenudata.menu.Settings, adminmenu.menu.Settings);
|
302 | }
|
303 | if (extmenudata.menu.User) {
|
304 | adminmenu.menu.User = merge(extmenudata.menu.User, adminmenu.menu.User);
|
305 | }
|
306 |
|
307 | }
|
308 | }
|
309 | return adminmenu;
|
310 | };
|
311 |
|
312 | module.exports = Utilities; |
\ | No newline at end of file |