[Plugin: User-Cats Manager] ‘FileReader’ not found
-
Hi i can′t seem to get this running, it allways gives me this error:
Fatal error: Class ‘FileReader’ not found in (…)/wp-content/plugins/user-cats-manager/user-cats-manager.php on line 48
Has this happened to you?
Thanks
MAC
-
Hi mac,
i have the same problem.
I just insered die Line:
include($_SERVER['DOCUMENT_ROOT'] . “/wp-includes/streams.php”);
on the top (after die comments) of
/wp-content/plugins/user-cats-manager/user-cats-manager.phpand it works.
I’ve also fixed another bug of the extension (display all categories at the front page)
replacte
if ($current_user->user_level == 10) return $cats;
with
// If user is on frontend or admin is logged in show all categories if ($current_user->user_level == 10 || is_front_page()) return $cats;
as i’ve described at my blog (german!)
https://www.shofer.at/2009/07/wordpress-kategorien-nur-fur-bestimmte-benutzer-freigeben/i hope that die extension owner can integrate these bugfixes!
I have the same problem, y tri your way but i still can`t. Would you helpe me? can you show me in what line i must enter the:
include($_SERVER[‘DOCUMENT_ROOT’] . “/wp-includes/streams.php”);
and another question, excuse me please but: in “document_root” what root i have to write there?
excuse my english am latino.
thanx a lot
If you install WordPress in subfolder than you will get the problem..
Don’t insert the following line.
include($_SERVER[‘DOCUMENT_ROOT’] . “/wp-includes/streams.php”);For avoiding the all problem all the time Use and following code..
require_once(ABSPATH . ‘wp-includes/streams.php’);If you print the $_SERVER[‘DOCUMENT_ROOT’] variable then it will be easy to get the problem..
In WP >= 2.9.x there is no wp-includes/streams.php or an equivalent FileReader class anymore. Maybe using wp-includes/pomo/streams.php and replacing FileReader with POMO_CachedFileReader may work, but I haven’ tried.
This is the code I’m using with WP 2.9.x. DGmike, thanks for the idea and code base – It’s still yours, I’m just bugfixing. The code contains the needed FileReader class directly (as I didn’t had the time to find and test a suitable replacement) and some spelling changes. It still needs a complete refactoring…
It also contains an additional filter on category_save_pre – whatever the user does, the post is assigned to his categories on save. If there are no categories specified for that user, the categories selected by the user will be used.
If you don’t want that feature, just comment out the line
add_filter ('category_save_pre', array('UserCatsManager', 'categorySavePre'));
in the init() method.The code hasn’t been tested very intensively and I’m not able to provide support…
<?php /* Plugin Name: User-Cats Manager Plugin URI: https://dgmike.wordpress.com/user-cats-manager Description: Provides to admin users a way to select what categorie determined users can write. Version 2.3 has been fixed "quick & dirty" for WP >= 2.9.x as original author doesn't seem to work on the plugin anymore. Version: 2.3 Modified: 2010-01-08: HerrB Author: DGmike/HerrB Author URI: https://dgmike.wordpress.com */ // Bugfix WP < 2.9.x //require_once(ABSPATH . 'wp-includes/streams.php'); // Bugfix WP >= 2.9.x // FileReader (wp-includes/streams.php) doesn't exist in WP 2.9.x anymore // POMO_FileReader in wp-includes/pomo/streams.php would work, but doesn't provide length attribute. // POMO_CachedFileReader may work, but seems to provide unneccessary overhead class FileReader { var $_pos; var $_fd; var $_length; function FileReader($filename) { if (file_exists($filename)) { $this->_length=filesize($filename); $this->_pos = 0; $this->_fd = fopen($filename,'rb'); if (!$this->_fd) { $this->error = 3; // Cannot read file, probably permissions return false; } } else { $this->error = 2; // File doesn't exist return false; } } function read($bytes) { if ($bytes) { fseek($this->_fd, $this->_pos); // PHP 5.1.1 does not read more than 8192 bytes in one fread() // the discussions at PHP Bugs suggest it's the intended behaviour while ($bytes > 0) { $chunk = fread($this->_fd, $bytes); $data .= $chunk; $bytes -= strlen($chunk); } $this->_pos = ftell($this->_fd); return $data; } else return ''; } function seekto($pos) { fseek($this->_fd, $pos); $this->_pos = ftell($this->_fd); return $this->_pos; } function currentpos() { return $this->_pos; } function length() { return $this->_length; } function close() { fclose($this->_fd); } } // FileReader class UserCatsManager { static $wpdb; static $info; public static function init() { global $wpdb; UserCatsManager::$wpdb = $wpdb; //Outros mapeamentos UserCatsManager::$info['plugin_fpath'] = dirname(__FILE__); add_action ('admin_menu', array('UserCatsManager','options')); add_action ('load-post.php', array('UserCatsManager','loadpost')); add_filter ('get_terms', array ('UserCatsManager', 'filterCats'), 0); add_filter ('category_save_pre', array('UserCatsManager', 'categorySavePre')); // this filter is used when publishing a post } static function install (){ if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); UserCatsManager::$wpdb->query (sprintf(' CREATE TABLE %suser_cats_manager ( <code>user_id</code> INT NOT NULL, <code>term_id</code> INT NOT NULL, PRIMARY KEY (<code>user_id</code>, <code>term_id</code>) ) ', UserCatsManager::$wpdb->prefix)); } static function uninstall () { if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); UserCatsManager::$wpdb->query (sprintf (' DROP TABLE %suser_cats_manager ', UserCatsManager::$wpdb->prefix)); } static function options () { add_options_page (__('Categories and Users'), 'Categories And Users', 10, __FILE__, array('UserCatsManager','optionsMenu')); } static function optionsMenu ($nick = '') { if ($_POST['user']) $nick = $_POST['user']; if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); if ($nick) $nick = UserCatsManager::$wpdb->get_results(sprintf('SELECT * FROM %susers WHERE ID=\'%s\'', UserCatsManager::$wpdb->prefix, $nick)); $tplObj = new FileReader(UserCatsManager::$info['plugin_fpath'] . '/options.html'); $tpl = $tplObj->read($tplObj->length()); $items = array ( '{LEGEND}' => __('Choose the categories that the user can use'), '{LEGEND_BM}' => __('Choose the categories of bookmarks that the user can use'), '{ACTION}' => $_SERVER['REQUEST_URI'], '{ALL_CATS}' => UserCatsManager::allCats($nick[0]->ID), '{ALL_CATS_BM}' => UserCatsManager::allCatsBm($nick[0]->ID), '{USERS}' => UserCatsManager::allUsersSelect(), '{NICK}' => $nick[0]->ID, '{SELECT_USER}' => __('Select the user <small>(admin users have all access)</small>'), '{EDITING}' => __('Editing user ') . $nick[0]->user_nicename, '{EDIT}' => __('edit'), '{SAVE}' => __('Save'), '{SAVED}' => __(''), '{MESSAGE_DEFAULT_CATS}' => __('If the user does not choose a category, wordpress puts the default category on post and/or bookmark. Tip: give your user the privileges on the default category. To edit the default category, go to <strong>Writing Settings</strong>.') ); if ($_POST['nick']) { UserCatsManager::save($_POST['nick'], (array) $_POST['categoria']); $items['{SAVED}'] = __('<div id="message" class="updated fade"><p>The changes has been taked</p></div>'); } $tpl = str_replace("\n", '\n', $tpl); if ($nick == '') $tpl = preg_replace("/{POST}.*{\/POST}/", '', $tpl); $tpl = str_replace('\n', "\n", $tpl); $tpl = preg_replace("/{\/?POST}/", '', $tpl); foreach ($items as $key=>$value) $tpl = str_replace ($key, $value, $tpl); print $tpl; } static function getCats($n=0, $user='', $type='category'){ if ($_POST) $user = $_POST['user']; if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); $wpdb = UserCatsManager::$wpdb; $sqlCats = 'SELECT %sterms.term_id, slug, name FROM %sterms NATURAL JOIN %sterm_taxonomy WHERE taxonomy = \'%s\' AND parent = \'%s\''; $cats = $wpdb->get_results(sprintf($sqlCats, $wpdb->prefix, $wpdb->prefix, $wpdb->prefix, $type, $n), ARRAY_N); $base = '%s<li class="popular-category"><label for="ck_%s"><input id="ck_%s" type="checkbox" name="categoria[]" value="%s"%s /> %s%s</label></li>'; $return = array (); if (count($cats)) { foreach ($cats as $cat){ $uniq = uniqid(); $isSelect = $wpdb->get_results(sprintf( 'SELECT * FROM %suser_cats_manager WHERE user_id = \'%s\' AND term_id = \'%s\'', $wpdb->prefix, $user, $cat[0] ) ); $checked = count($isSelect) ? ' checked="checked"' : ''; $default = in_array($cat[0], array(get_option('default_category'), get_option('default_link_category') )) ? __(' <strong>(default)</strong>') : ''; $return[] = sprintf ($base, "\n ", $uniq, $uniq, $cat[0], $checked, $cat[1], $default); if (count(UserCatsManager::getCats($cat[0]))) $return[] = "\n<ul>" . implode ('', UserCatsManager::getCats($cat[0], $user)) . "\n</ul>\n"; } } return $return; } static function allCats($user = '') { $return = UserCatsManager::getCats(0, $user); return '<ul>'.implode ('', $return).'</ul>'; } static function allCatsBm($user = '') { $return = UserCatsManager::getCats(0, $user, 'link_category'); return '<ul>'.implode ('', $return).'</ul>'; } static function allUsersSelect($user = '') { if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); if ($_POST) $user = $_POST['user']; $wpdb = UserCatsManager::$wpdb; $res = $wpdb->get_results(sprintf('SELECT id, user_nicename FROM %susers', $wpdb->prefix)); $base = '<option value="%s"%s>%s</option>'; $return = ''; foreach ($res as $value) { $usr = new WP_User($value->id); if ((int) $usr->user_level === 10) continue; $selected = ( preg_match ('/\d+/', $user) && $user == $value->id) ? ' selected="selected"' : ''; $return .= sprintf($base, $value->id, $selected, $value->user_nicename); } return $return; } static function save ($user, $cats) { if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); $wpdb = UserCatsManager::$wpdb; $wpdb->query(sprintf('DELETE FROM %suser_cats_manager WHERE user_id = \'%s\'', $wpdb->prefix, $user)); foreach ($cats as $cat) { $sql = sprintf('INSERT INTO %suser_cats_manager VALUES (\'%s\', \'%s\');', $wpdb->prefix, $user, $cat); $wpdb->query($sql); } } static function filterCats($cats) { if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); $wpdb = UserCatsManager::$wpdb; $current_user = wp_get_current_user(); // Bugfix // Old: if ($current_user->user_level == 10) if ($current_user->user_level == 10 || !is_admin()) { return $cats; } if (gettype($cats) != 'array') return $cats; foreach ($cats as $key=>$cat) { $sql = sprintf('SELECT * FROM %suser_cats_manager WHERE user_id = \'%s\' AND term_id = \'%s\' ', $wpdb->prefix, $current_user->ID, $cat->term_id); $catH = $wpdb->get_results($sql); if (!count($catH)) unset ($cats[$key]); } return $cats; //$accepteds = $wpdb->get_results() } function categorySavePre($categories_checked) { // filter checked categories when a post is saved if ($current_user->user_level == 10 || !is_admin()) { return $categories_checked; } if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); $wpdb = UserCatsManager::$wpdb; $current_user = wp_get_current_user(); $sql = sprintf('SELECT term_id FROM %suser_cats_manager WHERE user_id = \'%s\'', $wpdb->prefix, $current_user->ID); $oCategories = $wpdb->get_results($sql, 'ARRAY_N'); $aValidCategories = array(); foreach ($oCategories as $aCategory) { $aValidCategories[] = $aCategory[0]; } if (count($aValidCategories) == 0) { // There is no specifically allowed category for the user, allow every category return $categories_checked; } else { // Set the allowed categories as selected categories. Ignore specific selections. return $aValidCategories; } } static function set_current_user () { global $current_user; if ($current_user->user_level == 10) return; unset($current_user->allcaps['manage_categories']); } static function loadpost () { if ($_REQUEST['action'] != 'edit' && !isset($_REQUEST['post'])) return; global $current_user; if ($current_user->user_level == 10) return; if ( is_null(UserCatsManager::$wpdb) ) UserCatsManager::init(); $categories = UserCatsManager::$wpdb->get_results(sprintf( 'SELECT term_id FROM %suser_cats_manager WHERE user_id = \'%s\'', UserCatsManager::$wpdb->prefix, $current_user->ID ), 'ARRAY_N' ); $validCategories = array(); foreach ($categories as $cat) $validCategories[] = $cat[0]; $categories = wp_get_post_categories($_GET['post']); if ((bool) array_diff_assoc($categories, $validCategories) === true) { $_GET = array(); $_POST = array(); $_REQUEST = array(); } } } $ucmPluginFile = substr(strrchr(dirname(__FILE__),DIRECTORY_SEPARATOR),1).DIRECTORY_SEPARATOR.basename(__FILE__); register_activation_hook($ucmPluginFile,array('UserCatsManager','install')); register_deactivation_hook($ucmPluginFile,array('UserCatsManager','uninstall')); add_action('set_current_user', array('UserCatsManager','set_current_user')); add_filter('init', array('UserCatsManager','init')); ?>
Regards,
HerrB
HerrB I’m glad your attempting to fix this plugin as it’s definitely something a lot of people will use.
Though it seems buggy still when you go to update something or post something you get this error but everything still goes through you just have to click back or continue to do something else but when you click like Submit or Update or Publish you will get this error but it will still go through and Submit etc. Is there a fix for this?
Warning: Invalid argument supplied for foreach() in /home2/thesidet/public_html/wp-content/plugins/user-cats-manager/user-cats-manager.php on line 257
Warning: Cannot modify header information – headers already sent by (output started at /home2/thesidet/public_html/wp-content/plugins/user-cats-manager/user-cats-manager.php:257) in /home2/thesidet/public_html/wp-includes/pluggable.php on line 868
Any word on a possible fix?
Hi HerrB,
thank you for your solution, it partially works :
I can access to the admin options, set settings and save, but unfortunattely, those settings are not saved.
(when I re-edit, nothing is checked, and nothing changed in author mode).I tried to deactivate every other plugins, I’m on wordpress 2.9.1
Any idea ?
Thanks for sharing !I have the same problem and takes a lot of this plugin working…
Could solve in unusual ways.
on the place where this code:
foreach ($oCategories as $aCategory) {
$aValidCategories[] = $aCategory[0];
}replace with this:
if(is_array($oCategories))
{
foreach ($oCategories as $aCategory) {
$aValidCategories[] = $aCategory[0];
}
}
- The topic ‘[Plugin: User-Cats Manager] ‘FileReader’ not found’ is closed to new replies.