Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update password.php #2229

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 49 additions & 8 deletions classes/fields/password.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ public function options () {
'default' => 255,
'type' => 'number',
'help' => __( 'Set to -1 for no limit', 'pods' )
)/*,
),
self::$type . '_hash' => array(
'label' => __( 'Hash', 'pods' ),
'default' => 1,
'type' => 'boolean',
'help' => __( 'Securely hash passwords when storing them', 'pods' )
),
self::$type . '_salt' => array(
'label' => __( 'Salt', 'pods' ),
'default' => "",
'type' => 'string',
'help' => __( 'Used for salting the hashes of secure fields', 'pods' )
)/*,
self::$type . '_size' => array(
'label' => __( 'Field Size', 'pods' ),
'default' => 'medium',
Expand All @@ -85,7 +97,10 @@ public function options () {
* @since 2.0
*/
public function schema ( $options = null ) {
$length = (int) pods_var( self::$type . '_max_length', $options, 255 );
if( pods_var( self::$type . '_hash', $options ) )
$length = 6 + 32;
else
$length = (int) pods_var( self::$type . '_max_length', $options, 255 );

$schema = 'VARCHAR(' . $length . ')';

Expand Down Expand Up @@ -125,6 +140,24 @@ public function input ( $name, $value = null, $options = null, $pod = null, $id
pods_view( PODS_DIR . 'ui/fields/password.php', compact( array_keys( get_defined_vars() ) ) );
}

/**
* Helper function to check if a string is a tagged hash or not
* The format of a tagged hash string is the string "{SHA1}"
* followed by 32 hexets
*
* @param string $value
*
* @return bool
*/
function tagged_hash_string( $value ) {
if ( 6 + 32 == strlen( $value ) ) {
if ("{SHA1}" == pods_mb_substr($value, 0, 6) ) {
return true;
}
}
return false;
}

/**
* Validate a value before it's saved
*
Expand Down Expand Up @@ -174,12 +207,20 @@ public function validate ( $value, $name = null, $options = null, $fields = null
* @since 2.0
*/
public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
$length = (int) pods_var( self::$type . '_max_length', $options, 255 );

if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
$value = pods_mb_substr( $value, 0, $length );
}
$length = (int) pods_var( self::$type . '_max_length', $options, 255 );

return $value;
if ( 0 < $length && $length < pods_mb_strlen( $value ) ) {
$value = pods_mb_substr( $value, 0, $length );
}

if( pods_var( self::$type . '_hash', $options ) ) {
if ( PodsField_Password::tagged_hash_string( $value ) )
return $value;
else
return "{SHA1}" . hash_pbkdf2('sha1', $value, pods_var( self::$type . '_salt', $options ), 1, 32, false);
}
else {
return $value;
}
}
}