Issues (1686)

sources/ElkArte/Controller/Jslocale.php (3 issues)

1
<?php
2
3
/**
4
 * This file loads javascript localizations (i.e. language strings)
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\Controller;
15
16
use ElkArte\AbstractController;
17
use ElkArte\Action;
18
use ElkArte\Agreement;
19
use ElkArte\Http\Headers;
20
use ElkArte\PrivacyPolicy;
21
use ElkArte\User;
22
use ElkArte\Languages\Loader as LangLoader;
23
24
/**
25
 * This file is called via ?action=jslocale to load in a list of
26
 * language strings for a given area.
27
 */
28
class Jslocale extends AbstractController
29
{
30
	/** @var string The content of the file to be returned  */
31
	private $_file_data;
32
33
	/**
34
	 * {@inheritDoc}
35
	 */
36
	public function trackStats($action = '')
37
	{
38
		return false;
39
	}
40
41
	/**
42
	 * The default action for the class
43
	 */
44
	public function action_index()
45
	{
46
		$subActions = [
47
			'agreement' => [$this, 'action_agreement_api'],
48
			'sceditor' => [$this, 'action_sceditor']
49
		];
50
51
		// Set up for some action
52
		$action = new Action();
53
54
		// Get the sub action or set a default, call integrate_sa_avatar_settings
55
		$subAction = $action->initialize($subActions);
56
		$action->dispatch($subAction);
57
	}
58
59
	/**
60
	 * Creates the javascript code for localization of the editor (SCEditor)
61
	 */
62
	public function action_sceditor()
63
	{
64
		global $txt;
65
66
		$editortxt = $this->_prepareLocale('Editor');
67
68
		// If we don't have any locale better avoid broken js
69
		if (empty($txt['lang_locale']) || empty($editortxt))
70
		{
71
			die();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
		}
73
74
		$this->_file_data = '(function (sceditor) {
75
		\'use strict\';
76
77
		sceditor.locale[' . JavaScriptEscape($txt['lang_locale']) . '] = {';
78
79
		foreach ($editortxt as $key => $val)
80
		{
81
			$this->_file_data .= '
82
			' . JavaScriptEscape($key) . ': ' . JavaScriptEscape($val) . ',';
83
		}
84
85
		$this->_file_data .= '
86
			dateFormat: "day.month.year"
87
		}
88
	})(sceditor);';
89
90
		$this->_sendFile();
91
	}
92
93
	/**
94
	 * Handy shortcut to prepare the "system"
95
	 *
96
	 * @param string $language_file
97
	 */
98
	private function _prepareLocale($language_file)
99
	{
100
		global $modSettings, $language;
101
102
		$txteditor = [];
103
		$lang = new LangLoader(User::$info->language ?? $language, $txteditor, database(), 'editortxt');
0 ignored issues
show
Bug Best Practice introduced by
The property language does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
104
		$lang->load($language_file);
105
106
		theme()->getLayers()->removeAll();
107
108
		// Lets make sure we aren't going to output anything nasty.
109
		obStart(!empty($modSettings['enableCompressedOutput']));
110
111
		return $txteditor;
112
	}
113
114
	/**
115
	 * Takes care of echo'ing the javascript file stored in $this->_file_data
116
	 */
117
	private function _sendFile()
118
	{
119
		// Make sure they know what type of file we are.
120
		Headers::instance()
121
			->contentType('text/javascript', 'UTF-8')
122
			->sendHeaders();
123
124
		echo $this->_file_data;
125
126
		// And terminate
127
		obExit(false);
128
	}
129
130
	/**
131
	 * Method to handle the API request for agreement and privacy policy by returning it in a
132
	 * selected language.  Used when checkbox accept agreement is enabled.
133
	 */
134
	public function action_agreement_api()
135
	{
136
		global $context, $modSettings;
137
138
		$languages = getLanguages();
139
		$lang = $this->_req->getPost('lang', 'trim', 'English');
140
141
		setJsonTemplate();
142
		$context['require_agreement'] = !empty($modSettings['requireAgreement']);
143
144
		if (isset($languages[$lang]))
145
		{
146
			// If you have to agree to the agreement, it needs to be fetched from the file.
147
			$agreement = new Agreement($lang);
148
			if (!empty($modSettings['requirePrivacypolicy']))
149
			{
150
				$privacypol = new PrivacyPolicy($lang);
151
			}
152
153
			$context['json_data'] = ['agreement' => '', 'privacypol' => ''];
154
			try
155
			{
156
				$context['json_data']['agreement'] = $agreement->getParsedText();
157
				if (!empty($modSettings['requirePrivacypolicy']))
158
				{
159
					$context['json_data']['privacypol'] = $privacypol->getParsedText();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $privacypol does not seem to be defined for all execution paths leading up to this point.
Loading history...
160
				}
161
			}
162
			catch (\Exception $e)
163
			{
164
				$context['json_data'] = $e->getMessage();
165
			}
166
		}
167
		else
168
		{
169
			$context['json_data'] = '';
170
		}
171
	}
172
}
173