≡ Menu

Using Macrium Reflect to Clone a Windows PC

Like so many developers are, I am the IT guy for my family as well as being one “in real life”. Today, I was confronted with an issue that was one of those hardware-related issues that crop up from time to time, and those can be just as difficult (if not more so) than software problems. I got a call telling me that Windows was popping up an alert window that warned of impending doom with the hard disk drive.

Windows 7 imminent HDD failure

A message similar to this one appeared repeatedly.

I used to deal with hardware issues all the time, and Norton Ghost was my software of choice for such problems in years past. However, I didn’t have a copy on hand, and searched for an alternative. Fortunately, I found out about Macrium Reflect. After an emergency trip to Best Buy for a new drive, I was able to clone the old hard drive in about twenty-five minutes using the free version of Macrium Reflect!

Macrium Reflect screen

The steps for using Macrium were very simple. Just make certain that you know which drive is which when you’re using the software; otherwise, you’ll end up erasing your old hard drive.

Using PHP to Search for Text in Website Source Code

As I’ve mentioned before, I use the Thesis premium theme on my WordPress site, and I generally have no problems at all. However, the newest Thesis update came out, and I am getting an HTML5 validation error.

Usually when this sort of thing happens, whether it be with a theme or with a plugin, I’ll try to fix what is causing the error and then report the fix to the author. The validation error I am getting is below.

HTML5 validation error on Thesis 2.2.1

The validation error and the rendered HTML.

The Thesis codebase is fairly complicated and is not easy to decipher if you’ve never seen it before. Even though I’d hacked on it a few times before, I’d never come across the code that generated this bit of HTML.

<style type="text/css">
#thesis_launcher { position: fixed; bottom: 0; left: 0; font: bold 16px/1em "Helvetica Neue", Helvetica, Arial, sans-serif; padding: 12px; text-align: center; color: #fff; background: rgba(0,0,0,0.5); text-shadow: 0 1px 1px rgba(0,0,0,0.75); 
#thesis_launcher input { font-size: 16px; margin-top: 6px; -webkit-appearance: none; 
</style>

My blog runs on a shared server, and I don’t have SSH enabled currently, so there was no way I could use grep to search for the text, and the cPanel search utility only looks at filenames.

After some searching, I found an article that had code for finding filenames in all subfolders from a path on your site. This code would not search the text itself, but would allow for recursive folder searching.

function rsearch($folder, $pattern) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
    $fileList = array();
    foreach($files as $file) {
        $fileList = array_merge($fileList, $file);
    }
    return $fileList;
}

Also, I was able to find another post that explained text searching in a file.

$path_to_check = '';
$needle = 'match';

foreach(glob($path_to_check.'*.txt') as $filename)
{
  foreach(file($filename) as $fli=>$fl)
  {
    if(strpos($fl, $needle)!==false)
    {
      echo $filename.' on line '.($fli+1).': '.$fl;
    }
  }
}

By combining and modifying these, I was able to put together a relatively simple file that will search through all files matching a pattern (in this case, PHP files) and printing instances of the text that contains the search term.

$path_to_check = "(your folder)";
$pattern = "/.*php/";
$needle = $_GET['needle'];

function rsearch($folder, $pattern, $needle) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
    //$fileList = array();
    foreach($files as $file) {    	
        //$fileList = array_merge($fileList, $file);
        foreach($file as $filename) {
           foreach (file($filename) as $fli=>$fl) {
               //echo $filename."<br /><br />\n\n";
               if(strpos($fl, $needle)!==false) {
	           echo $filename.' on line '.($fli+1).': '.$fl."<br /><br />\n\n";
               }   
           }
        }
        
    }
    //return $fileList;
    return 0;
}

//var_dump(rsearch($path_to_check,$pattern,$needle));

if (strlen($needle) > 0) {
    rsearch($path_to_check,$pattern,$needle);
}
echo "Search complete.";

