PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/psm/Module/Config/Controller/ConfigController.php

https://gitlab.com/billyprice1/phpservermon
PHP | 359 lines | 272 code | 27 blank | 60 comment | 28 complexity | 4d80bfcad345b39cf3eab425eea840e8 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP Server Monitor
  4. * Monitor your servers and websites.
  5. *
  6. * This file is part of PHP Server Monitor.
  7. * PHP Server Monitor is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * PHP Server Monitor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package phpservermon
  21. * @author Pepijn Over <pep@peplab.net>
  22. * @copyright Copyright (c) 2008-2015 Pepijn Over <pep@peplab.net>
  23. * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
  24. * @version Release: @package_version@
  25. * @link http://www.phpservermonitor.org/
  26. **/
  27. namespace psm\Module\Config\Controller;
  28. use psm\Module\AbstractController;
  29. use psm\Service\Database;
  30. class ConfigController extends AbstractController {
  31. /**
  32. * Checkboxes
  33. * @var array $checkboxes
  34. */
  35. protected $checkboxes = array(
  36. 'email_status',
  37. 'email_smtp',
  38. 'sms_status',
  39. 'pushover_status',
  40. 'log_status',
  41. 'log_email',
  42. 'log_sms',
  43. 'log_pushover',
  44. 'show_update',
  45. );
  46. /**
  47. * Fields for saving
  48. * @var array $fields
  49. */
  50. protected $fields = array(
  51. 'email_from_name',
  52. 'email_from_email',
  53. 'email_smtp_host',
  54. 'email_smtp_port',
  55. 'email_smtp_username',
  56. 'email_smtp_password',
  57. 'sms_gateway_username',
  58. 'sms_gateway_password',
  59. 'sms_from',
  60. 'pushover_api_token',
  61. );
  62. private $default_tab = 'general';
  63. function __construct(Database $db, \Twig_Environment $twig) {
  64. parent::__construct($db, $twig);
  65. $this->setMinUserLevelRequired(PSM_USER_ADMIN);
  66. $this->setCSRFKey('config');
  67. $this->setActions(array(
  68. 'index', 'save',
  69. ), 'index');
  70. }
  71. /**
  72. * Populate all the config fields with values from the database
  73. *
  74. * @return string
  75. */
  76. protected function executeIndex() {
  77. $this->twig->addGlobal('subtitle', psm_get_lang('menu', 'config'));
  78. $tpl_data = $this->getLabels();
  79. $config_db = $this->db->select(
  80. PSM_DB_PREFIX . 'config',
  81. null,
  82. array('key', 'value')
  83. );
  84. $config = array();
  85. foreach($config_db as $entry) {
  86. $config[$entry['key']] = $entry['value'];
  87. }
  88. // generate language array
  89. $lang_keys = psm_get_langs();
  90. $tpl_data['language_current'] = (isset($config['language']))
  91. ? $config['language']
  92. : 'en_US';
  93. $tpl_data['languages'] = array();
  94. foreach($lang_keys as $key => $label) {
  95. $tpl_data['languages'][] = array(
  96. 'value' => $key,
  97. 'label' => $label,
  98. );
  99. }
  100. // @todo these selected values can easily be rewritten in the template using twig
  101. $tpl_data['sms_selected_' . $config['sms_gateway']] = 'selected="selected"';
  102. $tpl_data['alert_type_selected_' . $config['alert_type']] = 'selected="selected"';
  103. $smtp_sec = isset($config['email_smtp_security']) ? $config['email_smtp_security'] : '';
  104. $tpl_data['email_smtp_security_selected_' . $smtp_sec] = 'selected="selected"';
  105. $tpl_data['auto_refresh_servers'] = (isset($config['auto_refresh_servers'])) ? $config['auto_refresh_servers'] : '0';
  106. $tpl_data['log_retention_period'] = (isset($config['log_retention_period'])) ? $config['log_retention_period'] : '365';
  107. $tpl_data['password_encrypt_key'] = (isset($config['password_encrypt_key'])) ? $config['password_encrypt_key'] : sha1(microtime());
  108. foreach($this->checkboxes as $input_key) {
  109. $tpl_data[$input_key . '_checked'] =
  110. (isset($config[$input_key]) && (int) $config[$input_key] == 1)
  111. ? 'checked="checked"'
  112. : '';
  113. }
  114. foreach($this->fields as $input_key) {
  115. $tpl_data[$input_key] = (isset($config[$input_key])) ? $config[$input_key] : '';
  116. }
  117. $tpl_data[$this->default_tab . '_active'] = 'active';
  118. $testmodals = array('email', 'sms', 'pushover');
  119. foreach($testmodals as $modal_id) {
  120. $modal = new \psm\Util\Module\Modal($this->twig, 'test' . ucfirst($modal_id), \psm\Util\Module\Modal::MODAL_TYPE_OKCANCEL);
  121. $this->addModal($modal);
  122. $modal->setTitle(psm_get_lang('servers', 'send_' . $modal_id));
  123. $modal->setMessage(psm_get_lang('config', 'test_' . $modal_id));
  124. $modal->setOKButtonLabel(psm_get_lang('config', 'send'));
  125. }
  126. return $this->twig->render('module/config/config.tpl.html', $tpl_data);
  127. }
  128. /**
  129. * If a post has been done, gather all the posted data
  130. * and save it to the database
  131. */
  132. protected function executeSave() {
  133. if(!empty($_POST)) {
  134. // save new config
  135. $clean = array(
  136. 'language' => $_POST['language'],
  137. 'sms_gateway' => $_POST['sms_gateway'],
  138. 'alert_type' => $_POST['alert_type'],
  139. 'email_smtp_security' =>
  140. in_array($_POST['email_smtp_security'], array('', 'ssl', 'tls'))
  141. ? $_POST['email_smtp_security']
  142. : '',
  143. 'auto_refresh_servers' => intval(psm_POST('auto_refresh_servers', 0)),
  144. 'log_retention_period' => intval(psm_POST('log_retention_period', 365)),
  145. 'password_encrypt_key' => psm_POST('password_encrypt_key', sha1(microtime())),
  146. );
  147. foreach($this->checkboxes as $input_key) {
  148. $clean[$input_key] = (isset($_POST[$input_key])) ? '1': '0';
  149. }
  150. foreach($this->fields as $input_key) {
  151. if(isset($_POST[$input_key])) {
  152. $clean[$input_key] = $_POST[$input_key];
  153. }
  154. }
  155. $language_refresh = ($clean['language'] != psm_get_conf('language'));
  156. foreach($clean as $key => $value) {
  157. psm_update_conf($key, $value);
  158. }
  159. $this->addMessage(psm_get_lang('config', 'updated'), 'success');
  160. if(!empty($_POST['test_email'])) {
  161. $this->testEmail();
  162. } elseif(!empty($_POST['test_sms'])) {
  163. $this->testSMS();
  164. } elseif(!empty($_POST['test_pushover'])) {
  165. $this->testPushover();
  166. }
  167. if($language_refresh) {
  168. header('Location: ' . psm_build_url(array('mod' => 'config'), true, false));
  169. die();
  170. }
  171. if(isset($_POST['general_submit'])) {
  172. $this->default_tab = 'general';
  173. } elseif(isset($_POST['email_submit']) || !empty($_POST['test_email'])) {
  174. $this->default_tab = 'email';
  175. } elseif(isset($_POST['sms_submit']) || !empty($_POST['test_sms'])) {
  176. $this->default_tab = 'sms';
  177. } elseif(isset($_POST['pushover_submit']) || !empty($_POST['test_pushover'])) {
  178. $this->default_tab = 'pushover';
  179. }
  180. }
  181. return $this->runAction('index');
  182. }
  183. /**
  184. * Execute email test
  185. *
  186. * @todo move test to separate class
  187. */
  188. protected function testEmail() {
  189. $mail = psm_build_mail();
  190. $message = psm_get_lang('config', 'test_message');
  191. $mail->Subject = psm_get_lang('config', 'test_subject');
  192. $mail->Priority = 1;
  193. $mail->Body = $message;
  194. $mail->AltBody = str_replace('<br/>', "\n", $message);
  195. $user = $this->getUser()->getUser();
  196. $mail->AddAddress($user->email, $user->name);
  197. if($mail->Send()) {
  198. $this->addMessage(psm_get_lang('config', 'email_sent'), 'success');
  199. } else {
  200. $this->addMessage(psm_get_lang('config', 'email_error') . ': ' . $mail->ErrorInfo, 'error');
  201. }
  202. }
  203. /**
  204. * Execute SMS test
  205. *
  206. * @todo move test to separate class
  207. */
  208. protected function testSMS() {
  209. $sms = psm_build_sms();
  210. if($sms) {
  211. $user = $this->getUser()->getUser();
  212. if(empty($user->mobile)) {
  213. $this->addMessage(psm_get_lang('config', 'sms_error_nomobile'), 'error');
  214. } else {
  215. $sms->addRecipients($user->mobile);
  216. if($sms->sendSMS(psm_get_lang('config', 'test_message'))) {
  217. $this->addMessage(psm_get_lang('config', 'sms_sent'), 'success');
  218. } else {
  219. $this->addMessage(psm_get_lang('config', 'sms_error'), 'error');
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * Execute pushover test
  226. *
  227. * @todo move test to separate class
  228. */
  229. protected function testPushover() {
  230. $pushover = psm_build_pushover();
  231. $pushover->setDebug(true);
  232. $user = $this->getUser()->getUser();
  233. $api_token = psm_get_conf('pushover_api_token');
  234. if(empty($api_token)) {
  235. $this->addMessage(psm_get_lang('config', 'pushover_error_noapp'), 'error');
  236. } elseif(empty($user->pushover_key)) {
  237. $this->addMessage(psm_get_lang('config', 'pushover_error_nokey'), 'error');
  238. } else {
  239. $pushover->setPriority(0);
  240. $pushover->setTitle(psm_get_lang('config', 'test_subject'));
  241. $pushover->setMessage(psm_get_lang('config', 'test_message'));
  242. $pushover->setUser($user->pushover_key);
  243. if($user->pushover_device != '') {
  244. $pushover->setDevice($user->pushover_device);
  245. }
  246. $result = $pushover->send();
  247. if(isset($result['output']->status) && $result['output']->status == 1) {
  248. $this->addMessage(psm_get_lang('config', 'pushover_sent'), 'success');
  249. } else {
  250. if(isset($result['output']->errors->error)) {
  251. $error = $result['output']->errors->error;
  252. } else {
  253. $error = 'Unknown';
  254. }
  255. $this->addMessage(sprintf(psm_get_lang('config', 'pushover_error'), $error), 'error');
  256. }
  257. }
  258. }
  259. protected function getLabels() {
  260. return array(
  261. 'label_tab_email' => psm_get_lang('config', 'tab_email'),
  262. 'label_tab_sms' => psm_get_lang('config', 'tab_sms'),
  263. 'label_tab_pushover' => psm_get_lang('config', 'tab_pushover'),
  264. 'label_settings_email' => psm_get_lang('config', 'settings_email'),
  265. 'label_settings_sms' => psm_get_lang('config', 'settings_sms'),
  266. 'label_settings_pushover' => psm_get_lang('config', 'settings_pushover'),
  267. 'label_settings_notification' => psm_get_lang('config', 'settings_notification'),
  268. 'label_settings_log' => psm_get_lang('config', 'settings_log'),
  269. 'label_general' => psm_get_lang('config', 'general'),
  270. 'label_language' => psm_get_lang('config', 'language'),
  271. 'label_show_update' => psm_get_lang('config', 'show_update'),
  272. 'label_password_encrypt_key' => psm_get_lang('config', 'password_encrypt_key'),
  273. 'label_password_encrypt_key_note' => psm_get_lang('config', 'password_encrypt_key_note'),
  274. 'label_email_status' => psm_get_lang('config', 'email_status'),
  275. 'label_email_from_email' => psm_get_lang('config', 'email_from_email'),
  276. 'label_email_from_name' => psm_get_lang('config', 'email_from_name'),
  277. 'label_email_smtp' => psm_get_lang('config', 'email_smtp'),
  278. 'label_email_smtp_host' => psm_get_lang('config', 'email_smtp_host'),
  279. 'label_email_smtp_port' => psm_get_lang('config', 'email_smtp_port'),
  280. 'label_email_smtp_security' => psm_get_lang('config', 'email_smtp_security'),
  281. 'label_email_smtp_security_none' => psm_get_lang('config', 'email_smtp_security_none'),
  282. 'label_email_smtp_username' => psm_get_lang('config', 'email_smtp_username'),
  283. 'label_email_smtp_password' => psm_get_lang('config', 'email_smtp_password'),
  284. 'label_email_smtp_noauth' => psm_get_lang('config', 'email_smtp_noauth'),
  285. 'label_sms_status' => psm_get_lang('config', 'sms_status'),
  286. 'label_sms_gateway' => psm_get_lang('config', 'sms_gateway'),
  287. 'label_sms_gateway_mosms' => psm_get_lang('config', 'sms_gateway_mosms'),
  288. 'label_sms_gateway_mollie' => psm_get_lang('config', 'sms_gateway_mollie'),
  289. 'label_sms_gateway_spryng' => psm_get_lang('config', 'sms_gateway_spryng'),
  290. 'label_sms_gateway_inetworx' => psm_get_lang('config', 'sms_gateway_inetworx'),
  291. 'label_sms_gateway_clickatell' => psm_get_lang('config', 'sms_gateway_clickatell'),
  292. 'label_sms_gateway_textmarketer' => psm_get_lang('config', 'sms_gateway_textmarketer'),
  293. 'label_sms_gateway_smsit' => psm_get_lang('config', 'sms_gateway_smsit'),
  294. 'label_sms_gateway_freevoipdeal' => psm_get_lang('config', 'sms_gateway_freevoipdeal'),
  295. 'label_sms_gateway_smsglobal' => psm_get_lang('config', 'sms_gateway_smsglobal'),
  296. 'label_sms_gateway_nexmo' => psm_get_lang('config', 'sms_gateway_nexmo'),
  297. 'label_sms_gateway_octopush' => psm_get_lang('config', 'sms_gateway_octopush'),
  298. 'label_sms_gateway_freemobilesms' => psm_get_lang('config', 'sms_gateway_freemobilesms'),
  299. 'label_sms_gateway_clicksend' => psm_get_lang('config', 'sms_gateway_clicksend'),
  300. 'label_sms_gateway_username' => psm_get_lang('config', 'sms_gateway_username'),
  301. 'label_sms_gateway_password' => psm_get_lang('config', 'sms_gateway_password'),
  302. 'label_sms_from' => psm_get_lang('config', 'sms_from'),
  303. 'label_pushover_description' => psm_get_lang('config', 'pushover_description'),
  304. 'label_pushover_status' => psm_get_lang('config', 'pushover_status'),
  305. 'label_pushover_clone_app' => psm_get_lang('config', 'pushover_clone_app'),
  306. 'pushover_clone_url' => PSM_PUSHOVER_CLONE_URL,
  307. 'label_pushover_api_token' => psm_get_lang('config', 'pushover_api_token'),
  308. 'label_pushover_api_token_description' => sprintf(
  309. psm_get_lang('config', 'pushover_api_token_description'),
  310. PSM_PUSHOVER_CLONE_URL
  311. ),
  312. 'label_alert_type' => psm_get_lang('config', 'alert_type'),
  313. 'label_alert_type_description' => psm_get_lang('config', 'alert_type_description'),
  314. 'label_alert_type_status' => psm_get_lang('config', 'alert_type_status'),
  315. 'label_alert_type_offline' => psm_get_lang('config', 'alert_type_offline'),
  316. 'label_alert_type_always' => psm_get_lang('config', 'alert_type_always'),
  317. 'label_log_status' => psm_get_lang('config', 'log_status'),
  318. 'label_log_status_description' => psm_get_lang('config', 'log_status_description'),
  319. 'label_log_email' => psm_get_lang('config', 'log_email'),
  320. 'label_log_sms' => psm_get_lang('config', 'log_sms'),
  321. 'label_log_pushover' => psm_get_lang('config', 'log_pushover'),
  322. 'label_auto_refresh' => psm_get_lang('config', 'auto_refresh'),
  323. 'label_auto_refresh_servers' => psm_get_lang('config', 'auto_refresh_servers'),
  324. 'label_seconds' => psm_get_lang('config', 'seconds'),
  325. 'label_save' => psm_get_lang('system', 'save'),
  326. 'label_test' => psm_get_lang('config', 'test'),
  327. 'label_log_retention_period' => psm_get_lang('config', 'log_retention_period'),
  328. 'label_log_retention_period_description' => psm_get_lang('config', 'log_retention_period_description'),
  329. 'label_log_retention_days' => psm_get_lang('config', 'log_retention_days'),
  330. );
  331. }
  332. }