Plugin for TextLinkAds

Creating and modifying plugins.
Post Reply
Hathor
Regular
Posts: 118
Joined: Tue Mar 22, 2005 11:48 pm

Plugin for TextLinkAds

Post by Hathor »

TextLinkAds.com uses php, perl or several other server-side formats to put ads on your site. It can even generate a WordPress plugin. It does not generate ads in the javascript or HTML format, which is why I can't use any of the existing plugins to implement it. Nor am I having any luck figuring out how to stick straight php code into the template.

Since my PHP skills are very limited, I was wondering how difficult it would be to make a plugin that would work with this service, so I can put it on my blog.

Ideally, what I'd like is a php version of the HTML Head Nugget plugin, which allows you to put something at the top or bottom of the page or each entry. Is this something that someone could/would do? I would help in any way I can.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Plugin for TextLinkAds

Post by garvinhicking »

Hi!

You could create a serendipity plugin for that quite easily.

I can create it for you, but on their site I found no PHP code snippet anywhere. Please point me at it :)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hathor
Regular
Posts: 118
Joined: Tue Mar 22, 2005 11:48 pm

Post by Hathor »

Sure! Below is the php it generates for me to insert somewhere in my template (or a plugin). It also instructs me to create a local xml file, which is what's being referred to in that "Local file to store XML" section. I've already done that. I blocked out the actual local file name it gave me just in case that's something a hacker could use. Where you see " $LOCAL_XML_FILENAME = "local_xxxxxx.xml";" it would have numbers in place of the xxxxxx.

Hope that's helpful!
<?php

function tla_ads() {

// Number of seconds before connection to XML times out
// (This can be left the way it is)
$CONNECTION_TIMEOUT = 10;

// Local file to store XML
// This file MUST be writable by web server
// You should create a blank file and CHMOD it to 666
$LOCAL_XML_FILENAME = "local_xxxxxx.xml";

if( !file_exists($LOCAL_XML_FILENAME) ) die("Text Link Ads script error: $LOCAL_XML_FILENAME does not exist. Please create a blank file named $LOCAL_XML_FILENAME.");
if( !is_writable($LOCAL_XML_FILENAME) ) die("Text Link Ads script error: $LOCAL_XML_FILENAME is not writable. Please set write permissions on $LOCAL_XML_FILENAME.");

if( filemtime($LOCAL_XML_FILENAME) < (time() - 3600) || filesize($LOCAL_XML_FILENAME) < 20) {
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : "";
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
tla_updateLocalXML("http://www.text-link-ads.com/xml.php?in ... M&referer=" . urlencode($request_uri) . "&user_agent=" . urlencode($user_agent), $LOCAL_XML_FILENAME, $CONNECTION_TIMEOUT);
}

$xml = tla_getLocalXML($LOCAL_XML_FILENAME);

$arr_xml = tla_decodeXML($xml);

if ( is_array($arr_xml) ) {
echo "
<style type=\"text/css\">
ul#links52795 { width: 100%; list-style: none; overflow: hidden; margin: 0px; padding: 0px; border: 1px solid #000000; border-spacing: 0px; background-color: #F0F0F0; }
ul#links52795 li { display: inline; float: left; clear: none; width: 100%; padding: 0px; margin: 0px; }
ul#links52795 li span { display: block; width: 100%; padding: 3px; margin: 0px; font-size: 12px; color: #000000; }
ul#links52795 li span a { font-size: 12px; color: #0000FF; }
</style>
";
echo "\n<ul id=\"links52795\">\n";
for ($i = 0; $i < count($arr_xml['URL']); $i++) {
echo "<li><span>".$arr_xml['BeforeText'][$i]." <a href=\"".$arr_xml['URL'][$i]."\">".$arr_xml['Text'][$i]."</a> ".$arr_xml['AfterText'][$i]."</span></li>\n";
}
echo "</ul>";
}

}

