Facebook-Status-2.0 announced!

by Matt Danger on February 10, 2009

I have updated my original Facebook status script to work with Facebook 2.0.

Why is this interesting? Until recently the Facebook Developer Platform didn’t allow the changing of user status messages. This does and can be implemented in other utilities, which I may do in the future. Enjoy!

Leave a Comment :, , , more...

Scripting Login Tasks in Mac OS X

by Matt Danger on January 21, 2009

Recently I wanted to configure some of our lab computers to automatically configure printers when a user logged on. While reading Apple’s documentation to find a solution I learned about the possible ways to script login procedures in Mac OS X 10.5.

Login Hooks

The first method of scripting a login action is the use of a Login Hook. A Login Hook is a script that is executed immediately after the user logs on but before other login processes are performed. The script runs as root and needs to be chmod’d +x to be made executable. This feature has existed in OS X for some time and is handy for some account specific tasks.

To configure OS X to run your script run the following command as root in Terminal:

defaults write com.apple.loginwindow LoginHook /path/to/script 

This modifies root’s login window properties in /var/root/Library/Preferences/. In Leopard login hooks do not work in any other user or system-wide com.apple.loginwindow preference file. I’m not sure if they did in previous versions.

More information about LoginHooks can be found on Apple’s developer site.

System Login Items (Deprecated)

SystemLoginItems was an old login script mechanism that has now been deprecated and will be removed in Snow Leopard. Scripts were executed when the Login Window was displayed and ran as root. When a user logged in the script died and was executed again as the user.

In Leopard SystemLoginItems behaves oddly. Applications do not launch upon login, instead they launch when the user logs out and attempt to run behind the Login Window.

Example usage of SystemLoginItems:

defaults write /Library/Preferences/com.apple.SystemLoginItems AutoLaunchedApplicationDictionary -array-add '{ "Path" = "/path/to/script"; "Hide" = 0; }'

SystemLoginItems was not well documented by Apple and it’s believed Apple created them specifically for a few developers. Now that Launchd exists SystemLoginItems is no longer necessary, likely the reason it is to be removed.

A brief discussion of SystemLoginItems can be found in this MacEnterprise thread.

Global Login Items

GlobalLoginItems has superseded SystemLoginItems, and in some cases LoginHooks, as the best method for launching a login script. Unlike LoginHooks, a Global Login Item executes after all the login processes have completed and is meant for GUI applications. You can even configure whether you would like the application’s GUI to appear or not.

To configure a Global Login Item run the following command as root:

defaults write /Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -array-add '{ "Path" = "/path/to/script"; "Hide" = "0"; }'

To remove the global login item either delete /Library/Preferences/loginwindow or run the following command as root:

defaults delete /Library/Preferences/loginwindow AutoLaunchedApplicationDictionary

Note that we are modifying the loginwindow.plist file and not the more popular com.apple.loginwindow.plist file. A reboot may also be required after deleting a global login item

More information about Login Items can be found in this Apple Technical Note.

Leave a Comment :, , , , , , , more...

Sudo without a password in Mac OS X

by Matt Danger on January 12, 2009

Mac OS X users who spend a lot of time in Terminal may find having to enter their account passwords when using sudo to be inconvenient. A few simple steps can disable this and allow you to use sudo without ever needing a password in Mac OS X 10.5 Leopard. The steps are similar for 10.3 and 10.4 however /etc/sudoers may be slightly different.

Warning! Be aware that removing the password requirement to use sudo eliminates a level of security. If someone gains access to your account they will be able to easily escalate to root privileges.

