forked from lxbarth/PuSHSubscriber
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPSubscriptionFile.php
More file actions
137 lines (128 loc) · 3.52 KB
/
Copy pathPSubscriptionFile.php
File metadata and controls
137 lines (128 loc) · 3.52 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Implement file-based storage backend for subscriptions.
*
* Variables passed in to the constructor must be accessible as public class
* variables.
*/
class PSubscriptionFile implements PuSHSubscriptionInterface {
public static $subscription_dir = './subscriptions';
public static $subscriptions;
/**
* @param $domain
* A string that defines the domain in which the subscriber_id is unique.
* @param $subscriber_id
* A unique numeric subscriber id.
* @param $hub
* The URL of the hub endpoint.
* @param $topic
* The topic to subscribe to.
* @param $secret
* A secret key used for message authentication.
* @param $status
* The status of the subscription.
* 'subscribe' - subscribing to a feed.
* 'unsubscribe' - unsubscribing from a feed.
* 'subscribed' - subscribed.
* 'unsubscribed' - unsubscribed.
* 'subscribe failed' - subscribe request failed.
* 'unsubscribe failed' - unsubscribe request failed.
* @param $post_fields
* An array of the fields posted to the hub.
*/
public function __construct($domain, $subscriber_id, $hub, $topic, $secret, $status = '', $callback_url, $verify_token, $lease_time) {
$this->domain = $domain;
$this->subscriber_id = $subscriber_id;
$this->hub = $hub;
$this->topic = $topic;
$this->secret = $secret;
$this->status = $status;
$this->callback_url = $callback_url;
$this->verify_token = $verify_token;
$this->lease_time = $lease_time;
}
/**
* Save a subscription.
*/
public function save() {
$filepath = self::get_filepath($this->domain, $this->subscriber_id);
if(file_put_contents($filepath, $this->_toJSON(), LOCK_EX)) {
return true;
} else {
return false;
}
}
/**
* Delete a subscription.
*/
public function delete() {
$filepath = self::get_filepath($this->domain, $this->subscriber_id);
if(is_readable($filepath)) {
if(unlink($filepath)) {
return true;
} else {
return false;
}
} else {
return true;
}
}
public function _toJSON() {
$data = array(
'domain' => $this->domain,
'subscriber_id' => $this->subscriber_id,
'hub' => $this->hub,
'topic' => $this->topic,
'secret' => $this->secret,
'status' => $this->status,
'callback_url' => $this->callback_url,
'verify_token' => $this->verify_token,
'lease_time' => $this->lease_time
);
return json_encode($data);
}
public static _fromJSON($data) {
$data = json_decode($data, true);
$subscription = new PSubscriptionFile(
$data['domain'],
$data['subscriber_id'],
$data['hub'],
$data['topic'],
$data['secret'],
$data['status'],
$data['callback_url'],
$data['verify_token'],
$data['lease_time']
);
return $subscription;
}
public static function get_filepath($domain, $subscriber_id) {
return self::$subscription_dir . '/' . $domain . '/' . $subscriber_id . '.pubsub';
}
/**
* Load a subscription.
*
* @return
* A PuSHSubscriptionInterface object if a subscription exist, NULL
* otherwise.
*/
public static function load($domain, $subscriber_id) {
if(isset(self::$subscriptions[$domain][$subscriber_id])) {
return self::$subscriptions[$domain][$subscriber_id];
} else {
$filepath = self::get_filepath($domain, $subscriber_id);
if(is_readable($filepath)) {
if($data = file_get_contents($filepath)) {
$sub = self::_fromJSON($data);
self::$subscriptions[$domain][$subscriber_id] = $sub;
return $sub;
} else {
return NULL;
}
} else {
return NULL;
}
}
}
}
?>