The search term currently is entered using the querystring (such as search.php?needle=yoursearchterm), and the path is currently hard coded. The pattern uses a regular expression. I did find that this has the potential to use all of your allotted memory, so use it sparingly. Also, don’t leave this on your site in PHP form, but rename to TXT when not in use so that no one can use it without your knowledge – it could be used to find passwords for databases and other sensitive information.

Incidentally, I did find the code that generates the CSS above; it’s in the wp-content/themes/thesis/lib/core/skin.php file:

echo
	"<style type=\"text/css\">\n",
	"#thesis_launcher { position: fixed; bottom: 0; $position: 0; font: bold 16px/1em \"Helvetica Neue\", Helvetica, Arial, sans-serif; padding: 12px; text-align: center; color: #fff; background: rgba(0,0,0,0.5); text-shadow: 0 1px 1px rgba(0,0,0,0.75); }\n",
	"#thesis_launcher input { font-size: 16px; margin-top: 6px; -webkit-appearance: none; }\n",
	"</style>\n";

Due to the amount of time it would take for me to suss out how to move this into the head without breaking the site, I’m just going to report this one. It should be fixed in the next minor release.

Which is More Important: Newest Data, or Fastest Load Time?

Happy New Year!

As we developers often do, I have recently looked at some code that had aged a bit – more like vinegar and less like wine – and thought, “Did I write that? What was I thinking?” Constantly becoming aware of and implementing best practices, avoiding bad practices, and recovering from failures are all part of growing in many careers, and most certainly must be a part of getting better at software development.

Many of my posts have dealt with Web scraping and using APIs to display profile data from various sites. One question that I’ve never answered is whether or not the data needs to be real-time, or if it can be delayed a bit. The benefits of real-time data are obvious, but the drawbacks can be considerable.

For example, on this blog: until recently, every time the blog loaded, the profile data from several sites was retrieved from these sites on each request. This caused the page to load slowly (aside from my other performance issues), and it’s entirely possible that these other sites would have stopped allowing me to query them, had my traffic been very heavy.

Since it’s far from critical that this profile data be less than a day old, and since I would like my blog to load as quickly as possible, I decided to refactor and then enhance the PHP code I had written to pull this profile data. The new code would store the profile data on my site, and reload the data only if it was older than a given number of days.

YSlow Chrome Extension

Use YSlow to diagnose performance issues on your website.

This data could be stored either as text in a file, or as a value in a database. In this case, I stored it in a text file.

/* initialize variables */
$filename = "whatever.txt";
$html = '';   
$norefresh = FALSE;
$days = 1;

/* checks to see if file exists and is current */
if (file_exists($filename)) {
    $stats = stat($filename);
    /* 86400 seconds in one day */
    if ($stats[9] > (time() - (86400 * $days))) {
    	$norefresh = TRUE;
    }
}

/* if $norefresh is still FALSE, file will be created or updated; otherwise, it will be loaded */
if ($norefresh) {
    $html = file_get_contents($filename);
} else {    
    /* do whatever needs to be done to build the $html variable */
    // ...
    // ...
    // ...

    /* put the $html value into the file */
    file_put_contents($filename, $html);
}

/* display the $html variable contents */
echo $html;

The above code will check to see if a file with the expected data exists, and if so, whether it is new enough – in this case, less than one day old. If not, the data is retrieved and stored in a file for future use. Lastly, the data – whether cached or newly retrieved – is displayed.

Selecting Specific Data with the JSON Selector Generator

Last post of the year, I promise! Something I forgot to mention in yesterday’s post: I found a great tool for finding the correct JSON selector!

The JSON Selector Generator allows you to put in a JSON string, and it parses it such that you can then select which block or item you wish to reference in your code. When your JSON string is small and well-formatted, it’s easy to see what selector to use without a tool like this. However, if an API returns a JSON string that fills your screen, eyeballing it is not likely to get you anywhere quickly.

Processing the long JSON string returned by the call to my Duolingo profile takes only a few seconds with this great tool.

The JSON Selector Generator at work!

The JSON Selector Generator at work!