<?php
/*
 * File:        service.class.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For questions, help, comments, discussion, etc., please join the
 * Smarty mailing list. Send a blank e-mail to
 * smarty-general-subscribe@lists.php.net
 *
 * You may contact the author of the service ñlass by e-mail at:
 * public@grik.net
 *
 * @link http://www.grik.net/
 * @copyright 2002-2004 Grigori Kochanov
 * @author Grigori Kochanov <public@grik.net>
 * @version 2.0.3
*/

defined('DEBUG_MODE')||define('DEBUG_MODE'true);
defined('LOG_DIR')||define('LOG_DIR'false);
defined('EOL')||define('EOL'"\r\n");
defined('NON_FATAL') || define ('NON_FATAL'E_NOTICE|E_USER_NOTICE);

class 
Service {

// public fields
var $_SERVER;
var 
$_SESSION;
var 
$_POST;
var 
$_GET;
var 
$_COOKIE;
var 
$_FILES;
var 
$magic_quotes;
var 
$PAGE;
var 
$error_code=0;

//constructor
function Service(){
    
$this->magic_quotes get_magic_quotes_gpc();
    
$scopes = array ('SERVER''POST''GET''COOKIE');
    
$this->_refer_globals($scopes);
}

// void _refer_globals ( array scopes )
function _refer_globals($scopes){
    
$php_version PHP_VERSION;
    
//we don't support PHP versions 2 and 3
    
if ($php_version[0]<4){
        die (
'Sorry, PHP ver. '.$php_version[0].' is not supported. Please upgrade.');
    }

    
// Instead of checking the PHP version, I check the existence of $_SERVER variable. 
    // It exists in PHP >=4.1 even if script is called from a command-line
    
if (!isset($_SERVER)){
        
$prefix 'HTTP_';
        
$postfix '_VARS';
    }else{
    
//for PHP 4.1 and later
        
$prefix '_';
        
$postfix '';
    }
    
//assign referencies to the external data
    //here one can process all external data befor using it in code
    
foreach ($scopes as $scope){
        
$this->{"_".$scope} =& $GLOBALS[$prefix.$scope.$postfix];
    }
    
//just a short way to write if-else statement
    
isset($_SERVER) and $this->_FILES=&$_GET or 
        
$this->_FILES=&$GLOBALS['HTTP_POST_FILES'];
}

//start session if not started already
function session_start(){
    
$session_id $this->unescaped_gpc('PHPSESSID','COOKIE');
    if (
$session_id && strspn ($session_id,'0123456789abcdef') != strlen($session_id)){
        
$this->_COOKIE['PHPSESSID'] = md5(mt_rand().microtime());
    }
    if (empty(
$this->_SESSION['_closed']) && session_id()){
        return 
1;
    }
    if (
headers_sent($file$line)){
        
$this->log('Could not start session - headers have been sent from '.$file.' line '.$linefalse);
        return 
0;
    }
    empty(
$GLOBALS['session_cookie_expire']) or 
        
session_set_cookie_params(60*$GLOBALS['session_cookie_expire']);
    
session_start();
    
$this->_refer_globals(array('SESSION'));
    
$this->_SESSION['_closed']=false;
    return 
2;
}

function 
session_close(){
    
session_write_close();
    
$this->_SESSION['_closed']=true;
}

//returns value of a field with escaped quotes
//taking into account the state of magic_quotes_gpc
function escaped_gpc($field_name$type 'POST'){
    
$scope =& $this->{"_".$type};
    if (!isset(
$scope[$field_name]))
        return 
null;
    if (
$this->magic_quotes)
        return 
$scope[$field_name];
    if (!
is_array($scope[$field_name]))
        return 
addslashes($scope[$field_name]);
    foreach(
$scope[$field_name] as $key=>$value)
        
$scope[$field_name][$key] = addslashes($value);
    return 
$scope[$field_name];
}

//returns value of a field with unescaped quotes
//taking into account the state of magic_quotes_gpc
function unescaped_gpc($field_name$type 'POST'){
    
$scope =& $this->{"_".$type};
    if (!isset(
$scope[$field_name]))
        return 
null;
    if (!
$this->magic_quotes)
        return 
$scope[$field_name];
    if (!
is_array($scope[$field_name]))
        return 
stripslashes($scope[$field_name]);
    foreach(
$scope[$field_name] as $key=>$value)
        
$scope[$field_name][$key] = stripslashes($value);
    return 
$scope[$field_name];
}

//write messages to the script log
function log($message$error false){
    
$today strftime('%Y-%m-%d');
    
$time strftime('%H:%M:%S');
    
$log_file_name LOG_DIR '/' $today;
    
$message trim($message);
    
$string_to_write EOL.($error?$time.' *** Error ***' $time.' event logged').EOL.
        
'REQUEST_URI = '.SITE_URL.$this->_SERVER['REQUEST_URI'].EOL.
        
'SCRIPT_NAME = '.$this->_SERVER['SCRIPT_NAME'].EOL.
        
$message.EOL;
    
$return=@error_log($string_to_write,3,$log_file_name);
    if (!
LOG_DIR || !is_writable(LOG_DIR)){
        @
error_log('Log is not accessable!'.EOL.EOL.$string_to_write1ADMIN_EMAIL);
    }
}

/*eror handler for unexpected errors
 private void _handler(errno, errstr, errfile, errline)
*/
function _handler($errno$errstr$errfile$errline){
    
$errortype = array (
       
1   =>  "Error"
       
2   =>  "Warning"
       
4   =>  "Parsing Error"
       
8   =>  "Notice"
       
16  =>  "Core Error"
       
32  =>  "Core Warning"
       
64  =>  "Compile Error"
       
128 =>  "Compile Warning"
       
256 =>  "User Error"
       
512 =>  "User Warning"
       
1024=>  "User Notice" 
       
); 
    
$error_message=$errfile.' Line '.$errline.EOL.$errortype[$errno].EOL.$errstr.EOL;
    
$error_reporting=ini_get('error_reporting');
    if (
DEBUG_MODE&& $error_reporting&$errno){
        
//@header('Content-type: text/plain');
        
echo EOL.'<br><b>'.$errortype[$errno].'</b> occured <br>'.EOL.
            
'File '.$errfile.'<br> Line '.$errline.' <br> '.$errstr.
            
'<br>'.EOL.' Please contact developer and describe how to replicate this error. ';
    }
    if (
$error_reporting&$errno&~NON_FATAL){
        @
mail(ADMIN_EMAIL,'Error at line '.$errline.' in '.$errfile$error_message);
        
headers_sent() || header('Location: /fatal.html');
        echo 
'<br>Sorry, fatal error occured. Please contact '.ADMIN_EMAIL .' about this.<br> Thank you.';
        
$this->log($error_messagetrue);
        die;
    }elseif(
ini_get('error_reporting')&$errno){
        
$this->log($error_messagefalse);
    }
}

// end of class
}
?>