index.php ➔ flash_msg()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 2
dl 0
loc 8
rs 9.6111
c 0
b 0
f 0
1
<?php
2
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'bootstrap.php';
3
4
/**
5
 * produce us a cache and sweeper object please
6
 *
7
 * @param array $config
8
 *
9
 * @return array
10
 */
11
function factory(array $config)
12
{
13
    extract($config);
14
    $cache = new \PhpDbaCache\Cache($file, $handler, $mode, $persistently);
15
    $sweep = new \PhpDbaCache\Sweep($cache);
16
17
    return array($cache, $sweep);
18
}
19
20
/**
21
 * create an test-entry for two seconds.
22
 *
23
 * @param \PhpDbaCache\Cache $cache
24
 *
25
 * @return bool
26
 */
27
function put(\PhpDbaCache\Cache $cache)
28
{
29
    return $cache->put(uniqid('test_'), (object)array(rand(1, 100)), 2);
30
}
31
32
/**
33
 * pretty printer for byte values.
34
 *
35
 * @param $size
36
 *
37
 * @return string
38
 */
39
function bsize($size)
40
{
41
    $bytes = '';
42
43
    foreach (array('', 'K', 'M', 'G') as $bytes) {
44
        if ($size < 1024) {
45
            break;
46
        }
47
        $size /= 1024;
48
    }
49
50
    return sprintf("%5.1f %sBytes", $size, $bytes);
51
}
52
53
/**
54
 * @param $check
55
 * @param $msg
56
 */
57
function flash_msg(&$check, $msg)
58
{
59
    if (isset($check) && $check === true) {
60
        echo '<div class="alert alert-success">' . $msg . '</div>';
61
    } elseif (isset($check) && $check === false) {
62
        echo '<div class="alert alert-error"><strong>NO!</strong> ' . $msg . '</div>';
63
    }
64
}
65
66
// retrieve an cache and a cache-sweeper.
67
try {
68
    list($cache, $sweep) = factory($config);
69
} catch (Exception $exception) {
70
    die($exception->getMessage());
71
}
72
73
// load the configuration at the global namespace.
74
extract($config);
75
76
// make a list of all the available handlers.
77
$available_handlers = $handler_in_use = '';
78
foreach (dba_handlers(true) as $handler_name => $handler_version) {
79
    $handler_version = str_replace('$', '', $handler_version);
80
    if ($handler == $handler_name) {
81
        $handler_in_use = "$handler_name: $handler_version <br />";
82
        continue;
83
    }
84
    $available_handlers .= "$handler_name: $handler_version <br />";
85
}
86
87
// compute the user authentication.
88
$authenticated = false;
89
if (isset($_POST['login']) || isset($_SERVER['PHP_AUTH_USER'])) {
90
    if (!isset($_SERVER['PHP_AUTH_USER']) ||
91
        !isset($_SERVER['PHP_AUTH_PW']) ||
92
        $_SERVER['PHP_AUTH_USER'] != $authentication['username'] ||
93
        $_SERVER['PHP_AUTH_PW'] != $authentication['password']
94
    ) {
95
        header("WWW-Authenticate: Basic realm=\"PHP DBA Cache Login\"");
96
        header("HTTP/1.0 401 Unauthorized");
97
        exit;
98
    }
99
    $authenticated = true;
100
}
101
102
103
if (isset($_POST['create-test-entry'])) {
104
    $create_test_entry = put($cache);
105
}
106
107
if (isset($_POST['optimize'])) {
108
    $optimize = @dba_optimize($cache->getDba());
109
}
110
111
if (isset($_POST['synchronize'])) {
112
    $synchronize = @dba_sync($cache->getDba());
113
}
114
115
if ($authenticated && isset($_POST['delete-old'])) {
116
    $delete_old = true;
117
    $sweep->old();
118
}
119
120
if ($authenticated && isset($_POST['delete-all'])) {
121
    try {
122
        $delete_all = $sweep->flush();
123
    } catch (\RuntimeException $re) {
124
        $delete_all = false;
125
    }
126
127
    list($cache, $sweep) = factory($config);
128
    put($cache);
129
}
130
131
// find the host-name
132
$host = php_uname('n');
133
if ($host) {
134
    $host = '(' . $host . ')';
135
}
136
if (isset($_SERVER['SERVER_ADDR'])) {
137
    $host .= ' (' . $_SERVER['SERVER_ADDR'] . ')';
138
}
139
140
clearstatcache();
141
$file_info = new \SplFileInfo($file);
142
?>
143
<!DOCTYPE html>
144
<html lang="en">
145
<head>
146
    <meta charset="utf-8">
