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;

How to update Jailbroken iPhone to 4.3.3 [solved]

After having some problems with my home button, I tried taking my iPhone to the Apple store, who told me it would cost $200 to replace the hardware button on my iPhone. They also mentioned there were some home button issues with 4.3.1, the version of my iPhone OS, and to try updating. (They also said they had to mention the jailbreak could be my problem and to remove it).

I went home, determined to either fix the home button myself (they cost $5 online) or try a software update. I tried updating to 4.3.3 first, the latest untethered jailbreak. This is trickier now that 4.3.5 is out.

First, I ran AptBackup, a package you can find in Cydia to save all of your jailbroken apps for restoring later. (Edit: AptBackup deleted all my AppStore apps, people recommend PkgBackup instead.) I also saved my SHSH blobs in Cydia (important) when it prompted me, so that I could upgrade or downgrade my iOS freely.

I downloaded iOS 4.3.3 from here and tried option-updating or option-restoring in Itunes, but it gave me the error “The device isn’t eligible for the requested build.” I learned this is because Apple stopped signing iOS 4.3.3 in an effort to prevent jailbreaking.

I followed Saurik’s old guide, adding the line “74.208.10.249 gs.apple.com” to my /etc/hosts file to have Saurik’s Cydia server spoof apple’s verification servers. This bypasses the “eligible” error in iTunes, but as described, at the end of the restore I got the error message The iPhone “iPhone” could not be restored. An unknown error occured (1015)” Saurik writes that you should restore twice, but that you will get this message both times and get stuck in recovery mode, and then you should jailbreak.

Then, I tried using the latest version of RedSn0w to jailbreak. RedSn0w requires you to enter DFU mode by turning off the phone, but in recovery mode the phone turns on automatically when it’s plugged into USB. Regardless, I was able to enter DFU mode by holding down power for 2 seconds, pressing the power and home button for 10 seconds, and then releasing the power button and holding home for 10 seconds. Then, the phone hung on the “Installing Activation Bundles” screen and then rebooted and was stuck on the recovery screen.

I tried the application “fixrecovery43” but it didn’t work. (I got the error: libusb:error [darwin_transfer_status] transfer error: timed out)

What did work for me was installing TinyUmbrella and clicking the “Exit Recovery” option. TinyUmbrella crashed a few times on me, I think because of conflicts with Apple’s httpd server. I had to disable websharing and type “sudo killall httpd” in Terminal first. Then I clicked TinyUmbrella’s “Start TSS Server” and then it ran better. Open up /Applications/Utilities/Console.app for more details on what’s happening if you’re having trouble with TinyUmbrella. Evidentially you can use their TSS servers to restore the phone via iTunes as well but it didn’t work for me (I got a “could not connect to apple servers” error in iTunes).

Note: TinyUmbrella edits your hosts file and deletes everything except the gs.apple.com line. If you have other lines in your hosts file I suggest backing it up.

First I tried DFU mode, but the “fix restore” button didn’t work (the phone got stuck on a greenpois0n screen with the message “unable to find gbdevlist” and “unable to find fs_mount”). What worked for me was leaving the iPhone in recovery mode (iPhone screen on, usb cable icon pointing to iTunes icon), and then it shows up in the list of devices in recovery mode. Then I simply clicked on “exit recovery” and my phone booted normally into 4.3.3. Whew!

Finally, I reinstalled AptBackup and clicked “restore”, which restored all of my Cydia apps, but deleted all of my appstore apps. None of the suggested online fixes (installing an appstore app, resyncing with iTunes, turning on and off restrictions) worked. People online recommend PkgBackup instead of AptBackup, note to self. Anyway I restored my apps and settings from backup again using my old backup in iTunes and all is good. So far so good with the home button. If your iPhone 4 home button is still on the fritz, there are some good voodoo tricks in the comments here.

Control Headphone and Speaker Volume Independently on a MOTU [solved]

I have successfully extended the life of my MOTU 896 audio interface. This solution required buying a new product, but I am so happy with the result that I wanted to post about it anyway.

I had two problems with the MOTU. One was that the volume knob is on the unit which I had to keep nearby my seat at all times, which even with a custom rack makes my room layout cluttered and makes me reach awkwardly for it to adjust the volume. The pot was also dirty and created scratching noise when I turned the knob.

The other problem was that if I wanted to record microphone audio with headphones, the volume knob would control both the headphone levels and the active monitors, and I would have to turn off my speakers in order to set the levels in the headphones to prevent track bleeding and feedback. This was a major pain.

I found a solution by getting the TC Electronic Level Pilot. At $80 it’s fairly pricey but it’s much cheaper than getting a new audio interface or outboard mixer, and I’m quite happy with it. It’s a high-resolution analog volume control with two XLR inputs and two XLR outputs.

