PluginProbe
User Access Manager / 1.2.4.3
User Access Manager v1.2.4.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.4.3, at class/UserAccessManager.class.php

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