<?php
/* SVN FILE: $Id: app_controller.php 6311 2008-01-02 06:33:52Z phpnut $ */
/**
 * Short description for file.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.app
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 6311 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2008-01-01 22:33:52 -0800 (Tue, 01 Jan 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Short description for class.
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.app
 */

App::import('Sanitize');
class AppController extends Controller {

	public $helpers = array('Ajax', 'Form', 'Html', 'Javascript', 'Number', 'Time', 'Text', 'Session','Fck');	
	var $components = array('Session', 'Email', 'Auth');
	//var $components = array('Stats','Auth');
	
	
	
	function beforeFilter() {
		
		
		
		
	/**	
		if($this->Session->valid()){
			$this->Session->del("session_timed_out");
		}else{
			$this->Session -> write("session_timed_out", "true");
		}**/
		
		//write the stats
		
		
		//Security::setHash('md5');
		//parent::beforeFilter();

		
		
		
		//$this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login');
		
		
		
		//$this->Auth->fields = array('username' => 'email','password'=>'password');
		//$this->Auth->authorize = 'controller';
		
		//$writeStatsResult = $this->Stats->writeStats($this->params['controller'], $this->params['action']);
		
			$this->Auth->authorize = 'controller';

            

            

            //$this->Auth->loginRedirect = '/users/home';

            
            		
		//Authentication section
   		// allow all access to section for now...make sure to fix later on
   		//$this->Auth->allow('*');
		//Set the user information into the variable currentUser
		//$this->currentUser = $this->Auth;
		//set the isAuthenticated variable 
   		//$this->isAuthenticated = !empty($this->currentUser); 
   		  //      $this->Auth->authError = "Please log in first in order to preform that action";
   		  
		
		
	
	}
	
function isAuthorized() {
	return true;
}

	function beforeRender() {
   		//set the isAuthenticated variable for the views
   		//$this->set('isAuthenticated', $this->Auth->user());
	} 
	
	
	
	
	
	
	/**  
 * uploads files to the server  
 * @params:  
 *      $folder     = the folder to upload the files e.g. 'img/files'  
 *      $formdata   = the array containing the form files  
 *      $itemId     = id of the item (optional) will create a new sub folder  
 * @return:  
 *      will return an array with the success of each file upload  
 */   
function uploadFiles($folder, $formdata, $itemId = null) {   
    // setup dir names absolute and relative   
    //print_r($formdata);
    
    $folder_url = WWW_ROOT.$folder;  
    //echo $folder_url; 
    $rel_url = $folder;   
      
    // create the folder if it does not exist   
    if(!is_dir($folder_url)) {   
        mkdir($folder_url); 
         
    }   
           
    // if itemId is set create an item folder   
    if($itemId) {   
        // set new absolute folder   
        $folder_url = WWW_ROOT.$folder.'/'.$itemId;    
        // set new relative folder   
        $rel_url = $folder.'/'.$itemId;   
        // create directory   
        if(!is_dir($folder_url)) {   
            mkdir($folder_url);   
        }   
    }   
       
    // list of permitted file types, this is only images but documents can be added   
    $permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');   
       
    // loop through and deal with the files   
    //foreach($formdata as $file) {   
        // replace spaces with underscores  
        $file=$formdata;
         
        $filename = str_replace(' ', '_', $file['name']);
           
        // assume filetype is false   
        $typeOK = false;   
        // check filetype is ok   
        foreach($permitted as $type) {   
            if($type == $file['type']) {   
                $typeOK = true;   
                break;   
            }   
        }   
           
        // if file type ok upload the file   
        if($typeOK) {   
            // switch based on error code   
            switch($file['error']) {   
                case 0:   
                    // check filename already exists   
                    if(!file_exists($folder_url.'/'.$filename)) {   
                        // create full filename   
                        $full_url = $folder_url.'/'.$filename;   
                        $url = $rel_url.'/'.$filename;   
                        // upload the file   
                        $success = move_uploaded_file($file['tmp_name'], $url);   
                    } else {   
                        // create unique filename and upload file   
                        ini_set('date.timezone', 'Europe/London');   
                        $now = date('Y-m-d-His');   
                        $full_url = $folder_url.'/'.$now.$filename;   
                        $url = $rel_url.'/'.$now.$filename;   
                        $success = move_uploaded_file($file['tmp_name'], $url);   
                    }   
                    // if upload was successful   
                    if($success) {   
                        // save the url of the file   
                        $result['urls'][] = substr($url,4);   
                    } else {   
                        $result['errors'][] = "Error uploaded $filename. Please try again.";   
                    }   
                    break;   
                case 3:   
                    // an error occured   
                    $result['errors'][] = "Error uploading $filename. Please try again.";   
                    break;   
                default:   
                    // an error occured   
                    $result['errors'][] = "System error uploading $filename. Contact webmaster.";   
                    break;   
            }   
        } elseif($file['error'] == 4) {   
            // no file was selected for upload   
            $result['nofiles'][] = "No file Selected";   
        } else {   
            // unacceptable file type   
            $result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";   
        }   
    
return $result;   
}   
	
}
?>