updated database.defaults.sql and added some example scripts
[NeonServV5.git] / scripts / examples / calc.php
1 <?php
2 /* calc.php - NeonServ v5.6
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
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.
9  * 
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.
14  * 
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/>. 
17  */
18
19 $text = implode(" ", array_slice($argv, 1));
20
21 $descriptor = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
22 $proc = proc_open('bc -l -q', $descriptor, $pipes);
23
24 if(!is_resource($proc)) {
25     echo"internal calc error - please contact an administrator.\n";
26     die();
27 }
28
29 fwrite($pipes[0], $text."\nquit\n");
30
31 $start = time();
32
33 $read=array($pipes[1]);
34 $write=NULL;
35 $except=NULL;
36
37 while(1) {
38     $data = proc_get_status($proc);
39     if(!$data['running']) {
40         $out="";
41         while(!feof($pipes[1])) { 
42             $out .= fgets($pipes[1], 128); 
43         }
44         if($out == "") {
45             $out="\0034";
46             while(!feof($pipes[2])) { 
47                 $out .= fgets($pipes[2], 128); 
48             }
49             $out.="";
50         }
51         $out=str_replace("\n","",$out);
52         $out=str_replace("\r","",$out);
53         $out=str_replace("\\","",$out);
54         
55         if(strlen($text) > 20) {
56             if(strlen($out) > 450) {
57                 echo "output too long (".strlen($out).")\n";
58             } else {
59                 echo "".$text."\n";
60                 echo $out."\n";
61             }
62         } else {
63             $fout="".$text." = ".$out;
64             if(strlen($fout) > 450) {
65                 echo "output too long (".strlen($out).")";
66             } else {
67                 echo $fout."\n";
68             }
69         }
70         
71         fclose($pipes[0]);
72         fclose($pipes[1]);
73         fclose($pipes[2]);
74         proc_close($proc);
75         
76         die();
77     } else {
78         
79         if($start+3 > time()) {
80             usleep(200000); //200ms
81         } else {
82             //TIMEOUT
83             fclose($pipes[0]);
84             fclose($pipes[1]);
85             fclose($pipes[2]);
86             //can't simply use proc_terminate: https://bugs.php.net/bug.php?id=39992
87             $ppid = $data['pid'];
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
93                 }
94             }
95             proc_close($proc); 
96             echo "calculator timeout. (maximum of 3 seconds exceeded)\n";
97             die();
98         }
99     }
100 }
101
102 ?>