Tag Archive for 'php'

Preserving Permalinks From a MovableType to Wordpress Conversion

As mentioned in my previous entry, I converted davidchiu.net from MT to WP. The migration was pretty easy and I just followed the instructions I found on Wordpress.org. The only thing is that I didn’t care for their solution for redirecting previous MovableType permalinks to new Wordpress pages. So I came up with my own solution: create a custom WP 404 page which redirects the user to a search results page. The effect would be this:

If a user comes from an old MT permalink such as http://davidchiu.net/2003/05/some_old_title.php that does not correspond to an existing Wordpress page, convert the filename to a title fragment (ie, some_old_title.php becomes “some old title”) and return a Wordpress search result.

This should take care of all old permalinks. The corresponding code is pretty simple. I just added the following code to the top of my custom 404 page:

<?php
$filename=basename($_SERVER['REQUEST_URI'],”.php”);
$filename=basename($filename,”.html”);
$title=ereg_replace(’_|-’,’ ‘,$filename);
header(”Location: http://”.$_SERVER['SERVER_NAME'].”/?s=\”".$title.”\”");
?>

The first two lines strip either the “.html” or “.php” extension from the original request URL. The ereg_replace() function converts any underscores or dashes to spaces. Finally, the header() function redirects the user to the Wordpress search result page for the title fragment.

In my initial tests it works great. Let me know if this code could be better.

Remote Execution of Windows Desktop Applications Using Apache and PHP

Install Apache.

Install PHP.

Install PsTools.

Use PHP’s exec() function to launch a windows program. The following is an example:

<?php
exec(’C:\PsTools\psexec.exe -d -accepteula “C:\Program Files\VideoLAN\VLC\vlc.exe” -f http://movies.apple.com/movies/lionsgate/the_spirit/the_spirit_h.320.mov 2>&1′,$output);
var_dump($output);
?>

Take note of the flags. “-d” means don’t wait for the child process to terminate before continuing PHP execution. “-accepteula” is required in order for PsTools to run properly. VLC note: the “-f” flag tells VLC to play the movie fullscreen.

Start Apache, but don’t run it as a service. Instead run Apache from the desktop as the local user.
From a remote computer, point a web browser at the PHP page that implements the exec() function.

Adobe Flex/AIR Research Links

I’m doing a little research into Adobe Flex/AIR for a proposal I’m putting together. Until now I haven’t had the chance to look at Flex or AIR very closely, but now that I’ve watched a couple videos and read a couple articles I’m pretty excited about this technology. What I find even more interesting is that Adobe’s Flex Builder 3 is built on top of Eclipse. I’ve been spending a lot of time in the Eclipse platform because of the Java/Hibernate/Spring project I’m managing at work and using Aptana on my Mac for personal projects. I’ve also looked at Zend Studio which is yet another IDE built on top of Eclipse for PHP programming. I just wish there was an easy way to combine all of the different features from each IDE under a single Eclipse installation without causing headaches and conflicts. Plus I want a Textpad “plugin” since Textpad is the best text editor in the world and I couldn’t live without Textpad’s powerful regex search and replace.

Here are the links I’ve used for my research into Flex/AIR/BlazeDS:

The Yahoo Open Strategy

Yahoo wants to turn itself into a huge social-networking platform offering more API’s and it’s own application platform to compete directly with Google App Engine and Amazon Web Services. Sounds interesting. I think this idea could help rejuvenate Yahoo.

PHP Firewall Script

Do you worry about your site’s security? Has your site ever been hacked? Worry no more! FireWall Script is a PHP-based configurable firewall. Once installed, you can configure what mischevious things you want to be on the lookout for and let FireWall Script do the rest. With the logging functionality included, you can also go back and see if anyone is attempting to sidestep your rules and stay ahead of the game. Install FireWall Script, stop worrying about your site’s security, and start worrying about your site.

Fixing Broken Archive Links After MT4 Upgrade

Tonight I realized that the upgrade I did from MovableType 3 to MovableType 4 broke any and all links to my old articles once I had rebuilt my blog. The problem lies in the configuration option named “Basename Length” under Preferences->Blog Settings->Entries in MT4. The default value in MT4 is 30, whereas the default setting in MT3 was 15. Now I thought I could easily fix this by changing the value to 15 in MT4 and republishing my site, but that didn’t work. So instead I wrote this little PHP script to make copies of all my old archives articles and truncate the filenames to 15 characters. Here’s the script I wrote. Maybe it will help someone else. Be sure to CHMOD -R a+w archives/* before you run the script so that the script has permissions to create the new files. Then make sure you put the permissions back to what they should be when you’re done.

<?php

function changeext($directory, $verbose = false) {
  $num = 0;
  if($curdir = opendir($directory)) {
   while($file = readdir($curdir)) {
     if($file != '.' && $file != '..') {

       $srcfile = $directory . '/' . $file;
       $string  = "$file";
       $str     = strlen($ext1);
       $str++;
       $newfile = substr($string, 0, 15);
       $newfile = $newfile.'.php';
       $dstfile = $directory . '/' . $newfile;

      if (eregi("\.php",$file)&&(strlen($file)>19)) {
            ($srcfile, $dstfile);
            //echo "copy $srcfile, $dstfile
";
        }

       if(is_dir($srcfile)) {
         $num += changeext($srcfile, $verbose);
       }
     }
   }
   closedir($curdir);
  }
  return $num;
}

changeext('archives',  'false');

?>