Redirecting a Request with Path Information

bnmng.com is mapped to my share on totalchoicehosting‘s servers. But users can still type bnmng.com in the address bar and get redirected to the WordPress blog along with any extra path information. So http://bnmng.com/category/computing/ becomes https://bnmng.wordpress.com/category/computing.

I do this with a simple index.php in the home directory:
index.php
<?php
header('LOCATION: https://bnmng.wordpress.com' . $_SERVER['REQUEST_URI']);
?>

$_SERVER[‘REQUEST_URI’] is everything after the .com. In case the code wraps on your screen, this is just one line of code between the <?php and ?> tags.

But this wouldn’t work unless I ensured that my index.php file would respond to the url with the path info.

To do that, I copied WordPress.org‘s .htaccess file (described here as “genius”, and explained in more detail here), which is found on self-hosted WordPress blogs. This requires Apache’s mod_rewrite.

.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php  [L]
</IfModule>

Without this, http://bnmng.com/category/computing/ would simply return a “page not found” error.

That’s all I need to make the redirect. The redirect only occurs if the request is bnmng.com without any path information or bnmng.com with path information that doesn’t correspond to anything on my share. http://bnmng.com/codeexamples, for example, doesn’t get redirected.

This also seems to work for any bnmng.com feeds that people may have subscribed to before I moved bnmng.com from wordpress.com to totalcoicehosting.com. There’s no need to create a feed directory on totalchoicehosting, as I described in an earlier post.

I could have just put a self-hosted blog in my home directory and had a smoother effect without the redirect, but I like the convenience of using WordPress.com’s servers. And if they make a little money with ads on my blog then I’m happy to support them, especially if it doesn’t cost me a dime.

I’m messing around with my domain settings

I’m going to map bnmng.com to another server because I’m so much more than just an erudite blogger, I want to have a place under my URL for such projects as my WordPress themes and by blog reader/sharer if I ever get around to finishing it.

This blog will still be under https://bnmng.wordpress.com, but no longer under bnmng.com.

If you’re one of my countless devoted followers, there’s no need for panic or suicide! I think I can keep the old RSS feeds going by creating a directory on the new server called “feed” and creating an index.php file with the following code:

<?php

$url='https://bnmng.wordpress.com/feed';
if(isset($_SERVER['QUERY_STRING'])) {
$url=$url . '?' . $_SERVER['QUERY_STRING'];
}
$output=file_get_contents($url);
echo $output;
?>

So the RSS feed “http://bnmng.com/feed?cat=262&#8221;, which was mapped to this wordpress blog but will be mapped to the new server, will grab and return the contents from “https://bnmng.wordpress.com/feed?cat=262&#8221;. That should work.

I don’t know if email subscriptions will be affected at all, but I think when I select bnmng.wordpress.com as my primary URL, those will automatically be updated.

Blog Reader / Post Sharer

I’ve been experimenting a way to share posts on my sidebar. Rather than a static blogroll which just lists blog titles or an RSS feed which shows every post from the feed, I want to specifically select the posts that appear on the sidebar. I don’t know if there’s already something out there that does this.

I’m working on a blog reader which lets me select posts. When I choose to share a post, it writes an XML file which includes only those selected posts.

On my blog, I have an RSS widget which reads the file that I created.

My source is a yahoo pipe:

I use the regex module to add a feed title to the beginning of each post. Doing this is useful for a mashup because the feed title would otherwise get lost.

But the source can be any valid feed, either a mashup or a feed from a single blog.

What my reader does is displays all posts from the source feed, each with a “Share” button, which, when pressed, recreates my XML file. The XML file is an RSS feed which only includes posts which I have selected. It doesn’t include posts which have scrolled off my feed, so this isn’t like a bookmarking service which will save bookmarks indefinitely.

The result is on my sidebar, titled “Selected Posts from my Blogroll”.

It only updates when I use it, so my sidebar will be stale if I don’t keep up on it.

The server that this all runs on is at suffolkian.com. My xml output file is http://www.suffolkian.com/bnmng_blogroll/selectposts/selectedposts_bnmng.xml

I’m still just experimenting, but if you want to use this application or help me test it, use the contact form and let me know your user name, email address, desired password, and source feed. This is real low security, so you probably shouldn’t use the same password that you use for your online banking. I’ll make a configuration file for you and email the link to your reader and the link to the xml file that the reader generates. I make no promises that it will work or that you won’t loose your saved posts as I make changes to the code.

An improvement, and another use for the safetags function

A while ago I wrote a function which checks HTML snippets and ensures that tags which should be closed are removed unless they are actually closed. For example, if user types

<div style="whatever">Blah Blah

