PluginProbe
User Access Manager / 1.2.4.1
User Access Manager v1.2.4.1
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.1, at class/UserAccessManager.class.php

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