PluginProbe
User Access Manager / 1.2.5.0
User Access Manager v1.2.5.0
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.5.0, at class/UserAccessManager.class.php

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