<?php

//函数集合
if(!function_exists('log_broker_credit')) {
  function log_broker_credit($uid, $operation, $kind) {
    GLOBAL $db;

    $ss = array();
    $ss["uid"] = $uid;
    $ss["operation"] = $operation;
    $ss["kind"] = $kind;
    $ss["dateline"] = time();

    $q = insert_sql('log_broker_credit', $ss);
    $db->Execute($q);
}  
}
if(!function_exists('log_agent_credit')) {
function log_agent_credit($uid, $operation, $kind) {
    GLOBAL $db;

    $ss = array();
    $ss["uid"] = $uid;
    $ss["operation"] = $operation;
    $ss["kind"] = $kind;
    $ss["dateline"] = time();

    $q = insert_sql('log_agent_credit', $ss);
    $db->Execute($q);
}
}



//获取IP
if(!function_exists('GetIP')) {
function GetIP() {

    $ip = $_SERVER["REMOTE_ADDR"];
    if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        $tip = split(",", $_SERVER["HTTP_X_FORWARDED_FOR"]);
        $ip = $tip[0];
    }
    return(trim($ip));
}
}
if(!function_exists('tonyForm')) {
function tonyForm($tag, $name, $option_arr, $value = '', $td_num = 1, $validate = '') {
    switch ($tag) {

        case 'select':
            $str = "<select name='$name' id='$name' $validate>";
            foreach ($option_arr as $k => $v) {
                if ($value == $k && $value != '') {
                    $str .= "<option value='$k' selected>$v</option>";
                } else {
                    $str .= "<option value='$k'>$v</option>";
                }
            }
            $str.="</select>";
            break;

        case 'radio':
            $checked_arr = explode(',', $value);
            $td_width = "width=" . floor(100 / $td_num) . "%";
            $blank_i = ceil(count($option_arr) / $td_num) * $td_num - count($option_arr);
            for ($i = 0; $i < $blank_i; $i++)
                $option_arr[] = "";//补全使之成为整数

            $str = "<table align=center border=0 cellspacing=0 width=100%> \n";
            $i = 1;
            foreach ($option_arr as $k => $v) {
                $j = $i % $td_num;
                if ($j == 1)
                    $str.="<tr>";
                //$checked= $k==$value ? "checked":"";
                $checked = in_array($k, $checked_arr) ? "checked" : "";
                $input = $v ? "<input type=radio name=$name value='$k' $checked>$v" : "&nbsp;";
                $str.="<td $td_width>$input</td>";
                if ($j == 0)
                    $str.="</tr> \n";
                $i++;
            }
            $str.="</table> \n";
            break;

        case 'checkbox':
            $checked_arr = explode(',', $value);
            $td_width = "width=" . floor(100 / $td_num) . "%";
            $blank_i = ceil(count($option_arr) / $td_num) * $td_num - count($option_arr);
            for ($i = 0; $i < $blank_i; $i++)
                $option_arr[] = "";//补全使之成为整数

            $str = "<table align=center border=0 cellspacing=0 width=100%> \n";
            $i = 1;
            foreach ($option_arr as $k => $v) {
                $j = $i % $td_num;
                if ($j == 1)
                    $str.="<tr>";
                $checked = in_array($k, $checked_arr) ? "checked" : "";
                $input = $v ? "<input type=checkbox name={$name}[] value='$k' $checked>$v" : "&nbsp;";
                $str.="<td $td_width>$input</td>";
                if ($j == 0)
                    $str.="</tr> \n";
                $i++;
            }
            $str.="</table> \n";
            break;

        case 'text':
            $checked_arr = explode(',', $value);
            $td_width = "width=" . floor(100 / $td_num) . "%";
            $blank_i = ceil(count($option_arr) / $td_num) * $td_num - count($option_arr);
            for ($i = 0; $i < $blank_i; $i++)
                $option_arr[] = "";//补全使之成为整数

            $str = "<table align=center border=0 cellspacing=0 width=100%> \n";
            $i = 1;
            foreach ($option_arr as $k => $v) {
                $j = $i % $td_num;
                if ($j == 1)
                    $str.="<tr>";
                $checked = in_array($k, $checked_arr) ? "checked" : "";
                $input = $v ? "<input type=checkbox name={$name}[] value='$k' $checked>$v" : "&nbsp;";
                $str.="<td $td_width>$input</td>";
                if ($j == 0)
                    $str.="</tr> \n";
                $i++;
            }
            $str.="</table> \n";
            break;
    }
    return $str;
}
}
if(!function_exists('create_htmlable')) {
function create_htmlable($name,$label,$value='',$val='',$class='',$style='',$js=''){//创建html标签
		//name标签名称|label标签类型|value标签的值(当为checkbox时为数组)|val主要用于radio和checkbox
		$html = '';
		if($class) $cls = 'class="'.$class.'"';
		if($style) $sty = 'style="'.$style.'"';
		if($name)  $nam = 'name="'.$name.'"';

		if($label=='text' || $label=='password'){//文本框
			if($name) $id = 'id="'.$name.'"';
			$html = '<input '.$cls.' type="'.$label.'" '.$nam.' '.$id.' value="'.$value.'" '.$sty.' '.$js.'>';			
		}elseif($label=='radio'){//radio 
			if(is_array($val)){
				foreach($val as $key=>$item){
					if($key==$value) $checked='checked';else $checked = '';
					$html.=' <input type="'.$label.'" '.$nam.' '.$checked.' value="'.$key.'" '.$sty.' '.$js.'>'.$item;
				}
			}
		}elseif($label=='checkbox'){//checkbox
			if(is_array($val)){
				foreach($val as $key=>$item){
					if(in_array($key,$value)) $checked='checked';else $checked = '';
					$html.=' <input type="'.$label.'" '.$nam.' '.$checked.' value="'.$key.'" '.$sty.' '.$js.'>'.$item;
				}
			}
		}elseif($label=='select'){//下拉框
			if(is_array($val)){
				if($name) $id = 'id="'.$name.'"';
				$html.='<select '.$nam.' '.$id.'  '.$sty.' '.$js.'>';
				foreach($val as $key=>$item){
					if($key==$value) $selected = 'selected'; else $selected='';						
					$html.='<option '.$selected.' value="'.$key.'">'.$item.'</option>';
					
				}
				$html.='</select>';
			}			
		}
		return $html;
	}
}
if(!function_exists('ctrl_select')) {
function ctrl_select($opt_para, $ctrl_name, $ctrl_value, $style_ext = "class=flat1") {
    /**   2002.03.05
      ctrl_select 函数参数说明
      @param [opt_para]   : opt_para是一个数组，元素含义如下
      [opt_arr]    下拉框选项的数组，可以是简单数组，也可以是关联数组
      [field_key]  如果是关联数组，field_key表示，  下拉框的key  出自关联数组的哪一个field，为空表示出自简单数组的key
      [field_value]如果是关联数组，field_value表示，下拉框的value出自关联数组的哪一个field，为空表示出自简单数组的value
      @param [ctrl_name]  : select控件的名称
      @param [ctrl_value]	: select控件选择的值
      @param [style_ext]  : 扩充的样式，可以是样式或js
      返回select的string
     * */
    // 1.重新构建数组，将关联数组化为简单数组
    $old_opt_arr = (is_array($opt_para[opt_arr])) ? ($opt_para[opt_arr]) : (array());
    FOR ($opt_arr = array(), reset($old_opt_arr), $i = 0; $i < count($old_opt_arr); $i++) {
        $key = key($old_opt_arr);
        $field_key = ($opt_para[field_key] != "") ? ($old_opt_arr[$key][$opt_para[field_key]] ) : ($key);
        $field_value = ($opt_para[field_value] != "") ? ($old_opt_arr[$key][$opt_para[field_value]]) : ($old_opt_arr[$key]);
        $opt_arr[$field_key] = $field_value;
        next($old_opt_arr);
    }

    // 2.生成
    $ss = "<select name=\"" . $ctrl_name . "\" " . $style_ext . ">\n";
    for (reset($opt_arr), $i = 1; $i <= count($opt_arr); $i++) {
        $key = key($opt_arr);
        $value = $opt_arr[$key];
        if (strval($key) == strval($ctrl_value))
            $selected_flag = "selected"; else
            $selected_flag = "";
        $ss.="  <option value=\"" . $key . "\" " . $selected_flag . ">" . $value . "</option>\n";
        next($opt_arr);
    }
    $ss.="</select>";

//if ($ctrl_name=="") echo $key."___".$ctrl_value."<br>";

    RETURN $ss;
}
}
if(!function_exists('tony_flag2name')) {
function tony_flag2name($flag, $arr) {
    //return $flag;
    return $arr[$flag];
}
}
if(!function_exists('tony_flag2name2')) {
function tony_flag2name2($str, $arr) {
    $arr_flag = explode(",", $str);
    foreach ($arr_flag as $key => $flag) {
        $row[$key] = $arr[$flag];
    }
    return implode(",", $row);
}
}
if(!function_exists('csubstr2')) {
function csubstr2($str_cut, $length = 30) {

    if (strlen($str_cut) > $length) {
        for ($i = 0; $i < $length; $i++)
            if (ord($str_cut[$i]) > 128)
                $i++;
        //  $str_cut = substr($str_cut,0,$i) . "...";
        $str_cut = substr($str_cut, 0, $i);
    }
    return $str_cut;
}
}


