]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Checking includes for valid paths
[friendica.git] / include / poller.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 require_once("boot.php");
14
15 function poller_run(&$argv, &$argc){
16         global $a, $db;
17
18         if(is_null($a)) {
19                 $a = new App;
20         }
21
22         if(is_null($db)) {
23                 @include(".htconfig.php");
24                 require_once("include/dba.php");
25                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
26                 unset($db_host, $db_user, $db_pass, $db_data);
27         };
28
29         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
30                 // Run the cron job that calls all other jobs
31                 proc_run("php","include/cron.php");
32
33                 // Run the cronhooks job separately from cron for being able to use a different timing
34                 proc_run("php","include/cronhooks.php");
35
36                 // Cleaning dead processes
37                 $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
38                 foreach($r AS $pid)
39                         if (!posix_kill($pid["pid"], 0))
40                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
41                                         intval($pid["pid"]));
42
43         } else
44                 // Sleep two seconds before checking for running processes to avoid having too many workers
45                 sleep(4);
46
47         // Checking number of workers
48         $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
49
50         $queues = intval(get_config("system", "worker_queues"));
51
52         if ($queues == 0)
53                 $queues = 4;
54
55         if ($workers[0]["workers"] >= $queues)
56                 return;
57
58         while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
59                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
60                         dbesc(datetime_convert()),
61                         intval(getmypid()),
62                         intval($r[0]["id"]));
63
64                 $argv = json_decode($r[0]["parameter"]);
65
66                 $argc = count($argv);
67
68                 // Check for existance and validity of the include file
69                 $include = $argv[0];
70
71                 if (!validate_include($include)) {
72                         logger("Include file ".$argv[0]." is not valid!");
73                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
74                         continue;
75                 }
76
77                 require_once($include);
78
79                 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
80
81                 if (function_exists($funcname)) {
82                         logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
83                         $funcname($argv, $argc);
84
85                         logger("Process ".getmypid().": ".$funcname." - done");
86
87                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
88                 } else
89                         logger("Function ".$funcname." does not exist");
90         }
91
92 }
93
94 if (array_search(__file__,get_included_files())===0){
95   poller_run($_SERVER["argv"],$_SERVER["argc"]);
96   killme();
97 }
98 ?>