Last active
December 20, 2015 13:09
-
-
Save torounit/6136827 to your computer and use it in GitHub Desktop.
ACFで、内容が保存時に暗号化されるフィールド。需要は知らない。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Advanced Custom Fields: encrypted_field | |
Version: 1.0.0 | |
Author: Toro_Unit | |
Author URI: http://www.jbnet.jp | |
License: GPLv2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
class acf_field_encrypted_field_plugin | |
{ | |
/* | |
* Construct | |
* | |
* @description: | |
* @since: 3.6 | |
* @created: 1/04/13 | |
*/ | |
function __construct() | |
{ | |
// set text domain | |
/* | |
$domain = 'acf-encrypted_field'; | |
$mofile = trailingslashit(dirname(__File__)) . 'lang/' . $domain . '-' . get_locale() . '.mo'; | |
load_textdomain( $domain, $mofile ); | |
*/ | |
// version 4+ | |
add_action('acf/register_fields', array($this, 'register_fields')); | |
// version 3- | |
//add_action( 'init', array( $this, 'init' ), 5); | |
} | |
/* | |
* Init | |
* | |
* @description: | |
* @since: 3.6 | |
* @created: 1/04/13 | |
*/ | |
function init() | |
{ | |
if(function_exists('register_field')) | |
{ | |
register_field('acf_field_encrypted_field', dirname(__File__) . '/encrypted_field-v3.php'); | |
} | |
} | |
/* | |
* register_fields | |
* | |
* @description: | |
* @since: 3.6 | |
* @created: 1/04/13 | |
*/ | |
function register_fields() | |
{ | |
include_once('encrypted_field-v4.php'); | |
} | |
} | |
new acf_field_encrypted_field_plugin(); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class acf_field_encrypted_field extends acf_field | |
{ | |
// vars | |
var $settings, // will hold info such as dir / path | |
$defaults, // will hold default field options | |
$key, | |
$iv, | |
$resource; | |
public function __construct() | |
{ | |
// vars | |
$this->name = 'encrypted_field'; | |
$this->label = __('encrypted field'); | |
$this->category = __("Basic",'acf'); // Basic, Content, Choice, etc | |
// do not delete! | |
parent::__construct(); | |
// settings | |
$this->settings = array( | |
'path' => apply_filters('acf/helpers/get_path', __FILE__), | |
'dir' => apply_filters('acf/helpers/get_dir', __FILE__), | |
'version' => '1.0.0' | |
); | |
} | |
public function create_options( $field ) | |
{ | |
$key = $field['name']; | |
?> | |
<tr class="field_option field_option_<?php echo $this->name; ?>"> | |
<td class="label"> | |
<label><?php _e("Key Strings",'acf'); ?></label> | |
</td> | |
<td> | |
<?php | |
do_action('acf/create_field', array( | |
'type' => 'text', | |
'name' => 'fields['.$key.'][key_string]', | |
'value' => $field['key_string'], | |
)); | |
?> | |
</td> | |
</tr> | |
<?php | |
} | |
public function create_field( $field ) { | |
echo '<input type="text" value="' . esc_attr( $field['value'] ) . '" id="' . esc_attr( $field['id'] ) . '" class="' . esc_attr( $field['class'] ) . '" name="' . esc_attr( $field['name'] ) . '" />'; | |
} | |
public function load_value( $value, $post_id, $field ) { | |
$value = $this->_decrypt( $value ,$field['key_string']); | |
return $value; | |
} | |
public function update_value( $value, $post_id, $field ) { | |
$value = $this->_encrypt( $value ,$field['key_string']); | |
return $value; | |
} | |
protected function mcript_open($key) { | |
$td = mcrypt_module_open('des', '', 'ecb', ''); | |
$key = substr($key, 0, mcrypt_enc_get_key_size($td)); | |
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); | |
if (mcrypt_generic_init($td, $key, $iv) < 0) { | |
exit('error.'); | |
} | |
return $td; | |
} | |
protected function mcrypt_close( $td ) { | |
mcrypt_generic_deinit($td); | |
mcrypt_module_close($td); | |
} | |
protected function _encrypt( $input, $key ) { | |
$td = $this->mcript_open($key); | |
$encrypt_text = base64_encode(mcrypt_generic($td, $input)); | |
$this->mcrypt_close($td); | |
return $encrypt_text; | |
} | |
protected function _decrypt( $input, $key ) { | |
if( !$input ) { | |
return $input; | |
} | |
$td = $this->mcript_open($key); | |
$decrypt_text = mdecrypt_generic($td, base64_decode( $input )); | |
$this->mcrypt_close($td); | |
return str_replace("\0", "", $decrypt_text); | |
} | |
} | |
// create field | |
new acf_field_encrypted_field(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment