• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lonnieezell / Bonfire2 / 13542952505

26 Feb 2025 11:34AM UTC coverage: 45.449% (-0.02%) from 45.473%
13542952505

push

github

web-flow
Fix log deletion issues

Fix log deletion and related changes

7 of 19 new or added lines in 5 files covered. (36.84%)

4 existing lines in 4 files now uncovered.

1568 of 3450 relevant lines covered (45.45%)

62.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.82
/src/Tools/Controllers/LogsController.php
1
<?php
2

3
/**
4
 * This file is part of Bonfire.
5
 *
6
 * (c) Lonnie Ezell <lonnieje@gmail.com>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11

12
namespace Bonfire\Tools\Controllers;
13

14
use Bonfire\Core\AdminController;
15
use Bonfire\Tools\Libraries\Logs;
16
use CodeIgniter\HTTP\RedirectResponse;
17

18
class LogsController extends AdminController
19
{
20
    protected $theme      = 'Admin';
21
    protected $viewPrefix = 'Bonfire\Tools\Views\\';
22
    protected $logsPath   = WRITEPATH . 'logs/';
23
    protected $ext        = '.log';
24
    protected $logsLimit;
25
    protected $logsHandler;
26

27
    public function __construct()
28
    {
29
        helper('filesystem');
6✔
30
        $this->logsLimit   = setting('Site.perPage');
6✔
31
        $this->logsHandler = new Logs();
6✔
32
    }
33

34
    /**
35
     * Displays all logs.
36
     *
37
     * @return string
38
     */
39
    public function index()
40
    {
41
        // Load the Log Files.
42
        $logs = array_reverse(get_filenames($this->logsPath));
×
43

44
        // Define the regular expression pattern for log files
45
        $logPattern = '/^log-\d{4}-\d{2}-\d{2}\.log$/';
×
46
        // Filter the array removing index.html and other files that do not match
47
        $logs = array_filter($logs, static fn ($filename) => preg_match($logPattern, (string) $filename));
×
48

49
        $result = $this->logsHandler->paginateLogs($logs, $this->logsLimit);
×
50
        // Cycle through the $result array and attach the content property
51
        $counter = count($result['logs']);
×
52

53
        // Cycle through the $result array and attach the content property
54
        for ($i = 0; $i < $counter; $i++) {
×
55
            if ($result['logs'][$i] === 'index.html') {
×
56
                unset($result['logs'][$i]);
×
57

58
                continue;
×
59
            }
60
            $logFilePath        = $this->logsPath . $result['logs'][$i];
×
61
            $result['logs'][$i] = [
×
62
                'filename' => $result['logs'][$i],
×
63
                'content'  => $this->logsHandler->countLogLevels($logFilePath),
×
64
            ];
×
65
        }
66

67
        return $this->render($this->viewPrefix . 'logs', [
×
68
            'logs'  => $result['logs'],
×
69
            'pager' => $result['pager'],
×
70
        ]);
×
71
    }
72

73
    /**
74
     * Show the contents of a single log file.
75
     *
76
     * @param string $file The full name of the file to view (including extension).
77
     *
78
     * @return RedirectResponse|string
79
     */
80
    public function view(string $file = '')
81
    {
82
        helper('security');
6✔
83
        $file = sanitize_filename($file);
6✔
84

85
        if (empty($file) || ! file_exists($this->logsPath . $file . $this->ext)) {
6✔
86
            return redirect()->to(ADMIN_AREA . '/tools/logs')->with('danger', lang('Tools.empty'));
×
87
        }
88

89
        $logs = $this->logsHandler->processFileLogs($this->logsPath . $file . $this->ext);
6✔
90

91
        $result = $this->logsHandler->paginateLogs($logs, $this->logsLimit);
6✔
92

93
        $filePagerData = $this->logsHandler->getAdjacentLogFiles($file, $this->logsPath);
6✔
94

95
        return $this->render($this->viewPrefix . 'view_log', [
6✔
96
            'logFile'       => $file,
6✔
97
            'canDelete'     => 1,
6✔
98
            'logContent'    => $result['logs'],
6✔
99
            'pager'         => $result['pager'],
6✔
100
            'filesPager'    => view($this->viewPrefix . '_pager', $filePagerData),
6✔
101
            'logFilePretty' => app_date(str_replace('log-', '', $file)),
6✔
102
        ]);
6✔
103
    }
104

105
    /**
106
     * Delete the specified log file or all.
107
     *
108
     * @return RedirectResponse
109
     */
110
    public function delete()
111
    {
112
        if (
NEW
113
            $this->request->getPost('checked') === null
×
NEW
114
            && $this->request->getPost('delete_all') === null
×
115
        ) {
116
            return redirect()->to(ADMIN_AREA . '/tools/logs')->with(
×
117
                'error',
×
NEW
118
                lang('Tools.noLogsSelected'),
×
119
            );
×
120
        }
121

122
        if (
NEW
123
            $this->request->getPost('delete') !== null
×
NEW
124
            && is_array($this->request->getPost('checked'))
×
125
        ) {
UNCOV
126
            helper('security');
×
127

NEW
128
            $checked    = $this->request->getPost('checked');
×
129
            $numChecked = count($checked);
×
130

NEW
131
            if ($numChecked) {
×
132
                foreach ($checked as $file) {
×
133
                    @unlink($this->logsPath . sanitize_filename($file . $this->ext));
×
134
                }
135

136
                return redirect()->to(ADMIN_AREA . '/tools/logs')->with('message', lang('Tools.deleteSuccess'));
×
137
            }
138
        }
139

NEW
140
        if ($this->request->getPost('delete_all') !== null) {
×
141
            if (delete_files($this->logsPath)) {
×
142
                // Restore the index.html file.
143
                @copy(APPPATH . '/index.html', "{$this->logsPath}index.html");
×
144

145
                return redirect()->to(ADMIN_AREA . '/tools/logs')->with('message', lang('Tools.deleteAllSuccess'));
×
146
            }
147

148
            return redirect()->to(ADMIN_AREA . '/tools/logs')->with('error', lang('Tools.deleteError'));
×
149
        }
150

151
        return redirect()->to(ADMIN_AREA . '/tools/logs')->with('error', lang('Bonfire.unknownAction'));
×
152
    }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc