Matt [Danger]

One step closer to world domination

Follow me on TwitterRSS Feeds

  • Home
  • Resume
  • Code
  • Portfolio
  • F.A.Q.
  • About Matt

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

Dec 15th

Posted by Matt Danger in Mac OS X

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."

  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
defaults, login window, Mac OS X

Simple Reg-Ex to Validate an Email Address

Nov 24th

Posted by Matt Danger in PHP

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!");
}
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
email, PHP, reg-ex, regular expressions

Update Your Facebook Status from the Command Line

Nov 24th

Posted by Matt Danger in Perl

2 comments

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 September 9, 2011 to version 2.2

Click here to download.

#!/usr/bin/perl -w
 
#===============================================================================
#
# Facebook status script v2.2
#
# This script allows you to update your Facebook status from the shell.
#
# Copyright (C) 2011 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 $fb_dtsg;
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://www.facebook.com/login.php?m=m&amp;refsrc=http%3A%2F%2Fm.facebook.com%2Fhome.php&amp;refid=8', 
                           ['email' => $login,
                            'pass' => $password,
                            'login' => 'Log In'], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;
$response = $browser->get('http://m.facebook.com/home.php', @header);
 
$auth_key = $response->content;
$auth_key =~ s/\n//g;
$auth_key =~ s/^.*name="post_form_id" value="//;
$auth_key =~ s/".*$//;
 
$fb_dtsg = $response->content;
$fb_dtsg =~ s/\n//g;
$fb_dtsg =~ s/^.*name="fb_dtsg" value="//;
$fb_dtsg =~ 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' => $fb_dtsg,
                            '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";
  $response->content;
}
 
# Now that we're done we can delete the cookies.dat file.
exec('rm cookies.dat');
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
Facebook, Perl, status

Hacking “Share This” to Configure Sharing Options

Nov 24th

Posted by Matt Danger in PHP

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'.");
}
?>
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
PHP, Share This, Wordpress

Bash Examples

Nov 23rd

Posted by Matt Danger in Shell Scripting

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
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
Bash, Unix

PHP MySQL class example

Feb 27th

Posted by Matt Danger in PHP

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);
 
    }
 
}
?>
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
class, MySQL, PHP

PHP 5 Email Class

Jan 25th

Posted by Matt Danger in PHP

Here is an example class that you can use to send email to users. It includes a properly formatted email body meta information that should avoid most spam filters.

<?php
 
/****************************************************
*
* File:     class.email.php
* Purpose:  Email Class
* Author:   Matt West, http://mattdanger.net
* Date:     January 25, 2007
*
****************************************************/
 
class Email {
 
    // Public variables
    public $recipient;
    public $sender;
    public $reply_to;
    public $subject;
    public $body;
    public $cc;
    public $bcc;
    public $headers;
 
    /**
     * Constructor: Read in $file.
     * @param string $to        Recipient
     * @param array $from       "Sender's Name" => "Sender's Email"
     * @param string $subject   Email subject
     * @param string $body      Email body
     */
    public function __construct ($to, $from = array(), $subject, $body) {
 
        // Set receiver
        $this->receiver($to);
 
        // Set sender
        if ( !empty($from) ) {
 
            $this->sender = array( key($from) => $from[ key($from) ] );
 
        } else {
 
            $this->sender = array( $_SERVER['SERVER_ADMIN'] => $_SERVER['SERVER_ADMIN'] . '@' . $_SERVER['SERVER_NAME'] );
 
        }
 
        // Set subject
        $this->subject($subject);
 
        // Set body
        $this->body($body);
 
    }
 
