Passed
Push — develop ( 751171...058086 )
by Nikolay
05:21
created

PHPConf::reStart()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 7 2020
7
 *
8
 */
9
10
namespace MikoPBX\Core\System\Configs;
11
12
13
use MikoPBX\Core\System\MikoPBXConfig;
14
use MikoPBX\Core\System\System;
15
use MikoPBX\Core\System\Util;
16
use Phalcon\Di;
17
use Phalcon\Di\Injectable;
18
19
class PHPConf extends Injectable
20
{
21
22
    /**
23
     * Relocate PHP error log to storage mount
24
     */
25
    public static function setupLog(): void
26
    {
27
        $src_log_file = '/var/log/php_error.log';
28
        $dst_log_file = self::getLogFile();
29
        if ( ! file_exists($src_log_file)) {
30
            file_put_contents($src_log_file, '');
31
        }
32
        $options = file_exists($dst_log_file) ? '>' : '';
33
        $catPath = Util::which('cat');
34
        Util::mwExec("{$catPath} {$src_log_file} 2> /dev/null >{$options} {$dst_log_file}");
35
        Util::createUpdateSymlink($dst_log_file, $src_log_file);
36
    }
37
38
39
    /**
40
     * Returns php error log filepath
41
     * @return string
42
     */
43
    public static function getLogFile(): string
44
    {
45
        $logdir = System::getLogDir() . '/php';
46
        Util::mwMkdir($logdir);
47
        return "$logdir/error.log";
48
    }
49
50
    /**
51
     * Rotate php error log
52
     */
53
    public static function rotateLog(): void
54
    {
55
        $logrotatePath = Util::which('logrotate');
56
57
        $max_size    = 2;
58
        $f_name      = self::getLogFile();
59
        $text_config = $f_name . " {
60
    nocreate
61
    nocopytruncate
62
    delaycompress
63
    nomissingok
64
    start 0
65
    rotate 9
66
    size {$max_size}M
67
    missingok
68
    noolddir
69
    postrotate
70
    endscript
71
}";
72
        // TODO::Доделать рестарт PHP-FPM после обновление лога
73
        $di     = Di::getDefault();
74
        if ($di !== null){
75
            $varEtcPath = $di->getConfig()->path('core.varEtcPath');
76
        } else {
77
            $varEtcPath = '/var/etc';
78
        }
79
        $path_conf   = $varEtcPath . '/php_logrotate_' . basename($f_name) . '.conf';
80
        file_put_contents($path_conf, $text_config);
81
        $mb10 = $max_size * 1024 * 1024;
82
83
        $options = '';
84
        if (Util::mFileSize($f_name) > $mb10) {
85
            $options = '-f';
86
        }
87
        Util::mwExecBg("{$logrotatePath} {$options} '{$path_conf}' > /dev/null 2> /dev/null");
88
    }
89
90
    /**
91
     * Setup timezone for PHP
92
     */
93
    public static function phpTimeZoneConfigure(): void
94
    {
95
        $mikoPBXConfig = new MikoPBXConfig();
96
        $timezone      = $mikoPBXConfig->getGeneralSettings('PBXTimezone');
97
        date_default_timezone_set($timezone);
98
        if (file_exists('/etc/TZ')) {
99
            $catPath = Util::which('cat');
100
            Util::mwExec("export TZ='$({$catPath} /etc/TZ)'");
101
        }
102
        $etcPhpIniPath = '/etc/php.ini';
103
        $contents = file_get_contents($etcPhpIniPath);
104
        $contents = preg_replace("/date.timezone(.*)/", 'date.timezone="'.$timezone.'"', $contents);
105
        Util::fileWriteContent($etcPhpIniPath, $contents);
106
    }
107
108
    /**
109
     *   Restart php-fpm
110
     **/
111
    public function reStart(): void
112
    {
113
        if (Util::isSystemctl()) {
114
            $systemCtrlPath = Util::which('systemctl');
115
            Util::mwExec("{$systemCtrlPath} restart php7.4-fpm");
116
        } else {
117
            $phpFPMPath = Util::which('php-fpm');
118
            Util::killByName('php-fpm');
119
            Util::mwExec("{$phpFPMPath} -c /etc/php.ini");
120
        }
121
    }
122
}