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

1,676 lines 47.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.0;
33 protected $uamDbVersion = 1.1;
34 protected $adminOptions;
35 protected $accessHandler = null;
36
37 /**
38 * Consturctor
39 *
40 * @return null
41 */
42 function __construct()
43 {
44
45 }
46
47 /**
48 * Creates the needed tables at the database
49 *
50 * @return null;
51 */
52 function install()
53 {
54 global $wpdb;
55 $uamDbVersion = $this->uamDbVersion;
56
57 include_once ABSPATH . 'wp-admin/includes/upgrade.php';
58 $charset_collate = '';
59
60 if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
61 if (!empty($wpdb->charset)) {
62 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
63 }
64
65 if (!empty($wpdb->collate)) {
66 $charset_collate.= " COLLATE $wpdb->collate";
67 }
68 }
69
70 $dbUserGroup = $wpdb->get_var(
71 "SHOW TABLES
72 LIKE '" . DB_ACCESSGROUP . "'"
73 );
74
75 if ($dbUserGroup != DB_ACCESSGROUP) {
76 $sql = "CREATE TABLE " . DB_ACCESSGROUP . " (
77 ID int(11) NOT NULL auto_increment,
78 groupname tinytext NOT NULL,
79 groupdesc text NOT NULL,
80 read_access tinytext NOT NULL,
81 write_access tinytext NOT NULL,
82 ip_range mediumtext NULL,
83 PRIMARY KEY (ID)
84 ) $charset_collate;";
85 dbDelta($sql);
86 }
87
88 $dbUserGroupToPost = $wpdb->get_var(
89 "SHOW TABLES
90 LIKE '" . DB_ACCESSGROUP_TO_POST . "'"
91 );
92
93 if ($dbUserGroupToPost != DB_ACCESSGROUP_TO_POST) {
94 $sql = "CREATE TABLE " . DB_ACCESSGROUP_TO_POST . " (
95 post_id int(11) NOT NULL,
96 group_id int(11) NOT NULL,
97 PRIMARY KEY (post_id,group_id)
98 ) $charset_collate;";
99 dbDelta($sql);
100 }
101
102 $dbUserGroupToUser = $wpdb->get_var(
103 "SHOW TABLES
104 LIKE '" . DB_ACCESSGROUP_TO_USER . "'"
105 );
106
107 if ($dbUserGroupToUser != DB_ACCESSGROUP_TO_USER) {
108 $sql = "CREATE TABLE " . DB_ACCESSGROUP_TO_USER . " (
109 user_id int(11) NOT NULL,
110 group_id int(11) NOT NULL,
111 PRIMARY KEY (user_id,group_id)
112 ) $charset_collate;";
113 dbDelta($sql);
114 }
115
116 $dbUserGroupToCategory = $wpdb->get_var(
117 "SHOW TABLES
118 LIKE '" . DB_ACCESSGROUP_TO_CATEGORY . "'"
119 );
120
121 if ($dbUserGroupToCategory != DB_ACCESSGROUP_TO_CATEGORY) {
122 $sql = "CREATE TABLE " . DB_ACCESSGROUP_TO_CATEGORY . " (
123 category_id int(11) NOT NULL,
124 group_id int(11) NOT NULL,
125 PRIMARY KEY (category_id,group_id)
126 ) $charset_collate;";
127 dbDelta($sql);
128 }
129
130 $dbUserGroupToRole = $wpdb->get_var(
131 "SHOW TABLES
132 LIKE '" . DB_ACCESSGROUP_TO_ROLE . "'"
133 );
134
135 if ($dbUserGroupToRole != DB_ACCESSGROUP_TO_ROLE) {
136 $sql = "CREATE TABLE " . DB_ACCESSGROUP_TO_ROLE . " (
137 role_name varchar(255) NOT NULL,
138 group_id int(11) NOT NULL,
139 PRIMARY KEY (role_name,group_id)
140 ) $charset_collate;";
141 dbDelta($sql);
142 }
143
144 add_option("uam_db_version", $uamDbVersion);
145 }
146
147 /**
148 * Updates the database if an old version was installed.
149 *
150 * @return null;
151 */
152 function update()
153 {
154 global $wpdb;
155 $currentDbVersion = get_option("uam_db_version");
156
157 if (empty($currentDbVersion)) {
158 $this->install();
159 }
160
161 if (!get_option('uam_version')
162 || get_option('uam_version') < $this->uamVersion
163 ) {
164 update_option('uam_version', $this->uamVersion);
165
166 delete_option('allow_comments_locked');
167 }
168
169 $dbUserGroup = $wpdb->get_var(
170 "SHOW TABLES
171 LIKE '" . DB_ACCESSGROUP . "'"
172 );
173
174 if ($currentDbVersion != $this->uamDbVersion) {
175 if ($currentDbVersion == 1.0) {
176
177
178 if ($dbUserGroup == DB_ACCESSGROUP) {
179 $wpdb->query(
180 "ALTER TABLE " . DB_ACCESSGROUP . "
181 ADD read_access TINYTEXT NOT NULL DEFAULT '',
182 ADD write_access TINYTEXT NOT NULL DEFAULT '',
183 ADD ip_range MEDIUMTEXT NULL DEFAULT ''"
184 );
185
186 $wpdb->query(
187 "UPDATE " . DB_ACCESSGROUP . "
188 SET read_access = 'group',
189 write_access = 'group'"
190 );
191
192 update_option('uam_db_version', $this->uamDbVersion);
193 }
194 }
195 }
196
197 if ($dbUserGroup == DB_ACCESSGROUP) {
198 $dbIpRange = $wpdb->get_var(
199 "SHOW columns
200 FROM " . DB_ACCESSGROUP . "
201 LIKE 'ip_range'"
202 );
203
204 if ($dbIpRange != 'ip_range') {
205 $wpdb->query(
206 "ALTER TABLE " . DB_ACCESSGROUP . "
207 ADD ip_range MEDIUMTEXT NULL DEFAULT ''"
208 );
209 }
210 }
211 }
212
213 /**
214 * Clean up wordpress if the plugin will be uninstalled.
215 *
216 * @return null
217 */
218 function uninstall()
219 {
220 global $wpdb;
221 $wpdb->query(
222 "DROP TABLE " . DB_ACCESSGROUP . ",
223 " . DB_ACCESSGROUP_TO_POST . ",
224 " . DB_ACCESSGROUP_TO_USER . ",
225 " . DB_ACCESSGROUP_TO_CATEGORY . ",
226 " . DB_ACCESSGROUP_TO_ROLE
227 );
228
229 delete_option($this->adminOptionsName);
230 delete_option('uam_version');
231 delete_option('uam_db_version');
232 $this->deleteHtaccessFiles();
233 }
234
235 /**
236 * Remove the htaccess file if the plugin is deactivated.
237 *
238 * @return null
239 */
240 function deactivate()
241 {
242 $this->deleteHtaccessFiles();
243 }
244
245 /**
246 * Creates a htaccess file.
247 *
248 * @return null.
249 */
250 function createHtaccess()
251 {
252 // Make .htaccess file to protect data
253 // get url
254
255 $wud = wp_upload_dir();
256 if (empty($wud['error'])) {
257 $dir = $wud['basedir'] . "/";
258 $permaStruc = get_option('permalink_structure');
259
260 if (empty($permaStruc)) {
261 $areaname = "WP-Files";
262 $uamOptions = $this->getAdminOptions();
263
264 if ($uamOptions['lock_file_types'] == 'selected') {
265 $fileTypes = $uamOptions['locked_file_types'];
266 } elseif ($uamOptions['lock_file_types'] == 'not_selected') {
267 $fileTypes = $uamOptions['not_locked_file_types'];
268 }
269
270 if (isset($fileTypes)) {
271 $fileTypes = str_replace(",", "|", $fileTypes);
272 }
273
274 // make .htaccess and .htpasswd
275 $htaccessTxt = "";
276
277 if ($uamOptions['lock_file_types'] == 'selected') {
278 $htaccessTxt .= "<FilesMatch '\.(" . $fileTypes . ")'>\n";
279 } elseif ($uamOptions['lock_file_types'] == 'not_selected') {
280 $htaccessTxt .= "<FilesMatch '^\.(" . $fileTypes . ")'>\n";
281 }
282
283 $htaccessTxt .= "AuthType Basic" . "\n";
284 $htaccessTxt .= "AuthName \"" . $areaname . "\"" . "\n";
285 $htaccessTxt .= "AuthUserFile " . $dir . ".htpasswd" . "\n";
286 $htaccessTxt .= "require valid-user" . "\n";
287
288 if ($uamOptions['lock_file_types'] == 'selected'
289 || $uamOptions['lock_file_types'] == 'not_selected'
290 ) {
291 $htaccessTxt.= "</FilesMatch>\n";
292 }
293 } else {
294 $homeRoot = parse_url(home_url());
295 if (isset($homeRoot['path'])) {
296 $homeRoot = trailingslashit($homeRoot['path']);
297 } else {
298 $homeRoot = '/';
299 }
300
301 $htaccessTxt = "<IfModule mod_rewrite.c>\n";
302 $htaccessTxt .= "RewriteEngine On\n";
303 $htaccessTxt .= "RewriteBase ".$homeRoot."\n";
304 $htaccessTxt .= "RewriteRule ^index\.php$ - [L]\n";
305 $htaccessTxt .= "RewriteRule (.*) ".$homeRoot."index.php?getfile=$1 [L]\n";
306 $htaccessTxt .= "</IfModule>\n";
307 }
308
309 // save files
310 $htaccess = fopen($dir . ".htaccess", "w");
311 fwrite($htaccess, $htaccessTxt);
312 fclose($htaccess);
313 }
314 }
315
316 /**
317 * Creates a htpasswd file.
318 *
319 * @param boolean $createNew Force to create new file.
320 *
321 * @return null
322 */
323 function createHtpasswd($createNew = false)
324 {
325 global $current_user;
326 $uamOptions = $this->getAdminOptions();
327
328 // get url
329 $wud = wp_upload_dir();
330 if (empty($wud['error'])) {
331 $url = $wud['basedir'] . "/";
332 $curUserdata = get_userdata($current_user->ID);
333 $user = $curUserdata->user_login;
334
335 if (!file_exists($url . ".htpasswd") || $createNew) {
336 if ($uamOptions['file_pass_type'] == 'random') {
337 // create password
338 $array = array();
339 $length = 10;
340 $capitals = true;
341 $specialSigns = false;
342 if ($length < 8) {
343 $length = mt_rand(8, 20);
344 }
345
346 // numbers
347 for ($i = 48; $i < 58; $i++) {
348 $array[] = chr($i);
349 }
350
351 // small
352 for ($i = 97; $i < 122; $i++) {
353 $array[] = chr($i);
354 }
355
356 // capitals
357 if ($capitals) {
358 for ($i = 65; $i < 90; $i++) {
359 $array[] = chr($i);
360 }
361 }
362
363 // specialchar:
364 if ($specialSigns) {
365 for ($i = 33; $i < 47; $i++) {
366 $array[] = chr($i);
367 }
368
369 for ($i = 59; $i < 64; $i++) {
370 $array[] = chr($i);
371 }
372
373 for ($i = 91; $i < 96; $i++) {
374 $array[] = chr($i);
375 }
376
377 for ($i = 123; $i < 126; $i++) {
378 $array[] = chr($i);
379 }
380 }
381
382 mt_srand((double)microtime() * 1000000);
383 $password = '';
384
385 for ($i = 1; $i <= $length; $i++) {
386 $rnd = mt_rand(0, count($array) - 1);
387 $password.= $array[$rnd];
388 $password = md5($password);
389 }
390 } elseif ($uamOptions['file_pass_type'] == 'admin') {
391 $password = $curUserdata->user_pass;
392 }
393
394 // make .htpasswd
395 $htpasswd_txt = "$user:" . $password . "\n";
396
397 // save file
398 $htpasswd = fopen($url . ".htpasswd", "w");
399 fwrite($htpasswd, $htpasswd_txt);
400 fclose($htpasswd);
401 }
402 }
403 }
404
405 /**
406 * Deletes the htaccess files.
407 *
408 * @return null
409 */
410 function deleteHtaccessFiles()
411 {
412 $wud = wp_upload_dir();
413 if (empty($wud['error'])) {
414 $url = $wud['basedir'] . "/";
415
416 if (file_exists($url.".htaccess")) {
417 unlink($url.".htaccess");
418 }
419
420 if (file_exists($url.".htpasswd")) {
421 unlink($url.".htpasswd");
422 }
423 }
424 }
425
426 /**
427 * Returns the current settings
428 *
429 * @return array
430 */
431 function getAdminOptions()
432 {
433 if (empty($this->adminOptions)) {
434 $uamAdminOptions = array(
435 'hide_post_title' => 'false',
436 'post_title' => __('No rights!', 'user-access-manager'),
437 'post_content' => __(
438 'Sorry you have no rights to view this post!',
439 'user-access-manager'
440 ),
441 'hide_post' => 'false',
442 'hide_post_comment' => 'false',
443 'post_comment_content' => __(
444 'Sorry no rights to view comments!',
445 'user-access-manager'
446 ),
447 'post_comments_locked' => 'false',
448 'hide_page_title' => 'false',
449 'page_title' => __('No rights!', 'user-access-manager'),
450 'page_content' => __(
451 'Sorry you have no rights to view this page!',
452 'user-access-manager'
453 ),
454 'hide_page' => 'false',
455 'hide_page_comment' => 'false',
456 'page_comment_content' => __(
457 'Sorry no rights to view comments!',
458 'user-access-manager'
459 ),
460 'page_comments_locked' => 'false',
461 'redirect' => 'false',
462 'redirect_custom_page' => '',
463 'redirect_custom_url' => '',
464 'lock_recursive' => 'true',
465 'authors_has_access_to_own' => 'true',
466 'authors_can_add_posts_to_groups' => 'false',
467 'lock_file' => 'false',
468 'file_pass_type' => 'random',
469 'lock_file_types' => 'all',
470 'download_type' => 'fopen',
471 'locked_file_types' => 'zip,rar,tar,gz,bz2',
472 'not_locked_file_types' => 'gif,jpg,jpeg,png',
473 'blog_admin_hint' => 'true',
474 'blog_admin_hint_text' => '[L]',
475 'hide_empty_categories' => 'true',
476 'protect_feed' => 'true',
477 'show_post_content_before_more' => 'false',
478 'full_access_role' => 'administrator'
479 );
480
481 $uamOptions = get_option($this->adminOptionsName);
482
483 if (!empty($uamOptions)) {
484 foreach ($uamOptions as $key => $option) {
485 $uamAdminOptions[$key] = $option;
486 }
487 }
488
489 update_option($this->adminOptionsName, $uamAdminOptions);
490 $this->adminOptions = $uamAdminOptions;
491 }
492
493 return $this->adminOptions;
494 }
495
496 /**
497 * Retruns the content of the excecuded php file.
498 *
499 * @param string $fileName The file name
500 * @param integer $id The id if needed.
501 *
502 * @return string
503 */
504 function getIncludeContents($fileName, $id = null)
505 {
506 if (is_file($fileName)) {
507 ob_start();
508 include $fileName;
509 $contents = ob_get_contents();
510 ob_end_clean();
511
512 return $contents;
513 }
514
515 return '';
516 }
517
518 /**
519 * Returns the access handler object.
520 *
521 * @return object
522 */
523 function &getAccessHandler()
524 {
525 if ($this->accessHandler == null) {
526 $this->accessHandler = new UamAccessHandler(&$this);
527 }
528
529 return $this->accessHandler;
530 }
531
532
533 /*
534 * Functions for the admin panel content.
535 */
536
537 /**
538 * The function for the wp_print_styles action.
539 *
540 * @return null
541 */
542 function addStyles()
543 {
544 wp_enqueue_style(
545 'UserAccessManagerAdmin',
546 UAM_URLPATH . "css/uamAdmin.css",
547 false,
548 '1.0',
549 'screen'
550 );
551
552 wp_enqueue_style(
553 'UserAccessManagerLoginForm',
554 UAM_URLPATH . "css/uamLoginForm.css",
555 false,
556 '1.0',
557 'screen'
558 );
559 }
560
561 /**
562 * The function for the wp_print_scripts action.
563 *
564 * @return null
565 */
566 function addScripts()
567 {
568 wp_enqueue_script(
569 'UserAccessManagerJQueryTools',
570 UAM_URLPATH . 'js/jquery.tools.min.js',
571 array('jquery')
572 );
573 wp_enqueue_script(
574 'UserAccessManagerFunctions',
575 UAM_URLPATH . 'js/functions.js',
576 array('jquery', 'UserAccessManagerJQueryTools')
577 );
578 }
579
580 /**
581 * Prints the admin page
582 *
583 * @return null
584 */
585 function printAdminPage()
586 {
587 if (isset($_GET['page'])) {
588 $curAdminPage = $_GET['page'];
589 }
590
591 if ($curAdminPage == 'uam_settings') {
592 include UAM_REALPATH."tpl/adminSettings.php";
593 } elseif ($curAdminPage == 'uam_usergroup') {
594 include UAM_REALPATH."tpl/adminGroup.php";
595 } elseif ($curAdminPage == 'uam_setup') {
596 include UAM_REALPATH."tpl/adminSetup.php";
597 } elseif ($curAdminPage == 'uam_about') {
598 include UAM_REALPATH."tpl/about.php";
599 }
600 }
601
602 /**
603 * Shows the error if the user has no rights to edit the content
604 *
605 * @return null
606 */
607 function noRightsToEditContent()
608 {
609 $noRights = false;
610
611 if (isset($_GET['post'])
612 && is_numeric($_GET['post'])
613 ) {
614 $noRights
615 = !$this->getAccessHandler()->checkPostAccess($_GET['post']);
616 }
617
618 if (isset($_GET['attachment_id'])
619 && is_numeric($_GET['attachment_id'])
620 && !$noRights
621 ) {
622 $noRights
623 = !$this->getAccessHandler()->checkPostAccess($_GET['attachment_id']);
624 }
625
626 if (isset($_GET['tag_ID'])
627 && is_numeric($_GET['tag_ID'])
628 && !$noRights
629 ) {
630 $noRights
631 = !$this->getAccessHandler()->checkCategoryAccess($_GET['tag_ID']);
632 }
633
634 if ($noRights) {
635 wp_die(TXT_NO_RIGHTS);
636 }
637 }
638
639 /**
640 * The function for the wp_dashboard_setup action.
641 * Removes widgets to which a user should not have access.
642 *
643 * @return null
644 */
645 function setupAdminDashboard()
646 {
647 global $wp_meta_boxes;
648
649 if (!$this->getAccessHandler()->checkUserAccess()) {
650 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
651 }
652 }
653
654 /**
655 * The function for the update_option_permalink_structure action.
656 *
657 * @return null
658 */
659 function updatePermalink()
660 {
661 $this->createHtaccess();
662 }
663
664 /**
665 * The function for the manage_posts_columns and
666 * the manage_pages_columns filter.
667 *
668 * @param array $defaults The table headers.
669 *
670 * @return array
671 */
672 function addPostColumnsHeader($defaults)
673 {
674 $defaults['uam_access'] = __('Access');
675 return $defaults;
676 }
677
678 /**
679 * The function for the manage_users_custom_column action.
680 *
681 * @param string $columnName The column name.
682 * @param integer $id The id.
683 *
684 * @return String
685 */
686 function addPostColumn($columnName, $id)
687 {
688 if ($columnName == 'uam_access') {
689 echo $this->getIncludeContents(UAM_REALPATH.'tpl/postColumn.php', $id);
690 }
691 }
692
693 /**
694 * The function for the uma_post_access metabox.
695 *
696 * @param object $post The post.
697 *
698 * @return null;
699 */
700 function editPostContent($post)
701 {
702 include UAM_REALPATH.'tpl/postEditForm.php';
703 }
704
705 /**
706 * The function for the save_post action.
707 *
708 * @param mixed $postParam The post id or a array of a post.
709 *
710 * @return null
711 */
712 function savePostData($postParam)
713 {
714 $uamAccessHandler = &$this->getAccessHandler();
715 $uamOptions = $this->getAdminOptions();
716
717 if ($uamAccessHandler->checkUserAccess()
718 || $uamOptions['authors_can_add_posts_to_groups'] == 'true'
719 ) {
720 if (is_array($postParam)) {
721 $post = get_post($postParam['ID']);
722 } else {
723 $post = get_post($postParam);
724 }
725
726 if ($post->post_type == 'revision') {
727 $postId = $post->post_parent;
728 $post = get_post($postId);
729 } else {
730 $postId = $post->ID;
731 }
732
733 if ($post->post_type == 'post') {
734 $postType = 'Post';
735 } elseif ($post->post_type == 'page') {
736 $postType = 'Page';
737 } elseif ($post->post_type == 'attachment') {
738 $postType = 'File';
739 }
740
741 $userGroupsForPost = $uamAccessHandler->getUserGroupsForPost($postId);
742
743 foreach ($userGroupsForPost as $uamUserGroup) {
744 $uamUserGroup->{'remove'.$postType}($postId);
745 $uamUserGroup->save();
746 }
747
748 if (isset($_POST['usergroups'])) {
749 $userGroups = $_POST['usergroups'];
750 }
751
752 if (isset($userGroups)) {
753 foreach ($userGroups as $userGroupId) {
754 $uamUserGroup = $uamAccessHandler->getUserGroups($userGroupId);
755
756 $uamUserGroup->{'add'.$postType}($postId);
757 $uamUserGroup->save();
758 }
759 }
760 }
761 }
762
763 /**
764 * The function for the attachment_fields_to_save filter.
765 * We have to use this because the attachment actions work
766 * not in the way we need.
767 *
768 * @param object $attachment The attachment id.
769 *
770 * @return object
771 */
772 function saveAttachmentData($attachment)
773 {
774 $this->savePostData($attachment['ID']);
775
776 return $attachment;
777 }
778
779 /**
780 * The function for the delete_post action.
781 *
782 * @param integer $postId The post id.
783 *
784 * @return null
785 */
786 function removePostData($postId)
787 {
788 global $wpdb;
789
790 $wpdb->query(
791 "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
792 WHERE post_id = ".$postId
793 );
794 }
795
796 /**
797 * The function for the media_meta action.
798 *
799 * @param string $meta The meta.
800 * @param object $post The post.
801 *
802 * @return string
803 */
804 function showMediaFile($meta = '', $post = null)
805 {
806 $content = $meta;
807 $content .= '</td></tr><tr>';
808 $content .= '<th class="label">';
809 $content .= '<label>'.TXT_SET_UP_USERGROUPS.'</label>';
810 $content .= '</th>';
811 $content .= '<td class="field">';
812 $content .= $this->getIncludeContents(UAM_REALPATH.'tpl/postEditForm.php');
813
814 return $content;
815 }
816
817 /**
818 * The function for the manage_users_columns filter.
819 *
820 * @param array $defaults The table headers.
821 *
822 * @return array
823 */
824 function addUserColumnsHeader($defaults)
825 {
826 $defaults['uam_access'] = __('uam user groups');
827 return $defaults;
828 }
829
830 /**
831 * The function for the manage_users_custom_column action.
832 *
833 * @param unknown $empty An empty string from wordpress? What the hell?!?
834 * @param string $columnName The column name.
835 * @param integer $id The id.
836 *
837 * @return String
838 */
839 function addUserColumn($empty, $columnName, $id)
840 {
841 if ($columnName == 'uam_access') {
842 return $this->getIncludeContents(
843 UAM_REALPATH.'tpl/userColumn.php',
844 $id
845 );
846 }
847 }
848
849 /**
850 * The function for the edit_user_profile action.
851 *
852 * @return null
853 */
854 function showUserProfile()
855 {
856 echo $this->getIncludeContents(UAM_REALPATH.'tpl/userProfileEditForm.php');
857 }
858
859 /**
860 * The function for the profile_update action.
861 *
862 * @param integer $userId The user id.
863 *
864 * @return null
865 */
866 function saveUserData($userId)
867 {
868 $uamAccessHandler = &$this->getAccessHandler();
869
870 if ($uamAccessHandler->checkUserAccess()) {
871 $userGroupsForUser
872 = $uamAccessHandler->getUserGroupsForUser($userId);
873
874 foreach ($userGroupsForUser as $uamUserGroup) {
875 $uamUserGroup->removeUser($userId);
876 $uamUserGroup->save();
877 }
878
879 if (isset($_POST['usergroups'])) {
880 $userGroups = $_POST['usergroups'];
881 }
882
883 if (isset($userGroups)) {
884 foreach ($userGroups as $userGroupId) {
885 $uamUserGroup
886 = $uamAccessHandler->getUserGroups($userGroupId);
887
888 $uamUserGroup->addUser($userId);
889 $uamUserGroup->save();
890 }
891 }
892 }
893 }
894
895 /**
896 * The function for the delete_user action.
897 *
898 * @param integer $userId The user id.
899 *
900 * @return null
901 */
902 function removeUserData($userId)
903 {
904 global $wpdb;
905
906 $wpdb->query(
907 "DELETE FROM " . DB_ACCESSGROUP_TO_USER . "
908 WHERE user_id = ".$userId
909 );
910 }
911
912 /**
913 * The function for the manage_categories_columns filter.
914 *
915 * @param array $defaults The table headers.
916 *
917 * @return array
918 */
919 function addCategoryColumnsHeader($defaults)
920 {
921 $defaults['uam_access'] = __('Access');
922 return $defaults;
923 }
924
925 /**
926 * The function for the manage_categories_custom_column action.
927 *
928 * @param unknown $empty An empty string from wordpress? What the hell?!?
929 * @param string $columnName The column name.
930 * @param integer $id The id.
931 *
932 * @return String
933 */
934 function addCategoryColumn($empty, $columnName, $id)
935 {
936 if ($columnName == 'uam_access') {
937 return $this->getIncludeContents(
938 UAM_REALPATH.'tpl/categoryColumn.php',
939 $id
940 );
941 }
942 }
943
944 /**
945 * The function for the edit_category_form action.
946 *
947 * @param object $category The category.
948 *
949 * @return null
950 */
951 function showCategoryEditForm($category)
952 {
953 include UAM_REALPATH.'tpl/categoryEditForm.php';
954 }
955
956 /**
957 * The function for the edit_category action.
958 *
959 * @param integer $categoryId The category id.
960 *
961 * @return null
962 */
963 function saveCategoryData($categoryId)
964 {
965 $uamAccessHandler = &$this->getAccessHandler();
966 $uamOptions = $this->getAdminOptions();
967
968 if ($uamAccessHandler->checkUserAccess()
969 || $uamOptions['authors_can_add_posts_to_groups'] == 'true'
970 ) {
971 $userGroupsForCategory
972 = $uamAccessHandler->getUserGroupsForCategory($categoryId);
973
974 foreach ($userGroupsForCategory as $uamUserGroup) {
975 $uamUserGroup->removeCategory($categoryId);
976 $uamUserGroup->save();
977 }
978
979 if (isset($_POST['usergroups'])) {
980 $userGroups = $_POST['usergroups'];
981 }
982
983 if (isset($userGroups)) {
984 foreach ($userGroups as $userGroupId) {
985 $uamUserGroup = $uamAccessHandler->getUserGroups($userGroupId);
986
987 $uamUserGroup->addCategory($categoryId);
988 $uamUserGroup->save();
989 }
990 }
991 }
992 }
993
994 /**
995 * The function for the delete_category action.
996 *
997 * @param integer $categoryId The id of the category.
998 *
999 * @return null
1000 */
1001 function removeCategoryData($categoryId)
1002 {
1003 global $wpdb;
1004
1005 $wpdb->query(
1006 "DELETE FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
1007 WHERE category_id = ".$categoryId
1008 );
1009 }
1010
1011
1012 /*
1013 * Functions for the blog content.
1014 */
1015
1016 /**
1017 * Modifies the content of the post by the given settings.
1018 *
1019 * @param object $post The current post.
1020 *
1021 * @return object
1022 */
1023 private function _getPost($post)
1024 {
1025 $uamOptions = $this->getAdminOptions();
1026 $uamAccessHandler = &$this->getAccessHandler();
1027
1028 $postType = $post->post_type;
1029
1030 if ($postType == 'attachment') {
1031 $postType = 'post';
1032 } elseif ($postType != 'post' && $postType != 'page') {
1033 return $post;
1034 }
1035
1036 if ($uamOptions['hide_'.$postType] == 'true'
1037 || $this->atAdminPanel
1038 ) {
1039 if ($uamAccessHandler->checkPostAccess($post->ID)) {
1040 $post->post_title .= $this->adminOutput($post->ID);
1041
1042 return $post;
1043 }
1044 } else {
1045 if (!$uamAccessHandler->checkPostAccess($post->ID)) {
1046 $uamPostContent = $uamOptions[$postType.'_content'];
1047 $uamPostContent = str_replace(
1048 "[LOGIN_FORM]",
1049 $this->getLoginBarHtml(),
1050 $uamPostContent
1051 );
1052
1053 if ($uamOptions['hide_'.$postType.'_title'] == 'true') {
1054 $post->post_title = $uamOptions[$postType.'_title'];
1055 }
1056
1057 if ($uamOptions[$postType.'_comments_locked'] == 'false') {
1058 $post->comment_status = 'close';
1059 }
1060
1061 if ($uamOptions['show_post_content_before_more'] == 'true'
1062 && $postType == "post"
1063 && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches)
1064 ) {
1065 $post->post_content = explode(
1066 $matches[0],
1067 $post->post_content,
1068 2
1069 );
1070 $uamPostContent
1071 = $post->post_content[0] . " " . $uamPostContent;
1072 }
1073
1074 $post->post_content = $uamPostContent;
1075 }
1076
1077 $post->post_title .= $this->adminOutput($post->ID);
1078
1079 return $post;
1080 }
1081
1082 return null;
1083 }
1084
1085 /**
1086 * The function for the the_posts filter.
1087 *
1088 * @param arrray $posts The posts.
1089 *
1090 * @return array
1091 */
1092 function showPost($posts = array())
1093 {
1094 $showPosts = array();
1095 $uamOptions = $this->getAdminOptions();
1096
1097 if (!is_feed()
1098 || ($uamOptions['protect_feed'] == 'true' && is_feed())
1099 ) {
1100 foreach ($posts as $post) {
1101 $post = $this->_getPost($post);
1102
1103 if ($post !== null) {
1104 $showPosts[] = $post;
1105 }
1106 }
1107
1108 $posts = $showPosts;
1109 }
1110
1111 return $posts;
1112 }
1113
1114 /**
1115 * The function for the wp_get_nav_menu_items filter.
1116 *
1117 * @param array $items The menu item.
1118 *
1119 * @return array
1120 */
1121 function showCustomMenu($items)
1122 {
1123 $showItems = array();
1124
1125 foreach ($items as $item) {
1126 if ($item->object == 'post'
1127 || $item->object == 'page'
1128 ) {
1129 $object = get_post($item->object_id);
1130 $post = $this->_getPost($object);
1131
1132 if ($post !== null) {
1133 $item->title = $post->post_title;
1134
1135 $showItems[] = $item;
1136 }
1137 } elseif ($item->object == 'category') {
1138 $object = get_category($item->object_id);
1139 $category = $this->_getCategory($object);
1140
1141 if ($category !== null
1142 && !$category->isEmpty
1143 ) {
1144 $showItems[] = $item;
1145 }
1146 } else {
1147 $showItems[] = $item;
1148 }
1149 }
1150
1151 return $showItems;
1152 }
1153
1154 /**
1155 * The function for the comments_array filter.
1156 *
1157 * @param array $comments The comments.
1158 *
1159 * @return array
1160 */
1161 function showComment($comments = array())
1162 {
1163 $showComments = array();
1164 $uamOptions = $this->getAdminOptions();
1165 $uamAccessHandler = &$this->getAccessHandler();
1166
1167 foreach ($comments as $comment) {
1168 $post = get_post($comment->comment_post_ID);
1169 $postType = $post->post_type;
1170
1171 if ($uamOptions['hide_'.$postType.'_comment'] == 'true'
1172 || $uamOptions['hide_'.$postType] == 'true'
1173 || $this->atAdminPanel
1174 ) {
1175 if ($uamAccessHandler->checkPostAccess($post->ID)) {
1176 $showComments[] = $comment;
1177 }
1178 } else {
1179 if (!$uamAccessHandler->checkPostAccess($post->ID)) {
1180 $comment->comment_content
1181 = $uamOptions[$postType.'_comment_content'];
1182 }
1183
1184 $showComments[] = $comment;
1185 }
1186 }
1187
1188 $comments = $showComments;
1189
1190 return $comments;
1191 }
1192
1193 /**
1194 * The function for the get_pages filter.
1195 *
1196 * @param array $pages The pages.
1197 *
1198 * @return array
1199 */
1200 function showPage($pages = array())
1201 {
1202 $showPages = array();
1203 $uamOptions = $this->getAdminOptions();
1204 $uamAccessHandler = &$this->getAccessHandler();
1205
1206 foreach ($pages as $page) {
1207 if ($uamOptions['hide_page'] == 'true'
1208 || $this->atAdminPanel
1209 ) {
1210 if ($uamAccessHandler->checkPostAccess($page->ID)) {
1211 $page->post_title.= $this->adminOutput($page->ID);
1212 $showPages[] = $page;
1213 }
1214 } else {
1215 if (!$uamAccessHandler->checkPostAccess($page->ID)) {
1216 if ($uamOptions['hide_page_title'] == 'true') {
1217 $page->post_title = $uamOptions['page_title'];
1218 }
1219
1220 $page->post_content = $uamOptions['page_content'];
1221 }
1222
1223 $page->post_title.= $this->adminOutput($page->ID);
1224 $showPages[] = $page;
1225 }
1226 }
1227
1228 $pages = $showPages;
1229
1230 return $pages;
1231 }
1232
1233 /**
1234 * Modifies the content of the category by the given settings.
1235 *
1236 * @param object $category The current category.
1237 *
1238 * @return object
1239 */
1240 private function _getCategory($category)
1241 {
1242 $uamOptions = $this->getAdminOptions();
1243 $uamAccessHandler = &$this->getAccessHandler();
1244
1245 $category->isEmpty = false;
1246
1247 if ($uamAccessHandler->checkCategoryAccess($category->term_id)) {
1248 if ($this->atAdminPanel == false
1249 && ($uamOptions['hide_post'] == 'true'
1250 || $uamOptions['hide_page'] == 'true')
1251 ) {
1252 $args = array(
1253 'numberposts' => - 1,
1254 'category' => $category->term_id
1255 );
1256
1257 $categoryPosts = get_posts($args);
1258
1259 if (isset($categoryPosts)) {
1260 foreach ($categoryPosts as $post) {
1261 if ($uamOptions['hide_'.$post->post_type] == 'true'
1262 && !$uamAccessHandler->checkPostAccess($post->ID)
1263 ) {
1264 $category->count--;
1265 }
1266 }
1267 }
1268
1269 if ($category->count <= 0
1270 && $uamOptions['hide_empty_categories'] == 'true'
1271 && $category->taxonomy == "category"
1272 ) {
1273 $category->isEmpty = true;
1274 }
1275
1276 if ($uamOptions['lock_recursive'] == 'false') {
1277 $curCategory = $category;
1278
1279 while ($curCategory->parent != 0) {
1280 $curCategory = get_category($curCategory->parent);
1281
1282 if ($uamAccessHandler->checkCategoryAccess($curCategory->term_id)) {
1283 $category->parent = $curCategory->term_id;
1284 break;
1285 }
1286 }
1287 }
1288
1289 return $category;
1290 } else {
1291 return $category;
1292 }
1293 }
1294
1295 return null;
1296 }
1297
1298 /**
1299 * The function for the get_terms filter.
1300 *
1301 * @param array $categories The categories.
1302 * @param array $args The given arguments.
1303 *
1304 * @return array
1305 */
1306 function showCategory($categories = array(), $args = array())
1307 {
1308 $uamOptions = $this->getAdminOptions();
1309 $uamAccessHandler = &$this->getAccessHandler();
1310
1311 $showCategories = array();
1312
1313 $uamOptions = $this->getAdminOptions();
1314
1315 foreach ($categories as $category) {
1316 if (!is_object($category)) {
1317 return $categories;
1318 }
1319
1320 $category = $this->_getCategory($category);
1321
1322 if ($category !== null) {
1323 if (!$category->isEmpty) {
1324 $showCategories[$category->term_id] = $category;
1325 }
1326 }
1327 }
1328
1329 foreach ($categories as $key => $category) {
1330 if (!array_key_exists($category->term_id, $showCategories)) {
1331 unset($categories[$key]);
1332 }
1333 }
1334
1335 return $categories;
1336 }
1337
1338 /**
1339 * The function for the get_previous_post_where and
1340 * the get_next_post_where filter.
1341 *
1342 * @param string $sql The current sql string.
1343 *
1344 * @return string
1345 */
1346 function showNextPreviousPost($sql)
1347 {
1348 $uamOptions = $this->getAdminOptions();
1349
1350 if ($uamOptions['hide_post'] == 'true') {
1351 $posts = get_posts();
1352 $uamAccessHandler = &$this->getAccessHandler();
1353
1354 if (isset($posts)) {
1355 foreach ($posts as $post) {
1356 if (!$uamAccessHandler->checkPostAccess($post->ID)) {
1357 $excludedPosts[] = $post->ID;
1358 }
1359 }
1360
1361 global $wpdb;
1362
1363 if (isset($excludedPosts)) {
1364 $excludedPostsStr = implode(",", $excludedPosts);
1365 $sql.= "AND p.ID NOT IN($excludedPostsStr)";
1366 }
1367 }
1368 }
1369
1370 return $sql;
1371 }
1372
1373 /**
1374 * Returns the admin hint.
1375 *
1376 * @param integer $postId The post id we want to check.
1377 *
1378 * @return string
1379 */
1380 function adminOutput($postId)
1381 {
1382 $output = "";
1383
1384 if (!$this->atAdminPanel) {
1385 $uamOptions = $this->getAdminOptions();
1386
1387 if ($uamOptions['blog_admin_hint'] == 'true') {
1388 global $current_user;
1389
1390 $curUserdata = get_userdata($current_user->ID);
1391
1392 if (!isset($curUserdata->user_level)) {
1393 return $output;
1394 }
1395
1396 $uamAccessHandler = &$this->getAccessHandler();
1397
1398 if (count($uamAccessHandler->getUserGroupsForPost($postId)) > 0) {
1399 $output .= $uamOptions['blog_admin_hint_text'];
1400 }
1401 }
1402 }
1403
1404 return $output;
1405 }
1406
1407 /**
1408 * The function for the edit_post_link filter.
1409 *
1410 * @param string $link The edit link.
1411 * @param integer $postId The id of the post.
1412 *
1413 * @return string
1414 */
1415 function showGroupMembership($link, $postId)
1416 {
1417 $uamAccessHandler = &$this->getAccessHandler();
1418 $groups = $uamAccessHandler->getUserGroupsForPost($postId);
1419
1420 if (count($groups) > 0) {
1421 $link .= ' | '.TXT_ASSIGNED_GROUPS.': ';
1422
1423 foreach ($groups as $group) {
1424 $link .= $group->getGroupName().', ';
1425 }
1426
1427 $link = rtrim($link, ', ');
1428 }
1429
1430 return $link;
1431 }
1432
1433 /**
1434 * Returns the login bar.
1435 *
1436 * @return string
1437 */
1438 function getLoginBarHtml()
1439 {
1440 if (!is_user_logged_in()) {
1441 return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
1442 }
1443
1444 return '';
1445 }
1446
1447
1448 /*
1449 * Functions for the redirection and files.
1450 */
1451
1452 /**
1453 * Redirects to a page or to content.
1454 *
1455 * @return null
1456 */
1457 function redirect()
1458 {
1459 $uamOptions = $this->getAdminOptions();
1460
1461 if (isset($_GET['getfile'])) {
1462 $fileUrl = $_GET['getfile'];
1463 }
1464
1465 $emptyId = null;
1466 $post = get_post($emptyId);
1467
1468 if ($post !== null
1469 && $uamOptions['redirect'] != 'false'
1470 && !$this->getAccessHandler()->checkPostAccess($post->ID)
1471 && !$this->atAdminPanel
1472 && !isset($fileUrl)
1473 ) {
1474 $this->redirectUser();
1475 } elseif (isset($fileUrl)) {
1476 $permaStruc = get_option('permalink_structure');
1477
1478 if (!empty($permaStruc)) {
1479 $uploadDir = wp_upload_dir();
1480 $fileUrl = $uploadDir['baseurl'].'/'.$fileUrl;
1481 }
1482
1483 $this->getFile($fileUrl);
1484 }
1485 }
1486
1487 /**
1488 * Redirects the user to his destination.
1489 *
1490 * @return null
1491 */
1492 function redirectUser()
1493 {
1494 global $wp_query;
1495
1496 $postToShow = false;
1497 $posts = $wp_query->get_posts();
1498
1499 if (isset($posts)) {
1500 foreach ($posts as $post) {
1501 if ($this->getAccessHandler()->checkPostAccess($post->ID)) {
1502 $postToShow = true;
1503 break;
1504 }
1505 }
1506 }
1507
1508 if (!$postToShow) {
1509 $uamOptions = $this->getAdminOptions();
1510
1511 if ($uamOptions['redirect'] == 'blog') {
1512 $url = home_url('/');
1513 } elseif ($uamOptions['redirect'] == 'custom_page') {
1514 $post = get_post($uamOptions['redirect_custom_page']);
1515 $url = $post->guid;
1516 } elseif ($uamOptions['redirect'] == 'custom_url') {
1517 $url = $uamOptions['redirect_custom_url'];
1518 }
1519
1520 if ($url != "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]) {
1521 wp_redirect($url);
1522 }
1523 }
1524 }
1525
1526 /**
1527 * Delivers the content of the requestet file.
1528 *
1529 * @param string $url The file url.
1530 *
1531 * @return null
1532 */
1533 function getFile($url)
1534 {
1535 $post = get_post($this->getAttachmentIdByUrl($url));
1536
1537 if ($post !== null) {
1538 $file = null;
1539 } else {
1540 return null;
1541 }
1542
1543 if ($post->post_type == 'attachment'
1544 && $this->getAccessHandler()->checkPostAccess($post->ID)
1545 ) {
1546 $uploadDir = wp_upload_dir();
1547 $file = $uploadDir['basedir'].str_replace(
1548 $uploadDir['baseurl'],
1549 '',
1550 $url
1551 );
1552 } else if (wp_attachment_is_image($post->ID)) {
1553 $file = UAM_REALPATH.'gfx/noAccessPic.png';
1554 } else {
1555 wp_die(TXT_NO_RIGHTS);
1556 }
1557
1558 //Deliver content
1559 if (file_exists($file)) {
1560 $uamOptions = $this->getAdminOptions();
1561 $fileName = basename($file);
1562
1563 header('Content-Description: File Transfer');
1564 header('Content-Type: '.$post->post_mime_type);
1565
1566 if (!wp_attachment_is_image($post->ID)) {
1567 $baseName = str_replace(' ', '_', basename($file));
1568
1569 header('Content-Disposition: attachment; filename="'.$baseName.'"');
1570 }
1571
1572 header('Content-Transfer-Encoding: binary');
1573 header('Content-Length: '.filesize($file));
1574
1575 if ($uamOptions['download_type'] == 'fopen'
1576 && !wp_attachment_is_image($post->ID)
1577 ) {
1578 $fp = fopen($file, 'rb');
1579
1580 while (!feof($fp)) {
1581 set_time_limit(30);
1582 $buffer = fread($fp, 1024);
1583 echo $buffer;
1584 }
1585
1586 exit;
1587 } else {
1588 ob_clean();
1589 flush();
1590 readfile($file);
1591 exit;
1592 }
1593 } else {
1594 wp_die(TXT_FILE_NOT_FOUND_ERROR);
1595 }
1596 }
1597
1598 /**
1599 * Returns the url for a locked file.
1600 *
1601 * @param string $url The base url.
1602 * @param integer $id The id of the file.
1603 *
1604 * @return string
1605 */
1606 function getFileUrl($url, $id)
1607 {
1608 $uamOptions = $this->getAdminOptions();
1609 $permaStruc = get_option('permalink_structure');
1610
1611 if (empty($permaStruc)
1612 && $uamOptions['lock_file'] == 'true'
1613 ) {
1614 $post = &get_post($id);
1615
1616 $type = explode("/", $post->post_mime_type);
1617 $type = $type[1];
1618
1619 $fileTypes = explode(
1620 ",",
1621 $uamOptions['locked_file_types']
1622 );
1623
1624 if (in_array($type, $fileTypes)
1625 || $uamOptions['lock_file_types'] == 'all'
1626 ) {
1627 $url = home_url('/').'?getfile='.$url;
1628 }
1629 }
1630
1631 return $url;
1632 }
1633
1634 /**
1635 * Returns the post by the given url.
1636 *
1637 * @param string $url The url of the post(attachment).
1638 *
1639 * @return object The post.
1640 */
1641 function getAttachmentIdByUrl($url)
1642 {
1643 //Filter editstring
1644 $newUrl = preg_split("/-e[0-9]*/", $url);
1645
1646 if (count($newUrl) == 2) {
1647 $newUrl = $newUrl[0].$newUrl[1];
1648 } else {
1649 $newUrl = $newUrl[0];
1650 }
1651
1652 //Filter size
1653 $newUrl = preg_split("/-[0-9]*x[0-9]*/", $newUrl);
1654
1655 if (count($newUrl) == 2) {
1656 $newUrl = $newUrl[0].$newUrl[1];
1657 } else {
1658 $newUrl = $newUrl[0];
1659 }
1660
1661 global $wpdb;
1662 $dbPost = $wpdb->get_row(
1663 "SELECT ID
1664 FROM ".$wpdb->prefix."posts
1665 WHERE guid = '" . $newUrl . "'
1666 LIMIT 1",
1667 ARRAY_A
1668 );
1669
1670 if ($dbPost) {
1671 return $dbPost['ID'];
1672 }
1673
1674 return null;
1675 }
1676 }