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

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

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