Passed
Push — develop ( b91c6d...602856 )
by Nikolay
05:44 queued 10s
created

PHPConf   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 41
c 1
b 0
f 0
dl 0
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogFile() 0 5 1
A setupLog() 0 11 3
A phpTimeZoneConfigure() 0 13 2
A rotateLog() 0 36 3
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
        $asteriskPath = Util::which('asterisk');
56
        $logrotatePath = Util::which('logrotate');
57
58
        $max_size    = 2;
59
        $f_name      = self::getLogFile();
60
        $text_config = (string)($f_name) . " {
61
    nocreate
62
    nocopytruncate
63
    delaycompress
64
    nomissingok
65
    start 0
66
    rotate 9
67
    size {$max_size}M
68
    missingok
69
    noolddir
70
    postrotate
71
        {$asteriskPath} -rx 'logger reload' > /dev/null 2> /dev/null
72
    endscript
73
}";
74
        $di     = Di::getDefault();
75
        if ($di !== null){
76
            $varEtcPath = $di->getConfig()->path('core.varEtcPath');
77
        } else {
78
            $varEtcPath = '/var/etc';
79
        }
80
        $path_conf   = $varEtcPath . '/php_logrotate_' . basename($f_name) . '.conf';
81
        file_put_contents($path_conf, $text_config);
82
        $mb10 = $max_size * 1024 * 1024;
83
84
        $options = '';
85
        if (Util::mFileSize($f_name) > $mb10) {
86
            $options = '-f';
87
        }
88
        Util::mwExecBg("{$logrotatePath} {$options} '{$path_conf}' > /dev/null 2> /dev/null");
89
    }
90
91
    /**
92
     * Setup timezone for PHP
93
     */
94
    public static function phpTimeZoneConfigure(): void
95
    {
96
        $mikoPBXConfig = new MikoPBXConfig();
97
        $timezone      = $mikoPBXConfig->getGeneralSettings('PBXTimezone');
98
        date_default_timezone_set($timezone);
99
        if (file_exists('/etc/TZ')) {
100
            $catPath = Util::which('cat');
101
            Util::mwExec("export TZ='$({$catPath} /etc/TZ)'");
102
        }
103
        $etcPhpIniPath = '/etc/php.ini';
104
        $contents = file_get_contents($etcPhpIniPath);
105
        $contents = preg_replace("/date.timezone(.*)/", 'date.timezone="'.$timezone.'"', $contents);
106
        Util::fileWriteContent($etcPhpIniPath, $contents);
107
    }
108
}