PHP

PHP Countries and their call codes with two letter abbreviations

<?php
$countries = array();
$countries[] = array("code"=>"AF","name"=>"Afghanistan","d_code"=>"+93");
$countries[] = array("code"=>"AL","name"=>"Albania","d_code"=>"+355");
$countries[] = array("code"=>"DZ","name"=>"Algeria","d_code"=>"+213");
$countries[] = array("code"=>"AS","name"=>"American Samoa","d_code"=>"+1");
$countries[] = array("code"=>"AD","name"=>"Andorra","d_code"=>"+376");
$countries[] = array("code"=>"AO","name"=>"Angola","d_code"=>"+244");
$countries[] = array("code"=>"AI","name"=>"Anguilla","d_code"=>"+1");
$countries[] = array("code"=>"AG","name"=>"Antigua","d_code"=>"+1");
$countries[] = array("code"=>"AR","name"=>"Argentina","d_code"=>"+54");
$countries[] = array("code"=>"AM","name"=>"Armenia","d_code"=>"+374");
$countries[] = array("code"=>"AW","name"=>"Aruba","d_code"=>"+297");
$countries[] = array("code"=>"AU","name"=>"Australia","d_code"=>"+61");
$countries[] = array("code"=>"AT","name"=>"Austria","d_code"=>"+43");
$countries[] = array("code"=>"AZ","name"=>"Azerbaijan","d_code"=>"+994");
$countries[] = array("code"=>"BH","name"=>"Bahrain","d_code"=>"+973");
$countries[] = array("code"=>"BD","name"=>"Bangladesh","d_code"=>"+880");
$countries[] = array("code"=>"BB","name"=>"Barbados","d_code"=>"+1");
$countries[] = array("code"=>"BY","name"=>"Belarus","d_code"=>"+375");
$countries[] = array("code"=>"BE","name"=>"Belgium","d_code"=>"+32");
$countries[] = array("code"=>"BZ","name"=>"Belize","d_code"=>"+501");
$countries[] = array("code"=>"BJ","name"=>"Benin","d_code"=>"+229");
$countries[] = array("code"=>"BM","name"=>"Bermuda","d_code"=>"+1");
$countries[] = array("code"=>"BT","name"=>"Bhutan","d_code"=>"+975");
$countries[] = array("code"=>"BO","name"=>"Bolivia","d_code"=>"+591");
$countries[] = array("code"=>"BA","name"=>"Bosnia and Herzegovina","d_code"=>"+387");
$countries[] = array("code"=>"BW","name"=>"Botswana","d_code"=>"+267");
$countries[] = array("code"=>"BR","name"=>"Brazil","d_code"=>"+55");
$countries[] = array("code"=>"IO","name"=>"British Indian Ocean Territory","d_code"=>"+246");
$countries[] = array("code"=>"VG","name"=>"British Virgin Islands","d_code"=>"+1");
$countries[] = array("code"=>"BN","name"=>"Brunei","d_code"=>"+673");
$countries[] = array("code"=>"BG","name"=>"Bulgaria","d_code"=>"+359");
$countries[] = array("code"=>"BF","name"=>"Burkina Faso","d_code"=>"+226");
$countries[] = array("code"=>"MM","name"=>"Burma Myanmar" ,"d_code"=>"+95");
$countries[] = array("code"=>"BI","name"=>"Burundi","d_code"=>"+257");
$countries[] = array("code"=>"KH","name"=>"Cambodia","d_code"=>"+855");
$countries[] = array("code"=>"CM","name"=>"Cameroon","d_code"=>"+237");
$countries[] = array("code"=>"CA","name"=>"Canada","d_code"=>"+1");
$countries[] = array("code"=>"CV","name"=>"Cape Verde","d_code"=>"+238");
$countries[] = array("code"=>"KY","name"=>"Cayman Islands","d_code"=>"+1");
$countries[] = array("code"=>"CF","name"=>"Central African Republic","d_code"=>"+236");
$countries[] = array("code"=>"TD","name"=>"Chad","d_code"=>"+235");
$countries[] = array("code"=>"CL","name"=>"Chile","d_code"=>"+56");
$countries[] = array("code"=>"CN","name"=>"China","d_code"=>"+86");
$countries[] = array("code"=>"CO","name"=>"Colombia","d_code"=>"+57");
$countries[] = array("code"=>"KM","name"=>"Comoros","d_code"=>"+269");
$countries[] = array("code"=>"CK","name"=>"Cook Islands","d_code"=>"+682");
$countries[] = array("code"=>"CR","name"=>"Costa Rica","d_code"=>"+506");
$countries[] = array("code"=>"CI","name"=>"Côte d'Ivoire" ,"d_code"=>"+225");
$countries[] = array("code"=>"HR","name"=>"Croatia","d_code"=>"+385");
$countries[] = array("code"=>"CU","name"=>"Cuba","d_code"=>"+53");
$countries[] = array("code"=>"CY","name"=>"Cyprus","d_code"=>"+357");
$countries[] = array("code"=>"CZ","name"=>"Czech Republic","d_code"=>"+420");
$countries[] = array("code"=>"CD","name"=>"Democratic Republic of Congo","d_code"=>"+243");
$countries[] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries[] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries[] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
$countries[] = array("code"=>"DO","name"=>"Dominican Republic","d_code"=>"+1");
$countries[] = array("code"=>"EC","name"=>"Ecuador","d_code"=>"+593");
$countries[] = array("code"=>"EG","name"=>"Egypt","d_code"=>"+20");
$countries[] = array("code"=>"SV","name"=>"El Salvador","d_code"=>"+503");
$countries[] = array("code"=>"GQ","name"=>"Equatorial Guinea","d_code"=>"+240");
$countries[] = array("code"=>"ER","name"=>"Eritrea","d_code"=>"+291");
$countries[] = array("code"=>"EE","name"=>"Estonia","d_code"=>"+372");
$countries[] = array("code"=>"ET","name"=>"Ethiopia","d_code"=>"+251");
$countries[] = array("code"=>"FK","name"=>"Falkland Islands","d_code"=>"+500");
$countries[] = array("code"=>"FO","name"=>"Faroe Islands","d_code"=>"+298");
$countries[] = array("code"=>"FM","name"=>"Federated States of Micronesia","d_code"=>"+691");
$countries[] = array("code"=>"FJ","name"=>"Fiji","d_code"=>"+679");
$countries[] = array("code"=>"FI","name"=>"Finland","d_code"=>"+358");
$countries[] = array("code"=>"FR","name"=>"France","d_code"=>"+33");
$countries[] = array("code"=>"GF","name"=>"French Guiana","d_code"=>"+594");
$countries[] = array("code"=>"PF","name"=>"French Polynesia","d_code"=>"+689");
$countries[] = array("code"=>"GA","name"=>"Gabon","d_code"=>"+241");
$countries[] = array("code"=>"GE","name"=>"Georgia","d_code"=>"+995");
$countries[] = array("code"=>"DE","name"=>"Germany","d_code"=>"+49");
$countries[] = array("code"=>"GH","name"=>"Ghana","d_code"=>"+233");
$countries[] = array("code"=>"GI","name"=>"Gibraltar","d_code"=>"+350");
$countries[] = array("code"=>"GR","name"=>"Greece","d_code"=>"+30");
$countries[] = array("code"=>"GL","name"=>"Greenland","d_code"=>"+299");
$countries[] = array("code"=>"GD","name"=>"Grenada","d_code"=>"+1");
$countries[] = array("code"=>"GP","name"=>"Guadeloupe","d_code"=>"+590");
$countries[] = array("code"=>"GU","name"=>"Guam","d_code"=>"+1");
$countries[] = array("code"=>"GT","name"=>"Guatemala","d_code"=>"+502");
$countries[] = array("code"=>"GN","name"=>"Guinea","d_code"=>"+224");
$countries[] = array("code"=>"GW","name"=>"Guinea-Bissau","d_code"=>"+245");
$countries[] = array("code"=>"GY","name"=>"Guyana","d_code"=>"+592");
$countries[] = array("code"=>"HT","name"=>"Haiti","d_code"=>"+509");
$countries[] = array("code"=>"HN","name"=>"Honduras","d_code"=>"+504");
$countries[] = array("code"=>"HK","name"=>"Hong Kong","d_code"=>"+852");
$countries[] = array("code"=>"HU","name"=>"Hungary","d_code"=>"+36");
$countries[] = array("code"=>"IS","name"=>"Iceland","d_code"=>"+354");
$countries[] = array("code"=>"IN","name"=>"India","d_code"=>"+91");
$countries[] = array("code"=>"ID","name"=>"Indonesia","d_code"=>"+62");
$countries[] = array("code"=>"IR","name"=>"Iran","d_code"=>"+98");
$countries[] = array("code"=>"IQ","name"=>"Iraq","d_code"=>"+964");
$countries[] = array("code"=>"IE","name"=>"Ireland","d_code"=>"+353");
$countries[] = array("code"=>"IL","name"=>"Israel","d_code"=>"+972");
$countries[] = array("code"=>"IT","name"=>"Italy","d_code"=>"+39");
$countries[] = array("code"=>"JM","name"=>"Jamaica","d_code"=>"+1");
$countries[] = array("code"=>"JP","name"=>"Japan","d_code"=>"+81");
$countries[] = array("code"=>"JO","name"=>"Jordan","d_code"=>"+962");
$countries[] = array("code"=>"KZ","name"=>"Kazakhstan","d_code"=>"+7");
$countries[] = array("code"=>"KE","name"=>"Kenya","d_code"=>"+254");
$countries[] = array("code"=>"KI","name"=>"Kiribati","d_code"=>"+686");
$countries[] = array("code"=>"XK","name"=>"Kosovo","d_code"=>"+381");
$countries[] = array("code"=>"KW","name"=>"Kuwait","d_code"=>"+965");
$countries[] = array("code"=>"KG","name"=>"Kyrgyzstan","d_code"=>"+996");
$countries[] = array("code"=>"LA","name"=>"Laos","d_code"=>"+856");
$countries[] = array("code"=>"LV","name"=>"Latvia","d_code"=>"+371");
$countries[] = array("code"=>"LB","name"=>"Lebanon","d_code"=>"+961");
$countries[] = array("code"=>"LS","name"=>"Lesotho","d_code"=>"+266");
$countries[] = array("code"=>"LR","name"=>"Liberia","d_code"=>"+231");
$countries[] = array("code"=>"LY","name"=>"Libya","d_code"=>"+218");
$countries[] = array("code"=>"LI","name"=>"Liechtenstein","d_code"=>"+423");
$countries[] = array("code"=>"LT","name"=>"Lithuania","d_code"=>"+370");
$countries[] = array("code"=>"LU","name"=>"Luxembourg","d_code"=>"+352");
$countries[] = array("code"=>"MO","name"=>"Macau","d_code"=>"+853");
$countries[] = array("code"=>"MK","name"=>"Macedonia","d_code"=>"+389");
$countries[] = array("code"=>"MG","name"=>"Madagascar","d_code"=>"+261");
$countries[] = array("code"=>"MW","name"=>"Malawi","d_code"=>"+265");
$countries[] = array("code"=>"MY","name"=>"Malaysia","d_code"=>"+60");
$countries[] = array("code"=>"MV","name"=>"Maldives","d_code"=>"+960");
$countries[] = array("code"=>"ML","name"=>"Mali","d_code"=>"+223");
$countries[] = array("code"=>"MT","name"=>"Malta","d_code"=>"+356");
$countries[] = array("code"=>"MH","name"=>"Marshall Islands","d_code"=>"+692");
$countries[] = array("code"=>"MQ","name"=>"Martinique","d_code"=>"+596");
$countries[] = array("code"=>"MR","name"=>"Mauritania","d_code"=>"+222");
$countries[] = array("code"=>"MU","name"=>"Mauritius","d_code"=>"+230");
$countries[] = array("code"=>"YT","name"=>"Mayotte","d_code"=>"+262");
$countries[] = array("code"=>"MX","name"=>"Mexico","d_code"=>"+52");
$countries[] = array("code"=>"MD","name"=>"Moldova","d_code"=>"+373");
$countries[] = array("code"=>"MC","name"=>"Monaco","d_code"=>"+377");
$countries[] = array("code"=>"MN","name"=>"Mongolia","d_code"=>"+976");
$countries[] = array("code"=>"ME","name"=>"Montenegro","d_code"=>"+382");
$countries[] = array("code"=>"MS","name"=>"Montserrat","d_code"=>"+1");
$countries[] = array("code"=>"MA","name"=>"Morocco","d_code"=>"+212");
$countries[] = array("code"=>"MZ","name"=>"Mozambique","d_code"=>"+258");
$countries[] = array("code"=>"NA","name"=>"Namibia","d_code"=>"+264");
$countries[] = array("code"=>"NR","name"=>"Nauru","d_code"=>"+674");
$countries[] = array("code"=>"NP","name"=>"Nepal","d_code"=>"+977");
$countries[] = array("code"=>"NL","name"=>"Netherlands","d_code"=>"+31");
$countries[] = array("code"=>"AN","name"=>"Netherlands Antilles","d_code"=>"+599");
$countries[] = array("code"=>"NC","name"=>"New Caledonia","d_code"=>"+687");
$countries[] = array("code"=>"NZ","name"=>"New Zealand","d_code"=>"+64");
$countries[] = array("code"=>"NI","name"=>"Nicaragua","d_code"=>"+505");
$countries[] = array("code"=>"NE","name"=>"Niger","d_code"=>"+227");
$countries[] = array("code"=>"NG","name"=>"Nigeria","d_code"=>"+234");
$countries[] = array("code"=>"NU","name"=>"Niue","d_code"=>"+683");
$countries[] = array("code"=>"NF","name"=>"Norfolk Island","d_code"=>"+672");
$countries[] = array("code"=>"KP","name"=>"North Korea","d_code"=>"+850");
$countries[] = array("code"=>"MP","name"=>"Northern Mariana Islands","d_code"=>"+1");
$countries[] = array("code"=>"NO","name"=>"Norway","d_code"=>"+47");
$countries[] = array("code"=>"OM","name"=>"Oman","d_code"=>"+968");
$countries[] = array("code"=>"PK","name"=>"Pakistan","d_code"=>"+92");
$countries[] = array("code"=>"PW","name"=>"Palau","d_code"=>"+680");
$countries[] = array("code"=>"PS","name"=>"Palestine","d_code"=>"+970");
$countries[] = array("code"=>"PA","name"=>"Panama","d_code"=>"+507");
$countries[] = array("code"=>"PG","name"=>"Papua New Guinea","d_code"=>"+675");
$countries[] = array("code"=>"PY","name"=>"Paraguay","d_code"=>"+595");
$countries[] = array("code"=>"PE","name"=>"Peru","d_code"=>"+51");
$countries[] = array("code"=>"PH","name"=>"Philippines","d_code"=>"+63");
$countries[] = array("code"=>"PL","name"=>"Poland","d_code"=>"+48");
$countries[] = array("code"=>"PT","name"=>"Portugal","d_code"=>"+351");
$countries[] = array("code"=>"PR","name"=>"Puerto Rico","d_code"=>"+1");
$countries[] = array("code"=>"QA","name"=>"Qatar","d_code"=>"+974");
$countries[] = array("code"=>"CG","name"=>"Republic of the Congo","d_code"=>"+242");
$countries[] = array("code"=>"RE","name"=>"Réunion" ,"d_code"=>"+262");
$countries[] = array("code"=>"RO","name"=>"Romania","d_code"=>"+40");
$countries[] = array("code"=>"RU","name"=>"Russia","d_code"=>"+7");
$countries[] = array("code"=>"RW","name"=>"Rwanda","d_code"=>"+250");
$countries[] = array("code"=>"BL","name"=>"Saint Barthélemy" ,"d_code"=>"+590");
$countries[] = array("code"=>"SH","name"=>"Saint Helena","d_code"=>"+290");
$countries[] = array("code"=>"KN","name"=>"Saint Kitts and Nevis","d_code"=>"+1");
$countries[] = array("code"=>"MF","name"=>"Saint Martin","d_code"=>"+590");
$countries[] = array("code"=>"PM","name"=>"Saint Pierre and Miquelon","d_code"=>"+508");
$countries[] = array("code"=>"VC","name"=>"Saint Vincent and the Grenadines","d_code"=>"+1");
$countries[] = array("code"=>"WS","name"=>"Samoa","d_code"=>"+685");
$countries[] = array("code"=>"SM","name"=>"San Marino","d_code"=>"+378");
$countries[] = array("code"=>"ST","name"=>"São Tomé and Príncipe" ,"d_code"=>"+239");
$countries[] = array("code"=>"SA","name"=>"Saudi Arabia","d_code"=>"+966");
$countries[] = array("code"=>"SN","name"=>"Senegal","d_code"=>"+221");
$countries[] = array("code"=>"RS","name"=>"Serbia","d_code"=>"+381");
$countries[] = array("code"=>"SC","name"=>"Seychelles","d_code"=>"+248");
$countries[] = array("code"=>"SL","name"=>"Sierra Leone","d_code"=>"+232");
$countries[] = array("code"=>"SG","name"=>"Singapore","d_code"=>"+65");
$countries[] = array("code"=>"SK","name"=>"Slovakia","d_code"=>"+421");
$countries[] = array("code"=>"SI","name"=>"Slovenia","d_code"=>"+386");
$countries[] = array("code"=>"SB","name"=>"Solomon Islands","d_code"=>"+677");
$countries[] = array("code"=>"SO","name"=>"Somalia","d_code"=>"+252");
$countries[] = array("code"=>"ZA","name"=>"South Africa","d_code"=>"+27");
$countries[] = array("code"=>"KR","name"=>"South Korea","d_code"=>"+82");
$countries[] = array("code"=>"ES","name"=>"Spain","d_code"=>"+34");
$countries[] = array("code"=>"LK","name"=>"Sri Lanka","d_code"=>"+94");
$countries[] = array("code"=>"LC","name"=>"St. Lucia","d_code"=>"+1");
$countries[] = array("code"=>"SD","name"=>"Sudan","d_code"=>"+249");
$countries[] = array("code"=>"SR","name"=>"Suriname","d_code"=>"+597");
$countries[] = array("code"=>"SZ","name"=>"Swaziland","d_code"=>"+268");
$countries[] = array("code"=>"SE","name"=>"Sweden","d_code"=>"+46");
$countries[] = array("code"=>"CH","name"=>"Switzerland","d_code"=>"+41");
$countries[] = array("code"=>"SY","name"=>"Syria","d_code"=>"+963");
$countries[] = array("code"=>"TW","name"=>"Taiwan","d_code"=>"+886");
$countries[] = array("code"=>"TJ","name"=>"Tajikistan","d_code"=>"+992");
$countries[] = array("code"=>"TZ","name"=>"Tanzania","d_code"=>"+255");
$countries[] = array("code"=>"TH","name"=>"Thailand","d_code"=>"+66");
$countries[] = array("code"=>"BS","name"=>"The Bahamas","d_code"=>"+1");
$countries[] = array("code"=>"GM","name"=>"The Gambia","d_code"=>"+220");
$countries[] = array("code"=>"TL","name"=>"Timor-Leste","d_code"=>"+670");
$countries[] = array("code"=>"TG","name"=>"Togo","d_code"=>"+228");
$countries[] = array("code"=>"TK","name"=>"Tokelau","d_code"=>"+690");
$countries[] = array("code"=>"TO","name"=>"Tonga","d_code"=>"+676");
$countries[] = array("code"=>"TT","name"=>"Trinidad and Tobago","d_code"=>"+1");
$countries[] = array("code"=>"TN","name"=>"Tunisia","d_code"=>"+216");
$countries[] = array("code"=>"TR","name"=>"Turkey","d_code"=>"+90");
$countries[] = array("code"=>"TM","name"=>"Turkmenistan","d_code"=>"+993");
$countries[] = array("code"=>"TC","name"=>"Turks and Caicos Islands","d_code"=>"+1");
$countries[] = array("code"=>"TV","name"=>"Tuvalu","d_code"=>"+688");
$countries[] = array("code"=>"UG","name"=>"Uganda","d_code"=>"+256");
$countries[] = array("code"=>"UA","name"=>"Ukraine","d_code"=>"+380");
$countries[] = array("code"=>"AE","name"=>"United Arab Emirates","d_code"=>"+971");
$countries[] = array("code"=>"GB","name"=>"United Kingdom","d_code"=>"+44");
$countries[] = array("code"=>"US","name"=>"United States","d_code"=>"+1");
$countries[] = array("code"=>"UY","name"=>"Uruguay","d_code"=>"+598");
$countries[] = array("code"=>"VI","name"=>"US Virgin Islands","d_code"=>"+1");
$countries[] = array("code"=>"UZ","name"=>"Uzbekistan","d_code"=>"+998");
$countries[] = array("code"=>"VU","name"=>"Vanuatu","d_code"=>"+678");
$countries[] = array("code"=>"VA","name"=>"Vatican City","d_code"=>"+39");
$countries[] = array("code"=>"VE","name"=>"Venezuela","d_code"=>"+58");
$countries[] = array("code"=>"VN","name"=>"Vietnam","d_code"=>"+84");
$countries[] = array("code"=>"WF","name"=>"Wallis and Futuna","d_code"=>"+681");
$countries[] = array("code"=>"YE","name"=>"Yemen","d_code"=>"+967");
$countries[] = array("code"=>"ZM","name"=>"Zambia","d_code"=>"+260");
$countries[] = array("code"=>"ZW","name"=>"Zimbabwe","d_code"=>"+263");
PHP

