public
Description: WordPress plugin for displaying your most recent Brightkite position, note and photo.
Homepage: http://evansims.com/projects/brightkite-location
Clone URL: git://github.com/evansims/brightkite-location-for-wordpress.git
100644 192 lines (140 sloc) 9.318 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
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
<?php
/*
Plugin Name: Brightkite Location
Version: 1.0 Beta 3a
Plugin URI: http://evansims.com/projects/brightkite-location
Description: Displays a map of your last known location at Brightkite.com
Author: Evan Sims
Author URI: http://evansims.com
*/
 
function bkUpdateFeed($user, $path, $critical = false) {
 
$_user = sprintf("%u", crc32(md5($user . get_bloginfo('wpurl')))) . '.cache';
 
if(!@is_writable("{$path}{$_user}")) {
if(!@chmod("{$path}{$_user}", 0777)) {
if($critical) echo("<p><strong>Brightkite Location Plugin</strong><br />The file {$_user} is not writable. Please ensure that your Brighkite Location plugin folder is writable, or create the file manually and apply appropriate write permissions.");
return;
}
}
 
$headers = "GET /people/{$user}/objects.rss HTTP/1.0\r\n";
$headers .= "Host: brightkite.com\r\n";
$headers .= "Connection: Close\r\n\r\n";
 
$socket = @fsockopen("brightkite.com", 80, $errno, $errstr, 30);
if(!$socket) {
if($critical) echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to reach to the Brightkite servers. Try again later.</p>';
return false;
} else {
@fwrite($socket, $headers);
$data = ''; while (!feof($socket)) $data .= @fgets($socket, 128);
@fclose($socket);
 
$data = trim(substr($data, strpos($data, "\r\n\r\n")));
 
if(!$data) {
if($critical) echo '<p><strong>Brightkite Location Plugin</strong><br />There was an error retrieving your feed. Brightkite may be down for maintenance. Please try again shortly.</p>';
return false;
}
 
if(!function_exists('xmlize')) include("{$path}xml.php");
if(!function_exists('xmlize')) echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to load XML processing library.</p>';
 
$xml = xmlize($data);
 
if(!$xml || !isset($xml['rss']['#']['channel'][0]['#']['item'][0]['#']['title'])) {
if($critical) echo '<p><strong>Brightkite Location Plugin</strong><br />Brightkite returned an unexpected message. Please check your username and try again.</p>';
return false;
} unset($data);
 
$event = $xml['rss']['#']['channel'][0]['#']['item'][0]['#'];
 
$data['link'] = $event['link'][0]['#'];
 
$data['lat'] = $event['geo:lat'][0]['#'];
$data['long'] = $event['geo:long'][0]['#'];
 
if(isset($event['bk:placeAddress'][0]['#'])) {
$data['place'] = $event['bk:placeAddress'][0]['#'];
if(substr($data['place'], -4) == ' USA') {
// Strip out zip if in the United States.
$data['place'] = trim(substr($data['place'], 0, -4));
if(substr($data['place'], -1) == ',' && is_numeric(substr($data['place'], -6, 5))) {
$data['place'] = trim(substr($data['place'], 0, -7));
}
}
}
 
if($event['bk:eventType'][0]['#'] == 'photo' && isset($event['media:thumbnail'])) {
$photo = $event['media:thumbnail'][0]['@'];
 
$data['description'] = '<img src="' . $photo['url'] . '" width="' . $photo['width'] . '" height="' . $photo['height'] . '" alt="My most recent Brightkite checkin." />';
if($event['bk:photoCaption'][0]['#']) $data['description'] .= '<br />' . htmlentities($event['bk:photoCaption'][0]['#']);
} else {
$data['description'] = htmlentities($event['description'][0]['#']);
}
 
unset($xml);
 
$socket = @fopen("{$path}{$_user}", 'w');
if(!$socket) {
echo("<p><strong>Brightkite Location Plugin</strong><br />The file {$_user} is not writable. Please ensure that your Brighkite Location plugin folder is writable, or create the file manually and apply appropriate write permissions.");
return;
}
 
@fwrite($socket, '<' . '?php $bkRenderDate = \'' . time() . '\'; $bkRenderData = \'' . addslashes(serialize($data)) . '\'; ?' . '>');
@fclose($socket);
 
return $data;
}
 
}
 
function bkWordpressHeaders() {
 
if(strpos(__FILE__, '\\wp-content')) {
$path['css'] = substr(__FILE__, strpos(__FILE__, '\\wp-content'));
$path['css'] = substr($path['css'], 0, strrpos($path['css'], '\\'));
} else {
$path['css'] = substr(__FILE__, strpos(__FILE__, '/wp-content'));
$path['css'] = substr($path['css'], 0, strrpos($path['css'], '/'));
}
 
echo "\t" . '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('wpurl') . str_replace('\\', '/', $path['css']) . '/style.css" />' . "\n";
 
}
 
function bkRenderLocation($user, $api_key, $settings = array()) {
 
$_user = sprintf("%u", crc32(md5($user . get_bloginfo('wpurl')))) . '.cache';
 
if(strpos(__FILE__, '\\wp-content')) {
$path['physical'] = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '\\'));
$path['physical'] .= substr(__FILE__, strpos(__FILE__, '\\wp-content'));
$path['physical'] = substr($path['physical'], 0, strrpos($path['physical'], '\\')) . '/';
 
