forked from gharlan/alfred-github-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.php
263 lines (242 loc) · 9.12 KB
/
workflow.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
require 'item.php';
class Workflow
{
const VERSION = '$Format:%H$';
const BUNDLE = 'de.gh01.alfred.github';
const DEFAULT_CACHE_MAX_AGE = 10;
private static $fileCookies;
/** @var PDO */
private static $db;
private static $query;
private static $items = array();
public static function init($query = null)
{
self::$query = $query;
$dataDir = $_SERVER['HOME'] . '/Library/Application Support/Alfred 2/Workflow Data/' . self::BUNDLE;
if (!is_dir($dataDir)) {
mkdir($dataDir);
}
self::$fileCookies = $dataDir . '/cookies';
$fileDb = $dataDir . '/db.sqlite';
$exists = file_exists($fileDb);
self::$db = new PDO('sqlite:' . $fileDb, null, null, array(PDO::ATTR_PERSISTENT => true));
if (!$exists) {
self::$db->exec('
CREATE TABLE config (
key TEXT PRIMARY KEY,
value TEXT
)
');
self::$db->exec('
CREATE TABLE request_cache (
url TEXT PRIMARY KEY,
timestamp INTEGER,
etag TEXT,
content TEXT,
refresh INTEGER
)
');
}
register_shutdown_function(array(__CLASS__, 'shutdown'));
}
public static function shutdown()
{
self::$db->exec('DELETE FROM request_cache WHERE timestamp < ' . (time() - 30 * 24 * 60 * 60));
}
public static function setConfig($key, $value)
{
self::getStatement('REPLACE INTO config VALUES(?, ?)')->execute(array($key, $value));
}
public static function getConfig($key, $default = null)
{
$stmt = self::getStatement('SELECT value FROM config WHERE key = ?');
$stmt->execute(array($key));
$value = $stmt->fetchColumn();
return false !== $value ? $value : $default;
}
public static function removeConfig($key)
{
self::getStatement('DELETE FROM config WHERE key = ?')->execute(array($key));
}
public static function request($url, &$status = null, &$etag = null, $post = false, array $data = array())
{
$debug = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, self::$fileCookies);
curl_setopt($ch, CURLOPT_COOKIEFILE, self::$fileCookies);
curl_setopt($ch, CURLOPT_USERAGENT, 'alfred-github-workflow');
if ($debug) {
curl_setopt($ch, CURLOPT_PROXY, 'localhost');
curl_setopt($ch, CURLOPT_PROXYPORT, 8888);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
} elseif ($etag) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('If-None-Match: ' . $etag));
}
$response = curl_exec($ch);
if (false === $response) {
curl_close($ch);
return false;
}
if ($debug) {
list(, $header, $body) = explode("\r\n\r\n", $response, 3);
} else {
list($header, $body) = explode("\r\n\r\n", $response, 2);
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (preg_match('/^ETag: (\V*)/mi', $header, $match)) {
$etag = $match[1];
}
return $status == 200 ? $body : null;
}
public static function requestCache($url, $maxAge = self::DEFAULT_CACHE_MAX_AGE, $refreshInBackground = true)
{
$stmt = self::getStatement('SELECT * FROM request_cache WHERE url = ?');
$stmt->execute(array($url));
$stmt->bindColumn('timestamp', $timestamp);
$stmt->bindColumn('etag', $etag);
$stmt->bindColumn('content', $content);
$stmt->bindColumn('refresh', $refresh);
$stmt->fetch(PDO::FETCH_BOUND);
if ($timestamp < time() - 60 * $maxAge) {
if ($refreshInBackground && !is_null($content)) {
if ($refresh < time() - 60) {
self::getStatement('UPDATE request_cache SET refresh = ? WHERE url = ?')->execute(array(time(), $url));
exec('php action.php "> refresh-cache ' . $url . '" > /dev/null 2>&1 &');
}
return $content;
}
$newContent = self::request($url, $status, $etag);
if (false === $newContent) {
return $content;
}
switch ($status) {
/** @noinspection PhpMissingBreakStatementInspection */
case 200:
$content = $newContent;
// fall trough
case 304:
$timestamp = time();
self::getStatement('REPLACE INTO request_cache VALUES(?, ?, ?, ?, 0)')->execute(array($url, $timestamp, $etag, $content));
break;
default:
self::getStatement('DELETE FROM request_cache WHERE url = ?')->execute(array($url));
return null;
}
}
return $content;
}
public static function requestCacheJson($url, $key = null, $maxAge = self::DEFAULT_CACHE_MAX_AGE)
{
$content = self::requestCache($url, $maxAge);
if (!is_string($content)) {
return null;
}
$content = json_decode($content);
if ($key && !isset($content->$key)) {
return null;
}
return $key ? $content->$key : $content;
}
public static function deleteCache()
{
self::$db->exec('DELETE FROM request_cache');
}
public static function deleteCookies()
{
if (file_exists(self::$fileCookies)) {
unlink(self::$fileCookies);
}
self::removeConfig('user');
}
public static function getToken($content = null)
{
$content = $content ?: self::request('https://github.com/');
preg_match('@<meta content="(.*)" name="csrf-token" />@U', $content, $match);
return isset($match[1]) ? $match[1] : null;
}
public static function askForPassword($title, $label)
{
return exec('osascript <<END
tell application "Alfred 2"
activate
set alfredPath to (path to application "Alfred 2")
set alfredIcon to path to resource "appicon.icns" in bundle (alfredPath as alias)
display dialog "' . escapeshellcmd(addslashes($label)) . ':" with title "' . escapeshellcmd(addslashes($title)) . '" buttons {"OK"} default button "OK" default answer "" with icon alfredIcon with hidden answer
set answer to text returned of result
end tell
END');
}
public static function checkUpdate()
{
if (self::getConfig('version') !== self::VERSION) {
if (file_exists($file = __DIR__ . '/class.php')) {
unlink($file);
}
$configFile = $_SERVER['HOME'] . '/Library/Application Support/Alfred 2/Workflow Data/' . self::BUNDLE . '/config.json';
if (file_exists($configFile)) {
$config = json_decode(file_get_contents($configFile), true);
foreach ($config as $key => $value) {
self::setConfig($key, $value);
}
unlink($configFile);
}
$cacheDir = $_SERVER['HOME'] . '/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/' . self::BUNDLE;
$cacheFile = $cacheDir . '/cache.json';
if (file_exists($cacheFile)) {
$cache = json_decode(file_get_contents($cacheFile), true);
$stmt = self::getStatement('REPLACE INTO request_cache VALUES(?, ?, ?, ?, 0)');
foreach ($cache as $url => $c) {
$stmt->execute(array($url, $c['timestamp'], $c['etag'], $c['content']));
}
unlink($cacheFile);
}
if (is_dir($cacheDir)) {
rmdir($cacheDir);
}
self::setConfig('version', self::VERSION);
self::deleteCache();
}
if (!self::getConfig('autoupdate', 1)) {
return false;
}
$version = self::requestCache('http://gh01.de/alfred/github/current', 1440);
return $version !== null && $version !== self::VERSION;
}
public static function addItem(Item $item, $check = true)
{
if (!$check || $item->match(self::$query)) {
self::$items[] = $item;
}
}
public static function sortItems()
{
usort(self::$items, function (Item $a, Item $b) {
return $a->compare($b);
});
}
public static function getItemsAsXml()
{
return Item::toXml(self::$items);
}
/**
* @param string $query
* @return PDOStatement
*/
protected static function getStatement($query)
{
static $stmts = array();
if (!isset($stmts[$query])) {
$stmts[$query] = self::$db->prepare($query);
}
return $stmts[$query];
}
}