Basic State Controller for a checkout

<?php
/**
 * 
 * @ This class allows us to achieve stateful routing
 *   It only tracks current, completed and previous steps
 *   If the user presses the back button this will do nothing.
 *   
 *   An example of it's usage is in the summary page:
 *    //-- You press edit on the address
 *    //-- This script will hold the information of the previous step (summary)
 *    //-- When you enter a new address or select another it understands that
 *         the summary page is where you need to go and will place you there.
 *
 */

class checkout_state_controller{

	///* Previous stage
	protected $previous;
	
	///* Current stage
	protected $current;
	
	///* All the stages in an array
	protected $stages = array();
	
	///* Singleton instance of this class
	private static $instance;

    /**
     * Shopping basket stages
     * 
     * @ This function returns an array of the stages of the shopping basket.
     *   It allows this class to make a descision about which page to redirect to.
     *   Make sure these are in order.
     * 
     * @params void
     * @return Array $stages
     */
	public function stages(){
		return array(
					"shopping_basket" => new stage(HTTPS_BASE_URL."/shopping_basket.php"), 
					"welcome" => new stage(HTTPS_BASE_URL."/checkout/welcome.php"),
					"shipping_address" => new stage(HTTPS_BASE_URL."/checkout/shipping_address.php"),
					"payment" => new stage(HTTPS_BASE_URL."/checkout/payment.php"), 
					"billing_address" => new stage(HTTPS_BASE_URL."/checkout/billing_address.php"),
					"summary" => new stage(HTTPS_BASE_URL."/checkout/summary.php"),
					"confirmation" => new stage(HTTPS_BASE_URL."/checkout/confirmation.php")
				);
	}	
	
