init.php 19.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
<?php
if (!defined('ABSPATH')) {
    exit('Direct\'s not allowed');
}
function convertip_tiny($ip, $ipdatafile) {

	static $fp = NULL, $offset = array(), $index = NULL;

	$ipdot = explode('.', $ip);
	$ip    = pack('N', ip2long($ip));

	$ipdot[0] = (int)$ipdot[0];
	$ipdot[1] = (int)$ipdot[1];

	if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
		$offset = @unpack('Nlen', @fread($fp, 4));
		$index  = @fread($fp, $offset['len'] - 4);
	} elseif($fp == FALSE) {
		return  '- Invalid IP data file';
	}

	$length = $offset['len'] - 1028;
	$start  = @unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);

	for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {

		if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
			$index_offset = @unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
			$index_length = @unpack('Clen', $index{$start + 7});
			break;
		}
	}

	@fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
	if($index_length['len']) {
		return '- '.@fread($fp, $index_length['len']);
	} else {
		return '- Unknown';
	}

}

//add js, css
add_action('admin_enqueue_scripts', 'cf7_custom_wp_admin_style');
function cf7_custom_wp_admin_style()
{
    wp_register_script('cf7d_js', CF7D_PLUGIN_URL . '/admin/assets/js/cf7d.js', array('jquery'));

    wp_register_style('cf7d', CF7D_PLUGIN_URL . '/admin/assets/css/cf7d.css');
    wp_enqueue_style('cf7d');
}

