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