| 1 |
<?php |
<?php |
| 2 |
|
|
| 3 |
define('TEXTLINKADS_VERSION', '$Id: textlinkads.module,v 1.4 2007/02/10 04:24:40 kbahey Exp $'); |
/** |
| 4 |
|
* Support multiple inventory keys per site |
| 5 |
|
* 1. Multiple blocks? (with a key per block) - let drupal block visibility system manage visibility? |
| 6 |
|
* |
| 7 |
|
* 2. Database changes - store inventory key? |
| 8 |
|
* 3. Cron changes |
| 9 |
|
* 4. Settings changes |
| 10 |
|
* Inventory key(s), path mach pattern? |
| 11 |
|
* Module will provide block per key |
| 12 |
|
*/ |
| 13 |
|
|
| 14 |
|
define('TEXTLINKADS_VERSION', '$Id: textlinkads.module,v 1.5 2007/02/10 04:32:46 kbahey Exp $'); |
| 15 |
define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads')); |
define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads')); |
| 16 |
|
|
| 17 |
function textlinkads_help($section) { |
function textlinkads_help($section) { |
| 39 |
return $output; |
return $output; |
| 40 |
|
|
| 41 |
case 'admin/settings/textlinkads': |
case 'admin/settings/textlinkads': |
| 42 |
return '<p>'. t('You are using version %version of the Text-Link-Ads module.', array('%version' => TEXTLINKADS_VERSION)). '</p><p>'. t('For complete instructions on configuring this module, refer to the %help_pages.', array('%help_pages' => l(t('Help pages'), 'admin/help/textlinkads'))). '</p>'; |
return '<p>'. t('You are using version %version of the Text-Link-Ads module.', array('%version' => TEXTLINKADS_VERSION)). '</p><p>'. t('For complete instructions on configuring this module, refer to the !help_pages.', array('!help_pages' => l(t('Help pages'), 'admin/help/textlinkads'))). '</p>'; |
| 43 |
} |
} |
| 44 |
} |
} |
| 45 |
|
|
| 85 |
return $items; |
return $items; |
| 86 |
} |
} |
| 87 |
|
|
| 88 |
|
function textlinkads_get_keys() { |
| 89 |
|
$keys = explode(',', variable_get('textlinkads_website_xml_key', '')); |
| 90 |
|
return $keys; |
| 91 |
|
} |
| 92 |
|
|
| 93 |
function textlinkads_block($op = 'list', $delta = 0, $edit = array()) { |
function textlinkads_block($op = 'list', $delta = 0, $edit = array()) { |
| 94 |
|
$keys = textlinkads_get_keys(); |
| 95 |
|
$nblocks = count($keys); |
| 96 |
switch ($op) { |
switch ($op) { |
| 97 |
case 'list': |
case 'list': |
| 98 |
$blocks[0]['info'] = t('text link ads advertisments'); |
for ($i = 0; $i < $nblocks; $i++) { |
| 99 |
|
$blocks[$i]['info'] = t('TLA Block for key= !key', array('!key'=>$keys[$i])); |
| 100 |
|
} |
| 101 |
return $blocks; |
return $blocks; |
| 102 |
case 'view': |
case 'view': |
| 103 |
switch ($delta) { |
$block['subject'] = t(variable_get('textlinkads_ad_block_title', t('Advertisements'))); |
| 104 |
case 0: |
$block['content'] = textlinkads_content($keys[$delta]); |
|
$block['subject'] = t(variable_get('textlinkads_ad_block_title', 'Advertisments')); |
|
|
$block['content'] = textlinkads_content(); |
|
|
break; |
|
|
} |
|
| 105 |
return $block; |
return $block; |
| 106 |
} |
} |
| 107 |
} |
} |
| 108 |
|
|
| 109 |
function textlinkads_content() { |
function textlinkads_content($key) { |
| 110 |
$output = theme('textlinkads_ads', textlinkads_get_links('text')); |
$output = theme('textlinkads_ads', textlinkads_get_links('text', $key), $key); |
| 111 |
return $output; |
return $output; |
| 112 |
} |
} |
| 113 |
|
|
| 118 |
|
|
| 119 |
$form['textlinkads_website_xml_key'] = array( |
$form['textlinkads_website_xml_key'] = array( |
| 120 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 121 |
'#title' => t('Website XML Key'), |
'#title' => t('Website XML Keys'), |
| 122 |
'#default_value' => variable_get('textlinkads_website_xml_key', ''), |
'#default_value' => variable_get('textlinkads_website_xml_key', ''), |
| 123 |
'#description' => t('You get your XML Site Key from the <a href="http://www.text-link-ads.com/my_account.php?view=my_sites">Get Ad Code page</a> on Text-Link-Ads.com. Each of your sites has its own code that looks something like this: <code>A4WWSAAOULU1XGHWU78G</code>.'), |
'#description' => t('A comma-separated list of keys (no spaces after commas). You get your XML Site Key from the <a href="http://www.text-link-ads.com/my_account.php?view=my_sites">Get Ad Code page</a> on Text-Link-Ads.com. Each of your site or page within a site has its own code depending on your configuration. It should look something like this: <code>A4WWSAAOULU1XGHWU78G,A4WWSAAOULU1XGHWU78G</code>.'), |
| 124 |
); |
); |
| 125 |
$form['textlinkads_ad_block_title'] = array( |
$form['textlinkads_ad_block_title'] = array( |
| 126 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 211 |
} |
} |
| 212 |
|
|
| 213 |
function textlinkads_stats_page() { |
function textlinkads_stats_page() { |
| 214 |
if (!$key = variable_get('textlinkads_website_xml_key', '')) { |
// @todo: fix this to work with multi-keys |
| 215 |
|
$keys = textlinkads_get_keys(); |
| 216 |
|
$key = $keys[0]; // first key is RSS key - sucks, needs to be smarter |
| 217 |
|
|
| 218 |
|
if (!$key) { |
| 219 |
drupal_set_message(t('You need to enter your Customer ID on the <a href="%admin">administration page</a> before you can view your statistics here.', array('%admin' => url('admin/settings/textlinkads'))), 'error'); |
drupal_set_message(t('You need to enter your Customer ID on the <a href="%admin">administration page</a> before you can view your statistics here.', array('%admin' => url('admin/settings/textlinkads'))), 'error'); |
| 220 |
drupal_goto('admin/settings/textlinkads'); |
drupal_goto('admin/settings/textlinkads'); |
| 221 |
} |
} |
| 286 |
} |
} |
| 287 |
|
|
| 288 |
|
|
| 289 |
function theme_textlinkads_ads($links) { |
function theme_textlinkads_ads($links, $key) { |
| 290 |
$output = ''; |
$output = ''; |
| 291 |
|
|
| 292 |
$font_size = variable_get('textlinkads_font', 12); |
$font_size = variable_get('textlinkads_font', 12); |
| 366 |
*/ |
*/ |
| 367 |
function textlinkads_check_update() { |
function textlinkads_check_update() { |
| 368 |
$CONNECTION_TIMEOUT = 10; |
$CONNECTION_TIMEOUT = 10; |
| 369 |
$url = 'http://www.text-link-ads.com/xml.php?inventory_key=' . check_plain(variable_get(textlinkads_website_xml_key, '')) |
// enumerate inventory keylist - pass key to textlinkads_update_links function |
| 370 |
|
$keys = textlinkads_get_keys(); |
| 371 |
|
$nkeys = count($keys); |
| 372 |
|
|
| 373 |
|
$prevcount = db_result(db_query("SELECT COUNT(*) FROM {textlinkads}")); |
| 374 |
|
db_query("DELETE FROM {textlinkads}"); |
| 375 |
|
$count = 0; |
| 376 |
|
for ($i=0; $i<$nkeys; $i++) { |
| 377 |
|
$key = $keys[$i]; |
| 378 |
|
$url = 'http://www.text-link-ads.com/xml.php?inventory_key=' . check_plain($key) |
| 379 |
. '&referer=' . urlencode($_SERVER['REQUEST_URI']) |
. '&referer=' . urlencode($_SERVER['REQUEST_URI']) |
| 380 |
. '&user_agent=' . urlencode($_SERVER['HTTP_USER_AGENT']) |
. '&user_agent=' . urlencode($_SERVER['HTTP_USER_AGENT']) |
| 381 |
. '&drupal_module_version=' . urlencode(TEXTLINKADS_VERSION); |
. '&drupal_module_version=' . urlencode(TEXTLINKADS_VERSION); |
| 382 |
textlinkads_update_links($url, $CONNECTION_TIMEOUT); |
$count += textlinkads_update_links($url, $CONNECTION_TIMEOUT, $key); |
| 383 |
|
} |
| 384 |
|
// report changes as appropriate |
| 385 |
|
if ($count != $prevcount) { |
| 386 |
|
watchdog('TextLinkAds', "update_links: Inventory change (was: $prevcount now: $count)" ); |
| 387 |
|
} |
| 388 |
|
variable_set('textlinkads_last_update', time()); |
| 389 |
} |
} |
| 390 |
|
|
| 391 |
function textlinkads_update_links($url, $time_out) { |
function textlinkads_update_links($url, $time_out, $inventory_key) { |
| 392 |
$xml = textlinkads_file_get_contents($url, $time_out); |
$xml = textlinkads_file_get_contents($url, $time_out); |
| 393 |
$xml = substr($xml, strpos($xml, '<?')); |
$xml = substr($xml, strpos($xml, '<?')); |
|
|
|
|
db_query("DELETE FROM {textlinkads}"); |
|
| 394 |
$parser = new xml2array(); |
$parser = new xml2array(); |
| 395 |
|
|
| 396 |
if ($data = $parser->parseXMLintoarray($xml)) { |
if ($data = $parser->parseXMLintoarray($xml)) { |
| 397 |
// there are no links or just one link |
// there are no links or just one link |
| 398 |
if (is_array($data['LINKS']) && $data['LINKS']['LINK']['URL']) { |
if (is_array($data['LINKS']) && $data['LINKS']['LINK']['URL']) { |
| 412 |
} |
} |
| 413 |
} |
} |
| 414 |
foreach ($links as $key => $values) { |
foreach ($links as $key => $values) { |
| 415 |
db_query("INSERT INTO {textlinkads} (url, text, beforetext, aftertext, rsstext, rssbeforetext, rssaftertext) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')", $values['URL'], $values['TEXT'], $values['BEFORETEXT'], $values['AFTERTEXT'], $values['RSSTEXT'], $values['RSSBEFORETEXT'], $values['RSSAFTERTEXT']); |
db_query("INSERT INTO {textlinkads} (url, text, beforetext, aftertext, rsstext, rssbeforetext, rssaftertext, inventory_key) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $values['URL'], $values['TEXT'], $values['BEFORETEXT'], $values['AFTERTEXT'], $values['RSSTEXT'], $values['RSSBEFORETEXT'], $values['RSSAFTERTEXT'], $inventory_key); |
| 416 |
} |
} |
| 417 |
} |
} |
| 418 |
$count=count($links); |
return count($links); |
|
watchdog('TextLinkAds', "update_links: $count ads found" ); |
|
|
variable_set('textlinkads_last_update', time()); |
|
| 419 |
} |
} |
| 420 |
|
|
| 421 |
/** |
/** |
| 426 |
static $links = NULL; |
static $links = NULL; |
| 427 |
|
|
| 428 |
if (!is_array($links)) { |
if (!is_array($links)) { |
| 429 |
$links = textlinkads_get_links('rss'); |
$keys = textlinkads_get_keys(); |
| 430 |
|
$key = $keys[0]; // rss is first key |
| 431 |
|
$links = textlinkads_get_links('rss', $key); |
| 432 |
shuffle($links); |
shuffle($links); |
| 433 |
} |
} |
| 434 |
|
|
| 440 |
* |
* |
| 441 |
* @param string $type |
* @param string $type |
| 442 |
* 'text' or 'rss' |
* 'text' or 'rss' |
| 443 |
|
* @param $key The inventory key if not null |
| 444 |
* @return Array |
* @return Array |
| 445 |
*/ |
*/ |
| 446 |
function textlinkads_get_links($type = 'text') { |
function textlinkads_get_links($type = 'text', $key=NULL) { |
| 447 |
$links = array(); |
$links = array(); |
| 448 |
$limit = ($type == 'text') ? variable_get('textlinkads_total', 4) : variable_get('feed_default_items', 10); |
$limit = ($type == 'text') ? variable_get('textlinkads_total', 4) : variable_get('feed_default_items', 10); |
| 449 |
$where = ($type == 'text') ? 'text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL' : 'rsstext IS NOT NULL'; |
$where = ($type == 'text') ? 'text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL' : 'rsstext IS NOT NULL'; |
| 450 |
|
if ($key) { |
| 451 |
|
$where .= " AND inventory_key='$key'"; |
| 452 |
|
} |
| 453 |
$result = db_query("SELECT * FROM {textlinkads} WHERE $where LIMIT %d", $limit); |
$result = db_query("SELECT * FROM {textlinkads} WHERE $where LIMIT %d", $limit); |
| 454 |
while ($row = db_fetch_array($result)) { |
while ($row = db_fetch_array($result)) { |
| 455 |
$links[] = $row; |
$links[] = $row; |