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

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