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

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