Passed
Push — develop ( 9412f6...11c76b )
by Портнов
04:56
created

AsteriskConf::getLogFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 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\Core\Asterisk\Configs;
21
22
23
use MikoPBX\Common\Models\PbxSettingsConstants;
24
use MikoPBX\Core\System\Processes;
25
use MikoPBX\Core\System\System;
26
use MikoPBX\Core\System\Util;
27
use Phalcon\Di;
28
29
/**
30
 * Represents the AsteriskConf class responsible for generating asterisk.conf configuration file.
31
 *
32
 * @package MikoPBX\Core\Asterisk\Configs
33
 */
34
class AsteriskConf extends AsteriskConfigClass
35
{
36
    // The module hook applying priority
37
    public int $priority = 1000;
38
39
    protected string $description = 'asterisk.conf';
40
41
    /**
42
     * Generates the protected configuration content.
43
     */
44
    protected function generateConfigProtected(): void
45
    {
46
        $lang = $this->generalSettings[PbxSettingsConstants::PBX_LANGUAGE];
47
48
        // Build the configuration content
49
        $conf = "[directories]\n" .
50
            "astetcdir => {$this->config->path('asterisk.astetcdir')}\n" .
51
            "astagidir => {$this->config->path('asterisk.astagidir')}\n" .
52
            "astkeydir => /etc/asterisk\n" .
53
            "astrundir => /var/asterisk/run\n" .
54
            "astmoddir => {$this->config->path('asterisk.astmoddir')}\n" .
55
            "astvarlibdir => {$this->config->path('asterisk.astvarlibdir')}\n" .
56
            "astdbdir => {$this->config->path('asterisk.astdbdir')}\n" .
57
            "astlogdir => {$this->config->path('asterisk.astlogdir')}\n" .
58
            "astspooldir => {$this->config->path('asterisk.astspooldir')}\n" .
59
            "astdatadir => {$this->config->path('asterisk.astvarlibdir')}\n" .
60
            "\n" .
61
            "\n" .
62
            "[options]\n" .
63
            "verbose = 0\n" .
64
            "debug = 0\n" .
65
            "dumpcore = no\n" .
66
            "transcode_via_sln = no\n" .
67
            "hideconnect = yes\n" .
68
            "defaultlanguage = {$lang}\n" .
69
            "systemname = mikopbx\n";
70
71
        // Write the configuration content to the file
72
        Util::fileWriteContent($this->config->path('asterisk.astetcdir') . '/asterisk.conf', $conf);
73
74
        $logCmdFile  = self::getLogFile();
75
        if(!file_exists($logCmdFile)){
76
            file_put_contents($logCmdFile, '');
77
        }
78
        $cmdFileLink = '/root/.asterisk_history';
79
        if(!file_exists($cmdFileLink)){
80
            Util::createUpdateSymlink($logCmdFile, $cmdFileLink, true);
81
        }
82
83
        $chownPath = Util::which('chown');
84
        shell_exec("$chownPath -R www:www $logCmdFile $cmdFileLink");
85
    }
86
87
    /**
88
     * Returns the CLI log file path.
89
     *
90
     * @return string The log file path.
91
     */
92
    public static function getLogFile():string
93
    {
94
        return System::getLogDir() . '/asterisk/asterisk-cli.log';
95
    }
96
97
    /**
98
     * Rotates the CLI log.
99
     *
100
     * @return void
101
     */
102
    public static function logRotate(): void
103
    {
104
        $logRotatePath = Util::which('logrotate');
105
        $max_size    = 1;
106
        $f_name      = self::getLogFile();
107
        $text_config = $f_name . " {
108
    nocreate
109
    nocopytruncate
110
    delaycompress
111
    nomissingok
112
    start 0
113
    rotate 2
114
    size {$max_size}M
115
    missingok
116
    noolddir
117
    postrotate
118
    endscript
119
}";
120
        $di = Di::getDefault();
121
        if ($di !== null){
122
            $varEtcDir = $di->getConfig()->path('core.varEtcDir');
123
        } else {
124
            $varEtcDir = '/var/etc';
125
        }
126
        $path_conf   = $varEtcDir . '/asterisk_cli_logrotate_' . basename($f_name) . '.conf';
127
        file_put_contents($path_conf, $text_config);
128
        $mb10 = $max_size * 1024 * 1024;
129
        $options = '';
130
        if (Util::mFileSize($f_name) > $mb10) {
131
            $options = '-f';
132
        }
133
        Processes::mwExecBg("{$logRotatePath} {$options} '{$path_conf}' > /dev/null 2> /dev/null");
134
    }
135
}