First, edit /etc/sudoers and uncomment (remove the “#”)

# %wheel ALL=(ALL) NOPASSWD: ALL

Then, in Terminal run the following command as root, replacing your_username with your account username:

dscl . append /Groups/wheel GroupMembership your_username

Leave a Comment more...

Adjust Mac OS X display brightness from the Terminal

by Matt Danger on December 17, 2008

One of the classrooms I maintain is comprised of 2.4Ghz iMacs (MB323LL/A) that are dual booted with Windows XP and Mac OS X. Sometimes when the workstations are restarted from XP to OS X the display brightness gets changed to its dim power saver setting. This requires the user manually increase the display brightness by pressing the F2 key, which is inconvenient. I haven’t been able to determine the cause of this issue so I wanted to create a login hook to increases the brightness as a temporary fix.

OS X doesn’t ship with a command line utility that let’s you change display brightness. Fortunately, Nicholas Riley has written a utility that uses OS X frameworks to accomplish this quickly and easily. Here’s the code:

#include <stdio.h>
#include <unistd.h>
#include <IOKit/graphics/IOGraphicsLib.h>
#include <ApplicationServices/ApplicationServices.h>
 
const int kMaxDisplays = 16;
const CFStringRef kDisplayBrightness = CFSTR(kIODisplayBrightnessKey);
const char *APP_NAME;
 
void errexit(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  fprintf(stderr, "%s: ", APP_NAME);
  vfprintf(stderr, fmt, ap);
  fprintf(stderr, "\n");
  exit(1);
}
 
void usage() {
  fprintf(stderr, "usage: %s [-m|-d display] [-v] <brightness>\n", APP_NAME);
  fprintf(stderr, "   or: %s -l [-v]\n", APP_NAME);
  exit(1);
}
 
int main(int argc, char * const argv[]) {
  APP_NAME = argv[0];
  if (argc == 1)
    usage();
 
  int verbose = 0;
  unsigned long displayToSet = 0;
  enum { ACTION_LIST, ACTION_SET_ALL, ACTION_SET_ONE } action = ACTION_SET_ALL;
  extern char *optarg;
  extern int optind;
  int ch;
 
  while ( (ch = getopt(argc, argv, "lmvd:")) != -1) {
    switch (ch) {
    case 'l':
      if (action == ACTION_SET_ONE) usage();
      action = ACTION_LIST;
      break;
    case 'v':
      verbose = 1;
      break;
    case 'm':
      if (action != ACTION_SET_ALL) usage();
      action = ACTION_SET_ONE;
      displayToSet = (unsigned long)CGMainDisplayID();
      break;
    case 'd':
      if (action != ACTION_SET_ALL) usage();
      action = ACTION_SET_ONE;
      errno = 0;
      displayToSet = strtoul(optarg, NULL, 0);
      if (errno == EINVAL || errno == ERANGE)
	errexit("display must be an integer index (0) or a hexadecimal ID (0x4270a80)");
      break;
    default: usage();
    }
  }
 
  argc -= optind;
  argv += optind;
 
  float brightness;
  if (action == ACTION_LIST) {
    if (argc > 0) usage();
  } else {
    if (argc != 1) usage();
 
    errno = 0;
    brightness = strtof(argv[0], NULL);
    if (errno == ERANGE)
      usage();
    if (brightness < 0 || brightness > 1)
      errexit("brightness must be between 0 and 1");
  }
 
  CGDirectDisplayID display[kMaxDisplays];
  CGDisplayCount numDisplays;
  CGDisplayErr err;
  err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);
  if (err != CGDisplayNoErr)
    errexit("cannot get list of displays (error %d)\n", err);
 
  CFWriteStreamRef stdoutStream = NULL;
  if (verbose) {
    CFURLRef devStdout =
      CFURLCreateWithFileSystemPath(NULL, CFSTR("/dev/stdout"),
				    kCFURLPOSIXPathStyle, false);
    stdoutStream = CFWriteStreamCreateWithFile(NULL, devStdout);
    if (stdoutStream == NULL)
      errexit("cannot create CFWriteStream for /dev/stdout");
    if (!CFWriteStreamOpen(stdoutStream))
      errexit("cannot open CFWriteStream for /dev/stdout");
  }								  
 
  for (CGDisplayCount i = 0; i < numDisplays; ++i) {
    CGDirectDisplayID dspy = display[i];
    CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
    if (originalMode == NULL)
      continue;
 
    if (action == ACTION_LIST) {
      printf("display %d: ", i);
      if (CGMainDisplayID() == dspy)
	printf("main display, ");
      printf("ID 0x%x\n", (unsigned int)dspy);
      if (verbose) {
	CFStringRef error = NULL;
	CFPropertyListWriteToStream(originalMode, stdoutStream,
				    kCFPropertyListXMLFormat_v1_0, &error);
	if (error != NULL)
	  errexit("failed to write display info (%s)",
		  CFStringGetCStringPtr(error,
					CFStringGetFastestEncoding(error)));
      }
    }
 
    io_service_t service = CGDisplayIOServicePort(dspy);
    switch (action) {
    case ACTION_SET_ONE:
      if ((CGDirectDisplayID)displayToSet != dspy && displayToSet != i)
	continue;
    case ACTION_SET_ALL:
      err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness,
				       brightness);
      if (err != kIOReturnSuccess) {
	fprintf(stderr,
		"%s: failed to set brightness of display 0x%x (error %d)",
		APP_NAME, (unsigned int)dspy, err);
	continue;
      }
      if (!verbose) continue;
    case ACTION_LIST:
      err = IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness,
				       &brightness);
      if (err != kIOReturnSuccess) {
	fprintf(stderr,
		"%s: failed to get brightness of display 0x%x (error %d)",
		APP_NAME, (unsigned int)dspy, err);
	continue;
      }
      printf("display %d: brightness %f\n", i, brightness);
    }
  }
 
  return 0;
}