	/**
	 * @ This is the singleton method as defined by the singleton pattern
	 *   This ensures we only ever work on one of this object type.
	 * 
	 * @param void
	 * @return object $this
	 */
	public static function getInstance(){
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }	
	
    /**
     * Constructor
     * 
     * @ In the constructor we get the stages and read from session
     * 
     * @param void
     * @return void
     */
	private function __construct(){ 
		
		$this->stages = $this->stages();
		$this->read();
		
	}	
	
	/**
	 * PHP_SELF replacement echoer
	 * 
	 * @ This function is designed to echo a string that will
	 *   replace the insecure PHP_SELF function.
	 *   
	 * @params void
	 * @return string $url
	 */
	public function echo_self(){
		return $this->stages[$this->current]->url;
	}
	
	/**
	 * Echo stage url
	 * 
	 * @ This allows us to quickly and easily echo a stage url
	 * 
	 * @params string $stage
	 * @return string $url
	 */
	public function stage_url($stage){
		return $stage ? $this->stages[$stage]->url : "";
	}

	/**
	 * Sets current stage
	 * 
	 * @ This simply sets the active stage
	 * 
	 * @param string $caption
	 * 
	 */
	public function set_active_stage($caption){
		
		if($this->current) $this->previous = $this->current;
		
		$this->current = $caption;
		$this->write();
		
	}
	
