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