PHP: Detect if Script is Running from Console

I hacked together a script to import a large amount of data from a CSV file that was not very fun to work with, and I felt the need to restrict access to the command line in case there was any extra overhead in the script, or anything unforeseen quirks from running such an intensive script in the browser. (I didn’t really have to restrict access as I removed the import script after it was run, but now I needed to know.) Somehow, I needed to capture information about exactly HOW the script was being executed. It’s actually quite simple, PHP captures your console environment into a $_SERVER variable, specifically $_SERVER['CONSOLE'] and/or $_SERVER['TERM'].

if(!isset($_SERVER['SHELL'])) { echo "Please run this script from the console."; exit; }

[Edit: Shortly after posting the above snippet, I found a PHP function (see: php_sapi_name) that returns the type of interface between the web server and PHP. In this case, we are looking for it to return the value 'cli' for command line interface.]

if(php_sapi_name() != 'cli') {  echo "Please run this script from the console."; exit; }

The Hacker’s Code

“A hacker of the Old Code.”

  • Hackers come and go, but a great hack is forever.
  • Public goods belong to the public.*
  • Software hoarding is evil. Software does the greatest good given to the greatest number.
  • Don’t be evil.
  • Sourceless software sucks.
  • People have rights. Organizations live on sufferance.
  • Governments are organizations.
  • If it is wrong when citizens do it, it is wrong when governments do it.
  • Information wants to be free. Information deserves to be free.
  • Being legal doesn’t make it right.
  • Being illegal doesn’t make it wrong.
  • Subverting tyranny is the highest duty.
  • Trust your technolust!

* Definition: A good is public if the marginal production cost is lower than the marginal billing cost.

Must Have List of Android Applications

I recently purchased the Motorola Droid X (the day after release, I actually snagged the last one in my area, w00t) and squandered much time in the Google Market, the Android applications store. Here’s my results after downloading tons of apps, from project / time management apps to soundboard, and of course, the Star Wars lightsaber app. Click on the names to view the QR Code that is recognized by the Barcode Scanner application.

Fun / Useless Applications

  • Air Horn
    Use this application to get someone’s attention or annoy them, tons of effects to choose from: vuvuzella, loudspeaker, air horn, and more!
  • Ethereal Dialpad
    Random effects processor / synthesizer thingy, really fun to play with when you’re bored or drunk.
  • Wilhelm Scream
    You know that scream that has been used over and over in film? You can make your phone scream just like that, every time you throw it up in the air!

Localized / Nightlife Applications

  • FourSquare
    Check into your FourSquare account, and find out where your friends are at!
  • HappyHours
    Exactly like it sounds, an app that helps you find the closest bar that is serving drinks at happy hour prices.
  • Wertago
    Find out what’s going on tonight, who’s hanging out at what bar, club, or event, and check reviews of the place (even how hot it is or how much it sucks right now) before you go!

Music Applications

  • AudioBox.fm
    Upload your MP3s from home, play them anywhere on your phone!
  • Pandora
    Must have for any smart phone nowadays, stream radio from the cloud!

File Management

  • FTPServer
    Create an FTP server accessible via internet or intranet to share files to and from your device.
  • Twonky Server
    Create a network accessible share to transfer files to and from your device.

Game Applications

  • Oregon Trail
    Remake of the old classic, it feels a little limited, but it’s still fun!
  • SNESoid
    SNES emulator, and it’s the best one, to say the least.

Blogging Applications

  • WordPress
    Manage your WordPress blogs anywhere, as long as you brought your phone!
  • WP Stats
    Keep a close eye on your WordPress blog statistics any time, anywhere!

Geeky Applications

  • Spot Message
    Leave a message for another person based on GPS location. When they get to the intended destination, they will receive your message.
  • IRSSI ConnectBot
    Must have for geeks, store and run ssh commands, also includes a text based IRC client.
  • PixelPipe
    Post media or blog to over 110+ social networking services, or your own hosted WordPress platform.
  • Torrent-Fu
    Remotely manage your uTorrent or Transmission bittorrent clients.

Resizing DIVs On Browser Load And Resize

I will take Jelo over jQuery any day, but I had to tread in murky waters today making an update to a theme on one of my WordPress blogs.

I had a three column layout, the two sidebars were floated to the right properly, but the text in my main content div was going under my sidebars, to the full 100% width of the page. I couldn’t find a quick CSS solution without rewriting the structure of the page, so I decided to use jQuery, which was already loaded to handle some animations in the menu.

$(document).ready(function() {
  $('#content').css("width", ($(window).width() - $('#sidebar1').width() - $('#sidebar2').width()) + 'px');
});
$(window).bind('resize', function () {
  $('#content').css("width", ($(window).width() - $('#sidebar1').width() - $('#sidebar2').width()) + 'px');
});

Hope this was helpful to some one in some fashion!

Windows 7 Dream Scene Activator

Some of you might remember the “Dream Scene” feature that was available in versions of Windows Vista, where you would be able to set a video in WMV/MPG format as your desktop background. Well, sadly, this feature was eighty-six’ed in Windows 7. (Well, not really, because after a registry tweak, it becomes available again.)

However, Kishan Bagaria of The Windows Club has released a nifty little application that changes a registry value to re-enable this feature in Windows 7. Once you’ve applied this little patch, right click on any WMV/MPG and you will be able to “Set as desktop wallpaper …” from the context menu.

Don’t be confused by the title on the window of this application, it works for Windows 7 64 bit as well!

If you are looking for some neat looped videos to use as your wallpaper, or inspirational ones to make your own, look no further than DeviantART!

Download Windows 7 Dream Scene Activator

Snow Leopard Command Line Hacks