You can compile your own utility by saving this code to a file named brightness.c and doing:

gcc -std=c99 -o brightness brightness.c -framework IOKit -framework ApplicationServices

You can also download the precompiled binary.

You can create a login hook to automatically increase the brightness by chmoding the downloaded ‘brightness’ file to 755 and running the following command as root in Terminal:

defaults write com.apple.loginwindow LoginHook "/path/to/brightness 1"

8 Comments :, , more...

A few Mac OS X tweaks you might not have known about

by Matt Danger on December 15, 2008

I’m a die hard Mac OS X user and also a lab administrator at a college. Over the years I’ve found several tweaks that help me configure both my personal computer and lab workstations to do different tasks.

Force the printer icon to auto quit after a print job completes. Right-click on the icon and select “Auto Quit” or do:
defaults write /Users/yourusername/Library/Preferences/com.apple.print.PrintingPrefs "Quit When Finished" -bool true

Disable disk checksum verification
defaults write com.apple.frameworks.diskimages skip-verify true

Gradient highlights for Stacks
defaults write com.apple.dock mouse-over-hilte-stack -boolean true

Add recent applications as a Stack on Dock
defaults write com.apple.dock persistent-others -array-add '{ "tile-data" = { "list-type" = 1; }; "tile-type" = "recents-tile"; }

Make extended print dialog show by default
defaults write -g PMPrintingExpandedStateForPrint -bool true

Change the current screenshot format
defaults write com.apple.screencapture type jpg

Disable iTunes store arrows
defaults write com.apple.iTunes show-store-arrow-links -bool false

Stop Safari “saved text” dialogs
defaults write com.apple.Safari DebugConfirmTossingUnsubmittedFormText NO

Force Safari to open all links in new tab
defaults write com.apple.Safari TargetedClicksCreateTabs -bool true

Disable application crash dialogs
defaults write com.apple.CrashReporter DialogType none

Make hidden Dock icons transparent
defaults write com.apple.Dock showhidden -bool true

Disable Dashboard
defaults write com.apple.dashboard mcx-disabled -boolean true

Stop disk image verification
defaults write com.apple.frameworks.diskimages skip-verify true

Enable Data Detectors in 10.5 iChat
defaults write com.apple.iChat EnableDataDetectors true

