aboutsummaryrefslogtreecommitdiffstats
path: root/addon/poormancron/poormancron.php
blob: bbe023cd731aa14ed2ce308fd7677e8464508179 (plain) (blame)
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
<?php
/**
 * Name: Poor Man Cron
 * Description: Execute updates on pageviews, without the need of commandline php
 * Version: 1.2
 * Author: Fabio Comuni <http://kirgroup.com/profile/fabrix>
 */

function poormancron_install() {
	// check for command line php
	$a = get_app();
	$ex = Array();
	$ex[0] = ((x($a->config,'php_path')) && (strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
	$ex[1] = dirname(dirname(dirname(__file__)))."/testargs.php";
	$ex[2] = "test";
	$out = exec(implode(" ", $ex));
	if ($out==="test") {
		set_config('poormancron','usecli',1);
		logger("poormancron will use cli php");
	} else {
		set_config('poormancron','usecli',0);
		logger("poormancron will NOT use cli php");
	}
	
	register_hook('page_end', 'addon/poormancron/poormancron.php', 'poormancron_hook');
	register_hook('proc_run', 'addon/poormancron/poormancron.php','poormancron_procrun');
	logger("installed poormancron");
}

function poormancron_uninstall() {
	unregister_hook('page_end', 'addon/poormancron/poormancron.php', 'poormancron_hook');
	unregister_hook('proc_run', 'addon/poormancron/poormancron.php','poormancron_procrun');
	logger("removed poormancron");
}



function poormancron_hook(&$a,&$b) {
    $now = time();
    $lastupdate = get_config('poormancron', 'lastupdate');

    // 300 secs, 5 mins
    if (!$lastupdate || ($now-$lastupdate)>300) {
        set_config('poormancron','lastupdate', $now);
        proc_run('php',"include/poller.php");
    }
}

function poormancron_procrun(&$a, &$arr) {
	if (get_config('poormancron','usecli')==1) return;
	$argv = $arr['args'];
	$arr['run_cmd'] = false;
	logger("poormancron procrun ".implode(", ",$argv));
	array_shift($argv);
	$argc = count($argv);
	logger("poormancron procrun require_once ".basename($argv[0]));
	require_once(basename($argv[0]));
	$funcname=str_replace(".php", "", basename($argv[0]))."_run";
  
	$funcname($argv, $argc);
}


?>