Cookie Path Plugin For WordPress 2.0 (root Cookie)

UPDATE: This Plugin has been updated, the below links to the new file, but please comment on the new post, thanks.

I’ve been busy this AM, my site is a little bit integrated with WordPress, basically I call the header and footer from WP so that my custom code changes themes when WordPress does :)

I want to take it further and use the authentication cookie, so that my code recognizes me when I log in (in the same way WordPress does). In order for linickx.com to see the cookie set by linickx.com/blog, I need to change the COOKIEPATH. The change is actually quite simple, in fact the “set cookie” function is a pluggable one :D

The function / plugin I’ve written is really simple, all I’ve done is change the PATH to be / (root) that way linickx.com or linickx.com/blah can reference the cookie, and because it’s set to / the plugin can be used on any site !

To install:

  • Save root-cookie.txt in your plugins directory as root-cookie.php (Right Click – Save As)
  • From the Plugin Manager in the Dashboard, “Activate” the plugin
  • Log out, and log in again
  • Done

Now your cookie can be referenced from any custom code you may have written outside of your WordPress install directory !

h4x0r (Hacker) / The Matrix Theme for WordPress

My h4x0r Theme

I thought I’d try something different ;)

I’ve been playing with wordpress for nearly a year now, and thought it’d be cool to make a publicly available theme, I wasn’t sure on what to do so I thought I’d start with a simple pallet. My theme is a black on green code looking theme, you know the hacker site style; to make it more interesting I added some matrix references, and silly bits of php code.

The theme has been tested on wp 1.5 & 2.01 (including a blank one as requested on the codex) , there is a screenshot and I’ve also installed a theme switcher so you can demo it :D

h4x0r is available for download here, please let me know your thoughts.

Multiple Loops in WordPress

I wanted to run the wordpress loop on my php page more than once, & I don’t know if I’m the only person, but I found the offical multiple look example hard to swallow.

Google found is nima’s how to which was useful, but I wanted to take it further; the problem was I wanted a function.

This is what I came up with:

<?php
require(‘path_to_wordpress/wp-blog-header.php’);
?>

<?php
function show_posts_from_cat($MYCAT){

?>

<ul>
<?php if (have_posts()) : ?>

<?php $my_query = new WP_Query(“category_name=$MYCAT”); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>

<span class=”my-post-title”>
<span id=”post-<?php the_ID(); ?>”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></a></span>
<!–
<?php trackback_rdf(); ?>
–>
</span>
<small><?php the_time(‘jS F Y’) ?> <!– by <?php the_author() ?> –></small>
</li>

<?php endwhile; ?>

<?php else : ?>

<h2 class=”center”>Not Found</h2>
<p class=”center”><?php _e(“looks like there aren’t any related posts.”); ?></p>

<?php endif; ?>

</ul>

<?php
}
?>

So here’s what I found, if you just run the wordpress loop twice, the second time you run it, it’ll fail :s so my function creates a new object each time you run the loop. The object is then used to display the results, the benefit being ,each time you call the function it over-writes the object with the new results, allowing you to call the function as many times as you like, and generating fresh results every time :D

function usage: show_posts_from_cat(“Category_name”);

Good luck, let me know if you have ant problems.

Google Adsense code (javascript) in wordpress pages / posts

For ages I could not work out why google ad’s included within a wordpress page didn’t work (in fact this applies to a post too). When google support pointed out that there was <br/> ‘s in my code the penny finally dropped.

So here’s the deal, I pasted the following code into my page: (In the wordpress dashboard)

firefox ad
<!– FireFox Referral –>
<script type=”text/javascript”>
<!–
google_ad_client = “pub-6732086233580907″;
google_ad_width = 180;google_ad_height = 60;
google_ad_format = “180x60_as_rimg”;
google_cpa_choice = “CAAQyaj8zwEaCIwcWMzeycafKMu293M”;
//–>
</script>
<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”></script>

adsense ad
<!– Adsense Referral–>
<script type=”text/javascript”>
<!–
google_ad_client = “pub-6732086233580907″;
google_ad_width = 180;
google_ad_height = 60;
google_ad_format = “180x60_as_rimg”;
google_cpa_choice = “CAAQ__qy0gEaCP_s0gMhUOtXKL3D93M”;
//–>
</script>
<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”></script>