Modify Sheet Animation
defaults write NSGlobalDomain NSWindowResizeTime .001

Login window tweaks

The text underneath the “Mac OS X” title of the login window will change if you click on it. You can configure the login window to display any of these as default. Valid options are HostName, SystemVersion, SystemBuild, SerialNumber, IPAddress, DSStatus, or Time
defaults write /Library/Preferences/com.apple.loginwindow AdminHostInfo Time

You can add a message to the login window. This is useful in a lab environment when you need to give instructions to your users.
defaults read /Library/Preferences/com.apple.loginwindow LoginwindowText "Some custom message."

Leave a Comment :, , more...

Simple Reg-Ex to Validate an Email Address

by Matt Danger on November 24, 2008

Regular expressions can be a pain sometimes. Here’s a code snippet to make your life a little easier when validating email addresses:

$email = 'user@example.com';
if (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $email)) {
    print_error("Your email address is invalid!");
}
Leave a Comment :, , , more...

Update Your Facebook Status from the Command Line

by Matt Danger on November 24, 2008

The Facebook Developer Platform doesn’t give us the ability to set a user’s status so I decided to write my own method that would allow Facebook users to update their status messages without having to log into the website.

The first version that I wrote in 2006 was a little complicated. The script had to connect to several Facebook pages and collect cookies and challenge codes that were necessary to login and update the user’s status.

Facebook changed their login and status update mechanisms in version 2.0 breaking my original script. I’ve rewritten it to work with Facebook 2.0. It is now actually shorter because Facebook uses the user’s homepage as the central information area.

Any feedback is welcome. I plan to implement this in other utilities soon. Enjoy!

Updated on May 6, 2010 to version 2.1

#!/usr/bin/perl -w
 
#===============================================================================
#
# Facebook status script v2.1
#
# This script allows you to update your Facebook status from the shell.
#
# Copyright (C) 2010 Matt West <matt at mattdanger dot net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# For a copy of the GNU General Public License visit <http://www.gnu.org/licenses/>.
#
#===============================================================================
 
# Be sure you have installed the LWP and Crypt::SSLeay packages from CPAN
use LWP;
use HTTP::Cookies;
use Term::ReadKey;
use strict;
 
# General vars
my $login;
my $password;
my $status;
my $auth_key;
my $response;
my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ( 'Referer' => 'http://www.facebook.com/', 
               'User-Agent' => $user_agent);
my $cookie_jar = HTTP::Cookies->new(
                file => 'cookies.dat',
                autosave => 1,
                ignore_discard => 1);
my $browser = LWP::UserAgent->new;
$browser->cookie_jar($cookie_jar);
 
# Get login information & the status message to send.
print "Facebook login name: ";
$login = <>; chomp($login);
 
print "Password: ";
ReadMode('noecho');
$password = ReadLine(0); chomp($password); ReadMode 0;
 
print "\nYour status (Facebook appears to have a 232 character limit): ";
$status = <>; chomp($status);
print "\nSending... ";
 
 
#================================================
# Login and get auth key
#================================================
 
