Passed
Push — develop ( 5bdc5b...331e75 )
by Портнов
04:29
created

PHPConf::logRotate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 35
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
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\System\Configs;
21
22
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\Core\System\Processes;
25
use MikoPBX\Core\System\System;
26
use MikoPBX\Core\System\Util;
27
use Phalcon\Di;
28
use Phalcon\Di\Injectable;
29
30
/**
31
 * Class PHPConf
32
 *
33
 * Represents the PHP configuration.
34
 *
35
 * @package MikoPBX\Core\System\Configs
36
 */
37
class PHPConf extends Injectable
38
{
39
40
    /**
41
     * Relocates PHP error log to the storage mount.
42
     *
43
     * @return void
44
     */
45
    public static function setupLog(): void
46
    {
47
        $src_log_file = '/var/log/php_error.log';
48
        $dst_log_file = self::getLogFile();
49
        if ( ! file_exists($src_log_file)) {
50
            file_put_contents($src_log_file, '');
51
        }
52
        $options = file_exists($dst_log_file) ? '>' : '';
53
        $catPath = Util::which('cat');
54
        Processes::mwExec("{$catPath} {$src_log_file} 2> /dev/null >{$options} {$dst_log_file}");
55
        Util::createUpdateSymlink($dst_log_file, $src_log_file);
56
    }
57
58
59
    /**
60
     * Returns the PHP error log file path.
61
     *
62
     * @return string The log file path.
63
     */
64
    public static function getLogFile(): string
65
    {
66
        $logdir = System::getLogDir() . '/php';
67
        Util::mwMkdir($logdir);
68
        return "$logdir/error.log";
69
    }
70
71
    /**
72
     * Rotates the PHP error log.
73
     *
74
     * @return void
75
     */
76
    public static function logRotate(): void
77
    {
78
        $logrotatePath = Util::which('logrotate');
79
80
        $max_size    = 10;
81
        $f_name      = self::getLogFile();
82
        $text_config = $f_name . " {
83
    nocreate
84
    nocopytruncate
85
    delaycompress
86
    nomissingok
87
    start 0
88
    rotate 9
89
    size {$max_size}M
90
    missingok
91
    noolddir
92
    postrotate
93
    endscript
94
}";
95
        // TODO::Add restart PHP-FPM after rotation
96
        $di     = Di::getDefault();
97
        if ($di !== null){
98
            $varEtcDir = $di->getConfig()->path('core.varEtcDir');
99
        } else {
100
            $varEtcDir = '/var/etc';
101
        }
102
        $path_conf   = $varEtcDir . '/php_logrotate_' . basename($f_name) . '.conf';
103
        file_put_contents($path_conf, $text_config);
104
        $mb10 = $max_size * 1024 * 1024;
105
106
        $options = '';
107
        if (Util::mFileSize($f_name) > $mb10) {
108
            $options = '-f';
109
        }
110
        Processes::mwExecBg("{$logrotatePath} {$options} '{$path_conf}' > /dev/null 2> /dev/null");
111
    }
112
113
    /**
114
     * Sets up the timezone for PHP.
115
     *
116
     * @return void
117
     */
118
    public static function phpTimeZoneConfigure(): void
119
    {
120
        $timezone      = PbxSettings::getValueByKey('PBXTimezone');
121
        date_default_timezone_set($timezone);
122
        if (file_exists('/etc/TZ')) {
123
            $catPath = Util::which('cat');
124
            Processes::mwExec("export TZ='$({$catPath} /etc/TZ)'");
125
        }
126
        $etcPhpIniPath = '/etc/php.d/01-timezone.ini';
127
        $contents = 'date.timezone="'.$timezone.'"';
128
        file_put_contents($etcPhpIniPath, $contents);
129
    }
130
131
    /**
132
     * Restarts php-fpm.
133
     *
134
     * @return void
135
     */
136
    public static function reStart(): void
137
    {
138
        $phpFPMPath = Util::which('php-fpm');
139
140
        // Send graceful shutdown signal
141
        Processes::mwExec('kill -SIGQUIT "$(cat /var/run/php-fpm.pid)"');
142
        usleep(100000);
143
144
        // Forcefully terminate
145
        Processes::killByName('php-fpm');
146
        Processes::mwExec("{$phpFPMPath} -c /etc/php.ini");
147
    }
148
}