PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/connectors/lang.js.php

http://github.com/modxcms/revolution
PHP | 81 lines | 61 code | 5 blank | 15 comment | 1 complexity | 920c67805fc41686385ef86642939785 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Loads the lexicon into a JS-compatible function _()
  12. *
  13. * @var modX $modx
  14. * @package modx
  15. * @subpackage lexicon
  16. */
  17. define('MODX_CONNECTOR_INCLUDED', 1);
  18. ob_start();
  19. require_once dirname(__FILE__).'/index.php';
  20. ob_clean();
  21. if (!empty($_REQUEST['topic'])) {
  22. $topics = explode(',',$_REQUEST['topic']);
  23. foreach($topics as $topic) $modx->lexicon->load($topic);
  24. }
  25. $entries = $modx->lexicon->fetch();
  26. echo '
  27. MODx.lang = {';
  28. $s = '';
  29. foreach ($entries as $k => $v) {
  30. $s .= "'$k': ".'"'.esc($v).'",';
  31. }
  32. $s = trim($s,',');
  33. echo $s.'
  34. };
  35. var _ = function(s,v) {
  36. if (v != null && typeof(v) == "object") {
  37. var t = ""+MODx.lang[s];
  38. for (var k in v) {
  39. t = t.replace("[[+"+k+"]]",v[k]);
  40. }
  41. return t;
  42. } else return MODx.lang[s];
  43. }';
  44. function esc($s) {
  45. return strtr($s,array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
  46. }
  47. /* gather output from buffer */
  48. $output = ob_get_contents();
  49. ob_end_clean();
  50. /* if turned on, will cache lexicon entries in JS based upon http headers */
  51. if ($modx->getOption('cache_lang_js',null,false)) {
  52. $hash = md5($output);
  53. $headers = $modx->request->getHeaders();
  54. /* if Browser sent ID, check if they match */
  55. if (isset($headers['If-None-Match']) && @preg_match($hash, $headers['If-None-Match'])) {
  56. header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
  57. } else {
  58. header("ETag: \"{$hash}\"");
  59. header('Accept-Ranges: bytes');
  60. //header('Content-Length: '.strlen($output));
  61. header('Content-Type: application/x-javascript');
  62. echo $output;
  63. }
  64. } else {
  65. /* just output JS with no server caching */
  66. //header('Content-Length: '.strlen($output));
  67. header('Content-Type: application/x-javascript');
  68. echo $output;
  69. }
  70. @session_write_close();
  71. exit();