worldpress插件编写-WP_OTP

worldpress插件编写-WP_OTP,结合多个示例,理解后编写而成.
实际运行效果和介绍见:WP_OTP

/*
Plugin Name: OATH_OTP
Plugin URI: http://devzc.com/wp_otp
Version: 0.0.1
Author: Richard Chou
Description: OATH_OTP
*/

// error_reporting(E_ALL);

global $shakehand_key;

//20bit
$shakehand_key = get_option("shakehand_key");

//translate language, not completed.
if(!function_exists('tr')){
function tr($str ,$section){
return $str ;
}
}


function auth_check($clientSideSha1Str){
global $shakehand_key;
//die($shakehand_key);
$seed = time();
$seed = $seed - $seed%30 ;
//$clientSideSha1Str = sha1($code.$shakehand_key);
$webSideSha1Str =sha1($seed.$shakehand_key);
//echo "====seed:$seed=====".$webSideSha1Str.",".$clientSideSha1Str."===";

$clientSideCode = substr($clientSideSha1Str,0,6);
$webSideCode = substr($webSideSha1Str,0,6);

if($clientSideCode == $webSideCode ){
return true;
}else{
return false;
}
}
// this function adds captcha to the login form
function oath_login_form() {
if( session_id() == "" )
session_start();
global $cptch_options;

// captcha html - login form
echo '

';
if( "" != $cptch_options['cptch_label_form'] )
echo 'test';
if( isset( $_SESSION['cptch_error'] ) ) {
echo "test". $_SESSION['cptch_error'] ."test";
unset( $_SESSION['cptch_error'] );
}
echo 'test';
oath_display_captcha();
echo '


test';

return true;

} // end function oath_login_form

// this function checks captcha posted with a login
function oath_login_post($errors) {
global $str_key;
$str_key = "123";
// Delete errors, if they set
if( isset( $_SESSION['cptch_error'] ) )
unset( $_SESSION['cptch_error'] );

if( isset( $_REQUEST['action'] ) && 'register' == $_REQUEST['action'] )
return($errors);

// If captcha not complete, return error
if ( isset( $_REQUEST['cptch_number'] ) && "" == $_REQUEST['cptch_number'] ) {
return $errors.''. tr( 'ERROR', 'captcha' ) .': '. tr( 'Please complete the CAPTCHA.', 'captcha' );
}

if(auth_check(trim($_REQUEST['cptch_number']))===true){
// captcha was matched
} else {
return $errors.''. tr( 'ERROR', 'captcha' ) .': '. tr( 'That CAPTCHA was incorrect.', 'captcha' );
}
return($errors);
} // end function oath_login_post

// this function checks the captcha posted with a login when login errors are absent
function oath_login_check($url) {
global $str_key;
global $auth_tmp_key;
if( session_id() == "" )
session_start();

$str_key = "123";
// Add error if captcha is empty
if ( isset( $_REQUEST['cptch_number'] ) && "" == $_REQUEST['cptch_number'] ) {
$_SESSION['cptch_error'] = tr( 'Please complete the CAPTCHA.', 'captcha' );
// Redirect to wp-login.php
return $_SERVER["REQUEST_URI"];
}
if ( isset( $_REQUEST['cptch_result'] ) && isset( $_REQUEST['cptch_number'] ) ) {
if(auth_check(trim($_REQUEST['cptch_number']))===true){
return $url; // captcha was matched
} else {
// Add error if captcha is incorrect
$_SESSION['cptch_error'] = tr('That CAPTCHA was incorrect.', 'captcha');
// Redirect to wp-login.php
return $_SERVER["REQUEST_URI"];
}
}
else {
return $url; // captcha was matched
}
} // end function oath_login_post



// Functionality of the captcha logic work
function oath_display_captcha()
{
global $cptch_options,$auth_tmp_key ;
global $shakehand_key;

// String for display
$seed = time();
?>
OATH_OTP():test


}


add_action( 'login_form', 'oath_login_form' );
add_filter( 'login_errors', 'oath_login_post' );
add_filter( 'login_redirect', 'oath_login_check', 10, 3 );



// Function for display oather settings page in the admin area

function oather_activate() {
add_option( 'shakehand_key', "testcoco" );
//这里也可以用update_option,区别是使用update_action会在每一次重新启用插件时重置用户设置为默认值。

}
register_activation_hook(__FILE__, 'oather_activate');


function oather_admin_init(){
//注册配置,第3个参数是回调函数,用于过滤提交的内容
//register_setting( $option_group, $option_name, $sanitize_callback )
register_setting( 'shakehand_key', 'oather_options', 'oather_options_validate' );

//添加设置群
//add_settings_section( $id, $title, $callback, $page );
add_settings_section('oather_main', tr('Settings','oath'), 'oather_section', 'oather');

//添加具体配置
//add_settings_field( $id, $title, $callback, $page, $section, $args );
add_settings_field('oather_shakehand_key', tr('oather_shakehand_key','oath'), 'oather_authkey', 'oather', 'oather_main');


}
add_action('admin_init', 'oather_admin_init');



function oather_authkey()
{
$shakehand_key = get_option( 'shakehand_key' );
// echo "
$shakehand_key:";print_r($shakehand_key);
?>(6-20 chars)test
}



function oather_section($section){
return $section;
}
function oather_options_validate($input) {
return $input;
}

function oather_options_page() {
?>













}

//在设置页添加链接
function oather_menu() {
add_options_page('OATH_OTP Settings', tr('OATH_OTP','oath'), 'manage_options', 'oath_otp', 'oather_options_page');
}

function oather_action_links( $links, $file ) {
if ( $file != plugin_basename( __FILE__ ))
return $links;
$settings_link = 'Settings';
array_unshift( $links, $settings_link );
return $links;
//在插件页添加链接
}
add_filter( 'plugin_action_links', 'oather_action_links',10,2);
add_action('admin_menu','oather_menu');



if(isset($_POST['oather_shakehand_key'])){
//update post
$shakehand_len = strlen($_POST['oather_shakehand_key']);
if($shakehand_len >=6 && $shakehand_len <=20){
update_option( 'shakehand_key', $_POST['oather_shakehand_key'] ,'', 'yes' );
// print_r($_POST);
$message = __( "Options saved.", 'oath' );
}else{
$message = tr( "Options save failed,shakehand_key must be in 6 to 20 chars.", 'oath' );
}
}



?>

 
| 0个评论