1
|
<?php
|
2
|
/*
|
3
|
phpservice.inc
|
4
|
part of pfSense (https://www.pfSense.org/)
|
5
|
Copyright (C) 2008 Mark J Crane
|
6
|
Copyright (C) 2015 ESF, LLC
|
7
|
All rights reserved.
|
8
|
|
9
|
Redistribution and use in source and binary forms, with or without
|
10
|
modification, are permitted provided that the following conditions are met:
|
11
|
|
12
|
1. Redistributions of source code must retain the above copyright notice,
|
13
|
this list of conditions and the following disclaimer.
|
14
|
|
15
|
2. Redistributions in binary form must reproduce the above copyright
|
16
|
notice, this list of conditions and the following disclaimer in the
|
17
|
documentation and/or other materials provided with the distribution.
|
18
|
|
19
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
20
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
21
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
22
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
23
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
24
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
25
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
26
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
27
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28
|
POSSIBILITY OF SUCH DAMAGE.
|
29
|
*/
|
30
|
require_once('pkg-utils.inc');
|
31
|
|
32
|
function phpservice_sync_package() {
|
33
|
global $config;
|
34
|
conf_mount_rw();
|
35
|
|
36
|
if ($config['installedpackages']['phpservice']['config'] != "") {
|
37
|
$tmp = '
|
38
|
<?php
|
39
|
|
40
|
// Set time limit to indefinite execution
|
41
|
set_time_limit (0);
|
42
|
//
|
43
|
//run this program as long as the pid file exists
|
44
|
$filename = "/tmp/phpmonitor.pid";
|
45
|
$fp = fopen($filename, "w");
|
46
|
fwrite($fp, "If this file is deleted then phpmonitor will stop.");
|
47
|
fclose($fp);
|
48
|
unset($filename);
|
49
|
|
50
|
function send_to_syslog($syslogaddress, $syslogport, $syslogmsg) {
|
51
|
|
52
|
$syslogaddress = "127.0.0.1";
|
53
|
$syslogport = 514;
|
54
|
|
55
|
$fp = fsockopen("udp://".$syslogaddress, $syslogport, $errno, $errstr);
|
56
|
if (!$fp) {
|
57
|
return;
|
58
|
} else {
|
59
|
fwrite($fp, $syslogmsg);
|
60
|
fclose($fp);
|
61
|
}
|
62
|
}
|
63
|
|
64
|
$x = 0;
|
65
|
while ($x == 0) {
|
66
|
|
67
|
// If the pid file does not exist then close the program.
|
68
|
if (!file_exists("/tmp/phpmonitor.pid")) {
|
69
|
return;
|
70
|
}
|
71
|
|
72
|
|
73
|
';
|
74
|
|
75
|
foreach ($config['installedpackages']['phpservice']['config'] as $rowhelper) {
|
76
|
if ($rowhelper['enabled'] != "false") {
|
77
|
$tmp_php = base64_decode($rowhelper['php']);
|
78
|
if (strlen($tmp_php) > 0) {
|
79
|
$tmp .= "// name: ".$rowhelper['name']." \n";
|
80
|
$tmp .= "// description: " . $rowhelper['description'] . " \n\n";
|
81
|
$tmp .= preg_replace('/\r\n/', "\n", base64_decode($rowhelper['php']));
|
82
|
$tmp .= "\n";
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
|
87
|
$tmp .= '
|
88
|
|
89
|
sleep(1);
|
90
|
|
91
|
}
|
92
|
?>
|
93
|
';
|
94
|
|
95
|
$fout = fopen("/usr/local/pkg/phpservice.php", "w");
|
96
|
fwrite($fout, $tmp);
|
97
|
unset($tmp);
|
98
|
fclose($fout);
|
99
|
|
100
|
phpservice_write_rcfile();
|
101
|
if (is_service_running("phpservice")) {
|
102
|
restart_service("phpservice");
|
103
|
} else {
|
104
|
start_service("phpservice");
|
105
|
}
|
106
|
} else {
|
107
|
stop_service("phpservice");
|
108
|
unlink_if_exists("/usr/local/etc/rc.d/phpservice.sh");
|
109
|
}
|
110
|
conf_mount_ro();
|
111
|
}
|
112
|
|
113
|
function phpservice_write_rcfile() {
|
114
|
write_rcfile(array(
|
115
|
"file" => "phpservice.sh",
|
116
|
"start" => "/usr/local/bin/php -f /usr/local/pkg/phpservice.php >> /var/log/phpservice.log &",
|
117
|
"stop" => "/bin/rm -f /tmp/phpmonitor.pid; sleep 3"
|
118
|
)
|
119
|
);
|
120
|
}
|
121
|
|
122
|
function phpservice_custom_php_service_status_command() {
|
123
|
exec("/bin/pgrep -fq phpservice", $output, $retval);
|
124
|
return $retval;
|
125
|
}
|
126
|
|
127
|
function phpservice_deinstall_command() {
|
128
|
rmdir_recursive("/usr/local/www/packages/phpservice");
|
129
|
unlink_if_exists("/usr/local/pkg/phpservice.php");
|
130
|
}
|
131
|
|
132
|
?>
|