Here are my favorite command line hacks that enable special features or views in OS X 10.6 Snow Leopard.

  • Enable Quick Look Folder Previews
    defaults write com.apple.finder QLEnableXRayFolders 1
    Once you’ve entered this command, restart Finder (killall finder or ctrl+option+click Finder Dock Icon, and Relaunch), select a folder, and hit the space bar.
  • Enable AppleScript Studio Palette
    defaults write com.apple.InterfaceBuilder3 IBEnableAppleScriptStudioSupport -bool YES
    OS X 10.6 Snow Leopard allows you to work on AppleScript Studio Projects, but you can’t create new ones. Run the above command to add it back into Interface Builder.
  • Disable Focus Follows Mouse
    defaults write com.apple.Terminal FocusFollowsMouse -string NO
  • Confine Dictionary To One Window
    defaults write com.apple.Dictionary ProhibitNewWindowForRequest -bool TRUE
  • Change iTunes Green Maximize Button Functionality
    defaults write com.apple.iTunes zoom-to-window -bool YES
    During the initial launch of iTunes 9, the green + icon on the window bar maximized the iTunes window instead of alternating to the mini player. Use this command line hack to enable this functionality once again.
  • Automatically Play Movies in QuickTime X
    defaults write com.apple.QuickTimePlayerX MGPlayMovieOnOpen 1
  • Keep QuickTime Full Screen While Switching Applications
    defaults write com.apple.QuickTimePlayerX MGFullScreenExitOnAppSwitch 0
  • Disable QuickTime Rounded Corners
    defaults write com.apple.QuickTimePlayerX MGCinematicWindowDebugForceNoRoundedCorners 1
  • Automatically Show Closed Captioning or Subtitles in QuickTime
    defaults write com.apple.QuickTimePlayerX MGEnableCCAndSubtitlesOnOpen 1
  • Make List View Stacks Function Like Grid View Stacks
    defaults write com.apple.dock use-new-list-stack -bool YES
  • Enable MouseOver Highlight in Stacks
    defaults write com.apple.dock mouse-over-hilite-stack -boolean yes
    Once typing in this command, restart Dock (killall dock) to view changes.

Create a Text File of Directory Contents

If you’ve ever needed to get a list of files in a specific directory for whatever purpose, maybe you need to verify files during a backup or transfer, you probably hunted for a good freeware application to do the trick. Sure, there are good options out there, but why find a download for something you can just as easily accomplish from the command line?

Running the following command will output a list of the contents of the current directory to your Desktop. Just replace YOU with your Windows username.

dir /b *. > C:\Users\YOU\Desktop\musicfiles.txt

It’s that easy!

Enable Stacks Mouseover Highlight (OSX 10.6)

Enabling automatic mouseover highlighting of icons in your grid stacks makes browsing for files that much easier, and it’s just a simple command away. It’s been changed from the original command in Leopard, so after opening Terminal, run these commands:

  1. defaults write com.apple.dock mouse-over-hilite-stack -boolean yes
  2. killall dock

When the dock restarts, you can check out your fancy new highlight by clicking on your grid stack’s icon!

Disable SSH Known Host Checking

We’ve all ran into this annoying message when using ssh to connect to more than one computer behind the same IP address.

Offending key in /root/.ssh/known_hosts

And we all know that deleting the offending line in that file will solve our problems, but doing it every time is such a hassle, and if you need to do this on a constant basis, you really can’t be doing this every time. Well, a quick change to your SSH’s configuration file will do the trick!

Open the file /etc/ssh/ssh_config in your favorite text editor, and add the following two lines below Host * in the configuration file.

StrictHostKeyChecking no
UserKnownHostsFile /dev/null

This will disable strict host checking, and automatically write the known hosts to a black hole. You will never be disturbed by that prompt again!

NOTE: If your network security isn’t extrapolated from this process somehow, you probably don’t want to disable this check.

Windows 7 Super User Guide

We’ve all seen the countless top 10 articles, shortcut guides, etc out for Windows 7, but what really separates this new release from the rest of the Windows herd? Listed here are several features I believe sets Windows 7 above and beyond all previous releases.

Display Projection Management
Along with the standard Presentation Mode that was already available in Windows Vista, you can now control the output settings of your secondary display with a few keystrokes! Just hit WINDOWS KEY + P to bring up the Display Projection dialog and choose whether you would like to duplicate or extend your display, or only show output on your main or secondary display. AWESOME!

Better Font Management
No longer is it a complete pain in the ass and total waste of time to preview and install fonts. Microsoft has integrated an Install button right into the preview interface, so now you can add fonts to your library with one click if you like them, instead of navigating every time to your fonts directory and dragging and dropping the files over. Microsoft also grouped different weight classes for the same fonts into the same preview window for faster comparison, as well as adding support for more than the four standard weights in the “Font Style” dialogs.

Better Standards Support
Windows 7 includes bigger, better, and beefier versions of our age old favorites, Paint and Wordpad. What most articles fail to mention is that Wordpad is capable of reading and saving documents in both the new Word 2007 Office Open XML and the OpenDocument Specification that IBM and Sun have been touting about.

Better Media Support
Windows 7 plays nice with most video codecs right out of the box. Yeah, you can just double click a movie file and it will open in WINDOWS MEDIA PLAYER. I’m not joking! Certain videos still require downloading the appropriate codecs, but you can install Shark007′s Windows 7 Codec Pack and never think twice. Not only that, but you can also burn ISO files right in Windows Explorer, just double click your ISO file, insert a blank disc, and click Burn!

Power User Hot Keys

  • WINDOWS KEY + HOME
    Minimize all other windows behind active window.
  • WINDOWS KEY + SHIFT + (LEFT OR RIGHT)
    Shift active window to the desktop to the left or right of active desktop.