PluginProbe
User Access Manager / 1.2.7.5
User Access Manager v1.2.7.5
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.php

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