-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivephpmonitor.php
More file actions
171 lines (155 loc) · 5.29 KB
/
livephpmonitor.php
File metadata and controls
171 lines (155 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Live.php file monitor
* @author Bence Meszaros
* @link http://bencemeszaros.com
* @version 1.2
*/
namespace MBence\LivePHP;
class LivephpMonitor
{
/** list of directories to check for changes */
protected $dirs = array('.');
/** ignore these files or directories */
protected $ignore = array('');
/** default time limit in seconds */
protected $timeLimit = 125;
/** Refresh css files without reloading the page */
protected $cssOnTheFly = true;
/** the time to die */
protected $deadLine;
/**
* Constructor
*/
public function __construct($options = array())
{
// check for runtime options
if (!empty($options) && is_array($options)) {
foreach ($options as $key => $val) {
if (isset($this->$key)) {
$this->$key = $val;
}
}
}
// check that $_GET['s'] is a valid unix timestamp
if (!empty($_GET['s']) && is_numeric($_GET['s']))
{
// $_GET['s'] is in millisec, but we need only seconds
$start = (int) ($_GET['s'] / 1000);
$this->headers();
$this->setDeadLine();
$this->main($start);
}
else
{
header('HTTP/1.1 400 Bad Request');
die;
}
}
/**
* Output the no-cache headers
*/
protected function headers()
{
header('Cache-Control: no-cache, must-revalidate');
header('Expires: -1');
}
/**
* Sets the time limit if possible
*/
protected function setDeadLine()
{
// in safe mode there is no way to set the time limit
if (!ini_get('safe_mode'))
{
set_time_limit($this->timeLimit);
}
// lets check what the actual limit is
$limit = ini_get('max_execution_time');
if (empty($limit) || $limit < 1)
{
// in case of unsuccesful ini_get, (or unlimited execution), we fall back to the default 30 sec
$limit = 30;
}
// we stop the loop 5 sec befor the time limit, just for sure
$this->deadLine = time() + $limit - 5;
}
/**
* Main function
* @param int $start start date in unix timestamp
*/
protected function main($start)
{
// clear file status cache
clearstatcache();
// long polling loop
do
{
// look for the changes every second until the execution time allows it.
foreach ($this->dirs as $root)
{
$result = $this->checkDir(realpath($root), $start);
if ($result)
{
// if we find modified files in any of the directories, we can skip the rest
echo json_encode($result);
die;
}
}
sleep(1);
}
while (time() < $this->deadLine);
}
/**
* A fast (and non-recursive) function to check for modified files in a directory structure
*
* @param string $root directory path
* @param int $start (unix timestamp) to find newer files of
* @return bool true (or the modification time of the css file) if modified file found, false otherwise
*/
protected function checkDir($root, $start)
{
$stack[] = $root;
// walk through the stack
while (!empty($stack))
{
$dir = array_shift($stack);
$files = glob($dir . '/*');
// make sure that we have an array (glob can return false in some cases)
if (!empty($files) && is_array($files))
{
foreach ($files as $file)
{
if (empty($this->ignore) || !in_array(basename($file), $this->ignore))
{
if (is_dir($file))
{
// we add the directories to the stack to check them later
$stack[] = $file;
}
elseif (is_file($file))
{
// and check the modification times of the files
$mtime = filemtime($file);
if ($mtime && $start < $mtime)
{
$pinfo = pathinfo($file);
// return true at the first positive match
if ($this->cssOnTheFly && $pinfo['extension'] == 'css') {
// if the file is a css then then we send the whole path back
return $mtime * 1000;
}
else {
// otherwise return true
return true;
}
}
}
}
} // end foreach
}
} // end while
return false;
}
} // end LivephpMonitor
new LivephpMonitor();