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