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
607033f8 » evansims 2008-08-27 v1.0 Beta 3a 1 <?php
2 /*
3 Plugin Name: Brightkite Location
4 Version: 1.0 Beta 3a
5 Plugin URI: http://evansims.com/projects/brightkite-location
6 Description: Displays a map of your last known location at Brightkite.com
7 Author: Evan Sims
8 Author URI: http://evansims.com
9 */
10
11 function bkUpdateFeed($user, $path, $critical = false) {
12
13 $_user = sprintf("%u", crc32(md5($user . get_bloginfo('wpurl')))) . '.cache';
14
15 if(!@is_writable("{$path}{$_user}")) {
16 if(!@chmod("{$path}{$_user}", 0777)) {
17 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.");
18 return;
19 }
20 }
21
22 $headers = "GET /people/{$user}/objects.rss HTTP/1.0\r\n";
23 $headers .= "Host: brightkite.com\r\n";
24 $headers .= "Connection: Close\r\n\r\n";
25
26 $socket = @fsockopen("brightkite.com", 80, $errno, $errstr, 30);
27 if(!$socket) {
28 if($critical) echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to reach to the Brightkite servers. Try again later.</p>';
29 return false;
30 } else {
31 @fwrite($socket, $headers);
32 $data = ''; while (!feof($socket)) $data .= @fgets($socket, 128);
33 @fclose($socket);
34
35 $data = trim(substr($data, strpos($data, "\r\n\r\n")));
36
37 if(!$data) {
38 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>';
39 return false;
40 }
41
42 if(!function_exists('xmlize')) include("{$path}xml.php");
43 if(!function_exists('xmlize')) echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to load XML processing library.</p>';
44
45 $xml = xmlize($data);
46
47 if(!$xml || !isset($xml['rss']['#']['channel'][0]['#']['item'][0]['#']['title'])) {
48 if($critical) echo '<p><strong>Brightkite Location Plugin</strong><br />Brightkite returned an unexpected message. Please check your username and try again.</p>';
49 return false;
50 } unset($data);
51
52 $event = $xml['rss']['#']['channel'][0]['#']['item'][0]['#'];
53
54 $data['link'] = $event['link'][0]['#'];
55
56 $data['lat'] = $event['geo:lat'][0]['#'];
57 $data['long'] = $event['geo:long'][0]['#'];
58
59 if(isset($event['bk:placeAddress'][0]['#'])) {
60 $data['place'] = $event['bk:placeAddress'][0]['#'];
61 if(substr($data['place'], -4) == ' USA') {
62 // Strip out zip if in the United States.
63 $data['place'] = trim(substr($data['place'], 0, -4));
64 if(substr($data['place'], -1) == ',' && is_numeric(substr($data['place'], -6, 5))) {
65 $data['place'] = trim(substr($data['place'], 0, -7));
66 }
67 }
68 }
69
70 if($event['bk:eventType'][0]['#'] == 'photo' && isset($event['media:thumbnail'])) {
71 $photo = $event['media:thumbnail'][0]['@'];
72
73 $data['description'] = '<img src="' . $photo['url'] . '" width="' . $photo['width'] . '" height="' . $photo['height'] . '" alt="My most recent Brightkite checkin." />';
74 if($event['bk:photoCaption'][0]['#']) $data['description'] .= '<br />' . htmlentities($event['bk:photoCaption'][0]['#']);
75 } else {
76 $data['description'] = htmlentities($event['description'][0]['#']);
77 }
78
79 unset($xml);
80
81 $socket = @fopen("{$path}{$_user}", 'w');
82 if(!$socket) {
83 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.");
84 return;
85 }
86
87 @fwrite($socket, '<' . '?php $bkRenderDate = \'' . time() . '\'; $bkRenderData = \'' . addslashes(serialize($data)) . '\'; ?' . '>');
88 @fclose($socket);
89
90 return $data;
91 }
92
93 }
94
95 function bkWordpressHeaders() {
96
97 if(strpos(__FILE__, '\\wp-content')) {
98 $path['css'] = substr(__FILE__, strpos(__FILE__, '\\wp-content'));
99 $path['css'] = substr($path['css'], 0, strrpos($path['css'], '\\'));
100 } else {
101 $path['css'] = substr(__FILE__, strpos(__FILE__, '/wp-content'));
102 $path['css'] = substr($path['css'], 0, strrpos($path['css'], '/'));
103 }
104
105 echo "\t" . '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('wpurl') . str_replace('\\', '/', $path['css']) . '/style.css" />' . "\n";
106
107 }
108
109 function bkRenderLocation($user, $api_key, $settings = array()) {
110
111 $_user = sprintf("%u", crc32(md5($user . get_bloginfo('wpurl')))) . '.cache';
112
113 if(strpos(__FILE__, '\\wp-content')) {
114 $path['physical'] = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '\\'));
115 $path['physical'] .= substr(__FILE__, strpos(__FILE__, '\\wp-content'));
116 $path['physical'] = substr($path['physical'], 0, strrpos($path['physical'], '\\')) . '/';
117
118 $path['css'] = substr(__FILE__, strpos(__FILE__, '\\wp-content'));
119 $path['css'] = substr($path['css'], 0, strrpos($path['css'], '\\'));
120 } else {
121 $path['physical'] = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/'));
122 $path['physical'] .= substr(__FILE__, strpos(__FILE__, '/wp-content'));
123 $path['physical'] = substr($path['physical'], 0, strrpos($path['physical'], '/')) . '/';
124
125 $path['css'] = substr(__FILE__, strpos(__FILE__, '/wp-content'));
126 $path['css'] = substr($path['css'], 0, strrpos($path['css'], '/'));
127 }
128 if(!$path['physical']) { echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to determine physical location.</p>'; return; }
129 if(!$path['css']) { echo '<p><strong>Brightkite Location Plugin</strong><br />Unable to determine stylesheet location.</p>'; return; }
130
131 $path['css'] = str_replace('\\', '/', $path['css']) . '/style.css';
132 $data = null;
133 $bkRenderDate = null;
134
135 //if(!file_exists("{$path['physical']}{$_user}")) {
136 $data = bkUpdateFeed($user, $path['physical'], true);
137 //} else {
138 // include("{$path['physical']}{$_user}");
139 // if(!isset($bkRenderDate) || $bkRenderDate == null || (time() - $bkRenderDate > 600)) $data = bkUpdateFeed($user, $path['physical']);
140 // if(!$data) $data = unserialize(stripslashes($bkRenderData));
141 //}
142
143 if($data) {
144
145 if(!isset($settings['map_width'])) $settings['map_width'] = 223;
146 if(!isset($settings['map_height'])) $settings['map_height'] = 134;
147 if(!isset($settings['map_zoom'])) $settings['map_zoom'] = 14;
148 if(!isset($settings['map_beacon_color'])) $settings['map_beacon_color'] = 'red';
149 if(!isset($settings['map_beacon_size'])) $settings['map_beacon_size'] = 'mid';
150
151 if(!isset($settings['map_provider'])) $settings['map_provider'] = 'static';
152 if($settings['map_provider'] != 'static' && $settings['map_provider'] != 'dynamic') $settings['map_provider'] = 'static';
153
154 if($settings['map_provider'] == 'static') {
155
156 if(!isset($settings['map_style'])) $settings['map_style'] = 'roadmap';
157 if($settings['map_style'] != 'roadmap' && $settings['map_style'] != 'mobile') $settings['map_style'] = 'roadmap';
158
159 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>';
160
161 } elseif($settings['map_provider'] == 'dynamic') {
162
163 if(!isset($settings['map_style'])) $settings['map_style'] = 'terrain';
164 if($settings['map_style'] != 'terrain' && $settings['map_style'] != 'roadmap' && $settings['map_style'] != 'satellite' && $settings['map_style'] != 'hybrid') $settings['map_style'] = 'terrain';
165
166 if($settings['map_style'] == 'terrain') $settings['map_style'] = 'G_PHYSICAL_MAP';
167 elseif($settings['map_style'] == 'roadmap') $settings['map_style'] = 'G_NORMAL_MAP';
168 elseif($settings['map_style'] == 'satellite') $settings['map_style'] = 'G_SATELLITE_MAP';
169 elseif($settings['map_style'] == 'hybrid') $settings['map_style'] = 'G_HYBRID_MAP';
170
171 echo '<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . $api_key . '" type="text/javascript"></script>';
172 echo '<div id="brightkite-map"><div id="brightkite-map-' . $user . '" style="width: ' . $settings['map_width']. 'px; height: ' . $settings['map_height']. 'px;"></div></div>';
173 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>';
174
175 }
176
177 echo '<p id="brightkite-loc"><a href="' . $data['link'] . '">' . $data['place'] . '</a></p>';
178
179 if(!strpos($data['description'], 'checked in @')) echo '<p id="brightkite-status">' . $data['description'] . '</p>';
180
181 } else {
182
183 echo("<p><strong>Brightkite Location Plugin</strong><br />Your feed could not be retrieved at this time. Try again shortly.");
184
185 }
186
187 }
188
189 add_action('wp_head', 'bkWordpressHeaders');
190
191 ?>