<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// translation
if ( ! function_exists('trans')):
	function trans($phrase){
        return ucwords(str_replace('_', ' ', $phrase));
  //       $translate_phrase = $phrase;
	 //    $ci =& get_instance();
		// $ci->load->helper('language');
		// $active_language = $ci->language_model->get_active_language();
		// $ci->lang->load('site_lang',$active_language);
		// $ci->config->set_item('language', $active_language);
  //       if($ci->lang->line($phrase) == FALSE):
  //           //$ci->language_model->create_phrase($active_language,$phrase);            
  //       else:
  //           $translate_phrase = $ci->lang->line($phrase,FALSE);
  //       endif;
	 //    return $translate_phrase;
    }
endif;

// configuration helper
if (! function_exists('ovoo_config')):
	function ovoo_config($title)
    {
    	$ci =& get_instance();
        return $ci->common_model->get_config($title);
    }
endif;

if (! function_exists('is_demo')):
    function is_demo()
    {
        $ci =& get_instance();
        return ($ci->config->item('is_demo') === true ) ? true : false;
    }
endif;


// theme helper
if (! function_exists('active_theme')):
	function active_theme()
    {
    	$ci =& get_instance();
        return $ci->common_model->get_active_theme();
    }
endif;

//generate slug
if (!function_exists('str_slug')) {
    function str_slug($str)
    {
        return url_title($str, "-", true);
    }
}

// ============================================
// Secure Password Hashing Functions
// ============================================

/**
 * Check if a hash is in MD5 format (legacy)
 * MD5 hashes are 32 characters long and hexadecimal
 * 
 * @param string $hash The hash to check
 * @return bool True if the hash appears to be MD5
 */
if (!function_exists('is_md5_hash')) {
    function is_md5_hash($hash)
    {
        if (empty($hash) || strlen($hash) !== 32) {
            return false;
        }
        // Check if it's a valid hexadecimal string (MD5 format)
        return ctype_xdigit($hash) && !preg_match('/^\$2[ayb]\$|\$argon2/', $hash);
    }
}

/**
 * Hash a password using secure algorithm (bcrypt)
 * Uses PASSWORD_BCRYPT with cost factor 12 for good security/performance balance
 * 
 * @param string $password Plain text password
 * @return string|false Hashed password or FALSE on failure
 */
if (!function_exists('secure_password_hash')) {
    function secure_password_hash($password)
    {
        if (empty($password)) {
            return false;
        }
        // Use bcrypt with cost 12 (good balance between security and performance)
        // For PHP 7.2+, you could use PASSWORD_ARGON2ID if available
        $options = array('cost' => 12);
        return password_hash($password, PASSWORD_BCRYPT, $options);
    }
}

/**
 * Verify a password against a stored hash
 * Supports both legacy MD5 hashes and modern bcrypt/Argon2 hashes
 * 
 * @param string $password Plain text password to verify
 * @param string $stored_hash Stored hash from database (MD5 or bcrypt/Argon2)
 * @return bool True if password matches, false otherwise
 */
if (!function_exists('secure_password_verify')) {
    function secure_password_verify($password, $stored_hash)
    {
        if (empty($password) || empty($stored_hash)) {
            return false;
        }
        
        // Check if stored hash is MD5 (legacy)
        if (is_md5_hash($stored_hash)) {
            // Verify against MD5 hash
            $md5_password = md5($password);
            return hash_equals($stored_hash, $md5_password);
        }
        
        // Use PHP's password_verify for modern hashes (bcrypt/Argon2)
        return password_verify($password, $stored_hash);
    }
}

/**
 * Verify password and upgrade MD5 hash to secure hash if needed
 * This function verifies the password and returns upgrade information
 * 
 * @param string $password Plain text password
 * @param string $stored_hash Stored hash from database
 * @return array Array with 'verified' (bool) and 'needs_upgrade' (bool) keys
 */
if (!function_exists('secure_password_verify_with_upgrade_check')) {
    function secure_password_verify_with_upgrade_check($password, $stored_hash)
    {
        $result = array(
            'verified' => false,
            'needs_upgrade' => false
        );
        
        if (empty($password) || empty($stored_hash)) {
            return $result;
        }
        
        // Check if stored hash is MD5 (legacy)
        if (is_md5_hash($stored_hash)) {
            // Verify against MD5 hash
            $md5_password = md5($password);
            if (hash_equals($stored_hash, $md5_password)) {
                $result['verified'] = true;
                $result['needs_upgrade'] = true; // Needs upgrade to secure hash
            }
            return $result;
        }
        
        // Use PHP's password_verify for modern hashes
        $result['verified'] = password_verify($password, $stored_hash);
        $result['needs_upgrade'] = false; // Already using secure hash
        
        return $result;
    }
}