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