-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingcatcher.php
171 lines (133 loc) · 4.17 KB
/
pingcatcher.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
require( 'secrets.php' );
if( !isset( $_GET[ 'secret' ] ) || $_GET[ 'secret' ] != EXEC_SECRET ) {
die( 'Adios.' );
}
/**
* Ping Catcher v1
*/
class PingCatcher
{
public $history;
function log_task( $sig, $note )
{
$my_file = 'history/' . $sig . '.txt';
$handle = fopen( $my_file, 'w' ) or die( 'Cannot open file: ' . $my_file );
$data = date('r') . PHP_EOL . PHP_EOL . $note;
fwrite($handle, $data);
fclose($handle);
return true;
}
function already_added( $sig )
{
return file_exists( 'history/' . $sig . '.txt' );
}
//clean out any tasks added more than 72 hours ago
function clean_history()
{
return;
}
function add_task( $title, $link = null , $project = 'github', $note = null )
{
$temp_id = md5( time() . rand() );
$uuid = sha1( time() . rand() );
$temp_id2 = md5( time() . rand() );
$uuid2 = sha1( time() . rand() );
$sig = md5( $title . $link . $project . $note );
if( $this->already_added( $sig ) ) {
echo 'Already added "' . $title . '" (' . $link . ')<br />';
return;
}
echo '<strong>Adding: <a href="' . $link . '">' . $title . '</a></strong><br />';
echo '<pre>' . $note . '</pre>';
if( $project == 'wpcom' ) {
$project_id = WPC_TARGET_LIST;
} elseif( $project == 'wpcom-p2' ) {
$project_id = WPC_P2_TARGET_LIST;
} else {
$project_id = GITHUB_TARGET_LIST;
}
$args = array();
$args['type'] = 'item_add';
$args['temp_id'] = $temp_id;
$args['uuid'] = $uuid;
$args['args'] = array();
if( $link ) {
$args['args']['content'] = $link . ' (' . $title . ')';
} else {
$args['args']['content'] = $title;
}
$args['args']['project_id'] = $project_id;
if( $note ) {
$note = str_ireplace( '<blockquote>', '========= Quote =========' . PHP_EOL, $note );
$note = str_ireplace( '</blockquote>', PHP_EOL . '========= End Quote =========' . PHP_EOL, $note );
$note = str_ireplace( '<em>', '_', $note );
$note = str_ireplace( '</em>', '_', $note );
$note = str_ireplace( '<strong>', '__', $note );
$note = str_ireplace( '</strong>', '__', $note );
$note = str_ireplace( '<code>', '` ', $note );
$note = str_ireplace( '</code>', ' `', $note );
$note = strip_tags( $note );
// If we can add the task, then try to add The Body
if( $this->todoist_request( $args ) )
{
$this->log_task( $sig, $args['args']['content'] . PHP_EOL . $note );
$args = array();
$args['type'] = 'note_add';
$args['temp_id'] = $temp_id2;
$args['uuid'] = $uuid2;
$args['args'] = array();
$args['args']['content'] = $note;
$args['args']['item_id'] = $temp_id;
// Add note
$this->todoist_request( $args );
}
}
return true;
}
function todoist_request( $opts = array() )
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://todoist.com/api/v7/sync",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"token\"\r\n\r\n" . TODOIST_TOKEN ."\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"commands\"\r\n\r\n" . '[' . json_encode( $opts ) . ']' . "\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
),
));
$ret = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return false;
}
sleep( 5 );
$o = json_decode( $ret, true );
foreach( $o['sync_status'] as $is_ok ) {
if( $is_ok == 'ok' ) {
return true;
} else {
return false;
}
}
}
}
if( WPC_TOKEN ) {
echo 'Checking wpcom';
include 'wpcom.php';
$wpc = new WPCom;
$wpc->get_mentions();
}
if( GITHUB_TOKEN ) {
echo 'Checking github';
include 'github.php';
$github = new GitHub;
$github->get_mentions();
}