]> git.mxchange.org Git - friendica.git/blob - include/pubsubpublish.php
Merge pull request #2190 from annando/1512-getload
[friendica.git] / include / pubsubpublish.php
1 <?php
2 require_once("boot.php");
3 require_once("include/ostatus.php");
4
5 function handle_pubsubhubbub() {
6         global $a, $db;
7
8         logger('start');
9
10         // We'll push to each subscriber that has push > 0,
11         // i.e. there has been an update (set in notifier.php).
12
13         $r = q("SELECT * FROM `push_subscriber` WHERE `push` > 0");
14
15         foreach($r as $rr) {
16                 //$params = get_feed_for($a, '', $rr['nickname'], $rr['last_update'], 0, true);
17                 $params = ostatus_feed($a, $rr['nickname'], $rr['last_update']);
18                 $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
19
20                 $headers = array("Content-type: application/atom+xml",
21                                 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
22                                         $a->get_baseurl().'/pubsubhubbub',
23                                         $rr['topic']),
24                                 "X-Hub-Signature: sha1=".$hmac_sig);
25
26                 logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
27
28                 post_url($rr['callback_url'], $params, $headers);
29                 $ret = $a->get_curl_code();
30
31                 if ($ret >= 200 && $ret <= 299) {
32                         logger('successfully pushed to '.$rr['callback_url']);
33
34                         // set last_update to "now", and reset push=0
35                         $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
36                         q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
37                                 dbesc($date_now),
38                                 intval($rr['id']));
39
40                 } else {
41                         logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
42
43                         // we use the push variable also as a counter, if we failed we
44                         // increment this until some upper limit where we give up
45                         $new_push = intval($rr['push']) + 1;
46
47                         if ($new_push > 30) // OK, let's give up
48                                 $new_push = 0;
49
50                         q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
51                                 $new_push,
52                                 intval($rr['id']));
53                 }
54         }
55
56         logger('done');
57 }
58
59
60 function pubsubpublish_run(&$argv, &$argc){
61         global $a, $db;
62
63         if(is_null($a)){
64                 $a = new App;
65         }
66
67         if(is_null($db)){
68                 @include(".htconfig.php");
69                 require_once("include/dba.php");
70                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
71                 unset($db_host, $db_user, $db_pass, $db_data);
72         };
73
74         require_once('include/items.php');
75         require_once('include/pidfile.php');
76
77         load_config('config');
78         load_config('system');
79
80         $lockpath = get_lockpath();
81         if ($lockpath != '') {
82                 $pidfile = new pidfile($lockpath, 'pubsubpublish');
83                 if($pidfile->is_already_running()) {
84                         logger("Already running");
85                         if ($pidfile->running_time() > 9*60) {
86                                 $pidfile->kill();
87                                 logger("killed stale process");
88                                 // Calling a new instance
89                                 proc_run('php',"include/pubsubpublish.php");
90                         }
91                         return;
92                 }
93         }
94
95         $a->set_baseurl(get_config('system','url'));
96
97         load_hooks();
98
99         if($argc > 1)
100                 $pubsubpublish_id = intval($argv[1]);
101         else
102                 $pubsubpublish_id = 0;
103
104         handle_pubsubhubbub();
105
106         return;
107
108 }
109
110 if (array_search(__file__,get_included_files())===0){
111   pubsubpublish_run($_SERVER["argv"],$_SERVER["argc"]);
112   killme();
113 }
114