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

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