-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanurl.php
67 lines (53 loc) · 1.48 KB
/
cleanurl.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
<?php
error_reporting(E_ALL);
// get clipboard from STDIN (piped in Alfred Script Filter)
$url = fgets(STDIN);
$shorter_url = null;
$response = ['items' => []];
// check if message is an URL and if so get it's <title>
if (filter_var($url, FILTER_VALIDATE_URL)) {
// load all cleaner classes
require_once('URLCleaner.php');
foreach (glob("Cleaner/*.php") as $filename) {
require_once $filename;
}
$cleaners = array_filter(
get_declared_classes(),
function ($className) {
return in_array('URLCleaner', class_implements($className));
}
);
// check if we can handle the URL
$url_parts = parse_url($url);
foreach($cleaners as $c) {
$cleaner = new $c();
if ($cleaner->isApplicable($url_parts['host'])) {
$shorter_url = $cleaner->getShortURL($url_parts);
break;
}
}
if (!is_null($shorter_url)) {
$response['items'][] = [
'arg' => $shorter_url,
'title' => $shorter_url,
'subtitle' => sprintf('New URL is %s chars shorter.', ( strlen($url) - strlen($shorter_url) )),
'text' => [
'copy' => $shorter_url,
]
];
} else {
$response['items'][] = [
'arg' => 'new_url',
'title' => 'Sorry, I don\'t know this URL yet.',
'subtitle' => 'Hit enter for infos on how to add a new URL.',
];
}
} else {
// clipboard doesnt contain a valid URL.
$response['items'][] = [
'arg' => '',
'title' => 'Sorry, in your clipboard is no URL',
];
}
// Report back to Alfred
echo json_encode($response);