PluginProbe
User Access Manager / 1.2.7.3
User Access Manager v1.2.7.3
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / class / UserAccessManager.class.php

UserAccessManager.class.php in User Access Manager 1.2.7.3, at class/UserAccessManager.class.php

2,357 lines 71.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UserAccessManager.class.php
4 *
5 * The UserAccessManager class file.
6 *
7 * PHP versions 5
8 *
9 * @category UserAccessManager
10 * @package UserAccessManager
11 * @author Alexander Schneider <alexanderschneider85@googlemail.com>
12 * @copyright 2008-2016 Alexander Schneider
13 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
14 * @version SVN: $Id$
15 * @link http://wordpress.org/extend/plugins/user-access-manager/
16 */
17
18 /**
19 * The user user access manager class.
20 *
21 * @category UserAccessManager
22 * @package UserAccessManager
23 * @author Alexander Schneider <alexanderschneider85@gmail.com>
24 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
25 * @link http://wordpress.org/extend/plugins/user-access-manager/
26 */
27 class UserAccessManager
28 {
29 protected $_blAtAdminPanel = false;
30 protected $_sAdminOptionsName = "uamAdminOptions";
31 protected $_sUamVersion = "1.2.7.3";
32 protected $_sUamDbVersion = "1.3";
33 protected $_aAdminOptions = null;
34 protected $_oAccessHandler = null;
35 protected $_aPostUrls = array();
36 protected $_aMimeTypes = null;
37 protected $_aCache = array();
38 protected $_aPosts = array();
39 protected $_aCategories = array();
40 protected $_aWpOptions = array();
41 protected $_aProcessedTerms = array();
42
43 /**
44 * Constructor.
45 */
46 public function __construct()
47 {
48 do_action('uam_init', $this);
49 }
50
51 /**
52 * Returns the admin options name for the uam.
53 *
54 * @return string
55 */
56 public function getAdminOptionsName()
57 {
58 return $this->_sAdminOptionsName;
59 }
60
61 /**
62 * Flushes the cache.
63 */
64 public function flushCache()
65 {
66 $this->_aCache = array();
67 }
68
69 /**
70 * Adds the variable to the cache.
71 *
72 * @param string $sKey The cache key
73 * @param mixed $mValue The value.
74 */
75 public function addToCache($sKey, $mValue)
76 {
77 $this->_aCache[$sKey] = $mValue;
78 }
79
80 /**
81 * Returns a value from the cache by the given key.
82 *
83 * @param string $sKey
84 *
85 * @return mixed
86 */
87 public function getFromCache($sKey)
88 {
89 if (isset($this->_aCache[$sKey])) {
90 return $this->_aCache[$sKey];
91 }
92
93 return null;
94 }
95
96 public function getWpOption($sOption)
97 {
98 if (!isset($this->_aWpOptions[$sOption])) {
99 $this->_aWpOptions[$sOption] = get_option($sOption);
100 }
101
102 return $this->_aWpOptions[$sOption];
103 }
104
105 /**
106 * Returns a post.
107 *
108 * @param string $sId The post id.
109 *
110 * @return mixed
111 */
112 public function getPost($sId)
113 {
114 if (!isset($this->_aPosts[$sId])) {
115 $this->_aPosts[$sId] = get_post($sId);
116 }
117
118 return $this->_aPosts[$sId];
119 }
120
121 /**
122 * Returns a category.
123 *
124 * @param string $sId The category id.
125 *
126 * @return mixed
127 */
128 public function getCategory($sId)
129 {
130 if (!isset($this->_aCategories[$sId])) {
131 $this->_aCategories[$sId] = get_category($sId);
132 }
133
134 return $this->_aCategories[$sId];
135 }
136
137 /**
138 * Returns all blog of the network.
139 *
140 * @return array()
141 */
142 protected function _getBlogIds()
143 {
144 /**
145 * @var wpdb $wpdb
146 */
147 global $wpdb;
148 $aBlogIds = array();
149
150 if (is_multisite()) {
151 $aBlogIds = $wpdb->get_col(
152 "SELECT blog_id
153 FROM ".$wpdb->blogs
154 );
155 }
156
157 return $aBlogIds;
158 }
159
160 /**
161 * Installs the user access manager.
162 */
163 public function install()
164 {
165 global $wpdb;
166 $aBlogIds = $this->_getBlogIds();
167
168 if (isset($_GET['networkwide'])
169 && ((int)$_GET['networkwide'] === 1)
170 ) {
171 $iCurrentBlogId = $wpdb->blogid;
172
173 foreach ($aBlogIds as $iBlogId) {
174 switch_to_blog($iBlogId);
175 $this->_installUam();
176 }
177
178 switch_to_blog($iCurrentBlogId);
179
180 return null;
181 }
182
183 $this->_installUam();
184 }
185
186 /**
187 * Creates the needed tables at the database and adds the options
188 */
189 protected function _installUam()
190 {
191 /**
192 * @var wpdb $wpdb
193 */
194 global $wpdb;
195 include_once ABSPATH.'wp-admin/includes/upgrade.php';
196
197 $sCharsetCollate = $this->_getCharset();
198
199 $sDbAccessGroupTable = $wpdb->prefix.'uam_accessgroups';
200
201 $sDbUserGroup = $wpdb->get_var(
202 "SHOW TABLES
203 LIKE '".$sDbAccessGroupTable."'"
204 );
205
206 if ($sDbUserGroup != $sDbAccessGroupTable) {
207 dbDelta(
208 "CREATE TABLE ".$sDbAccessGroupTable." (
209 ID int(11) NOT NULL auto_increment,
210 groupname tinytext NOT NULL,
211 groupdesc text NOT NULL,
212 read_access tinytext NOT NULL,
213 write_access tinytext NOT NULL,
214 ip_range mediumtext NULL,
215 PRIMARY KEY (ID)
216 ) $sCharsetCollate;"
217 );
218 }
219
220 $sDbAccessGroupToObjectTable = $wpdb->prefix.'uam_accessgroup_to_object';
221
222 $sDbAccessGroupToObject = $wpdb->get_var(
223 "SHOW TABLES
224 LIKE '".$sDbAccessGroupToObjectTable."'"
225 );
226
227 if ($sDbAccessGroupToObject != $sDbAccessGroupToObjectTable) {
228 dbDelta(
229 "CREATE TABLE " . $sDbAccessGroupToObjectTable . " (
230 object_id VARCHAR(64) NOT NULL,
231 object_type varchar(64) NOT NULL,
232 group_id int(11) NOT NULL,
233 PRIMARY KEY (object_id,object_type,group_id)
234 ) $sCharsetCollate;"
235 );
236 }
237
238 add_option("uam_db_version", $this->_sUamDbVersion);
239 }
240
241 /**
242 * Checks if a database update is necessary.
243 *
244 * @return boolean
245 */
246 public function isDatabaseUpdateNecessary()
247 {
248 global $wpdb;
249 $sBlogIds = $this->_getBlogIds();
250
251 if ($sBlogIds !== array()
252 && is_super_admin()
253 ) {
254 $iCurrentBlogId = $wpdb->blogid;
255
256 foreach ($sBlogIds as $iBlogId) {
257 switch_to_blog($iBlogId);
258 $sCurrentDbVersion = $this->getWpOption("uam_db_version");
259
260 if (version_compare($sCurrentDbVersion, $this->_sUamDbVersion, '<')) {
261 switch_to_blog($iCurrentBlogId);
262 return true;
263 }
264 }
265
266 switch_to_blog($iCurrentBlogId);
267 }
268
269 $sCurrentDbVersion = $this->getWpOption("uam_db_version");
270 return version_compare($sCurrentDbVersion, $this->_sUamDbVersion, '<');
271 }
272
273 /**
274 * Updates the user access manager if an old version was installed.
275 *
276 * @param boolean $blNetworkWide If true update network wide
277 */
278 public function update($blNetworkWide)
279 {
280 global $wpdb;
281 $aBlogIds = $this->_getBlogIds();
282
283 if ($blNetworkWide
284 && $aBlogIds !== array()
285 ) {
286 $iCurrentBlogId = $wpdb->blogid;
287
288 foreach ($aBlogIds as $iBlogId) {
289 switch_to_blog($iBlogId);
290 $this->_installUam();
291 $this->_updateUam();
292 }
293
294 switch_to_blog($iCurrentBlogId);
295 } else {
296 $this->_updateUam();
297 }
298 }
299
300 /**
301 * Updates the user access manager if an old version was installed.
302 */
303 protected function _updateUam()
304 {
305 /**
306 * @var wpdb $wpdb
307 */
308 global $wpdb;
309 $sCurrentDbVersion = $this->getWpOption("uam_db_version");
310
311 if (empty($sCurrentDbVersion)) {
312 $this->install();
313 }
314
315 if (!$this->getWpOption('uam_version') || version_compare($this->getWpOption('uam_version'), "1.0", '<')) {
316 delete_option('allow_comments_locked');
317 }
318
319 $sDbAccessGroup = $wpdb->prefix.'uam_accessgroups';
320
321 $sDbUserGroup = $wpdb->get_var(
322 "SHOW TABLES
323 LIKE '".$sDbAccessGroup."'"
324 );
325
326 if (version_compare($sCurrentDbVersion, $this->_sUamDbVersion, '<')) {
327 $sCharsetCollate = $this->_getCharset();
328
329 if (version_compare($sCurrentDbVersion, "1.0", '<=')) {
330 if ($sDbUserGroup == $sDbAccessGroup) {
331 $wpdb->query(
332 "ALTER TABLE ".$sDbAccessGroup."
333 ADD read_access TINYTEXT NOT NULL DEFAULT '',
334 ADD write_access TINYTEXT NOT NULL DEFAULT '',
335 ADD ip_range MEDIUMTEXT NULL DEFAULT ''"
336 );
337
338 $wpdb->query(
339 "UPDATE ".$sDbAccessGroup."
340 SET read_access = 'group',
341 write_access = 'group'"
342 );
343
344 $sDbIpRange = $wpdb->get_var(
345 "SHOW columns
346 FROM ".$sDbAccessGroup."
347 LIKE 'ip_range'"
348 );
349
350 if ($sDbIpRange != 'ip_range') {
351 $wpdb->query(
352 "ALTER TABLE ".$sDbAccessGroup."
353 ADD ip_range MEDIUMTEXT NULL DEFAULT ''"
354 );
355 }
356 }
357
358 $sDbAccessGroupToObject = $wpdb->prefix.'uam_accessgroup_to_object';
359 $sDbAccessGroupToPost = $wpdb->prefix.'uam_accessgroup_to_post';
360 $sDbAccessGroupToUser = $wpdb->prefix.'uam_accessgroup_to_user';
361 $sDbAccessGroupToCategory = $wpdb->prefix.'uam_accessgroup_to_category';
362 $sDbAccessGroupToRole = $wpdb->prefix.'uam_accessgroup_to_role';
363
364 $wpdb->query(
365 "ALTER TABLE '{$sDbAccessGroupToObject}'
366 CHANGE 'object_id' 'object_id' VARCHAR(64)
367 ".$sCharsetCollate
368 );
369
370 $aObjectTypes = $this->getAccessHandler()->getObjectTypes();
371
372 foreach ($aObjectTypes as $sObjectType) {
373 $sAddition = '';
374
375 if ($this->getAccessHandler()->isPostableType($sObjectType)) {
376 $sDbIdName = 'post_id';
377 $sDatabase = $sDbAccessGroupToPost.', '.$wpdb->posts;
378 $sAddition = " WHERE post_id = ID
379 AND post_type = '".$sObjectType."'";
380 } elseif ($sObjectType == 'category') {
381 $sDbIdName = 'category_id';
382 $sDatabase = $sDbAccessGroupToCategory;
383 } elseif ($sObjectType == 'user') {
384 $sDbIdName = 'user_id';
385 $sDatabase = $sDbAccessGroupToUser;
386 } elseif ($sObjectType == 'role') {
387 $sDbIdName = 'role_name';
388 $sDatabase = $sDbAccessGroupToRole;
389 } else {
390 continue;
391 }
392
393 $sFullDatabase = $sDatabase.$sAddition;
394
395 $sSql = "SELECT {$sDbIdName} as id, group_id as groupId
396 FROM {$sFullDatabase}";
397
398 $aDbObjects = $wpdb->get_results($sSql);
399
400 foreach ($aDbObjects as $oDbObject) {
401 $sSql = "INSERT INTO {$sDbAccessGroupToObject} (
402 group_id,
403 object_id,
404 object_type
405 )
406 VALUES(
407 '{$oDbObject->groupId}',
408 '{$oDbObject->id}',
409 '{$sObjectType}'
410 )";
411
412 $wpdb->query($sSql);
413 }
414 }
415
416 $wpdb->query(
417 "DROP TABLE {$sDbAccessGroupToPost},
418 {$sDbAccessGroupToUser},
419 {$sDbAccessGroupToCategory},
420 {$sDbAccessGroupToRole}"
421 );
422 }
423
424 if (version_compare($sCurrentDbVersion, "1.2", '<=')) {
425 $sDbAccessGroupToObject = $wpdb->prefix.'uam_accessgroup_to_object';
426
427 $sSql = "
428 ALTER TABLE `{$sDbAccessGroupToObject}`
429 CHANGE `object_id` `object_id` VARCHAR(64) NOT NULL,
430 CHANGE `object_type` `object_type` VARCHAR(64) NOT NULL";
431
432 $wpdb->query($sSql);
433 }
434
435 update_option('uam_db_version', $this->_sUamDbVersion);
436 }
437 }
438
439 /**
440 * Clean up wordpress if the plugin will be uninstalled.
441 */
442 public function uninstall()
443 {
444 /**
445 * @var wpdb $wpdb
446 */
447 global $wpdb;
448
449 $wpdb->query(
450 "DROP TABLE ".DB_ACCESSGROUP.",
451 ".DB_ACCESSGROUP_TO_OBJECT
452 );
453
454 delete_option($this->_sAdminOptionsName);
455 delete_option('uam_version');
456 delete_option('uam_db_version');
457 $this->deleteFileProtectionFiles();
458 }
459
460 /**
461 * Returns the database charset.
462 *
463 * @return string
464 */
465 protected function _getCharset()
466 {
467 global $wpdb;
468 $sCharsetCollate = '';
469
470 $sMySlqVersion = $wpdb->get_var("SELECT VERSION() as mysql_version");
471
472 if (version_compare($sMySlqVersion, '4.1.0', '>=')) {
473 if (!empty($wpdb->charset)) {
474 $sCharsetCollate = "DEFAULT CHARACTER SET $wpdb->charset";
475 }
476
477 if (!empty($wpdb->collate)) {
478 $sCharsetCollate.= " COLLATE $wpdb->collate";
479 }
480 }
481
482 return $sCharsetCollate;
483 }
484
485 /**
486 * Remove the htaccess file if the plugin is deactivated.
487 */
488 public function deactivate()
489 {
490 $this->deleteFileProtectionFiles();
491 }
492
493 /**
494 * Returns the current user.
495 *
496 * @return WP_User
497 */
498 public function getCurrentUser()
499 {
500 if (!function_exists('get_userdata')) {
501 include_once ABSPATH.'wp-includes/pluggable.php';
502 }
503
504 //Force user information
505 return wp_get_current_user();
506 }
507
508 /**
509 * Returns the full supported mine types.
510 *
511 * @return array
512 */
513 protected function _getMimeTypes()
514 {
515 if ($this->_aMimeTypes === null) {
516 $aMimeTypes = get_allowed_mime_types();
517 $aFullMimeTypes = array();
518
519 foreach ($aMimeTypes as $sExtensions => $sMineType) {
520 $aExtension = explode('|', $sExtensions);
521
522 foreach ($aExtension as $sExtension) {
523 $aFullMimeTypes[$sExtension] = $sMineType;
524 }
525 }
526
527 $this->_aMimeTypes = $aFullMimeTypes;
528 }
529
530 return $this->_aMimeTypes;
531 }
532
533 /**
534 * @param string $sFileTypes The file types which should be cleaned up.
535 *
536 * @return string
537 */
538 protected function _cleanUpFileTypesForHtaccess($sFileTypes)
539 {
540 $aValidFileTypes = array();
541 $aFileTypes = explode(',', $sFileTypes);
542 $aMimeTypes = $this->_getMimeTypes();
543
544 foreach ($aFileTypes as $sFileType) {
545 $sCleanFileType = trim($sFileType);
546
547 if (isset($aMimeTypes[$sCleanFileType])) {
548 $aValidFileTypes[$sCleanFileType] = $sCleanFileType;
549 }
550 }
551
552 return implode('|', $aValidFileTypes);
553 }
554
555 /**
556 * Returns true if web server is nginx.
557 *
558 * @return bool
559 */
560 public function isNginx()
561 {
562 global $is_nginx;
563 return $is_nginx;
564 }
565
566 /**
567 * Creates a htaccess file.
568 *
569 * @param string $sDir The destination directory.
570 * @param string $sObjectType The object type.
571 */
572 public function createFileProtection($sDir = null, $sObjectType = null)
573 {
574 $blNginx = $this->isNginx();
575
576 if ($sDir === null) {
577 $aWordpressUploadDir = wp_upload_dir();
578
579 if (empty($aWordpressUploadDir['error'])) {
580 $sDir = $aWordpressUploadDir['basedir'] . "/";
581 }
582 }
583
584 $sFileName = ($blNginx === true) ? "uam.conf" : ".htaccess";
585
586 if ($sDir !== null) {
587 $sFile = "";
588 $sAreaName = "WP-Files";
589 $aUamOptions = $this->getAdminOptions();
590
591 if (!$this->isPermalinksActive()) {
592 $sFileTypes = null;
593
594 if ($aUamOptions['lock_file_types'] == 'selected') {
595 $sFileTypes = $this->_cleanUpFileTypesForHtaccess($aUamOptions['locked_file_types']);
596 $sFileTypes = "\.(".$sFileTypes.")";
597 } elseif ($blNginx === false && $aUamOptions['lock_file_types'] == 'not_selected') {
598 $sFileTypes = $this->_cleanUpFileTypesForHtaccess($aUamOptions['not_locked_file_types']);
599 $sFileTypes = "^\.(".$sFileTypes.")";
600 }
601
602 if ($blNginx === true) {
603 $sFile = "location " . str_replace(ABSPATH, '/', $sDir) . " {\n";
604
605 if ($sFileTypes !== null) {
606 $sFile .= "location ~ $sFileTypes {\n";
607 }
608
609 $sFile .= "auth_basic \"" . $sAreaName . "\";" . "\n";
610 $sFile .= "auth_basic_user_file ". $sDir . ".htpasswd;" . "\n";
611 $sFile .= "}\n";
612
613 if ($sFileTypes !== null) {
614 $sFile .= "}\n";
615 }
616 } else {
617 // make .htaccess and .htpasswd
618 $sFile .= "AuthType Basic" . "\n";
619 $sFile .= "AuthName \"" . $sAreaName . "\"" . "\n";
620 $sFile .= "AuthUserFile " . $sDir . ".htpasswd" . "\n";
621 $sFile .= "require valid-user" . "\n";
622
623 if ($sFileTypes !== null) {
624 $sFile = "<FilesMatch '" . $sFileTypes . "'>\n" . $sFile . "</FilesMatch>\n";
625 }
626 }
627
628 $this->createHtpasswd(true);
629 } else {
630 if ($sObjectType === null) {
631 $sObjectType = 'attachment';
632 }
633
634 if ($blNginx === true) {
635 $sFile = "location " . str_replace(ABSPATH, '/', $sDir) . " {" . "\n";
636 $sFile .= "rewrite ^(.*)$ /index.php?uamfiletype=" . $sObjectType . "&uamgetfile=$1 last;" . "\n";
637 $sFile .= "}" . "\n";
638 } else {
639 $aHomeRoot = parse_url(home_url());
640 $sHomeRoot = (isset($aHomeRoot['path'])) ? trailingslashit($aHomeRoot['path']) : '/';
641
642 $sFile = "<IfModule mod_rewrite.c>\n";
643 $sFile .= "RewriteEngine On\n";
644 $sFile .= "RewriteBase " . $sHomeRoot . "\n";
645 $sFile .= "RewriteRule ^index\.php$ - [L]\n";
646 $sFile .= "RewriteRule (.*) ";
647 $sFile .= $sHomeRoot . "index.php?uamfiletype=" . $sObjectType . "&uamgetfile=$1 [L]\n";
648 $sFile .= "</IfModule>\n";
649 }
650 }
651
652 // save files
653 $sFileWithPath = ($blNginx === true) ? ABSPATH.$sFileName : $sDir.$sFileName;
654
655 $oFileHandler = fopen($sFileWithPath, "w");
656 fwrite($oFileHandler, $sFile);
657 fclose($oFileHandler);
658 }
659 }
660
661 /**
662 * Creates a htpasswd file.
663 *
664 * @param boolean $blCreateNew Force to create new file.
665 * @param string $sDir The destination directory.
666 */
667 public function createHtpasswd($blCreateNew = false, $sDir = null)
668 {
669 $oCurrentUser = $this->getCurrentUser();
670 if (!function_exists('get_userdata')) {
671 include_once ABSPATH.'wp-includes/pluggable.php';
672 }
673
674 $aUamOptions = $this->getAdminOptions();
675
676 // get url
677 if ($sDir === null) {
678 $aWordpressUploadDir = wp_upload_dir();
679
680 if (empty($aWordpressUploadDir['error'])) {
681 $sDir = $aWordpressUploadDir['basedir'] . "/";
682 }
683 }
684
685 if ($sDir !== null) {
686 $oUserData = get_userdata($oCurrentUser->ID);
687
688 if (!file_exists($sDir.".htpasswd") || $blCreateNew) {
689 if ($aUamOptions['file_pass_type'] == 'random') {
690 $sPassword = md5($this->getRandomPassword());
691 } else {
692 $sPassword = $oUserData->user_pass;
693 }
694
695 $sUser = $oUserData->user_login;
696
697 // make .htpasswd
698 $sHtpasswdTxt = "$sUser:" . $sPassword . "\n";
699
700 // save file
701 $oFileHandler = fopen($sDir.".htpasswd", "w");
702 fwrite($oFileHandler, $sHtpasswdTxt);
703 fclose($oFileHandler);
704 }
705 }
706 }
707
708 /**
709 * Deletes the htaccess files.
710 *
711 * @param string $sDir The destination directory.
712 */
713 public function deleteFileProtectionFiles($sDir = null)
714 {
715 if ($sDir === null) {
716 $aWordpressUploadDir = wp_upload_dir();
717
718 if (empty($aWordpressUploadDir['error'])) {
719 $sDir = $aWordpressUploadDir['basedir'] . "/";
720 }
721 }
722
723 if ($sDir !== null) {
724 $blNginx = $this->isNginx();
725 $sFileName = ($blNginx === true) ? ABSPATH."uam.conf" : $sDir.".htaccess";
726
727 if (file_exists($sFileName)) {
728 unlink($sFileName);
729 }
730
731 if (file_exists($sDir.".htpasswd")) {
732 unlink($sDir.".htpasswd");
733 }
734 }
735 }
736
737 /**
738 * Generates and returns a random password.
739 *
740 * @return string
741 */
742 public function getRandomPassword()
743 {
744 //create password
745 $aArray = array();
746 $iLength = 16;
747
748 // numbers
749 for ($i = 48; $i < 58; $i++) {
750 $aArray[] = chr($i);
751 }
752
753 // small
754 for ($i = 97; $i < 122; $i++) {
755 $aArray[] = chr($i);
756 }
757
758 // capitals
759 for ($i = 65; $i < 90; $i++) {
760 $aArray[] = chr($i);
761 }
762
763 mt_srand((double)microtime() * 1000000);
764 $sPassword = '';
765
766 for ($i = 1; $i <= $iLength; $i++) {
767 $iRandomNumber = mt_rand(0, count($aArray) - 1);
768 $sPassword .= $aArray[$iRandomNumber];
769 }
770
771 return $sPassword;
772 }
773
774 /**
775 * Returns the current settings
776 *
777 * @return array
778 */
779 public function getAdminOptions()
780 {
781 if ($this->_aAdminOptions === null) {
782 $aUamAdminOptions = array(
783 'hide_post_title' => 'false',
784 'post_title' => __('No rights!', 'user-access-manager'),
785 'post_content' => __(
786 'Sorry you have no rights to view this post!',
787 'user-access-manager'
788 ),
789 'hide_post' => 'false',
790 'hide_post_comment' => 'false',
791 'post_comment_content' => __(
792 'Sorry no rights to view comments!',
793 'user-access-manager'
794 ),
795 'post_comments_locked' => 'false',
796 'hide_page_title' => 'false',
797 'page_title' => __('No rights!', 'user-access-manager'),
798 'page_content' => __(
799 'Sorry you have no rights to view this page!',
800 'user-access-manager'
801 ),
802 'hide_page' => 'false',
803 'hide_page_comment' => 'false',
804 'page_comment_content' => __(
805 'Sorry no rights to view comments!',
806 'user-access-manager'
807 ),
808 'page_comments_locked' => 'false',
809 'redirect' => 'false',
810 'redirect_custom_page' => '',
811 'redirect_custom_url' => '',
812 'lock_recursive' => 'true',
813 'authors_has_access_to_own' => 'true',
814 'authors_can_add_posts_to_groups' => 'false',
815 'lock_file' => 'false',
816 'file_pass_type' => 'random',
817 'lock_file_types' => 'all',
818 'download_type' => 'fopen',
819 'locked_file_types' => 'zip,rar,tar,gz',
820 'not_locked_file_types' => 'gif,jpg,jpeg,png',
821 'blog_admin_hint' => 'true',
822 'blog_admin_hint_text' => '[L]',
823 'hide_empty_categories' => 'true',
824 'protect_feed' => 'true',
825 'show_post_content_before_more' => 'false',
826 'full_access_role' => 'administrator'
827 );
828
829 $aUamOptions = $this->getWpOption($this->_sAdminOptionsName);
830
831 if (!empty($aUamOptions)) {
832 foreach ($aUamOptions as $sKey => $mOption) {
833 $aUamAdminOptions[$sKey] = $mOption;
834 }
835 }
836
837 update_option($this->_sAdminOptionsName, $aUamAdminOptions);
838 $this->_aAdminOptions = $aUamAdminOptions;
839 }
840
841 return $this->_aAdminOptions;
842 }
843
844 /**
845 * Returns the content of the excluded php file.
846 *
847 * @param string $sFileName The file name
848 * @param integer $iObjectId The _iId if needed.
849 * @param string $sObjectType The object type if needed.
850 *
851 * @return string
852 */
853 public function getIncludeContents($sFileName, $iObjectId = null, $sObjectType = null)
854 {
855 if (is_file($sFileName)) {
856 ob_start();
857 include $sFileName;
858 $sContents = ob_get_contents();
859 ob_end_clean();
860
861 return $sContents;
862 }
863
864 return '';
865 }
866
867 /**
868 * Returns the access handler object.
869 *
870 * @return UamAccessHandler
871 */
872 public function &getAccessHandler()
873 {
874 if ($this->_oAccessHandler == null) {
875 $this->_oAccessHandler = new UamAccessHandler($this);
876 }
877
878 return $this->_oAccessHandler;
879 }
880
881 /**
882 * Returns the current version of the user access manager.
883 *
884 * @return string
885 */
886 public function getVersion()
887 {
888 return $this->_sUamVersion;
889 }
890
891 /**
892 * Returns true if a user is at the admin panel.
893 *
894 * @return boolean
895 */
896 public function atAdminPanel()
897 {
898 return $this->_blAtAdminPanel;
899 }
900
901 /**
902 * Sets the atAdminPanel var to true.
903 */
904 public function setAtAdminPanel()
905 {
906 $this->_blAtAdminPanel = true;
907 }
908
909
910 /*
911 * Helper functions.
912 */
913
914 /**
915 * Checks if a string starts with the given needle.
916 *
917 * @param string $sHaystack The haystack.
918 * @param string $sNeedle The needle.
919 *
920 * @return boolean
921 */
922 public function startsWith($sHaystack, $sNeedle)
923 {
924 return strpos($sHaystack, $sNeedle) === 0;
925 }
926
927
928 /*
929 * Functions for the admin panel content.
930 */
931
932 /**
933 * The function for the wp_print_styles action.
934 */
935 public function addStyles()
936 {
937 wp_enqueue_style(
938 'UserAccessManagerAdmin',
939 UAM_URLPATH . "css/uamAdmin.css",
940 array() ,
941 '1.0',
942 'screen'
943 );
944
945 wp_enqueue_style(
946 'UserAccessManagerLoginForm',
947 UAM_URLPATH . "css/uamLoginForm.css",
948 array() ,
949 '1.0',
950 'screen'
951 );
952 }
953
954 /**
955 * The function for the wp_print_scripts action.
956 */
957 public function addScripts()
958 {
959 wp_enqueue_script(
960 'UserAccessManagerFunctions',
961 UAM_URLPATH . 'js/functions.js',
962 array('jquery')
963 );
964 }
965
966 /**
967 * Prints the admin page.
968 */
969 public function printAdminPage()
970 {
971 if (isset($_GET['page'])) {
972 $sAdminPage = $_GET['page'];
973
974 if ($sAdminPage == 'uam_settings') {
975 include UAM_REALPATH."tpl/adminSettings.php";
976 } elseif ($sAdminPage == 'uam_usergroup') {
977 include UAM_REALPATH."tpl/adminGroup.php";
978 } elseif ($sAdminPage == 'uam_setup') {
979 include UAM_REALPATH."tpl/adminSetup.php";
980 } elseif ($sAdminPage == 'uam_about') {
981 include UAM_REALPATH."tpl/about.php";
982 }
983 }
984 }
985
986 /**
987 * Shows the error if the user has no rights to edit the content.
988 */
989 public function noRightsToEditContent()
990 {
991 $blNoRights = false;
992
993 if (isset($_GET['post']) && is_numeric($_GET['post'])) {
994 $oPost = $this->getPost($_GET['post']);
995 $blNoRights = !$this->getAccessHandler()->checkObjectAccess( $oPost->post_type, $oPost->ID );
996 }
997
998 if (isset($_GET['attachment_id']) && is_numeric($_GET['attachment_id']) && !$blNoRights) {
999 $oPost = $this->getPost($_GET['attachment_id']);
1000 $blNoRights = !$this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID);
1001 }
1002
1003 if (isset($_GET['tag_ID']) && is_numeric($_GET['tag_ID']) && !$blNoRights) {
1004 $blNoRights = !$this->getAccessHandler()->checkObjectAccess('category', $_GET['tag_ID']);
1005 }
1006
1007 if ($blNoRights) {
1008 wp_die(TXT_UAM_NO_RIGHTS);
1009 }
1010 }
1011
1012 /**
1013 * The function for the wp_dashboard_setup action.
1014 * Removes widgets to which a user should not have access.
1015 */
1016 public function setupAdminDashboard()
1017 {
1018 global $wp_meta_boxes;
1019
1020 if (!$this->getAccessHandler()->checkUserAccess('manage_user_groups')) {
1021 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
1022 }
1023 }
1024
1025 /**
1026 * The function for the update_option_permalink_structure action.
1027 */
1028 public function updatePermalink()
1029 {
1030 $this->createFileProtection();
1031 }
1032
1033
1034 /*
1035 * Meta functions
1036 */
1037
1038 /**
1039 * Saves the object data to the database.
1040 *
1041 * @param string $sObjectType The object type.
1042 * @param integer $iObjectId The _iId of the object.
1043 * @param array $aUserGroups The new usergroups for the object.
1044 */
1045 protected function _saveObjectData($sObjectType, $iObjectId, $aUserGroups = null)
1046 {
1047 $oUamAccessHandler = $this->getAccessHandler();
1048 $oUamOptions = $this->getAdminOptions();
1049 $aFormData = array();
1050
1051 if (isset($_POST['uam_update_groups'])) {
1052 $aFormData = $_POST;
1053 } elseif (isset($_GET['uam_update_groups'])) {
1054 $aFormData = $_GET;
1055 }
1056
1057 if (isset($aFormData['uam_update_groups'])
1058 && ($oUamAccessHandler->checkUserAccess('manage_user_groups')
1059 || $oUamOptions['authors_can_add_posts_to_groups'] == 'true')
1060 ) {
1061 if ($aUserGroups === null) {
1062 $aUserGroups = (isset($aFormData['uam_usergroups']) && is_array($aFormData['uam_usergroups']))
1063 ? $aFormData['uam_usergroups'] : array();
1064 }
1065
1066 $aAddUserGroups = array_flip($aUserGroups);
1067 $aRemoveUserGroups = $oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId);
1068 $aUamUserGroups = $oUamAccessHandler->getUserGroups();
1069 $blRemoveOldAssignments = true;
1070
1071 if (isset($aFormData['uam_bulk_type'])) {
1072 $sBulkType = $aFormData['uam_bulk_type'];
1073
1074 if ($sBulkType === 'add') {
1075 $blRemoveOldAssignments = false;
1076 } elseif ($sBulkType === 'remove') {
1077 $aRemoveUserGroups = $aAddUserGroups;
1078 $aAddUserGroups = array();
1079 }
1080 }
1081
1082 foreach ($aUamUserGroups as $sGroupId => $oUamUserGroup) {
1083 if (isset($aRemoveUserGroups[$sGroupId])) {
1084 $oUamUserGroup->removeObject($sObjectType, $iObjectId);
1085 }
1086
1087 if (isset($aAddUserGroups[$sGroupId])) {
1088 $oUamUserGroup->addObject($sObjectType, $iObjectId);
1089 }
1090
1091 $oUamUserGroup->save($blRemoveOldAssignments);
1092 }
1093 }
1094 }
1095
1096
1097 /*
1098 * Functions for the post actions.
1099 */
1100
1101 /**
1102 * The function for the manage_posts_columns and
1103 * the manage_pages_columns filter.
1104 *
1105 * @param array $aDefaults The table headers.
1106 *
1107 * @return array
1108 */
1109 public function addPostColumnsHeader($aDefaults)
1110 {
1111 $aDefaults['uam_access'] = __('Access', 'user-access-manager');
1112 return $aDefaults;
1113 }
1114
1115 /**
1116 * The function for the manage_users_custom_column action.
1117 *
1118 * @param string $sColumnName The column name.
1119 * @param integer $iId The _iId.
1120 */
1121 public function addPostColumn($sColumnName, $iId)
1122 {
1123 if ($sColumnName == 'uam_access') {
1124 $oPost = $this->getPost($iId);
1125 echo $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $oPost->ID, $oPost->post_type);
1126 }
1127 }
1128
1129 /**
1130 * The function for the uma_post_access metabox.
1131 *
1132 * @param object $oPost The post.
1133 */
1134 public function editPostContent($oPost)
1135 {
1136 $iObjectId = $oPost->ID;
1137 include UAM_REALPATH.'tpl/postEditForm.php';
1138 }
1139
1140 public function addBulkAction($sColumnName)
1141 {
1142 if ($sColumnName == 'uam_access') {
1143 include UAM_REALPATH.'tpl/bulkEditForm.php';
1144 }
1145 }
1146
1147 /**
1148 * The function for the save_post action.
1149 *
1150 * @param mixed $mPostParam The post _iId or a array of a post.
1151 */
1152 public function savePostData($mPostParam)
1153 {
1154 if (is_array($mPostParam)) {
1155 $oPost = $this->getPost($mPostParam['ID']);
1156 } else {
1157 $oPost = $this->getPost($mPostParam);
1158 }
1159
1160 $iPostId = $oPost->ID;
1161 $sPostType = $oPost->post_type;
1162
1163 if ($sPostType == 'revision') {
1164 $iPostId = $oPost->post_parent;
1165 $oParentPost = $this->getPost($iPostId);
1166 $sPostType = $oParentPost->post_type;
1167 }
1168
1169 $this->_saveObjectData($sPostType, $iPostId);
1170 }
1171
1172 /**
1173 * The function for the attachment_fields_to_save filter.
1174 * We have to use this because the attachment actions work
1175 * not in the way we need.
1176 *
1177 * @param object $oAttachment The attachment _iId.
1178 *
1179 * @return object
1180 */
1181 public function saveAttachmentData($oAttachment)
1182 {
1183 $this->savePostData($oAttachment['ID']);
1184
1185 return $oAttachment;
1186 }
1187
1188 /**
1189 * The function for the delete_post action.
1190 *
1191 * @param integer $iPostId The post _iId.
1192 */
1193 public function removePostData($iPostId)
1194 {
1195 /**
1196 * @var wpdb $wpdb
1197 */
1198 global $wpdb;
1199 $oPost = $this->getPost($iPostId);
1200
1201 $wpdb->query(
1202 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1203 WHERE object_id = '".$iPostId."'
1204 AND object_type = '".$oPost->post_type."'"
1205 );
1206 }
1207
1208 /**
1209 * The function for the media_meta action.
1210 *
1211 * @param string $sMeta The meta.
1212 * @param object $oPost The post.
1213 *
1214 * @return string
1215 */
1216 public function showMediaFile($sMeta = '', $oPost = null)
1217 {
1218 $sContent = $sMeta;
1219 $sContent .= '</td></tr><tr>';
1220 $sContent .= '<th class="label">';
1221 $sContent .= '<label>'.TXT_UAM_SET_UP_USERGROUPS.'</label>';
1222 $sContent .= '</th>';
1223 $sContent .= '<td class="field">';
1224 $sContent .= $this->getIncludeContents(UAM_REALPATH.'tpl/postEditForm.php', $oPost->ID);
1225
1226 return $sContent;
1227 }
1228
1229
1230 /*
1231 * Functions for the user actions.
1232 */
1233
1234 /**
1235 * The function for the manage_users_columns filter.
1236 *
1237 * @param array $aDefaults The table headers.
1238 *
1239 * @return array
1240 */
1241 public function addUserColumnsHeader($aDefaults)
1242 {
1243 $aDefaults['uam_access'] = __('uam user groups');
1244 return $aDefaults;
1245 }
1246
1247 /**
1248 * The function for the manage_users_custom_column action.
1249 *
1250 * @param string $sReturn The normal return value.
1251 * @param string $sColumnName The column name.
1252 * @param integer $iId The _iId.
1253 *
1254 * @return string|null
1255 */
1256 public function addUserColumn($sReturn, $sColumnName, $iId)
1257 {
1258 if ($sColumnName == 'uam_access') {
1259 return $this->getIncludeContents(UAM_REALPATH.'tpl/userColumn.php', $iId, 'user');
1260 }
1261
1262 return $sReturn;
1263 }
1264
1265 /**
1266 * The function for the edit_user_profile action.
1267 */
1268 public function showUserProfile()
1269 {
1270 echo $this->getIncludeContents(UAM_REALPATH.'tpl/userProfileEditForm.php');
1271 }
1272
1273 /**
1274 * The function for the profile_update action.
1275 *
1276 * @param integer $iUserId The user _iId.
1277 */
1278 public function saveUserData($iUserId)
1279 {
1280 $this->_saveObjectData('user', $iUserId);
1281 }
1282
1283 /**
1284 * The function for the delete_user action.
1285 *
1286 * @param integer $iUserId The user _iId.
1287 */
1288 public function removeUserData($iUserId)
1289 {
1290 /**
1291 * @var wpdb $wpdb
1292 */
1293 global $wpdb;
1294
1295 $wpdb->query(
1296 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1297 WHERE object_id = ".$iUserId."
1298 AND object_type = 'user'"
1299 );
1300 }
1301
1302
1303 /*
1304 * Functions for the category actions.
1305 */
1306
1307 /**
1308 * The function for the manage_categories_columns filter.
1309 *
1310 * @param array $aDefaults The table headers.
1311 *
1312 * @return array
1313 */
1314 public function addCategoryColumnsHeader($aDefaults)
1315 {
1316 $aDefaults['uam_access'] = __('Access', 'user-access-manager');
1317 return $aDefaults;
1318 }
1319
1320 /**
1321 * The function for the manage_categories_custom_column action.
1322 *
1323 * @param string $sEmpty An empty string from wordpress? What the hell?!?
1324 * @param string $sColumnName The column name.
1325 * @param integer $iId The _iId.
1326 *
1327 * @return string|null
1328 */
1329 public function addCategoryColumn($sEmpty, $sColumnName, $iId)
1330 {
1331 if ($sColumnName == 'uam_access') {
1332 return $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $iId, 'category');
1333 }
1334
1335 return null;
1336 }
1337
1338 /**
1339 * The function for the edit_category_form action.
1340 *
1341 * @param object $oCategory The category.
1342 */
1343 public function showCategoryEditForm($oCategory)
1344 {
1345 include UAM_REALPATH.'tpl/categoryEditForm.php';
1346 }
1347
1348 /**
1349 * The function for the edit_category action.
1350 *
1351 * @param integer $iCategoryId The category _iId.
1352 */
1353 public function saveCategoryData($iCategoryId)
1354 {
1355 $this->_saveObjectData('category', $iCategoryId);
1356 }
1357
1358 /**
1359 * The function for the delete_category action.
1360 *
1361 * @param integer $iCategoryId The _iId of the category.
1362 */
1363 public function removeCategoryData($iCategoryId)
1364 {
1365 /**
1366 * @var wpdb $wpdb
1367 */
1368 global $wpdb;
1369
1370 $wpdb->query(
1371 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1372 WHERE object_id = ".$iCategoryId."
1373 AND object_type = 'category'"
1374 );
1375 }
1376
1377
1378 /*
1379 * Functions for the pluggable object actions.
1380 */
1381
1382 /**
1383 * The function for the pluggable save action.
1384 *
1385 * @param string $sObjectType The name of the pluggable object.
1386 * @param integer $iObjectId The pluggable object _iId.
1387 * @param array $aUserGroups The user groups for the object.
1388 */
1389 public function savePlObjectData($sObjectType, $iObjectId, $aUserGroups = null)
1390 {
1391 $this->_saveObjectData($sObjectType, $iObjectId, $aUserGroups);
1392 }
1393
1394 /**
1395 * The function for the pluggable remove action.
1396 *
1397 * @param string $sObjectName The name of the pluggable object.
1398 * @param integer $iObjectId The pluggable object _iId.
1399 */
1400 public function removePlObjectData($sObjectName, $iObjectId)
1401 {
1402 /**
1403 * @var wpdb $wpdb
1404 */
1405 global $wpdb;
1406
1407 $wpdb->query(
1408 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1409 WHERE object_id = ".$iObjectId."
1410 AND object_type = ".$sObjectName
1411 );
1412 }
1413
1414 /**
1415 * Returns the group selection form for pluggable _aObjects.
1416 *
1417 * @param string $sObjectType The object type.
1418 * @param integer $iObjectId The _iId of the object.
1419 * @param string $aGroupsFormName The name of the form which contains the groups.
1420 *
1421 * @return string;
1422 */
1423 public function showPlGroupSelectionForm($sObjectType, $iObjectId, $aGroupsFormName = null)
1424 {
1425 $sFileName = UAM_REALPATH.'tpl/groupSelectionForm.php';
1426 $aUamUserGroups = $this->getAccessHandler()->getUserGroups();
1427 $aUserGroupsForObject = $this->getAccessHandler()->getUserGroupsForObject($sObjectType, $iObjectId);
1428
1429 if (is_file($sFileName)) {
1430 ob_start();
1431 include $sFileName;
1432 $sContents = ob_get_contents();
1433 ob_end_clean();
1434
1435 return $sContents;
1436 }
1437
1438 return '';
1439 }
1440
1441 /**
1442 * Returns the column for a pluggable object.
1443 *
1444 * @param string $sObjectType The object type.
1445 * @param integer $iObjectId The object _iId.
1446 *
1447 * @return string
1448 */
1449 public function getPlColumn($sObjectType, $iObjectId)
1450 {
1451 return $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $iObjectId, $sObjectType);
1452 }
1453
1454
1455 /*
1456 * Functions for the blog content.
1457 */
1458
1459 /**
1460 * Manipulates the wordpress query object to filter content.
1461 *
1462 * @param object $oWpQuery The wordpress query object.
1463 */
1464 public function parseQuery($oWpQuery)
1465 {
1466 $aUamOptions = $this->getAdminOptions();
1467
1468 if ($aUamOptions['hide_post'] == 'true') {
1469 $oUamAccessHandler = $this->getAccessHandler();
1470 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1471
1472 if (count($aExcludedPosts) > 0) {
1473 $oWpQuery->query_vars['post__not_in'] = array_merge(
1474 $oWpQuery->query_vars['post__not_in'],
1475 $aExcludedPosts
1476 );
1477 }
1478 }
1479 }
1480
1481 /**
1482 * Modifies the content of the post by the given settings.
1483 *
1484 * @param object $oPost The current post.
1485 *
1486 * @return object|null
1487 */
1488 protected function _getPost($oPost)
1489 {
1490 $aUamOptions = $this->getAdminOptions();
1491 $oUamAccessHandler = $this->getAccessHandler();
1492
1493 $sPostType = $oPost->post_type;
1494
1495 if ($this->getAccessHandler()->isPostableType($sPostType) && $sPostType != 'post' && $sPostType != 'page') {
1496 $sPostType = 'post';
1497 } elseif ($sPostType != 'post' && $sPostType != 'page') {
1498 return $oPost;
1499 }
1500
1501 if ($aUamOptions['hide_'.$sPostType] == 'true' || $this->atAdminPanel()) {
1502 if ($oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1503 $oPost->post_title .= $this->adminOutput($oPost->post_type, $oPost->ID);
1504 return $oPost;
1505 }
1506 } else {
1507 if (!$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1508 $oPost->isLocked = true;
1509
1510 $sUamPostContent = $aUamOptions[$sPostType.'_content'];
1511 $sUamPostContent = str_replace("[LOGIN_FORM]", $this->getLoginBarHtml(), $sUamPostContent);
1512
1513 if ($aUamOptions['hide_'.$sPostType.'_title'] == 'true') {
1514 $oPost->post_title = $aUamOptions[$sPostType.'_title'];
1515 }
1516
1517 if ($aUamOptions[$sPostType.'_comments_locked'] == 'false') {
1518 $oPost->comment_status = 'close';
1519 }
1520
1521 if ($aUamOptions['show_post_content_before_more'] == 'true'
1522 && $sPostType == "post"
1523 && preg_match('/<!--more(.*?)?-->/', $oPost->post_content, $aMatches)
1524 ) {
1525 $oPost->post_content = explode($aMatches[0], $oPost->post_content, 2);
1526 $sUamPostContent = $oPost->post_content[0] . " " . $sUamPostContent;
1527 }
1528
1529 $oPost->post_content = stripslashes($sUamPostContent);
1530 }
1531
1532 $oPost->post_title .= $this->adminOutput($oPost->post_type, $oPost->ID);
1533
1534 return $oPost;
1535 }
1536
1537 return null;
1538 }
1539
1540 /**
1541 * The function for the the_posts filter.
1542 *
1543 * @param array $aPosts The posts.
1544 *
1545 * @return array
1546 */
1547 public function showPost($aPosts = array())
1548 {
1549 $aShowPosts = array();
1550 $aUamOptions = $this->getAdminOptions();
1551
1552 if (!is_feed() || ($aUamOptions['protect_feed'] == 'true' && is_feed())) {
1553 foreach ($aPosts as $iPostId) {
1554 if ($iPostId !== null) {
1555 $oPost = $this->_getPost($iPostId);
1556
1557 if ($oPost !== null) {
1558 $aShowPosts[] = $oPost;
1559 }
1560 }
1561 }
1562
1563 $aPosts = $aShowPosts;
1564 }
1565
1566 return $aPosts;
1567 }
1568
1569 /**
1570 * The function for the posts_where_paged filter.
1571 *
1572 * @param string $sSql The where sql statement.
1573 *
1574 * @return string
1575 */
1576 public function showPostSql($sSql)
1577 {
1578 $oUamAccessHandler = $this->getAccessHandler();
1579 $aUamOptions = $this->getAdminOptions();
1580
1581 if ($aUamOptions['hide_post'] == 'true') {
1582 global $wpdb;
1583 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1584
1585 if (count($aExcludedPosts) > 0) {
1586 $sExcludedPostsStr = implode(",", $aExcludedPosts);
1587 $sSql .= " AND $wpdb->posts.ID NOT IN($sExcludedPostsStr) ";
1588 }
1589 }
1590
1591 return $sSql;
1592 }
1593
1594 /**
1595 * The function for the wp_get_nav_menu_items filter.
1596 *
1597 * @param array $aItems The menu item.
1598 *
1599 * @return array
1600 */
1601 public function showCustomMenu($aItems)
1602 {
1603 $aShowItems = array();
1604
1605 foreach ($aItems as $oItem) {
1606 if ($oItem->object == 'post' || $oItem->object == 'page') {
1607 $oObject = $this->getPost($oItem->object_id);
1608
1609 if ($oObject !== null) {
1610 $oPost = $this->_getPost($oObject);
1611
1612 if ($oPost !== null) {
1613 if (isset($oPost->isLocked)) {
1614 $oItem->title = $oPost->post_title;
1615 }
1616
1617 $oItem->title .= $this->adminOutput($oItem->object, $oItem->object_id);
1618 $aShowItems[] = $oItem;
1619 }
1620 }
1621 } elseif ($oItem->object == 'category') {
1622 $oObject = $this->getCategory($oItem->object_id);
1623 $oCategory = $this->_getTerm('category', $oObject);
1624
1625 if ($oCategory !== null && !$oCategory->isEmpty) {
1626 $oItem->title .= $this->adminOutput($oItem->object, $oItem->object_id);
1627 $aShowItems[] = $oItem;
1628 }
1629 } else {
1630 $aShowItems[] = $oItem;
1631 }
1632 }
1633
1634 return $aShowItems;
1635 }
1636
1637 /**
1638 * The function for the comments_array filter.
1639 *
1640 * @param array $aComments The comments.
1641 *
1642 * @return array
1643 */
1644 public function showComment($aComments = array())
1645 {
1646 $aShowComments = array();
1647 $aUamOptions = $this->getAdminOptions();
1648 $oUamAccessHandler = $this->getAccessHandler();
1649
1650 foreach ($aComments as $oComment) {
1651 $oPost = $this->getPost($oComment->comment_post_ID);
1652 $sPostType = $oPost->post_type;
1653
1654 if ($aUamOptions['hide_'.$sPostType.'_comment'] == 'true'
1655 || $aUamOptions['hide_'.$sPostType] == 'true'
1656 || $this->atAdminPanel()
1657 ) {
1658 if ($oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1659 $aShowComments[] = $oComment;
1660 }
1661 } else {
1662 if (!$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1663 $oComment->comment_content = $aUamOptions[$sPostType.'_comment_content'];
1664 }
1665
1666 $aShowComments[] = $oComment;
1667 }
1668 }
1669
1670 $aComments = $aShowComments;
1671
1672 return $aComments;
1673 }
1674
1675 /**
1676 * The function for the get_pages filter.
1677 *
1678 * @param array $aPages The pages.
1679 *
1680 * @return array
1681 */
1682 public function showPage($aPages = array())
1683 {
1684 $aShowPages = array();
1685 $aUamOptions = $this->getAdminOptions();
1686 $oUamAccessHandler = $this->getAccessHandler();
1687
1688 foreach ($aPages as $oPage) {
1689 if ($aUamOptions['hide_page'] == 'true'
1690 || $this->atAdminPanel()
1691 ) {
1692 if ($oUamAccessHandler->checkObjectAccess($oPage->post_type, $oPage->ID)) {
1693 $oPage->post_title .= $this->adminOutput(
1694 $oPage->post_type,
1695 $oPage->ID
1696 );
1697 $aShowPages[] = $oPage;
1698 }
1699 } else {
1700 if (!$oUamAccessHandler->checkObjectAccess($oPage->post_type, $oPage->ID)) {
1701 if ($aUamOptions['hide_page_title'] == 'true') {
1702 $oPage->post_title = $aUamOptions['page_title'];
1703 }
1704
1705 $oPage->post_content = $aUamOptions['page_content'];
1706 }
1707
1708 $oPage->post_title .= $this->adminOutput($oPage->post_type, $oPage->ID);
1709 $aShowPages[] = $oPage;
1710 }
1711 }
1712
1713 $aPages = $aShowPages;
1714
1715 return $aPages;
1716 }
1717
1718 /**
1719 * Modifies the content of the term by the given settings.
1720 *
1721 * @param string $sTermType The type of the term.
1722 * @param object $oTerm The current term.
1723 *
1724 * @return object|null
1725 */
1726 protected function _getTerm($sTermType, $oTerm)
1727 {
1728 $aUamOptions = $this->getAdminOptions();
1729 $oUamAccessHandler = $this->getAccessHandler();
1730
1731 $oTerm->isEmpty = false;
1732
1733 $oTerm->name .= $this->adminOutput('term', $oTerm->term_id);
1734
1735 if ($sTermType == 'post_tag'
1736 || ( $sTermType == 'category' || $sTermType == $oTerm->taxonomy)
1737 && $oUamAccessHandler->checkObjectAccess('category', $oTerm->term_id)
1738 ) {
1739 if ($this->atAdminPanel() == false
1740 && ($aUamOptions['hide_post'] == 'true'
1741 || $aUamOptions['hide_page'] == 'true')
1742 ) {
1743 $iTermRequest = $oTerm->term_id;
1744 $sTermRequestType = $sTermType;
1745
1746 if ($sTermType == 'post_tag') {
1747 $iTermRequest = $oTerm->slug;
1748 $sTermRequestType = 'tag';
1749 }
1750
1751 $aArgs = array(
1752 'numberposts' => - 1,
1753 $sTermRequestType => $iTermRequest
1754 );
1755
1756 $aTermPosts = get_posts($aArgs);
1757 $oTerm->count = count($aTermPosts);
1758
1759 if (isset($aTermPosts)) {
1760 foreach ($aTermPosts as $oPost) {
1761 if ($aUamOptions['hide_'.$oPost->post_type] == 'true'
1762 && !$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)
1763 ) {
1764 $oTerm->count--;
1765 }
1766 }
1767 }
1768
1769 //For post_tags
1770 if ($sTermType == 'post_tag' && $oTerm->count <= 0) {
1771 return null;
1772 }
1773
1774 //For categories
1775 if ($oTerm->count <= 0
1776 && $aUamOptions['hide_empty_categories'] == 'true'
1777 && ($oTerm->taxonomy == "term"
1778 || $oTerm->taxonomy == "category")
1779 ) {
1780 $oTerm->isEmpty = true;
1781 }
1782
1783 if ($aUamOptions['lock_recursive'] == 'false') {
1784 $oCurCategory = $oTerm;
1785
1786 while ($oCurCategory->parent != 0) {
1787 $oCurCategory = get_term($oCurCategory->parent, 'category');
1788
1789 if ($oUamAccessHandler->checkObjectAccess('term', $oCurCategory->term_id)) {
1790 $oTerm->parent = $oCurCategory->term_id;
1791 break;
1792 }
1793 }
1794 }
1795 }
1796
1797 return $oTerm;
1798 }
1799
1800 return null;
1801 }
1802
1803 /**
1804 * The function for the get_terms filter.
1805 *
1806 * @param array $aTerms The terms.
1807 * @param array $aTaxonomies The given arguments.
1808 * @param array $aArgs The given arguments.
1809 * @param WP_Term_Query $oTermQuery The term query.
1810 *
1811 * @return array
1812 */
1813 public function showTerms($aTerms = array(), $aTaxonomies = array(), $aArgs = array(), $oTermQuery = null)
1814 {
1815 $aShowTerms = array();
1816
1817 foreach ($aTerms as $oTerm) {
1818 if (!is_object($oTerm)) {
1819 return $aTerms;
1820 }
1821
1822 if (isset($this->_aProcessedTerms[$oTerm->term_id])) {
1823 if ($this->_aProcessedTerms[$oTerm->term_id] !== false) {
1824 $aShowTerms[$oTerm->term_id] = $this->_aProcessedTerms[$oTerm->term_id];
1825 }
1826
1827 continue;
1828 } else {
1829 $this->_aProcessedTerms[$oTerm->term_id] = false;
1830 }
1831
1832 if ($oTerm->taxonomy == 'category' || $oTerm->taxonomy == 'post_tag') {
1833 $oTerm = $this->_getTerm($oTerm->taxonomy, $oTerm);
1834 }
1835
1836 if ($oTerm !== null && (!isset($oTerm->isEmpty) || !$oTerm->isEmpty)) {
1837 $aShowTerms[$oTerm->term_id] = $oTerm;
1838 $this->_aProcessedTerms[$oTerm->term_id] = $oTerm;
1839 }
1840 }
1841
1842 foreach ($aTerms as $sKey => $oTerm) {
1843 if (!isset($aShowTerms[$oTerm->term_id])) {
1844 unset($aTerms[$sKey]);
1845 }
1846 }
1847
1848 return $aTerms;
1849 }
1850
1851 /**
1852 * The function for the get_previous_post_where and
1853 * the get_next_post_where filter.
1854 *
1855 * @param string $sSql The current sql string.
1856 *
1857 * @return string
1858 */
1859 public function showNextPreviousPost($sSql)
1860 {
1861 $oUamAccessHandler = $this->getAccessHandler();
1862 $aUamOptions = $this->getAdminOptions();
1863
1864 if ($aUamOptions['hide_post'] == 'true') {
1865 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1866
1867 if (count($aExcludedPosts) > 0) {
1868 $sExcludedPosts = implode(",", $aExcludedPosts);
1869 $sSql.= " AND p.ID NOT IN($sExcludedPosts) ";
1870 }
1871 }
1872
1873 return $sSql;
1874 }
1875
1876 /**
1877 * Returns the admin hint.
1878 *
1879 * @param string $sObjectType The object type.
1880 * @param integer $iObjectId The object _iId we want to check.
1881 *
1882 * @return string
1883 */
1884 public function adminOutput($sObjectType, $iObjectId)
1885 {
1886 $sOutput = "";
1887
1888 if (!$this->atAdminPanel()) {
1889 $aUamOptions = $this->getAdminOptions();
1890
1891 if ($aUamOptions['blog_admin_hint'] == 'true') {
1892 $oCurrentUser = $this->getCurrentUser();
1893
1894 $oUserData = get_userdata($oCurrentUser->ID);
1895
1896 if (!isset($oUserData->user_level)) {
1897 return $sOutput;
1898 }
1899
1900 $oUamAccessHandler = $this->getAccessHandler();
1901
1902 if ($oUamAccessHandler->userIsAdmin($oCurrentUser->ID)
1903 && count($oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId)) > 0
1904 ) {
1905 $sOutput .= $aUamOptions['blog_admin_hint_text'];
1906 }
1907 }
1908 }
1909
1910 return $sOutput;
1911 }
1912
1913 /**
1914 * The function for the edit_post_link filter.
1915 *
1916 * @param string $sLink The edit link.
1917 * @param integer $iPostId The _iId of the post.
1918 *
1919 * @return string
1920 */
1921 public function showGroupMembership($sLink, $iPostId)
1922 {
1923 $oUamAccessHandler = $this->getAccessHandler();
1924 $aGroups = $oUamAccessHandler->getUserGroupsForObject('post', $iPostId);
1925
1926 if (count($aGroups) > 0) {
1927 $sLink .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
1928
1929 foreach ($aGroups as $oGroup) {
1930 $sLink .= htmlentities($oGroup->getGroupName()).', ';
1931 }
1932
1933 $sLink = rtrim($sLink, ', ');
1934 }
1935
1936 return $sLink;
1937 }
1938
1939 /**
1940 * Returns the login bar.
1941 *
1942 * @return string
1943 */
1944 public function getLoginBarHtml()
1945 {
1946 if (!is_user_logged_in()) {
1947 return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
1948 }
1949
1950 return '';
1951 }
1952
1953
1954 /*
1955 * Functions for the redirection and files.
1956 */
1957
1958 /**
1959 * Returns true if permalinks are active otherwise false.
1960 *
1961 * @return boolean
1962 */
1963 public function isPermalinksActive()
1964 {
1965 $sPermalinkStructure = $this->getWpOption('permalink_structure');
1966
1967 if (empty($sPermalinkStructure)) {
1968 return false;
1969 } else {
1970 return true;
1971 }
1972 }
1973
1974 /**
1975 * Redirects to a page or to content.
1976 *
1977 * @param string $sHeaders The headers which are given from wordpress.
1978 * @param object $oPageParams The params of the current page.
1979 *
1980 * @return string
1981 */
1982 public function redirect($sHeaders, $oPageParams)
1983 {
1984 $oUamOptions = $this->getAdminOptions();
1985
1986 if (isset($_GET['uamgetfile']) && isset($_GET['uamfiletype'])) {
1987 $sFileUrl = $_GET['uamgetfile'];
1988 $sFileType = $_GET['uamfiletype'];
1989 $this->getFile($sFileType, $sFileUrl);
1990 } elseif (!$this->atAdminPanel() && $oUamOptions['redirect'] !== 'false') {
1991 $oObject = null;
1992
1993 if (isset($oPageParams->query_vars['p'])) {
1994 $oObject = $this->getPost($oPageParams->query_vars['p']);
1995 $oObjectType = $oObject->post_type;
1996 $iObjectId = $oObject->ID;
1997 } elseif (isset($oPageParams->query_vars['page_id'])) {
1998 $oObject = $this->getPost($oPageParams->query_vars['page_id']);
1999 $oObjectType = $oObject->post_type;
2000 $iObjectId = $oObject->ID;
2001 } elseif (isset($oPageParams->query_vars['cat_id'])) {
2002 $oObject = $this->getCategory($oPageParams->query_vars['cat_id']);
2003 $oObjectType = 'category';
2004 $iObjectId = $oObject->term_id;
2005 } elseif (isset($oPageParams->query_vars['name'])) {
2006 global $wpdb;
2007
2008 $sQuery = $wpdb->prepare(
2009 "SELECT ID
2010 FROM {$wpdb->posts}
2011 WHERE post_name = %s
2012 AND post_type IN ('post', 'page')",
2013 $oPageParams->query_vars['name']
2014 );
2015
2016 $sObjectId = $wpdb->get_var($sQuery);
2017
2018 if ($sObjectId) {
2019 $oObject = get_post($sObjectId);
2020 }
2021
2022 if ($oObject !== null) {
2023 $oObjectType = $oObject->post_type;
2024 $iObjectId = $oObject->ID;
2025 }
2026 } elseif (isset($oPageParams->query_vars['pagename'])) {
2027 $oObject = get_page_by_path($oPageParams->query_vars['pagename']);
2028
2029 if ($oObject !== null) {
2030 $oObjectType = $oObject->post_type;
2031 $iObjectId = $oObject->ID;
2032 }
2033 }
2034
2035 if ($oObject === null || $oObject !== null && isset($oObjectType) && isset($iObjectId)
2036 && !$this->getAccessHandler()->checkObjectAccess($oObjectType, $iObjectId)
2037 ) {
2038 $this->redirectUser($oObject);
2039 }
2040 }
2041
2042 return $sHeaders;
2043 }
2044
2045 /**
2046 * Returns the current url.
2047 *
2048 * @return string
2049 */
2050 public function getCurrentUrl()
2051 {
2052 if (!isset($_SERVER['REQUEST_URI'])) {
2053 $sServerRequestUri = $_SERVER['PHP_SELF'];
2054 } else {
2055 $sServerRequestUri = $_SERVER['REQUEST_URI'];
2056 }
2057
2058 $sSecure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
2059 $aProtocols = explode("/", strtolower($_SERVER["SERVER_PROTOCOL"]));
2060 $sProtocol = $aProtocols[0].$sSecure;
2061 $sPort = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
2062
2063 return $sProtocol."://".$_SERVER['SERVER_NAME'].$sPort.$sServerRequestUri;
2064 }
2065
2066 /**
2067 * Redirects the user to his destination.
2068 *
2069 * @param object $oObject The current object we want to access.
2070 */
2071 public function redirectUser($oObject = null)
2072 {
2073 global $wp_query;
2074
2075 $blPostToShow = false;
2076 $aPosts = $wp_query->get_posts();
2077
2078 if ($oObject === null && isset($aPosts)) {
2079 foreach ($aPosts as $oPost) {
2080 if ($this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID)) {
2081 $blPostToShow = true;
2082 break;
2083 }
2084 }
2085 }
2086
2087 if (!$blPostToShow) {
2088 $aUamOptions = $this->getAdminOptions();
2089 $sPermalink = null;
2090
2091 if ($aUamOptions['redirect'] == 'custom_page') {
2092 $oPost = $this->getPost($aUamOptions['redirect_custom_page']);
2093 $sUrl = $oPost->guid;
2094 $sPermalink = get_page_link($oPost);
2095 } elseif ($aUamOptions['redirect'] == 'custom_url') {
2096 $sUrl = $aUamOptions['redirect_custom_url'];
2097 } else {
2098 $sUrl = home_url('/');
2099 }
2100
2101 if ($sUrl != $this->getCurrentUrl() && $sPermalink != $this->getCurrentUrl()) {
2102 wp_redirect($sUrl);
2103 exit;
2104 }
2105 }
2106 }
2107
2108 /**
2109 * Delivers the content of the requested file.
2110 *
2111 * @param string $sObjectType The type of the requested file.
2112 * @param string $sObjectUrl The file url.
2113 *
2114 * @return null
2115 */
2116 public function getFile($sObjectType, $sObjectUrl)
2117 {
2118 $oObject = $this->_getFileSettingsByType($sObjectType, $sObjectUrl);
2119
2120 if ($oObject === null) {
2121 return null;
2122 }
2123
2124 $sFile = null;
2125
2126 if ($this->getAccessHandler()->checkObjectAccess($oObject->type, $oObject->id)) {
2127 $sFile = $oObject->file;
2128 } elseif ($oObject->isImage) {
2129 $sFile = UAM_REALPATH.'gfx/noAccessPic.png';
2130 } else {
2131 wp_die(TXT_UAM_NO_RIGHTS);
2132 }
2133
2134 //Deliver content
2135 if (file_exists($sFile)) {
2136 $sFileName = basename($sFile);
2137
2138 /*
2139 * This only for compatibility
2140 * mime_content_type has been deprecated as the PECL extension file info
2141 * provides the same functionality (and more) in a much cleaner way.
2142 */
2143 $sFileExt = strtolower(array_pop(explode('.', $sFileName)));
2144 $aMimeTypes = $this->_getMimeTypes();
2145
2146 if (function_exists('finfo_open')) {
2147 $sFileInfo = finfo_open(FILEINFO_MIME);
2148 $sFileMimeType = finfo_file($sFileInfo, $sFile);
2149 finfo_close($sFileInfo);
2150 } elseif (function_exists('mime_content_type')) {
2151 $sFileMimeType = mime_content_type($sFile);
2152 } elseif (isset($aMimeTypes[$sFileExt])) {
2153 $sFileMimeType = $aMimeTypes[$sFileExt];
2154 } else {
2155 $sFileMimeType = 'application/octet-stream';
2156 }
2157
2158 header('Content-Description: File Transfer');
2159 header('Content-Type: '.$sFileMimeType);
2160
2161 if (!$oObject->isImage) {
2162 $sBaseName = str_replace(' ', '_', basename($sFile));
2163 header('Content-Disposition: attachment; filename="'.$sBaseName.'"');
2164 }
2165
2166 header('Content-Transfer-Encoding: binary');
2167 header('Content-Length: '.filesize($sFile));
2168
2169 $aUamOptions = $this->getAdminOptions();
2170
2171 if ($aUamOptions['download_type'] == 'fopen'
2172 && !$oObject->isImage
2173 ) {
2174 $oHandler = fopen($sFile, 'r');
2175
2176 //TODO find better solution (prevent '\n' / '0A')
2177 ob_clean();
2178 flush();
2179
2180 while (!feof($oHandler)) {
2181 if (!ini_get('safe_mode')) {
2182 set_time_limit(30);
2183 }
2184
2185 echo fread($oHandler, 1024);
2186 }
2187
2188 exit;
2189 } else {
2190 ob_clean();
2191 flush();
2192 readfile($sFile);
2193 exit;
2194 }
2195 } else {
2196 wp_die(TXT_UAM_FILE_NOT_FOUND_ERROR);
2197 return null;
2198 }
2199 }
2200
2201 /**
2202 * Returns the file object by the given type and url.
2203 *
2204 * @param string $sObjectType The type of the requested file.
2205 * @param string $sObjectUrl The file url.
2206 *
2207 * @return object|null
2208 */
2209 protected function _getFileSettingsByType($sObjectType, $sObjectUrl)
2210 {
2211 $oObject = null;
2212
2213 if ($sObjectType == 'attachment') {
2214 $aUploadDir = wp_upload_dir();
2215 $sUploadDir = str_replace(ABSPATH, '/', $aUploadDir['basedir']);
2216 $sRegex = '/.*'.str_replace('/', '\/', $sUploadDir).'\//i';
2217 $sCleanObjectUrl = preg_replace($sRegex, '', $sObjectUrl);
2218 $sUploadUrl = str_replace('/files', $sUploadDir, $aUploadDir['baseurl']);
2219 $sObjectUrl = $sUploadUrl.'/'.ltrim($sCleanObjectUrl, '/');
2220 $oPost = $this->getPost($this->getPostIdByUrl($sObjectUrl));
2221
2222 if ($oPost !== null
2223 && $oPost->post_type == 'attachment'
2224 ) {
2225 $oObject = new stdClass();
2226 $oObject->id = $oPost->ID;
2227 $oObject->isImage = wp_attachment_is_image($oPost->ID);
2228 $oObject->type = $sObjectType;
2229 $sMultiPath = str_replace('/files', $sUploadDir, $aUploadDir['baseurl']);
2230 $oObject->file = $aUploadDir['basedir'].str_replace($sMultiPath, '', $sObjectUrl );
2231 }
2232 } else {
2233 $aPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
2234
2235 if (isset($aPlObject) && isset($aPlObject['getFileObject'])) {
2236 $oObject = $aPlObject['reference']->{$aPlObject['getFileObject']}($sObjectUrl);
2237 }
2238 }
2239
2240 return $oObject;
2241 }
2242
2243 /**
2244 * Returns the url for a locked file.
2245 *
2246 * @param string $sUrl The base url.
2247 * @param integer $iId The _iId of the file.
2248 *
2249 * @return string
2250 */
2251 public function getFileUrl($sUrl, $iId)
2252 {
2253 $aUamOptions = $this->getAdminOptions();
2254
2255 if (!$this->isPermalinksActive() && $aUamOptions['lock_file'] == 'true') {
2256 $oPost = &$this->getPost($iId);
2257 $aType = explode("/", $oPost->post_mime_type);
2258 $sType = $aType[1];
2259 $aFileTypes = explode(',', $aUamOptions['locked_file_types']);
2260
2261 if ($aUamOptions['lock_file_types'] == 'all' || in_array($sType, $aFileTypes)) {
2262 $sUrl = home_url('/').'?uamfiletype=attachment&uamgetfile='.$sUrl;
2263 }
2264 }
2265
2266 return $sUrl;
2267 }
2268
2269 /**
2270 * Returns the post by the given url.
2271 *
2272 * @param string $sUrl The url of the post(attachment).
2273 *
2274 * @return object The post.
2275 */
2276 public function getPostIdByUrl($sUrl)
2277 {
2278 if (isset($this->_aPostUrls[$sUrl])) {
2279 return $this->_aPostUrls[$sUrl];
2280 }
2281
2282 $this->_aPostUrls[$sUrl] = null;
2283
2284 //Filter edit string
2285 $sNewUrl = preg_split("/-e[0-9]{1,}/", $sUrl);
2286
2287 if (count($sNewUrl) == 2) {
2288 $sNewUrl = $sNewUrl[0].$sNewUrl[1];
2289 } else {
2290 $sNewUrl = $sNewUrl[0];
2291 }
2292
2293 //Filter size
2294 $sNewUrl = preg_split("/-[0-9]{1,}x[0-9]{1,}/", $sNewUrl);
2295
2296 if (count($sNewUrl) == 2) {
2297 $sNewUrl = $sNewUrl[0].$sNewUrl[1];
2298 } else {
2299 $sNewUrl = $sNewUrl[0];
2300 }
2301
2302 /**
2303 * @var wpdb $wpdb
2304 */
2305 global $wpdb;
2306
2307 $sSql = $wpdb->prepare(
2308 "SELECT ID
2309 FROM ".$wpdb->prefix."posts
2310 WHERE guid = %s
2311 LIMIT 1",
2312 $sNewUrl
2313 );
2314
2315 $oDbPost = $wpdb->get_row($sSql);
2316
2317 if ($oDbPost) {
2318 $this->_aPostUrls[$sUrl] = $oDbPost->ID;
2319 }
2320
2321 return $this->_aPostUrls[$sUrl];
2322 }
2323
2324 /**
2325 * Caches the urls for the post for a later lookup.
2326 *
2327 * @param string $sUrl The url of the post.
2328 * @param object $oPost The post object.
2329 *
2330 * @return null
2331 */
2332 public function cachePostLinks($sUrl, $oPost)
2333 {
2334 $this->_aPostUrls[$sUrl] = $oPost->ID;
2335 return $sUrl;
2336 }
2337
2338 /**
2339 * Filter for Yoast SEO Plugin
2340 *
2341 * Hides the url from the site map if the user is not allowed
2342 *
2343 * @param string $url
2344 * @param string $type
2345 * @param object $object
2346 * @return false|string
2347 */
2348 function wp_seo_url($url, $type, $object)
2349 {
2350 $uaManager = new UserAccessManager();
2351 $handler = $uaManager->getAccessHandler();
2352 if($handler->checkObjectAccess($type, $object->ID)){
2353 return $url;
2354 }
2355 return false;
2356 }
2357 }