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

1,691 lines 48.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.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 echo $userId;
872
873 $userGroupsForUser
874 = $uamAccessHandler->getUserGroupsForUser($userId);
875
876 foreach ($userGroupsForUser as $uamUserGroup) {
877 $uamUserGroup->removeUser($userId);
878 $uamUserGroup->save();
879 }
880
881 if (isset($_POST['usergroups'])) {
882 $userGroups = $_POST['usergroups'];
883 }
884
885 if (isset($userGroups)) {
886 foreach ($userGroups as $userGroupId) {
887 $uamUserGroup
888 = $uamAccessHandler->getUserGroups($userGroupId);
889
890 $uamUserGroup->addUser($userId);
891 $uamUserGroup->save();
892 }
893 }
894 }
895 }
896
897 /**
898 * The function for the delete_user action.
899 *
900 * @param integer $userId The user id.
901 *
902 * @return null
903 */
904 function removeUserData($userId)
905 {
906 global $wpdb;
907
908 $wpdb->query(
909 "DELETE FROM " . DB_ACCESSGROUP_TO_USER . "
910 WHERE user_id = ".$userId
911 );
912 }
913
914 /**
915 * The function for the manage_categories_columns filter.
916 *
917 * @param array $defaults The table headers.
918 *
919 * @return array
920 */
921 function addCategoryColumnsHeader($defaults)
922 {
923 $defaults['uam_access'] = __('Access');
924 return $defaults;
925 }
926
927 /**
928 * The function for the manage_categories_custom_column action.
929 *
930 * @param unknown $empty An empty string from wordpress? What the hell?!?
931 * @param string $columnName The column name.
932 * @param integer $id The id.
933 *
934 * @return String
935 */
936 function addCategoryColumn($empty, $columnName, $id)
937 {
938 if ($columnName == 'uam_access') {
939 return $this->getIncludeContents(
940 UAM_REALPATH.'tpl/categoryColumn.php',
941 $id
942 );
943 }
944 }
945
946 /**
947 * The function for the edit_category_form action.
948 *
949 * @param object $category The category.
950 *
951 * @return null
952 */
953 function showCategoryEditForm($category)
954 {
955 include UAM_REALPATH.'tpl/categoryEditForm.php';
956 }
957
958 /**
959 * The function for the edit_category action.
960 *
961 * @param integer $categoryId The category id.
962 *
963 * @return null
964 */
965 function saveCategoryData($categoryId)
966 {
967 $uamAccessHandler = &$this->getAccessHandler();
968 $uamOptions = $this->getAdminOptions();
969
970 if ($uamAccessHandler->checkUserAccess()
971 || $uamOptions['authors_can_add_posts_to_groups'] == 'true'
972 ) {
973 $userGroupsForCategory
974 = $uamAccessHandler->getUserGroupsForCategory($categoryId);
975
976 foreach ($userGroupsForCategory as $uamUserGroup) {
977 $uamUserGroup->removeCategory($categoryId);
978 $uamUserGroup->save();
979 }
980
981 if (isset($_POST['usergroups'])) {
982 $userGroups = $_POST['usergroups'];
983 }
984
985 if (isset($userGroups)) {
986 foreach ($userGroups as $userGroupId) {
987 $uamUserGroup = $uamAccessHandler->getUserGroups($userGroupId);
988
989 $uamUserGroup->addCategory($categoryId);
990 $uamUserGroup->save();
991 }
992 }
993 }
994 }
995
996 /**
997 * The function for the delete_category action.
998 *
999 * @param integer $categoryId The id of the category.
1000 *
1001 * @return null
1002 */
1003 function removeCategoryData($categoryId)
1004 {
1005 global $wpdb;
1006
1007 $wpdb->query(
1008 "DELETE FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
1009 WHERE category_id = ".$categoryId
1010 );
1011 }
1012
1013
1014 /*
1015 * Functions for the blog content.
1016 */
1017
1018 /**
1019 * Modifies the content of the post by the given settings.
1020 *
1021 * @param object $post The current post.
1022 *
1023 * @return object
1024 */
1025 private function _getPost($post)
1026 {
1027 $uamOptions = $this->getAdminOptions();
1028 $uamAccessHandler = &$this->getAccessHandler();
1029
1030 $postType = $post->post_type;
1031
1032 if ($postType == 'attachment') {
1033 $postType = 'post';
1034 } elseif ($postType != 'post' && $postType != 'page') {
1035 return $post;
1036 }
1037
1038 if ($uamOptions['hide_'.$postType] == 'true'
1039 || $this->atAdminPanel
1040 ) {
1041 if ($uamAccessHandler->checkPostAccess($post->ID)) {
1042 $post->post_title .= $this->adminOutput($post->ID);
1043
1044 return $post;
1045 }
1046 } else {
1047 if (!$uamAccessHandler->checkPostAccess($post->ID)) {
1048 $uamPostContent = $uamOptions[$postType.'_content'];
1049 $uamPostContent = str_replace(
1050 "[LOGIN_FORM]",
1051 $this->getLoginBarHtml(),
1052 $uamPostContent
1053 );
1054
1055 if ($uamOptions['hide_'.$postType.'_title'] == 'true') {
1056 $post->post_title = $uamOptions[$postType.'_title'];
1057 }
1058
1059 if ($uamOptions[$postType.'_comments_locked'] == 'false') {
1060 $post->comment_status = 'close';
1061 }
1062
1063 if ($uamOptions['show_post_content_before_more'] == 'true'
1064 && $postType == "post"
1065 && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches)
1066 ) {
1067 $post->post_content = explode(
1068 $matches[0],
1069 $post->post_content,
1070 2
1071 );
1072 $uamPostContent
1073 = $post->post_content[0] . " " . $uamPostContent;
1074 }
1075
1076 $post->post_content = $uamPostContent;
1077 }
1078
1079 $post->post_title .= $this->adminOutput($post->ID);
1080
1081 return $post;
1082 }
1083
1084 return null;
1085 }
1086
1087 /**
1088 * The function for the the_posts filter.
1089 *
1090 * @param arrray $posts The posts.
1091 *
1092 * @return array
1093 */
1094 function showPost($posts = array())
1095 {
1096 $showPosts = array();
1097 $uamOptions = $this->getAdminOptions();
1098
1099 if (!is_feed()
1100 || ($uamOptions['protect_feed'] == 'true' && is_feed())
1101 ) {
1102 foreach ($posts as $post) {
1103 $post = $this->_getPost($post);
1104
1105 if ($post !== null) {
1106 $showPosts[] = $post;
1107 }
1108 }
1109
1110 $posts = $showPosts;
1111 }
1112
1113 return $posts;
1114 }
1115
1116 /**
1117 * The function for the wp_get_nav_menu_items filter.
1118 *
1119 * @param array $items The menu item.
1120 *
1121 * @return array
1122 */
1123 function showCustomMenu($items)
1124 {
1125 $showItems = array();
1126
1127 foreach ($items as $item) {
1128 if ($item->object == 'post'
1129 || $item->object == 'page'
1130 ) {
1131 $object = get_post($item->object_id);
1132 $post = $this->_getPost($object);
1133
1134 if ($post !== null) {
1135 $item->title = $post->post_title;
1136
1137 $showItems[] = $item;
1138 }
1139 } elseif ($item->object == 'category') {
1140 $object = get_category($item->object_id);
1141 $category = $this->_getCategory($object);
1142
1143 if ($category !== null
1144 && !$category->isEmpty
1145 ) {
1146 $showItems[] = $item;
1147 }
1148 } else {
1149 $showItems[] = $item;
1150 }
1151 }
1152
1153 return $showItems;
1154 }
1155
1156 /**
1157 * The function for the comments_array filter.
1158 *
1159 * @param array $comments The comments.
1160 *
1161 * @return array
1162 */
1163 function showComment($comments = array())
1164 {
1165 $showComments = array();
1166 $uamOptions = $this->getAdminOptions();
1167 $uamAccessHandler = &$this->getAccessHandler();
1168
1169 foreach ($comments as $comment) {
1170 $post = get_post($comment->comment_post_ID);
1171 $postType = $post->post_type;
1172
1173 if ($uamOptions['hide_'.$postType.'_comment'] == 'true'
1174 || $uamOptions['hide_'.$postType] == 'true'
1175 || $this->atAdminPanel
1176 ) {
1177 if ($uamAccessHandler->checkPostAccess($post->ID)) {
1178 $showComments[] = $comment;
1179 }
1180 } else {
1181 if (!$uamAccessHandler->checkPostAccess($post->ID)) {
1182 $comment->comment_content
1183 = $uamOptions[$postType.'_comment_content'];
1184 }
1185
1186 $showComments[] = $comment;
1187 }
1188 }
1189
1190 $comments = $showComments;
1191
1192 return $comments;
1193 }
1194
1195 /**
1196 * The function for the get_pages filter.
1197 *
1198 * @param array $pages The pages.
1199 *
1200 * @return array
1201 */
1202 function showPage($pages = array())
1203 {
1204 $showPages = array();
1205 $uamOptions = $this->getAdminOptions();
1206 $uamAccessHandler = &$this->getAccessHandler();
1207
1208 foreach ($pages as $page) {
1209 if ($uamOptions['hide_page'] == 'true'
1210 || $this->atAdminPanel
1211 ) {
1212 if ($uamAccessHandler->checkPostAccess($page->ID)) {
1213 $page->post_title.= $this->adminOutput($page->ID);
1214 $showPages[] = $page;
1215 }
1216 } else {
1217 if (!$uamAccessHandler->checkPostAccess($page->ID)) {
1218 if ($uamOptions['hide_page_title'] == 'true') {
1219 $page->post_title = $uamOptions['page_title'];
1220 }
1221
1222 $page->post_content = $uamOptions['page_content'];
1223 }
1224
1225 $page->post_title.= $this->adminOutput($page->ID);
1226 $showPages[] = $page;
1227 }
1228 }
1229
1230 $pages = $showPages;
1231
1232 return $pages;
1233 }
1234
1235 /**
1236 * Modifies the content of the category by the given settings.
1237 *
1238 * @param object $category The current category.
1239 *
1240 * @return object
1241 */
1242 private function _getCategory($category)
1243 {
1244 $uamOptions = $this->getAdminOptions();
1245 $uamAccessHandler = &$this->getAccessHandler();
1246
1247 $category->isEmpty = false;
1248
1249 if ($uamAccessHandler->checkCategoryAccess($category->term_id)) {
1250 if ($this->atAdminPanel == false
1251 && ($uamOptions['hide_post'] == 'true'
1252 || $uamOptions['hide_page'] == 'true')
1253 ) {
1254 $args = array(
1255 'numberposts' => - 1,
1256 'category' => $category->term_id
1257 );
1258
1259 $categoryPosts = get_posts($args);
1260
1261 if (isset($categoryPosts)) {
1262 foreach ($categoryPosts as $post) {
1263 if ($uamOptions['hide_'.$post->post_type] == 'true'
1264 && !$uamAccessHandler->checkPostAccess($post->ID)
1265 ) {
1266 $category->count--;
1267 }
1268 }
1269 }
1270
1271 if ($category->count <= 0
1272 && $uamOptions['hide_empty_categories'] == 'true'
1273 && $category->taxonomy == "category"
1274 ) {
1275 $category->isEmpty = true;
1276 }
1277
1278 if ($uamOptions['lock_recursive'] == 'false') {
1279 $curCategory = $category;
1280
1281 while ($curCategory->parent != 0) {
1282 $curCategory = get_category($curCategory->parent);
1283
1284 if ($uamAccessHandler->checkCategoryAccess($curCategory->term_id)) {
1285 $category->parent = $curCategory->term_id;
1286 break;
1287 }
1288 }
1289 }
1290
1291 return $category;
1292 } else {
1293 return $category;
1294 }
1295 }
1296
1297 return null;
1298 }
1299
1300 /**
1301 * The function for the get_terms filter.
1302 *
1303 * @param array $categories The categories.
1304 * @param array $args The given arguments.
1305 *
1306 * @return array
1307 */
1308 function showCategory($categories = array(), $args = array())
1309 {
1310 $uamOptions = $this->getAdminOptions();
1311 $uamAccessHandler = &$this->getAccessHandler();
1312
1313 $showCategories = array();
1314
1315 $uamOptions = $this->getAdminOptions();
1316
1317 foreach ($categories as $category) {
1318 if (!is_object($category)) {
1319 return $categories;
1320 }
1321
1322 $category = $this->_getCategory($category);
1323
1324 if ($category !== null) {
1325 if (!$category->isEmpty) {
1326 $showCategories[$category->term_id] = $category;
1327 }
1328 }
1329 }
1330
1331 foreach ($categories as $key => $category) {
1332 if (!array_key_exists($category->term_id, $showCategories)) {
1333 unset($categories[$key]);
1334 }
1335 }
1336
1337 return $categories;
1338 }
1339
1340 /**
1341 * The function for the get_previous_post_where and
1342 * the get_next_post_where filter.
1343 *
1344 * @param string $sql The current sql string.
1345 *
1346 * @return string
1347 */
1348 function showNextPreviousPost($sql)
1349 {
1350 $uamOptions = $this->getAdminOptions();
1351
1352 if ($uamOptions['hide_post'] == 'true') {
1353 $posts = get_posts();
1354 $uamAccessHandler = &$this->getAccessHandler();
1355
1356 if (isset($posts)) {
1357 foreach ($posts as $post) {
1358 if (!$uamAccessHandler->checkPostAccess($post->ID)) {
1359 $excludedPosts[] = $post->ID;
1360 }
1361 }
1362
1363 global $wpdb;
1364
1365 if (isset($excludedPosts)) {
1366 $excludedPostsStr = implode(",", $excludedPosts);
1367 $sql.= "AND p.ID NOT IN($excludedPostsStr)";
1368 }
1369 }
1370 }
1371
1372 return $sql;
1373 }
1374
1375 /**
1376 * Returns the admin hint.
1377 *
1378 * @param integer $postId The post id we want to check.
1379 *
1380 * @return string
1381 */
1382 function adminOutput($postId)
1383 {
1384 $output = "";
1385
1386 if (!$this->atAdminPanel) {
1387 $uamOptions = $this->getAdminOptions();
1388
1389 if ($uamOptions['blog_admin_hint'] == 'true') {
1390 global $current_user;
1391
1392 $curUserdata = get_userdata($current_user->ID);
1393
1394 if (!isset($curUserdata->user_level)) {
1395 return $output;
1396 }
1397
1398 $uamAccessHandler = &$this->getAccessHandler();
1399
1400 if (count($uamAccessHandler->getUserGroupsForPost($postId)) > 0) {
1401 $output .= $uamOptions['blog_admin_hint_text'];
1402 }
1403 }
1404 }
1405
1406 return $output;
1407 }
1408
1409 /**
1410 * The function for the edit_post_link filter.
1411 *
1412 * @param string $link The edit link.
1413 * @param integer $postId The id of the post.
1414 *
1415 * @return string
1416 */
1417 function showGroupMembership($link, $postId)
1418 {
1419 $uamAccessHandler = &$this->getAccessHandler();
1420 $groups = $uamAccessHandler->getUserGroupsForPost($postId);
1421
1422 if (count($groups) > 0) {
1423 $link .= ' | '.TXT_ASSIGNED_GROUPS.': ';
1424
1425 foreach ($groups as $group) {
1426 $link .= $group->getGroupName().', ';
1427 }
1428
1429 $link = rtrim($link, ', ');
1430 }
1431
1432 return $link;
1433 }
1434
1435 /**
1436 * Returns the login bar.
1437 *
1438 * @return string
1439 */
1440 function getLoginBarHtml()
1441 {
1442 if (!is_user_logged_in()) {
1443 return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
1444 }
1445
1446 return '';
1447 }
1448
1449
1450 /*
1451 * Functions for the redirection and files.
1452 */
1453
1454 /**
1455 * Redirects to a page or to content.
1456 *
1457 * @return null
1458 */
1459 function redirect()
1460 {
1461 $uamOptions = $this->getAdminOptions();
1462
1463 if (isset($_GET['getfile'])) {
1464 $fileUrl = $_GET['getfile'];
1465 }
1466
1467 $emptyId = null;
1468 $post = get_post($emptyId);
1469
1470 if ($uamOptions['redirect'] != 'false'
1471 && !$this->getAccessHandler()->checkPostAccess($post->ID)
1472 && !$this->atAdminPanel
1473 && !isset($fileUrl)
1474 ) {
1475 $this->redirectUser();
1476 } elseif (isset($fileUrl)) {
1477 $permaStruc = get_option('permalink_structure');
1478
1479 if (!empty($permaStruc)) {
1480 $uploadDir = wp_upload_dir();
1481 $fileUrl = $uploadDir['baseurl'].'/'.$fileUrl;
1482 }
1483
1484 $this->getFile($fileUrl);
1485 }
1486 }
1487
1488 /**
1489 * Redirects the user to his destination.
1490 *
1491 * @return null
1492 */
1493 function redirectUser()
1494 {
1495 global $wp_query;
1496
1497 $postToShow = false;
1498 $posts = $wp_query->get_posts();
1499
1500 if (isset($posts)) {
1501 foreach ($posts as $post) {
1502 if ($this->getAccessHandler()->checkPostAccess($post->ID)) {
1503 $postToShow = true;
1504 break;
1505 }
1506 }
1507 }
1508
1509 if (!$postToShow) {
1510 $uamOptions = $this->getAdminOptions();
1511
1512 if ($uamOptions['redirect'] == 'blog') {
1513 $url = home_url('/');
1514 } elseif ($uamOptions['redirect'] == 'custom_page') {
1515 $post = get_post($uamOptions['redirect_custom_page']);
1516 $url = $post->guid;
1517 } elseif ($uamOptions['redirect'] == 'custom_url') {
1518 $url = $uamOptions['redirect_custom_url'];
1519 }
1520
1521 if ($url != "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]) {
1522 wp_redirect($url);
1523 }
1524 }
1525 }
1526
1527 /**
1528 * Delivers the content of the requestet file.
1529 *
1530 * @param string $url The file url.
1531 *
1532 * @return null
1533 */
1534 function getFile($url)
1535 {
1536 $post = get_post($this->getAttachmentIdByUrl($url));
1537
1538 if ($post !== null) {
1539 $file = null;
1540 } else {
1541 return null;
1542 }
1543
1544 if ($post->post_type == 'attachment'
1545 && $this->getAccessHandler()->checkPostAccess($post->ID)
1546 ) {
1547 $uploadDir = wp_upload_dir();
1548 $file = $uploadDir['basedir'].'/'.str_replace(
1549 $uploadDir['baseurl'],
1550 '',
1551 $url
1552 );
1553 } else if (wp_attachment_is_image($post->ID)) {
1554 $file = UAM_REALPATH.'gfx/noAccessPic.png';
1555 } else {
1556 wp_die(TXT_NO_RIGHTS);
1557 }
1558
1559 //Deliver content
1560 if (file_exists($file)) {
1561 $fileName = basename($file);
1562
1563 /**
1564 * This only for compatibility
1565 * mime_content_type has been deprecated as the PECL extension Fileinfo
1566 * provides the same functionality (and more) in a much cleaner way.
1567 */
1568 if (function_exists('finfo_open')) {
1569 $finfo = finfo_open(FILEINFO_MIME);
1570
1571 if (!$finfo) {
1572 wp_die(TXT_FILEINFO_DB_ERROR);
1573 }
1574
1575 $fileType = finfo_file($finfo, $file);
1576 } else {
1577 $fileType = mime_content_type($file);
1578 }
1579
1580 header('Content-Description: File Transfer');
1581 header('Content-Type: '.$fileType);
1582 header('Content-Length: '.filesize($file));
1583 header('Content-Transfer-Encoding: binary');
1584 header('Expires: 0');
1585
1586 if (!wp_attachment_is_image($post->ID)) {
1587 header('Content-Disposition: attachment; filename='.basename($file));
1588 }
1589
1590 if ($uamOptions['download_type'] == 'fopen'
1591 && !wp_attachment_is_image($post->ID)
1592 ) {
1593 $fp = fopen($file, 'rb');
1594
1595 while (!feof($fp)) {
1596 set_time_limit(30);
1597 $buffer = fread($fp, 1024);
1598 echo $buffer;
1599 }
1600
1601 exit;
1602 } else {
1603 ob_clean();
1604 flush();
1605 readfile($file);
1606 exit;
1607 }
1608 } else {
1609 wp_die(TXT_FILE_NOT_FOUND_ERROR);
1610 }
1611 }
1612
1613 /**
1614 * Returns the url for a locked file.
1615 *
1616 * @param string $url The base url.
1617 * @param integer $id The id of the file.
1618 *
1619 * @return string
1620 */
1621 function getFileUrl($url, $id)
1622 {
1623 $uamOptions = $this->getAdminOptions();
1624 $permaStruc = get_option('permalink_structure');
1625
1626 if (empty($permaStruc)
1627 && $uamOptions['lock_file'] == 'true'
1628 ) {
1629 $post = &get_post($id);
1630
1631 $type = explode("/", $post->post_mime_type);
1632 $type = $type[1];
1633
1634 $fileTypes = explode(
1635 ",",
1636 $uamOptions['locked_file_types']
1637 );
1638
1639 if (in_array($type, $fileTypes)
1640 || $uamOptions['lock_file_types'] == 'all'
1641 ) {
1642 $url = home_url('/').'?getfile='.$url;
1643 }
1644 }
1645
1646 return $url;
1647 }
1648
1649 /**
1650 * Returns the post by the given url.
1651 *
1652 * @param string $url The url of the post(attachment).
1653 *
1654 * @return object The post.
1655 */
1656 function getAttachmentIdByUrl($url)
1657 {
1658 //Filter editstring
1659 $newUrl = preg_split("/-e[0-9]*/", $url);
1660
1661 if (count($newUrl) == 2) {
1662 $newUrl = $newUrl[0].$newUrl[1];
1663 } else {
1664 $newUrl = $newUrl[0];
1665 }
1666
1667 //Filter size
1668 $newUrl = preg_split("/-[0-9]*x[0-9]*/", $newUrl);
1669
1670 if (count($newUrl) == 2) {
1671 $newUrl = $newUrl[0].$newUrl[1];
1672 } else {
1673 $newUrl = $newUrl[0];
1674 }
1675
1676 global $wpdb;
1677 $dbPost = $wpdb->get_row(
1678 "SELECT ID
1679 FROM ".$wpdb->prefix."posts
1680 WHERE guid = '" . $newUrl . "'
1681 LIMIT 1",
1682 ARRAY_A
1683 );
1684
1685 if ($dbPost) {
1686 return $dbPost['ID'];
1687 }
1688
1689 return null;
1690 }
1691 }