//add admin menu page
add_action('admin_menu', 'cf7d_register_custom_submenu_page');
function cf7d_register_custom_submenu_page()
{
    $menu = add_submenu_page('wpcf7', 'Database', 'Database', 'manage_options', 'cf7-data', 'cf7d_custom_submenu_page_callback');
    add_action('load-' . $menu, 'cf7d_form_action_callback');
}
function cf7d_custom_submenu_page_callback_bak()
{
    if (!class_exists('WP_List_Table')) {
        require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
    }
    require_once 'table.php';
    $entries = new Cf7_Entries_List();
    ?>
    <div class="wrap">
        <h2>WP_List_Table Class Example</h2>

        <div id="poststuff">
            <div id="post-body" class="metabox-holder columns-2">
                <div id="post-body-content">
                    <div class="meta-box-sortables ui-sortable">
                        <form method="post">
                            <?php
                            $entries->prepare_items();
                            $entries->display(); ?>
                        </form>
                    </div>
                </div>
            </div>
            <br class="clear">
        </div>
    </div>
    <?php
}
function cf7d_custom_submenu_page_callback()
{
    global $wpdb;
	if(isset($_POST['sbt_google_js'])&&$_POST['sbt_google_js']){
		$google_js = $_POST['google_js'];
		update_option('_google_js',$google_js);		
	}
	$google_js = get_option('_google_js');
    $tinyipfile = ABSPATH . 'wp-content/plugins/contact-form-7-database/ipdata/tinyipdata.dat';
    wp_enqueue_script('cf7d_js');
    wp_enqueue_script('jquery-ui-sortable');
    echo '<div class="wrap page-inquiry" style="margin: 20px 0 0 5px;">';
    if (!class_exists('WPCF7_Contact_Form_List_Table')) {
        require_once WPCF7_PLUGIN_DIR . '/admin/includes/class-contact-forms-list-table.php';
    }

    $list_table = new WPCF7_Contact_Form_List_Table();
    $list_table->prepare_items();

    $first_form_id = 20;
    $fid = ((isset($_GET['fid'])) ? (int) $_GET['fid'] : '');
    $search = ((isset($_GET['search'])) ? addslashes($_GET['search']) : '');

    /*echo '<form action="'.admin_url('admin.php').'" method="GET">';
    echo '<div style="float:left;margin:3px 3px 0 0;font-weight:bold;">切换询盘表单:</div><input type="hidden" name="page" value="cf7-data" />';
    echo '<select name="fid" onchange="this.form.submit();">';
    echo '<option value="" '.(( count($list_table->items) == 0 ) ? 'selected="selected"' : '').' >'.__('Choose form').'</option>';
    $i = 0;
    foreach ($list_table->items as $k => $v) {
        if ($first_form_id == 0 && $i == 0) {
            $first_form_id = $v->id;

            if (empty($fid) && ($first_form_id > 0)) {
                $fid = $first_form_id;
            }
        }
        echo '<option value="'.$v->id.'" '.(( !empty($fid) && ($fid == $v->id)) ? 'selected="selected"' : '').'>'.$v->title.'</option>';
    }
    echo '</select>';
    echo '</form>';*/
    $current_user = wp_get_current_user();
	$username = $current_user->user_login;
    if (!empty($fid)) {
        
        $cf7d_entry_order_by = apply_filters('cf7d_entry_order_by', '`data_id` DESC');
        $cf7d_entry_order_by = trim($cf7d_entry_order_by);

        $items_per_page = apply_filters('cf7d_entry_per_page', 20);
        $page = isset($_GET['cpage']) ? abs((int)$_GET['cpage']) : 1;
        $offset = ( $page * $items_per_page ) - $items_per_page;

        $query = sprintf("SELECT * FROM `".$wpdb->prefix."cf7_data_entry` WHERE `cf7_id` = %d AND data_id IN(SELECT * FROM (SELECT data_id FROM `".$wpdb->prefix."cf7_data_entry` WHERE 1 = 1 and cf7_id=$fid ".((!empty($search)) ? "AND `value` LIKE '%%".$search."%%'" : "")." GROUP BY `data_id` ORDER BY ".$cf7d_entry_order_by." LIMIT %d,%d) temp_table) ORDER BY " . $cf7d_entry_order_by, $fid, $offset, $items_per_page);
        $data = $wpdb->get_results($query);
        $data_sorted = cf7d_sortdata($data);

        $fields = cf7d_get_db_fields($fid);

        $total = $wpdb->get_results("SELECT data_id FROM `".$wpdb->prefix."cf7_data_entry` WHERE `cf7_id` = " . (int)$fid . " ".((!empty($search)) ? "AND `value` LIKE '%%".$search."%%'" : "")." GROUP BY `data_id`");
        $total = count($total);
		if( $username == 'pat' ) {
	        $entry_actions = array(
	                'delete' => '批量删除'
	            );
		}
			
        $entry_actions = apply_filters('cf7d_entry_actions', $entry_actions);
        ?>
	 
        <form action="" method="GET" id="cf7d-admin-action-frm">
            <div class="inquiry-list-head">
            <span class="filter-toggle"></span>
            <input type="hidden" name="page" value="cf7-data">
            <input type="hidden" name="fid" value="<?php echo $fid; ?>">
            <div class="tablenav top xs-filter-hide">
                <div class="alignleft actions bulkactions">
                    <label for="bulk-action-selector-top" class="screen-reader-text"><?php _e('Select bulk action');?></label>
                    <select name="action" id="bulk-action-selector-top">
                        <option value="-1"><?php _e('Bulk Actions'); ?></option>
                        <?php echo cf7d_arr_to_option($entry_actions); ?>
                    </select>
                    <input id="doaction" name="btn_apply" class="button action" value="<?php _e('Apply'); ?>" type="submit" />
                    <?php do_action('cf7d_after_bulkaction_btn', $fid); ?>                    
                </div>
                <div class="tablenav-pages">
                    <span class="displaying-num"><?php echo (($total == 1) ?
                    '1 ' . __('item') :
                    $total . ' ' . __('items')) ?></span>
					<span class="pagination-links">
                        <?php
                        echo paginate_links(array(
                            'base' => add_query_arg('cpage', '%#%'),
                            'format' => '',
                            'prev_text' => __('&laquo;'),
                            'next_text' => __('&raquo;'),
                            'total' => ceil($total / $items_per_page),
                            'current' => $page
                        ));
                        ?>
                    </span>

                </div>
                </div>
            </div>
            <table class="wp-list-table widefat fixed striped posts cf7d-admin-table">
                <thead>
                    <tr>
                        <?php
                        echo '<th id="cb" class="manage-column column-cb check-column"><input type="checkbox" id="cb-select-all-1" /></th>';
      					//print_r($fields);exit;
                        foreach ($fields as $k => $v) {
                          	if($k == 'captcha' || $k == 'ip_area'){continue;}
							if($k=='readed'){
								echo '<th class="manage-column gd-xs-hide" data-key="'.$v.'">是否已读</th>';
							}else if($k=='submit_ip'){
                            	echo '<th class="manage-column gd-xs-hide" data-key="'.$v.'">国家/地区(IP)</th>';
                            }
                            else if($v!='询盘发送时间' && $v!='邮箱(Email)'){
                            echo '<th class="manage-column gd-xs-hide '.$v.'" data-key="'.$v.'">'.cf7d_admin_get_field_name($v).'</th>';
                            }
							else{
                              	
                                echo '<th class="manage-column '.$v.'" data-key="'.$v.'">'.cf7d_admin_get_field_name($v).'</th>';
                            }
                            
                        }
                        ?>
                        <?php do_action('cf7d_admin_after_heading_field'); ?>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($data_sorted as $k => $v) {
                        echo '<tr>';
                        echo '<th class="check-column" scope="row"><input id="cb-select-'.$k.'" type="checkbox" name="del_id[]" value="'.$k.'" /></th>';
                        foreach ($fields as $k2 => $v2) {
                          	if($k2 == 'captcha' || $k2 == 'ip_area'){continue;}
                          	if($v2 == '发送页面'){
                                echo '<td class="manage-column gd-xs-hide '.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.'<a target="_blank" href="'.((isset($v[$k2])) ? $v[$k2] : '&nbsp;').'" title="'.((isset($v[$k2])) ? $v[$k2] : '&nbsp;').'">'.((isset($v[$k2])) ? mb_strimwidth($v[$k2],0,36,'...') : '&nbsp;').'</a></td>';
                            }else if($k2=='submit_ip'){
                              	if(isset($v['ip_area']) && $v['ip_area']){
                                	echo '<td class="gd-xs-hide '.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? $v['ip_area'].'('.$v[$k2].')' : '&nbsp;').'</td>';
                                }else{
                                  	$ip_area = get_ip_from_taobaoapi($v[$k2]);
                                  	//$wpdb->query($wpdb->prepare('INSERT INTO  wp_cf7_data_entry(`cf7_id`, `data_id`, `name`, `value`) VALUES (%d,%d,%s,%s)', 19, $k, 'ip_area', $ip_area));
                                	echo '<td class="gd-xs-hide '.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? $ip_area.'('.$v[$k2].')' : '&nbsp;').'</td>';
                                }	
							}else if($k2=='Email'){
								if(isset($v[$k2])){
									$img_twitter="/wp-content/plugins/contact-form-7/images/comment_twitter_no.png";
									if($res->color=="red"){
										$img_twitter="/wp-content/plugins/contact-form-7/images/comment_twitter.png";
									}
								}
								
	 							if($username == 'visitor'){
                                	echo '<td class="'.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? (substr($v[$k2],0,4).'***') : '&nbsp;').'</td>';
                                }else{
                                	echo '<td class="'.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? $v[$k2] : '&nbsp;').'</td>';
                                }
							}else if($k2=='readed'){
	 							echo '<td class="gd-xs-hide '.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.(($v[$k2]==1) ? "已读(SEO)" : "未读(SEO)").'</td>';
							}else if( $v2!='询盘发送时间' &&  $v2!='邮箱(Email)'){
                                if($username == 'visitor'){
                                        if($k2=='Phone'){
                                            echo '<td class="'.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2]) && $v[$k2]) ? (substr($v[$k2],0,4).'***') : '&nbsp;').'</td>';
                                        }else{
                                            echo '<td class="'.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? $v[$k2] : '&nbsp;').'</td>';
                                        }
                                    }else{
                                        echo '<td class="manage-column gd-xs-hide '.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? $v[$k2] : '&nbsp;').'</td>';
                                    }   
                            }else{
                                echo '<td class="'.cf7d_admin_get_field_name($v2).'" data-head="'.cf7d_admin_get_field_name($v2).'">'.((isset($v[$k2])) ? $v[$k2] : '&nbsp;').'</td>';
                            }
                        }
                        $row_id = $k;
                        do_action('cf7d_admin_after_body_field', $fid, $row_id);
                        echo '</tr>';
                    }
                    ?>
                </tbody>
                <tfoot>
                    <tr>
                        <?php
                        echo '<th class="manage-column column-cb check-column"><input type="checkbox" id="cb-select-all-2" /></th>';
                        foreach ($fields as $k => $v) {
                          	if($k == 'captcha' || $k == 'ip_area'){continue;}
                            if($k=='readed'){
                                echo '<th class="manage-column gd-xs-hide" data-key="'.$v.'">是否已读</th>';
                            }else if($k=='submit_ip'){
                            	echo '<th class="manage-column gd-xs-hide" data-key="'.$v.'">访客国家(IP)</th>';
                            }
                            else if($v!='询盘发送时间' && $v!='邮箱(Email)'){
                            echo '<th class="manage-column gd-xs-hide '.$v.'" data-key="'.$v.'">'.cf7d_admin_get_field_name($v).'</th>';
                            }
                            else{
                                echo '<th class="manage-column '.$v.'" data-key="'.$v.'">'.cf7d_admin_get_field_name($v).'</th>';
                            }
                        }
                        ?>
                        <?php do_action('cf7d_admin_after_heading_field'); ?>
                    </tr>
                </tfoot>
            </table>
            <div class="tablenav bottom">
                <div class="alignleft actions bulkactions">
                    <label for="bulk-action-selector-bottom" class="screen-reader-text"><?php _e('Select bulk action'); ?></label>
                    <select name="action2" id="bulk-action-selector-bottom">
                        <option value="-1"><?php _e('Bulk Actions'); ?></option>
                        <?php echo cf7d_arr_to_option($entry_actions); ?>
                    </select>
                    <input id="doaction2" class="button action" value="<?php _e('Apply'); ?>" type="submit" name="btn_apply2" />
                </div>
                <div class="tablenav-pages">
                    <span class="displaying-num"><?php echo (($total == 1) ?
                    '1 ' . __('item') :
                    $total . ' ' . __('items')) ?></span>
					<span class="pagination-links">
                        <?php
                        echo paginate_links(array(
                            'base' => add_query_arg('cpage', '%#%'),
                            'format' => '',
                            'prev_text' => __('&laquo;'),
                            'next_text' => __('&raquo;'),
                            'total' => ceil($total / $items_per_page),
                            'current' => $page
                        ));
                        ?>
                    </span>
                </div>
                <br class="clear">
            </div> 			
        </form>
        <form method="post" action="" style="display:none;">
			<div id="" class="">
			    <h3>Google统计转换代码Js</h3>
			    <table width="100%">
			    <tr>
					<th width="180">开启代码执行</th>
					<td><input type="radio" name="google_js[open]" value="1" <?php if($google_js['open']==1) echo"checked";?>>开启<input type="radio" name="google_js[open]" value="1" <?php if($google_js['open']==0||!$google_js['open']) echo"checked";?>>关闭</td>
			    </tr>
				<tr>
					<th width="180">全局代码</th>
					<td><textarea name="google_js[global]" rows="8" style="width:100%"><?php echo  stripslashes($google_js['global']);?></textarea></td>
			    </tr>
				<tr>
					<th>事件代码</th>
					<td><textarea name="google_js[run]" rows="8" style="width:100%"><?php echo  stripslashes($google_js['run']);?></textarea></td>
			    </tr>
			    </table>
				<input type="submit" name="sbt_google_js" onclick="" value="保存"  >
			</div>
        </form>
        <?php do_action('cf7d_after_admin_form', $fid); ?>        
        <?php
    } else {
        if ($first_form_id > 0 && !(isset($_GET['action'])) && !(isset($_GET['action2']))) {
            ?>
            <script>
            location.replace('<?php echo admin_url("admin.php?page=cf7-data&fid=" . $first_form_id); ?>');
            </script>
            <?php
        }
    }
    echo '</div>';
}

