Passed
Push — develop ( ccaf35...b84971 )
by Портнов
04:52
created

GeneralSettingsController::getSimplePasswords()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 4
nc 3
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\AdminCabinet\Controllers;
21
22
use MikoPBX\AdminCabinet\Forms\GeneralSettingsEditForm;
23
use MikoPBX\Common\Models\Codecs;
24
use MikoPBX\Common\Models\PbxSettings;
25
use MikoPBX\Core\System\Util;
26
use Phalcon\Mvc\Model;
27
28
class GeneralSettingsController extends BaseController
29
{
30
31
    /**
32
     * Builds general settings form
33
     */
34
    public function modifyAction(): void
35
    {
36
        $audioCodecs     = Codecs::find(['conditions'=>'type="audio"'])->toArray();
37
        usort($audioCodecs, [__CLASS__, 'sortArrayByPriority']);
38
        $this->view->audioCodecs = $audioCodecs;
39
        $videoCodecs     = Codecs::find(['conditions'=>'type="video"'])->toArray();
40
        usort($videoCodecs, [__CLASS__, 'sortArrayByPriority']);
41
        $this->view->videoCodecs = $videoCodecs;
42
        $pbxSettings            = PbxSettings::getAllPbxSettings();
43
        $this->view->form       = new GeneralSettingsEditForm(null, $pbxSettings);
44
        $this->view->submitMode = null;
45
46
        $this->view->simplePasswords = $this->getSimplePasswords($pbxSettings);
47
    }
48
49
    private function getSimplePasswords($data):array
50
    {
51
        $passwordCheckFail = [];
52
        foreach (['SSHPassword', 'WebAdminPassword'] as $value){
53
            if(isset($data[$value]) && Util::isSimplePassword($data[$value])){
54
                $passwordCheckFail[] = $value;
55
            }
56
        }
57
        return $passwordCheckFail;
58
    }
59
60
    /**
61
     * Сохранение настроек системы
62
     */
63
    public function saveAction(): void
64
    {
65
        if ( ! $this->request->isPost()) {
66
            return;
67
        }
68
        $data        = $this->request->getPost();
69
        $passwordCheckFail = $this->getSimplePasswords($data);
70
        if(!empty($passwordCheckFail)){
71
            $this->view->message = [
72
                'error' => $this->translation->_('gs_SetPasswordInfo')
73
            ];
74
        }
75
        if(!empty($passwordCheckFail)){
76
            $this->view->success = false;
77
            $this->view->passwordCheckFail = $passwordCheckFail;
78
            return;
79
        }
80
81
        $pbxSettings = PbxSettings::getDefaultArrayValues();
82
        if(isset($data['SSHPassword'])){
83
            // Если отправили пароль по-умолчанию, то сделаем его хэш равным хэш пароля WEB
84
            if($data['SSHPassword'] === $pbxSettings['SSHPassword']){
85
                $data['SSHPasswordHash'] = md5($data['WebAdminPassword']);
86
            }else{
87
                $data['SSHPasswordHash'] = md5($data['SSHPassword']);
88
            }
89
        }
90
        $this->db->begin();
91
        foreach ($pbxSettings as $key => $value) {
92
            switch ($key) {
93
                case 'PBXRecordCalls':
94
                case 'AJAMEnabled':
95
                case 'AMIEnabled':
96
                case 'RestartEveryNight':
97
                case 'RedirectToHttps':
98
                case 'PBXSplitAudioThread':
99
                case 'UseWebRTC':
100
                case 'SSHDisablePasswordLogins':
101
                case 'PBXAllowGuestCalls':
102
                case '***ALL CHECK BOXES ABOVE***':
103
                    $newValue = ($data[$key] === 'on') ? '1' : '0';
104
                    break;
105
                case 'SSHPassword':
106
                    // Если отправили пароль по-умолчанию, то сделаем его равным паролю WEB
107
                    if ($data[$key] === $value) {
108
                        $newValue = $data['WebAdminPassword'];
109
                    } else {
110
                        $newValue = $data[$key];
111
                    }
112
                    break;
113
                case 'SendMetrics':
114
                    $newValue = ($data[$key] === 'on') ? '1' : '0';
115
                    $this->session->set('SendMetrics', $newValue);
116
                    break;
117
                case 'PBXFeatureTransferDigitTimeout':
118
                    $newValue = ceil((int)$data['PBXFeatureDigitTimeout']/1000);
119
                    break;
120
                default:
121
                    $newValue = $data[$key];
122
            }
123
124
            if (array_key_exists($key, $data)) {
125
                $record = PbxSettings::findFirstByKey($key);
126
                if ($record === null) {
127
                    $record        = new PbxSettings();
128
                    $record->key   = $key;
129
                    $record->value = $newValue;
130
                } elseif ($record->key === $key
131
                    && $record->value === $newValue) {
132
                    continue;
133
                }
134
                $record->value = $newValue;
135
136
                if ($record->save() === false) {
137
                    $errors = $record->getMessages();
138
                    $this->flash->warning(implode('<br>', $errors));
139
                    $this->view->success = false;
140
                    $this->db->rollback();
141
142
                    return;
143
                }
144
            }
145
        }
146
147
        $codecs = json_decode($data['codecs'], true);
148
        foreach ($codecs as $codec){
149
           $record = Codecs::findFirstById($codec['codecId']);
150
           $record->priority = $codec['priority'];
151
           $record->disabled = $codec['disabled']===true?'1':'0';
152
           $record->update();
153
        }
154
155
        $this->flash->success($this->translation->_('ms_SuccessfulSaved'));
156
        $this->view->success = true;
157
        $this->db->commit();
158
    }
159
160
}