and forgets to close the div tag, then that div tag will be removed. Sure it would be better if the div tag was fixed, but that requires more code. This is good enough for me.

Another use for this function is excerpts. An original article may have all the tags properly closed but the excerpt might have an opening tag with it’s matching tag cut off.

A copy of the function is here.

It is not a validator. It ignores tags like p and br which will work even if not coded properly. You can edit the function to change which tags are allowed.

I used the function to alter Virginia CURE’s website from the original cover-wp theme. For comparison, I found a couple of other websites which use cover-wp: NELA Lives, Hedge Futures, and Marco Luthe. I’m not familiar with these websites, I just looked for websites that use the cover-wp theme. Eventually, any of these websites might disappear, or any of them might change themes, including Virginia CURE. But as I’m writing this they’re good examples. Virginia CURE’s excepts have the paragraphs preserved, the others don’t. The cover-wp theme uses a customized get_the_excerpt function, which in turn calls the built-in php function striptags to remove all of the tags.

Here’s copy of functions.php which I altered from cover-wp. I renamed it functions.txt so it can be viewed. I also made a small change to get_the_excerpt.

If anyone who reads this knows of a better function then I’d be interested. I’m just an amateur hack. But for now, my function works better than anything I found.

I fixed a couple of problems since I first posted it.

Combining feeds

A friend suggested I combine my schizophrenic group of blogs into a single blog. I’m not ready to do that but I did decide to display a feed from all my blogs on a single page. Easy enough, but I wanted to combine them all and sort them by date, rather than have a separate feed for each. I found a simple feed puller in a book called Plug-In PHP, by Robin Nixon. From that I modified and expanded the code to allow combining different feeds into one. Also, the books code worked for my wordpress blogs, but then I also wanted to make another feed which included blogs that I read, and my daughter users blogger. I had to write new code to accept her feed. I don’t know if this takes all kinds of feeds, but it does what I’ve needed it to do so far.

<?php
//Sort posts by publication date
function sortpdate ($a, $b) {
 $adate=strtotime($a[‘pubdate’]);
 $bdate=strtotime($b[‘pubdate’]);
 if ($adate == $bdate) { return 0;}
 return ($adate < $bdate) ? 1 : -1;
}

function RSStoHTML ($urllist, $usortfunction, $fullposts=3, $maxposts=12, $titletag=”h4″, $classprefix=””) {
//Takes blog posts from multiple blogs using a comma separated list of feeds, usorts them, and echos them
 

 $urls=explode(‘,’, $urllist);
 $i=0;

 $showitems=array();

 foreach ($urls AS $url) {
  
  $rss=file_get_contents($url);
  $xml = simplexml_load_string($rss);

  //The two foreach loops that follow pickup wordpress and blogger
  //I don’t know yet if these fail on other types of feeds.
  foreach($xml->channel->item as $item) {
   $i++;
   $showitems[$i][‘blogtitle’]=@$xml->channel->title;
   $showitems[$i][‘bloglink’]=@$xml->channel->link;
   $showitems[$i][‘blogdescription’]=@$xml->channel->description;
   $showitems[$i][‘itemlink’] = @$item->link;
   $showitems[$i][‘pubdate’] = @$item->pubDate;
   $showitems[$i][‘itemtitle’] = @$item->title;
   $showitems[$i][‘summary’] = @$item->description;
   $showitems[$i][‘content’] = @$item->children(“content”, true)->encoded;
  }
  foreach($xml->entry as $item) {
   $i++;
   $showitems[$i][‘blogtitle’]=@$xml->title;
   foreach (@$xml->children()->link AS $child ) {
    if($child->attributes()->rel==’alternate’) {
     $showitems[$i][‘bloglink’]=$child->attributes()->href;
    }
   }
   $showitems[$i][‘blogdescription’]=@$xml->subtitle;
   foreach (@$item->children()->link AS $child ) {
    if($child->attributes()->rel==’alternate’) {
     $showitems[$i][‘itemlink’]=$child->attributes()->href;
    }
   }
   $showitems[$i][‘pubdate’] = @$item->published;
   $showitems[$i][‘itemtitle’] = @$item->title;
   $showitems[$i][‘summary’] = substr ( @$item->content , 0, 30 );
   $showitems[$i][‘content’] = @$item->content;
  }
 }
 usort($showitems, $usortfunction);
 $out=””;
 $i=0;
 while ($i < sizeof($showitems) && $i < $maxposts) {
  if($i<$fullposts) {
   echo ‘<‘, $titletag, ‘ class=”‘, $classprefix, ‘itemtitle”><a href=”‘, $showitems[$i][‘itemlink’], ‘”>’, $showitems[$i][‘itemtitle’], ‘</a></’, $titletag, ‘><p class=”‘, $classprefix, ‘pubdate”>’, (date(‘l, Y F d’, strtotime($showitems[$i][‘pubdate’]))), ‘ in <a href=”‘, $showitems[$i][‘bloglink’], ‘”>’, $showitems[$i][‘blogtitle’]>”” ? $showitems[$i][‘blogtitle’] : $showitems[$i][‘bloglink’], ‘</a></p><p class=”‘, $classprefix, ‘summary”>’, $showitems[$i][‘content’], ‘</p>’;
  } else {
   echo ‘<‘, $titletag, ‘ class=”‘, $classprefix, ‘itemtitle”><a href=”‘, $showitems[$i][‘itemlink’], ‘”>’, $showitems[$i][‘itemtitle’], ‘</a></’, $titletag, ‘><p class=”‘, $classprefix, ‘pubdate”>’, (date(‘l, Y F d’, strtotime($showitems[$i][‘pubdate’]))), ‘ in <a href=”‘, $showitems[$i][‘bloglink’], ‘”>’, $showitems[$i][‘blogtitle’]>”” ? $showitems[$i][‘blogtitle’] : $showitems[$i][‘bloglink’], ‘</a></p><p class=”‘, $classprefix, ‘summary”>’, $showitems[$i][‘summary’], ‘</p>’;
  }
  $i++;
 }

 echo $out;
}

