rendered paste body <?php
// $Id: alexa.module,v 1.0 2006/07/07 on #drupal : heine , xxx_xxx_ , brainless, merlinofchaos, Exp $
/**
* @file
* Grab the latest info from Alexa
*/
/********************************************************************
* Drupal Hooks
********************************************************************/
/**
* Implementation of hook_help().
*/
function alexa_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Grab the latest alexa data from www.alexa.com');
}
}
function alexa_perm() {
return array ('access alexa');
}
function alexa_link($type, $node = 0, $main = 0) {
global $user;
$links = array();
if ($type == 'page' && user_access('use alexa')) {
$links[] = l(t('alexa'), 'alexa');
}
return $links;
}
function alexa_menu($may_cache) {
if ($may_cache) {
$items[] = array(
'path' => 'alexa',
'title' => t('Alexa'),
'callback' => 'alexa_page',
'access' => user_access('access alexa'),
);
}
return $items;
}
function alexa_page() {
$form['button1'] = array(
'#type' => 'textfield',
'#title' => t('replace domains, and press -submit-'),
'#default_value' => 'www.thingstoday.com' ,
);
$form['button2'] = array(
'#type' => 'textfield',
'#default_value' => 'www.drupal.org' ,
);
$form['button3'] = array(
'#type' => 'textfield',
'#default_value' => 'www.brainless-website.com' ,
);
$form['button4'] = array(
'#type' => 'textfield',
'#default_value' => 'www.slashdot.org' ,
);
$form['button5'] = array(
'#type' => 'textfield',
'#default_value' => 'www.digg.com' ,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Get Alexa Charts')
);
$form['charttype']['chart_type'] = array(
'#type' => 'select',
'#title' => t('Display type of chart'),
'#default_value' => variable_get('chart_type','Reach'),
'#options' => array(
'Reach' => t('Reach'),
'Page Views' => t('Page Views'),
'Traffic Rank' => t('Traffic Rank'),
),
'#description' => t('This is the Type of Chart to be displayed.'),
);
$form['chartrange']['chart_range'] = array(
'#type' => 'select',
'#title' => t('Display Range of Chart'),
'#default_value' => variable_get('chart_range','3 month'),
'#options' => array(
'7 days' => t('7 days'),
'14 days' => t('14 days'),
'1 month' => t('1 month'),
'3 month' => t('3 month'),
'6 month' => t('6 month'),
'1 year' => t('1 year'),
'2 year' => t('2 year'),
'3 year' => t('3 year'),
'4 year' => t('4 year'),
'5 year' => t('5 year'),
),
'#description' => t('Time Frame of the page to be displayed.'),
);
return drupal_get_form('alexa_page',$form);
}
function alexa_page_submit($form_id, $form_values) {
$image = <<<STR
<img src="http://traffic.alexa.com/graph?w=450&h=285&r=3m&z=1&y=r
STR;
($form_values['button1'] != '')?($image.='&u='.$form_values['button1']):'';
($form_values['button2'] != '')?($image.='&u='.$form_values['button2']):'';
($form_values['button3'] != '')?($image.='&u='.$form_values['button3']):'';
($form_values['button4'] != '')?($image.='&u='.$form_values['button4']):'';
($form_values['button5'] != '')?($image.='&u='.$form_values['button5']):'';
$image .= <<<STR
" width="450" height="285" alt="Alexa">
STR;
if($image != '<img src="http://traffic.alexa.com/graph?w=379&h=216&r=3m&z=1&y=r" width="450" height="285" alt="Alexa">')
drupal_set_message($image);
return ('alexa');
}
/**
* Fetch the alexa info for a given ticker symbol from alexa!
*/
function alexa_fetch($form) {
if (empty($form)) {
return FALSE;
}
$host = 'http://traffic.alexa.com/graph?w=725&h=370&r=1m&y=r&a=1&z=1&u=';
$url = $host . urlencode($form['button1']) .'&u='.urlencode($form['button2']).'&u='.urlencode($form['button3']).'&u='.urlencode($form['button4']).'&u='.urlencode($form['button5']);
$contents = @file_get_contents($url);
if ($contents) {
if ($data = explode(',', str_replace('"', '', $contents))) {
$data = array_map('trim', $data);
return $data;
}
}
return FALSE;
}
function alexa_load($form_values) {
if (!empty($form_values)) {
$form_values = db_fetch_array(db_query("SELECT * FROM {alexa} WHERE form_values = '%s'", $form_values));
if (!$alexa) {
$alexa = alexa_fetch($form_values);
if ($alexa) {
if ($alexa[5] != 'N/A') {
// Date is 'N/A' means an invalid entry line
alexa_save($alexa);
// Load again so it can show for the first time
$alexa = alexa_load($form_values);
}
}
}
}
return $alexa;
}