    /**
     * receiver(): Set receiver of the email
     * @param array $send_to List of Names & Email addresses
     */
    public function receiver ($send_to) {
 
        foreach ($send_to as $name => $email) {
 
            if ( $this->email_is_valid($email) ) {
 
                if ( !is_numeric($name) ) { 
 
                    $this->recipient = ucfirst($name) . ' <' . $email . '>';
 
                } else {
 
                    $this->recipient = $email;
 
                }
 
                $this->recipient .= ', ';
 
            } else { 
 
                exit( $email . " is not a valid email address." ); 
 
            }
 
        }
 
        $this->recipient = preg_replace('/, $/', '', $this->recipient);
 
    }
 
    /**
     * sender(): Set send of the email
     * @param array $sender Name & Email address
     */
    public function sender ($sender) {
 
        if ( email_is_valid($sender[0]) ) {
 
            if ( !is_numeric( key($sender) ) ) { 
 
                $this->sender = ucfirst( key($sender) ) . ' <' . $email . '>';
 
            } else {
 
                $this->sender = $sender[0];
 
            }
 
        } else {
 
            exit( $sender[0] . " is not a valid email address." ); 
 
        }
 
    }
 
    /**
     * reply_to(): Set send of the email
     * @param array $reply_to Name & Email address
     */
    public function reply_to ($reply_to) {
 
        if ( email_is_valid($reply_to[0]) ) {
 
            if ( !is_numeric( key($reply_to) ) ) { 
 
                $this->reply_to = ucfirst( key($reply_to) ) . ' <' . $email . '>';
 
            } else {
 
                $this->reply_to = $sender[ key($reply_to) ];
 
            }
 
        } else {
 
            exit( $reply_to[0] . " is not a valid email address." ); 
 
        }
 
    }
 
    /**
     * cc(): Set Cc of the email
     * @param array $cc List of Names & Email addresses
     */
    public function cc ($cc) {
 
        foreach ($cc as $name => $email) {
 
            if ( email_is_valid($email) ) {
 
                if ( !is_numeric($name) ) { 
 
                    $this->cc = ucfirst($name) . ' <' . $email . '>';
 
                } else {
 
                    $this->cc = $email;
 
                }
 
                $this->cc .= ', ';
 
            } else { 
 
                exit( $email . " is not a valid email address." ); 
 
            }
 
        }
 
        $this->cc = preg_replace('/, $/', '', $this->to);
 
    }
 
    /**
     * bcc(): Set Bcc of the email
     * @param array $bcc List of Names & Email addresses
     */
    public function bcc ($bcc) {
 
        foreach ($bcc as $name => $email) {
 
            if ( !is_numeric($name) ) { 
 
                $this->bcc = ucfirst($name) . ' <' . $email . '>';
 
            } else {
 
                $this->bcc = $email;
 
            }
 
            $this->bcc .= ', ';
 
        }
 
        $this->bcc = preg_replace('/, $/', '', $this->to);
 
    }
 
    /**
     * set_headers(): Set email headers
     */
    public function set_headers () {
 
        $this->set_from();
 
        $this->headers  = "MIME-Version: 1.0rn"
                        . "From: ". $this->sender . "rn"
                        . "To: " . $this->recipient . "rn";
 
        if ( !empty($this->reply_to) ) $this->headers .= "Reply-To: " . $this->reply_to . "rn";
        if ( !empty($this->cc) ) $this->headers .= "Cc: " . $this->cc . "rn"; 
        if ( !empty($this->bcc) ) $this->headers .= "Bcc: " . $this->bcc . "rn"; 
 
        $this->headers .= "X-Priority: 1rn"
                        . "X-Mailer: PHP/" . phpversion() . "rn"
                        . "Content-type: text/html; charset=iso-8859-1rn";
 
    }
 
    /**
     * subject(): Sets the subject of the email.
     * @param string $subject The subject message.
     */
    public function subject ($subject) {
 
        // Strip any newlines
        $this->subject = str_replace('n', '', $subject);
 
    }
 
    /**
     * body(): Sets the body message of the email.
     * @param string $body The body message.
     */
    public function body ($body) {
 
        $this->body = $body;
 
    }
 
