PluginProbe
User Access Manager / 1.2.7.4
User Access Manager v1.2.7.4
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.4, at class/UserAccessManager.class.php

2,353 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.4";
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 $_aTerms = 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 $sKey = $sTermType.'|'.$oTerm->term_id;
1729
1730 if (!isset($this->_aTerms[$sKey])) {
1731 $this->_aTerms[$sKey] = false;
1732 $aUamOptions = $this->getAdminOptions();
1733 $oUamAccessHandler = $this->getAccessHandler();
1734
1735 $oTerm->isEmpty = false;
1736
1737 $oTerm->name .= $this->adminOutput('term', $oTerm->term_id);
1738
1739 if ($sTermType == 'post_tag'
1740 || ( $sTermType == 'category' || $sTermType == $oTerm->taxonomy)
1741 && $oUamAccessHandler->checkObjectAccess('category', $oTerm->term_id)
1742 ) {
1743 if ($this->atAdminPanel() == false
1744 && ($aUamOptions['hide_post'] == 'true'
1745 || $aUamOptions['hide_page'] == 'true')
1746 ) {
1747 $iTermRequest = $oTerm->term_id;
1748 $sTermRequestType = $sTermType;
1749
1750 if ($sTermType == 'post_tag') {
1751 $iTermRequest = $oTerm->slug;
1752 $sTermRequestType = 'tag';
1753 }
1754
1755 $aArgs = array(
1756 'numberposts' => - 1,
1757 $sTermRequestType => $iTermRequest
1758 );
1759
1760 $aTermPosts = get_posts($aArgs);
1761 $oTerm->count = count($aTermPosts);
1762
1763 if (isset($aTermPosts)) {
1764 foreach ($aTermPosts as $oPost) {
1765 if ($aUamOptions['hide_'.$oPost->post_type] == 'true'
1766 && !$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)
1767 ) {
1768 $oTerm->count--;
1769 }
1770 }
1771 }
1772
1773 //For post_tags
1774 if ($sTermType == 'post_tag' && $oTerm->count <= 0) {
1775 return null;
1776 }
1777
1778 //For categories
1779 if ($oTerm->count <= 0
1780 && $aUamOptions['hide_empty_categories'] == 'true'
1781 && ($oTerm->taxonomy == "term"
1782 || $oTerm->taxonomy == "category")
1783 ) {
1784 $oTerm->isEmpty = true;
1785 }
1786
1787 if ($aUamOptions['lock_recursive'] == 'false') {
1788 $oCurCategory = $oTerm;
1789
1790 while ($oCurCategory->parent != 0) {
1791 $oCurCategory = get_term($oCurCategory->parent, 'category');
1792
1793 if ($oUamAccessHandler->checkObjectAccess('term', $oCurCategory->term_id)) {
1794 $oTerm->parent = $oCurCategory->term_id;
1795 break;
1796 }
1797 }
1798 }
1799 }
1800
1801 $this->_aTerms[$sKey] = $oTerm;
1802 }
1803 }
1804
1805 return ($this->_aTerms[$sKey] !== false) ? $this->_aTerms[$sKey] : null;
1806 }
1807
1808 /**
1809 * The function for the get_terms filter.
1810 *
1811 * @param array $aTerms The terms.
1812 * @param array $aTaxonomies The given arguments.
1813 * @param array $aArgs The given arguments.
1814 * @param WP_Term_Query $oTermQuery The term query.
1815 *
1816 * @return array
1817 */
1818 public function showTerms($aTerms = array(), $aTaxonomies = array(), $aArgs = array(), $oTermQuery = null)
1819 {
1820 $aShowTerms = array();
1821
1822 remove_filter('get_terms', array($this, 'showTerms'), 10);
1823 foreach ($aTerms as $oTerm) {
1824 if (!is_object($oTerm)) {
1825 return $aTerms;
1826 }
1827
1828 if ($oTerm->taxonomy == 'category' || $oTerm->taxonomy == 'post_tag') {
1829 $oTerm = $this->_getTerm($oTerm->taxonomy, $oTerm);
1830 }
1831
1832 if ($oTerm !== null && (!isset($oTerm->isEmpty) || !$oTerm->isEmpty)) {
1833 $aShowTerms[$oTerm->term_id] = $oTerm;
1834 }
1835 }
1836 add_filter('get_terms', array($this, 'showTerms'), 10, 2);
1837
1838 foreach ($aTerms as $sKey => $oTerm) {
1839 if (!isset($aShowTerms[$oTerm->term_id])) {
1840 unset($aTerms[$sKey]);
1841 }
1842 }
1843
1844 return $aTerms;
1845 }
1846
1847 /**
1848 * The function for the get_previous_post_where and
1849 * the get_next_post_where filter.
1850 *
1851 * @param string $sSql The current sql string.
1852 *
1853 * @return string
1854 */
1855 public function showNextPreviousPost($sSql)
1856 {
1857 $oUamAccessHandler = $this->getAccessHandler();
1858 $aUamOptions = $this->getAdminOptions();
1859
1860 if ($aUamOptions['hide_post'] == 'true') {
1861 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1862
1863 if (count($aExcludedPosts) > 0) {
1864 $sExcludedPosts = implode(",", $aExcludedPosts);
1865 $sSql.= " AND p.ID NOT IN($sExcludedPosts) ";
1866 }
1867 }
1868
1869 return $sSql;
1870 }
1871
1872 /**
1873 * Returns the admin hint.
1874 *
1875 * @param string $sObjectType The object type.
1876 * @param integer $iObjectId The object _iId we want to check.
1877 *
1878 * @return string
1879 */
1880 public function adminOutput($sObjectType, $iObjectId)
1881 {
1882 $sOutput = "";
1883
1884 if (!$this->atAdminPanel()) {
1885 $aUamOptions = $this->getAdminOptions();
1886
1887 if ($aUamOptions['blog_admin_hint'] == 'true') {
1888 $oCurrentUser = $this->getCurrentUser();
1889
1890 $oUserData = get_userdata($oCurrentUser->ID);
1891
1892 if (!isset($oUserData->user_level)) {
1893 return $sOutput;
1894 }
1895
1896 $oUamAccessHandler = $this->getAccessHandler();
1897
1898 if ($oUamAccessHandler->userIsAdmin($oCurrentUser->ID)
1899 && count($oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId)) > 0
1900 ) {
1901 $sOutput .= $aUamOptions['blog_admin_hint_text'];
1902 }
1903 }
1904 }
1905
1906 return $sOutput;
1907 }
1908
1909 /**
1910 * The function for the edit_post_link filter.
1911 *
1912 * @param string $sLink The edit link.
1913 * @param integer $iPostId The _iId of the post.
1914 *
1915 * @return string
1916 */
1917 public function showGroupMembership($sLink, $iPostId)
1918 {
1919 $oUamAccessHandler = $this->getAccessHandler();
1920 $aGroups = $oUamAccessHandler->getUserGroupsForObject('post', $iPostId);
1921
1922 if (count($aGroups) > 0) {
1923 $sLink .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
1924
1925 foreach ($aGroups as $oGroup) {
1926 $sLink .= htmlentities($oGroup->getGroupName()).', ';
1927 }
1928
1929 $sLink = rtrim($sLink, ', ');
1930 }
1931
1932 return $sLink;
1933 }
1934
1935 /**
1936 * Returns the login bar.
1937 *
1938 * @return string
1939 */
1940 public function getLoginBarHtml()
1941 {
1942 if (!is_user_logged_in()) {
1943 return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
1944 }
1945
1946 return '';
1947 }
1948
1949
1950 /*
1951 * Functions for the redirection and files.
1952 */
1953
1954 /**
1955 * Returns true if permalinks are active otherwise false.
1956 *
1957 * @return boolean
1958 */
1959 public function isPermalinksActive()
1960 {
1961 $sPermalinkStructure = $this->getWpOption('permalink_structure');
1962
1963 if (empty($sPermalinkStructure)) {
1964 return false;
1965 } else {
1966 return true;
1967 }
1968 }
1969
1970 /**
1971 * Redirects to a page or to content.
1972 *
1973 * @param string $sHeaders The headers which are given from wordpress.
1974 * @param object $oPageParams The params of the current page.
1975 *
1976 * @return string
1977 */
1978 public function redirect($sHeaders, $oPageParams)
1979 {
1980 $oUamOptions = $this->getAdminOptions();
1981
1982 if (isset($_GET['uamgetfile']) && isset($_GET['uamfiletype'])) {
1983 $sFileUrl = $_GET['uamgetfile'];
1984 $sFileType = $_GET['uamfiletype'];
1985 $this->getFile($sFileType, $sFileUrl);
1986 } elseif (!$this->atAdminPanel() && $oUamOptions['redirect'] !== 'false') {
1987 $oObject = null;
1988
1989 if (isset($oPageParams->query_vars['p'])) {
1990 $oObject = $this->getPost($oPageParams->query_vars['p']);
1991 $oObjectType = $oObject->post_type;
1992 $iObjectId = $oObject->ID;
1993 } elseif (isset($oPageParams->query_vars['page_id'])) {
1994 $oObject = $this->getPost($oPageParams->query_vars['page_id']);
1995 $oObjectType = $oObject->post_type;
1996 $iObjectId = $oObject->ID;
1997 } elseif (isset($oPageParams->query_vars['cat_id'])) {
1998 $oObject = $this->getCategory($oPageParams->query_vars['cat_id']);
1999 $oObjectType = 'category';
2000 $iObjectId = $oObject->term_id;
2001 } elseif (isset($oPageParams->query_vars['name'])) {
2002 global $wpdb;
2003
2004 $sQuery = $wpdb->prepare(
2005 "SELECT ID
2006 FROM {$wpdb->posts}
2007 WHERE post_name = %s
2008 AND post_type IN ('post', 'page')",
2009 $oPageParams->query_vars['name']
2010 );
2011
2012 $sObjectId = $wpdb->get_var($sQuery);
2013
2014 if ($sObjectId) {
2015 $oObject = get_post($sObjectId);
2016 }
2017
2018 if ($oObject !== null) {
2019 $oObjectType = $oObject->post_type;
2020 $iObjectId = $oObject->ID;
2021 }
2022 } elseif (isset($oPageParams->query_vars['pagename'])) {
2023 $oObject = get_page_by_path($oPageParams->query_vars['pagename']);
2024
2025 if ($oObject !== null) {
2026 $oObjectType = $oObject->post_type;
2027 $iObjectId = $oObject->ID;
2028 }
2029 }
2030
2031 if ($oObject === null || $oObject !== null && isset($oObjectType) && isset($iObjectId)
2032 && !$this->getAccessHandler()->checkObjectAccess($oObjectType, $iObjectId)
2033 ) {
2034 $this->redirectUser($oObject);
2035 }
2036 }
2037
2038 return $sHeaders;
2039 }
2040
2041 /**
2042 * Returns the current url.
2043 *
2044 * @return string
2045 */
2046 public function getCurrentUrl()
2047 {
2048 if (!isset($_SERVER['REQUEST_URI'])) {
2049 $sServerRequestUri = $_SERVER['PHP_SELF'];
2050 } else {
2051 $sServerRequestUri = $_SERVER['REQUEST_URI'];
2052 }
2053
2054 $sSecure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
2055 $aProtocols = explode("/", strtolower($_SERVER["SERVER_PROTOCOL"]));
2056 $sProtocol = $aProtocols[0].$sSecure;
2057 $sPort = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
2058
2059 return $sProtocol."://".$_SERVER['SERVER_NAME'].$sPort.$sServerRequestUri;
2060 }
2061
2062 /**
2063 * Redirects the user to his destination.
2064 *
2065 * @param object $oObject The current object we want to access.
2066 */
2067 public function redirectUser($oObject = null)
2068 {
2069 global $wp_query;
2070
2071 $blPostToShow = false;
2072 $aPosts = $wp_query->get_posts();
2073
2074 if ($oObject === null && isset($aPosts)) {
2075 foreach ($aPosts as $oPost) {
2076 if ($this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID)) {
2077 $blPostToShow = true;
2078 break;
2079 }
2080 }
2081 }
2082
2083 if (!$blPostToShow) {
2084 $aUamOptions = $this->getAdminOptions();
2085 $sPermalink = null;
2086
2087 if ($aUamOptions['redirect'] == 'custom_page') {
2088 $oPost = $this->getPost($aUamOptions['redirect_custom_page']);
2089 $sUrl = $oPost->guid;
2090 $sPermalink = get_page_link($oPost);
2091 } elseif ($aUamOptions['redirect'] == 'custom_url') {
2092 $sUrl = $aUamOptions['redirect_custom_url'];
2093 } else {
2094 $sUrl = home_url('/');
2095 }
2096
2097 if ($sUrl != $this->getCurrentUrl() && $sPermalink != $this->getCurrentUrl()) {
2098 wp_redirect($sUrl);
2099 exit;
2100 }
2101 }
2102 }
2103
2104 /**
2105 * Delivers the content of the requested file.
2106 *
2107 * @param string $sObjectType The type of the requested file.
2108 * @param string $sObjectUrl The file url.
2109 *
2110 * @return null
2111 */
2112 public function getFile($sObjectType, $sObjectUrl)
2113 {
2114 $oObject = $this->_getFileSettingsByType($sObjectType, $sObjectUrl);
2115
2116 if ($oObject === null) {
2117 return null;
2118 }
2119
2120 $sFile = null;
2121
2122 if ($this->getAccessHandler()->checkObjectAccess($oObject->type, $oObject->id)) {
2123 $sFile = $oObject->file;
2124 } elseif ($oObject->isImage) {
2125 $sFile = UAM_REALPATH.'gfx/noAccessPic.png';
2126 } else {
2127 wp_die(TXT_UAM_NO_RIGHTS);
2128 }
2129
2130 //Deliver content
2131 if (file_exists($sFile)) {
2132 $sFileName = basename($sFile);
2133
2134 /*
2135 * This only for compatibility
2136 * mime_content_type has been deprecated as the PECL extension file info
2137 * provides the same functionality (and more) in a much cleaner way.
2138 */
2139 $sFileExt = strtolower(array_pop(explode('.', $sFileName)));
2140 $aMimeTypes = $this->_getMimeTypes();
2141
2142 if (function_exists('finfo_open')) {
2143 $sFileInfo = finfo_open(FILEINFO_MIME);
2144 $sFileMimeType = finfo_file($sFileInfo, $sFile);
2145 finfo_close($sFileInfo);
2146 } elseif (function_exists('mime_content_type')) {
2147 $sFileMimeType = mime_content_type($sFile);
2148 } elseif (isset($aMimeTypes[$sFileExt])) {
2149 $sFileMimeType = $aMimeTypes[$sFileExt];
2150 } else {
2151 $sFileMimeType = 'application/octet-stream';
2152 }
2153
2154 header('Content-Description: File Transfer');
2155 header('Content-Type: '.$sFileMimeType);
2156
2157 if (!$oObject->isImage) {
2158 $sBaseName = str_replace(' ', '_', basename($sFile));
2159 header('Content-Disposition: attachment; filename="'.$sBaseName.'"');
2160 }
2161
2162 header('Content-Transfer-Encoding: binary');
2163 header('Content-Length: '.filesize($sFile));
2164
2165 $aUamOptions = $this->getAdminOptions();
2166
2167 if ($aUamOptions['download_type'] == 'fopen'
2168 && !$oObject->isImage
2169 ) {
2170 $oHandler = fopen($sFile, 'r');
2171
2172 //TODO find better solution (prevent '\n' / '0A')
2173 ob_clean();
2174 flush();
2175
2176 while (!feof($oHandler)) {
2177 if (!ini_get('safe_mode')) {
2178 set_time_limit(30);
2179 }
2180
2181 echo fread($oHandler, 1024);
2182 }
2183
2184 exit;
2185 } else {
2186 ob_clean();
2187 flush();
2188 readfile($sFile);
2189 exit;
2190 }
2191 } else {
2192 wp_die(TXT_UAM_FILE_NOT_FOUND_ERROR);
2193 return null;
2194 }
2195 }
2196
2197 /**
2198 * Returns the file object by the given type and url.
2199 *
2200 * @param string $sObjectType The type of the requested file.
2201 * @param string $sObjectUrl The file url.
2202 *
2203 * @return object|null
2204 */
2205 protected function _getFileSettingsByType($sObjectType, $sObjectUrl)
2206 {
2207 $oObject = null;
2208
2209 if ($sObjectType == 'attachment') {
2210 $aUploadDir = wp_upload_dir();
2211 $sUploadDir = str_replace(ABSPATH, '/', $aUploadDir['basedir']);
2212 $sRegex = '/.*'.str_replace('/', '\/', $sUploadDir).'\//i';
2213 $sCleanObjectUrl = preg_replace($sRegex, '', $sObjectUrl);
2214 $sUploadUrl = str_replace('/files', $sUploadDir, $aUploadDir['baseurl']);
2215 $sObjectUrl = $sUploadUrl.'/'.ltrim($sCleanObjectUrl, '/');
2216 $oPost = $this->getPost($this->getPostIdByUrl($sObjectUrl));
2217
2218 if ($oPost !== null
2219 && $oPost->post_type == 'attachment'
2220 ) {
2221 $oObject = new stdClass();
2222 $oObject->id = $oPost->ID;
2223 $oObject->isImage = wp_attachment_is_image($oPost->ID);
2224 $oObject->type = $sObjectType;
2225 $sMultiPath = str_replace('/files', $sUploadDir, $aUploadDir['baseurl']);
2226 $oObject->file = $aUploadDir['basedir'].str_replace($sMultiPath, '', $sObjectUrl );
2227 }
2228 } else {
2229 $aPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
2230
2231 if (isset($aPlObject) && isset($aPlObject['getFileObject'])) {
2232 $oObject = $aPlObject['reference']->{$aPlObject['getFileObject']}($sObjectUrl);
2233 }
2234 }
2235
2236 return $oObject;
2237 }
2238
2239 /**
2240 * Returns the url for a locked file.
2241 *
2242 * @param string $sUrl The base url.
2243 * @param integer $iId The _iId of the file.
2244 *
2245 * @return string
2246 */
2247 public function getFileUrl($sUrl, $iId)
2248 {
2249 $aUamOptions = $this->getAdminOptions();
2250
2251 if (!$this->isPermalinksActive() && $aUamOptions['lock_file'] == 'true') {
2252 $oPost = &$this->getPost($iId);
2253 $aType = explode("/", $oPost->post_mime_type);
2254 $sType = $aType[1];
2255 $aFileTypes = explode(',', $aUamOptions['locked_file_types']);
2256
2257 if ($aUamOptions['lock_file_types'] == 'all' || in_array($sType, $aFileTypes)) {
2258 $sUrl = home_url('/').'?uamfiletype=attachment&uamgetfile='.$sUrl;
2259 }
2260 }
2261
2262 return $sUrl;
2263 }
2264
2265 /**
2266 * Returns the post by the given url.
2267 *
2268 * @param string $sUrl The url of the post(attachment).
2269 *
2270 * @return object The post.
2271 */
2272 public function getPostIdByUrl($sUrl)
2273 {
2274 if (isset($this->_aPostUrls[$sUrl])) {
2275 return $this->_aPostUrls[$sUrl];
2276 }
2277
2278 $this->_aPostUrls[$sUrl] = null;
2279
2280 //Filter edit string
2281 $sNewUrl = preg_split("/-e[0-9]{1,}/", $sUrl);
2282
2283 if (count($sNewUrl) == 2) {
2284 $sNewUrl = $sNewUrl[0].$sNewUrl[1];
2285 } else {
2286 $sNewUrl = $sNewUrl[0];
2287 }
2288
2289 //Filter size
2290 $sNewUrl = preg_split("/-[0-9]{1,}x[0-9]{1,}/", $sNewUrl);
2291
2292 if (count($sNewUrl) == 2) {
2293 $sNewUrl = $sNewUrl[0].$sNewUrl[1];
2294 } else {
2295 $sNewUrl = $sNewUrl[0];
2296 }
2297
2298 /**
2299 * @var wpdb $wpdb
2300 */
2301 global $wpdb;
2302
2303 $sSql = $wpdb->prepare(
2304 "SELECT ID
2305 FROM ".$wpdb->prefix."posts
2306 WHERE guid = %s
2307 LIMIT 1",
2308 $sNewUrl
2309 );
2310
2311 $oDbPost = $wpdb->get_row($sSql);
2312
2313 if ($oDbPost) {
2314 $this->_aPostUrls[$sUrl] = $oDbPost->ID;
2315 }
2316
2317 return $this->_aPostUrls[$sUrl];
2318 }
2319
2320 /**
2321 * Caches the urls for the post for a later lookup.
2322 *
2323 * @param string $sUrl The url of the post.
2324 * @param object $oPost The post object.
2325 *
2326 * @return null
2327 */
2328 public function cachePostLinks($sUrl, $oPost)
2329 {
2330 $this->_aPostUrls[$sUrl] = $oPost->ID;
2331 return $sUrl;
2332 }
2333
2334 /**
2335 * Filter for Yoast SEO Plugin
2336 *
2337 * Hides the url from the site map if the user is not allowed
2338 *
2339 * @param string $url
2340 * @param string $type
2341 * @param object $object
2342 * @return false|string
2343 */
2344 function wp_seo_url($url, $type, $object)
2345 {
2346 $uaManager = new UserAccessManager();
2347 $handler = $uaManager->getAccessHandler();
2348 if($handler->checkObjectAccess($type, $object->ID)){
2349 return $url;
2350 }
2351 return false;
2352 }
2353 }