/* * **************************************************** */


if(!function_exists('tony_date')) {
function tony_date($format, $timestamp) {
    $ss = $timestamp == '' ? '' : date($format, $timestamp);
    return $ss;
}
}
if(!function_exists('ischecked')) {
function ischecked($args) {
    $ss = $args > 0 ? "checked" : "";
    Return $ss;
}
}


/* * ******************************************************* */
if(!function_exists('tony_view2')) {
function tony_view2($pic, $picPath, $picid = '', $blockid = '') {
    GLOBAL $db;

    $url = $picPath;
    $ss = $picPath ? "<a href='$url' title='<img src=$url width=200 height=200 />'>查看</a>&nbsp;&nbsp;<a href=$url onclick=\"return delpic('$pic');\">删除</a>&nbsp;" . $block . "" : "";
    return $ss;
}
}
if(!function_exists('tony_view')) {
function tony_view($pic, $picPath) {
    //$url=PUBURL.$picPath;
    $url = $picPath;
    $ss = $picPath ? "<a href='$url' target='_blank' title='<img src=$url width=200 height=200 />' >查看</a>&nbsp;&nbsp;<a href=$url onclick=\"return delpic('$pic');\">删除</a>" : "";
    return $ss;
}
}
if(!function_exists('tony_actionlog')) {
function tony_actionlog($tbl, $rowid, $msg) {
    GLOBAL $db, $web_auth;

    $ss = array();
    $ss["tbl"] = $tbl;
    $ss["rowid"] = $rowid;
    $ss["operator"] = $_SESSION[$web_auth]["authname"];
    $ss["msg"] = $msg;
    $ss["ip"] = GetIP();
    $ss["date"] = date("YmdHi");

    $q = insert_sql('log', $ss);
    $db->Execute($q);
}
}
if(!function_exists('log_view')) {
function log_view($tbl, $rowid) {
    GLOBAL $db, $web_auth;

    $ss = array();
    $ss["tbl"] = $tbl;
    $ss["rowid"] = $rowid;
    $ss["username"] = $_SESSION[$web_auth]["authname"];
    $ss["dateline"] = date("YmdHi");
    $ss["ip"] = GetIP();

    $q = insert_sql('log_view', $ss);
    $db->Execute($q);
}
}
if(!function_exists('agent_actionlog')) {
function agent_actionlog($tbl, $rowid, $msg) {

    GLOBAL $db, $web_auth, $infotype, $statistics;

	if (is_array($rowid)) {
		$arr = $rowid;
	} else {
		$arr = explode(",", $rowid);
	}

    $ss = array();
    $ss["agentcode"] = $_SESSION[$web_auth]["agentcode"];
    $ss["uid"] = $_SESSION[$web_auth]["uid"];
    $ss["infotype"] = $infotype;
    $ss["tbl"] = $tbl;
    $ss["msg"] = $msg;       //0:删除 1:增加 2:刷新
    $ss["y"] = date('Y');
    $ss["m"] = date('m');
    $ss["d"] = date('d');
    $ss["time"] = date('Hi');
    $ss["ip"] = GetIP();

    foreach ($arr as $k => $v) {
        $ss["rowid"] = $v;
        $q = insert_sql('log_agent', $ss);
        $db->Execute($q);

		//新版统计2011-10-55
		if ($_SESSION[$web_auth]["agentcode"] == 'B000' && $msg == 2) {
			$ymd = date("Y-m-d");

			$s = "select id from log_refresh where uid='" . $_SESSION[$web_auth]["uid"] . "' and tbl='" . $tbl . "' and rowid='" . $v . "' and ymd='" . $ymd . "'";
			$r = $db->GetRow($s);
			if ($r) {
				$db->Execute("update log_refresh set num=num+1 where id=$r[id] ");
			} else {
				$db->Execute("insert into log_refresh(uid,tbl,rowid,ymd,num)values('" . $_SESSION[$web_auth]["uid"] . "','" . $tbl . "','" . $v . "','" . $ymd . "',1)");
			}
		}
	}

    if ($_SESSION[$web_auth]["agentcode"] == 'B000' && $msg != 0) {
        $time = date("Y-m-d");
        $num = count($arr);

        $s = "select id from log_broker_tmp where uid='" . $_SESSION[$web_auth]["uid"] . "' and updatetime='" . $time . "'";
        $r = $db->GetRow($s);
        if ($r) {
            $db->Execute("update log_broker_tmp set num=num+$num where id=$r[id] ");
        } else {
            $db->Execute("insert into log_broker_tmp(num,agentcode,uid,updatetime)values('" . $num . "','" . $_SESSION[$web_auth]["companycode"] . "','" . $_SESSION[$web_auth]["uid"] . "','" . $time . "')");
        }
    }
}
}
if(!function_exists('tony_str_replace')) {
function tony_str_replace($arr, $str) {
    foreach ($arr as $key => $value) {
        $arr_from[] = $key;
        $arr_to[] = $value;
    }

    return str_replace($arr_from, $arr_to, $str);
}
}
if(!function_exists('tony_fileupload_add')) {
function tony_fileupload_add($position) {
    $ss = "<input type=file name='$position'>&nbsp;图片描述:<input type='text' size=20 name='{$position}_title' id='{$position}_title'>";
    return $ss;
}
}
if(!function_exists('tony_fileupload_modify')) {
function tony_fileupload_modify($tbl, $rowid, $position) {
    global $db;

    $uploadtbl = get_upload_tblname($tbl, $rowid);

    $row = $db->GetRow("select * from $uploadtbl where tbl='$tbl' and rowid='$rowid' and position='$position'");

    $ss = "<input type=file name='$position'>&nbsp;图片描述:<input type='text' size=20 name='{$position}_title' id='{$position}_title' value='$row[title]'>&nbsp;<span id='{$position}_control'>" . tony_view($position, $row[filename]) . "</span>";

    return $ss;
}
}
if(!function_exists('get_upload_tblname')) {
function get_upload_tblname($tbl, $id) {

	return 'upload';

    if ($tbl == 'sell' and $id < 16679765)
        return 'upload0';

    if ($tbl == 'rent' and $id < 7672437)
        return 'upload0';

    return 'upload';
}
}
if(!function_exists('auth_diff')) {
function auth_diff($auth1, $auth2) {
    $arr = array_diff(explode(",", $auth1), explode(",", $auth2));

    return implode(",", $arr);
}
}
if(!function_exists('get_auth_cp')) {
function get_auth_cp($auth_scope, $auth_str, $name = 'auth') {
    $auth_module = get_auth_module($auth_scope);

    $ss = "<script>
	$(function() {
		$('.grpname').click(function() {
			v=$(this).attr('checked') ?true:false;
			$(this).parent().next('table').find('input[@type=checkbox]').attr('checked',v);

		});
	});
</script> ";
    foreach ($auth_module as $grpkey => $grpname) {

        $ss.="<span class=auth_module><input type='checkbox' class='grpname'><font color=green>$grpname</font></span>";
        $arr = get_authgrp_arr($auth_scope, $grpkey);
        $ss.=tonyForm('checkbox', $name, $arr, $auth_str, 6);
    }
    return $ss;
}
}







?>