2018-09-07 23:45:47 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
Plugin Name: Auth Manager Plus
|
|
|
|
Plugin URI: https://github.com/joshp23/YOURLS-AuthMgrPlus
|
|
|
|
Description: Role Based Access Controlls with seperated user data for authenticated users
|
2021-03-13 06:43:02 +01:00
|
|
|
Version: 2.2.6
|
2018-09-07 23:45:47 +02:00
|
|
|
Author: Josh Panter, nicwaller, Ian Barber <ian.barber@gmail.com>
|
|
|
|
Author URI: https://unfettered.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
// No direct call
|
|
|
|
if( !defined( 'YOURLS_ABSPATH' ) ) die();
|
|
|
|
|
|
|
|
/****************** SET UP CONSTANTS ******************/
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
class ampRoles {
|
|
|
|
const Administrator = 'Administrator';
|
2018-09-07 23:45:47 +02:00
|
|
|
const Editor = 'Editor';
|
|
|
|
const Contributor = 'Contributor';
|
|
|
|
}
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
class ampCap {
|
2018-09-07 23:45:47 +02:00
|
|
|
const ShowAdmin = 'ShowAdmin';
|
|
|
|
const AddURL = 'AddURL';
|
|
|
|
const DeleteURL = 'DeleteURL';
|
|
|
|
const EditURL = 'EditURL';
|
2021-03-13 06:43:02 +01:00
|
|
|
const ShareURL = 'ShareURL';
|
2019-11-23 11:47:38 +01:00
|
|
|
const Traceless = 'Traceless';
|
2018-09-07 23:45:47 +02:00
|
|
|
const ManageAnonURL = 'ManageAnonURL';
|
|
|
|
const ManageUsrsURL = 'ManageUsrsURL';
|
|
|
|
const ManagePlugins = 'ManagePlugins';
|
|
|
|
const API = 'API';
|
|
|
|
const APIu = 'APIu';
|
|
|
|
const ViewStats = 'ViewStats';
|
|
|
|
const ViewAll = 'ViewAll';
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** Add hooks to intercept functionality in CORE **********/
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_action( 'load_template_infos', 'amp_intercept_stats' );
|
|
|
|
function amp_intercept_stats() {
|
2018-09-07 23:45:47 +02:00
|
|
|
if ( 'YOURLS_PRIVATE_INFOS' === true ) {
|
2019-09-27 03:01:08 +02:00
|
|
|
amp_require_capability( ampCap::ViewStats );
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_action( 'api', 'amp_intercept_api' );
|
|
|
|
function amp_intercept_api() {
|
2018-09-07 23:45:47 +02:00
|
|
|
if ( 'YOURLS_PRIVATE_API' === true ) {
|
|
|
|
if ( isset( $_REQUEST['shorturl'] ) || isset( $_REQUEST['stats'] ) ) {
|
2019-09-27 03:01:08 +02:00
|
|
|
amp_require_capability( ampCap::APIu );
|
2018-09-07 23:45:47 +02:00
|
|
|
} else {
|
2019-09-27 03:01:08 +02:00
|
|
|
amp_require_capability( ampCap::API );
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 11:47:38 +01:00
|
|
|
yourls_add_action( 'auth_successful', function() {
|
|
|
|
if( yourls_is_admin() ) amp_intercept_admin();
|
|
|
|
} );
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* YOURLS processes most actions in the admin page. It would be ideal
|
|
|
|
* to add a unique hook for each action, but unfortunately we need to
|
|
|
|
* hook the admin page load itself, and try to figure out what action
|
|
|
|
* is intended.
|
|
|
|
*
|
|
|
|
* TODO: look for these hooks
|
|
|
|
*
|
|
|
|
* At this point, reasonably assume that the current request is for
|
|
|
|
* a rendering of the admin page.
|
|
|
|
*/
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_intercept_admin() {
|
|
|
|
amp_require_capability( ampCap::ShowAdmin );
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// we use this GET param to send up a feedback notice to user
|
|
|
|
if ( isset( $_GET['access'] ) && $_GET['access']=='denied' ) {
|
|
|
|
yourls_add_notice('Access Denied');
|
|
|
|
}
|
|
|
|
|
2020-07-26 08:22:28 +02:00
|
|
|
// allow manipulation of this list ( be mindfull of extending Auth mp Capability class if needed )
|
|
|
|
$action_capability_map = yourls_apply_filter( 'amp_action_capability_map',
|
|
|
|
array( 'add' => ampCap::AddURL,
|
|
|
|
'delete' => ampCap::DeleteURL,
|
|
|
|
'edit_display' => ampCap::EditURL,
|
|
|
|
'edit_save' => ampCap::EditURL,
|
|
|
|
'activate' => ampCap::ManagePlugins,
|
|
|
|
'deactivate' => ampCap::ManagePlugins,
|
|
|
|
) );
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// Key actions like Add/Edit/Delete are AJAX requests
|
|
|
|
if ( yourls_is_Ajax() ) {
|
|
|
|
|
|
|
|
// Define some boundaries for ownership
|
|
|
|
// Allow some flexability with those boundaries
|
2020-07-26 08:22:28 +02:00
|
|
|
$restricted_actions = yourls_apply_filter( 'amp_restricted_ajax_actions',
|
|
|
|
array( 'edit_display',
|
|
|
|
'edit_save',
|
|
|
|
'delete'
|
|
|
|
) );
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
$action_keyword = $_REQUEST['action'];
|
2020-07-26 08:22:28 +02:00
|
|
|
$cap_needed = $action_capability_map[$action_keyword];
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// Check the action against those boundaries
|
|
|
|
if ( in_array( $action_keyword, $restricted_actions) ) {
|
|
|
|
$keyword = $_REQUEST['keyword'];
|
2020-07-26 08:22:28 +02:00
|
|
|
$do = amp_manage_keyword( $keyword, $cap_needed );
|
2018-09-07 23:45:47 +02:00
|
|
|
} else {
|
2020-07-26 08:22:28 +02:00
|
|
|
$do = amp_have_capability( $cap_needed );
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $do !== true ) {
|
|
|
|
$err = array();
|
|
|
|
$err['status'] = 'fail';
|
|
|
|
$err['code'] = 'error:authorization';
|
|
|
|
$err['message'] = 'Access Denied';
|
|
|
|
$err['errorCode'] = '403';
|
|
|
|
echo json_encode( $err );
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intercept requests for plugin management
|
|
|
|
if( isset( $_SERVER['REQUEST_URI'] ) && preg_match('/\/admin\/plugins\.php.*/', $_SERVER['REQUEST_URI'] ) ) {
|
|
|
|
// Is this a plugin page request?
|
|
|
|
if ( isset( $_REQUEST['page'] ) ) {
|
|
|
|
// Is this an allowed plugin?
|
2019-09-27 03:01:08 +02:00
|
|
|
global $amp_allowed_plugin_pages;
|
|
|
|
if ( amp_have_capability( ampCap::ManagePlugins ) !== true) {
|
2018-09-07 23:45:47 +02:00
|
|
|
$r = $_REQUEST['page'];
|
2019-09-27 03:01:08 +02:00
|
|
|
if(!in_array($r, $amp_allowed_plugin_pages ) ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Should this user touch plugins?
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( amp_have_capability( ampCap::ManagePlugins ) !== true) {
|
2018-09-07 23:45:47 +02:00
|
|
|
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// intercept requests for global plugin management actions
|
|
|
|
if (isset( $_REQUEST['plugin'] ) ) {
|
|
|
|
$action_keyword = $_REQUEST['action'];
|
|
|
|
$cap_needed = $action_capability_map[$action_keyword];
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( $cap_needed !== NULL && amp_have_capability( $cap_needed ) !== true) {
|
2018-09-07 23:45:47 +02:00
|
|
|
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 11:47:38 +01:00
|
|
|
|
2021-03-13 06:43:02 +01:00
|
|
|
/*
|
|
|
|
* Cosmetic filter: removes disallowed buttons from link list per short link
|
|
|
|
*/
|
|
|
|
|
|
|
|
yourls_add_filter( 'table_add_row_action_array', 'amp_ajax_button_check' );
|
|
|
|
function amp_ajax_button_check( $actions, $keyword ) {
|
|
|
|
// define the amp capabilities that map to the buttons
|
|
|
|
$button_cap_map = array('stats' => ampCap::ViewStats,
|
|
|
|
'share' => ampCap::ShareURL,
|
|
|
|
'edit' => ampCap::EditURL,
|
|
|
|
'delete' => ampCap::DeleteURL,
|
|
|
|
);
|
|
|
|
|
|
|
|
$button_cap_map = yourls_apply_filter( 'amp_button_capability_map', $button_cap_map );
|
|
|
|
|
|
|
|
// define restricted buttons
|
|
|
|
$restricted_buttons = array('delete', 'edit');
|
|
|
|
if ( 'YOURLS_PRIVATE_INFOS' === true )
|
2021-03-13 08:13:46 +01:00
|
|
|
$restricted_buttons += ['stats'];
|
2021-03-13 06:43:02 +01:00
|
|
|
|
|
|
|
$restricted_buttons = yourls_apply_filter( 'amp_restricted_buttons', $restricted_buttons );
|
|
|
|
|
|
|
|
// unset any disallowed buttons
|
|
|
|
foreach ( $actions as $action => $vars ) {
|
|
|
|
$cap_needed = $button_cap_map[$action];
|
|
|
|
if ( in_array( $action, $restricted_buttons) )
|
|
|
|
$show = amp_manage_keyword( $keyword, $cap_needed );
|
|
|
|
else
|
|
|
|
$show = amp_have_capability( $cap_needed );
|
|
|
|
if (!$show)
|
|
|
|
unset( $actions[$action] );
|
|
|
|
}
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
/*
|
|
|
|
* Cosmetic filter: removes disallowed plugins from link list
|
|
|
|
*/
|
2019-11-23 11:47:38 +01:00
|
|
|
|
|
|
|
yourls_add_filter( 'admin_sublinks', 'amp_admin_sublinks' );
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_admin_sublinks( $links ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
global $amp_allowed_plugin_pages;
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2021-03-12 07:18:42 +01:00
|
|
|
if( empty( $links['plugins'] ) ) {
|
|
|
|
|
2021-03-12 06:12:04 +01:00
|
|
|
unset($links['plugins']);
|
2021-03-12 07:18:42 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if ( amp_have_capability( ampCap::ManagePlugins ) !== true) {
|
|
|
|
foreach( $links['plugins'] as $link => $ar ) {
|
|
|
|
if(!in_array($link, $amp_allowed_plugin_pages) )
|
|
|
|
unset($links['plugins'][$link]);
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 06:12:04 +01:00
|
|
|
sort($links['plugins']);
|
2021-03-12 07:18:42 +01:00
|
|
|
}
|
2018-09-07 23:45:47 +02:00
|
|
|
return $links;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cosmetic filter: displays currently available roles
|
|
|
|
* by hovering mouse over the username in logout link.
|
|
|
|
*/
|
2019-11-23 11:47:38 +01:00
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_filter( 'logout_link', 'amp_html_append_roles' );
|
|
|
|
function amp_html_append_roles( $original ) {
|
2019-11-23 11:47:38 +01:00
|
|
|
if ( amp_is_valid_user() ) {
|
2019-09-27 03:01:08 +02:00
|
|
|
$listcaps = implode(', ', amp_current_capabilities());
|
2018-09-07 23:45:47 +02:00
|
|
|
return '<div title="'.$listcaps.'">'.$original.'</div>';
|
|
|
|
} else {
|
|
|
|
return $original;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************** CAPABILITY TESTING ****************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If capability is not permitted in current context, then abort.
|
|
|
|
* This is the most basic way to intercept unauthorized usage.
|
|
|
|
*/
|
|
|
|
// TODO: API responses!
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_require_capability( $capability ) {
|
|
|
|
if ( !amp_have_capability( $capability ) ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
// If the user can't view admin interface, return a plain error.
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( !amp_have_capability( ampCap::ShowAdmin ) ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
// header("HTTP/1.0 403 Forbidden");
|
|
|
|
die('Require permissions to show admin interface.');
|
|
|
|
}
|
|
|
|
// Otherwise, render errors in admin interface
|
|
|
|
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heart of system - Can the user do "X"?
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_have_capability( $capability ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
global $amp_anon_capabilities;
|
|
|
|
global $amp_role_capabilities;
|
|
|
|
global $amp_admin_ipranges;
|
2020-06-23 11:20:50 +02:00
|
|
|
global $amp_default_role;
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// Make sure the environment has been setup
|
2019-09-27 03:01:08 +02:00
|
|
|
amp_env_check();
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// Check anon capabilities
|
2019-09-27 03:01:08 +02:00
|
|
|
$return = in_array( $capability, $amp_anon_capabilities );
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// Check user-role based auth
|
|
|
|
if( !$return ) {
|
2019-11-23 11:47:38 +01:00
|
|
|
// Only users have roles
|
|
|
|
if ( !amp_is_valid_user() ) //XXX
|
2018-09-07 23:45:47 +02:00
|
|
|
return false;
|
|
|
|
// List capabilities of particular user role
|
2019-09-27 03:24:12 +02:00
|
|
|
$user = YOURLS_USER !== false ? YOURLS_USER : NULL;
|
2018-09-07 23:45:47 +02:00
|
|
|
$user_caps = array();
|
2019-09-27 03:01:08 +02:00
|
|
|
foreach ( $amp_role_capabilities as $rolename => $rolecaps ) {
|
2021-04-05 16:01:33 +02:00
|
|
|
if ( amp_user_has_role( $user, $rolename ) ) {
|
|
|
|
$amp_role_assigned = True;
|
|
|
|
$user_caps = array_merge( $user_caps, $rolecaps );
|
|
|
|
}
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
$user_caps = array_unique( $user_caps );
|
|
|
|
// Is the requested capability in this list?
|
|
|
|
$return = in_array( $capability, $user_caps );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is user connecting from an admin designated IP?
|
|
|
|
if( !$return ) {
|
|
|
|
// the array of ranges: '127.0.0.0/8' will always be admin
|
2019-09-27 03:01:08 +02:00
|
|
|
foreach ($amp_admin_ipranges as $range) {
|
|
|
|
$return = amp_cidr_match( $_SERVER['REMOTE_ADDR'], $range );
|
2018-09-07 23:45:47 +02:00
|
|
|
if( $return )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 16:01:33 +02:00
|
|
|
if( !$amp_role_assigned ) {
|
|
|
|
if ( isset( $amp_default_role ) && in_array ($amp_default_role, array_keys( $amp_role_capabilities ) ) ) {
|
|
|
|
$default_caps = $amp_role_capabilities [ $amp_default_role ];
|
|
|
|
$return = in_array( $capability, $default_caps );
|
|
|
|
}
|
2020-06-23 11:20:50 +02:00
|
|
|
}
|
2018-09-07 23:45:47 +02:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether a specific user has a role.
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_user_has_role( $username, $rolename ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
global $amp_role_assignment;
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
// if no role assignments are created, grant everything FIXME: Make 'admin'
|
|
|
|
// so the site still works even if stuff is configured wrong
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( empty( $amp_role_assignment ) )
|
2018-09-07 23:45:47 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// do this the case-insensitive way
|
|
|
|
// the entire array was made lowercase in environment check
|
|
|
|
$username = strtolower($username);
|
|
|
|
$rolename = strtolower($rolename);
|
|
|
|
|
|
|
|
// if the role doesn't exist, give up now.
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( !in_array( $rolename, array_keys( $amp_role_assignment ) ) )
|
2018-09-07 23:45:47 +02:00
|
|
|
return false;
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
$users_in_role = $amp_role_assignment[$rolename];
|
2018-09-07 23:45:47 +02:00
|
|
|
return in_array( $username, $users_in_role );
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************* KEYWORD OWNERSHIP ************************/
|
|
|
|
|
|
|
|
// Filter out restricted access to keyword data in...
|
|
|
|
// Admin list
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_filter( 'admin_list_where', 'amp_admin_list_where' );
|
|
|
|
function amp_admin_list_where($where) {
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( amp_have_capability( ampCap::ViewAll ) )
|
2018-09-07 23:45:47 +02:00
|
|
|
return $where; // Allow admin/editor users to see the lot.
|
|
|
|
|
2019-09-27 03:24:12 +02:00
|
|
|
$user = YOURLS_USER !== false ? YOURLS_USER : NULL;
|
2019-11-23 11:47:38 +01:00
|
|
|
$where['sql'] = $where['sql'] . " AND (`user` = :user OR `user` IS NULL) ";
|
|
|
|
$where['binds']['user'] = $user;
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
return $where;
|
|
|
|
}
|
2019-11-23 05:02:40 +01:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
// API stats
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_filter( 'api_url_stats', 'amp_api_url_stats' );
|
|
|
|
function amp_api_url_stats( $return, $shorturl ) {
|
2019-11-23 05:02:40 +01:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
$keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'
|
|
|
|
$keyword = yourls_sanitize_string( $keyword );
|
|
|
|
$keyword = addslashes($keyword);
|
|
|
|
|
2019-11-23 05:02:40 +01:00
|
|
|
if( ( !defined('YOURLS_PRIVATE_INFOS') || YOURLS_PRIVATE_INFOS !== false )
|
|
|
|
&& !amp_access_keyword($keyword) )
|
2018-09-07 23:45:47 +02:00
|
|
|
return array('simple' => "URL is owned by another user", 'message' => 'URL is owned by another user', 'errorCode' => 403);
|
2019-11-23 05:02:40 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
return $return;
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
2019-11-23 05:02:40 +01:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
// Info pages
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_action( 'pre_yourls_infos', 'amp_pre_yourls_infos' );
|
|
|
|
function amp_pre_yourls_infos( $keyword ) {
|
2019-11-23 05:02:40 +01:00
|
|
|
|
|
|
|
if( yourls_is_private() && !amp_access_keyword($keyword) ) {
|
|
|
|
|
2019-11-23 11:47:38 +01:00
|
|
|
if ( !amp_is_valid_user() )
|
2019-11-23 05:02:40 +01:00
|
|
|
yourls_redirect( yourls_admin_url( '?access=denied' ), 302 );
|
|
|
|
else
|
|
|
|
yourls_redirect( YOURLS_SITE, 302 );
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB stats
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_filter( 'get_db_stats', 'amp_get_db_stats' );
|
|
|
|
function amp_get_db_stats( $return, $where ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( amp_have_capability( ampCap::ViewAll ) )
|
2018-09-07 23:45:47 +02:00
|
|
|
return $return; // Allow admin/editor users to see the lot.
|
|
|
|
|
|
|
|
// or... filter results
|
|
|
|
global $ydb;
|
|
|
|
$table_url = YOURLS_DB_TABLE_URL;
|
2019-09-27 03:24:12 +02:00
|
|
|
$user = YOURLS_USER !== false ? YOURLS_USER : NULL;
|
2019-11-23 11:47:38 +01:00
|
|
|
|
|
|
|
$where['sql'] = $where['sql'] . " AND (`user` = :user OR `user` IS NULL) ";
|
|
|
|
$where['binds']['user'] = $user;
|
|
|
|
|
|
|
|
$sql = "SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM `$table_url` WHERE 1=1 " . $where['sql'];
|
|
|
|
$binds = $where['binds'];
|
|
|
|
|
|
|
|
$totals = $ydb->fetchObject($sql, $binds);
|
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
$return = array( 'total_links' => $totals->count, 'total_clicks' => $totals->sum );
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2019-11-23 11:47:38 +01:00
|
|
|
// Fine tune track-me-not
|
|
|
|
yourls_add_action('redirect_shorturl', 'amp_tracking');
|
2019-12-07 03:31:44 +01:00
|
|
|
function amp_tracking( $u, $k = false ) {
|
2019-11-23 11:47:38 +01:00
|
|
|
if( amp_is_valid_user() && ( amp_keyword_owner($k) || amp_have_capability( ampCap::Traceless ) ) ) {
|
|
|
|
// No logging
|
2019-12-07 03:31:44 +01:00
|
|
|
yourls_add_filter( 'shunt_update_clicks', function( ) { return true; } );
|
|
|
|
yourls_add_filter( 'shunt_log_redirect', function( ) { return true; } );
|
2019-11-23 11:47:38 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-07 23:45:47 +02:00
|
|
|
/********************* HOUSEKEEPING ************************/
|
|
|
|
// Validate environment setup
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_env_check() {
|
|
|
|
global $amp_anon_capabilities;
|
|
|
|
global $amp_role_capabilities;
|
|
|
|
global $amp_role_assignment;
|
|
|
|
global $amp_admin_ipranges;
|
|
|
|
global $amp_allowed_plugin_pages;
|
|
|
|
|
|
|
|
if ( !isset( $amp_anon_capabilities) ) {
|
|
|
|
$amp_anon_capabilities = array();
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( !isset( $amp_role_capabilities) ) {
|
|
|
|
$amp_role_capabilities = array(
|
|
|
|
ampRoles::Administrator => array(
|
|
|
|
ampCap::ShowAdmin,
|
|
|
|
ampCap::AddURL,
|
|
|
|
ampCap::EditURL,
|
|
|
|
ampCap::DeleteURL,
|
2021-03-13 06:43:02 +01:00
|
|
|
ampCap::ShareURL,
|
2019-11-23 11:47:38 +01:00
|
|
|
ampCap::Traceless,
|
2019-09-27 03:01:08 +02:00
|
|
|
ampCap::ManageAnonURL,
|
|
|
|
ampCap::ManageUsrsURL,
|
|
|
|
ampCap::ManagePlugins,
|
|
|
|
ampCap::API,
|
|
|
|
ampCap::APIu,
|
|
|
|
ampCap::ViewStats,
|
|
|
|
ampCap::ViewAll,
|
2018-09-07 23:45:47 +02:00
|
|
|
),
|
2019-09-27 03:01:08 +02:00
|
|
|
ampRoles::Editor => array(
|
|
|
|
ampCap::ShowAdmin,
|
|
|
|
ampCap::AddURL,
|
|
|
|
ampCap::EditURL,
|
|
|
|
ampCap::DeleteURL,
|
2021-03-13 06:43:02 +01:00
|
|
|
ampCap::ShareURL,
|
2019-11-23 11:47:38 +01:00
|
|
|
ampCap::Traceless,
|
2019-09-27 03:01:08 +02:00
|
|
|
ampCap::ManageAnonURL,
|
|
|
|
ampCap::APIu,
|
|
|
|
ampCap::ViewStats,
|
|
|
|
ampCap::ViewAll,
|
2018-09-07 23:45:47 +02:00
|
|
|
),
|
2019-09-27 03:01:08 +02:00
|
|
|
ampRoles::Contributor => array(
|
|
|
|
ampCap::ShowAdmin,
|
|
|
|
ampCap::AddURL,
|
|
|
|
ampCap::EditURL,
|
|
|
|
ampCap::DeleteURL,
|
2021-03-13 06:43:02 +01:00
|
|
|
ampCap::ShareURL,
|
2019-09-27 03:01:08 +02:00
|
|
|
ampCap::APIu,
|
|
|
|
ampCap::ViewStats,
|
2018-09-07 23:45:47 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( !isset( $amp_role_assignment ) ) {
|
|
|
|
$amp_role_assignment = array();
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( !isset( $amp_admin_ipranges ) ) {
|
|
|
|
$amp_admin_ipranges = array(
|
2018-09-07 23:45:47 +02:00
|
|
|
'127.0.0.0/8',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( !isset( $amp_allowed_plugin_pages ) ) {
|
|
|
|
$amp_allowed_plugin_pages = array(
|
2018-09-07 23:45:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert role assignment table to lower case if it hasn't been done already
|
|
|
|
// this makes searches much easier!
|
2019-09-27 03:01:08 +02:00
|
|
|
$amp_role_assignment_lower = array();
|
|
|
|
foreach ( $amp_role_assignment as $key => $value ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
$t_key = strtolower( $key );
|
|
|
|
$t_value = array_map('strtolower', $value);
|
2019-09-27 03:01:08 +02:00
|
|
|
$amp_role_assignment_lower[$t_key] = $t_value;
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
2019-09-27 03:01:08 +02:00
|
|
|
$amp_role_assignment = $amp_role_assignment_lower;
|
|
|
|
unset($amp_role_assignment_lower);
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activation: add the user column to the URL table if not added
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_action( 'activated_authMgrPlus/plugin.php', 'amp_activated' );
|
|
|
|
function amp_activated() {
|
2018-09-07 23:45:47 +02:00
|
|
|
global $ydb;
|
2021-04-05 16:01:33 +02:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
$table = YOURLS_DB_TABLE_URL;
|
2020-07-22 07:28:28 +02:00
|
|
|
$sql = "DESCRIBE `".$table."`";
|
2019-11-23 11:47:38 +01:00
|
|
|
$results = $ydb->fetchObjects($sql);
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
$activated = false;
|
|
|
|
foreach($results as $r) {
|
|
|
|
if($r->Field == 'user') {
|
|
|
|
$activated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$activated) {
|
|
|
|
if ($version) {
|
2020-07-22 07:28:28 +02:00
|
|
|
$sql = "ALTER TABLE `".$table."` ADD `user` VARCHAR(255) NULL";
|
2018-09-07 23:45:47 +02:00
|
|
|
$insert = $ydb->fetchAffected($sql);
|
|
|
|
} else {
|
2020-07-22 07:28:28 +02:00
|
|
|
$ydb->query("ALTER TABLE `".$table."` ADD `user` VARCHAR(255) NULL");
|
2018-09-07 23:45:47 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 05:02:40 +01:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
/***************** HELPER FUNCTIONS ********************/
|
|
|
|
|
|
|
|
// List currently available capabilities
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_current_capabilities() {
|
2018-09-07 23:45:47 +02:00
|
|
|
$current_capabilities = array();
|
|
|
|
$all_capabilities = array(
|
2019-09-27 03:01:08 +02:00
|
|
|
ampCap::ShowAdmin,
|
|
|
|
ampCap::AddURL,
|
|
|
|
ampCap::EditURL,
|
|
|
|
ampCap::DeleteURL,
|
2021-03-13 06:43:02 +01:00
|
|
|
ampCap::ShareURL,
|
2019-11-23 11:47:38 +01:00
|
|
|
ampCap::Traceless,
|
2019-09-27 03:01:08 +02:00
|
|
|
ampCap::ManageAnonURL,
|
|
|
|
ampCap::ManageUsrsURL,
|
|
|
|
ampCap::ManagePlugins,
|
|
|
|
ampCap::API,
|
|
|
|
ampCap::APIu,
|
|
|
|
ampCap::ViewStats,
|
|
|
|
ampCap::ViewAll,
|
2018-09-07 23:45:47 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $all_capabilities as $cap ) {
|
2019-09-27 03:01:08 +02:00
|
|
|
if ( amp_have_capability( $cap ) ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
$current_capabilities[] = $cap;
|
|
|
|
}
|
|
|
|
}
|
2020-07-26 08:22:28 +02:00
|
|
|
// allow manipulation of this list ( be mindfull of extending the ampCap class if needed )
|
2021-03-13 06:49:42 +01:00
|
|
|
$current_capabilities = yourls_apply_filter( 'amp_current_capabilities', $current_capabilities);
|
|
|
|
return $current_capabilities;
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for IP in a range
|
|
|
|
// from: http://stackoverflow.com/questions/594112/matching-an-ip-to-a-cidr-mask-in-php5
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_cidr_match($ip, $range) {
|
2018-09-07 23:45:47 +02:00
|
|
|
list ($subnet, $bits) = explode('/', $range);
|
|
|
|
$ip = ip2long($ip);
|
|
|
|
$subnet = ip2long($subnet);
|
|
|
|
$mask = -1 << (32 - $bits);
|
|
|
|
$subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned
|
|
|
|
return ($ip & $mask) == $subnet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check user access to a keyword ( can they see it )
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_access_keyword( $keyword ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
|
2019-11-23 11:47:38 +01:00
|
|
|
$users = array( YOURLS_USER !== false ? YOURLS_USER : NULL , NULL );
|
|
|
|
$owner = amp_keyword_owner( $keyword );
|
|
|
|
if ( amp_have_capability( ampCap::ViewAll ) || in_array( $owner , $users ) )
|
2018-09-07 23:45:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-23 05:02:40 +01:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
// Check user rights to a keyword ( can manage it )
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_manage_keyword( $keyword, $capability ) {
|
2020-03-08 05:21:04 +01:00
|
|
|
$return = false; // default is to deny access
|
|
|
|
if ( amp_is_valid_user() ) { // only authenticated users can manaage keywords
|
|
|
|
$owner = amp_keyword_owner($keyword);
|
|
|
|
$user = YOURLS_USER !== false ? YOURLS_USER : NULL;
|
|
|
|
if ( amp_have_capability( ampCap::ManageUsrsURL ) // Admin?
|
|
|
|
|| ( $owner === NULL && amp_have_capability( ampCap::ManageAnonURL ) ) // Editor?
|
2020-08-14 15:00:29 +02:00
|
|
|
|| ( $owner === $user && amp_have_capability( $capability ) ) ) // Self Edit?
|
2020-03-08 05:21:04 +01:00
|
|
|
$return = true;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
2019-11-23 05:02:40 +01:00
|
|
|
|
2018-09-07 23:45:47 +02:00
|
|
|
// Check keyword ownership
|
2019-09-27 03:01:08 +02:00
|
|
|
function amp_keyword_owner( $keyword ) {
|
2018-09-07 23:45:47 +02:00
|
|
|
global $ydb;
|
|
|
|
$table = YOURLS_DB_TABLE_URL;
|
2019-11-23 11:47:38 +01:00
|
|
|
$binds = array( 'keyword' => $keyword );
|
|
|
|
$sql = "SELECT * FROM `$table` WHERE `keyword` = :keyword";
|
|
|
|
$result = $ydb->fetchOne($sql, $binds);
|
2018-09-07 23:45:47 +02:00
|
|
|
return $result['user'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record user info on keyword creation
|
2019-09-27 03:01:08 +02:00
|
|
|
yourls_add_action( 'insert_link', 'amp_insert_link' );
|
|
|
|
function amp_insert_link($actions) {
|
2018-09-07 23:45:47 +02:00
|
|
|
global $ydb;
|
|
|
|
|
|
|
|
$keyword = $actions[2];
|
2019-09-27 03:24:12 +02:00
|
|
|
$user = YOURLS_USER !== false ? YOURLS_USER : NULL;
|
2018-09-07 23:45:47 +02:00
|
|
|
$table = YOURLS_DB_TABLE_URL;
|
|
|
|
|
|
|
|
// Insert $keyword against $username
|
2019-11-23 11:47:38 +01:00
|
|
|
$binds = array( 'user' => $user,
|
|
|
|
'keyword' => $keyword);
|
|
|
|
$sql = "UPDATE `$table` SET `user` = :user WHERE `keyword` = :keyword";
|
|
|
|
$result = $ydb->fetchAffected($sql, $binds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quick user validation without triggering hooks
|
|
|
|
function amp_is_valid_user() {
|
|
|
|
|
|
|
|
$valid = defined( 'YOURLS_USER' ) ? true : false;
|
|
|
|
|
|
|
|
if ( !$valid ) {
|
|
|
|
|
|
|
|
if ( yourls_is_API()
|
|
|
|
&& isset( $_REQUEST['timestamp'] ) && !empty($_REQUEST['timestamp'] )
|
|
|
|
&& isset( $_REQUEST['signature'] ) && !empty($_REQUEST['signature'] ) )
|
|
|
|
$valid = yourls_check_signature_timestamp();
|
|
|
|
elseif ( yourls_is_API()
|
|
|
|
&& !isset( $_REQUEST['timestamp'] )
|
|
|
|
&& isset( $_REQUEST['signature'] ) && !empty( $_REQUEST['signature'] ) )
|
|
|
|
$valid = yourls_check_signature();
|
|
|
|
elseif ( isset( $_REQUEST['username'] ) && isset( $_REQUEST['password'] )
|
|
|
|
&& !empty( $_REQUEST['username'] ) && !empty( $_REQUEST['password'] ) )
|
|
|
|
$valid = yourls_check_username_password();
|
|
|
|
elseif ( !yourls_is_API() && isset( $_COOKIE[ yourls_cookie_name() ] ) )
|
|
|
|
$valid = yourls_check_auth_cookie();
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
2019-11-23 11:47:38 +01:00
|
|
|
|
|
|
|
return $valid;
|
2018-09-07 23:45:47 +02:00
|
|
|
}
|
|
|
|
?>
|