$response = $browser->post('https://login.facebook.com/login.php?m&next=http%3A%2F%2Fm.facebook.com%2Fhome.php', 
                           ['email' => $login,
                            'pass' => $password,
                            'login' => 'Log In'], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
$response = $browser->get('http://m.facebook.com/a/home.php', @header);
 
$auth_key = $response->content;
$auth_key =~ s/\n//g;
$auth_key =~ s/^.*name="post_form_id" value="//;
$auth_key =~ s/".*$//;
 
#================================================
# Submit the status update
#================================================
 
@header = ('Referer' => 'http://m.facebook.com/a/home.php', 
           'User-Agent' => $user_agent, 
           'Host' => 'm.facebook.com');
 
$response = $browser->post('http://m.facebook.com/a/home.php?re974fcaf&refid=7&rbb94a931', 
                           ['fb_dtsg' => 'jYNZj',
                            'post_form_id' => $auth_key,
                            'status' => $status,
                            'update' => 'Share'], @header);
 
# Did we do good here?
if ($response->content eq '') {
  print "Done!\n\n";
} else {
  print "An error occurred while setting your profile status.\n\n";
}
 
# Now that we're done we can delete the cookies.dat file.
exec('rm cookies.dat');
2 Comments :, , more...

Hacking “Share This” to Configure Sharing Options

by Matt Danger on November 24, 2008

Thomas likes to use Alex King’s Share This plugin on his Wordpress install but wanted a little more configurability from it. I wrote a little hack to allow him to select which sharing tab appears by default. This was written for version 1.3 and I’m not sure if it could be successfully implemented into the 2.3 version.

To select the default tab set “DEFAULT_TAB” to either ’social’ or ‘email’ for which tab you’d like to be displayed when Share This is activated. Set “FIRST_TAB” to either ’social’ or ‘email’ for which tab you’d like to appear first in the tab order when Share This is activated.

To begin, add this code at line 26:

@define('DEFAULT_TAB', 'email'); // 'social' or 'email' 
@define('FIRST_TAB', 'email'); // 'social' or 'email' as the first tab.

Then add this code at line 123:

function get_default_tab() {
  switch (DEFAULT_TAB) {
    case 'email':
      $tab_number = 2;
      break;
    case 'social':
      $tab_number = 1;
      break;
    default: die("Error with the Share This plugin: Please define DEFAULT_TAB as either 'social' or 'email'.");  
  }
  echo $tab_number;
}

Then add this code at line 174:

akst_share_tab('<?php get_default_tab(); ?>');

Finally, at line 442 replace:

<li id="akst_tab1" onclick="akst_share_tab('1');"><?php _e('Social Web', 'alexking.org'); ?></li>
<li id="akst_tab2" class="selected" onclick="akst_share_tab('2');"><?php _e('E-mail', 'alexking.org'); ?></li>

with:

<?php 
switch (FIRST_TAB) {
  case 'social':
?>
    <li id="akst_tab1" onclick="akst_share_tab('1');"><?php _e('Social Web', 'alexking.org'); ?></li>
    <li id="akst_tab2" class="selected" onclick="akst_share_tab('2');"><?php _e('E-mail', 'alexking.org'); ?></li>
<?php
    break;
  case 'email':
?>
    <li id="akst_tab2" class="selected" onclick="akst_share_tab('2');"><?php _e('E-mail', 'alexking.org'); ?></li>
    <li id="akst_tab1" onclick="akst_share_tab('1');"><?php _e('Social Web', 'alexking.org'); ?></li>
<?php
    break;
  default: die("Error with the Share This plugin: Please define FIRST_TAB as either 'social' or 'email'.");
}
?>
Leave a Comment :, , more...

Bash Examples

by Matt Danger on November 23, 2008

A while ago a friend was taking an entry level Unix programming class and asked me to help him study. I came up with some sample problems for him. Here are my solutions:

The first is a program that will allow a user to input as many numbers as the user wants (999 as the choice that ends the user input). The program will then output the highest number, the lowest number, the sum of all the numbers, and the average number.

#!/bin/bash
COUNT=0
echo -n "Enter a number (999 to quit): "
 
while read NUM; do
  # Set initials
  if [ $COUNT -eq 0 ]; then
    HIGHEST=$NUM
    LOWEST=$NUM
    SUM=$NUM
  fi
 
  # If 999 is entered, break out
  if [ $NUM -eq 999 ]; then
    break
  else
    # See if inputted number is the highest number
    if [ $NUM -gt $HIGHEST ]; then
      HIGHEST=$NUM
    fi
 
    # See if inputted number is the lowest number
    if [ $NUM -lt $LOWEST ]; then
      LOWEST=$NUM;
    fi
 
    # Calculate sum
    SUM=$(($SUM+$NUM))
 
    echo -n "Enter another number (999 to quit): "
  fi
 
  COUNT=$(($COUNT+1))
done
 
AVERAGE=$(($SUM/$COUNT))
echo "Highest number: $HIGHEST"
echo "Lowest number: $LOWEST"
echo "Sum of the numbers: $SUM"
echo "Average of the numbers: $AVERAGE"

The second program is a number guessing game. The computer picks a number from 0-60 then allows the user to guess at the number until the user gets it right. The program gives the user a hint on whether they should guess a higher or lower number.

#!/bin/bash 
RAN=$((RANDOM%60))
COUNTER=1
echo -n "I've guessed a number between 0-60, what is it? "
 
while read GUESS; do
 
  if [ $GUESS -eq $RAN ]; then
    break;
  else
    echo
    if [ $GUESS -lt $RAN ]; then
      echo -n "Nope, try a higher number: "
    else
      echo -n "Nope, try a lower number: "
    fi	
  fi
  COUNTER=$((COUNTER+1))
 
done
 
echo
echo "Yep, good job! It took you $COUNTER tries to guess correctly."

The third program allows a user to type in a number, then the program will either count up to the number provided by the user or count down from that number. In addition, a menu lets the user choose which counter they wish to use. In both cases that counter will either count down to 0, or start counting from 0.

#!/bin/bash 
echo -n "Enter a number: "; read NUM
echo -n "Enter U to count up to $NUM and D to count down to 0: "; read CHOICE
 
case $CHOICE in
  'u'|'U') 
    COUNTER=0
    while [ $COUNTER -le $NUM ]; do
      echo $COUNTER
      COUNTER=$(($COUNTER+1))
    done ;;
  'd'|'D')
    while [ $NUM -ge 0 ]; do
      echo $NUM;
      NUM=$(($NUM-1))
    done ;;
  *) echo "Error: You entered an invalid argument."
