-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.driver.php
More file actions
116 lines (93 loc) · 4.06 KB
/
extension.driver.php
File metadata and controls
116 lines (93 loc) · 4.06 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
<?php
define('BITLY_CACHE', MANIFEST . '/cache/bitly');
class Extension_bitly extends Extension {
/*-------------------------------------------------------------------------
Delegate
-------------------------------------------------------------------------*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/frontend/',
'delegate' => 'FrontendParamsResolve',
'callback' => 'frontendParamsResolve'
)
);
}
/*-------------------------------------------------------------------------
Delegated functions
-------------------------------------------------------------------------*/
public function appendPreferences($context){
$group = new XMLElement('fieldset',null,array('class'=>'settings'));
$group->appendChild(new XMLElement('legend', 'Bit.ly Authentication'));
$div = new XMLElement('div',null,array('class'=>'group'));
$label = Widget::Label();
$input = Widget::Input('settings[bitly][login]', Symphony::Configuration()->get('login', 'bitly'), 'text');
$label->setValue(__('Username') . $input->generate());
$div->appendChild($label);
$label = Widget::Label();
$input = Widget::Input('settings[bitly][apikey]', Symphony::Configuration()->get('apikey', 'bitly'), 'password');
$label->setValue(__('API Key') . $input->generate());
$div->appendChild($label);
$group->appendChild($div);
// Append preferences
$context['wrapper']->appendChild($group);
}
public function frontendParamsResolve($context) {
$url = $context["params"]["current-url"];
try {
$result = $this->getFromCache($url);
if($result == false) {
$options = 'apikey=' . Symphony::Configuration()->get('apikey', 'bitly') . '&';
$options .= 'login=' . Symphony::Configuration()->get('login', 'bitly') . '&';
$options .= 'longUrl=' . $url;
$response = file_get_contents('https://api-ssl.bitly.com/v3/shorten/?' . $options);
$data = json_decode($response, true);
$result = $data["data"]["url"];
}
$context["params"]["tinyurl"] = $result;
$this->persist($url, $result);
} catch(Exception $exp) {
$context["params"]["tinyurl"] = '';
}
}
/*-------------------------------------------------------------------------
Private functions
-------------------------------------------------------------------------*/
private function getFromCache($url) {
if(file_exists(BITLY_CACHE)) {
$cache = file_get_contents(BITLY_CACHE);
$cache = json_decode($cache, true);
$token = md5($url);
if(array_key_exists($token, $cache[0])) {
return $cache[0][$token]["tinyurl"];
} else {
return false;
}
} else {
return false;
}
}
private function persist($url, $result) {
$cache = array();
$cache[] = array();
if(file_exists(BITLY_CACHE)) {
$cache = file_get_contents(BITLY_CACHE);
$cache = json_decode($cache, true);
}
$token = md5($url);
if(!array_key_exists($token, $cache)) {
$cache[0][$token] = array(
"url" => $url,
"tinyurl" => $result
);
}
$cache = json_encode($cache);
General::writeFile(BITLY_CACHE, $cache);
}
}
?>