Programming

Deploy data files only to new website with rsync [solved]

I wanted to copy everything from my git repo to production except images and media. This tutorial helped me out. This command will copy all js, php, html, and css files and directory structure without copying images or other media:

rsync -av --include='*/' --include='*.php' --include='*.html' --include='*.js' --include='*.css' --exclude='*' source/ destination/

This will work in linux, unix, or os x.

PHP DOMDocument stops render on errors

Several of PHP’s DomDocument functions halt rendering the page on any kind of error, and are finicky to test. I was parsing an RSS feed and wanted to display a scaled down version of the image in the feed. This code worked if the code contained an image:
$rss = @file_get_contents('http://problemstosolve.com/?feed=rss2', false);
if($rss === false)
{
$feed = false;
}else
{
$feed = mb_convert_encoding($rss, "HTML-ENTITIES", "UTF-8");
}
$result = new SimpleXMLElement($feed);
foreach($result->channel->item as $item)
{
$content = $item->children('http://purl.org/rss/1.0/modules/content/');
$dom = new DOMDocument();
$dom->loadHTML($content->encoded);
$images = $dom->getElementsByTagName('img');
echo "<img src=\"" . $images->item(0)->getAttribute('src') . "\" width=\"100\" /><br/>";
}

However, if the post didn’t contain an image, the whole page would blow up with no PHP error. To make things worse, if the problem was in one of several codeigniter views, it would make codeigniter display the views out of order. None of these tests for images worked:

//didn't work
if(!$images)
//didn't work
if(!empty($images))
//didn't work
if(!empty($images->hasAttribute('src')))
//didn't work
if(!empty($images->item(0)))
//didn't work
if(!$images->children)
//didn't work
if($images->length() == 0)

What finally worked was:
if($images->length == 0)

getElementsByTagName always returns a DOMNodeList and it’s hard to check to see if it actually contains anything. I haven’t figured out a way for PHP to handle errors better with DOMDocument.

Don’t Run Two Instances of TextWrangler at Once [Solved]

Update:Barebones support got back to me and explained why this wasn’t working, this method broke their codesigning. But they had a solution: Simply go to File > New > FTP/SFTP Browser. Thanks Barebones!


Edit: Darn, I’m getting this error when trying to open remote documents: (MacOS Error code: -25293)

When I’m working on two projects at once, or want to grab a remote file from FTP when I already have one FTP site open, it would be nice to run two instances of TextWrangler at once, since it only supports having one FTP site open at a time.

I figured out how:

-Go to Applications/ and right click on TextWrangler and select Duplicate.
-Rename Textwrangler copy to Textwrangler2
-Right click on Textwrangler and choose “Show Package Contents”
-Open info.plist inside the Contents folder
-Change the data from

	<key>CFBundleIdentifier</key>
	<string>com.barebones.textwrangler</string>



to

	<key>CFBundleIdentifier<</key>
	<string>com.barebones.textwrangler2</string>



Now you can open both TextWrangler apps.


Optionally, grab this snazzy textwrangler alternative icon from this page:
http://sethlilly.deviantart.com/art/TextWrangler-Replacement-Icon-180898652
Right click and choose "copy image". Then get info on TextWrangler2, click on the small icon in the top right, and hit command-V to paste. That way it's easy to tell the two instances apart in the Dock and switching between applications.

MySql Avg() doesn’t work with enums or sets

I had a very simple votes table with a rating column which was defined as a SET(’1′,’2′,’3′,’4′,’5′). This was meant to be easy to rate items from 1 to 5 stars.
Then I had the following values:

item_id rating
1 3
1 4
1 4
1 5

When I ran SELECT AVG(‘rating’) FROM votes WHERE item_id = 1, I would mysteriously get ’9′ instead of ’4′.

It turned out that ENUM and SETs return the index of the ratings, in powers of 2 for some reason, instead of the rating values themselves. So SELECT item_id, rating, CONV(rating, UNSIGNED) produced this:
item_id rating CONV(rating, unsigned)
1 3 4
1 4 8
1 4 8
1 5 16

So the average of the indexes of the set would be 9.

