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

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