	/**
	 * Routing Function
	 * 
	 * @ This function will route the user through the cart.
	 *   It allows for understanding completed sections of 
	 *   the checkout and redirecting between them as needed
	 *   
	 * @param string $caption
	 * @return boolean $success
	 */
	public function route($caption = null){
		
		/**
		 * Support netsuite iframe
		 * 
		 * @ To support the netsuite iframe we go on faith saying if the session variable is set
		 *   then the page is completed.
		 */
		if(isset($_SESSION['payment_selected_payment_option'])) $this->stages["payment"]->completed = true;		
		
		/** Lets make current previous if current exists **/
		if($this->current) $this->previous = $this->current;
		
		/** Lets set the current **/
		if(array_key_exists($caption, $this->stages) && $caption){
			$this->current = $caption;
		}else{
			return false;
		}
		
		/** We have now completed this stage **/
		$this->stages[$this->current]->completed = true;
		
		/** reset and touch the array just to be sure we have the right position **/
		$this->setCurrent($this->current);
			
		/** Lets get the next stage **/
		$next  = true;
			
		while($next){
			
			//* Get next stage
			$next_stage = next($this->stages);
			
			//* Is that stage completed?
			if(!$next_stage->completed){

				//* If not set current to that stage and redirect the user
				$this->current = key($this->stages);
				header("Location: ".$next_stage->url);
				$next = false;
			}
		}	

		/** Write to session **/
		$this->write();		
		
		return true;
	}
	
