-
-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathlang.js.php
81 lines (71 loc) · 2.07 KB
/
lang.js.php
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
<?php
/*
* This file is part of MODX Revolution.
*
* Copyright (c) MODX, LLC. All Rights Reserved.
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*/
/**
* Loads the lexicon into a JS-compatible function _()
*
* @var \MODX\Revolution\modX $modx
* @package modx
* @subpackage lexicon
*/
define('MODX_CONNECTOR_INCLUDED', 1);
ob_start();
require_once dirname(__FILE__).'/index.php';
ob_clean();
if (!empty($_REQUEST['topic'])) {
$topics = explode(',',$_REQUEST['topic']);
foreach($topics as $topic) $modx->lexicon->load($topic);
}
$entries = $modx->lexicon->fetch();
echo '
MODx.lang = {';
$s = '';
foreach ($entries as $k => $v) {
$s .= "'$k': ".'"'.esc($v).'",';
}
$s = trim($s,',');
echo $s.'
};
var _ = function(s,v) {
if (v != null && typeof(v) == "object") {
var t = ""+MODx.lang[s];
for (var k in v) {
t = t.replace("[[+"+k+"]]",v[k]);
}
return t;
} else return MODx.lang[s];
}';
function esc($s) {
return strtr($s, ['\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/']);
}
/* gather output from buffer */
$output = ob_get_contents();
ob_end_clean();
/* if turned on, will cache lexicon entries in JS based upon http headers */
if ($modx->getOption('cache_lang_js',null,false)) {
$hash = md5($output);
$headers = $modx->request->getHeaders();
/* if Browser sent ID, check if they match */
if (isset($headers['If-None-Match']) && @preg_match($hash, $headers['If-None-Match'])) {
header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
} else {
header("ETag: \"{$hash}\"");
header('Accept-Ranges: bytes');
//header('Content-Length: '.strlen($output));
header('Content-Type: application/x-javascript');
echo $output;
}
} else {
/* just output JS with no server caching */
//header('Content-Length: '.strlen($output));
header('Content-Type: application/x-javascript');
echo $output;
}
@session_write_close();
exit();