$urls=”https://bnmng.wordpress.com/feed,http://bnmngfarming.wordpress.com/feed,http://bnmngcomputing.wordpress.com/feed,http://healthyinsuffolk.wordpress.com,http://sashabyproxy.wordpress.com&#8221;;
RSStoHTML ($urls, ‘sortpdate’);

PHP function to validate HTML tags for user input fields.

I have some forms with text areas that take user input. My forms aren’t as fancy as the editor that wordpress provides. I want to allow users to add html tags, but need to make sure they close their div’s and other tags which might screw up the whole page if they forget to close them. And I want to control which tags I allow. So I wrote a function that uses the htmlentities function to disable all markup and then, for those tags that I want to allow, does some checking and converts them back into enabled markup.

The function won’t fix broken markup but it will isolate broken markup so it doesn’t affect the rest of the page.

I’ve only had this function running for a day and I can’t promise I have the bugs all worked out. But I think it’s good. Please let me know if you see or find any problems with it.

I’ll copy the function below so you can get a quick view of what it does, but I’m having trouble getting wordpress to display it correctly so I put the source code in a plain text file http://www.suffolkian.com/bnmng_old/blog_files/bnmngcomputing/function_safetags.txt. Of course, you’ll have to put it in php file to make it work.

function safetags ($text) {

#This function uses htmlentities to disble markup, then converts those tags that are allowed and properly formatted back into enabled markup.

$loner_tags=array(‘br’, ‘hl’, ‘img’, ‘p’ ); #These are tags that can left unclosed. But it’s OK if they are closed.
$closed_tags=array(‘a’, ‘b’, ‘center’, ‘div’, ‘left’, ‘li’, ‘right’, ‘span’, ‘ul’ ); #These are the ones that have to be closed.

$i=0;
$pattern=array();
$replace=array();

#convert the entire text to html entities. This takes all tags and quotes and converts them to harmless strings.
$text=htmlentities($text);

#for all allowed tags, if they have closing tags then enable them.
$all_tags=array_merge($loner_tags, $closed_tags);

foreach($all_tags as $tag) {
$pattern[]=’/(?<![])<' . $tag . '((?<=' . $tag . ')s+.*?)*(?<![])>(.*?)(?<![])</' . $tag . '(?<![])>/si';
$replace[]='2′;
}

#Enable those tags that don’t have closing tags and are allowed without them.
foreach($loner_tags as $tag) {
$pattern[]=’/(?<![])<' . $tag . '((?<=' . $tag . ')s+.*?)*/?(?<![])>/si';
$replace[]='’;
}

#Within each properly converted tag, convert the properly quoted attributes.
$pattern[]=’/<([^]*?)"(.*?)"([^]*?)>/’;
$replace[]=”;

$pattern[]=’/<([^]*?)'(.*?)'([^]*?)>/’;
$replace[]=”;

#Here’s where the work gets done

for($i=0;$i<count($pattern);$i++) {
while(preg_match($pattern[$i], $text, $matches) ) {
$text=preg_replace($pattern[$i], $replace[$i], $text);
}
}

return $text;
}

Update (2010 01/03): I’m amazed whenever I get hit. But the one person who happened to stumble upon my blog this month also managed to hit a broken link. Oh well. The link’s fixed now.