	/**
	 * A PHP_SELF override
	 * 
	 * @ This function replaces PHP_SELF for the checkout
	 * 
	 * @param void
	 * @return header() $location
	 */
	public function to_self(){
		header("Location: ".$this->stages[$this->current]->url);
	}
	
	/**
	 * Goto Function
	 * 
	 * @ This function will redirect to a specific stage
	 * 
	 * @param string $stage
	 * @return void
	 */
	public function goto_stage($stage){
		
		header("Location: ".$this->stages[$stage]->url);
		
	}
	
	/**
	 * Go back function
	 * 
	 * @ This function will redirect a user to the previous page
	 *   Will not work unless you use set_active_stage at the top
	 *   of the page in question
	 * 
	 * @param string $stage
	 * @return void
	 */
	public function go_back($stage){
		
		header("Location: ".$this->stages[$this->previous]->url);
		
	}

	/**
	 * Array SetCurrent
	 * 
	 * @ This function will allow you to set a current position of the array
	 *   
	 * @param string $step
	 * @return (Object)stage $current_stage
	 */
    public function setCurrent ($stage) {
    
      reset($this->stages);
      for ($i=1; $i<=8; $i++) {
        if ($this->stages[$i] == $stage) break;
      }
      return current($this->stages);
    
    }
  
    /**
     * Array getNext
     * 
     * @ This function will allow you to get the next stage in the list
     * 
     * @param void
     * @return (Object)stage $nextstage
     */
    public function getNext () {
    
      self::setCurrent($this->current);
      return next($this->stages);
    
    }
   