    /**
     * send(): Send the email
     * @returns boolean True if successful, false if not.
     */
    public function send () {
 
        if (mail ( $this->recipient, $this->subject, $this->body, $this->headers)) {
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * email_is_valid():    Check whether email address is valid
     * @param string $email Email address to check
     * @returns boolean     True if address is valid, false if not.
     */
    private function email_is_valid ($email) {
 
        if (eregi("^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$", $email)) {
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
}
 
?>
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
class, email, PHP

PHP Login Class

Jan 15th

Posted by Matt Danger in PHP

Here is an example of a PHP 4.2 Login class that you can use when writing your own CMS.

<?php
 
/****************************************************
*
* File:    class.login.php
* Author:  Matt West, 2007
* Purpose: Login class
*
*****************************************************/
 
class Login {
 
    // Public variables
    var $username;
    var $cookie;
 
    function Login() { }
 
    /**
     * login(): Authenticate the user's username & password
     * @param string $username User's username in the DB
     * @param string $password User's password in the DB
     * @param object $DB    MySQL database class object
     * @param string $table Table to query from
     * @returns bool        True if login successful, false if not
     */
 
    function login ($username, $password, $DB, $table = 'users') {
 
        $this->username = $username;
 
        $DB->query("SELECT `username`, `salt`, `password` FROM `$table` WHERE `username` = '$username' LIMIT 1");
 
        return ( $DB->result['password'] == sha1($DB->result['salt'] . $password) ) ? true : false ;
 
    }
 
    /**
     * login_admin(): Authenticate the an admin's username & password
     * @param string $username User's username in the DB
     * @param string $password User's password in the DB
     * @param object $DB    MySQL database class object
     * @param string $table Table to query from
     * @returns bool        True if login successful, false if not
     */
    function login_admin ($username, $password, $DB, $table = 'users') {
 
        $this->username = $username;
 
        $DB->query("SELECT `username`, `salt`, `password`, `user_level` FROM `$table` WHERE `username` = '$username' LIMIT 1");
 
        if ( ( $DB->result['password'] == sha1($DB->result['salt'] . $password) ) && ( $DB->result['user_level'] ) == '3') {
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * set_cookie():        Set the user's cookie
     * @param string $cookie_name   Name of the cookie
     * @param object $DB    MySQL database class object
     * @param bool $remember Whether to set for 30 days or just this session
     * @param string $table Table to query from
     * @returns bool        True if cookie was sent, false if not
     */
    function set_cookie ($cookie_name, $DB, $remember = true, $table = 'users') {
 
        $expiration = ( $remember ) ? ( time() + (60 * 60 * 24 * 30) ) : null ;
 
        $DB->query("SELECT * FROM `$table` WHERE `username` = '" . $this->username . "' LIMIT 1");
 
        return ( setcookie($cookie_name, base64_encode( 
                                $DB->result['username'] . ":" .
                                $DB->result['id'] . ":" .
                                $DB->result['user_level'] . ":" .
                                $DB->result['first_name'] . ":" .
                                $DB->result['last_name'] ), $expiration) ) ? true : false ;
 
    }
 
    /**
     * is_logged_in():      Check if user is currently logged in
     * @param string $cookie_name   Name of the cookie
     * @returns bool        True if cookie was sent, false if not
     */
    function is_logged_in ($cookie_name) {
 
        $this->cookie = explode(':', base64_decode($_COOKIE[$cookie_name]) );
 
    }
 
    /**
     * is_admin_logged_in(): Check if user is currently logged in
     * @param string $cookie_name   Name of the cookie
     * @returns bool True if cookie was sent, false if not
     */
    function is_admin_logged_in ($cookie_name) {
 
        if ( isset($_COOKIE[$cookie_name]) ) {
 
            $this->cookie = explode(':', base64_decode($_COOKIE[$cookie_name]) );
 
            if ( $this->cookie[2] == 3 ) {
 
                return true;
 
            } else {
 
                return false;
 
            }
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * logout(): Clear the user's cookie
     * @param string $cookie_name   Name of the cookie
     */
    function logout ($cookie_name) {
 
        setcookie($cookie_name, '');
 
    }
 
}
 
?>
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
login class, PHP

PHP photo resizing class example

Jan 12th

Posted by Matt Danger in PHP

Here is an example of a PHP class that manages simple photo resizing and saving using the GD library.

<?php
 
/****************************************************
*
* Purpose: Photo Resizing Class
* Author:  Matt West, http://mattdanger.net
* Date:    January 12th, 2007
*
****************************************************/
 
class Photo {
 
    // Private variables
    private $orig_image;
    private $portrait_frame;    // 1 = Landscape, 2 = Portrait
    private $image_size;
    private $src_width;
    private $src_height;
    private $src_image;
    private $dest_height;
    private $dest_width;
    private $dest_image;
    private $ratio;
    private $scale;
 
    // Public variables
    public $allowed_types = array('jpeg', 'jpg', 'png', 'gif');
 
    /**
     * Constructor:         Contruct the photo object
     * @param array $photo  Array of photo data, usually $_FILES['photo_name']
     */
    public function __construct($photo) {
 
        $this->photo($photo);
 
    }
 
    /**
     * photo():             Create new image resource
     * @param string $photo Array of photo data
     */
    public function photo($photo) {
 
        $this->orig_image = $photo;
 
        // Confirm photo is an allowed type;
        if ( !in_array(ereg_replace('image/', '', $this->orig_image['type']), $this->allowed_types) ) {
 
            exit("Photo type is '" . $this->orig_image['type'] . "' which is not allowed");
 
        }
 
        // Get image X and Y values in order to determine photo framing
        $this->image_size = getimagesize($this->orig_image['tmp_name']);
        $this->src_width = $this->image_size[0];
        $this->src_height = $this->image_size[1];
        $this->dest_height = 0;
 
        // 1 = Landscape, 2 = Portrait
        $this->photo_framing = ($this->src_width > $this->src_height) ? 1 : 2 ;
 
        // Calculate the ratio at which to resize based on the dimentions
        $this->ratio = $this->src_width / $this->src_height;
 
    }
 
    /**
     * scale_to_width():    Sets the width of the resize value
     * @param int $width    Width in pixels
     * @returns boolean     Returns true if successful, false if it was not.
     */
    public function scale_to_width($width) {
 
        // Set the destination width value
        $this->dest_width = $width;
 
        // Calculate the value at which to scale the image
        $this->scale = ($this->ratio) ? $this->dest_width / $this->src_width : $this->dest_height / $this->src_height; 
 
        if ($this->scale_image()) {
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * scale_to_height():   Sets the height of the resize value
     * @param int $height   Height in pixels
     * @returns boolean     Returns true if successful, false if it was not.
     */
    public function scale_to_height($height) {
 
        // Set the destination height value
        $this->dest_height = $height;
 
        // Calculate the ratio at which to resize based on the dimentions
        $this->ratio = $this->src_width / $this->src_height;
 
        // Calculate the value at which to scale the image
        $this->scale = ($this->ratio) ? $this->dest_height / $this->src_height : $this->dest_width / $this->src_width ; 
 
        if ($this->scale_image()) {
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * scale_image():   Resize image proportionally
     * @returns boolean Returns true if successful, false if it was not.
     */
    private function scale_image () {
 
        if ($this->scale > 1) {
 
            exit ("The scale width supplied is larger than the original image. Please try a smaller number.");
 
        }
 
        $this->dest_width = $this->src_width * $this->scale;
        $this->dest_height = $this->src_height * $this->scale;
 
        if ( $this->dest_width >= $this->src_width && $this->dest_width >= $this->src_width ) $this->scale = 1;
 
        $this->src_image = imagecreatefromjpeg ($this->orig_image['tmp_name']);
 
        $this->dest_image = imagecreatetruecolor ($this->src_width * $this->scale, $this->src_height * $this->scale);
 
        if ( imagecopyresampled(   $this->dest_image, 
                                    $this->src_image, 
                                    0, 0, 0, 0, 
                                    $this->dest_width, 
                                    $this->dest_height, 
                                    $this->src_width, 
                                    $this->src_height) ) { 
 
            return true;
 
        } else {
 
            return false;
 
        }
 
    }
 
    /**
     * save_photo():            Saves the resized photo
     * @param string $filename  The path and filename to save the file (ex: "/path/to/image.jpg")
     * @param int $compression  A number from 1-100 representing the level of compression. 100 is the least compression.
     * @returns boolean         Returns true if save was successful, false if it was not.
     */
    public function save_photo ($filename, $compression = 80) {
 
        if ( imagejpeg($this->dest_image, $filename, $compression) ) {
 
            $return_value = true;
 
        } else {
 
            $return_value = false;
 
        }
 
        imagedestroy($this->src_image);
        imagedestroy($this->dest_image);
        return $return_value;
 
    }
 
}
 
?>
  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
class, GD, image, PHP, resizing

Newest LiveJournal Images

Nov 24th

Posted by Matt Danger in PHP

This script displays the newest images posted by LiveJournal users. The script is pretty basic and simply parses a feed from LiveJournal.

You can view it in action here: http://mattdanger.net/lj/ (Warning: Some images may be NSFW!)

The code:

<?php
 
$n = 5; // Maximum is 250
$referrer = 'http://example.com'; // Your site
$email = 'user@example.com'; // Your email
 
if ($fp = fsockopen('livejournal.com', 80)) {
 
  fputs($fp, "GET /stats/latest-img.bml HTTP/1.0rn" .
      "Host: www.livejournal.comrn" .
      "User-Agent: " . $referrer . "; " . $email . "rnrn");
 
  $data = '';
 
  while(!feof($fp)) {
 
    $data .= fgets($fp);
 
  }
 
  fclose($fp);
  preg_match_all("<recent-image img='([^']+)' url='([^']+)' />", $data, $out);
 
  for ($i = 0; $i < $n; $i++) {
 
    echo '<a href="' . $out[2][$i] . '"><img border="0" src="' . $out[1][$i] . '" /></a><br /><br />' . "n";
 
  }
 
} else {
 
  echo "Connecting to LiveJournal failed.";
 
}
?>

As an aside, if you’re a LiveJournal user and don’t want your images posted to the public feed you can go to your command console and type “set latest_optout yes”.

  • delicious Bookmark on Delicious
  • digg Digg this post
  • facebook Recommend on Facebook
  • reddit share via Reddit
  • stumble Share with Stumblers
  • twitter Tweet about it
  • rss Subscribe to the comments on this post
LiveJournal, PHP
«123
  • Hello!

    I'm Matt (Danger) West, web and iOS developer, *nix guru, OSS evangelist and B5 S4 geek. I've been around the scene since 1998 and running this space since 2005. You can find out more about me, what I read, where I hang out, and what I'm interested in by reading some of my articles and postings.
  • Contact Me

    I'm active on most of the popular networks.



  • Recent Projects

    • Facebook status shell script
    • Motronic 7 collaboration
    • B5 S4 stuff
    • RocDubs!
    • Paper: Threats Botnets Pose to International Businesses
    • Geeklog CMS
  • Categories

    • Announcements
    • Drupal
    • Geeklog
    • Mac OS X
    • Performance
    • Perl
    • PHP
    • Shell Scripting
    • Summer of Code
    • terminal
    • Uncategorized
    • Unix
  • TweetFeed Wordpress Plugin by bytefrog.de
Mystique theme by digitalnature | Powered by WordPress | Social network icon pack by Komodo Media
RSS Feeds XHTML 1.1 Top
  • follow:follow:
  • RSS RSS