-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpages_access.php
More file actions
633 lines (436 loc) · 16 KB
/
pages_access.php
File metadata and controls
633 lines (436 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<?php
// Start the session
/*
* ---------------------------------------------------------------------------
* @plugin Name: Pages Access
*
* @description: Admin access control of users in backend
* Allows only certain pages to be viewed in admin(backend) based on user name.
* Allows admin to create groups and assign user to groups, assign groups to
* pages and users to pages.
*
* @version: 1.0
* @author: Craig Adams
* @author URL: http://www.codecobber.co.uk/
* --------------------------------------------------------------------------
*/
# get correct id for plugin
$thisfile=basename(__FILE__, ".php");
# register plugin
register_plugin(
$thisfile, //Plugin id
'Pages Access', //Plugin name
'1.0.8', //Plugin version
'Craig Adams', //Plugin author
'http://www.codecobber.co.uk/', //author website
'Allows only certain pages to be viewed in admin based on user name', //Plugin description
'pages_access', //page type - on which admin tab to display
'pages_access_show' //main function (administration) called immediately on activation of plugin
);
//Globals______________________________________________________________
$PA_current_user = get_cookie('GS_ADMIN_USERNAME');
$PA_folder = GSDATAOTHERPATH . 'categories/';
$PA_filename = $PA_folder . 'cats.php';
$PA_match = 0;
$PA_editMatch = 0;
$PA_catData = file_get_contents($PA_filename);
$PA_page_url=$_SERVER['REQUEST_URI'];
$editURL="";
$qVal = strpos($PA_page_url, "=");
$localAddress = substr($PA_page_url,0,$qVal+1);
$PA_editPage = strripos($PA_page_url, "/");
$PA_editBasicURL = substr($PA_page_url,$PA_editPage+1);
$firstAmpersand = strpos($PA_page_url, "&");
$pageNameforFile = substr($PA_page_url, $qVal+1, ($firstAmpersand-1)-$qVal);
$qm = strripos($PA_page_url, "?");
$stringQuery = substr($PA_page_url,$qm+4);
include(GSDATAOTHERPATH.'categories/groups.php');
//HOOKS________________________________________________________________
//set function for call in pages tab
add_action('pages-main','basicCheck');
# add a link in the admin tab 'pages'
foreach ($admin as $gVal) {
//display page access tab if user is admin
if($gVal === $PA_current_user){
add_action( 'nav-tab', 'createNavTab', array( 'pages_access', $thisfile, 'Pages Access','overview' ) );
add_action('pages_access-sidebar', 'createSideMenu', array($thisfile, '<i class="fa fa-eye" aria-hidden="true"></i> Overview', 'overview'));
add_action('pages_access-sidebar', 'createSideMenu', array($thisfile, '<i class="fa fa-users" aria-hidden="true"></i> Groups user list', 'groupslist'));
add_action('pages_access-sidebar', 'createSideMenu', array($thisfile, '<i class="fa fa-file-text" aria-hidden="true"></i> Add/remove user to page', 'usertopage'));
add_action('pages_access-sidebar', 'createSideMenu', array($thisfile, ' <i class="fa fa-user-circle" aria-hidden="true"></i> Add/remove user to group', 'usertogroup'));
add_action('pages_access-sidebar', 'createSideMenu', array($thisfile, '<i class="fa fa-tag" aria-hidden="true"></i> Add/remove group to page', 'grouptopage'));
}
}
# set function for call in edit page
add_action('edit-extras','editCheck');
# Check for renaming of page via slug
add_action('changedata-aftersave','slugCheck');
# Update pages list on every save
add_action('changedata-save','checkNewPages');
# Delete page
add_action('page-delete','PAdeletePages');
//register and queue the css for the side menu in the plugins page
register_style('pageAccessCSS', $SITEURL.'plugins/pages_access/css/page_access.css','1', false);
queue_style('pageAccessCSS', GSBACK);
//Functions_____________________________________________________________
/*
* ===============================================================
* get_pages() - Explanation:
*
* The get_pages function provides a method to extract a list of pages from the
* data/pages folder. The resulting list is then passed back to the variable that called the
* function.
* ===============================================================
*/
function get_pages(){
$dir_handle = @opendir(GSDATAPAGESPATH) or exit('Unable to open ...getsimple/data/pages folder');
$PA_filenames = array();
while ($PA_filename = readdir($dir_handle))
{
$PA_filenames[] = $PA_filename;
}
$PA_pages = array();
if (count($PA_filenames) != 0)
{
sort($PA_filenames);
foreach ($PA_filenames as $PA_file)
{
if (!($PA_file == '.' || $PA_file == '..' || is_dir(GSDATAPAGESPATH.$PA_file) || $PA_file == '.htaccess'))
{
$thisfile = file_get_contents(GSDATAPAGESPATH.$PA_file);
$PA_XMLdata = simplexml_load_string($thisfile);
$PA_url = (string)$PA_XMLdata->url;
$PA_title = (string)$PA_XMLdata->title;
$PA_pages[$PA_url] = $PA_url;
}
}
}
return $PA_pages;
}
/*
* ===============================================================
* hidePageTitle() & hidePage() - Explanation:
*
* Both functions are used by checkPages to check cats.php against
* each page found in data/pages and are responisble for hiding scripts
* ===============================================================
*/
function basicCheck(){
if(!isset($_POST['chk1'])){
echo "<form id='chk' method='POST' action='".$_SERVER['PHP_SELF']."'>
<input type='hidden' name='chk1' value='101'>
</form>
<script id='chkSend'>document.getElementById('chk').submit()</script>
";
die();
}
elseif(isset($_POST['chk1']) && $_POST['chk1'] === '101'){
echo "<script>console.log('Javascript enabled');</script>";
}
//pass in the page url to check
checkPages('pages.php');
}
function editCheck(){
//pass in the page url to check
checkPages('edit.php');
}
function hidePageTitle($pa_key){
//delete list item from menu and hide all traces from browser inspector
echo "<script id='deleteme1968'>$(document).ready(function(){
$('#tr-".$pa_key."').remove();
$('#deleteme1968').remove();
});
</script>";
}
function hidePage(){
//delete list item from menu and hide all traces from browser inspector
echo "<script id='deleteMain'>
$(document).ready(function(){
$('.main').html('<h2>Access denied!</h2><p>Please speak nicely to the administrator</p><p><b>If you have just renamed a page then go back to \'View All Pages\' in the menu and select the required page.</b></p>');
$('#deleteMain').remove();
});
</script>";
}
/*
* ===============================================================
* checkPages() - Explanation:
*
* On the edit.php page - Checks for current admin user and compares
* with access list in 'data/other/categories' to provide a list
* of pages for the user to access/edit. If user tries to edit
* the url by substituting a new url query string then access is
* denied and user is informed to contact the administrator.
* ===============================================================
*/
function checkPages($pg){
global $stringQuery, $PA_current_user, $PA_folder, $PA_filename, $PA_folder, $PA_match, $PA_editMatch, $PA_page_url, $PA_editPage, $PA_editBasicURL,$USR;
include($PA_filename); //include cats.php
include($PA_folder."groups.php");
$editPage = strpos($PA_page_url, "?");
$query = substr($PA_page_url,$editPage+4);
$editAddress = substr($PA_page_url,0,$editPage);// url upto '?''
$editURL = substr($PA_page_url,$PA_editPage+1,$editPage-$PA_editPage-1);
$thisEditPage=array();
$adminAccess = 0;
$p = get_pages(); //get array of pages from data/pages
//initialise admin array on installation
if(empty($admin)){
$adminInit = file_get_contents($PA_folder."groups.php");
$adminInitUD = str_replace("\$admin = array()","\$admin = array('".$USR."')",$adminInit);
file_put_contents($PA_folder."groups.php",$adminInitUD);
}
/*
loop through data/pages and compare with page data in cats.php
also check user groups for each page
*/
//for each page in data/pages
foreach ($p as $pa_key => $pa_value) {
//now loop through cats.php
foreach ($cats as $catkey => $catvalue){
//eg "t1"=>array('admin')
if($catkey != $pa_value){
continue;
}else{
//lastly, loop through cats.php and get user names
if(is_array($catvalue)){
foreach($catvalue as $cVal){
//create dynamic vaiable from the value
@$groupx = ${$cVal};
//if a group then check
if(is_array($groupx)){
foreach ($groupx as $xVal) {
//check logged in user matches entry in cats
if($xVal === $PA_current_user){
//display list item
$PA_match = 1;
$PA_editMatch = 1;
// create an array of pages user authoriased to view
// and loop through this later
array_push($thisEditPage,$catkey);
// set admin flag if usr name located in admin array
if($cVal === "admin"){
$adminAccess = 1;
}
}
}
}
else{
// if just a single name rather than a group then check user match
if($cVal === $PA_current_user){
$PA_match = 1; //display list item
$PA_editMatch = 1;
array_push($thisEditPage,$catkey);
}
}
}
}
}
}
if($PA_match === 0){
// for pages page
// redirect back to pages list
if($pg === $PA_editBasicURL){
hidePageTitle($pa_key);
}
elseif ($editPage > -1) {
//stop unauthoraised access to other pages
hidePageTitle($pa_key);
}
// if page doesn't exist then same
elseif($stringQuery === "or=The+requested+page+does+not+exist"){
hidePageTitle($pa_key);
}
}
//reset variable to auto hide
$PA_match = 0;
}
//edit page test
if($editURL === "edit.php"){
if($PA_editMatch == 0){
hidePage();
}
else{
$editHide=1;
foreach ($thisEditPage as $editValue) {
if($stringQuery === $editValue || $adminAccess === 1){
echo "Access granted for page: ".$editValue."<br>";
$adminAccess = 0;
$editHide=0;
break;
}
else{
$editHide=1;
}
}
if($editHide === 1){
hidePage();
}
}
}
}
/*
* ===============================================================
* pages_access_show() - Explanation:
*
* Description displayed in panel when link in sidebar is clicked
* and the list of pages displayed.
* ===============================================================
*/
function pages_access_show() {
?>
<script>
//populate the form with user data
var dataName="";
var dataGroup="";
var dataPage = "";
//Update the user form with user name value from radio button
function addData(e){
var dataMatch = e.getAttribute("name");
if(dataMatch==='userCheck'){
dataName = e.getAttribute("data-name");
var nameField = document.getElementById("userNameInput");
nameField.setAttribute("value",dataName);
}
if(dataMatch==='pageCheck'){
dataPage = e.getAttribute("data-page");
var pageField = document.getElementById("userPageInput");
pageField.setAttribute("value",dataPage);
}
if(dataMatch==='groupCheck'){
dataGroup = e.getAttribute("data-group");
var groupField = document.getElementById("userGroupInput");
groupField.setAttribute("value",dataGroup);
}
if(dataMatch==='groupToPageCheck'){
dataGroup = e.getAttribute("data-grouptopage");
var groupToPageField = document.getElementById("groupNameInput");
groupToPageField.setAttribute("value",dataGroup);
}
}
//send the forms
function updateCats(formID,e){
document.getElementById(formID).submit();
}
function updateRemove(formID,e){
var removeCheck = document.getElementById('userRemove');
removeCheck.setAttribute("value","remove");
document.getElementById(formID).submit();
}
</script>
<?php
global $USR, $PA_folder, $PA_filename, $PA_current_user;
$PA_admin = get_pages();//get pages list
$PA_users = GSUSERSPATH;
$userArray = array(); // hold list of pages without xml extension
include($PA_filename); //include cats.php
include($PA_folder."groups.php");
// Open a data/user directory, and read its contents (get a list of users)
if (is_dir($PA_users)){
if ($listDataPages = opendir($PA_users)){
while (($Pagefile = readdir($listDataPages )) !== false){
if($Pagefile === 'autosave' || $Pagefile === '..' || $Pagefile === '.' || $Pagefile === '.htaccess'){
continue;
}
$stripXML = str_replace(".xml", "", $Pagefile);
array_push($userArray, $stripXML);
}
closedir($listDataPages);
}
}
//dynamically load content
if( isset($_GET['overview'])){
include(GSPLUGINPATH.'pages_access/overview.php');
}elseif (isset($_GET['usertopage'])) {
include(GSPLUGINPATH.'pages_access/usertopage.php');
}elseif (isset($_GET['usertogroup'])) {
include(GSPLUGINPATH.'pages_access/usertogroup.php');
}elseif (isset($_GET['grouptopage'])) {
include(GSPLUGINPATH.'pages_access/grouptopage.php');
}elseif (isset($_GET['groupslist'])) {
include(GSPLUGINPATH.'pages_access/groupslist.php');
}
?>
<?php
}
/*
* ===============================================================
* slugCheck() - Explanation:
*
* In changedata.php - On every save, grab the POST variables of url
* and new slug name. Then perform a string replace in the cats.php list.
* ===============================================================
*/
function slugCheck(){
global $PA_filename;
$PA_pageDetails = file_get_contents($PA_filename);
$PA_slug_pageURL = htmlentities($_POST['existing-url']);
$PA_slug_id = htmlentities($_POST['post-id']);
$PA_slugUpdate = str_replace("\"".$PA_slug_pageURL."\"=>array", "\"".$PA_slug_id."\"=>array", $PA_pageDetails);
file_put_contents($PA_filename, $PA_slugUpdate);
checkNewPages(); // check for new pages
}
/*
* ===============================================================
* checkNewPages() - Explanation:
*
* In changedata.php -
* The function updates categories cats.php with a list of pages
* cross referenced from data/pages with a list of pages. By default
* the pages are only accessible by admin
* ===============================================================
*/
function checkNewPages(){
global $PA_filename, $PA_folder;
$PA_PagesList = GSDATAPAGESPATH;
// Open a data/pages directory, and read its contents
if (is_dir($PA_PagesList)){
if ($listDataPages = opendir($PA_PagesList)){
while (($Pagefile = readdir($listDataPages )) !== false){
if($Pagefile === 'autosave' || $Pagefile === '..' || $Pagefile === '.' || $Pagefile === '.htaccess'){
continue;
}
$catsCont = file_get_contents($PA_filename);
//remove extension and ensure add '=>array'to end to ensure accurate string is searched
$pageFileReady = str_replace(".xml","","\"".$Pagefile."\"")."=>array";
$catsPageFound = stripos($catsCont,$pageFileReady);
if($catsPageFound > -1){
continue;
}
else{
$pageFileReady = str_replace("=>array","",$pageFileReady);
$newCatFile = str_replace("cats = array(","cats = array(".$pageFileReady."=>array('admin'),",$catsCont);
file_put_contents($PA_filename,$newCatFile);
}
}
closedir($listDataPages);
}
}
}
/*
* ===============================================================
* pageCheck() - Explanation:
*
* Fired when a page is deleted
* ===============================================================
*/
function PAdeletePages($delPage){
global $PA_filename;
$deletec = get_pages();
$fileID = $_GET['id'];
// delete user to cats.php
$catsDelete = file_get_contents($PA_filename);
// find cats start
$catsStart = stripos($catsDelete, "\$cats = array("); //find start of cats array
// find specific line and save start of array
$deleteDataCheck = stripos($catsDelete, '"'.$fileID.'"=>array'); //find correct item line
$deleteComma = stripos($catsDelete, ",", $deleteDataCheck); // get end of line
if($deleteComma === false){
$semiColonCheck = stripos($catsDelete, ";",$deleteDataCheck);
$deleteString = substr($catsDelete, $deleteDataCheck-1, ($semiColonCheck-$deleteDataCheck));
}
else{
$deleteString = substr($catsDelete, $deleteDataCheck, ($deleteComma-$deleteDataCheck)+1);
}
$readyDeleteData = str_replace($deleteString,"", $catsDelete);
file_put_contents($PA_filename,$readyDeleteData);
}
?>