function tla_updateLocalXML($url, $file, $time_out)
{
if($handle = fopen($file, "a")){
fwrite($handle, "\n");
fclose($handle);
}
if($xml = file_get_contents_tla($url, $time_out)) {
$xml = substr($xml, strpos($xml,'<?'));

if ($handle = fopen($file, "w")) {
fwrite($handle, $xml);
fclose($handle);
}
}
}

function tla_getLocalXML($file)
{
$contents = "";
if($handle = fopen($file, "r")){
$contents = fread($handle, filesize($file)+1);
fclose($handle);
}
return $contents;
}

function file_get_contents_tla($url, $time_out)
{
$result = "";
$url = parse_url($url);

if ($handle = @fsockopen ($url["host"], 80)) {
if(function_exists("socket_set_timeout")) {
socket_set_timeout($handle,$time_out,0);
} else if(function_exists("stream_set_timeout")) {
stream_set_timeout($handle,$time_out,0);
}

fwrite ($handle, "GET $url[path]?$url[query] HTTP/1.0\r\nHost: $url[host]\r\nConnection: Close\r\n\r\n");
while (!feof($handle)) {
$result .= @fread($handle, 40960);
}
fclose($handle);
}

return $result;
}

function tla_decodeXML($xmlstg)
{

if( !function_exists('html_entity_decode') ){
function html_entity_decode($string)
{
// replace numeric entities
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr(\1)', $string);
// replace literal entities
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}
}

$out = "";
$retarr = "";

preg_match_all ("/<(.*?)>(.*?)</", $xmlstg, $out, PREG_SET_ORDER);
$search_ar = array('<', '>', '"');
$replace_ar = array('<', '>', '"');
$n = 0;
while (isset($out[$n]))
{
$retarr[$out[$n][1]][] = str_replace($search_ar, $replace_ar,html_entity_decode(strip_tags($out[$n][0])));
$n++;
}
return $retarr;
}

tla_ads();

?>
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Thanks for that code. Here, as promised your plugin:

http://nopaste.php-q.net/220621

Also committed to Spartacus. It would be nice if you could tell the textlinkads people about this plugin?

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hathor
Regular
Posts: 118
Joined: Tue Mar 22, 2005 11:48 pm

Post by Hathor »

Hey, Gavin,

I'm having trouble installing. I couldn't find it in Spartacus, so I created a directory called "serendipity_event_multilingual" (since that's the name given on the first line) and put it inside that directory. But it's not showing in my plugin panel. What am I doing wrong? :)
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

That's why I gave you the link, because it's not yet available in spartacus ;)

You actually created the wrong directory. The plugin is called serendipity_event_textlinkads -- the first line is just a PHP comment and doesn't indicate the filename.

Sorry for that confusion!

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Hathor
Regular
Posts: 118
Joined: Tue Mar 22, 2005 11:48 pm

Post by Hathor »

Yay, I got it working!

I'm going to blog about this for tomorrow's entry, and go write TLA about it today. Thanks a lot!
patrickgavin
Posts: 1
Joined: Thu Jun 22, 2006 6:59 pm

Thanks From Text Link Ads!

Post by patrickgavin »

Garvin, thanks for putting this together! We will be adding this to our site real soon and we will then we will also post about it on our blog. Thanks again! Patrick Gavin, President, Text Link Ads
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Thanks From Text Link Ads!

Post by garvinhicking »

Hi!

Hey, that's great - thank you for telling us!

Have fun,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Brian1969
Regular
Posts: 78
Joined: Tue Mar 22, 2005 7:03 pm
Contact:

Post by Brian1969 »

Question about this.

I just installed and it works fine. My problem is, I want it in one of the sidebars.

Where would I place the code on my index.tpl file to put it in one of the sidebars?
Brian
tuler
Regular
Posts: 19
Joined: Wed Aug 16, 2006 2:05 am

Post by tuler »

Brian1969 wrote:Question about this.

I just installed and it works fine. My problem is, I want it in one of the sidebars.

Where would I place the code on my index.tpl file to put it in one of the sidebars?
I would also like to do this. Any tips on how?
Post Reply