    /**
     * Array getPrev
     * 
     * @ This function will allow you to get the previous stage in the list
     * 
     * @param void
     * @return (Object)stage $previoustage
     */
    public function getPrev () {
    
      self::setCurrent($this->current);
      return prev($this->stages);
    
    }
    
    /**
     * Array getEnd
     * 
     * @ This function will return the end entry of the stage array
     * 
     * @param void
     * @return (Object)stage $endstage
     */
    public function getEnd(){
    	return end($this->stages);
    }
	
	/**
	 * Store read function
	 * 
	 * @ This function will read the stored class from session.
	 *   We do not store the object since you should never do that so
	 *   instead we translate the object in a session storable array.
	 *   
	 * @param void
	 * @return void
	 */
	public function read(){
		
		$checkout_state_controller = $_SESSION['checkout_state_controller'];
		
		if($checkout_state_controller){
			
			$this->previous = $checkout_state_controller['previous'];
			$this->current = $checkout_state_controller['current'];
			
			/** Compose stage array **/
			foreach($checkout_state_controller['stages'] as $stage => $completed){
				
				$this->stages[$stage]->completed = $completed;
				
			}
		}
	}
	
	/**
	 * Store write function
	 * 
	 * @ This function will write this class to session
	 * 
	 * @param void
	 * @return void
	 */
	public function write(){
		
		$_SESSION['checkout_state_controller']['previous'] = $this->previous;
		$_SESSION['checkout_state_controller']['current'] = $this->current;
		
		/** Compose stage array **/
		$stages_arr = Array();
		foreach($this->stages as $stage_name => $object){
			
			$stages_arr[$stage_name] = $object->completed;			
		}
		
		$_SESSION['checkout_state_controller']['stages'] = $stages_arr;
		
	}
	
	/**
	 * Store delete function
	 * 
	 * @ This function will delete a store version of this class from session
	 * 
	 * @param void
	 * @return void
	 */
	public function delete(){
		
		unset($_SESSSION['checkout_state_controller']);
	}

	/**
	 * @ This is the __clone magic called when a user tries to clone this class
	 *   This function will return an error since singleton cannot be copied
	 */
	public function __clone(){
        trigger_error('You cannot clone this class.', E_USER_ERROR);
    }	

}

/**
 * This is the stage entity
 * 
 * @ This is the object in which a stage is place.
 *   It allows us to control excatly what a stage means
 * 
 */
class stage{
	
	protected $url;
	protected $required;
	protected $completed = false;

	public function __construct($url, $required = false){
		$this->url = $url;
		$this->required = $required;
	}

	public function __get($k){
		return $this->{$k};
	}
	
	public function __set($k, $v){
		$this->{$k} = $v;
	}
}
?>


PHP

Get Tweets and cache in XML whilst staying under the API Limit

Edit: Added security features for judging when malicious tweets with dead links enter the XML. These links are designed to break XML web services to harm a site. We now test the xml and if it validates then write it to file.

The view:

    	<div class="twitter_box" style='width:150px; word-wrap: break-word;'>
    		<?php $tweet_amount=3; include $GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT']."/ext_webservices/twitter/test_twitter.php"; ?>
    		<?php 
    		if(is_array($tweets)){
	
				echo '<ul id="tweets">';
				foreach($tweets as $tweet){
					if($iterations < 4){				
						/** Echo Changed string **/
						if($iterations == 1){
							echo '<li class="first">'.$tweet['text'].'</li>';
						}elseif($iterations == 3){
							echo '<li class="last">'.$tweet['text'].'</li>';
						}else{
							echo '<li>'.$tweet['text'].'</li>';
						}	
						$iterations++;
					}
				}
				echo '</ul>';
			}else echo '<p>'.$tweets.'</p>';
		?>
    	</div>

The controller:

<?php
include_once "twitter.inc.php";

$iterations = 1;

$twitter = new Twitter();
$tweets = $twitter->getTweets('username', 'pass');
if(is_array($tweets)){
	for($i=0;$i<count($tweets);$i++){
		
		/** Put into a variable to make our coding lives a little easier **/
		$tweet_text = $tweets[$i]['text'];
		
		/** Replace tiny and shortened URIs
		 * 
		 * @ This replaces all the URIs in a tweet to correctly redirect 
		 */
		$pattern = "/http:\/\/([a-zA-z0-9]+).([a-zA-z]+)\/([a-zA-z0-9]+)/";
		$replacement = "<a href='http://$1.$2/$3'>http://$1.$2/$3</a>";
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);
		
		/** Replace URIs in general
		 * 
		 * @ This will replace all URIs in general
		 */
		$pattern = "/([http|https]+):\/\/([a-zA-z0-9.]+)([com|co.uk|biz]+)\/([a-zA-z0-9#=?._\-\/ ]+)/";
		$replacement = "<a href='$1://$2$3/$4'>$1://$2$3/$4</a>";
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);		
		
		/** Replace #URIs
		 * 
		 * @ This replaces all the hashed URIs to redirect to search pages
		 */
		$pattern = "/#([a-zA-Z0-9]+)/";
		$replacement = '<a href="http://twitter.com/search?q=%23$1">#$1</a>';
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);
		
		/** Replace Usernames
		 * 
		 * @ This replace all the @username URIs to redirect to profiles on Twitter
		 */
		$pattern = "/@([a-zA-Z0-9_]+)/";
		$replacement = "<a href='http://twitter.com/$1'>@$1</a>";
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);
		$tweets[$i]['text'] = $tweet_text;
	}
}else{
		/** Put into a variable to make our coding lives a little easier **/
		$tweet_text = $tweets;
		
		/** Replace tiny and shortened URIs
		 * 
		 * @ This replaces all the URIs in a tweet to correctly redirect 
		 */
		$pattern = "/http:\/\/([a-zA-z0-9]+).([a-zA-z]+)\/([a-zA-z0-9]+)/";
		$replacement = "<a href='http://$1.$2/$3'>http://$1.$2/$3</a>";
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);
		
		/** Replace URIs in general
		 * 
		 * @ This will replace all URIs in general
		 */
		$pattern = "/([http|https]+):\/\/([a-zA-z0-9.]+)([com|co.uk|biz]+)\/([a-zA-z0-9#=?._\-\/ ]+)/";
		$replacement = "<a href='$1://$2$3/$4'>$1://$2$3/$4</a>";
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);		
		
		/** Replace #URIs
		 * 
		 * @ This replaces all the hashed URIs to redirect to search pages
		 */
		$pattern = "/#([a-zA-Z0-9]+)/";
		$replacement = '<a href="http://twitter.com/search?q=%23$1">#$1</a>';
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);
		
		/** Replace Usernames
		 * 
		 * @ This replace all the @username URIs to redirect to profiles on Twitter
		 */
		$pattern = "/@([a-zA-Z0-9_]+)/";
		$replacement = "<a href='http://twitter.com/$1'>@$1</a>";
		$tweet_text = preg_replace($pattern, $replacement, $tweet_text);
		$tweets = $tweet_text;	
}