147
    <title>PHP DBA Cache Monitor by Gjero Krsteski</title>
148
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
149
    <meta name="description" content="PHP DBA Cache Monitor">
150
    <meta name="author" content="Gjero Krsteski">
151
    <link href="bootstrap.min.css" rel="stylesheet" type="text/css"/>
152
    <style>
153
        body {
154
            padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
155
        }
156
157
    </style>
158
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-responsive.min.css" rel="stylesheet">
159
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
160
    <!--[if lt IE 9]>
161
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
162
    <![endif]-->
163
    <!-- Fav and touch icons -->
164
    <link rel="shortcut icon" href="favicon.ico">
165
</head>
166
167
<body>
168
<div class="navbar navbar-inverse navbar-fixed-top">
169
    <div class="navbar-inner">
170
        <div class="container">
171
            <a class="btn btn-navbar collapsed" data-toggle="collapse" data-target=".nav-collapse"> <span
172
                    class="icon-bar"></span>
173
                <span class="icon-bar"></span>
174
                <span class="icon-bar"></span>
175
176
            </a>
177
            <a class="brand" style="color:white;" href="#">DBA Cache Monitor</a>
178
179
            <div class="nav-collapse collapse" style="height: 0px;">
180
                <ul class="nav">
181
                    <li>
182
                        <a href="#general">General</a>
183
                    </li>
184
                    <li>
185
                        <a href="#memory">Memory Usage</a>
186
                    </li>
187
                    <li>
188
                        <a href="#speed">Tune Cache</a>
189
                    </li>
190
                    <li>
191
                        <a href="#entries">Sweep Cache</a>
192
                    </li>
193
                </ul>
194
            </div>
195
            <!--/.nav-collapse -->
196
        </div>
197
    </div>
198
</div>
199
<div class="container">
200
201
    <p class="">The php-dba-cache uses the database (dbm-style) abstraction layer to store your PHP data. It depends on
202
        the free space of your disk.</p>
203
204
    <section>
205
        <a class="anchor" id="general"></a>
206
207
        <h3>General Information</h3>
208
209
        <table class="table table-bordered  table-striped  table-condensed">
210
            <tbody>
211
            <tr>
212
                <td class="">DBA Handler In Use</td>
213
                <td><?php echo $handler_in_use ?></td>
214
            </tr>
215
            <tr>
216
                <td class="">Available DBA Handlers</td>
217
                <td><?php echo $available_handlers ?></td>
218
            </tr>
219
            <tr>
220
                <td class="">DBA Cache File</td>
221
                <td><?php echo $cache->getStorage() ?></td>
222
            </tr>
223
            <tr>
224
                <td class="">PHP Version</td>
225
                <td><?php echo phpversion() ?></td>
226
            </tr>
227
228
            <?php if (!empty($_SERVER['SERVER_NAME'])) : ?>
229
                <tr>
230
                    <td class="">DBA Host</td>
231
                    <td><?php echo $_SERVER['SERVER_NAME'] . ' ' . $host ?></td>
232
                </tr>
233
            <?php endif; ?>
234
235
            <?php if (!empty($_SERVER['SERVER_SOFTWARE'])) : ?>
236
                <tr>
237
                    <td class="">Server Software</td>
238
                    <td><?php echo $_SERVER['SERVER_SOFTWARE'] ?></td>
239
                </tr>
240
            <?php endif; ?>
241
            <tr>
242
                <td class="">Start Time</td>
243
                <td><?php echo date($date_format, $file_info->getCTime()) ?></td>
244
            </tr>
245
            <tr>
246
                <td class="">Last Modified Time</td>
247
                <td><?php echo date($date_format, $file_info->getMTime()) ?></td>
248
            </tr>
249
            </tbody>
250
        </table>
251
    </section>
252
253
    <section>
254
        <a class="anchor" id="memory"></a>
255
256
        <h3>Memory Usage</h3>
257
258
        <div class="row">