esac

The last program asks the user if they want to add, subtract, multiply or divide two user inputted numbers. Then the program outputs the sum of number 1 and number 2, the difference of number 1 and number 2, the product of number 1 and number 2, and the quotient of number 1 and number 2

This program will run until the user wants to quit. Also, if the user puts in a 0 as the second number, we let the user know that if they choose divide that a division by zero error will occur.

#!/bin/bash
while true; do
  echo "Please enter two numbers (type 'q' to quit): "
  echo -n "Number 1: "
  read NUM1
  if [ $NUM1 == "q" ]; then exit; fi
  echo -n "Number 2: "
  read NUM2
  echo -n "Enter (a) to add, (s) to subtract, (m) to multiply, or (d) to divide these numbers: "
  read OPERATION
 
  case $OPERATION in
    a) echo "The sum of $NUM1 and $NUM2 is: $(($NUM1+$NUM2))" ;;
    s) echo "The difference of $NUM1 and $NUM2 is: $(($NUM1-$NUM2))";;
    m) echo "The product of $NUM1 and $NUM2 is: $(($NUM1*$NUM2))" ;;
    d) if [ $NUM2 -eq 0 ]; then
        echo "You can't divide by 0, a division by zero error will occur."
      else 			
	    echo "The quotient of $NUM1 and $NUM2 is: $(($NUM1/$NUM2))"
      fi ;;
  esac
done
Leave a Comment :, more...

PHP MySQL class example

by Matt Danger on February 27, 2007

Here is an example of a PHP 4.2 MySQL connector class. It handles all basic database queries and input filtering and validation.

<?php
 
/****************************************************
*
* File:    class.mysql.php
* Author:  Matt West, http://mattdanger.net
* Date:    February 22, 2007
* Purpose: MySQL Class
*
****************************************************/
 
class MySQL_Connection {
 
	// Private variables
    var $handler;
    var $hostname;
    var $username;
    var $password;
    var $database;
 
	// Public variables
    var $result;
    var $num_rows;
 
