PluginProbe
User Access Manager / 1.1.4
User Access Manager v1.1.4
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / class / UserAccessManager.class.php

UserAccessManager.class.php in User Access Manager 1.1.4, at class/UserAccessManager.class.php

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