Archive for the 'PHP' Category

reCAPTCHA plugin error messages

By default, on two of my installs of reCAPTCHA Plugin for WordPress - v2.7, I receive no error message if the captcha is entered incorrectly… the user of the weblog is to assume that they made a mistake I guess…

That is until I wrote this script to check for error messages being passed around on the URL by the plugin:

<script type="text/javascript">
if (window.location.search != "") {
        var searchArray = window.location.search.split("&");
        for (var i=0; i < searchArray.length; i++) {
                var searchSubArray = searchArray[i].split("=");
                if (searchSubArray[0] == "rerror" &&
                searchSubArray[1] == "incorrect-captcha-sol") {
                        document.write("<p><small style=\"color: red;\">The two words in the picture were typed incorrectly.</small></p>");
                }                              
        }
}
</script>

Just drop that snippet into your “comments.php” template. When the captcha is filled in incorrectly, the plugin adds “rerror=incorrect-captcha-sol” to the url. This checks for that string, and if found, writes an error message in red.

Posted by Karl Herrick on February 3rd, 2008 in Javascript, Wordpress, PHP, Web Development | 2 Comments »

reCAPTCHA plugin styling

Installing a CAPTCHA implementation on Wordpress is quite easy when using the reCAPTCHA plugin, however the styling is hard coded into the main php file included.

Goto the installation directory, for example, “wp-content/plugins/recaptcha-wordpress-2.7″ and edit recaptcha.php. Look for the line that embeds the theme type and change it.

<script type="text/javascript">
    var RecaptchaOptions = { theme : "red", tabindex : 5 };
</script>

I used the “clean” theme. There are other options and ideas for editing the code with css changes.

Posted by Karl Herrick on January 13th, 2008 in Wordpress, PHP, Web Development | No Comments »

Motion Capture Sorting

When using Motion for a security camera setup, it can generate a lot of files… One installation I have setup has “Encode movies in real-time” enabled, so not only are there many .jpgs in the capture directory, but also .avis.

This can become quite tedious… copying and pasting them from a GUI every day. Consequently, I would let the files simply pile up for a few days before moving them around. Then it became a big job.

Scripting to the rescue. The following below is a PHP-CLI script, that when run from the motion capture directory will sort the various files into a nice little structure that allows for dropping the .avis into a play list for viewing, and keeps the .jpgs around for backup.

#!/usr/bin/php
<?php

$fileExtOne = ".jpg";
$fileExtTwo = ".avi";
$fileNameArray = array();

//dump filenames (ending in .jpg or .avi)
//in current directory into array
if ($handle = opendir(".")) {
    while (false !== ($file = readdir($handle))) {
        if (substr($file, -4) == $fileExtOne ||
            substr($file, -4) == $fileExtTwo) {
            array_push($fileNameArray, $file);
        }
    }
    closedir($handle);
}

//make multi-dimensional array with substrs of filename
for ($i=0; $i<count($fileNameArray); $i++) {
    $fileNameSplit = split("-",$fileNameArray[$i]);
    $dirArray[$i]["group"] = $fileNameSplit[0];
    $dirArray[$i]["year"] = substr($fileNameSplit[1], 0, 4);
    $dirArray[$i]["month"] = substr($fileNameSplit[1], 4, 2);
    $dirArray[$i]["day"] = substr($fileNameSplit[1], 6, 2);
    $dirArray[$i]["name"] = $fileNameArray[$i];
}

//create unique array with new directory names and mkdir them
for ($i=0; $i<count($dirArray); $i++) {
    $dirNameArray[$i] = $dirArray[$i]["year"]
            ."-". $dirArray[$i]["month"]
            ."-". $dirArray[$i]["day"]
            ."/". $dirArray[$i]["group"];
}
$uniqueNewDirNames = array_unique($dirNameArray);
foreach ($uniqueNewDirNames as $value) {
    $splitUniqueDirNames = split("/", $value);
    mkdir($splitUniqueDirNames[0], 0755);
    mkdir($splitUniqueDirNames[0]
        ."/". $splitUniqueDirNames[1], 0755);
}

//move files into the appropriate directories
for ($i=0; $i<count($dirArray); $i++) {
    if (substr($dirArray[$i]["name"], -4) == $fileExtOne) {
        rename($dirArray[$i]["name"], $dirArray[$i]["year"]
            ."-". $dirArray[$i]["month"]
            ."-". $dirArray[$i]["day"]
            ."/". $dirArray[$i]["group"]
            ."/". $dirArray[$i]["name"]);
    }
    if (substr($dirArray[$i]["name"], -4) == $fileExtTwo) {
        rename($dirArray[$i]["name"], $dirArray[$i]["year"]
            ."-". $dirArray[$i]["month"]
            ."-". $dirArray[$i]["day"]
            ."/". $dirArray[$i]["name"]);
    }
}

?>

Here’s what find returns after running the script on the capture directory full of sample .avis and .jpgs.

username@workstation:~/captures> find .
.
./2008-01-06
./2008-01-06/01
./2008-01-06/01/01-20080106145755-02.jpg
./2008-01-06/01/01-20080106145755-04.jpg
./2008-01-06/01/01-20080106145756-00.jpg
./2008-01-06/01/01-20080106145757-00.jpg
./2008-01-06/01/01-20080106145757-01.jpg
./2008-01-06/01/01-20080106145758-00.jpg
./2008-01-06/01/01-20080106145758-01.jpg
./2008-01-06/01/01-20080106145758-02.jpg
./2008-01-06/01/01-20080106145758-04.jpg
./2008-01-06/01/01-20080106145758-05.jpg
./2008-01-06/01/01-20080106145758-06.jpg
./2008-01-06/01/01-20080106145759-00.jpg
./2008-01-06/01/01-20080106145808-01.jpg
./2008-01-06/01-20080106145755.avi

Posted by Karl Herrick on January 6th, 2008 in Video, PHP, Linux | No Comments »

Currency Conversion

A long time ago, I wrote a PHP script to compare current gas prices from Michigan and Ontario in current US and Canadian dollar rates from the following three sites:

http://ontariogasprices.com/
http://michigangasprices.com
http://finance.yahoo.com

as soon as all of the data was retreived (gas prices used a php web scraping library, currency data from Yahoo! Finance retrieved similarly to how Exch does), do the math, and it would let me know who was paying what for gas prices… well using a little Google search, most of the logic of this script became a lot easier (well it has been for some time, but I thought I would share on this little known secret :-)… for example Google these searches below:

3.16 USD per gallon in CAD per liter
3.16 (U.S. dollars per US gallon) = 0.945976868 Canadian dollars per liter

1.10 CAD per litre in USD per gallon
1.10 (Canadian dollars per litre) = 3.67450845 U.S. dollars per US gallon

1.10 CAD per litre in EUR per gallon
1.10 (Canadian dollars per litre) = 2.90268461 Euros per US gallon

1.10 CAD per litre in EUR per litre
1.10 (Canadian dollars per litre) = 0.766808149 Euros per litre

Google even acknowledges the alternative spellings of “liter/litre” … nice huh?

Posted by Karl Herrick on June 26th, 2006 in PHP, Google, Web Development | No Comments »

Recursive Convert

My little application is coming along. Slowly though. I found an mp3 library which utilizes the LAME mp3 library called Tritonus. We’ll see if I can get that to work. The example I have does. :-)

I also found a recursive directory structuring routine. Coupled with looking at my previous source code… I have working… PIECES! Now it’s time for it all to come together.

Alright, back to coding… kind of.

Posted by Karl Herrick on May 29th, 2004 in Java, PHP, MP3 | No Comments »