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

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