function get_ip_from_taobaoapi($ip){
	$country = file_get_contents("http://ip.globalso.com/?ip=".$ip);
    if($country){
      	return $country;
    }else{
      	return "未知";
    }	
}

function request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
        
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        
        return $data;
    }


function cf7d_form_action_callback()
{
    global $wpdb;
    if ($current_action = cf7d_current_action()) {
        if ($current_action == 'delete') {
            if (isset($_GET['del_id'])) {
                $del_id = implode(',', $_GET['del_id']);
                $fid = (int)$_GET['fid'];
                $wpdb->query("DELETE FROM {$wpdb->prefix}cf7_data_entry WHERE data_id IN($del_id)");
                $wpdb->query("DELETE FROM {$wpdb->prefix}cf7_data WHERE id IN($del_id)");
                wp_safe_redirect(admin_url('admin.php?page=cf7-data&fid=' . $fid));
                exit();
            }
        }
        do_action('cf7d_entry_action', $current_action);
    }
    do_action('cf7d_main_post');
}
function cf7d_current_action()
{
    $current_action = false;
    if (isset($_REQUEST['action']) && -1 != $_REQUEST['action'] && isset($_GET['btn_apply'])) {
        $current_action = $_REQUEST['action'];
        return apply_filters('cf7d_get_current_action', $current_action);
    }

    if (isset($_REQUEST['action2']) && -1 != $_REQUEST['action2'] && isset($_GET['btn_apply2'])) {
        $current_action = $_REQUEST['action2'];
        return apply_filters('cf7d_get_current_action', $current_action);
    }
    $current_action = apply_filters('cf7d_get_current_action', $current_action);
    return false;
}