<?php

defined('BASEPATH') OR exit('No direct script access allowed');







function daddslashes($string, $force = 1) {

	if(is_array($string)) {

		$keys = array_keys($string);

		foreach($keys as $key) {

			$val = $string[$key];

			unset($string[$key]);

			$string[addslashes($key)] = daddslashes($val, $force);

		}

	} else {

		$string = addslashes($string);

	}

	return $string;

}



function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

	$expiry = 3600 * 24 *365;

	$ckey_length = 4;

	// $key = md5($key != '' ? $key : ge global('authkey'));

	$key = md5($key != '' ? $key : ODZ_AUTHKEY);



	$keya = md5(substr($key, 0, 16));

	$keyb = md5(substr($key, 16, 16));

	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';



	$cryptkey = $keya.md5($keya.$keyc);

	$key_length = strlen($cryptkey);



	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;

	$string_length = strlen($string);



	$result = '';

	$box = range(0, 255);



	$rndkey = array();

	for($i = 0; $i <= 255; $i++) {

		$rndkey[$i] = ord($cryptkey[$i % $key_length]);

	}



	for($j = $i = 0; $i < 256; $i++) {

		$j = ($j + $box[$i] + $rndkey[$i]) % 256;

		$tmp = $box[$i];

		$box[$i] = $box[$j];

		$box[$j] = $tmp;

	}



	for($a = $j = $i = 0; $i < $string_length; $i++) {

		$a = ($a + 1) % 256;

		$j = ($j + $box[$a]) % 256;

		$tmp = $box[$a];

		$box[$a] = $box[$j];

		$box[$j] = $tmp;

		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));

	}



	if($operation == 'DECODE') {

		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {

			return substr($result, 26);

		} else {

			return '';

		}

	} else {

		return $keyc.str_replace('=', '', base64_encode($result));

	}



}





function get_extcredits(){

	$ci = &get_instance();

	$discuz_db = $ci->load->database('discuz', true);

	$extcredits = $discuz_db->get_where('common_setting', array('skey' => 'extcredits'))->row_array();

	$extcredits = addslashes($extcredits['svalue']);

	$extcredits = dunserialize($extcredits);

	if($discuz_db->char_set == 'gbk'){

		$extcredits = odz_encode($extcredits, $discuz_db->char_set);

	}





	return $extcredits;

}



function dunserialize($data) {

	if(($ret = @unserialize($data)) === false) {

		$ret = @unserialize(stripslashes($data));

	}

	return $ret;

}



function odz_encode($data, $char_set = '') {

    if(empty($data)){

        return $data;

    }



    if(is_array($data)) {

        foreach($data as $k => $v) {

            $data[$k] = odz_encode($v);

        }

    } else {

        if (is_string($data) && $char_set != 'utf-8') {

            $data = iconv('gbk', 'utf-8//IGNORE', $data);

        } 

        return $data;

    }

    return $data;

}





function get_discuz_auth_info(){

	$ci = &get_instance();

	@include DISCUZ_ROOT.'./config/config_global.php';



	$_config['cookie']['cookiepre'] = $_config['cookie']['cookiepre'].substr(md5($_config['cookie']['cookiepath'].'|'.$_config['cookie']['cookiedomain']), 0, 4).'_';

	$auth = $ci->input->cookie($_config['cookie']['cookiepre'].'auth');



	$saltkey = $ci->input->cookie($_config['cookie']['cookiepre'].'saltkey');

	$authkey = md5($_config['security']['authkey'].$saltkey);

	return array('auth'=>$auth, 'authkey' => $authkey);

}





function get_forums(){

	$ci = &get_instance();

	$discuz_db = $ci->load->database('discuz', true);

	$discuz_db->where('status', 1);

	$discuz_db->where('type <>', 'group');

	$result = $discuz_db->get('forum_forum')->result_array();

	if($discuz_db->char_set == 'gbk'){

		$result = odz_encode($result, $discuz_db->char_set);

	}

	foreach($result as $row){



		if(isset($forums[$row['fup']])){

			$forums[$row['fup']]['child'][$row['fid']] = $row['name']; 

			continue;

			

		}

		$forums[$row['fid']]['name'] = $row['name']; 

	}

	return $forums;

}

function mb_unserialize($serial_str) { 
  $serial_str= preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str ); 
  $serial_str= str_replace("\r", "", $serial_str); 
  return unserialize($serial_str); 
}


function get_forums_type($fid){

	$ci = &get_instance();

	$discuz_db = $ci->load->database('discuz', true);

	$discuz_db->where('fid', $fid);

	$result = $discuz_db->get('forum_forumfield')->row_array();

	if($discuz_db->char_set == 'gbk'){

		$result = odz_encode($result, $discuz_db->char_set);

	}

	if(empty($result['threadsorts'])){

		return false;

	}


	if($discuz_db->char_set == 'gbk'){
		$threadsort = mb_unserialize($result['threadsorts']);
	}else
	{
		$threadsort = unserialize($result['threadsorts']);
	}
	

	foreach($threadsort['types'] as $sortid => $sortname){

		$return[$sortid]['name'] = $sortname;

	

		$result = $discuz_db->where('sortid', $sortid)->select('optionid')->get('forum_typevar')->result_array();

		$optionid = array();

		foreach($result as $row){

			$optionid[] = $row['optionid'];

		}



		$result = $discuz_db->where_in('optionid', $optionid)

				  ->select('*')->get('forum_typeoption')->result_array();
		if($discuz_db->char_set == 'gbk'){

			$result = odz_encode($result, $discuz_db->char_set);

		}
		foreach($result as $row){

			if($row['type'] == 'image'){

				$return[$sortid]['image'][$row['optionid']] = $row['title'];

				continue;

			}

			$return[$sortid]['fileds'][$row['optionid']] = $row['title'];

		}

	

		

	}



	return $return;

}