PluginProbe
User Access Manager / 1.2.6.0
User Access Manager v1.2.6.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.6.0, at class/UserAccessManager.class.php

2,310 lines 67.4 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.6.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 '{$sDbAccessGroupToObject}'
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 'UserAccessManagerFunctions',
927 UAM_URLPATH . 'js/functions.js',
928 array('jquery')
929 );
930 }
931
932 /**
933 * Prints the admin page.
934 *
935 * @return null
936 */
937 public function printAdminPage()
938 {
939 if (isset($_GET['page'])) {
940 $sAdminPage = $_GET['page'];
941
942 if ($sAdminPage == 'uam_settings') {
943 include UAM_REALPATH."tpl/adminSettings.php";
944 } elseif ($sAdminPage == 'uam_usergroup') {
945 include UAM_REALPATH."tpl/adminGroup.php";
946 } elseif ($sAdminPage == 'uam_setup') {
947 include UAM_REALPATH."tpl/adminSetup.php";
948 } elseif ($sAdminPage == 'uam_about') {
949 include UAM_REALPATH."tpl/about.php";
950 }
951 }
952 }
953
954 /**
955 * Shows the error if the user has no rights to edit the content.
956 *
957 * @return null
958 */
959 public function noRightsToEditContent()
960 {
961 $blNoRights = false;
962
963 if (isset($_GET['post']) && is_numeric($_GET['post'])) {
964 $oPost = $this->getPost($_GET['post']);
965 $blNoRights = !$this->getAccessHandler()->checkObjectAccess( $oPost->post_type, $oPost->ID );
966 }
967
968 if (isset($_GET['attachment_id']) && is_numeric($_GET['attachment_id']) && !$blNoRights) {
969 $oPost = $this->getPost($_GET['attachment_id']);
970 $blNoRights = !$this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID);
971 }
972
973 if (isset($_GET['tag_ID']) && is_numeric($_GET['tag_ID']) && !$blNoRights) {
974 $blNoRights = !$this->getAccessHandler()->checkObjectAccess('category', $_GET['tag_ID']);
975 }
976
977 if ($blNoRights) {
978 wp_die(TXT_UAM_NO_RIGHTS);
979 }
980 }
981
982 /**
983 * The function for the wp_dashboard_setup action.
984 * Removes widgets to which a user should not have access.
985 *
986 * @return null
987 */
988 public function setupAdminDashboard()
989 {
990 global $wp_meta_boxes;
991
992 if (!$this->getAccessHandler()->checkUserAccess('manage_user_groups')) {
993 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
994 }
995 }
996
997 /**
998 * The function for the update_option_permalink_structure action.
999 *
1000 * @return null
1001 */
1002 public function updatePermalink()
1003 {
1004 $this->createHtaccess();
1005 $this->createHtpasswd();
1006 }
1007
1008
1009 /*
1010 * Meta functions
1011 */
1012
1013 /**
1014 * Saves the object data to the database.
1015 *
1016 * @param string $sObjectType The object type.
1017 * @param integer $iObjectId The _iId of the object.
1018 * @param array $aUserGroups The new usergroups for the object.
1019 *
1020 * @return null
1021 */
1022 protected function _saveObjectData($sObjectType, $iObjectId, $aUserGroups = null)
1023 {
1024 $oUamAccessHandler = $this->getAccessHandler();
1025 $oUamOptions = $this->getAdminOptions();
1026 $aFormData = array();
1027
1028 if (isset($_POST['uam_update_groups'])) {
1029 $aFormData = $_POST;
1030 } elseif (isset($_GET['uam_update_groups'])) {
1031 $aFormData = $_GET;
1032 }
1033
1034 if (isset($aFormData['uam_update_groups'])
1035 && ($oUamAccessHandler->checkUserAccess('manage_user_groups')
1036 || $oUamOptions['authors_can_add_posts_to_groups'] == 'true')
1037 ) {
1038 if ($aUserGroups === null) {
1039 $aUserGroups = isset($aFormData['uam_usergroups']) ? $aFormData['uam_usergroups'] : array();
1040 }
1041
1042 $aAddUserGroups = array_flip($aUserGroups);
1043 $aRemoveUserGroups = $oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId);
1044 $aUamUserGroups = $oUamAccessHandler->getUserGroups();
1045 $blRemoveOldAssignments = true;
1046
1047 if (isset($aFormData['uam_bulk_type'])) {
1048 $sBulkType = $aFormData['uam_bulk_type'];
1049
1050 if ($sBulkType === 'add') {
1051 $blRemoveOldAssignments = false;
1052 } elseif ($sBulkType === 'remove') {
1053 $aRemoveUserGroups = $aAddUserGroups;
1054 $aAddUserGroups = array();
1055 }
1056 }
1057
1058 foreach ($aUamUserGroups as $sGroupId => $oUamUserGroup) {
1059 if (isset($aRemoveUserGroups[$sGroupId])) {
1060 $oUamUserGroup->removeObject($sObjectType, $iObjectId);
1061 }
1062
1063 if (isset($aAddUserGroups[$sGroupId])) {
1064 $oUamUserGroup->addObject($sObjectType, $iObjectId);
1065 }
1066
1067 $oUamUserGroup->save($blRemoveOldAssignments);
1068 }
1069 }
1070 }
1071
1072
1073 /*
1074 * Functions for the post actions.
1075 */
1076
1077 /**
1078 * The function for the manage_posts_columns and
1079 * the manage_pages_columns filter.
1080 *
1081 * @param array $aDefaults The table headers.
1082 *
1083 * @return array
1084 */
1085 public function addPostColumnsHeader($aDefaults)
1086 {
1087 $aDefaults['uam_access'] = __('Access', 'user-access-manager');
1088 return $aDefaults;
1089 }
1090
1091 /**
1092 * The function for the manage_users_custom_column action.
1093 *
1094 * @param string $sColumnName The column name.
1095 * @param integer $iId The _iId.
1096 *
1097 * @return string
1098 */
1099 public function addPostColumn($sColumnName, $iId)
1100 {
1101 if ($sColumnName == 'uam_access') {
1102 $oPost = $this->getPost($iId);
1103 echo $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $oPost->ID, $oPost->post_type);
1104 }
1105 }
1106
1107 /**
1108 * The function for the uma_post_access metabox.
1109 *
1110 * @param object $oPost The post.
1111 *
1112 * @return null;
1113 */
1114 public function editPostContent($oPost)
1115 {
1116 $iObjectId = $oPost->ID;
1117 include UAM_REALPATH.'tpl/postEditForm.php';
1118 }
1119
1120 public function addBulkAction($sColumnName)
1121 {
1122 if ($sColumnName == 'uam_access') {
1123 include UAM_REALPATH.'tpl/bulkEditForm.php';
1124 }
1125 }
1126
1127 /**
1128 * The function for the save_post action.
1129 *
1130 * @param mixed $mPostParam The post _iId or a array of a post.
1131 *
1132 * @return null
1133 */
1134 public function savePostData($mPostParam)
1135 {
1136 if (is_array($mPostParam)) {
1137 $oPost = $this->getPost($mPostParam['ID']);
1138 } else {
1139 $oPost = $this->getPost($mPostParam);
1140 }
1141
1142 $iPostId = $oPost->ID;
1143 $sPostType = $oPost->post_type;
1144
1145 if ($sPostType == 'revision') {
1146 $iPostId = $oPost->post_parent;
1147 $oParentPost = $this->getPost($iPostId);
1148 $sPostType = $oParentPost->post_type;
1149 }
1150
1151 $this->_saveObjectData($sPostType, $iPostId);
1152 }
1153
1154 /**
1155 * The function for the attachment_fields_to_save filter.
1156 * We have to use this because the attachment actions work
1157 * not in the way we need.
1158 *
1159 * @param object $oAttachment The attachment _iId.
1160 *
1161 * @return object
1162 */
1163 public function saveAttachmentData($oAttachment)
1164 {
1165 $this->savePostData($oAttachment['ID']);
1166
1167 return $oAttachment;
1168 }
1169
1170 /**
1171 * The function for the delete_post action.
1172 *
1173 * @param integer $iPostId The post _iId.
1174 *
1175 * @return null
1176 */
1177 public function removePostData($iPostId)
1178 {
1179 /**
1180 * @var wpdb $wpdb
1181 */
1182 global $wpdb;
1183 $oPost = $this->getPost($iPostId);
1184
1185 $wpdb->query(
1186 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1187 WHERE object_id = '".$iPostId."'
1188 AND object_type = '".$oPost->post_type."'"
1189 );
1190 }
1191
1192 /**
1193 * The function for the media_meta action.
1194 *
1195 * @param string $sMeta The meta.
1196 * @param object $oPost The post.
1197 *
1198 * @return string
1199 */
1200 public function showMediaFile($sMeta = '', $oPost = null)
1201 {
1202 $sContent = $sMeta;
1203 $sContent .= '</td></tr><tr>';
1204 $sContent .= '<th class="label">';
1205 $sContent .= '<label>'.TXT_UAM_SET_UP_USERGROUPS.'</label>';
1206 $sContent .= '</th>';
1207 $sContent .= '<td class="field">';
1208 $sContent .= $this->getIncludeContents(UAM_REALPATH.'tpl/postEditForm.php', $oPost->ID);
1209
1210 return $sContent;
1211 }
1212
1213
1214 /*
1215 * Functions for the user actions.
1216 */
1217
1218 /**
1219 * The function for the manage_users_columns filter.
1220 *
1221 * @param array $aDefaults The table headers.
1222 *
1223 * @return array
1224 */
1225 public function addUserColumnsHeader($aDefaults)
1226 {
1227 $aDefaults['uam_access'] = __('uam user groups');
1228 return $aDefaults;
1229 }
1230
1231 /**
1232 * The function for the manage_users_custom_column action.
1233 *
1234 * @param string $sReturn The normal return value.
1235 * @param string $sColumnName The column name.
1236 * @param integer $iId The _iId.
1237 *
1238 * @return string|null
1239 */
1240 public function addUserColumn($sReturn, $sColumnName, $iId)
1241 {
1242 if ($sColumnName == 'uam_access') {
1243 return $this->getIncludeContents(UAM_REALPATH.'tpl/userColumn.php', $iId, 'user');
1244 }
1245
1246 return $sReturn;
1247 }
1248
1249 /**
1250 * The function for the edit_user_profile action.
1251 *
1252 * @return null
1253 */
1254 public function showUserProfile()
1255 {
1256 echo $this->getIncludeContents(UAM_REALPATH.'tpl/userProfileEditForm.php');
1257 }
1258
1259 /**
1260 * The function for the profile_update action.
1261 *
1262 * @param integer $iUserId The user _iId.
1263 *
1264 * @return null
1265 */
1266 public function saveUserData($iUserId)
1267 {
1268 $this->_saveObjectData('user', $iUserId);
1269 }
1270
1271 /**
1272 * The function for the delete_user action.
1273 *
1274 * @param integer $iUserId The user _iId.
1275 *
1276 * @return null
1277 */
1278 public function removeUserData($iUserId)
1279 {
1280 /**
1281 * @var wpdb $wpdb
1282 */
1283 global $wpdb;
1284
1285 $wpdb->query(
1286 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1287 WHERE object_id = ".$iUserId."
1288 AND object_type = 'user'"
1289 );
1290 }
1291
1292
1293 /*
1294 * Functions for the category actions.
1295 */
1296
1297 /**
1298 * The function for the manage_categories_columns filter.
1299 *
1300 * @param array $aDefaults The table headers.
1301 *
1302 * @return array
1303 */
1304 public function addCategoryColumnsHeader($aDefaults)
1305 {
1306 $aDefaults['uam_access'] = __('Access', 'user-access-manager');
1307 return $aDefaults;
1308 }
1309
1310 /**
1311 * The function for the manage_categories_custom_column action.
1312 *
1313 * @param string $sEmpty An empty string from wordpress? What the hell?!?
1314 * @param string $sColumnName The column name.
1315 * @param integer $iId The _iId.
1316 *
1317 * @return string|null
1318 */
1319 public function addCategoryColumn($sEmpty, $sColumnName, $iId)
1320 {
1321 if ($sColumnName == 'uam_access') {
1322 return $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $iId, 'category');
1323 }
1324
1325 return null;
1326 }
1327
1328 /**
1329 * The function for the edit_category_form action.
1330 *
1331 * @param object $oCategory The category.
1332 *
1333 * @return null
1334 */
1335 public function showCategoryEditForm($oCategory)
1336 {
1337 include UAM_REALPATH.'tpl/categoryEditForm.php';
1338 }
1339
1340 /**
1341 * The function for the edit_category action.
1342 *
1343 * @param integer $iCategoryId The category _iId.
1344 *
1345 * @return null
1346 */
1347 public function saveCategoryData($iCategoryId)
1348 {
1349 $this->_saveObjectData('category', $iCategoryId);
1350 }
1351
1352 /**
1353 * The function for the delete_category action.
1354 *
1355 * @param integer $iCategoryId The _iId of the category.
1356 *
1357 * @return null
1358 */
1359 public function removeCategoryData($iCategoryId)
1360 {
1361 /**
1362 * @var wpdb $wpdb
1363 */
1364 global $wpdb;
1365
1366 $wpdb->query(
1367 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1368 WHERE object_id = ".$iCategoryId."
1369 AND object_type = 'category'"
1370 );
1371 }
1372
1373
1374 /*
1375 * Functions for the pluggable object actions.
1376 */
1377
1378 /**
1379 * The function for the pluggable save action.
1380 *
1381 * @param string $sObjectType The name of the pluggable object.
1382 * @param integer $iObjectId The pluggable object _iId.
1383 * @param array $aUserGroups The user groups for the object.
1384 *
1385 * @return null
1386 */
1387 public function savePlObjectData($sObjectType, $iObjectId, $aUserGroups = null)
1388 {
1389 $this->_saveObjectData($sObjectType, $iObjectId, $aUserGroups);
1390 }
1391
1392 /**
1393 * The function for the pluggable remove action.
1394 *
1395 * @param string $sObjectName The name of the pluggable object.
1396 * @param integer $iObjectId The pluggable object _iId.
1397 *
1398 * @return null
1399 */
1400 public function removePlObjectData($sObjectName, $iObjectId)
1401 {
1402 /**
1403 * @var wpdb $wpdb
1404 */
1405 global $wpdb;
1406
1407 $wpdb->query(
1408 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1409 WHERE object_id = ".$iObjectId."
1410 AND object_type = ".$sObjectName
1411 );
1412 }
1413
1414 /**
1415 * Returns the group selection form for pluggable _aObjects.
1416 *
1417 * @param string $sObjectType The object type.
1418 * @param integer $iObjectId The _iId of the object.
1419 * @param string $aGroupsFormName The name of the form which contains the groups.
1420 *
1421 * @return string;
1422 */
1423 public function showPlGroupSelectionForm($sObjectType, $iObjectId, $aGroupsFormName = null)
1424 {
1425 $sFileName = UAM_REALPATH.'tpl/groupSelectionForm.php';
1426 $aUamUserGroups = $this->getAccessHandler()->getUserGroups();
1427 $aUserGroupsForObject = $this->getAccessHandler()->getUserGroupsForObject($sObjectType, $iObjectId);
1428
1429 if (is_file($sFileName)) {
1430 ob_start();
1431 include $sFileName;
1432 $sContents = ob_get_contents();
1433 ob_end_clean();
1434
1435 return $sContents;
1436 }
1437
1438 return '';
1439 }
1440
1441 /**
1442 * Returns the column for a pluggable object.
1443 *
1444 * @param string $sObjectType The object type.
1445 * @param integer $iObjectId The object _iId.
1446 *
1447 * @return string
1448 */
1449 public function getPlColumn($sObjectType, $iObjectId)
1450 {
1451 return $this->getIncludeContents(UAM_REALPATH.'tpl/objectColumn.php', $iObjectId, $sObjectType);
1452 }
1453
1454
1455 /*
1456 * Functions for the blog content.
1457 */
1458
1459 /**
1460 * Manipulates the wordpress query object to filter content.
1461 *
1462 * @param object $oWpQuery The wordpress query object.
1463 *
1464 * @return null
1465 */
1466 public function parseQuery($oWpQuery)
1467 {
1468 $aUamOptions = $this->getAdminOptions();
1469
1470 if ($aUamOptions['hide_post'] == 'true') {
1471 $oUamAccessHandler = $this->getAccessHandler();
1472 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1473
1474 if (count($aExcludedPosts) > 0) {
1475 $oWpQuery->query_vars['post__not_in'] = array_merge(
1476 $oWpQuery->query_vars['post__not_in'],
1477 $aExcludedPosts
1478 );
1479 }
1480 }
1481 }
1482
1483 /**
1484 * Modifies the content of the post by the given settings.
1485 *
1486 * @param object $oPost The current post.
1487 *
1488 * @return object|null
1489 */
1490 protected function _getPost($oPost)
1491 {
1492 $aUamOptions = $this->getAdminOptions();
1493 $oUamAccessHandler = $this->getAccessHandler();
1494
1495 $sPostType = $oPost->post_type;
1496
1497 if ($this->getAccessHandler()->isPostableType($sPostType) && $sPostType != 'post' && $sPostType != 'page') {
1498 $sPostType = 'post';
1499 } elseif ($sPostType != 'post' && $sPostType != 'page') {
1500 return $oPost;
1501 }
1502
1503 if ($aUamOptions['hide_'.$sPostType] == 'true' || $this->atAdminPanel()) {
1504 if ($oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1505 $oPost->post_title .= $this->adminOutput($oPost->post_type, $oPost->ID);
1506 return $oPost;
1507 }
1508 } else {
1509 if (!$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1510 $oPost->isLocked = true;
1511
1512 $sUamPostContent = $aUamOptions[$sPostType.'_content'];
1513 $sUamPostContent = str_replace("[LOGIN_FORM]", $this->getLoginBarHtml(), $sUamPostContent);
1514
1515 if ($aUamOptions['hide_'.$sPostType.'_title'] == 'true') {
1516 $oPost->post_title = $aUamOptions[$sPostType.'_title'];
1517 }
1518
1519 if ($aUamOptions[$sPostType.'_comments_locked'] == 'false') {
1520 $oPost->comment_status = 'close';
1521 }
1522
1523 if ($aUamOptions['show_post_content_before_more'] == 'true'
1524 && $sPostType == "post"
1525 && preg_match('/<!--more(.*?)?-->/', $oPost->post_content, $aMatches)
1526 ) {
1527 $oPost->post_content = explode($aMatches[0], $oPost->post_content, 2);
1528 $sUamPostContent = $oPost->post_content[0] . " " . $sUamPostContent;
1529 }
1530
1531 $oPost->post_content = $sUamPostContent;
1532 }
1533
1534 $oPost->post_title .= $this->adminOutput($oPost->post_type, $oPost->ID);
1535
1536 return $oPost;
1537 }
1538
1539 return null;
1540 }
1541
1542 /**
1543 * The function for the the_posts filter.
1544 *
1545 * @param array $aPosts The posts.
1546 *
1547 * @return array
1548 */
1549 public function showPost($aPosts = array())
1550 {
1551 $aShowPosts = array();
1552 $aUamOptions = $this->getAdminOptions();
1553
1554 if (!is_feed() || ($aUamOptions['protect_feed'] == 'true' && is_feed())) {
1555 foreach ($aPosts as $iPostId) {
1556 if ($iPostId !== null) {
1557 $oPost = $this->_getPost($iPostId);
1558
1559 if ($oPost !== null) {
1560 $aShowPosts[] = $oPost;
1561 }
1562 }
1563 }
1564
1565 $aPosts = $aShowPosts;
1566 }
1567
1568 return $aPosts;
1569 }
1570
1571 /**
1572 * The function for the posts_where_paged filter.
1573 *
1574 * @param string $sSql The where sql statement.
1575 *
1576 * @return string
1577 */
1578 public function showPostSql($sSql)
1579 {
1580 $oUamAccessHandler = $this->getAccessHandler();
1581 $aUamOptions = $this->getAdminOptions();
1582
1583 if ($aUamOptions['hide_post'] == 'true') {
1584 global $wpdb;
1585 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1586
1587 if (count($aExcludedPosts) > 0) {
1588 $sExcludedPostsStr = implode(",", $aExcludedPosts);
1589 $sSql .= " AND $wpdb->posts.ID NOT IN($sExcludedPostsStr) ";
1590 }
1591 }
1592
1593 return $sSql;
1594 }
1595
1596 /**
1597 * The function for the wp_get_nav_menu_items filter.
1598 *
1599 * @param array $aItems The menu item.
1600 *
1601 * @return array
1602 */
1603 public function showCustomMenu($aItems)
1604 {
1605 $aShowItems = array();
1606
1607 foreach ($aItems as $oItem) {
1608 if ($oItem->object == 'post' || $oItem->object == 'page') {
1609 $oObject = $this->getPost($oItem->object_id);
1610
1611 if ($oObject !== null) {
1612 $oPost = $this->_getPost($oObject);
1613
1614 if ($oPost !== null) {
1615 if (isset($oPost->isLocked)) {
1616 $oItem->title = $oPost->post_title;
1617 }
1618
1619 $oItem->title .= $this->adminOutput($oItem->object, $oItem->object_id);
1620 $aShowItems[] = $oItem;
1621 }
1622 }
1623 } elseif ($oItem->object == 'category') {
1624 $oObject = $this->getCategory($oItem->object_id);
1625 $oCategory = $this->_getTerm('category', $oObject);
1626
1627 if ($oCategory !== null && !$oCategory->isEmpty) {
1628 $oItem->title .= $this->adminOutput($oItem->object, $oItem->object_id);
1629 $aShowItems[] = $oItem;
1630 }
1631 } else {
1632 $aShowItems[] = $oItem;
1633 }
1634 }
1635
1636 return $aShowItems;
1637 }
1638
1639 /**
1640 * The function for the comments_array filter.
1641 *
1642 * @param array $aComments The comments.
1643 *
1644 * @return array
1645 */
1646 public function showComment($aComments = array())
1647 {
1648 $aShowComments = array();
1649 $aUamOptions = $this->getAdminOptions();
1650 $oUamAccessHandler = $this->getAccessHandler();
1651
1652 foreach ($aComments as $oComment) {
1653 $oPost = $this->getPost($oComment->comment_post_ID);
1654 $sPostType = $oPost->post_type;
1655
1656 if ($aUamOptions['hide_'.$sPostType.'_comment'] == 'true'
1657 || $aUamOptions['hide_'.$sPostType] == 'true'
1658 || $this->atAdminPanel()
1659 ) {
1660 if ($oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1661 $aShowComments[] = $oComment;
1662 }
1663 } else {
1664 if (!$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)) {
1665 $oComment->comment_content = $aUamOptions[$sPostType.'_comment_content'];
1666 }
1667
1668 $aShowComments[] = $oComment;
1669 }
1670 }
1671
1672 $aComments = $aShowComments;
1673
1674 return $aComments;
1675 }
1676
1677 /**
1678 * The function for the get_pages filter.
1679 *
1680 * @param array $aPages The pages.
1681 *
1682 * @return array
1683 */
1684 public function showPage($aPages = array())
1685 {
1686 $aShowPages = array();
1687 $aUamOptions = $this->getAdminOptions();
1688 $oUamAccessHandler = $this->getAccessHandler();
1689
1690 foreach ($aPages as $oPage) {
1691 if ($aUamOptions['hide_page'] == 'true'
1692 || $this->atAdminPanel()
1693 ) {
1694 if ($oUamAccessHandler->checkObjectAccess($oPage->post_type, $oPage->ID)) {
1695 $oPage->post_title .= $this->adminOutput(
1696 $oPage->post_type,
1697 $oPage->ID
1698 );
1699 $aShowPages[] = $oPage;
1700 }
1701 } else {
1702 if (!$oUamAccessHandler->checkObjectAccess($oPage->post_type, $oPage->ID)) {
1703 if ($aUamOptions['hide_page_title'] == 'true') {
1704 $oPage->post_title = $aUamOptions['page_title'];
1705 }
1706
1707 $oPage->post_content = $aUamOptions['page_content'];
1708 }
1709
1710 $oPage->post_title .= $this->adminOutput($oPage->post_type, $oPage->ID);
1711 $aShowPages[] = $oPage;
1712 }
1713 }
1714
1715 $aPages = $aShowPages;
1716
1717 return $aPages;
1718 }
1719
1720 /**
1721 * Modifies the content of the term by the given settings.
1722 *
1723 * @param string $sTermType The type of the term.
1724 * @param object $oTerm The current term.
1725 *
1726 * @return object|null
1727 */
1728 protected function _getTerm($sTermType, $oTerm)
1729 {
1730 $aUamOptions = $this->getAdminOptions();
1731 $oUamAccessHandler = $this->getAccessHandler();
1732
1733 $oTerm->isEmpty = false;
1734
1735 $oTerm->name .= $this->adminOutput('term', $oTerm->term_id);
1736
1737 if ($sTermType == 'post_tag'
1738 || ( $sTermType == 'category' || $sTermType == $oTerm->taxonomy)
1739 && $oUamAccessHandler->checkObjectAccess('category', $oTerm->term_id)
1740 ) {
1741 if ($this->atAdminPanel() == false
1742 && ($aUamOptions['hide_post'] == 'true'
1743 || $aUamOptions['hide_page'] == 'true')
1744 ) {
1745 $iTermRequest = $oTerm->term_id;
1746 $sTermRequestType = $sTermType;
1747
1748 if ($sTermType == 'post_tag') {
1749 $iTermRequest = $oTerm->slug;
1750 $sTermRequestType = 'tag';
1751 }
1752
1753 $aArgs = array(
1754 'numberposts' => - 1,
1755 $sTermRequestType => $iTermRequest
1756 );
1757
1758 $aTermPosts = get_posts($aArgs);
1759 $oTerm->count = count($aTermPosts);
1760
1761 if (isset($aTermPosts)) {
1762 foreach ($aTermPosts as $oPost) {
1763 if ($aUamOptions['hide_'.$oPost->post_type] == 'true'
1764 && !$oUamAccessHandler->checkObjectAccess($oPost->post_type, $oPost->ID)
1765 ) {
1766 $oTerm->count--;
1767 }
1768 }
1769 }
1770
1771 //For post_tags
1772 if ($sTermType == 'post_tag' && $oTerm->count <= 0) {
1773 return null;
1774 }
1775
1776 //For categories
1777 if ($oTerm->count <= 0
1778 && $aUamOptions['hide_empty_categories'] == 'true'
1779 && ($oTerm->taxonomy == "term"
1780 || $oTerm->taxonomy == "category")
1781 ) {
1782 $oTerm->isEmpty = true;
1783 }
1784
1785 if ($aUamOptions['lock_recursive'] == 'false') {
1786 $oCurCategory = $oTerm;
1787
1788 while ($oCurCategory->parent != 0) {
1789 $oCurCategory = get_term($oCurCategory->parent, 'category');
1790
1791 if ($oUamAccessHandler->checkObjectAccess('term', $oCurCategory->term_id)) {
1792 $oTerm->parent = $oCurCategory->term_id;
1793 break;
1794 }
1795 }
1796 }
1797 }
1798
1799 return $oTerm;
1800 }
1801
1802 return null;
1803 }
1804
1805 /**
1806 * The function for the get_terms filter.
1807 *
1808 * @param array $aTerms The terms.
1809 * @param array $aArgs The given arguments.
1810 *
1811 * @return array
1812 */
1813 public function showTerms($aTerms = array(), $aArgs = array())
1814 {
1815 $aShowTerms = array();
1816
1817 foreach ($aTerms as $oTerm) {
1818 if (!is_object($oTerm)) {
1819 return $aTerms;
1820 }
1821
1822 if ($oTerm->taxonomy == 'category' || $oTerm->taxonomy == 'post_tag') {
1823 $oTerm = $this->_getTerm($oTerm->taxonomy, $oTerm);
1824 }
1825
1826 if ($oTerm !== null && (!isset($oTerm->isEmpty) || !$oTerm->isEmpty)) {
1827 $aShowTerms[$oTerm->term_id] = $oTerm;
1828 }
1829 }
1830
1831 foreach ($aTerms as $sKey => $oTerm) {
1832 if (!isset($aShowTerms[$oTerm->term_id])) {
1833 unset($aTerms[$sKey]);
1834 }
1835 }
1836
1837 return $aTerms;
1838 }
1839
1840 /**
1841 * The function for the get_previous_post_where and
1842 * the get_next_post_where filter.
1843 *
1844 * @param string $sSql The current sql string.
1845 *
1846 * @return string
1847 */
1848 public function showNextPreviousPost($sSql)
1849 {
1850 $oUamAccessHandler = $this->getAccessHandler();
1851 $aUamOptions = $this->getAdminOptions();
1852
1853 if ($aUamOptions['hide_post'] == 'true') {
1854 $aExcludedPosts = $oUamAccessHandler->getExcludedPosts();
1855
1856 if (count($aExcludedPosts) > 0) {
1857 $sExcludedPosts = implode(",", $aExcludedPosts);
1858 $sSql.= " AND p.ID NOT IN($sExcludedPosts) ";
1859 }
1860 }
1861
1862 return $sSql;
1863 }
1864
1865 /**
1866 * Returns the admin hint.
1867 *
1868 * @param string $sObjectType The object type.
1869 * @param integer $iObjectId The object _iId we want to check.
1870 *
1871 * @return string
1872 */
1873 public function adminOutput($sObjectType, $iObjectId)
1874 {
1875 $sOutput = "";
1876
1877 if (!$this->atAdminPanel()) {
1878 $aUamOptions = $this->getAdminOptions();
1879
1880 if ($aUamOptions['blog_admin_hint'] == 'true') {
1881 $oCurrentUser = $this->getCurrentUser();
1882
1883 $oUserData = get_userdata($oCurrentUser->ID);
1884
1885 if (!isset($oUserData->user_level)) {
1886 return $sOutput;
1887 }
1888
1889 $oUamAccessHandler = $this->getAccessHandler();
1890
1891 if ($oUamAccessHandler->userIsAdmin($oCurrentUser->ID)
1892 && count($oUamAccessHandler->getUserGroupsForObject($sObjectType, $iObjectId)) > 0
1893 ) {
1894 $sOutput .= $aUamOptions['blog_admin_hint_text'];
1895 }
1896 }
1897 }
1898
1899 return $sOutput;
1900 }
1901
1902 /**
1903 * The function for the edit_post_link filter.
1904 *
1905 * @param string $sLink The edit link.
1906 * @param integer $iPostId The _iId of the post.
1907 *
1908 * @return string
1909 */
1910 public function showGroupMembership($sLink, $iPostId)
1911 {
1912 $oUamAccessHandler = $this->getAccessHandler();
1913 $aGroups = $oUamAccessHandler->getUserGroupsForObject('post', $iPostId);
1914
1915 if (count($aGroups) > 0) {
1916 $sLink .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
1917
1918 foreach ($aGroups as $oGroup) {
1919 $sLink .= $oGroup->getGroupName().', ';
1920 }
1921
1922 $sLink = rtrim($sLink, ', ');
1923 }
1924
1925 return $sLink;
1926 }
1927
1928 /**
1929 * Returns the login bar.
1930 *
1931 * @return string
1932 */
1933 public function getLoginBarHtml()
1934 {
1935 if (!is_user_logged_in()) {
1936 return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
1937 }
1938
1939 return '';
1940 }
1941
1942
1943 /*
1944 * Functions for the redirection and files.
1945 */
1946
1947 /**
1948 * Returns true if permalinks are active otherwise false.
1949 *
1950 * @return boolean
1951 */
1952 public function isPermalinksActive()
1953 {
1954 $sPermalinkStructure = $this->getWpOption('permalink_structure');
1955
1956 if (empty($sPermalinkStructure)) {
1957 return false;
1958 } else {
1959 return true;
1960 }
1961 }
1962
1963 /**
1964 * Redirects to a page or to content.
1965 *
1966 * @param string $sHeaders The headers which are given from wordpress.
1967 * @param object $oPageParams The params of the current page.
1968 *
1969 * @return string
1970 */
1971 public function redirect($sHeaders, $oPageParams)
1972 {
1973 $oUamOptions = $this->getAdminOptions();
1974
1975 if (isset($_GET['uamgetfile']) && isset($_GET['uamfiletype'])) {
1976 $sFileUrl = $_GET['uamgetfile'];
1977 $sFileType = $_GET['uamfiletype'];
1978 $this->getFile($sFileType, $sFileUrl);
1979 } elseif (!$this->atAdminPanel() && $oUamOptions['redirect'] != 'false') {
1980 $oObject = null;
1981
1982 if (isset($oPageParams->query_vars['p'])) {
1983 $oObject = $this->getPost($oPageParams->query_vars['p']);
1984 $oObjectType = $oObject->post_type;
1985 $iObjectId = $oObject->ID;
1986 } elseif (isset($oPageParams->query_vars['page_id'])) {
1987 $oObject = $this->getPost($oPageParams->query_vars['page_id']);
1988 $oObjectType = $oObject->post_type;
1989 $iObjectId = $oObject->ID;
1990 } elseif (isset($oPageParams->query_vars['cat_id'])) {
1991 $oObject = $this->getCategory($oPageParams->query_vars['cat_id']);
1992 $oObjectType = 'category';
1993 $iObjectId = $oObject->term_id;
1994 } elseif (isset($oPageParams->query_vars['name'])) {
1995 $oObject = get_page_by_title($oPageParams->query_vars['name'], OBJECT, 'post');
1996
1997 if ($oObject !== null) {
1998 $oObjectType = $oObject->post_type;
1999 $iObjectId = $oObject->ID;
2000 }
2001 } elseif (isset($oPageParams->query_vars['pagename'])) {
2002 $oObject = get_page_by_title($oPageParams->query_vars['pagename']);
2003
2004 if ($oObject !== null) {
2005 $oObjectType = $oObject->post_type;
2006 $iObjectId = $oObject->ID;
2007 }
2008 }
2009
2010 if ($oObject === null || $oObject !== null && isset($oObjectType) && isset($iObjectId)
2011 && !$this->getAccessHandler()->checkObjectAccess($oObjectType, $iObjectId)
2012 ) {
2013 $this->redirectUser($oObject);
2014 }
2015 }
2016
2017 return $sHeaders;
2018 }
2019
2020 /**
2021 * Returns the current url.
2022 *
2023 * @return string
2024 */
2025 public function getCurrentUrl()
2026 {
2027 if (!isset($_SERVER['REQUEST_URI'])) {
2028 $sServerRequestUri = $_SERVER['PHP_SELF'];
2029 } else {
2030 $sServerRequestUri = $_SERVER['REQUEST_URI'];
2031 }
2032
2033 $sSecure = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
2034 $aProtocols = explode("/", strtolower($_SERVER["SERVER_PROTOCOL"]));
2035 $sProtocol = $aProtocols[0].$sSecure;
2036 $sPort = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
2037
2038 return $sProtocol."://".$_SERVER['SERVER_NAME'].$sPort.$sServerRequestUri;
2039 }
2040
2041 /**
2042 * Redirects the user to his destination.
2043 *
2044 * @param object $oObject The current object we want to access.
2045 *
2046 * @return null
2047 */
2048 public function redirectUser($oObject = null)
2049 {
2050 global $wp_query;
2051
2052 $blPostToShow = false;
2053 $aPosts = $wp_query->get_posts();
2054
2055 if ($oObject === null && isset($aPosts)) {
2056 foreach ($aPosts as $oPost) {
2057 if ($this->getAccessHandler()->checkObjectAccess($oPost->post_type, $oPost->ID)) {
2058 $blPostToShow = true;
2059 break;
2060 }
2061 }
2062 }
2063
2064 if (!$blPostToShow) {
2065 $aUamOptions = $this->getAdminOptions();
2066
2067 if ($aUamOptions['redirect'] == 'custom_page') {
2068 $oPost = $this->getPost($aUamOptions['redirect_custom_page']);
2069 $sUrl = $oPost->guid;
2070 } elseif ($aUamOptions['redirect'] == 'custom_url') {
2071 $sUrl = $aUamOptions['redirect_custom_url'];
2072 } else {
2073 $sUrl = home_url('/');
2074 }
2075
2076 if ($sUrl != $this->getCurrentUrl()) {
2077 wp_redirect($sUrl);
2078 exit;
2079 }
2080 }
2081 }
2082
2083 /**
2084 * Delivers the content of the requested file.
2085 *
2086 * @param string $sObjectType The type of the requested file.
2087 * @param string $sObjectUrl The file url.
2088 *
2089 * @return null
2090 */
2091 public function getFile($sObjectType, $sObjectUrl)
2092 {
2093 $oObject = $this->_getFileSettingsByType($sObjectType, $sObjectUrl);
2094
2095 if ($oObject === null) {
2096 return null;
2097 }
2098
2099 $sFile = null;
2100
2101 if ($this->getAccessHandler()->checkObjectAccess($oObject->type, $oObject->id)) {
2102 $sFile = $oObject->file;
2103 } elseif ($oObject->isImage) {
2104 $sFile = UAM_REALPATH.'gfx/noAccessPic.png';
2105 } else {
2106 wp_die(TXT_UAM_NO_RIGHTS);
2107 }
2108
2109 //Deliver content
2110 if (file_exists($sFile)) {
2111 $sFileName = basename($sFile);
2112
2113 /*
2114 * This only for compatibility
2115 * mime_content_type has been deprecated as the PECL extension file info
2116 * provides the same functionality (and more) in a much cleaner way.
2117 */
2118 $sFileExt = strtolower(array_pop(explode('.', $sFileName)));
2119 $aMimeTypes = $this->_getMimeTypes();
2120
2121 if (function_exists('finfo_open')) {
2122 $sFileInfo = finfo_open(FILEINFO_MIME);
2123 $sFileMimeType = finfo_file($sFileInfo, $sFile);
2124 finfo_close($sFileInfo);
2125 } elseif (function_exists('mime_content_type')) {
2126 $sFileMimeType = mime_content_type($sFile);
2127 } elseif (isset($aMimeTypes[$sFileExt])) {
2128 $sFileMimeType = $aMimeTypes[$sFileExt];
2129 } else {
2130 $sFileMimeType = 'application/octet-stream';
2131 }
2132
2133 header('Content-Description: File Transfer');
2134 header('Content-Type: '.$sFileMimeType);
2135
2136 if (!$oObject->isImage) {
2137 $sBaseName = str_replace(' ', '_', basename($sFile));
2138 header('Content-Disposition: attachment; filename="'.$sBaseName.'"');
2139 }
2140
2141 header('Content-Transfer-Encoding: binary');
2142 header('Content-Length: '.filesize($sFile));
2143
2144 $aUamOptions = $this->getAdminOptions();
2145
2146 if ($aUamOptions['download_type'] == 'fopen'
2147 && !$oObject->isImage
2148 ) {
2149 $oHandler = fopen($sFile, 'r');
2150
2151 //TODO find better solution (prevent '\n' / '0A')
2152 ob_clean();
2153 flush();
2154
2155 while (!feof($oHandler)) {
2156 if (!ini_get('safe_mode')) {
2157 set_time_limit(30);
2158 }
2159
2160 echo fread($oHandler, 1024);
2161 }
2162
2163 exit;
2164 } else {
2165 ob_clean();
2166 flush();
2167 readfile($sFile);
2168 exit;
2169 }
2170 } else {
2171 wp_die(TXT_UAM_FILE_NOT_FOUND_ERROR);
2172 }
2173 }
2174
2175 /**
2176 * Returns the file object by the given type and url.
2177 *
2178 * @param string $sObjectType The type of the requested file.
2179 * @param string $sObjectUrl The file url.
2180 *
2181 * @return object|null
2182 */
2183 protected function _getFileSettingsByType($sObjectType, $sObjectUrl)
2184 {
2185 $oObject = null;
2186
2187 if ($sObjectType == 'attachment') {
2188 $aUploadDir = wp_upload_dir();
2189
2190 $sMultiPath = str_replace(ABSPATH, '/', $aUploadDir['basedir']);
2191 $sMultiPath = str_replace('/files', $sMultiPath, $aUploadDir['baseurl']);
2192
2193 if ($this->isPermalinksActive()) {
2194 $sObjectUrl = $sMultiPath.'/'.$sObjectUrl;
2195 }
2196
2197 $oPost = $this->getPost($this->getPostIdByUrl($sObjectUrl));
2198
2199 if ($oPost !== null
2200 && $oPost->post_type == 'attachment'
2201 ) {
2202 $oObject = new stdClass();
2203 $oObject->id = $oPost->ID;
2204 $oObject->isImage = wp_attachment_is_image($oPost->ID);
2205 $oObject->type = $sObjectType;
2206 $oObject->file = $aUploadDir['basedir'].str_replace($sMultiPath, '', $sObjectUrl );
2207 }
2208 } else {
2209 $aPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
2210
2211 if (isset($aPlObject) && isset($aPlObject['getFileObject'])) {
2212 $oObject = $aPlObject['reference']->{$aPlObject['getFileObject']}($sObjectUrl);
2213 }
2214 }
2215
2216 return $oObject;
2217 }
2218
2219 /**
2220 * Returns the url for a locked file.
2221 *
2222 * @param string $sUrl The base url.
2223 * @param integer $iId The _iId of the file.
2224 *
2225 * @return string
2226 */
2227 public function getFileUrl($sUrl, $iId)
2228 {
2229 $aUamOptions = $this->getAdminOptions();
2230
2231 if (!$this->isPermalinksActive() && $aUamOptions['lock_file'] == 'true') {
2232 $oPost = &$this->getPost($iId);
2233 $aType = explode("/", $oPost->post_mime_type);
2234 $sType = $aType[1];
2235 $aFileTypes = explode(',', $aUamOptions['locked_file_types']);
2236
2237 if ($aUamOptions['lock_file_types'] == 'all' || in_array($sType, $aFileTypes)) {
2238 $sUrl = home_url('/').'?uamfiletype=attachment&uamgetfile='.$sUrl;
2239 }
2240 }
2241
2242 return $sUrl;
2243 }
2244
2245 /**
2246 * Returns the post by the given url.
2247 *
2248 * @param string $sUrl The url of the post(attachment).
2249 *
2250 * @return object The post.
2251 */
2252 public function getPostIdByUrl($sUrl)
2253 {
2254 if (isset($this->_aPostUrls[$sUrl])) {
2255 return $this->_aPostUrls[$sUrl];
2256 }
2257
2258 $this->_aPostUrls[$sUrl] = null;
2259
2260 //Filter edit string
2261 $sNewUrl = preg_split("/-e[0-9]{1,}/", $sUrl);
2262
2263 if (count($sNewUrl) == 2) {
2264 $sNewUrl = $sNewUrl[0].$sNewUrl[1];
2265 } else {
2266 $sNewUrl = $sNewUrl[0];
2267 }
2268
2269 //Filter size
2270 $sNewUrl = preg_split("/-[0-9]{1,}x[0-9]{1,}/", $sNewUrl);
2271
2272 if (count($sNewUrl) == 2) {
2273 $sNewUrl = $sNewUrl[0].$sNewUrl[1];
2274 } else {
2275 $sNewUrl = $sNewUrl[0];
2276 }
2277
2278 /**
2279 * @var wpdb $wpdb
2280 */
2281 global $wpdb;
2282
2283 $oDbPost = $wpdb->get_row(
2284 "SELECT ID
2285 FROM ".$wpdb->prefix."posts
2286 WHERE guid = '" . $sNewUrl . "'
2287 LIMIT 1"
2288 );
2289
2290 if ($oDbPost) {
2291 $this->_aPostUrls[$sUrl] = $oDbPost->ID;
2292 }
2293
2294 return $this->_aPostUrls[$sUrl];
2295 }
2296
2297 /**
2298 * Caches the urls for the post for a later lookup.
2299 *
2300 * @param string $sUrl The url of the post.
2301 * @param object $oPost The post object.
2302 *
2303 * @return null
2304 */
2305 public function cachePostLinks($sUrl, $oPost)
2306 {
2307 $this->_aPostUrls[$sUrl] = $oPost->ID;
2308 return $sUrl;
2309 }
2310 }