and look what happens…..
firefox ad


adsense ad


WordPress adds page formatting to the java script that is posted, so if you look at the source, what you actually get is…

<!– FireFox Referal –><br />
<script type=”text/javascript”><br />
<!– <br />
google_ad_client = “pub-6732086233580907″;<br />
google_ad_width = 180;google_ad_height = 60;<br />
google_ad_format = “180x60_as_rimg”;<br />
google_cpa_choice = “CAAQyaj8zwEaCIwcWMzeycafKMu293M”; <br />
//–><br />
</script><br />
<script type=”text/javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”></script><br />

… DOH !!!!!!

Implementing Adsides in wordpress

I’ve decided to take a tip from the pro’s and separate my useful content from waffle. The blogging term appears to be asides , and photomatt has simple post on how to do it. Basically the idea is, that asides content doesn’t take up as much space on your blog, so readers can easily see reference or important material, and if they’ve got some spare time read your thoughts.

Implementation is quite straight forward, assign a category for these less important posts – for me that’s blog - and then on you theme index.php add the something like the following code:

<?php if (in_category(1) && !$single) { ?>
<ul class=”asides”>
<li id=”p< ?php the_ID(); ?>“><?php echo wptexturize($post->post_content); echo ‘ ‘; comments_popup_link(‘(0)’, ‘(1)’, ‘(%)’)?> <?php edit_post_link(‘(e)’); ?></li>
</ul>
< ?php } else { ?>

Where category 1 is the number of blog – you can see this in the admin dashboard. The effect is posts in cat(1) get treated differently , on my blog I’ve removed the post content completely so you just have the titles, I think it makes things tidier.

Nagios Ping And Traceroute Tool Version 0.3

This has been on my to-do list for a long while….I have finally release a bundled package which includes bug fixes from users, a big thank you goes out to you all.

Here’s a copy of my changelog:

Version 0.3
Package bundle of 0.1 & 0.2 – Plus valid html code for exit errors.

http://www.linickx.com/blog/archives/92/nagios-ping-and-traceroute-tool-version-03/

Date: 16.12.2005

Version 0.2
Commented out hosts problem.

http://www.linickx.com/blog/archives/81/nagios-ping-tool-another-hack/

Date: 09.11.2005

Version 0.1
NagiosQL – Multiple hosts.cfg files support.

http://www.linickx.com/blog/archives/79/nagios-ping-tool-nagios-ql/

Date: 08.11.2005

Version 0
Initial Release.
Nagios Exchange Release Date: 28.09.2005

Writing a PHP Page for the googlebot / google’s cache

Here’s something cool to share.

My calcylator project is written in php, it’s one of those “on-line tools” where users can create an account, log in, and can calculate personal profit & loss sheets for ebay.

Like most websites there are “log in” sections, now these pages need to be dealt with differently for search engines & users. Search engines will not be able to log in, but you may still want to deliver some content to them. Take my example, calcylator is running google adsense, and for adsense to work the googlebot (search engine) needs to be able to “see” some content, thinking this through, every time googlebot visited the log in or other protected pages all it would see is “access denied” error pages.

To target google with some “special” content, google kindly present themselves as google in their agent string. Now php even provide a simple tutorial on distinguishing user agent, so this is all I needed to do:

<php

function find_googlebot() {

# This is where we look for the googlebot
$user_agent = $_SERVER["HTTP_USER_AGENT"];

if (preg_match (“/[Gg][Oo][Oo][Gg][Ll][Ee]/”, $user_agent )) {
return true;
}

#For debugging, uncomment this and the agent will appear in the source of the html.
#echo “<!– $user_agent –>”;

}

if (find_googlebot()) {
include(“google-content.php”);
exit;
}

?>

If I quickly run though what I’ve done. We have a function called find_googlebot , we run a test (look for the word google), if we get a match, then we return “true” and run the php google-content.

Here is a working example, if you visit http://www.calcylator.com/index.php?cmd=myhome you’ll see a login page (or your account if you log in) and google will see:

http://www.google.com/search?q=cache:25ez2GhuhDQJ:www.calcylator.com/index.php%3Fcmd%3Dmyhome+site:http://www.calcylator.com

Have fun, just remember, google cache doesn’t update that often, so if you present a mistake, you may be stuck with it for a while ;)