The model:

<?php
class Twitter{
	private $tweet_count =10;
	private $twitter_api_url = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=';
	private $cache_folder = "";
	private $cache_expiration = 3600;

	public function getTweets($twitter_username = false, $twitter_password = false){

		if(!$twitter_username) return 'Error, you have not provided a Twitter username';

		$this->cache_folder = $_SERVER['DOCUMENT_ROOT']."/ext_webservices/twitter/cache";
		$cache_file = $this->cache_folder.'/'.$twitter_username.'.xml';

		if(!is_file($cache_file) || (date("U") - date("U", filemtime($cache_file))) > $this->cache_expiration){
			if(is_string($value = $this->storeCache($cache_file, $twitter_username, $twitter_password))) return $value;
		}

		return $this->readCache($cache_file);
	}

	private function readCache($cache_file = false, $tweets_array = false){

		libxml_use_internal_errors(true);

		$cacheXML = simplexml_load_file($cache_file);
		if(is_object($cacheXML)){
			foreach($cacheXML->status as $status){
				$tweets_array[] = array(
					'created' => (string)$status->created_at,
					'text' => (string)$status->text);
			}
			return(is_array($tweets_array))? array_slice($tweets_array, 0, $this->tweet_count) : 'There are no tweets available';
		}else{
			return 'There was an unknown error please try again later';
		}
	}

	private function storeCache($cache_file = false, $twitter_username = false, $twitter_password = false){

		libxml_use_internal_errors(true);

		if(is_dir($this->cache_folder)){
			if(is_writable($this->cache_folder)){
				if($twitter_password){
					ini_set("default_socket_timeout", 1);
					$context = stream_context_create(array(
														'http' => array(
																'header'=>"Authorization: Basic".base64_encode("$twitter_username:$twitter_password"),
																'timeout'=>5)
															));

					if(@file_get_contents($this->twitter_api_url . $twitter_username, false, $context)){

						$tweets = file_get_contents($this->twitter_api_url . $twitter_username, false, $context);

						$a = @simplexml_load_string($tweets);
						if($a===FALSE) {
						}else{
							$cacheHandle = fopen($cache_file, 'w');
							fwrite($cacheHandle, $tweets);
							fclose($cacheHandle);
						}
					}else{
						if(!file_exists($cache_file)){

							$a = @simplexml_load_string($tweets);
							if($a===FALSE) {
							}else{
								/** write an empty xml file **/
								$tweets = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<statuses type=\"array\"/>";
								$cacheHandle = fopen($cache_file, 'w');
								fwrite($cacheHandle, $tweets);
								fclose($cacheHandle);
							}
						}else{
							touch($cache_file);
						}
						//return "Twitter seems to be having problems at the moment. Please standby";
					}
				}else{
					$tweets = file_get_contents($this->twitter_api_url, $twitter_username);

					switch($http_response_header[0]){
						case 'HTTP/1.1 200 OK':

							$a = @simplexml_load_string($tweets);
							if($a===FALSE) {
							}else{
								$cacheHandle = fopen($cache_file, 'w');
								fwrite($cacheHandle, $tweets);
								fclose($cacheHandle);
							}
							break;
						case 'HTTP/1.1 401 Unauthorized':
							return "Error, the Twitter password you have supplied is either invalid or missing";
							break;
						case 'HTTP/1.1 404 Not Found':
							return "Error, the Twitter username you supplied has not been found";
							break;
						default:
							return "Error, an error occured while communicating with Twitter, please try again later";
							break;
					}

					return true;
				}
			}else return 'Error, the folder you have specified is not writable';
		}else return 'Error, the folder you have specified does not exist';
	}
}
PHP

Custom Error Handler Example

<?php
/*
 * This code was not written by me. Even though I have modified it to work with my mvc it was infact some guy on php manual who wrote this.
 * 
 * It was exactly what I was going to program so I thought "hell, why don't I just save myself some time"
 */

