Location: PHPKode > projects > OSShare > osshare-project-v1.0.1/app/app_controller.php
<?php
/* SVN FILE: $Id: app_controller.php 4410 2007-02-02 13:31:21Z 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
 *
 /*
OsShare v1 , 
Coded By Paimpozhil B. , SaravanaKumar M.S.
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2007, 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-2007, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.cake
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 4410 $
 * @modifiedby		$LastChangedBy: phpnut $
 * @lastmodified	$Date: 2007-02-02 07:31:21 -0600 (Fri, 02 Feb 2007) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.cake
 */
class AppController extends Controller {

var $sitename,$adminemail,$ads;
var $uses = array('Sitesetting','Ad',"Linksite");
var $ssettings;
var $admin_array,$user_array,$default_array;

function checkaccess(){

$this->admin_array['album']['index']=1;
$this->admin_array['album']['add']=1;
$this->admin_array['album']['view']=1;
$this->admin_array['album']['edit']=1;
$this->admin_array['album']['userview']=1;
$this->admin_array['album']['delete']=1;
$this->admin_array['playlist']['add']=1;
$this->admin_array['playlist']['view']=1;
$this->admin_array['playlist']['edit']=1;
$this->admin_array['playlist']['userview']=1;
$this->admin_array['playlist']['delete']=1;
$this->admin_array['videos']['userindex']=1;
$this->admin_array['videos']['view']=1;
$this->admin_array['videos']['add']=1;
$this->admin_array['videos']['edit']=1;
$this->admin_array['videos']['delete']=1;
$this->admin_array['videos']['thumbupload']=1;
$this->admin_array['videos']['turlupload']=1;
$this->admin_array['videos']['search']=1;
$this->admin_array['videos']['abuse']=1;
$this->admin_array['videos']['fetchurl']=1;
$this->admin_array['videos']['encode']=1;
$this->admin_array['videos']['simulatedownload']=1;
$this->admin_array['videos']['addflv']=1;
$this->admin_array['videos']['embedvideo']=1;
$this->admin_array['pictures']['userindex']=1;
$this->admin_array['pictures']['view']=1;
$this->admin_array['pictures']['add']=1;
$this->admin_array['pictures']['edit']=1;
$this->admin_array['pictures']['delete']=1;
$this->admin_array['pictures']['thumbupload']=1;
$this->admin_array['pictures']['turlupload']=1;
$this->admin_array['pictures']['search']=1;
$this->admin_array['pictures']['abuse']=1;
$this->admin_array['ratings']['add']=1;
$this->admin_array['messages']['addnew']=1;
$this->admin_array['messages']['trashmessage']=1;
$this->admin_array['messages']['sentitems']=1;
$this->admin_array['messages']['usermessage']=1;
$this->admin_array['messages']['reply']=1;
$this->admin_array['messages']['delete']=1;
$this->admin_array['messages']['sentitemsdelete']=1;
$this->admin_array['messages']['messageview']=1;
$this->admin_array['messages']['sentview']=1;
$this->admin_array['messages']['trashdelete']=1;
$this->admin_array['messages']['senttoinbox']=1;
$this->admin_array['users']['adminhome']=1;
$this->admin_array['users']['view']=1;
$this->admin_array['users']['edit']=1;
$this->admin_array['users']['delete']=1;
$this->admin_array['users']['add']=1;
$this->admin_array['users']['rview']=1;
$this->admin_array['users']['adminindex']=1;
$this->admin_array['pfavorites']['add']=1;
$this->admin_array['pfavorites']['rview']=1;
$this->admin_array['pfavorites']['delete']=1;
$this->admin_array['linksites']['view']=1;
$this->admin_array['linksites']['edit']=1;
$this->admin_array['linksites']['delete']=1;
$this->admin_array['linksites']['add']=1;
$this->admin_array['albitems']['add']=1;
$this->admin_array['albitems']['addtolist']=1;
$this->admin_array['albitems']['itemview']=1;
$this->admin_array['albitems']['image_gallery']=1;
$this->admin_array['categories']['rview']=1;
$this->admin_array['categories']['view']=1;
$this->admin_array['categories']['prview']=1;
$this->admin_array['categories']['add']=1;
$this->admin_array['categories']['edit']=1;
$this->admin_array['categories']['delete']=1;
$this->admin_array['ads']['add']=1;
$this->admin_array['ads']['edit']=1;
$this->admin_array['ads']['delete']=1;
$this->admin_array['ads']['view']=1;
$this->admin_array['sitesettings']['add']=1;
$this->admin_array['sitesettings']['edit']=1;
$this->admin_array['sitesettings']['delete']=1;
$this->admin_array['sitesettings']['testffmpeg']=1;
$this->admin_array['plsitems']['add']=1;
$this->admin_array['plsitems']['addtolist']=1;
$this->admin_array['plsitems']['itemview']=1;
$this->admin_array['plsitems']['playall']=1;
$this->admin_array['plsitems']['userplayall']=1;
$this->admin_array['profile']['edit']=1;
$this->admin_array['profile']['view']=1;
$this->admin_array['slaveservers']['add']=1;
$this->admin_array['slaveservers']['view']=1;
$this->admin_array['slaveservers']['edit']=1;
$this->admin_array['slaveservers']['delete']=1;
$this->admin_array['slaveservers']['test']=1;
$this->admin_array['favorites']['pview']=1;
$this->admin_array['favorites']['add']=1;
$this->admin_array['favorites']['delete']=1;

$this->default_array['videos']['view']=1;
$this->default_array['videos']['unadd']=1;
$this->default_array['videos']['search']=1;
$this->default_array['videos']['abuse']=1;
$this->default_array['pictures']['view']=1;
$this->default_array['pictures']['unadd']=1;
$this->default_array['pictures']['search']=1;
$this->default_array['pictures']['abuse']=1;
$this->default_array['users']['signup']=1;
$this->default_array['users']['login']=1;
$this->default_array['users']['forgot']=1;
$this->default_array['users']['rview']=1;
$this->default_array['pfavorites']['rview']=1;
$this->default_array['categories']['rview']=1;
$this->default_array['categories']['view']=1;
$this->default_array['categories']['prview']=1;
$this->default_array['plsitems']['playall']=1;
$this->default_array['profile']['view']=1;

$this->user_array['album']['index']=1;
$this->user_array['album']['add']=1;
$this->user_array['album']['view']=1;
$this->user_array['album']['edit']=1;
$this->user_array['album']['userview']=1;
$this->user_array['album']['delete']=1;
$this->user_array['playlist']['add']=1;
$this->user_array['playlist']['view']=1;
$this->user_array['playlist']['edit']=1;
$this->user_array['playlist']['userview']=1;
$this->user_array['playlist']['delete']=1;
$this->user_array['videos']['adminindex']=1;
$this->user_array['videos']['view']=1;
$this->user_array['videos']['add']=1;
$this->user_array['videos']['useredit']=1;
$this->user_array['videos']['userdelete']=1;
$this->user_array['videos']['thumbupload']=1;
$this->user_array['videos']['turlupload']=1;
$this->user_array['videos']['search']=1;
$this->user_array['videos']['abuse']=1;
$this->user_array['videos']['fetchurl']=1;
$this->user_array['videos']['encode']=1;
$this->user_array['videos']['simulatedownload']=1;
$this->user_array['videos']['addflv']=1;
$this->user_array['videos']['embedvideo']=1;
$this->user_array['pictures']['adminindex']=1;
$this->user_array['pictures']['view']=1;
$this->user_array['pictures']['add']=1;
$this->user_array['pictures']['useredit']=1;
$this->user_array['pictures']['userdelete']=1;
$this->user_array['pictures']['thumbupload']=1;
$this->user_array['pictures']['turlupload']=1;
$this->user_array['pictures']['search']=1;
$this->user_array['pictures']['abuse']=1;
$this->user_array['ratings']['add']=1;
$this->user_array['messages']['addnew']=1;
$this->user_array['messages']['trashmessage']=1;
$this->user_array['messages']['sentitems']=1;
$this->user_array['messages']['usermessage']=1;
$this->user_array['messages']['reply']=1;
$this->user_array['messages']['delete']=1;
$this->user_array['messages']['sentitemsdelete']=1;
$this->user_array['messages']['messageview']=1;
$this->user_array['messages']['sentview']=1;
$this->user_array['messages']['trashdelete']=1;
$this->user_array['messages']['senttoinbox']=1;
$this->user_array['users']['userindex']=1;
$this->user_array['users']['useredit']=1;
$this->user_array['users']['photo_upload']=1;
$this->user_array['users']['userdelete']=1;
$this->user_array['users']['rview']=1;
$this->user_array['pfavorites']['add']=1;
$this->user_array['pfavorites']['rview']=1;
$this->user_array['pfavorites']['delete']=1;
$this->user_array['albitems']['add']=1;
$this->user_array['albitems']['addtolist']=1;
$this->user_array['albitems']['itemview']=1;
$this->user_array['albitems']['image_gallery']=1;
$this->user_array['categories']['rview']=1;
$this->user_array['categories']['view']=1;
$this->user_array['categories']['prview']=1;
$this->user_array['plsitems']['add']=1;
$this->user_array['plsitems']['addtolist']=1;
$this->user_array['plsitems']['itemview']=1;
$this->user_array['plsitems']['playall']=1;
$this->user_array['plsitems']['userplayall']=1;
$this->user_array['profile']['edit']=1;
$this->user_array['profile']['view']=1;
$this->user_array['favorites']['pview']=1;
$this->user_array['favorites']['add']=1;
$this->user_array['favorites']['delete']=1;

		$data = $this->Session->read('User');
		if($data)
		{
		$priv = $data["privilege"];
		if ($priv=='user' || $priv=='mod')
			return isset($this->user_array[$this->params["controller"]][$this->action]);	
		elseif ($priv =='admin')
			return isset($this->admin_array[$this->params["controller"]][$this->action]);	
		else
			return isset($this->default_array[$this->params["controller"]][$this->action]);	
		}
		
		return false;

}


function dolayout()
{
		$data = $this->Session->read('User');
		if($data)
		{
		$priv = $data["privilege"];
		if ($priv=='user' || $priv=='mod')
			$this->layout = "user";	
		elseif ($priv =='admin')
			$this->layout = "admin";
		else
			$this->layout = "default";
		}
		
		return $data;
		
}

function fix_width($strs = null,$table_name = null,$tvar = null,$limit = null)
{
	$countt = count($strs);
	$i=0;
	foreach($strs as $str)
	{
		 if((string)strlen($strs[$i][$table_name][$tvar]) > $limit)
		 {
		 $strs[$i][$table_name]["fix"] = substr($str[$table_name][$tvar],0,$limit);
		 $strs[$i][$table_name]["fix"] = $strs[$i][$table_name]["fix"]."...";		 
	     $strs[$i][$table_name]["put_u"] = str_replace(' ','_', $strs[$i][$table_name][$tvar]);
		 }
		 else
		 {
 				 $strs[$i][$table_name]["fix"] = $strs[$i][$table_name][$tvar];
		 		 $strs[$i][$table_name]["fix"] = $strs[$i][$table_name]["fix"]."   ";
  			     $strs[$i][$table_name]["put_u"] = str_replace(' ','_', $strs[$i][$table_name][$tvar]);
			}
		 $i++;
		
	}
	return($strs);
	
}
function fix_width_cate($strs = null,$table_name1 = null,$table_name2 = null,$tvar = null,$limit = null)
{
	$countt = count($strs);
	$i=0;
	foreach($strs as $str)
	{
		 if((string)strlen($strs[$i][$table_name1][$table_name2][$tvar]) > $limit)
		 {
		 $strs[$i][$table_name1][$table_name2]["fix"] = substr($str[$table_name1][$table_name2][$tvar],0,$limit);
		 $strs[$i][$table_name1][$table_name2]["fix"] = $strs[$i][$table_name1][$table_name2]["fix"]."...";		 
	     $strs[$i][$table_name1][$table_name2]["put_u"] = str_replace(' ','_', $strs[$i][$table_name1][$table_name2][$tvar]);
		 }
		 else
		 {
 				 $strs[$i][$table_name1][$table_name2]["fix"] = $strs[$i][$table_name1][$table_name2][$tvar];
		 		 $strs[$i][$table_name1][$table_name2]["fix"] = $strs[$i][$table_name1][$table_name2]["fix"]."   ";
  			     $strs[$i][$table_name1][$table_name2]["put_u"] = str_replace(' ','_', $strs[$i][$table_name1][$table_name2][$tvar]);
			}
		 $i++;
		
	}
	return($strs);
	
}
function create_thumbnail($input_filepath = null,$output_filepath = null)
{
$ssettings = $this->Sitesetting->findbyid(1);
$cmd = $ssettings["Sitesetting"]["ImageMagicPath"]." -size 200x200 ".$input_filepath." -thumbnail 100x100 -background white  -gravity center -extent 100x100 ".$output_filepath;
	$ret = system($cmd);
//	return(system($cmd));

}
function create_mediumthumbnail($input_filepath = null,$output_filepath = null)
{
$ssettings = $this->Sitesetting->findbyid(1);
$cmd = $ssettings["Sitesetting"]["ImageMagicPath"]." ".$input_filepath." -resize 200x200 ".$output_filepath;
	$ret = system($cmd);
//	return(system($cmd));

}
function gd_thumbnail($input_filepath = null,$output_filepath = null,$ext = null)
{
		
		$thumbwidth = 100;
		$thumbheight = 100;
		$medthumbhei = 360;
		$medthumbwid = 480;
		list($width, $height) = getimagesize($input_filepath);
		$ratio_orig = $width/$height;
//		echo $orgfile;
		if ($thumbwidth/$thumbheight > $ratio_orig) {
   		  $thumbwidth = $thumbheight*$ratio_orig;
		} else {
		   $thumbheight = $thumbwidth/$ratio_orig;
		}
		
//		$thumbname = $actualfile;
		$ext = strtolower($ext);
		$tn = imagecreatetruecolor($thumbwidth, $thumbheight);
		if($ext == "jpg")
		$image = imagecreatefromjpeg($input_filepath);
		else if($ext == "png")
		$image = imagecreatefrompng($input_filepath);
		else if($ext == "gif")
		$image = imagecreatefromgif($input_filepath);
		imagecopyresized($tn, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height); 
		imagejpeg($tn, $output_filepath , 100); 
}

function gd_mediumthumbnail($input_filepath = null,$output_filepath = null,$ext = null)
{
		
		$thumbwidth = 100;
		$thumbheight = 100;
		$medthumbhei = 250;
		$medthumbwid = 250;
		list($width, $height) = getimagesize($input_filepath);
		$ratio_orig = $width/$height;
//		echo $orgfile;
		if ($medthumbwid/$medthumbhei > $ratio_orig) {
   		  $medthumbwid = $medthumbhei*$ratio_orig;
		} else {
		   $medthumbhei = $medthumbwid/$ratio_orig;
		}
		
//		$thumbname = $actualfile;
		$ext = strtolower($ext);
		$mthumbpath = $uploadDir . $hash . "_M.jpg"; //// Path where thumb nail image will be stored
		$tn = imagecreatetruecolor($medthumbwid, $medthumbhei);		
		imagejpeg($tn, $output_filepath , 100); 
		
		if($ext == "jpg")
		$image = imagecreatefromjpeg($input_filepath);
		else if($ext == "png")
		$image = imagecreatefrompng($input_filepath);
		else if($ext == "gif")
		$image = imagecreatefromgif($input_filepath);
		
		imagecopyresized($tn, $image, 0, 0,0,0, $medthumbwid, $medthumbhei, $width, $height); 
		imagejpeg($tn, $output_filepath , 100); 
}
function ClientIP() {
        if (env('HTTP_X_FORWARDED_FOR') != null) {
            $ipaddr = preg_replace('/,.*/', '', env('HTTP_X_FORWARDED_FOR'));
        } else {
             if (env('HTTP_CLIENT_IP') != null) {
                 $ipaddr = env('HTTP_CLIENT_IP');
            } else {
                 $ipaddr = env('REMOTE_ADDR');
             }
         }
 
         if (env('HTTP_CLIENTADDRESS') != null) {
             $tmpipaddr = env('HTTP_CLIENTADDRESS');
 
            if (!empty($tmpipaddr)) {
                $ipaddr = preg_replace('/,.*/', '', $tmpipaddr);
          }
       
	    } 
		  return trim($ipaddr);
    }

function beforeFilter()
{

global $sitename,$adminemail,$ads,$ssettings,$head_title,$meta_desc,$meta_key;

//var $authactions = array();
//$anonactions = array('login','logout','search','view','partview','related','rss','signup','uploaded','unadd','mupload','muploaded','upload','completeupload','uploaded','rview','prview','pview','uploadview','picuploadview','enlarge_view','forgot','forgotpassword','forgotemail','fetchurl');



if(!file_exists(WWW_ROOT . DS . "files" . DS . "install.lock") && $this->params["controller"] != "installer")
{
$this->redirect(array('controller'=>'installer','action'=>'index'));
exit();
}
$this->Sitesetting->id = 1;

$ssettings = $this->Sitesetting->findbyid(1);
$ssettings = $ssettings["Sitesetting"];
$this->ssettings = $ssettings;


if($ssettings['SiteCache'] != 'Enabled')
Configure::write('Cache.disable', true);


//Site TItle n url
$sitename = $ssettings["SiteName"];
$adminemail = $ssettings["AdminEmail"];
$this->set('var_for_layout', $sitename);
$this->set('url_for_layout', $ssettings["SiteUrl"]);

if($ssettings['SiteMode']!='Production')
{
Configure::write('debug', 2);
}
//Site Enabled
if((int)$ssettings["SiteEnabled"]==0)
{
if($this->params["controller"] != "pages" && $this->params["controller"] != "users" && $this->params["controller"] != "sitesettings" )
$this->redirect(array('controller'=>'pages','action'=>"undermaintanence"));
}

//Allow Uplaods

if((int)$ssettings["AllowUploads"]==0)
{

if( ($this->params["controller"] == "videos") && ($this->action == 'add' || $this->action == 'upload' || $this->action == 'uploaded'  || $this->action == 'completeupload' ))
$this->redirect(array('controller'=>'pages','action'=>"undermaintanence"));

}
//Anon Views
if((int)$ssettings["AnonViews"]==0)
{

if( $this->action == 'view' && $this->action == 'prview' && $this->Session->check('User') == false )
 $this->redirect(array('controller'=>'users','action'=>'login','next'=>base64_encode($this->params["url"]["url"])));
 

}

//Anon Upload
if((int)$ssettings["AnonUploads"]==0)
{

if( $this->action == 'unadd'  && $this->Session->check('User') == false )
 $this->redirect(array('controller'=>'users','action'=>'login','next'=>base64_encode($this->params["url"]["url"])));

}


	//Set Layout
	$this->layoutPath =  $ssettings["Skin"];
	

	
	//Init ads
	$ads = Cache::read('ads');
	if($ads==false)
	{
	$ads = $this->Ad->findall();
	$ad1 = array();
	for ($i=0;$i<count($ads);$i++)
	{
		$ad1[(int)$ads[$i]['Ad']['id']] = $ads[$i]['Ad']['adcode'];
	}
	$ads = $ad1;
	Cache::write('ads',$ads,'+1 week');
	}
	$this->set('ads_for_layout',$ads);
	$this->set('ads',$ads);
	
	//Init friend/sponsor links
	$ads = Cache::read('linksites');
	if($ads==false)
	{
	$ads = $this->Linksite->findall();
	Cache::write('linksites',$ads,'+1 week');
	}
	$this->set('linksites_for_layout',$ads);
	$this->set('linksites',$ads);
	
	
	
	if($this->params["controller"] != "pages" &&  $this->params["controller"] != "installer" && ($this->checkaccess()))
        {
            if($this->Session->check('User') == false)
            {
                $this->Session->setFlash('You Need To Login to Continue');
				//debug($this->params);die();
                $this->redirect(array('controller'=>'users','action'=>'login','next'=>base64_encode("/".$this->params["url"]["url"])));
            }
        }
		
		
	
	//init some vars for Pagination
	$this->page =	isset($this->params["named"]["page"])?$this->params["named"]["page"]:1;
	$this->show =	isset($this->params["named"]["show"])?$this->params["named"]["show"]:10;
	
	
	
}


    function runExternal($cmd,&$code) {
        $descriptorspec = array(
            0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
            1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
            2 => array("pipe", "w") // stderr is a file to write to
        );
       
        $pipes= array();
        $process = proc_open($cmd, $descriptorspec, $pipes);
       
        $output= "";
       
        if (!is_resource($process)) return false;
       
        #close child's input imidiately
        fclose($pipes[0]);
       
        stream_set_blocking($pipes[1],false);
        stream_set_blocking($pipes[2],false);
       
        $todo= array($pipes[1],$pipes[2]);
       
        while( true ) {
            $read= array();
            if( !feof($pipes[1]) ) $read[]= $pipes[1];
            if( !feof($pipes[2]) ) $read[]= $pipes[2];
           
            if (!$read) break;
           
            $ready= stream_select($read, $write=NULL, $ex= NULL, 2);
           
            if ($ready === false) {
                break; #should never happen - something died
            }
           
            foreach ($read as $r) {
                $s= fread($r,1024);
                $output.= $s;
            }
        }
       
        fclose($pipes[1]);
        fclose($pipes[2]);
       
        $code= proc_close($process);
       
        return $output;
    }
	
	
}
?>
Return current item: OSShare