3
3
Plugin Name: Auth Manager Plus
4
4
Plugin URI: https://github.com/joshp23/YOURLS-AuthMgrPlus
5
5
Description: Role Based Access Controlls with seperated user data for authenticated users
6
- Version: 1 .0.6
6
+ Version: 2 .0.0
7
7
Author: Josh Panter, nicwaller, Ian Barber <[email protected] >
8
8
Author URI: https://unfettered.net
9
9
*/
@@ -24,6 +24,7 @@ class ampCap {
24
24
const AddURL = 'AddURL ' ;
25
25
const DeleteURL = 'DeleteURL ' ;
26
26
const EditURL = 'EditURL ' ;
27
+ const Traceless = 'Traceless ' ;
27
28
const ManageAnonURL = 'ManageAnonURL ' ;
28
29
const ManageUsrsURL = 'ManageUsrsURL ' ;
29
30
const ManagePlugins = 'ManagePlugins ' ;
@@ -52,8 +53,10 @@ function amp_intercept_api() {
52
53
}
53
54
}
54
55
}
56
+ yourls_add_action ( 'auth_successful ' , function () {
57
+ if ( yourls_is_admin () ) amp_intercept_admin ();
58
+ } );
55
59
56
- yourls_add_action ( 'auth_successful ' , 'amp_intercept_admin ' );
57
60
/**
58
61
* YOURLS processes most actions in the admin page. It would be ideal
59
62
* to add a unique hook for each action, but unfortunately we need to
@@ -147,12 +150,12 @@ function amp_intercept_admin() {
147
150
}
148
151
}
149
152
}
153
+
150
154
/*
151
155
* Cosmetic filter: removes disallowed plugins from link list
152
156
*/
153
- if ( yourls_is_admin () ) {
154
- yourls_add_filter ( 'admin_sublinks ' , 'amp_admin_sublinks ' );
155
- }
157
+
158
+ yourls_add_filter ( 'admin_sublinks ' , 'amp_admin_sublinks ' );
156
159
function amp_admin_sublinks ( $ links ) {
157
160
158
161
global $ amp_allowed_plugin_pages ;
@@ -171,10 +174,10 @@ function amp_admin_sublinks( $links ) {
171
174
* Cosmetic filter: displays currently available roles
172
175
* by hovering mouse over the username in logout link.
173
176
*/
177
+
174
178
yourls_add_filter ( 'logout_link ' , 'amp_html_append_roles ' );
175
179
function amp_html_append_roles ( $ original ) {
176
- $ authenticated = yourls_is_valid_user ();
177
- if ( $ authenticated === true ) {
180
+ if ( amp_is_valid_user () ) {
178
181
$ listcaps = implode (', ' , amp_current_capabilities ());
179
182
return '<div title=" ' .$ listcaps .'"> ' .$ original .'</div> ' ;
180
183
} else {
@@ -217,11 +220,9 @@ function amp_have_capability( $capability ) {
217
220
218
221
// Check user-role based auth
219
222
if ( !$ return ) {
220
- // Only users have roles.
221
- $ authenticated = yourls_is_valid_user ();
222
- if ( $ authenticated !== true )
223
+ // Only users have roles
224
+ if ( !amp_is_valid_user () ) //XXX
223
225
return false ;
224
-
225
226
// List capabilities of particular user role
226
227
$ user = YOURLS_USER !== false ? YOURLS_USER : NULL ;
227
228
$ user_caps = array ();
@@ -281,12 +282,8 @@ function amp_admin_list_where($where) {
281
282
return $ where ; // Allow admin/editor users to see the lot.
282
283
283
284
$ user = YOURLS_USER !== false ? YOURLS_USER : NULL ;
284
- if (version_compare (YOURLS_VERSION , '1.7.3 ' ) >= 0 ) {
285
- $ where ['sql ' ] = $ where ['sql ' ] . " AND (`user` = :user OR `user` IS NULL) " ;
286
- $ where ['binds ' ]['user ' ] = $ user ;
287
- }
288
- else
289
- $ where = $ where . " AND (`user` = $ user OR `user` IS NULL) " ;
285
+ $ where ['sql ' ] = $ where ['sql ' ] . " AND (`user` = :user OR `user` IS NULL) " ;
286
+ $ where ['binds ' ]['user ' ] = $ user ;
290
287
291
288
return $ where ;
292
289
}
@@ -313,9 +310,7 @@ function amp_pre_yourls_infos( $keyword ) {
313
310
314
311
if ( yourls_is_private () && !amp_access_keyword ($ keyword ) ) {
315
312
316
- $ authenticated = yourls_is_valid_user ();
317
-
318
- if ( $ authenticated === true )
313
+ if ( !amp_is_valid_user () )
319
314
yourls_redirect ( yourls_admin_url ( '?access=denied ' ), 302 );
320
315
else
321
316
yourls_redirect ( YOURLS_SITE , 302 );
@@ -333,21 +328,29 @@ function amp_get_db_stats( $return, $where ) {
333
328
global $ ydb ;
334
329
$ table_url = YOURLS_DB_TABLE_URL ;
335
330
$ user = YOURLS_USER !== false ? YOURLS_USER : NULL ;
336
- if (version_compare (YOURLS_VERSION , '1.7.3 ' ) >= 0 ) {
337
- $ where ['sql ' ] = $ where ['sql ' ] . " AND (`user` = :user OR `user` IS NULL) " ;
338
- $ where ['binds ' ]['user ' ] = $ user ;
339
- $ sql = "SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM ` $ table_url` WHERE 1=1 " . $ where ['sql ' ];
340
- $ binds = $ where ['binds ' ];
341
- $ totals = $ ydb ->fetchObject ($ sql , $ binds );
342
- } else {
343
- $ where = $ where . " AND (`user` = $ user OR `user` IS NULL) " ;
344
- $ totals = $ ydb ->get_results ("SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM ` $ table_url` WHERE 1=1 " . $ where );
345
- }
331
+
332
+ $ where ['sql ' ] = $ where ['sql ' ] . " AND (`user` = :user OR `user` IS NULL) " ;
333
+ $ where ['binds ' ]['user ' ] = $ user ;
334
+
335
+ $ sql = "SELECT COUNT(keyword) as count, SUM(clicks) as sum FROM ` $ table_url` WHERE 1=1 " . $ where ['sql ' ];
336
+ $ binds = $ where ['binds ' ];
337
+
338
+ $ totals = $ ydb ->fetchObject ($ sql , $ binds );
339
+
346
340
$ return = array ( 'total_links ' => $ totals ->count , 'total_clicks ' => $ totals ->sum );
347
341
348
342
return $ return ;
349
343
}
350
344
345
+ // Fine tune track-me-not
346
+ yourls_add_action ('redirect_shorturl ' , 'amp_tracking ' );
347
+ function amp_tracking ( $ u , $ k ) {
348
+ if ( amp_is_valid_user () && ( amp_keyword_owner ($ k ) || amp_have_capability ( ampCap::Traceless ) ) ) {
349
+ // No logging
350
+ yourls_add_filter ( 'shunt_update_clicks ' , function ( $ u , $ k ) { return true ; } );
351
+ yourls_add_filter ( 'shunt_log_redirect ' , function ( $ u , $ k ) { return true ; } );
352
+ }
353
+ }
351
354
/********************* HOUSEKEEPING ************************/
352
355
// Validate environment setup
353
356
function amp_env_check () {
@@ -368,6 +371,7 @@ function amp_env_check() {
368
371
ampCap::AddURL,
369
372
ampCap::EditURL,
370
373
ampCap::DeleteURL,
374
+ ampCap::Traceless,
371
375
ampCap::ManageAnonURL,
372
376
ampCap::ManageUsrsURL,
373
377
ampCap::ManagePlugins,
@@ -381,6 +385,7 @@ function amp_env_check() {
381
385
ampCap::AddURL,
382
386
ampCap::EditURL,
383
387
ampCap::DeleteURL,
388
+ ampCap::Traceless,
384
389
ampCap::ManageAnonURL,
385
390
ampCap::APIu,
386
391
ampCap::ViewStats,
@@ -442,14 +447,8 @@ function amp_activated() {
442
447
global $ ydb ;
443
448
444
449
$ table = YOURLS_DB_TABLE_URL ;
445
- $ version = version_compare (YOURLS_VERSION , '1.7.3 ' ) >= 0 ;
446
-
447
- if ($ version ) {
448
- $ sql = "DESCRIBE ` $ table` " ;
449
- $ results = $ ydb ->fetchObjects ($ sql );
450
- } else {
451
- $ results = $ ydb ->get_results ("DESCRIBE $ table " );
452
- }
450
+ $ sql = "DESCRIBE ` $ table` " ;
451
+ $ results = $ ydb ->fetchObjects ($ sql );
453
452
454
453
$ activated = false ;
455
454
foreach ($ results as $ r ) {
@@ -478,6 +477,7 @@ function amp_current_capabilities() {
478
477
ampCap::AddURL,
479
478
ampCap::EditURL,
480
479
ampCap::DeleteURL,
480
+ ampCap::Traceless,
481
481
ampCap::ManageAnonURL,
482
482
ampCap::ManageUsrsURL,
483
483
ampCap::ManagePlugins,
@@ -511,66 +511,43 @@ function amp_cidr_match($ip, $range) {
511
511
512
512
// Check user access to a keyword ( can they see it )
513
513
function amp_access_keyword ( $ keyword ) {
514
- global $ ydb ;
515
514
516
- if ( amp_have_capability ( ampCap::ViewAll ) )
515
+ $ users = array ( YOURLS_USER !== false ? YOURLS_USER : NULL , NULL );
516
+ $ owner = amp_keyword_owner ( $ keyword );
517
+ if ( amp_have_capability ( ampCap::ViewAll ) || in_array ( $ owner , $ users ) )
517
518
return true ;
518
-
519
- $ table = YOURLS_DB_TABLE_URL ;
520
- $ user = YOURLS_USER !== false ? YOURLS_USER : NULL ;
521
- if (version_compare (YOURLS_VERSION , '1.7.3 ' ) >= 0 ) {
522
- $ binds = array ( 'keyword ' => $ keyword , 'user ' => $ user );
523
- $ sql = "SELECT 1 FROM ` $ table` WHERE (`user` IS NULL OR `user` = :user) AND `keyword` = :keyword " ;
524
- $ result = $ ydb ->fetchAffected ($ sql , $ binds );
525
- } else
526
- $ result = $ ydb ->query ("SELECT 1 FROM ` $ table` WHERE (`user` IS NULL OR `user` = $ user) AND `keyword` = $ keyword " );
527
-
528
- return $ result > 0 ;
529
519
}
530
520
531
521
// Check user rights to a keyword ( can manage it )
532
522
function amp_manage_keyword ( $ keyword , $ capability ) {
533
523
// only authenticated users can manaage keywords
534
- $ authenticated = yourls_is_valid_user ();
535
- if ( $ authenticated !== true )
524
+ if ( !amp_is_valid_user () )
536
525
return false ;
537
526
// Admin?
538
527
if ( amp_have_capability ( ampCap::ManageUsrsURL ) )
539
528
return true ;
540
529
// Editor?
541
530
$ owner = amp_keyword_owner ($ keyword );
542
- if ( $ owner === null ) {
543
- if ( amp_have_capability ( ampCap::ManageAnonURL ) ) {
544
- return true ;
545
- } else {
546
- return false ;
547
- }
548
- }
531
+ if ( $ owner === null && amp_have_capability ( ampCap::ManageAnonURL ) )
532
+ return true ;
533
+ else
534
+ return false ;
549
535
// Self Edit?
550
536
$ user = YOURLS_USER !== false ? YOURLS_USER : NULL ;
551
- if ( $ owner === $ user ) {
552
- if ( amp_have_capability ( $ capability ) ) {
537
+ if ( $ owner === $ user && amp_have_capability ( $ capability ) )
553
538
return true ;
554
- } else {
539
+ else
555
540
return false ;
556
- }
557
- }
558
-
559
541
return false ;
560
542
}
561
543
562
544
// Check keyword ownership
563
545
function amp_keyword_owner ( $ keyword ) {
564
546
global $ ydb ;
565
547
$ table = YOURLS_DB_TABLE_URL ;
566
-
567
- if (version_compare (YOURLS_VERSION , '1.7.3 ' ) >= 0 ) {
568
- $ binds = array ( 'keyword ' => $ keyword );
569
- $ sql = "SELECT * FROM ` $ table` WHERE `keyword` = :keyword " ;
570
- $ result = $ ydb ->fetchOne ($ sql , $ binds );
571
- } else
572
- $ result = $ ydb ->query ("SELECT 1 FROM ` $ table` WHERE `keyword` = $ keyword " );
573
-
548
+ $ binds = array ( 'keyword ' => $ keyword );
549
+ $ sql = "SELECT * FROM ` $ table` WHERE `keyword` = :keyword " ;
550
+ $ result = $ ydb ->fetchOne ($ sql , $ binds );
574
551
return $ result ['user ' ];
575
552
}
576
553
@@ -584,13 +561,34 @@ function amp_insert_link($actions) {
584
561
$ table = YOURLS_DB_TABLE_URL ;
585
562
586
563
// Insert $keyword against $username
587
- if (version_compare (YOURLS_VERSION , '1.7.3 ' ) >= 0 ) {
588
- $ binds = array ( 'user ' => $ user ,
589
- 'keyword ' => $ keyword );
590
- $ sql = "UPDATE ` $ table` SET `user` = :user WHERE `keyword` = :keyword " ;
591
- $ result = $ ydb ->fetchAffected ($ sql , $ binds );
592
- } else {
593
- $ result = $ ydb ->query ("UPDATE ` $ table` SET `user` = $ user WHERE `keyword` = $ keyword " );
564
+ $ binds = array ( 'user ' => $ user ,
565
+ 'keyword ' => $ keyword );
566
+ $ sql = "UPDATE ` $ table` SET `user` = :user WHERE `keyword` = :keyword " ;
567
+ $ result = $ ydb ->fetchAffected ($ sql , $ binds );
568
+ }
569
+
570
+ // Quick user validation without triggering hooks
571
+ function amp_is_valid_user () {
572
+
573
+ $ valid = defined ( 'YOURLS_USER ' ) ? true : false ;
574
+
575
+ if ( !$ valid ) {
576
+
577
+ if ( yourls_is_API ()
578
+ && isset ( $ _REQUEST ['timestamp ' ] ) && !empty ($ _REQUEST ['timestamp ' ] )
579
+ && isset ( $ _REQUEST ['signature ' ] ) && !empty ($ _REQUEST ['signature ' ] ) )
580
+ $ valid = yourls_check_signature_timestamp ();
581
+ elseif ( yourls_is_API ()
582
+ && !isset ( $ _REQUEST ['timestamp ' ] )
583
+ && isset ( $ _REQUEST ['signature ' ] ) && !empty ( $ _REQUEST ['signature ' ] ) )
584
+ $ valid = yourls_check_signature ();
585
+ elseif ( isset ( $ _REQUEST ['username ' ] ) && isset ( $ _REQUEST ['password ' ] )
586
+ && !empty ( $ _REQUEST ['username ' ] ) && !empty ( $ _REQUEST ['password ' ] ) )
587
+ $ valid = yourls_check_username_password ();
588
+ elseif ( !yourls_is_API () && isset ( $ _COOKIE [ yourls_cookie_name () ] ) )
589
+ $ valid = yourls_check_auth_cookie ();
594
590
}
591
+
592
+ return $ valid ;
595
593
}
596
594
?>
0 commit comments