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

YOURLS version 1.8.1 functionality #28

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ function ldapauth_environment_check() {
return true;
}


yourls_add_filter( 'is_valid_user', 'ldapauth_is_valid_user' );
# Reroute login to yourls filter
# (see https://github.com/YOURLS/YOURLS/wiki/Advanced-Hook-Syntax)
//yourls_add_filter( 'is_valid_user', 'ldapauth_is_valid_user' );
yourls_add_filter( 'shunt_is_valid_user', 'ldapauth_is_valid_user' );

function ldapauth_shuffle_assoc($list) {
if (!is_array($list)) return $list;
Expand Down Expand Up @@ -117,7 +119,7 @@ function ldapauth_get_ldap_connection() {
// returns true/false
function ldapauth_is_valid_user( $value ) {
global $yourls_user_passwords;

// Always check & set early
if ( !ldapauth_environment_check() ) {
die( 'Invalid configuration for YOURLS LDAP plugin. Check PHP error log.' );
Expand All @@ -141,6 +143,7 @@ function ldapauth_is_valid_user( $value ) {
if (!defined(LDAPAUTH_USERCACHE_TYPE) && isset( $_SESSION['LDAPAUTH_AUTH_USER'] ) ) {
// already authenticated...
$username = $_SESSION['LDAPAUTH_AUTH_USER'];

// why is this checked here, but not before the cookie is set?
if ( ldapauth_is_authorized_user( $username ) ) {
if( !isset($yourls_user_passwords[$username]) ) {
Expand Down Expand Up @@ -202,7 +205,7 @@ function ldapauth_is_valid_user( $value ) {
if (empty($ldapSuccess)) { // we don't need to do this if we already bound using username and LDAPAUTH_BIND_WITH_USER_TEMPLATE
$ldapSuccess = @ldap_bind($ldapConnection, $userDn, $_REQUEST['password']);
}

// success?
if ($ldapSuccess)
{
Expand Down Expand Up @@ -240,6 +243,7 @@ function ldapauth_is_valid_user( $value ) {
$_SESSION['LDAPAUTH_AUTH_USER'] = $username;
}
return true;
ldapauth_debug("User $username was successfully authenticated");
} else {
error_log("No LDAP success");
}
Expand All @@ -259,8 +263,8 @@ function ldapauth_is_authorized_user( $username ) {
global $ldapauth_authorized_admins;
if ( in_array( $username, $ldapauth_authorized_admins ) ) {
return true;
}

}
// not an admin user
return false;
}
Expand All @@ -281,23 +285,26 @@ function ldapauth_logout_hook( $args ) {
* their LDAP passwords
*/

yourls_add_action ('plugins_loaded', 'ldapauth_merge_users');
yourls_add_action('plugins_loaded', 'ldapauth_merge_users');
function ldapauth_merge_users() {
global $yourls_user_passwords;
if ( !ldapauth_environment_check() ) {
die( 'Invalid configuration for YOURLS LDAP plugin. Check PHP error log.' );
}
if(LDAPAUTH_USERCACHE_TYPE==1 && false !== yourls_get_option('ldapauth_usercache')) {
ldapauth_debug("Merging text file users and cached LDAP users");
//print_r($yourls_user_passwords) . "<br>";
$yourls_user_passwords = array_merge($yourls_user_passwords, yourls_get_option('ldapauth_usercache'));
//print_r($yourls_user_passwords) . "<br>";
//die('Paused');
}
}
/**
* Create user in config file
* Code reused from yourls_hash_passwords_now()
*/
function ldapauth_create_user( $user, $new_password ) {
$configdata = file_get_contents( YOURLS_CONFIGFILE );
$configdata = htmlspecialchars(file_get_contents( YOURLS_CONFIGFILE ));
if ( $configdata == FALSE ) {
die('Couldn\'t read the config file');
}
Expand All @@ -306,18 +313,25 @@ function ldapauth_create_user( $user, $new_password ) {
die('Can\'t write to config file');

$pass_hash = ldapauth_hash_password($new_password);
$user_line = "\t'$user' => 'phpass:$pass_hash' /* Password encrypted by YOURLS */,";
$user_line = "\t'$user' => 'phpass:$pass_hash' /* LDAP user added by plugin */,";

// Add the user on a new line after the start of the passwords array
$new_contents = preg_replace('/(yourls_user_passwords\s=\sarray\()/', '$0 ' . PHP_EOL . $user_line, $configdata, -1, $count);
$new_contents = preg_replace('/\$yourls_user_passwords\s=\s\[/', '$0 ' . PHP_EOL . $user_line, $configdata, -1, $count);
//echo YOURLS_CONFIGFILE . "<br>";
//echo $configdata . "<br>";
//echo $user_line . "<br>";
//echo $user . "<br>";
//echo htmlspecialchars_decode($new_contents) . "<br>";
//echo $count . "<br>";
//die('Paused');

if ($count === 0) {
die('Couldn\'t add user, plugin may not be compatible with YourLS version');
} else if ($count > 1) {
die('Added user more than once. Check config file.');
}

$success = file_put_contents( YOURLS_CONFIGFILE, $new_contents );
$success = file_put_contents( YOURLS_CONFIGFILE, htmlspecialchars_decode($new_contents) );
if ( $success === false ) {
die('Unable to save config file');
}
Expand Down