$path['css'] = substr(__FILE__, strpos(__FILE__, '\\wp-content'));
$path['css'] = substr($path['css'], 0, strrpos($path['css'], '\\'));
} else {
$path['physical'] = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));
$path['physical'] .= substr(__FILE__, strpos(__FILE__, '/wp-content'));
$path['physical'] = substr($path['physical'], 0, strrpos($path['physical'], '/')) . '/';
 
$path['css'] = substr(__FILE__, strpos(__FILE__, '/wp-content'));
$path['css'] = substr($path['css'], 0, strrpos($path['css'], '/'));
}
if(!$path['physical']) { echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to determine physical location.</p>'; return; }
if(!$path['css']) { echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to determine stylesheet location.</p>'; return; }
 
$path['css'] = str_replace('\\', '/', $path['css']) . '/style.css';
$data = null;
$bkRenderDate = null;
 
//if(!file_exists("{$path['physical']}{$_user}")) {
$data = bkUpdateFeed($user, $path['physical'], true);
//} else {
// include("{$path['physical']}{$_user}");
// if(!isset($bkRenderDate) || $bkRenderDate == null || (time() - $bkRenderDate > 600)) $data = bkUpdateFeed($user, $path['physical']);
// if(!$data) $data = unserialize(stripslashes($bkRenderData));
//}
 
if($data) {
 
if(!isset($settings['map_width'])) $settings['map_width'] = 223;
if(!isset($settings['map_height'])) $settings['map_height'] = 134;
if(!isset($settings['map_zoom'])) $settings['map_zoom'] = 14;
if(!isset($settings['map_beacon_color'])) $settings['map_beacon_color'] = 'red';
if(!isset($settings['map_beacon_size'])) $settings['map_beacon_size'] = 'mid';
 
if(!isset($settings['map_provider'])) $settings['map_provider'] = 'static';
if($settings['map_provider'] != 'static' && $settings['map_provider'] != 'dynamic') $settings['map_provider'] = 'static';
 
if($settings['map_provider'] == 'static') {
 
if(!isset($settings['map_style'])) $settings['map_style'] = 'roadmap';
if($settings['map_style'] != 'roadmap' && $settings['map_style'] != 'mobile') $settings['map_style'] = 'roadmap';
 
echo '<p id="brightkite-map"><a href="' . $data['link'] . '"><img class="google-map-static" src="http://maps.google.com/staticmap?center=' . $data['lat'] . ',' . $data['long'] . '&zoom=' . $settings['map_zoom'] . '&size=' . $settings['map_width'] . 'x' . $settings['map_height'] . '&maptype=' . $settings['map_style'] . '&markers=' . $data['lat'] . ',' . $data['long'] . ',' . $settings['map_beacon_size'] . $settings['map_beacon_color'] . '&key=' . $api_key . '" alt="Map" /></a></p>';
 
} elseif($settings['map_provider'] == 'dynamic') {
 
if(!isset($settings['map_style'])) $settings['map_style'] = 'terrain';
if($settings['map_style'] != 'terrain' && $settings['map_style'] != 'roadmap' && $settings['map_style'] != 'satellite' && $settings['map_style'] != 'hybrid') $settings['map_style'] = 'terrain';
 
if($settings['map_style'] == 'terrain') $settings['map_style'] = 'G_PHYSICAL_MAP';
elseif($settings['map_style'] == 'roadmap') $settings['map_style'] = 'G_NORMAL_MAP';
elseif($settings['map_style'] == 'satellite') $settings['map_style'] = 'G_SATELLITE_MAP';
elseif($settings['map_style'] == 'hybrid') $settings['map_style'] = 'G_HYBRID_MAP';
 
echo '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . $api_key . '" type="text/javascript"></script>';
echo '<div id="brightkite-map"><div id="brightkite-map-' . $user . '" style="width: ' . $settings['map_width']. 'px; height: ' . $settings['map_height']. 'px;"></div></div>';
echo '<script type="text/javascript">if (GBrowserIsCompatible()) { var map = new GMap2(document.getElementById("brightkite-map-' . $user . '")); map.enableScrollWheelZoom(); map.setMapType(G_PHYSICAL_MAP); map.addControl(new GSmallZoomControl()); map.setCenter(new GLatLng(' . $data['lat'] . ', ' . $data['long'] . '), ' . $settings['map_zoom'] . '); var point = new GLatLng(' . $data['lat'] . ', ' . $data['long'] . '); map.addOverlay(new GMarker(point)); }</script>';
 
}
 
echo '<p id="brightkite-loc"><a href="' . $data['link'] . '">' . $data['place'] . '</a></p>';
 
if(!strpos($data['description'], 'checked in @')) echo '<p id="brightkite-status">' . $data['description'] . '</p>';
 
} else {
 
echo("<p><strong>Brightkite Location Plugin</strong><br />Your feed could not be retrieved at this time. Try again shortly.");
 
}
 
}
 
add_action('wp_head', 'bkWordpressHeaders');
 
?>