-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubscriptionHandler.php
More file actions
81 lines (70 loc) · 2.31 KB
/
SubscriptionHandler.php
File metadata and controls
81 lines (70 loc) · 2.31 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace PubSubHubbubSubscriber;
use ImportStreamSource;
use MWTimestamp;
use WikiImporter;
class SubscriptionHandler {
/**
* @param string $topic
* @param string $hmacSignature
* @param string $file The file to read the POST data from. Defaults to stdin.
* @return bool whether the pushed data could be accepted.
*/
public function handlePush( $topic, $hmacSignature, $file = "php://input" ) {
$subscription = Subscription::findByTopic( $topic );
if ( $subscription && $subscription->isConfirmed() ) {
$source = ImportStreamSource::newFromFile( $file );
if ( $source->isGood() ) {
// Strip 'sha1='.
$hmacSignature = substr( trim( $hmacSignature ), 5 );
$content = file_get_contents( $file );
$expectedSignature = hash_hmac( 'sha1', $content, bin2hex( $subscription->getSecret() ), false );
if ( $expectedSignature !== $hmacSignature ) {
wfDebug( '[PubSubHubbubSubscriber] HMAC signature not matching. Ignoring data.' . PHP_EOL );
// Still need to return success according to specification.
return true;
}
$callbacks = new ImportCallbacks();
$importer = new WikiImporter( $source->value );
$importer->setLogItemCallback( array( &$callbacks, 'deletionPage' ) );
$callbacks->setPreviousPageOutCallback( $importer->setPageOutCallback(
array( &$callbacks, 'createRedirect' ) ) );
$importer->doImport();
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* @param string $topic
* @param int $lease_seconds
* @return bool whether the requested subscription could be confirmed.
*/
public function handleSubscribe( $topic, $lease_seconds ) {
$subscription = Subscription::findByTopic( $topic );
if ( $subscription && !$subscription->isConfirmed() ) {
$subscription->setConfirmed(true);
$subscription->setExpires( new MWTimestamp( $_SERVER['REQUEST_TIME'] + $lease_seconds ) );
$subscription->update();
return true;
} else {
return false;
}
}
/**
* @param string $topic
* @return bool whether the requested unsubscription could be confirmed.
*/
public function handleUnsubscribe( $topic ) {
$subscription = Subscription::findByTopic( $topic );
if ( $subscription && $subscription->isUnsubscribed() ) {
$subscription->delete();
return true;
} else {
return false;
}
}
}