<?php

/*
=====================================================
 ExpressionEngine - by pMachine
-----------------------------------------------------
 http://www.pmachine.com/
-----------------------------------------------------
 Copyright (c) 2003 - 2007 pMachine, Inc.
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
 http://eedocs.pmachine.com/license.html
=====================================================
 File: core.language.php
-----------------------------------------------------
 Purpose: This class manages language files.
=====================================================
*/


if ( ! defined('EXT'))
{
    exit('Invalid file request');
}



class Language {
    
    var $language   = array();
    var $cur_used   = array();
    
    
    /** -------------------------------------
    /**  Constructor
    /** -------------------------------------*/

    function Language()
    {
    }
    /* END */
    
    
    /** -------------------------------------
    /**  Fetch a language file
    /** -------------------------------------*/

    function fetch_language_file($which = '')
    {
        global $IN, $OUT, $LANG, $SESS, $PREFS, $FNS;
        
        if ($which == '')
        {
            return;
        }
        
        if ($SESS->userdata['language'] != '')
        {
            $user_lang = $SESS->userdata['language'];
        }
        else
        {
			if ($IN->GBL('language', 'COOKIE'))
			{
				$user_lang = $IN->GBL('language', 'COOKIE');
			}
			elseif ($PREFS->ini('deft_lang') != '')
            {
                $user_lang = $PREFS->ini('deft_lang');
            }
            else
            {
                $user_lang = 'english';
            }
        }
        
        $user_lang = $FNS->filename_security($user_lang);
            
        if ( ! in_array($user_lang, $this->cur_used))
        {                
            if ( ! @include PATH_LANG.$user_lang.'/lang.'.$which.EXT)
            {
				if ( ! @include PATH_LANG.'english/lang.'.$which.EXT)
				{
					if ($PREFS->ini('debug') >= 1)
					{
						$error = 'Unable to load the following language file:<br /><br />/lang.'.$which.EXT;
				
						return $OUT->fatal_error($error);
					}
					else
						return;
				}
            }
            
            $this->cur_used[] = $which;
            
            if (isset($L))
            {
            	$this->language = array_merge($this->language, $L);
            	unset($L);
            }
        }
    }
    /* END */
    
    
    /** -------------------------------------
    /**  Fetch a specific line of text
    /** -------------------------------------*/

    function line($which = '', $label = '')
    {
    	global $PREFS;
    
        if ($which != '')
        {
            $line = ( ! isset($this->language[$which])) ? FALSE : $this->language[$which];
                        
            $word_sub = ($PREFS->ini('weblog_nomenclature') != '' AND $PREFS->ini('weblog_nomenclature') != "weblog") ? $PREFS->ini('weblog_nomenclature') : '';
            
            if ($word_sub != '')
            {
                $line = preg_replace("/metaweblog/i", "Tr8Vc345s0lmsO", $line);
                $line = str_replace('"weblog"', 'Ghr77deCdje012', $line);
                $line = str_replace('weblog', strtolower($word_sub), $line);
                $line = str_replace('Weblog', ucfirst($word_sub),    $line);
                $line = str_replace("Tr8Vc345s0lmsO", 'Metaweblog', $line);
                $line = str_replace("Ghr77deCdje012", '"weblog"', $line);
            }
            
            if ($label != '')
            {
                $line = '<label for="'.$label.'">'.$line."</label>";
            }
            
            return stripslashes($line);
        }
    }
    /* END */
}
// END CLASS
?>