259
            <div class="span4">
260
                <div class="well well-small">
261
                    <h4><?php echo bsize($file_info->getSize()) ?></h4>
262
263
                    <p>Cache file in use size</p>
264
                </div>
265
            </div>
266
            <div class="span4">
267
                <div class="well well-small">
268
                    <h4><?php echo bsize(disk_free_space($file_info->getPath())) ?></h4>
269
270
                    <p>Cache directory free size</p>
271
                </div>
272
            </div>
273
            <div class="span4">
274
                <div class="well well-small">
275
                    <h4><?php echo bsize(disk_total_space($file_info->getPath())) ?></h4>
276
277
                    <p>Cache directory total size</p>
278
                </div>
279
            </div>
280
        </div>
281
282
    </section>
283
284
    <section>
285
        <a class="anchor" id="speed"></a>
286
287
        <h3>Tune Cache</h3>
288
289
        <?php
290
        flash_msg($synchronize, 'Database synchronized');
291
        flash_msg($optimize, 'Database optimized');
292
        ?>
293
294
        <p class="">If you add-remove-substitute keys with data having different content length,
295
            the db continues to grow, wasting space. Sometimes it is necessary, to optimize or synchronize the db in
296
            order to remove unused data from the db itself.
297
        </p>
298
299
300
        <a class="anchor" id="memory"></a>
301
302
        <form accept-charset="utf-8" method="post" class="well" action="#speed" name="memory-acts">
303
304
            <table class="table">
305
                <tbody>
306
                <tr>
307
                    <td class="" style="border-top:0;">
308
                        <button class="btn btn-success" type="submit" name="optimize">Optimize Cache</button>
309
                    </td>
310
                    <td style="border-top:0;">
311
                        <p class="">Optimize a database, which usually consists of eliminating gaps between records
312
                            created by deletes.</p>
313
                    </td>
314
                </tr>
315
316
                <tr>
317
                    <td class="" style="border-top:0;">
318
                        <button class="btn btn-success" type="submit" name="synchronize">Synchronize Cache</button>
319
                    </td>
320
                    <td style="border-top:0;"><p class="">Synchronize the view of the database in memory and its image
321
                            on the disk.
322
                            As you insert records, they may be cached in memory by the underlying engine.
323
                            Other processes reading from the database will not see these new records until
324
                            synchronization.</p>
325
                    </td>
326
                </tr>
327
328
                </tbody>
329
            </table>
330
331
        </form>
332
333
    </section>
334
335
    <section>
336
        <a class="anchor" id="entries"></a>
337
338
        <h3>Cache Entries</h3>
339
340
        <?php
341
        flash_msg($create_test_entry, 'Entry created');
342
        flash_msg($delete_all, 'Cache flushed');
343
        flash_msg($delete_old, 'All old entries deleted');
344
        ?>
345
346
        <p class="">Sometimes it is necessary to sweep the cache from old entries or to flush it.</p>
347
348
        <form accept-charset="utf-8" method="post" class="well" action="#entries" name="entries-acts">
349
350
            <button class="btn btn-success" type="submit" name="create-test-entry">Create Test Entry</button>
351
352
            <?php if ($authenticated === false) : ?>
353
                <button class="btn btn-info" type="submit" name="login">Login and Sweep</button>
354
            <?php endif; ?>
355
356
            <?php if ($authenticated === true && $cache->erasable()) : ?>
357
                <button class="btn btn-success" type="submit" name="delete-old">Remove old entries</button>
358
            <?php endif; ?>
359
360
            <?php if ($authenticated === true) : ?>
361
                <button class="btn btn-danger" type="submit" name="delete-all">Flush Cache</button>
362
            <?php endif; ?>
363
        </form>
364
365
    </section>
366
367
    <p class="muted">
368
        Having trouble with PHP DBA Cache Monitor? Please contact <a
369
            href="mailto:[email protected]">[email protected]</a> and we’ll help you to sort it out.
370
        Because this GUI development and source control is done through GitHub, anyone is able to make contributions to
371
        it.
372
        Anyone can fix bugs and add features. <a href="https://github.com/gjerokrsteski/php-dba-cache" target="_blank">Fork
373
            it here!</a>
374
    </p>
375
376
</div>
377
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
378
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
379
</body>
380
</html>
381