2 /* calc.php - NeonServ v5.6
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 $text = implode(" ", array_slice($argv, 1));
21 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
22 $proc = proc_open('bc -l -q', $descriptor, $pipes);
24 if(!is_resource($proc)) {
25 echo"internal calc error - please contact an administrator.\n";
29 fwrite($pipes[0], $text."\nquit\n");
33 $read=array($pipes[1]);
38 $data = proc_get_status($proc);
39 if(!$data['running']) {
41 while(!feof($pipes[1])) {
42 $out .= fgets($pipes[1], 128);
46 while(!feof($pipes[2])) {
47 $out .= fgets($pipes[2], 128);
51 $out=str_replace("\n","",$out);
52 $out=str_replace("\r","",$out);
53 $out=str_replace("\\","",$out);
55 if(strlen($text) > 20) {
56 if(strlen($out) > 450) {
57 echo "output too long (".strlen($out).")\n";
63 $fout="".$text." = ".$out;
64 if(strlen($fout) > 450) {
65 echo "output too long (".strlen($out).")";
79 if($start+3 > time()) {
80 usleep(200000); //200ms
86 //can't simply use proc_terminate: https://bugs.php.net/bug.php?id=39992
88 //use ps to get all the children of this process, and kill them
89 $pids = preg_split('/\s+/', `ps -o pid --no-heading --ppid $ppid`);
90 foreach($pids as $pid) {
91 if(is_numeric($pid)) {
92 posix_kill($pid, 9); //SIGKILL signal
96 echo "calculator timeout. (maximum of 3 seconds exceeded)\n";