Nagios Ping Tool – Another Hack

I’ve received a patch from ed.davison, if you have commented out hosts in your config, you’ll get odd \”address – hostname\” results in the drop down list. Ed’s patch fixes that; thankx :-D

At the moment you’d have to apply the patch your self from the below; but I have a official revision in the pipeline.

-- readhosts.php       2005/11/09 18:07:43     1.1
+++ readhosts.php       2005/11/09 18:08:12
@@ -23,16 +23,20 @@

       $current = trim($line);

-       if (preg_match (\"/addres/\", \"$current\")) {
-               $keywords = preg_split (\"/[\\s,]+/\", \"$current\");
-               $ipaddress[$counter] = $keywords[1];
-               $counter++;
-       }
-
-       if (preg_match (\"/host_name/\", \"$current\")) {
-               $keywords = preg_split (\"/[\\s,]+/\", \"$current\");
-               $node_name[$counter2] = $keywords[1];
-               $counter2++;
+       if (!preg_match (\"/^#/\", \"$current\")) {
+
+               if (preg_match (\"/addres/\", \"$current\")) {
+                       $keywords = preg_split (\"/[\\s,]+/\", \"$current\");
+                       $ipaddress[$counter] = $keywords[1];
+                       $counter++;
+               }
+
+               if (preg_match (\"/host_name/\", \"$current\")) {
+                       $keywords = preg_split (\"/[\\s,]+/\", \"$current\");
+                       $node_name[$counter2] = $keywords[1];
+                       $counter2++;
+               }
+
       }

Nagios Ping Tool & Nagios QL

Recently I had a request from rex to modify my nagios ping tool (Official Nagios Exchange page); he wanted to use the tool with his nagios configuration.

Rex appeared to be using Nagios QL (Which I believe to be a nagios management tool) , now QL handles nagios config slightly differently . The Ping Tool reads the nagios hosts.cfg file, and turns it into a couple of arrays to use for ping & display, with QL they generate multiple hosts.cfg files, one for each hosts.

Perhaps a short way of saying it, Rex needed to read in multiple hosts.cfg files from a directory, and below is the hack to do it :)

Simply copy & past the below code into a file called readhosts.php, replace your readhosts.php with the new one , and set the variable $hostfilepath in config.php, something like $hostfilepath = “/hosts”; (assuming /hosts is where you keep the files ;-) ) should do the job.

<?php

#This page reads in the hosts described in the config file to an array ready for use later

if ($hostfilepath == "" ) {
        ?>
	<h1> Please update your config file</h1>
	<h2> You need to add the line $hostfilepath = "/path/to/host/config/files"; to config.php </h2>
	<?php
        exit;
}

	#We need a counter to increment for the file array.
	$counter = 0;
	# A second counter is used for the array of actual hosts
	$counter2 = 0;

if (is_dir($hostfilepath)) {

	if ($dh = opendir($hostfilepath)) {

		while (($file = readdir($dh)) !== false) {

			$firstchar = substr($file,0,1);
			if ( $firstchar !== "." ) {
				$full_file_path = "$hostfilepath/$file";

				$lines = file($full_file_path);

				make_host_array();

				#echo "$full_file_path<br>";
			}
		}
       closedir($dh);
   	}
}

function make_host_array() {
	global $lines,$ipaddress,$node_name,$counter,$counter2;

	foreach ($lines as $line_num => $line) {

		$current = trim($line);

		if (preg_match ("/addres/", "$current")) {
	    		$keywords = preg_split ("/[\s,]+/", "$current");
			$ipaddress[$counter] = $keywords[1];
			$counter++;
		} 

		if (preg_match ("/host_name/", "$current")) {
	    		$keywords = preg_split ("/[\s,]+/", "$current");
			$node_name[$counter2] = $keywords[1];
			$counter2++;
		}

	}

#print_r($ipaddress);
#print_r($node_name);

}

?>

PHP – Nagios Ping & Traceroute Tool

Another day, another mini project.

My place of work pretty much demanded the need for our monitoring guys (i.e. people that stare at the nagios screen, waiting for red things) to have the ability to run traceroutes from nagios to the effected node – The twist in the story was that they wanted to do it from a browser !

So, I thought, that’d be an easy php script then :cool: , well to be honest it took a little longer than expected, mainly because I don’t really understand regular expressions (yet).

Nagios-ping-tool Is a package of small scripts :

  • config.php – some simple config variables, important for telling the script where to find your nagios hosts.cfg
  • index.php – the script that creates the buttons
  • readhosts.php – the script that reads /etc/nagios/hosts.cfg to find names & ip addresses
  • header.php & footer.php – my actual implementation went into another skinned site, so I put these in to format the pages properly

nagios-ping-tool.tgz can be downloaded from this site or the Nagios Exchange Project Page

PHP – Nagios Simple CFG Gen.

Nagios
.. or nagios ;) is great, I use it a lot.

In my continuing quest to cure command line phobia I’ve written a small (& Basic) php script that can generate some sample configs. Simply type in the IP of what you want to monitor , tick a couple of boxes, and copy & paste what you get into the end of your config files – nice :cool:

There’s a Demo Site here : http://www.linickx.com/index.php?content=nagios
and the Source here: http://www.linickx.com/files/php/nagios-simple-gen_php.txt

UPDATE:
Also available at nagiosexchange.org: http://www.nagiosexchange.org/Configuration.20.0.html?&tx_netnagext_pi1[p_view]=327

PHP Snippet: Reverse Proxy ?

I’m not suffering from Blog Burnout, just a little playstation distraction ;) Actually, I’ve spent a lot of time learning PHP for my potential new project; during this I found the function: file_get_contents, which in turn leads me to ask:

Is it possible to make a PHP Reverse proxy ?

I’m thinking yes, but it wouldn’t be easy. file_get_contents allows you to open a file or even a URL from a PHP script running on your webserver, just this simple thought gave birth to this snippet of code:


<?php
$URL=$_GET
["url"];
$self=$_SERVER["PHP_SELF"];

if ( $URL == "") {
?>
No URL Given:

Try <a HREF="<?php echo $self; ?>?url=http://www.linickx.com"><?php echo $self ;?>?url=http://www.linickx.com</a>
<?php
} else {
$PAGE = file_get_contents($URL);

echo "$PAGE";

}
?>

As you can see it allows a user to send a url , which the server downloads and delivers to their browser. Now before anyone puts this onto a webserver you should be aware of the MASSIVE Security Issue, any user that opens the page can ask the server to open any file, e.g /etc/passwd or a malicious URL.

If you do try it, you’ll find that the page doesn’t look right or links don’t work, or even that pictures have loaded, but they were delivered directly to the browser, not from the server. That’s because no re-writing is taking place (that’s the hard bit) , but with companies like whale & juniper investing huge sums of money in SSL vpns, which is simply a reverse proxy with a bit of ssl tunneling, I don’t suppose it’ll be long ;)

This is cool – Kplaylist

I’ve found this cool MP3 steaming software, easier to setup than icecast/ices & more appropiate for home users. :D Kplaylist uses mysql & php to give you a multi user website for your music directory, you can build playlists & stream music, and the album art is a nice touch. It even supports mp3 streaming over ssl, if only I can get the SSL patch for xmmsto work !

Check it out @www.kplaylist.net/ :cool:

PHP – want a random quote ?

I thought my site was missing something, perhaps a little bit of dynamic text , so now we have it ! Below my logo random one liner quotes will appear :cool:

I found the basics at totallyphp but it only read the quotes from an array, not a file, so: I added the file function & turned the script into a function :D

If you too want simple random quotes to include the source is here. The usage is simple, create a file called quotes.txt with quotes, a new one on each line (make it world readable) ; by default if you then call quotes.php it’ll choose a line & print the text… if you comment out echo randomquote(); you could include quote.php in other code :idea:

Here’s the code… enjoy !

# Where do I get my quotes from ?
$QUOTEFILE=”./quotes.txt”;

function randomquote() {
global $QUOTEFILE;
$quotes = file(“$QUOTEFILE”);

srand ((double) microtime() * 1000000);
$randomquote = rand(0,count($quotes)-1);

return $quotes[$randomquote];

}

echo randomquote();