    /**
     * Constructor: Construct the object.
     * @param string $hostname  Hostname of MySQL server (Default: localhost) 
     * @param string $username  Your MySQL account username
     * @param string $password  Your MySQL account password
     * @param string $database  The MySQL database to connect to
     */
    function MySQL_Connection($hostname, $username, $password, $database) {
 
        $this->hostname = $hostname;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
        $this->connect();
        $this->select_db();
 
    }
 
    /**
     * Destructor: Clean up
     */
    function __destruct() {
 
        $this->db_close();
 
    }
 
    /**
     * throw_error(): Print an error message
     * @param string $message   A descriptive error message that will print when invoked
     */
    function throw_error($message, $line = 0){
 
        $line = ( !empty($line) ) ? $line = ' on line ' . $line : '' ;
        die ('There was an error on line' . $line .' in class "' . __CLASS__ . '": ' . $message);
 
    }
 
    /**
     * connect(): Connect to MySQL
     */
    function connect() {
 
        $this->handler = mysql_pconnect($this->hostname, $this->username, $this->password) or $this->throw_error(mysql_error(), __LINE__);
 
    }
 
    /**
     * select_db(): Select the MySQL database
     */
    function select_db() {
 
        mysql_select_db($this->database, $this->handler) or $this->throw_error(mysql_error(), __LINE__);
 
    }
 
    /**
     * select(): Select a row or rows from the DB
     * @param string $table     Table name
     * @param array $columns    Either '*' or a list of columns to select
     * @param array $where      column => value
     * @param array $order_by   column1 => DESC, column2 => ASC
     * @param array $limit      1 or 30 or 0, 30 or 30, 60 (etc)
     * @param bool $debug       Returns query string if true
     * @returns MySQL resource
     */
    function select($table, $columns, $where = '', $order_by = '', $limit = '', $debug = 0) {
 
        if ( $table == '' || !sizeof($columns)) { return; }
 
        // Set up columns
        $tmp = '';
        if ( $columns != '*' ) {
 
            if ( is_array($columns) ) {
 
                foreach($columns as $column) {
 
                    $data .= '`' . $column . '`, ';
 
                }
 
                $columns = preg_replace( "/, $/" , "" , $data);
 
            } else {
 
                $columns = '`' . $columns . '`';
 
            }
 
        }
 
        // Set up the WHERE
        if ( !empty($where) ) {
 
            $tmp = '';
            foreach ($where as $key => $val) {
 
                $tmp .= '`' . $key . "` = '" . $this->escape_str($val) . "' AND ";
 
            }
 
            $where = ' WHERE ' . preg_replace( "/ AND $/" , "" , $tmp);
 
        }
 
        // Set up the ORDER BY
        $tmp = '';
        if ( !empty($order_by) ) {
 
            if ( is_array($order_by) ) {
 
                foreach($order_by as $val => $order) {
 
                    $tmp .= '`' . $val . '` ' . $order . ', ';
 
                }
 
                $order_by = ' ORDER BY ' . preg_replace( "/, $/" , "" , $tmp);
 
            } else {
 
                $order_by = ' ORDER BY ' . $order_by . ' DESC';
 
            }
 
        }
 
        // Set up the LIMIT
        $tmp = '';
        if ( !empty($limit) ) {
 
            if ( is_array($limit) ) {
 
                foreach ($limit as $num) {
 
                    $tmp .= $num . ', ';
 
                }
 
                $limit = ' LIMIT ' . preg_replace( "/, $/" , "" , $tmp);
 
            } else {
 
                $limit = ' LIMIT ' . $limit;
 
            }
 
        }
 
        $query = 'SELECT ' . $columns . ' FROM `' . $table . '`' . $where . $order_by . $limit;
 
        // Return query string for debugging purposes or just do the query
        if ( $debug ) {
 
            return $query;
 
        } else {
 
            // Perform the query
            return $this->query($query);
 
        }
 
 
    }
 
