Include an Evernote feed in your WordPress blog

I use Evernote to keep track of my ToDo’s, interesting web pages that I visited, found code snippets or some random thoughts. I have it configured in a browser on my laptop and as a separate app on my smartphone.

“Things” in Evernote are put in notebooks, notebooks can be shared. I’ve been sharing some of my notebooks with friends and colleagues to keep track of joint projects. These shared notebooks have a RSS feed. You can include this feed wherever you want : your favorite news reader or on your own web site.

The snippet below shows that it is fairly easy to accomplish this in WordPress. Unfortunately (as far as I can tell), you can not retrieve the tags of the notes in the RSS feed.
You’ll have to include the snippet somewhere in your WordPress custom theme. I have it in sidebar.php.

The code is also available at Github https://github.com/cudeso/tools.

// When you are changing your theme, the retrieved feed will be cached. You can prevent caching with limiting the lifetime. 
// Don't use this in a production environment!!
/*
function return_1( $seconds ) {
  return 1;
}
add_filter( 'wp_feed_cache_transient_lifetime' , return_1);
*/

// Limit the number of items to retrieve
$maxitems_feed = 10;
$feed_url = "http://www.evernote.com/_whatever_links_to_your_feed/rss.jsp";
$rss = fetch_feed($feed_url . "?max=" . $maxitems_feed . "&sort=2");

if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
    // Figure out how many total items there are, but limit it to maxitems_feed. 
    $maxitems = $rss->get_item_quantity($maxitems_feed); 
    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items(0, $maxitems); 
endif;

if (!($maxitems == 0)) {
	?><h1><?php _e( 'Evernote Feed' ); ?></h1>
	<ul>
	<?php
	foreach($rss_items as $item) {
		?>
		<li>
			<a href='<?php echo esc_url( $item->get_permalink() ); ?>'
			        title='<?php echo 'Posted '.$item->get_date('j F Y | G:i'); ?>'>
			        <?php echo esc_html( $item->get_title() ); ?></a>
		</li>
		<?php
	}
	?>
	</ul>
	<?php
}

Graph the first 8 bits of the IP of apache logs

Below is a PHP script that will graph the first 8 bits of the visitors source IP in your apache logs. The bigger the circle, the more visitors you had. You’ll need gd support in PHP. The minimal width of the circle is set to 3, the maximum width is 85. You can run the script with

 ./httpstatus_2_png.php /var/log/apache2/access.log 20x > /var/www/localhost/htdocs/20x.png

The first parameter is the log file. The second parameter is either 20x, 40x or 50x. This graphs the 20x, 40x or 50x HTTP response codes.

Example

