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

2,321 lines 66.0 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('manage_user_groups')) {
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('manage_user_groups')
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', 'user-access-manager');
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 $post = get_post($postId);
1130
1131 $wpdb->query(
1132 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1133 WHERE object_id = '".$postId."'
1134 AND object_type = '".$post->post_type."'"
1135 );
1136 }
1137
1138 /**
1139 * The function for the media_meta action.
1140 *
1141 * @param string $meta The meta.
1142 * @param object $post The post.
1143 *
1144 * @return string
1145 */
1146 public function showMediaFile($meta = '', $post = null)
1147 {
1148 $content = $meta;
1149 $content .= '</td></tr><tr>';
1150 $content .= '<th class="label">';
1151 $content .= '<label>'.TXT_UAM_SET_UP_USERGROUPS.'</label>';
1152 $content .= '</th>';
1153 $content .= '<td class="field">';
1154 $content .= $this->getIncludeContents(
1155 UAM_REALPATH.'tpl/postEditForm.php',
1156 $post->ID
1157 );
1158
1159 return $content;
1160 }
1161
1162
1163 /*
1164 * Functions for the user actions.
1165 */
1166
1167 /**
1168 * The function for the manage_users_columns filter.
1169 *
1170 * @param array $defaults The table headers.
1171 *
1172 * @return array
1173 */
1174 public function addUserColumnsHeader($defaults)
1175 {
1176 $defaults['uam_access'] = __('uam user groups');
1177 return $defaults;
1178 }
1179
1180 /**
1181 * The function for the manage_users_custom_column action.
1182 *
1183 * @param unknown $empty An empty string from wordpress? What the hell?!?
1184 * @param string $columnName The column name.
1185 * @param integer $id The id.
1186 *
1187 * @return String
1188 */
1189 public function addUserColumn($empty, $columnName, $id)
1190 {
1191 if ($columnName == 'uam_access') {
1192 return $this->getIncludeContents(
1193 UAM_REALPATH.'tpl/userColumn.php',
1194 $id,
1195 'user'
1196 );
1197 }
1198 }
1199
1200 /**
1201 * The function for the edit_user_profile action.
1202 *
1203 * @return null
1204 */
1205 public function showUserProfile()
1206 {
1207 echo $this->getIncludeContents(UAM_REALPATH.'tpl/userProfileEditForm.php');
1208 }
1209
1210 /**
1211 * The function for the profile_update action.
1212 *
1213 * @param integer $userId The user id.
1214 *
1215 * @return null
1216 */
1217 public function saveUserData($userId)
1218 {
1219 $this->_saveObjectData('user', $userId);
1220 }
1221
1222 /**
1223 * The function for the delete_user action.
1224 *
1225 * @param integer $userId The user id.
1226 *
1227 * @return null
1228 */
1229 public function removeUserData($userId)
1230 {
1231 global $wpdb;
1232
1233 $wpdb->query(
1234 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1235 WHERE object_id = ".$userId."
1236 AND object_type = 'user'"
1237 );
1238 }
1239
1240
1241 /*
1242 * Functions for the category actions.
1243 */
1244
1245 /**
1246 * The function for the manage_categories_columns filter.
1247 *
1248 * @param array $defaults The table headers.
1249 *
1250 * @return array
1251 */
1252 public function addCategoryColumnsHeader($defaults)
1253 {
1254 $defaults['uam_access'] = __('Access', 'user-access-manager');
1255 return $defaults;
1256 }
1257
1258 /**
1259 * The function for the manage_categories_custom_column action.
1260 *
1261 * @param unknown $empty An empty string from wordpress? What the hell?!?
1262 * @param string $columnName The column name.
1263 * @param integer $id The id.
1264 *
1265 * @return String
1266 */
1267 public function addCategoryColumn($empty, $columnName, $id)
1268 {
1269 if ($columnName == 'uam_access') {
1270 return $this->getIncludeContents(
1271 UAM_REALPATH.'tpl/objectColumn.php',
1272 $id,
1273 'category'
1274 );
1275 }
1276 }
1277
1278 /**
1279 * The function for the edit_category_form action.
1280 *
1281 * @param object $category The category.
1282 *
1283 * @return null
1284 */
1285 public function showCategoryEditForm($category)
1286 {
1287 include UAM_REALPATH.'tpl/categoryEditForm.php';
1288 }
1289
1290 /**
1291 * The function for the edit_category action.
1292 *
1293 * @param integer $categoryId The category id.
1294 *
1295 * @return null
1296 */
1297 public function saveCategoryData($categoryId)
1298 {
1299 $this->_saveObjectData('category', $categoryId);
1300 }
1301
1302 /**
1303 * The function for the delete_category action.
1304 *
1305 * @param integer $categoryId The id of the category.
1306 *
1307 * @return null
1308 */
1309 public function removeCategoryData($categoryId)
1310 {
1311 //TODO
1312 global $wpdb;
1313
1314 $wpdb->query(
1315 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1316 WHERE object_id = ".$categoryId."
1317 AND object_type = 'category'"
1318 );
1319 }
1320
1321
1322 /*
1323 * Functions for the pluggable object actions.
1324 */
1325
1326 /**
1327 * The function for the pluggable save action.
1328 *
1329 * @param string $objectType The name of the pluggable object.
1330 * @param integer $objectId The pluggable object id.
1331 * @param array $userGroups The user groups for the object.
1332 *
1333 * @return null
1334 */
1335 public function savePlObjectData($objectType, $objectId, $userGroups = null)
1336 {
1337 $this->_saveObjectData($objectType, $objectId, $userGroups);
1338 }
1339
1340 /**
1341 * The function for the pluggable remove action.
1342 *
1343 * @param string $objectName The name of the pluggable object.
1344 * @param integer $objectId The pluggable object id.
1345 *
1346 * @return null
1347 */
1348 public function removePlObjectData($objectName, $objectId)
1349 {
1350 global $wpdb;
1351
1352 $wpdb->query(
1353 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1354 WHERE user_id = ".$userId."
1355 AND object_type = ".$objectName
1356 );
1357 }
1358
1359 /**
1360 * Returns the group selection form for pluggable objects.
1361 *
1362 * @param string $objectType The object type.
1363 * @param integer $objectId The id of the object.
1364 * @param string $groupsFormName The name of the form which contains the groups.
1365 *
1366 * @return string;
1367 */
1368 public function showPlGroupSelectionForm($objectType, $objectId, $groupsFormName = null)
1369 {
1370 $fileName = UAM_REALPATH.'tpl/groupSelectionForm.php';
1371 $uamUserGroups = $this->getAccessHandler()->getUserGroups();
1372 $userGroupsForObject = $this->getAccessHandler()->getUserGroupsForObject(
1373 $objectType,
1374 $objectId
1375 );
1376
1377 if (is_file($fileName)) {
1378 ob_start();
1379 include $fileName;
1380 $contents = ob_get_contents();
1381 ob_end_clean();
1382
1383 return $contents;
1384 }
1385
1386 return '';
1387 }
1388
1389 /**
1390 * Returns the column for a pluggable object.
1391 *
1392 * @param string $objectType The object type.
1393 * @param integer $objectId The object id.
1394 *
1395 * @return string
1396 */
1397 public function getPlColumn($objectType, $objectId)
1398 {
1399 return $this->getIncludeContents(
1400 UAM_REALPATH.'tpl/objectColumn.php',
1401 $objectId,
1402 $objectType
1403 );
1404 }
1405
1406
1407 /*
1408 * Functions for the blog content.
1409 */
1410
1411 /**
1412 * Manipulates the wordpress query object to filter content.
1413 *
1414 * @param object $wpQuery The wordpress query object.
1415 *
1416 * @return null
1417 */
1418 public function parseQuery($wpQuery)
1419 {
1420 $uamOptions = $this->getAdminOptions();
1421
1422 if ($uamOptions['hide_post'] == 'true') {
1423 $uamAccessHandler = &$this->getAccessHandler();
1424 $excludedPosts = $uamAccessHandler->getExcludedPosts();
1425
1426 if (count($excludedPosts) > 0) {
1427 $wpQuery->query_vars['post__not_in'] = array_merge(
1428 $wpQuery->query_vars['post__not_in'],
1429 $excludedPosts
1430 );
1431 }
1432 }
1433 }
1434
1435 /**
1436 * Modifies the content of the post by the given settings.
1437 *
1438 * @param object $post The current post.
1439 *
1440 * @return object
1441 */
1442 private function _getPost($post)
1443 {
1444 $uamOptions = $this->getAdminOptions();
1445 $uamAccessHandler = &$this->getAccessHandler();
1446
1447 $postType = $post->post_type;
1448
1449 $postableTypes = $uamAccessHandler->getPostableTypes();
1450
1451 if (in_array($postType, $postableTypes)
1452 && $postType != 'post'
1453 && $postType != 'page'
1454 ) {
1455 $postType = 'post';
1456 } elseif ($postType != 'post' && $postType != 'page') {
1457 return $post;
1458 }
1459
1460 if ($uamOptions['hide_'.$postType] == 'true'
1461 || $this->atAdminPanel()
1462 ) {
1463 if ($uamAccessHandler->checkObjectAccess($post->post_type, $post->ID)) {
1464 $post->post_title .= $this->adminOutput($post->post_type, $post->ID);
1465
1466 return $post;
1467 }
1468 } else {
1469 if (!$uamAccessHandler->checkObjectAccess($post->post_type, $post->ID)) {
1470 $post->isLocked = true;
1471
1472 $uamPostContent = $uamOptions[$postType.'_content'];
1473 $uamPostContent = str_replace(
1474 "[LOGIN_FORM]",
1475 $this->getLoginBarHtml(),
1476 $uamPostContent
1477 );
1478
1479 if ($uamOptions['hide_'.$postType.'_title'] == 'true') {
1480 $post->post_title = $uamOptions[$postType.'_title'];
1481 }
1482
1483 if ($uamOptions[$postType.'_comments_locked'] == 'false') {
1484 $post->comment_status = 'close';
1485 }
1486
1487 if ($uamOptions['show_post_content_before_more'] == 'true'
1488 && $postType == "post"
1489 && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches)
1490 ) {
1491 $post->post_content = explode(
1492 $matches[0],
1493 $post->post_content,
1494 2
1495 );
1496 $uamPostContent
1497 = $post->post_content[0] . " " . $uamPostContent;
1498 }
1499
1500 $post->post_content = $uamPostContent;
1501 }
1502
1503 $post->post_title .= $this->adminOutput($post->post_type, $post->ID);
1504
1505 return $post;
1506 }
1507
1508 return null;
1509 }
1510
1511 /**
1512 * The function for the the_posts filter.
1513 *
1514 * @param arrray $posts The posts.
1515 *
1516 * @return array
1517 */
1518 public function showPost($posts = array())
1519 {
1520 $showPosts = array();
1521 $uamOptions = $this->getAdminOptions();
1522
1523 if (!is_feed()
1524 || ($uamOptions['protect_feed'] == 'true' && is_feed())
1525 ) {
1526 foreach ($posts as $post) {
1527 if ($post !== null) {
1528 $post = $this->_getPost($post);
1529 }
1530
1531 if ($post !== null) {
1532 $showPosts[] = $post;
1533 }
1534 }
1535
1536 $posts = $showPosts;
1537 }
1538
1539 return $posts;
1540 }
1541
1542 /**
1543 * The function for the posts_where_paged filter.
1544 *
1545 * @param string $sql The where sql statment.
1546 *
1547 * @return string
1548 */
1549 public function showPostSql($sql)
1550 {
1551 $uamAccessHandler = &$this->getAccessHandler();
1552 $uamOptions = $this->getAdminOptions();
1553
1554 if ($uamOptions['hide_post'] == 'true') {
1555 global $wpdb;
1556 $excludedPosts = $uamAccessHandler->getExcludedPosts();
1557
1558 if (count($excludedPosts) > 0) {
1559 $excludedPostsStr = implode(",", $excludedPosts);
1560 $sql .= " AND $wpdb->posts.ID NOT IN($excludedPostsStr) ";
1561 }
1562 }
1563
1564 return $sql;
1565 }
1566
1567 /**
1568 * The function for the wp_get_nav_menu_items filter.
1569 *
1570 * @param array $items The menu item.
1571 *
1572 * @return array
1573 */
1574 public function showCustomMenu($items)
1575 {
1576 $showItems = array();
1577
1578 foreach ($items as $item) {
1579 if ($item->object == 'post'
1580 || $item->object == 'page'
1581 ) {
1582 $object = get_post($item->object_id);
1583
1584 if ($object !== null) {
1585 $post = $this->_getPost($object);
1586 }
1587
1588 if ($post !== null) {
1589 if (isset($post->isLocked)) {
1590 $item->title = $post->post_title;
1591 }
1592
1593 $item->title .= $this->adminOutput(
1594 $item->object,
1595 $item->object_id
1596 );
1597
1598 $showItems[] = $item;
1599 }
1600 } elseif ($item->object == 'category') {
1601 $object = get_category($item->object_id);
1602 $category = $this->_getTerm('category', $object);
1603
1604 if ($category !== null
1605 && !$category->isEmpty
1606 ) {
1607 $item->title .= $this->adminOutput(
1608 $item->object,
1609 $item->object_id
1610 );
1611 $showItems[] = $item;
1612 }
1613 } else {
1614 $showItems[] = $item;
1615 }
1616 }
1617
1618 return $showItems;
1619 }
1620
1621 /**
1622 * The function for the comments_array filter.
1623 *
1624 * @param array $comments The comments.
1625 *
1626 * @return array
1627 */
1628 public function showComment($comments = array())
1629 {
1630 $showComments = array();
1631 $uamOptions = $this->getAdminOptions();
1632 $uamAccessHandler = &$this->getAccessHandler();
1633
1634 foreach ($comments as $comment) {
1635 $post = get_post($comment->comment_post_ID);
1636 $postType = $post->post_type;
1637
1638 if ($uamOptions['hide_'.$postType.'_comment'] == 'true'
1639 || $uamOptions['hide_'.$postType] == 'true'
1640 || $this->atAdminPanel()
1641 ) {
1642 if ($uamAccessHandler->checkObjectAccess($post->post_type, $post->ID)) {
1643 $showComments[] = $comment;
1644 }
1645 } else {
1646 if (!$uamAccessHandler->checkObjectAccess($post->post_type, $post->ID)) {
1647 $comment->comment_content
1648 = $uamOptions[$postType.'_comment_content'];
1649 }
1650
1651 $showComments[] = $comment;
1652 }
1653 }
1654
1655 $comments = $showComments;
1656
1657 return $comments;
1658 }
1659
1660 /**
1661 * The function for the get_pages filter.
1662 *
1663 * @param array $pages The pages.
1664 *
1665 * @return array
1666 */
1667 public function showPage($pages = array())
1668 {
1669 $showPages = array();
1670 $uamOptions = $this->getAdminOptions();
1671 $uamAccessHandler = &$this->getAccessHandler();
1672
1673 foreach ($pages as $page) {
1674 if ($uamOptions['hide_page'] == 'true'
1675 || $this->atAdminPanel()
1676 ) {
1677 if ($uamAccessHandler->checkObjectAccess($page->post_type, $page->ID)) {
1678 $page->post_title .= $this->adminOutput(
1679 $page->post_type,
1680 $page->ID
1681 );
1682 $showPages[] = $page;
1683 }
1684 } else {
1685 if (!$uamAccessHandler->checkObjectAccess($page->post_type, $page->ID)) {
1686 if ($uamOptions['hide_page_title'] == 'true') {
1687 $page->post_title = $uamOptions['page_title'];
1688 }
1689
1690 $page->post_content = $uamOptions['page_content'];
1691 }
1692
1693 $page->post_title .= $this->adminOutput($page->post_type, $page->ID);
1694 $showPages[] = $page;
1695 }
1696 }
1697
1698 $pages = $showPages;
1699
1700 return $pages;
1701 }
1702
1703 /**
1704 * Modifies the content of the term by the given settings.
1705 *
1706 * @param string $termType The type of the term.
1707 * @param object $term The current term.
1708 *
1709 * @return object
1710 */
1711 private function _getTerm($termType, $term)
1712 {
1713 $uamOptions = $this->getAdminOptions();
1714 $uamAccessHandler = &$this->getAccessHandler();
1715
1716 $term->isEmpty = false;
1717
1718 $term->name .= $this->adminOutput('term', $term->term_id);
1719
1720 if ($termType == 'post_tag'
1721 || $termType == 'category'
1722 && $uamAccessHandler->checkObjectAccess('category', $term->term_id)
1723 ) {
1724 if ($this->atAdminPanel() == false
1725 && ($uamOptions['hide_post'] == 'true'
1726 || $uamOptions['hide_page'] == 'true')
1727 ) {
1728 $termRequest = $term->term_id;
1729 $termRequestType = $termType;
1730
1731 if ($termType == 'post_tag') {
1732 $termRequest = $term->slug;
1733 $termRequestType = 'tag';
1734 }
1735
1736 $args = array(
1737 'numberposts' => - 1,
1738 $termRequestType => $termRequest
1739 );
1740
1741 $termPosts = get_posts($args);
1742 $term->count = count($termPosts);
1743
1744 if (isset($termPosts)) {
1745 foreach ($termPosts as $post) {
1746 if ($uamOptions['hide_'.$post->post_type] == 'true'
1747 && !$uamAccessHandler->checkObjectAccess($post->post_type, $post->ID)
1748 ) {
1749 $term->count--;
1750 }
1751 }
1752 }
1753
1754 //For post_tags
1755 if ($termType == 'post_tag'
1756 && $term->count <= 0
1757 ) {
1758 return null;
1759 }
1760
1761 //For categories
1762 if ($term->count <= 0
1763 && $uamOptions['hide_empty_categories'] == 'true'
1764 && ($term->taxonomy == "term"
1765 || $term->taxonomy == "category")
1766 ) {
1767 $term->isEmpty = true;
1768 }
1769
1770 if ($uamOptions['lock_recursive'] == 'false') {
1771 $curCategory = $term;
1772
1773 while ($curCategory->parent != 0) {
1774 $curCategory = get_term($curCategory->parent);
1775
1776 if ($uamAccessHandler->checkObjectAccess('term', $curCategory->term_id)) {
1777 $term->parent = $curCategory->term_id;
1778 break;
1779 }
1780 }
1781 }
1782
1783 return $term;
1784 } else {
1785 return $term;
1786 }
1787 }
1788
1789 return null;
1790 }
1791
1792 /**
1793 * The function for the get_terms filter.
1794 *
1795 * @param array $terms The terms.
1796 * @param array $args The given arguments.
1797 *
1798 * @return array
1799 */
1800 public function showTerms($terms = array(), $args = array())
1801 {
1802 $uamOptions = $this->getAdminOptions();
1803 $uamAccessHandler = &$this->getAccessHandler();
1804
1805 $showTerms = array();
1806
1807 $uamOptions = $this->getAdminOptions();
1808
1809 foreach ($terms as $term) {
1810 if (!is_object($term)) {
1811 return $terms;
1812 }
1813
1814 if ($term->taxonomy == 'category') {
1815 $term = $this->_getTerm('category', $term);
1816 } elseif ($term->taxonomy == 'post_tag') {
1817 $term = $this->_getTerm('post_tag', $term);
1818 }
1819
1820 if ($term !== null) {
1821 if (!isset($term->isEmpty)
1822 || !$term->isEmpty
1823 ) {
1824 $showTerms[$term->term_id] = $term;
1825 }
1826 }
1827 }
1828
1829 foreach ($terms as $key => $term) {
1830 if (!array_key_exists($term->term_id, $showTerms)) {
1831 unset($terms[$key]);
1832 }
1833 }
1834
1835 return $terms;
1836 }
1837
1838 /**
1839 * The function for the get_previous_post_where and
1840 * the get_next_post_where filter.
1841 *
1842 * @param string $sql The current sql string.
1843 *
1844 * @return string
1845 */
1846 public function showNextPreviousPost($sql)
1847 {
1848 $uamAccessHandler = &$this->getAccessHandler();
1849 $uamOptions = $this->getAdminOptions();
1850
1851 if ($uamOptions['hide_post'] == 'true') {
1852 $excludedPosts = $uamAccessHandler->getExcludedPosts();
1853
1854 if (count($excludedPosts) > 0) {
1855 $excludedPostsStr = implode(",", $excludedPosts);
1856 $sql.= " AND p.ID NOT IN($excludedPostsStr) ";
1857 }
1858 }
1859
1860 return $sql;
1861 }
1862
1863 /**
1864 * Returns the admin hint.
1865 *
1866 * @param string $objectType The object type.
1867 * @param integer $objectId The object id we want to check.
1868 *
1869 * @return string
1870 */
1871 public function adminOutput($objectType, $objectId)
1872 {
1873 $output = "";
1874
1875 if (!$this->atAdminPanel()) {
1876 $uamOptions = $this->getAdminOptions();
1877
1878 if ($uamOptions['blog_admin_hint'] == 'true') {
1879 global $current_user;
1880
1881 $curUserdata = get_userdata($current_user->ID);
1882
1883 if (!isset($curUserdata->user_level)) {
1884 return $output;
1885 }
1886
1887 $uamAccessHandler = &$this->getAccessHandler();
1888
1889 if ($uamAccessHandler->userIsAdmin($current_user->ID)
1890 && count($uamAccessHandler->getUserGroupsForObject($objectType, $objectId)) > 0
1891 ) {
1892 $output .= $uamOptions['blog_admin_hint_text'];
1893 }
1894 }
1895 }
1896
1897 return $output;
1898 }
1899
1900 /**
1901 * The function for the edit_post_link filter.
1902 *
1903 * @param string $link The edit link.
1904 * @param integer $postId The id of the post.
1905 *
1906 * @return string
1907 */
1908 public function showGroupMembership($link, $postId)
1909 {
1910 $uamAccessHandler = &$this->getAccessHandler();
1911 $groups = $uamAccessHandler->getUserGroupsForObject('post', $postId);
1912
1913 if (count($groups) > 0) {
1914 $link .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
1915
1916 foreach ($groups as $group) {
1917 $link .= $group->getGroupName().', ';
1918 }
1919
1920 $link = rtrim($link, ', ');
1921 }
1922
1923 return $link;
1924 }
1925
1926 /**
1927 * Returns the login bar.
1928 *
1929 * @return string
1930 */
1931 public function getLoginBarHtml()
1932 {
1933 if (!is_user_logged_in()) {
1934 return $this->getIncludeContents(UAM_REALPATH.'tpl/loginBar.php');
1935 }
1936
1937 return '';
1938 }
1939
1940
1941 /*
1942 * Functions for the redirection and files.
1943 */
1944
1945 /**
1946 * Returns ture if permalinks are active otherwise false.
1947 *
1948 * @return boolean
1949 */
1950 public function isPermalinksActive()
1951 {
1952 $permaStruc = get_option('permalink_structure');
1953
1954 if (empty($permaStruc)) {
1955 return false;
1956 } else {
1957 return true;
1958 }
1959 }
1960
1961 /**
1962 * Redirects to a page or to content.
1963 *
1964 * @param string $headers The headers which are given from wordpress.
1965 * @param object $pageParams The params of the current page.
1966 *
1967 * @return null
1968 */
1969 public function redirect($headers, $pageParams)
1970 {
1971 $uamOptions = $this->getAdminOptions();
1972
1973 if (isset($_GET['uamgetfile'])
1974 && isset($_GET['uamfiletype'])
1975 ) {
1976 $fileUrl = $_GET['uamgetfile'];
1977 $fileType = $_GET['uamfiletype'];
1978 $this->getFile($fileType, $fileUrl);
1979 } elseif (!$this->atAdminPanel() && $uamOptions['redirect'] != 'false') {
1980 $object = null;
1981
1982 if (isset($pageParams->query_vars['p'])) {
1983 $object = get_post($pageParams->query_vars['p']);
1984 $objectType = $object->post_type;
1985 $objectId = $object->ID;
1986 } elseif (isset($pageParams->query_vars['page_id'])) {
1987 $object = get_post($pageParams->query_vars['page_id']);
1988 $objectType = $object->post_type;
1989 $objectId = $object->ID;
1990 } elseif (isset($pageParams->query_vars['cat_id'])) {
1991 $object = get_category($pageParams->query_vars['cat_id']);
1992 $objectType = 'category';
1993 $objectId = $object->term_id;
1994 }
1995
1996 if ($object === null
1997 ||$object !== null
1998 && !$this->getAccessHandler()->checkObjectAccess($objectType, $objectId)
1999 ) {
2000 $this->redirectUser($object);
2001 }
2002 }
2003 }
2004
2005 /**
2006 * Returns the current url.
2007 *
2008 * @return string
2009 */
2010 public function getCurrentUrl()
2011 {
2012 if (!isset($_SERVER['REQUEST_URI'])) {
2013 $serverrequri = $_SERVER['PHP_SELF'];
2014 } else {
2015 $serverrequri = $_SERVER['REQUEST_URI'];
2016 }
2017
2018 $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
2019 $protocolArray = explode("/", strtolower($_SERVER["SERVER_PROTOCOL"]));
2020 $protocol = $protocolArray[0].$s;
2021 $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
2022
2023 $fullUrl = $protocol."://".$_SERVER['SERVER_NAME'].$port.$serverrequri;
2024
2025 return $fullUrl;
2026 }
2027
2028 /**
2029 * Redirects the user to his destination.
2030 *
2031 * @param object $object The current object we want to access.
2032 *
2033 * @return null
2034 */
2035 public function redirectUser($object = null)
2036 {
2037 global $wp_query;
2038
2039 $postToShow = false;
2040 $posts = $wp_query->get_posts();
2041
2042 if ($object === null
2043 && isset($posts)
2044 ) {
2045 foreach ($posts as $post) {
2046 if ($this->getAccessHandler()->checkObjectAccess($post->post_type, $post->ID)) {
2047 $postToShow = true;
2048 break;
2049 }
2050 }
2051 }
2052
2053 if (!$postToShow) {
2054 $uamOptions = $this->getAdminOptions();
2055
2056 if ($uamOptions['redirect'] == 'blog') {
2057 $url = home_url('/');
2058 } elseif ($uamOptions['redirect'] == 'custom_page') {
2059 $post = get_post($uamOptions['redirect_custom_page']);
2060 $url = $post->guid;
2061 } elseif ($uamOptions['redirect'] == 'custom_url') {
2062 $url = $uamOptions['redirect_custom_url'];
2063 }
2064
2065 if ($url != $this->getCurrentUrl()) {
2066 wp_redirect($url);
2067 exit;
2068 }
2069 }
2070 }
2071
2072 /**
2073 * Delivers the content of the requestet file.
2074 *
2075 * @param string $objectType The type of the requested file.
2076 * @param string $objectUrl The file url.
2077 *
2078 * @return null
2079 */
2080 public function getFile($objectType, $objectUrl)
2081 {
2082 $object = $this->_getFileSettingsByType($objectType, $objectUrl);
2083
2084 if ($object === null) {
2085 return null;
2086 }
2087
2088 $file = null;
2089
2090 if ($this->getAccessHandler()->checkObjectAccess($object->type, $object->id)) {
2091 $file = $object->file;
2092 } elseif ($object->isImage) {
2093 $file = UAM_REALPATH.'gfx/noAccessPic.png';
2094 } else {
2095 wp_die(TXT_UAM_NO_RIGHTS);
2096 }
2097
2098 //Deliver content
2099 if (file_exists($file)) {
2100 $fileName = basename($file);
2101
2102 /*
2103 * This only for compatibility
2104 * mime_content_type has been deprecated as the PECL extension Fileinfo
2105 * provides the same functionality (and more) in a much cleaner way.
2106 */
2107 $ext = strtolower(array_pop(explode('.', $fileName)));
2108
2109 if (function_exists('finfo_open')) {
2110 $finfo = finfo_open(FILEINFO_MIME);
2111 $fileMimeType = finfo_file($finfo, $file);
2112 finfo_close($finfo);
2113 } elseif (function_exists('mime_content_type')) {
2114 $fileMimeType = mime_content_type($file);
2115 } elseif (array_key_exists($ext, $this->mimeTypes)) {
2116 $fileMimeType = $this->mimeTypes[$ext];
2117 } else {
2118 $fileMimeType = 'application/octet-stream';
2119 }
2120
2121 header('Content-Description: File Transfer');
2122 header('Content-Type: '.$fileMimeType);
2123
2124 if (!$object->isImage) {
2125 $baseName = str_replace(' ', '_', basename($file));
2126
2127 header('Content-Disposition: attachment; filename="'.$baseName.'"');
2128 }
2129
2130 header('Content-Transfer-Encoding: binary');
2131 header('Content-Length: '.filesize($file));
2132
2133 $uamOptions = $this->getAdminOptions();
2134
2135 if ($uamOptions['download_type'] == 'fopen'
2136 && !$object->isImage
2137 ) {
2138 $fp = fopen($file, 'r');
2139
2140 //TODO find better solution (prevent '\n' / '0A')
2141 ob_clean();
2142 flush();
2143
2144 while (!feof($fp)) {
2145 if (!ini_get('safe_mode')) {
2146 set_time_limit(30);
2147 }
2148 $buffer = fread($fp, 1024);
2149 echo $buffer;
2150 }
2151
2152 exit;
2153 } else {
2154 ob_clean();
2155 flush();
2156 readfile($file);
2157 exit;
2158 }
2159 } else {
2160 wp_die(TXT_UAM_FILE_NOT_FOUND_ERROR);
2161 }
2162 }
2163
2164 /**
2165 * Returns the file object by the given type and url.
2166 *
2167 * @param string $objectType The type of the requested file.
2168 * @param string $objectUrl The file url.
2169 *
2170 * @return object|null
2171 */
2172 private function _getFileSettingsByType($objectType, $objectUrl)
2173 {
2174 $object = null;
2175
2176 if ($objectType == 'attachment') {
2177 $uploadDir = wp_upload_dir();
2178
2179 $multiPath = str_replace(ABSPATH, '/', $uploadDir['basedir']);
2180 $multiPath = str_replace('/files', $multiPath, $uploadDir['baseurl']);
2181
2182 if ($this->isPermalinksActive()) {
2183 //TODO Remove if not needed.
2184 //$objectUrl = $uploadDir['baseurl'].'/'.$objectUrl;
2185 $objectUrl = $multiPath.'/'.$objectUrl;
2186 }
2187
2188 $post = get_post($this->getPostIdByUrl($objectUrl));
2189
2190 if ($post !== null
2191 && $post->post_type == 'attachment'
2192 ) {
2193 $object->id = $post->ID;
2194 $object->isImage = wp_attachment_is_image($post->ID);
2195 $object->type = $objectType;
2196
2197 //TODO Remove if not needed.
2198 /*$object->file = $uploadDir['basedir'].str_replace(
2199 $uploadDir['baseurl'],
2200 '',
2201 $objectUrl
2202 );*/
2203
2204 $object->file = $uploadDir['basedir'].str_replace(
2205 $multiPath,
2206 '',
2207 $objectUrl
2208 );
2209 }
2210 } else {
2211 $plObject = $this->getAccessHandler()->getPlObject($objectType);
2212
2213 if (isset($plObject)
2214 && isset($plObject['getFileObject'])
2215 ) {
2216 $object = $plObject['reference']->{$plObject['getFileObject']}(
2217 $objectUrl
2218 );
2219 }
2220 }
2221
2222 return $object;
2223 }
2224
2225 /**
2226 * Returns the url for a locked file.
2227 *
2228 * @param string $url The base url.
2229 * @param integer $id The id of the file.
2230 *
2231 * @return string
2232 */
2233 public function getFileUrl($url, $id)
2234 {
2235 $uamOptions = $this->getAdminOptions();
2236
2237 if (!$this->isPermalinksActive()
2238 && $uamOptions['lock_file'] == 'true'
2239 ) {
2240 $post = &get_post($id);
2241
2242 $type = explode("/", $post->post_mime_type);
2243 $type = $type[1];
2244
2245 $fileTypes = explode(
2246 ",",
2247 $uamOptions['locked_file_types']
2248 );
2249
2250 if ($uamOptions['lock_file_types'] == 'all'
2251 || in_array($type, $fileTypes)
2252 ) {
2253 $url = home_url('/').'?uamfiletype=attachment&uamgetfile='.$url;
2254 }
2255 }
2256
2257 return $url;
2258 }
2259
2260 /**
2261 * Returns the post by the given url.
2262 *
2263 * @param string $url The url of the post(attachment).
2264 *
2265 * @return object The post.
2266 */
2267 public function getPostIdByUrl($url)
2268 {
2269 if (isset($this->postUrls[$url])) {
2270 return $this->postUrls[$url];
2271 }
2272
2273 $this->postUrls[$url] = null;
2274
2275 //Filter edit string
2276 $newUrl = preg_split("/-e[0-9]{1,}/", $url);
2277
2278 if (count($newUrl) == 2) {
2279 $newUrl = $newUrl[0].$newUrl[1];
2280 } else {
2281 $newUrl = $newUrl[0];
2282 }
2283
2284 //Filter size
2285 $newUrl = preg_split("/-[0-9]{1,}x[0-9]{1,}/", $newUrl);
2286
2287 if (count($newUrl) == 2) {
2288 $newUrl = $newUrl[0].$newUrl[1];
2289 } else {
2290 $newUrl = $newUrl[0];
2291 }
2292
2293 global $wpdb;
2294 $dbPost = $wpdb->get_row(
2295 "SELECT ID
2296 FROM ".$wpdb->prefix."posts
2297 WHERE guid = '" . $newUrl . "'
2298 LIMIT 1"
2299 );
2300
2301 if ($dbPost) {
2302 $this->postUrls[$url] = $dbPost->ID;
2303 }
2304
2305 return $this->postUrls[$url];
2306 }
2307
2308 /**
2309 * Caches the urls for the post for a later lookup.
2310 *
2311 * @param string $url The url of the post.
2312 * @param object $post The post object.
2313 *
2314 * @return null
2315 */
2316 public function cachePostLinks($url, $post)
2317 {
2318 $this->postUrls[$url] = $post->ID;
2319 return $url;
2320 }
2321 }