    /**
     * insert(): Insert data into a table
     * @param string $table Table to insert into
     * @param array $data   The column name & data
     * @param boolean $slashes True if you want to add 's to the string
     * @returns boolean True if successful, false if not.
     */
    function insert($table, $data, $slashes = false) {
 
        if ( $table == '' || $data == '' ) { return; }
 
        $fields = '';      
        $values = '';
 
        foreach($data as $key => $val) {
 
            $fields .= '`' . $key . '`, ';
            $val = ( $slashes == true ) ? addslashes($val) : $val;
            $values .= "'" . addslashes( stripslashes($val) ) . "'" . ', ';
 
        }
 
        $fields = preg_replace( "/, $/" , "" , $fields);
        $values = preg_replace( "/, $/" , "" , $values);
 
        mysql_query('INSERT INTO `' . $table . '` (' . $fields . ') VALUES (' . $values . ')', $this->handler) or $this->throw_error(mysql_error(), __LINE__);
 
    }
 
    /**
     * update(): Update data into a table
     * @param string $table Table to insert into
     * @param array $data   The column name & data
     * @param boolean $slashes True if you want to add 's to the string
     * @returns boolean True if successful, false if not.
     */
    function update($table, $data, $where, $slashes = false) {
 
        if ( $table == '' || $data == '' || $where == '') { return; }
 
        $string  = '';
        $tmp = '';
 
        foreach($data as $key => $val) {
            $string .= '`' . $key . "` = '" . $this->escape_str($val) . "', ";
        }
 
        $string = preg_replace( "/, $/" , "" , $string);
 
        if ( is_array($where) ) {
 
            foreach ($where as $key => $val) {
 
                $tmp .= $key . " = '" . $this->escape_str($val) . "' AND ";
 
            }
 
            $where = preg_replace( "/AND $/" , "" , $tmp);
 
        } else {
 
            $where = $where;
 
        }
 
        mysql_query('UPDATE `' . $table . '` SET ' . $string . ' WHERE ' . $where, $this->handler) or $this->throw_error(mysql_error(), __LINE__);
 
    }
 
    /**
     * delete(): Delete a row from a table
     * @param string $table Table to insert into
     * @param array $data   The column name & data
     */
    function delete($table, $data) {
 
        if ( $table == '' || $data == '') { return; }
 
        mysql_query('DELETE FROM `' . $table . '` WHERE `' . key($data) . "` = '" . $data[key($data)] . "'", $this->handler) or $this->throw_error(mysql_error(), __LINE__);
 
    }
 
    /**
     * query(): Query from MySQL. 
     * @param string $query The MySQL query
     * @returns MySQL resource
     *
     * Warning: This function is only ment to be called manually if being used in 
     * a development environment because it does not do any input validation.
     */
    function query($query) {
 
        if ( $query == '') { return; }
 
        $this->result = mysql_query($query, $this->handler) or $this->throw_error(mysql_error(), __LINE__);
        $this->num_rows = mysql_num_rows($this->result);
 
        if ($this->num_rows > 1) {
 
            $tmp_array = array();
            while ($row = mysql_fetch_assoc($this->result)) {
 
                array_push($tmp_array, $row);
 
            }
 
            $this->result = $tmp_array;
            return $this->result;
 
        } else {
 
            $this->result = mysql_fetch_assoc($this->result);
            return $this->result;
        }
 
    }
 
    function escape_str($string) {
 
    	if ( is_array($string) ) {
 
    		foreach($string as $key => $val) {
 
    			$str[$key] = $this->escape_str($val);
 
    		}
 
    		return $str;
    	}
 
    	if ( function_exists('mysql_escape_string') ) {
 
			return mysql_escape_string( stripslashes($string) );
 
		} else {
 
        	return addslashes( stripslashes($string) );
 
    	}
 
    }
 
 
    /**
     * close_connection(): Close connection to MySQL
     */
    function db_close(){
 
        mysql_close($this->handler);
 
    }
 
}
?>
Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Send me a message on AIM!

Friends

Read what I read...