The speakers are still going through MAIN OUT, but the signal runs through this new volume knob first. I switched the rocker switch next to the volume knob from “Main Out & Phones” to “Phones Only”. This causes you to have no volume control through your speakers and a line level signal blaring through them, but now with the Level Pilot I can set the level on the phones with one volume knob and the speaker levels with the pilot.

The knob sits happily next to my keyboard for easy control, and I’ve rearranged my whole studio now that the MOTU rack doesn’t have to sit right next to me. I’m very happy with the Level Pilot. It feels very durable and I have a much finer control of audio levels. My only complaint is that sometimes the base pivots on some surfaces making it harder to control. Placing it on a wood surface or putting some double stick tape underneath it solves this problem.

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.

Cyberduck pauses when dragging files to upload [solved]

I’m on a speed-up-my-computer kick today.
I’ve generally used Cyberduck as my main FTP client, but switched to Transmit and FireFTP when it became too sluggish. It has been sluggish for several years now, though I still use it for advanced operations such as right-click file permissions (with the Info menu).
I finally did enough googling to solve its sluggishness: In the transfers window, select all and click Remove. Unless you need a detailed history of everything you’ve downloaded, this window is fairly useless and is the cause of Cyberduck’s sluggishness.
I found this solution via this thread.

Firefox is sluggish and streaming video is choppy [solved]

I got a brand new 2010 8-core mac pro with 12 GB ram, and was horribly disappointed to find that streaming video on Hulu and Netflix still stuttered. Why is Apple’s second fastest machine worse than my iPhone at playing Netflix video? Flash is an infamous hog on mac, but Firefox in general was slow when hovering over links and clicking on things. Netflix uses silverlight too, so it’s bizarre that it would also stutter.
Running Netflix and Hulu in Safari worked fine, but I missed features in Firefox. So in Firefox I went into Tools > Add-ons, disabled everything and restarted. Everything was zippy again. Then I slowly enabled add-ons one at a time.
The culprit seems to be Firebug and the enormous cache it keeps. For now, I have disabled Firebug, and only enable it when developing.

SMSDaemon crash causes screen to wake up and hard drive to constantly access [Solved]

I have been having issues with my Mac screen waking up in the middle of the night for some years now. I think I may have finally solved it. Many sites have suggested that a faulty USB bus or oversensitive USB mouse could cause the wakeup, but I ruled that out when I migrated my OS to a new mac with new hardware. It had to be software related.
My solution is probably very uncommon, but perhaps the diagnostic steps I took would be useful to others.
I woke up at 5:57 AM to my mac screen turning on and my hard drive accessing constantly. I’ve written a script that can sleep my display from my phone, but instead of doing that I got up and started investigating.
I launched Console (Applications/Utilities) and took a look at the Console Messages log. At 5:52 AM there was a ReportCrash that said “Saved crash report for SMSDaemon” and a path to the crash file.
I googled “SMSDaemon OS X crash” and came across a macrumors thread that explained that it is an open source daemon installed by the Syphone utility, a piece of software that used to help get text messages off your iphone but is no longer supported. That thread didn’t have a lot of info on how to remove syphone though, so a google search of “uninstall syphone” lead me to this page, which has the following instructions:

1) Launch Syphone, open the Preferences, and UNcheck “Run helper app in the background” (leave it unchecked if it already is).

2) Close Syphone.

3) Go into ~/Library/Application Support/ and delete the folder named ‘Syphone’.

4) Go into ~/Library/Preferences/ and delete the file named ‘com.micromat.Syphone.plist’.

The Syphone application now just displays a link to techtool pro, but you can still access the preferences menu and turn off the Daemon. If you can’t run Syphone for some reason, the following commands will unload the daemon and delete it:

$ launchctl unload ~/Library/LaunchAgents/com.micromat.SMSDaemon.plist
$ rm ~/Library/LaunchAgents/com.micromat.SMSDaemon.plist

Apparently SMSDaemon may have had an enormous memory leak; I’ve come to my computer with 1.5 TB free and yet gotten the “there is no more disk space on the startup disk to write, please force quit some applications” errors before. Others have complained of memory leaks too; my machine seems to be running better now.

Windows 7 – “setup was unable to create a new system partition or locate an existing system partition” [solved]

When trying to install Windows 7 to dualboot with Snow Leopard on an existing partition on a Dell Mini 10v, I kept getting this error:

“Setup was unable to create a new system partition or locate an existing system partition.”

No matter how many times I deleted, created, and reformatted the partition using Windows’ installer or OS X’s disk utility, Windows 7 said it could not install to Partition 0.

I also got the error “Windows cannot install to a GPT partition”, and when I tried converting it to MBR in Diskpart, the error “Cannot convert partition”.

The solution was really, really, really stupid:

In my BIOS, I had my boot order set as USB drive before hard drive so I could boot from it. Apparently, that confused the installer. I set the hard drive as the first boot device, and then selected to boot from USB by hitting F12 when the POST screen appeared.

Windows is almost 30 years old, and it still has these types of problems? Really, Microsoft?

« Prev - Next »