The code is also available on GitHub (https://github.com/cudeso/tools/blob/master/httpstatus_2_png.php).

#!/usr/bin/php
<?php
/**
 *	Create a graph based on the first 8 bits of an IPv4 address
 *
 *	Inspiration from http://www.seehuhn.de/pages/internet
 *	As seen in HDMoore http://www.youtube.com/watch?v=b-uPh99whw4
 *
 *	@param 	logfile 								the log file to parse
 *	@param	http_response_code			what type of graph to create, based on http response code (20, 40, 50)
 *	@param	ignore_ips							(optional, what IPs to ignore)
 *
 *	@version 20121009
 * 	@author Koen Van Impe <koen.vanimpe@cudeso.be>
 *	@license New BSD : http://www.vanimpe.eu/license
 *
 */

error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Check for the correct numbers of paramters; display usage
if (!($argc == 3 or $argc == 4)) {
	?>
Create a graph based on the first 8 bits of an IPv4 address
  
Usage: 
 php <?php echo $argv[0]; ?> <logfile> <http_response_code> [ignore_ips] > <image>
  logfile	:	full path of the logfile to parse
  http_response_code : what http response code to plot (20x, 40x or 50x)
  [ignore_ips] : list of IPs to ignore (notation: '192,10,172')
  image : image to export to, fe. image.png

Example:
 php <?php echo $argv[0]; ?> access.log 40x '192,172' > image.png
	<?php
	die();
}

// Variable init
$ip_array = array();
$qt_array = array();
$ip_ignore_array = array();
$logfile = $argv[1];
$color_param = (int) $argv[2]; // strip the x from 50x
$ip = 0;

// Define graph settings
$basegraph_x = 800;
$basegraph_y = 800;
$max_width_circle = 85;

$im = ImageCreate($basegraph_x,$basegraph_y);
$background = ImageColorAllocate($im,0xa5,0x9a,0x7e);
$black = ImageColorAllocate($im,0x00,0x00,0x00);
$red = ImageColorAllocate($im,0x99,0x0f,0x06);
$green = ImageColorAllocate($im,0x63,0x99,0x3e);
$blue = ImageColorAllocate($im,0x28,0x4f,0x99);
ImageFilledRectangle($im,0,0,$basegraph_x,$basegraph_y,$background);

if ($color_param == "50") $color = $blue;
elseif ($color_param == "40") $color = $red;
else $color = $green;

// IPs to ignore
if (strlen($argv[3]) > 0) {
	$ip_ignore_array = explode(",", $argv[3]);
}

// Execute the command, save the output, then walk through the output
$color_param_esc = "^".$color_param;
exec("cat " . escapeshellarg($logfile) . " | awk '{ print $9 \" \" $1; }' |grep " . escapeshellarg($color_param_esc), $output);
if (is_array($output) and count($output) > 0) {
	foreach($output as $line) {
		if (strlen($line) > 0) {
			$arr_line = explode(" ", $line);
			
			// Build up the array width future "width" of the circles
			if (!(strpos($arr_line[1], ":") > 0)) {	// ignore IPv6 values
				$ipstr = substr($arr_line[1], 0, strpos($arr_line[1], ".") );
				// check if it's in the ignore list
				if (!(in_array($ipstr, $ip_ignore_array))) $ip_array[$ipstr]["qt"] = $ip_array[$ipstr]["qt"] + 1;							
			}
		}
	}			
}

// Get the max of the array and recalculate the width of the circles
foreach($ip_array as $key => $val) { $qt_array[] = $val["qt"]; }
if (max($qt_array) > $max_width_circle)	$overflow_value = round( max($qt_array) / $max_width_circle);
else $overflow_value = 1;
for($x = 1; $x<= 255; $x++) { $ip_array[$x]["qt"] = round($ip_array[$x]["qt"] / $overflow_value); }

// Walk through the array, set the coordinates and labels
for($y=0;$y<=15;$y++) {
	for($x=0;$x<=15;$x++) {
		$ipstr = (string) $ip;		

		if (strlen($ipstr) == 1)	$x_offset = 3;
		elseif (strlen($ipstr) == 2)	$x_offset = 8;
		else $x_offset = 11;

		$ip_array[$ipstr]["x_offset"] = $x_offset;
		$ip_array[$ipstr]["x"] = 15 + ($x * 50);
		$ip_array[$ipstr]["y"] = 15 + ($y * 50);			

		if (isset($ip_array[$ipstr]["qt"])) $width = (int) $ip_array[$ipstr]["qt"];
		else $width = 0;
		
		ImageArc($im, $ip_array[$ipstr]["x"] + $x_offset , $ip_array[$ipstr]["y"] + 7, $width, $width, 0, 360, $color);
		ImageFill($im, $ip_array[$ipstr]["x"] + $x_offset, $ip_array[$ipstr]["y"] + 7, $color);

		$ip++;	
	}	
}

// Put the labels on the graph
for ($ip = 1; $ip <= 255 ; $ip++) {
	$ipstr = (string) $ip;
	// Print "ip"-label after the Arc, otherwise the label gets ImageFilled
	ImageString($im, 4, (int) $ip_array[$ipstr]["x"], (int) $ip_array[$ipstr]["y"], $ipstr, $black);
}	
$title = date("Ymd H:i") . " / " . $argv[2];
ImageString($im, 2, 3, 0,  $title, $black);

// Export the image
Header('Content-Type: image/png');
ImagePNG($im);

?>

New design

I thought it was time for a new design of this website. The old setup was a combination of my own code with a WordPress blog. I now moved everything to WordPress with a custom theme and some template coding.

The old site used to look like this :

This site uses Google Fonts so you might want to turn on Javascript for all visual effects.

Use ONLY_FULL_GROUP_BY with WordPress

Something I came across recently when installing WordPress gave me headaches. Everything seemed to work properly except when selecting posts by category no results were returned.

I debugged the problem by looking at the SQL-queries performed by WordPress. One query returned an error :

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1  AND ( wp_term_relationships.term_taxonomy_id IN (1) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Because the MySQL server was configured to honor ONLY_FULL_GROUP_BY it gave the error “‘test.wpposts.post_author’ isn’t in GROUP BY”.

I could not disable ONLY_FULL_GROUP_BY serverwide so I had to insert it in the WordPress-code.

The best place to do this was in the wp-includes/wp-db.php. Look for the function db_connect() and add the code below as the last line of the function.

mysql_query( " SET sql_mode='ANSI,TRADITIONAL' ", $this->dbh);

Note that every time you perform an upgrade of WordPress you’ll have to add this line back to the source code.

UPDATE

After implementing this change I was unable to post new posts or pages through the WordPress interface. Doing Quick Posts (through the dashboard) and update via XML-RPC works without a problem.

WordPress reset

I finally managed to unlock my access to my WordPress blog. Since the last upgrade I always received the “You do not have sufficient permissions to access this page error”

I installed a fresh WordPress (new empty database, new path). Once the setup was done I copied everything from the old database to the new database except for wp_user and wp_usermeta. After logging in as an administrator everything was working as before the error. I created new users for posting content and so far I’ve not encountered any errors. I still have no clue why this error suddenly popped up.

Twitter or Blog

Dilemma:

Twitter your thoughts and have trouble recording them for later reference. Twitter + custom search don’t play along.
Blog your thoughts and having to think of “I can’t have a posting that’s only 100chars long”.

Topic: the metaweblog API is more powerful that the API provided by WordPress.
One of my customers asked me to create a function that would create a WordPress blogpost whenever one of their PR-people finishes a customer-visit (funny enough, these visits are recorded in a shared Google calendar that’s updated via another webapp, web2.0 in action). After fiddling around with the wp-native API I found that the metaweblog is much easier. I still need to return to the WordPress API for creating new categories but this only happens once every few months. WordPress rulz!

Widgetize your WordPress theme

If you want to use widgets on your WordPress theme, then these are the steps you should take:

1) Create a file functions.php in your theme directory. Add this to the file

if ( function_exists('register_sidebar') )
register_sidebar();
?>

2) Open your sidebar.php file and add this somewhere in the ul-list defintion

|| !dynamic_sidebar() ) : ?>

FOSDEM 2008

FOSDEM, the free and open source developers’european meeting is taking place in Brussels on 23/24 February.

Their schedule is online and shows that there are going to be some interesting talks :

Magpierss with UTF8

MagpieRSS is an RSS parser in PHP. If you’re parsing UTF-8 streams and the output looks crippled then you might want to try this (add this to your file that calls the Magpie-code ) :

define(‘MAGPIE_OUTPUT_ENCODING’, ‘UTF-8′);
define(‘MAGPIE_INPUT_ENCODING’, ‘UTF-8′);
define(‘MAGPIE_DETECT_ENCODING’, false);

Show comment box / comments on a WordPress Page

Just include this in your theme / template for the WP-pages :
 <?php comments_template(); ?>

If you’re not happy with the default then just edit the file comment.php