source: trunk/www.guidonia.net/wp/wp-content/plugins/alexarank/alexarank.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 11.0 KB
Line 
1<?php
2/*
3Plugin Name: AlexaRank
4Plugin URI: http://www.fliptel.de/wordpress-plugins
5Description: Displays the Alexa traffic ranking in the sidebar of your blog via widget or anywhere else. <a href="options-general.php?page=alexarank/alexarank.php">Configure here</a>. Check out more <a href="http://www.fliptel.de/wordpress-plugins">Wordpress Plugins</a> by <a href="http://www.fliptel.de">Fliptel</a>.
6Version: 0.2
7Author: fliptel
8Author URI: http://www.fliptel.de
9*/
10
11/**
12 * v0.2 13.07.2009 fixed css, some templates got brocken thx to sullivan
13 * v0.1 09.07.2009 initial release
14 */
15
16if(!class_exists('AlexaRank')):
17class AlexaRank {
18 var $id;
19 var $version;
20 var $title;
21 var $name;
22 var $options;
23 var $path;
24 var $cache_file;
25 var $locale;
26 var $url;
27 var $layouts;
28
29 function AlexaRank() {
30 $this->id = 'alexarank';
31 $this->title = 'AlexaRank';
32 $this->version = '0.2';
33 $this->name = $this->title. ' v'. $this->version;
34 $this->path = dirname(__FILE__);
35 $this->url = get_bloginfo('wpurl'). '/wp-content/plugins/' . $this->id;
36
37 $this->layouts = array(
38 array(
39 'icon' => '0.gif',
40 'width' => 80,
41 'height' => 30,
42 'text' => array(
43 'width' => 20,
44 'x' => 30,
45 'y' => 20,
46 'font' => 1,
47 'color' => '808080'
48 ),
49 'image' => array(
50 'x' => 31,
51 'y' => 15,
52 'w' => 40,
53 'h' => 3,
54 'color' => '1b13b7'
55 )
56 ),
57 array(
58 'icon' => '1.gif',
59 'width' => 80,
60 'height' => 15,
61 'text' => null,
62 'image' => array(
63 'x' => 35,
64 'y' => 6,
65 'w' => 40,
66 'h' => 3,
67 'color' => '1b13b7'
68 )
69 ),
70 array(
71 'icon' => '2.gif',
72 'width' => 80,
73 'height' => 15,
74 'text' => array(
75 'width' => 32,
76 'x' => 15,
77 'y' => 1,
78 'font' => 2,
79 'color' => '808080'
80 ),
81 'image' => null
82 ),
83 array(
84 'icon' => '3.gif',
85 'width' => 80,
86 'height' => 15,
87 'text' => array(
88 'width' => 32,
89 'x' => 18,
90 'y' => 1,
91 'font' => 2,
92 'color' => '808080'
93 ),
94 'image' => null
95 ),
96 array(
97 'icon' => '4.gif',
98 'width' => 80,
99 'height' => 15,
100 'text' => null,
101 'image' => array(
102 'x' => 28,
103 'y' => 11,
104 'w' => 40,
105 'h' => 1,
106 'color' => '1b13b7'
107 )
108 )
109 );
110
111 $this->locale = get_locale();
112
113 if(empty($this->locale)) {
114 $this->locale = 'en_US';
115 }
116
117 load_textdomain($this->id, sprintf('%s/%s.mo', $this->path, $this->locale));
118
119 $this->loadOptions();
120
121 $this->cache_file = $this->path. '/cache/layout'. $this->options['layout']. '.gif';
122
123 if(!@isset($_GET['image'])) {
124 if(is_admin()) {
125 add_action('admin_menu', array(&$this, 'optionMenu'));
126 }
127 else {
128 add_action('wp_head', array(&$this, 'blogHead'));
129 }
130
131 add_action('widgets_init', array(&$this, 'initWidgets'));
132 }
133 }
134
135 function optionMenu() {
136 add_options_page($this->title, $this->title, 8, __FILE__, array(&$this, 'optionMenuPage'));
137 }
138
139 function optionMenuPage() {
140
141 if(@$_REQUEST[$this->id]) {
142 @unlink($this->cache_file);
143
144 $this->updateOptions($_REQUEST[$this->id]);
145
146 echo '<div id="message" class="updated fade"><p><strong>' . __('Settings saved!', $this->id) . '</strong></p></div>';
147 }
148?>
149<div class="wrap">
150
151<h2><?php _e('Settings', $this->id); ?></h2>
152<form method="post" action="">
153<table class="form-table">
154<?php if(!file_exists($this->path. '/cache/') || !is_writeable($this->path. '/cache/')): ?>
155<tr valign="top"><th scope="row" colspan="3"><span style="color:red;"><?php _e('Warning! The cachedirectory is missing or not writeable!', $this->id); ?></span><br /><em><?php echo $this->path; ?>/cache</em></th></tr>
156<?php endif; ?>
157</tr>
158<tr valign="top">
159 <th scope="row"><?php _e('Title', $this->id); ?></th>
160 <td><input name="<?=$this->id?>[title]" type="text" class="code" value="<?=$this->options['title']?>" /><br /><?php _e('The title is displayed above the badge in widget mode only!', $this->id); ?></td></tr>
161 <th scope="row"><?php _e('Layout', $this->id); ?></th>
162 <td colspan="3">
163 <input name="<?=$this->id?>[layout]" type="radio" class="code" value="0"<?php echo intval($this->options['layout']) == 0 ? ' checked="checked"' : ''; ?> />
164 <img src="<?=$this->url?>/screenshot-1.gif" style="vertical-align:middle;" /><br /><br />
165 <input name="<?=$this->id?>[layout]" type="radio" class="code" value="1"<?php echo intval($this->options['layout']) == 1 ? ' checked="checked"' : ''; ?> />
166 <img src="<?=$this->url?>/screenshot-2.gif" style="vertical-align:middle;" /><br /><br />
167 <input name="<?=$this->id?>[layout]" type="radio" class="code" value="2"<?php echo intval($this->options['layout']) == 2 ? ' checked="checked"' : ''; ?> />
168 <img src="<?=$this->url?>/screenshot-3.gif" style="vertical-align:middle;" /><br /><br />
169 <input name="<?=$this->id?>[layout]" type="radio" class="code" value="3"<?php echo intval($this->options['layout']) == 3 ? ' checked="checked"' : ''; ?> />
170 <img src="<?=$this->url?>/screenshot-4.gif" style="vertical-align:middle;" /><br /><br />
171 <input name="<?=$this->id?>[layout]" type="radio" class="code" value="4"<?php echo intval($this->options['layout']) == 4 ? ' checked="checked"' : ''; ?> />
172 <img src="<?=$this->url?>/screenshot-5.gif" style="vertical-align:middle;" />
173 </td>
174</tr>
175
176</table>
177
178<p class="submit">
179 <input type="submit" value="<?php _e('save', $this->id); ?>" name="submit" />
180</p>
181
182</form>
183
184</div>
185<?php
186}
187 function loadOptions() {
188 if(!($this->options = get_option($this->id))) {
189 $this->options = array(
190 'title' => 'AlexaRank',
191 'layout' => 0
192 );
193
194 add_option($this->id, $this->options, $this->name, 'yes');
195 }
196 }
197
198 function updateOption($name, $value) {
199 $this->updateOptions(array($name => $value));
200 }
201
202 function updateOptions($options) {
203 foreach($this->options as $k => $v) {
204 if(array_key_exists($k, $options)) {
205 $this->options[$k] = $options[ $k ];
206 }
207 }
208
209 update_option($this->id, $this->options);
210 }
211
212 function blogHead() {
213 printf('<meta name="%s" content="%s/%s" />' . "\n", $this->id, $this->id, $this->version);
214 print( '<style type="text/css">
215#alexarank, #alexarank small {padding: 0;margin: 0;color: #aaa;font-family: Arial, sans-serif;font-size: 10px;font-style: normal;font-weight: normal;letter-spacing: 0px;text-transform: none; width: 80px;text-align:center;border:0;}
216#alexarank small a:hover, #alexarank small a:link, #alexarank small a:visited, #alexarank small a:active {color: #aaa;text-decoration:none;cursor: pointer;text-transform: none;font-size:10px;border:0;display:inline;}
217</style>');
218 }
219
220 function httpGet($url) {
221
222 if(!class_exists('Snoopy')) {
223 include_once(ABSPATH. WPINC. '/class-snoopy.php');
224 }
225
226 $Snoopy = new Snoopy();
227
228 if(@$Snoopy->fetch($url)) {
229
230 if(!empty( $Snoopy->results)) {
231 return $Snoopy->results;
232 }
233 }
234
235 return false;
236 }
237
238 function normalizeValue($value) {
239 if($value < 0) {
240 return 0;
241 }
242
243 $rank = intval(floor(log($value) / log(5)));
244
245 return $rank > 10 ? 10 : 10 - $rank;
246 }
247
248 function getAlexaRank() {
249 $url = get_bloginfo('wpurl');
250
251 $url = sprintf('http://data.alexa.com/data?cli=10&dat=snbamz&url=%s', urlencode($url));
252
253 if(($data = $this->httpGet($url)) !== false) {
254
255 preg_match('|POPULARITY URL="(.*?)" TEXT="([0-9]+)"|', $data, $matches);
256
257 if(count($matches) == 3 && !empty($matches[ 2 ])) {
258 return intval($matches[ 2 ]);
259 }
260 }
261
262 return 0;
263 }
264
265 function rgbColor(&$img, $rgb) {
266 if( $rgb[ 0 ] == '#' ) {
267 $rgb = substr( $rgb, 1 );
268 }
269
270 $a = substr($rgb, 0, 2);
271 $b = substr($rgb, 2, 2);
272 $c = substr($rgb, 4, 2);
273
274 return imagecolorallocate($img, hexdec($a), hexdec($b), hexdec($c));
275 }
276
277
278 function getCode() {
279 return sprintf( '<div id="%s"><a class="snap_noshots" href="http://www.fliptel.de/wordpress-plugins#%s" target="_blank"><img src="%s/%s/%s/%s.php?image=1" border="0" alt="%s" title="%s" /></a><br /><small><a href="http://www.fliptel.de/wordpress-plugins" class="snap_noshots" target="_blank">Plugin</a> by <a href="http://www.fliptel.de" class="snap_noshots" target="_blank">Fliptel</a></small></div>', $this->id, $this->id, get_bloginfo('wpurl'), PLUGINDIR, $this->id, $this->id, $this->title, $this->title);
280 }
281
282 function draw() {
283 clearstatcache();
284
285 $create = false;
286
287 if(!file_exists($this->cache_file)) {
288 $create = true;
289 }
290 elseif(time() - filemtime($this->cache_file) > (3600 * 3)) {
291 $create = true;
292 }
293
294 if($create) {
295
296 $numeric = $this->getAlexaRank();
297
298 $layout = $this->layouts[intval($this->options['layout'])];
299
300 $img = @imagecreatefromgif($this->path. '/img/'. $this->options['layout']. '.gif');
301
302 if(!is_null($layout['text'])) {
303
304 $x = intval(round($layout['text']['width'] - ((imagefontwidth($layout['text']['font'])*strlen($numeric==0?'none':number_format($numeric,0,'.','.')))/2) + $layout['text']['x']));
305 $color1 = $this->rgbColor($img, $layout['text']['color']);
306 imagestring($img, $layout['text']['font'], $x, $layout['text']['y'], $numeric == 0 ? 'none' : number_format($numeric,0,'.','.'), $color1);
307 }
308
309 if(!is_null($layout['image']) && $numeric > 0) {
310 $color2 = $this->rgbColor($img, $layout['image']['color']);
311 imagefilledrectangle($img, $layout['image']['x'], $layout['image']['y'], $layout['image']['x'] + ($this->normalizeValue($numeric)*4)-1, $layout['image']['y'] + $layout['image']['h']-1, $color2);
312 }
313
314 if(is_writeable($this->path. '/cache')) {
315 imagegif($img, $this->cache_file);
316 }
317 }
318 else {
319 $img = @imagecreatefromgif($this->cache_file);
320 }
321
322 header( 'Content-Type: image/gif' );
323
324 @imagegif($img);
325 }
326
327 function initWidgets() {
328 if(function_exists('register_sidebar_widget')) {
329 register_sidebar_widget($this->title. ' Widget', array(&$this, 'Widget'), null, 'widget_'. $this->id);
330 }
331 }
332
333 function widget($args) {
334 extract($args);
335
336 printf('%s%s%s%s%s%s', $before_widget, $before_title, $this->options['title'], $after_title, $this->getCode(), $after_widget);
337 }
338}
339
340function alexarank_display() {
341 global $AlexaRank;
342
343 if(!isset($AlexaRank)) {
344 $AlexaRank = new AlexaRank();
345 }
346
347 if($AlexaRank) {
348 echo $AlexaRank->getCode();
349 }
350}
351endif;
352
353if(@isset($_GET['image'])) {
354 include_once(dirname(__FILE__). '/../../../wp-config.php');
355
356 if(!isset($AlexaRank)) {
357 $AlexaRank = new AlexaRank();
358 }
359
360 $AlexaRank->draw();
361}
362else {
363 add_action('plugins_loaded', create_function('$AlexaRank_s92231c', 'global $AlexaRank; $AlexaRank = new AlexaRank();'));
364}
365
366?>
Note: See TracBrowser for help on using the repository browser.