This repository has been archived by the owner on May 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ThemeUtils.js
254 lines (209 loc) · 7.13 KB
/
ThemeUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
/*
* GreeterUtils.js
*
* Copyright © 2016 Antergos Developers <dev@antergos.com>
*
* This file is part of lightdm-webkit2-greeter.
*
* lightdm-webkit2-greeter is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* lightdm-webkit2-greeter is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The following additional terms are in effect as per Section 7 of the license:
*
* The preservation of all legal notices and author attributions in
* the material or in the Appropriate Legal Notices displayed
* by works containing it is required.
*
* You should have received a copy of the GNU General Public License
* along with lightdm-webkit2-greeter; If not, see <http://www.gnu.org/licenses/>.
*/
if ( 'undefined' === typeof window.navigator.languages ) {
window.navigator.languages = [ window.navigator.language ];
}
moment.locale( window.navigator.languages );
let localized_invalid_date = moment('today', '!@#'),
time_language = null,
time_format = null,
allowed_dirs = null;
/**
* Provides various utility methods for use in greeter themes. The greeter will automatically
* create an instance of this class when it starts. The instance can be accessed
* with the global variable: [`theme_utils`](#dl-window-theme_utils).
*
* @memberOf LightDM
*/
class ThemeUtils {
/**
* Binds `this` to class, `context`, for all of the class's methods.
*
* @arg {object} context An ES6 class instance with at least one method.
*
* @return {object} `context` with `this` bound to it for all of its methods.
*/
bind_this( context ) {
let excluded_methods = ['constructor'];
function not_excluded( _method, _context ) {
let is_excluded = excluded_methods.findIndex( excluded_method => _method === excluded_method ) > -1,
is_method = 'function' === typeof _context[_method];
return is_method && !is_excluded;
}
for ( let obj = context; obj; obj = Object.getPrototypeOf( obj ) ) {
// Stop once we have traveled all the way up the inheritance chain
if ( 'Object' === obj.constructor.name ) {
break;
}
for ( let method of Object.getOwnPropertyNames( obj ) ) {
if ( not_excluded( method, context ) ) {
context[method] = context[method].bind( context );
}
}
}
}
/**
* Returns the contents of directory found at `path` provided that the (normalized) `path`
* meets at least one of the following conditions:
* * Is located within the greeter themes' root directory.
* * Has been explicitly allowed in the greeter's config file.
* * Is located within the greeter's shared data directory (`/var/lib/lightdm-data`).
* * Is located in `/tmp`.
*
* @param {string} path The abs path to desired directory.
*
* @returns {string[]} List of abs paths for the files and directories found in `path`.
*/
dirlist( path ) {
let allowed = true;
if ( '' === path || ! path instanceof String ) {
console.log('[ERROR] theme_utils.dirlist(): path must be a non-empty string!');
allowed = false;
} else if ( null !== path.match(/^[^/].+/) ) {
console.log('[ERROR] theme_utils.dirlist(): path must not include be absolute!');
allowed = false;
}
if ( null !== path.match(/\/\.+(?=\/)/) ) {
// No special directory names allowed (eg ../../)
path = path.replace(/\/\.+(?=\/)/g, '' );
}
if ( null === allowed_dirs ) {
let user = lightdm.users.pop(),
user_data_dir = greeter_config.get_str( user.username, 'lightdm_data_dir' ),
lightdm_data_dir = user_data_dir.substr( 0, user_data_dir.lastIndexOf('/') ),
themes_dir = greeter_config.get_str( 'greeter', 'themes_dir' ),
backgrounds_dir = greeter_config.get_str( 'branding', 'background_images' );
allowed_dirs = { tmpdir: '/tmp' };
if ( '' !== lightdm_data_dir ) {
allowed_dirs.lightdm_data_dir = lightdm_data_dir;
}
if ( '' !== themes_dir ) {
allowed_dirs.themes_dir = themes_dir;
}
if ( '' !== backgrounds_dir ) {
allowed_dirs.backgrounds_dir = backgrounds_dir;
}
}
if ( ! Object.keys( allowed_dirs ).some( dir => path.startsWith( allowed_dirs[dir] ) ) ) {
console.log(`[ERROR] theme_utils.dirlist(): path is not allowed: ${path}`);
allowed = false;
}
if ( ! allowed ) {
return [];
}
try {
return __ThemeUtils.dirlist( path );
} catch( err ) {
console.log( `[ERROR] theme_utils.dirlist(): ${err}` );
return [];
}
}
/**
* Escape HTML entities in a string.
*
* @param {string} text The text to be escaped.
*
* @returns {string}
*/
esc_html( text ) {
return this.txt2html( text );
}
/**
* Get the current time in a localized format. Time format and language are auto-detected
* by default, but can be set manually in the greeter config file.
* * `language` defaults to the system's language, but can be set manually in the config file.
* * When `time_format` config file option has a valid value, time will be formatted
* according to that value.
* * When `time_format` does not have a valid value, the time format will be `LT`
* which is `1:00 PM` or `13:00` depending on the system's locale.
*
* @return {string} The current localized time.
*/
get_current_localized_time() {
if ( null === time_language ) {
let config = greeter_config.greeter,
manual_language = ( '' !== config.time_language && 'auto' !== config.time_language ),
manual_time_format = ( '' !== config.time_format && 'auto' !== config.time_format );
time_language = manual_language ? config.time_language : window.navigator.language;
time_format = manual_time_format ? config.time_format : 'LT';
if ( manual_language ) {
moment.locale( time_language );
}
}
let local_time = moment().format( time_format );
if ( local_time === localized_invalid_date ) {
local_time = moment().format( 'LT' );
}
return local_time;
}
/**
* Use {@link window.theme_utils.esc_html()} instead.
*
* @deprecated
*/
txt2html( text ) {
try {
return __ThemeUtils.txt2html( text );
} catch( err ) {
console.log( `[ERROR] theme_utils.dirlist(): ${err}` );
return text;
}
}
}
const __theme_utils = new Promise( (resolve, reject) => {
let waiting = 0;
const check_window_prop = () => {
if ( waiting > 15000 ) {
return reject( 'Timeout Reached!');
}
setTimeout( () => {
waiting += 1;
if ( '__ThemeUtils' in window ) {
return resolve( (() => new ThemeUtils())() );
}
check_window_prop();
}, 0 );
};
check_window_prop();
});
/**
* Theme Utils - various utility methods for use in greeter themes.
* @name theme_utils
* @type {LightDM.ThemeUtils}
* @memberOf window
*/
__theme_utils.then( result => {
window.theme_utils = result;
/**
* ***Deprecated!*** Use {@link window.theme_utils} instead.
* @name greeterutil
* @type {LightDM.ThemeUtils}
* @memberOf window
* @deprecated
*/
window.greeterutil = window.theme_utils;
} );