Instead I had to convert the set to a string, then convert the string to an unsigned INT to get the results I wanted:
SELECT rating, AVG( CONVERT( CONVERT( rating, CHAR ) , UNSIGNED ) ) FROM votes WHERE item_id = 1;

Create an encrypted folder with OS X Disk Utility [Solved]

I run several servers on my mac, and worry about getting hacked or accidentally sharing sensitive files. In addition, I worry about sensitive files when I take the computer in for tech support, or when others use my computer. Documents such as my tax files contain my social security number.

I don’t have to worry any longer (until AES is cracked, anyway), because it is easy to create an encrypted disk image on OS X. I followed the instructions here:

http://blog.smalldog.com/article/1814/use-disk-utility-to-encrypt-files/

I chose a 2.5 GB image, which automatically suggested a CD/DVD type partition. I’m not sure what partition type to use but don’t think it matters. These were the settings I used:

This will create a .sparseimage file. Sparse images are nice because they don’t actually take up their entire hard drive space until they’re filled with contents.

Note that after you drag the files in, you still have to delete the originals. Then when you eject the encrypted image, they are protected. When you double click on the image again, it will ask you for your password. Don’t forget this password, because it is impossible to recover!

I used spotlight to search my computer for files containing my passwords or social security number. If they were necessary, I dragged them in here, otherwise I deleted them.

Get Growl Notifications on VNC connections, SSH attempts, and HTTP logs [solved]

Use this script to monitor VNC connections with Vine Server:
nice -n 100 tail -n 0 -f /Users/jordan/Library/Logs/VineServer.log | php -r 'while ($m = fgets(STDIN)) shell_exec("growlnotify -p 0 Vine Server -m ". escapeshellarg($m));'

Change “jordan” to your username (home directory), or the whole path of the log if you’re monitoring something else. “Vine Server” is the alert title, which could be anything (“Hello World”).

Save this as a file such as vncnotify.sh. Run
chmod 755 vncnotify.sh
and then you can run the command by typing ./vinenotify.sh.

Duplicate this file and name it vinenotify.command.
Finally, in System Preferences > Accounts > Start up items, add vinenotify.command, and make it hidden, and it will run when you log in.

Things I’m still working on: preventing a string of notifications when you launch VNC; hiding the terminal window when you log in; and grepping for only connect/disconnect/login failures.
Thanks to drewish.com for the growl tail code.

Check for open logs running on OS X, unix or linux [solved]

Use this command to find the log you’re looking for:
lsof | grep -i log

On OS X, typical directories I look in are /var/log/, $HOME/Library/Logs/, and /private/var/log/.

Make Google Maps actually geo locate you in Firefox 3.5 [soved]

One of the most exciting features about Firefox 3.5 is its geo-location capabilities. But when I installed 3.5 and went to google maps, it didn’t automatically geo-locate me nor was there any option to.
This blog has a bookmarklet you can place on your toolbar to click to geo-locate you in google maps. Create a new toolbar bookmark, edit it, rename it “locate me” and paste in this code for the address:

javascript:function sv(s){document.querySelector("#q_d").value=s};sv("Checking...");navigator.geolocation.getCurrentPosition(function(a){c=a.coords;sv(c.latitude+"%20"+c.longitude);document.forms.q_form.submit();},function(){sv("Rejected!")});

Then when you go to google maps and click “locate me”, you can search for businesses near you wherever you are.

Thanks, John Ath!

Choose a random winner or randomize a list with terminal and PHP [solved]

I wrote this for my class, which needed to have a randomized order of presentations:

php -r '$order = array("person1","person2","person3","person4","person5","person6","person7","person8","person9","person10");shuffle($order);foreach($order as $number=>$name){echo $number+1 . " $name\n";}'

To run it, open up terminal (in OS X) and paste it in.

I’ve also had to choose a random winner for gift certificates before:
php -r '$order = array("person1","person2","person3","person4","person5","person6","person7","person8","person9","person10");shuffle($order);echo "$order[0]\n";'

Google sort by date bookmarklet [solved]

Based on this URL hack to sort google results by date, I made a bookmarklet:
javascript:location.href =location.href + '&as_qdr=d';
Now if you google search and then click this button, it will sort your results by the newest date. Works in FF and Safari.

Next »