function errorHandler($errno, $errstr='', $errfile='', $errline='')
{
    // if error has been supressed with an @
    if (error_reporting() == 0) {
        return;
    }

    global $cfg;

    // check if function has been called by an exception
    if(func_num_args() == 5) {
        // called by trigger_error()
        $exception = null;
        list($errno, $errstr, $errfile, $errline) = func_get_args();

        $backtrace = array_reverse(debug_backtrace());

    }else {
        // caught exception
        $exc = func_get_arg(0);
        $errno = $exc->getCode();
        $errstr = $exc->getMessage();
        $errfile = $exc->getFile();
        $errline = $exc->getLine();

        $backtrace = $exc->getTrace();
    }

    $errorType = array (
               E_ERROR            => 'ERROR',
               E_WARNING        => 'WARNING',
               E_PARSE          => 'PARSING ERROR',
               E_NOTICE         => 'NOTICE',
               E_CORE_ERROR     => 'CORE ERROR',
               E_CORE_WARNING   => 'CORE WARNING',
               E_COMPILE_ERROR  => 'COMPILE ERROR',
               E_COMPILE_WARNING => 'COMPILE WARNING',
               E_USER_ERROR     => 'USER ERROR',
               E_USER_WARNING   => 'USER WARNING',
               E_USER_NOTICE    => 'USER NOTICE',
               E_STRICT         => 'STRICT NOTICE',
               E_RECOVERABLE_ERROR  => 'RECOVERABLE ERROR'
               );

    // create error message
    if (array_key_exists($errno, $errorType)) {
        $err = $errorType[$errno];
    } else {
        $err = 'CAUGHT EXCEPTION';
    }

    $errMsg = "$err: $errstr in $errfile on line $errline";

    // start backtrace
    foreach ($backtrace as $v) {

        if (isset($v['class'])) {

            $trace = 'in class '.$v['class'].'::'.$v['function'].'(';

            if (isset($v['args'])) {
                $separator = '';

                foreach($v['args'] as $arg ) {
                    $trace .= "$separator".getArgument($arg);
                    $separator = ', ';
                }
            }
            $trace .= ')';
        }

        elseif (isset($v['function']) && empty($trace)) {
            $trace = 'in function '.$v['function'].'(';
            if (!empty($v['args'])) {

                $separator = '';

                foreach($v['args'] as $arg ) {
                    $trace .= "$separator".getArgument($arg);
                    $separator = ', ';
                }
            }
            $trace .= ')';
        }
    }

    // display error msg, if debug is enabled
    if(config::get('DEVELOPMENT_ENVIRONMENT')) {
        echo '<h2>Debug Msg</h2>'.nl2br($errMsg).'<br />
            Trace: '.nl2br($trace).'<br />';
    }

    // what to do
    switch ($errno) {
        case E_NOTICE:
        case E_USER_NOTICE:
            return;
            break;

        default:
            if(!config::get('DEVELOPMENT_ENVIRONMENT')){
                // send email to admin
                if(!empty($cfg['adminEmail'])) {
                    @mail($cfg['adminEmail'],'critical error on '.$_SERVER['HTTP_HOST'], $errorText,
                            'From: Error Handler');
                }
                // end and display error msg
                exit(displayClientMessage());
            }
            else
                exit('<p>aborting.</p>');
            break;

    }

} // end of errorHandler()

function displayClientMessage()
{
	$page_title = "Stagex | Fatal Error";
	$view = FRAMEWORK_BASE."/components/errors/FatalError.php";
    include ROOT.'/application/layouts/blank_page.php';

}

function getArgument($arg)
{
    switch (strtolower(gettype($arg))) {

        case 'string':
            return( '"'.str_replace( array("\n"), array(''), $arg ).'"' );

        case 'boolean':
            return (bool)$arg;

        case 'object':
            return 'object('.get_class($arg).')';

        case 'array':
            $ret = 'array(';
            $separtor = '';

            foreach ($arg as $k => $v) {
                $ret .= $separtor.getArgument($k).' => '.getArgument($v);
                $separtor = ', ';
            }
            $ret .= ')';

            return $ret;

        case 'resource':
            return 'resource('.get_resource_type($arg).')';

        default:
            return var_export($arg, true);
    }
}
PHP

PHP Email Address Validation

After about 4 hours of searching and reading RFC’s with blackened eyes I finally stumbled across a email validator that might actually work:

Sayers Validator

I have tested it against a bunch of stuff and it has about 99.9% accuracy rate which is pretty insane. Don’t even bother to try and understand is coding it is pretty long and tiring but you will notice it’s quite a bit bigger than most things you find on Google searching for these. This code implements every single RFC and is constantly updated with new amendments to keep its accuracy.

Give it a try you’ll find it will correctly validate almost every email address.

PHP

Get Certain Properties of a Class in PHP

Using the below code you can get the public and protected variables of a class. WARNING: do not use this in a loop. If you need to set class variables through predefined variables please use this function outside of a loop, store the results and then foreach it:

  	public function populate($user = Array()){
  		
  		$props = $this->listProperties();
  		
  		foreach($user as $k => $v){
  			if(array_key_exists($k, $props)){
  				$this->{$k} = $v;
  			}
  		}
  	}

This is because this function (to find class variables) is slow and I have discovered PHP will run this function everytime even in a foreach loop which will considerably reduce speed after a random amount of time.

The function for getting class variables is:

        $reflect = new ReflectionClass(get_class($this));
          foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC + ReflectionProperty::IS_PROTECTED) as $prop) {
              $arr[$prop->getName()] = $this->{$prop->getName()};
          }

When getting a class you can use either a name or an object. To use an object do: new ReflectionObject($this). To use the class constant use: new ReflectionClass(__CLASS__). And there you have the properties of a class in php.