diff -r 000000000000 -r a09fb41e48d5 plugins/nuggie/usercp.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/nuggie/usercp.php Tue Dec 11 02:03:54 2007 -0500 @@ -0,0 +1,462 @@ +Nuggie not installed'; + echo '

It looks like Nuggie isn\'t installed yet. You\'ll need to install Nuggie before you can do anything more.

'; + return true; + } + + $subsection = $paths->getParam(1); + $initted = true; + + $q = $db->sql_query('SELECT blog_id, blog_name, blog_subtitle, blog_type, allowed_users FROM ' . table_prefix . "blogs WHERE user_id = {$session->user_id};"); + if ( !$q ) + $db->_die('Nuggie User CP selecting blog info'); + + if ( $db->numrows() < 1 ) + { + $subsection = 'Settings'; + $initted = false; + } + + list(, $blog_name, $blog_desc, $blog_type, $allowed_users) = $db->fetchrow_num($q); + + switch($subsection) + { + case false: + case 'Home': + echo 'module Home'; + break; + case 'Settings': + + switch ( isset($_POST['do_save']) ) + { + // We're doing this so we can break out if we need to (if form validation fails) + case true: + + $errors = array(); + + $blog_name = trim($_POST['blog_name']); + $blog_desc = trim($_POST['blog_desc']); + $blog_access = trim($_POST['blog_access']); + $allowed_users = $_POST['allowed_users']; + + if ( empty($blog_name) ) + $errors[] = 'Please enter a name for your blog.'; + + if ( !in_array($blog_access, array('public', 'private')) ) + $errors[] = 'Hacking attempt on blog_access: must be one of public, private.'; + + if ( count($allowed_users) > 500 ) + $errors[] = 'You\'re asking that an unreasonable number of users have access to this blog. If you really have that many readers, you may want to ask the administrator of this site to make a usergroup with read access to your blog.'; + + if ( count($allowed_users) < 1 && $blog_access == 'private' ) + $errors[] = 'Please enter at least one username that will have access to your blog. Note that your account always has access to your blog.'; + + if ( $blog_access == 'public' ) + { + $allowed_users = 'NULL'; + } + else + { + if ( is_array($allowed_users) && count($errors) < 1 ) + { + $allowed_users = array_values(array_unique($allowed_users)); + foreach ( $allowed_users as $i => $_ ) + { + if ( empty( $allowed_users[$i] ) ) + { + unset($allowed_users[$i]); + } + else + { + $allowed_users[$i] = $db->escape($allowed_users[$i]); + } + } + $fragment = "username='" . implode("' OR username='", $allowed_users) . "'"; + $e = $db->sql_query('SELECT COUNT(username) AS num_valid FROM ' . table_prefix . "users WHERE $fragment;"); + if ( !$e ) + $db->_die('Nuggie user CP validating usernames'); + + $row = $db->fetchrow(); + if ( intval($row['num_valid']) != count($allowed_users) ) + $errors[] = 'One or more of the usernames you entered does not exist.'; + } + else + { + $errors[] = 'Invalid datatype on allowed_users.'; + } + } + + if ( count($errors) > 0 ) + { + $initted = true; + echo '
+ The following problems prevented your blog settings from being saved: + +
'; + break; + } + else + { + // Save changes + + if ( !is_string($allowed_users) ) + $allowed_users = "'" . $db->escape( serialize($allowed_users) ) . "'"; + + $blog_name = $db->escape($blog_name); + $blog_desc = $db->escape($blog_desc); + + if ( $initted ) + { + $sql = 'UPDATE ' . table_prefix . "blogs SET blog_name = '$blog_name', blog_subtitle = '$blog_desc', blog_type = '$blog_access', allowed_users = $allowed_users;"; + } + else + { + $sql = 'INSERT INTO ' . table_prefix . 'blogs(blog_name, blog_subtitle, blog_type, allowed_users, user_id)' . + "\n VALUES ( '$blog_name', '$blog_desc', '$blog_access', $allowed_users, {$session->user_id} );"; + } + + if ( $db->sql_query($sql) ) + { + echo '
' . + ( $initted ? 'Your changes have been saved.' : 'Your blog has been created; you can now + start writing some posts and + then view your blog.' ) + . '
'; + } + else + { + $db->_die('Nuggie user CP saving settings'); + } + + // Re-select the blog data + $db->free_result($q); + + $q = $db->sql_query('SELECT blog_id, blog_name, blog_subtitle, blog_type, allowed_users FROM ' . table_prefix . "blogs WHERE user_id = {$session->user_id};"); + if ( !$q ) + $db->_die('Nuggie User CP selecting blog info'); + + list(, $blog_name, $blog_desc, $blog_type, $allowed_users) = $db->fetchrow_num($q); + } + + $initted = true; + } + + if ( !$initted ) + { + echo '
+ It looks like your blog isn\'t set up yet.
+ You\'ll need to set up your blog by entering some basic information here before you can write any posts. +
'; + $blog_name = htmlspecialchars($session->username) . "'s blog"; + $blog_desc = ''; + } + else + { + $blog_name = htmlspecialchars(strtr($blog_name, array('"' => '"'))); + $blog_desc = htmlspecialchars(strtr($blog_desc, array('"' => '"'))); + } + + if ( !isset($blog_type) ) + $blog_type = 'public'; + + if ( !isset($allowed_users) ) + $allowed_users = serialize(array()); + + $form_action = makeUrlNS('Special', 'Preferences/Blog/Settings', false, true); + echo "
"; + + ?> +
+ + + + + + + + + + + + + + + + + + + +
+ +
+ Blog name: + + +
+ Blog description:
+ You're best off keeping this short and sweet. +
+ +
+ Blog access: + +
+
+ Administrators can always read all blogs, including private ones. + +
+ +
+
+ '; + + break; + case 'Posts': + echo 'module Posts'; + break; + case 'Write': + + $post_text = ''; + $post_title = 'Post title'; + + $post_id = $paths->getParam(2); + if ( isset($_POST['post_id']) ) + { + $post_id = $_POST['post_id']; + } + if ( $post_id ) + { + /* + * FIXME: Validate blog public/private status before sending text + * FIXME: Avoid ambiguous post_title_cleans through appending numbers when needed + */ + + $post_id = intval($post_id); + $q = $db->sql_query('SELECT p.post_id, p.post_title, p.post_title_clean, p.post_author, p.post_text, p.post_timestamp, u.username ' + . 'FROM ' . table_prefix . 'blog_posts AS p' + . ' LEFT JOIN ' . table_prefix . 'users AS u' + . ' ON ( p.post_author = u.user_id )' + . ' WHERE post_id = ' . $post_id . ';'); + + if ( !$q ) + $db->_die('Nuggie user CP obtaining post info'); + + if ( $db->numrows() > 0 ) + { + $row = $db->fetchrow(); + if ( $session->user_id != $row['post_author'] ) + { + // We have a possible security issue on our hands - the user is trying + // to edit someone else's post. Verify read and write permissions. + $post_page_id = "{$row['post_timestamp']}_{$row['post_id']}"; + $perms = $session->fetch_page_acl($post_page_id, 'Blog'); + if ( !$perms->get_permissions('read') || !$perms->get_permissions('nuggie_edit_other') ) + { + echo '

Post editing error

'; + echo '

You do not have permission to edit this blog post.

'; + + unset($row); + unset($row); + + $db->free_result(); + // Break out of this entire user CP module + return true; + } + } + else + { + $post_page_id = "{$row['post_timestamp']}_{$row['post_id']}"; + $perms = $session->fetch_page_acl($post_page_id, 'Blog'); + if ( !$perms->get_permissions('nuggie_edit_own') || !$perms->get_permissions('read') ) + { + echo '

Post editing error

'; + echo '

You do not have permission to edit this blog post.

'; + + unset($row); + unset($row); + + $db->free_result(); + // Break out of this entire user CP module + return true; + } + } + // We have permission - load post + $post_title = $row['post_title']; + $post_text = $row['post_text']; + } + } + + if ( isset($_POST['submit']) ) + { + switch($_POST['submit']) + { + case 'save_publish': + $publish = '1'; + case 'save_draft': + if ( !isset($publish) ) + $publish = '0'; + + $save_post_text = $_POST['post_text']; + $save_post_title = $db->escape($_POST['post_title']); + $save_post_title_clean = $db->escape(nuggie_sanitize_title($_POST['post_title'])); + + $save_post_text = RenderMan::preprocess_text($save_post_text, true, true); + + if ( $post_id ) + { + $sql = 'UPDATE ' . table_prefix . "blog_posts SET post_title = '$save_post_title', post_title_clean = '$save_post_title_clean', post_text = '$save_post_text', post_published = $publish WHERE post_id = $post_id;"; + } + else + { + $time = time(); + $sql = 'INSERT INTO ' . table_prefix . 'blog_posts ( post_title, post_title_clean, post_text, post_author, post_timestamp, post_published ) ' + . "VALUES ( '$save_post_title', '$save_post_title_clean', '$save_post_text', {$session->user_id}, $time, $publish );"; + } + + if ( $db->sql_query($sql) ) + { + echo '
+ ' . ( $publish == '1' ? 'Your post has been published.' : 'Your post has been saved.' ) . ' +
'; + } + else + { + $db->_die('Nuggie user CP running post-save query'); + } + + if ( !$post_id ) + { + $post_id = $db->insert_id(); + } + + $post_title = $_POST['post_title']; + $post_text = $_POST['post_text']; + break; + case 'preview': + $preview_text = $_POST['post_text']; + $preview_text = RenderMan::preprocess_text($preview_text, true, false); + $preview_text = RenderMan::render($preview_text); + + /* + * FIXME: Use the real post renderer (when it's ready) + */ + + echo '
'; + echo '

Post preview

'; + echo '

FIXME: This does not use the real post-display API, which is not yet implemented. Eventually this should look just like a real post.

'; + echo '

' . htmlspecialchars($_POST['post_title']) . '

'; + echo $preview_text; + echo '
'; + + $post_title = $_POST['post_title']; + $post_text = $_POST['post_text']; + break; + } + } + + $q = $db->sql_query('SELECT post_id, post_title FROM ' . table_prefix . "blog_posts WHERE post_published = 0 AND post_author = {$session->user_id};"); + if ( !$q ) + $db->_die('Nuggie user CP selecting draft posts'); + if ( $db->numrows() > 0 ) + { + echo '
Your drafts: '; + $posts = array(); + while ( $row = $db->fetchrow() ) + { + $posts[] = '' . htmlspecialchars($row['post_title']) . ''; + } + echo implode(', ', $posts); + echo '
'; + } + + echo ''; + + $post_text = htmlspecialchars($post_text); + $post_title = strtr(htmlspecialchars($post_title), array('"' => '"')); + + echo ''; + echo $template->tinymce_textarea('post_text', $post_text); + + // Buttons! + echo '
'; + echo '  '; + echo '  '; + echo '  '; + echo '
'; + + if ( $post_id ) + { + echo ''; + } + + echo '
'; + + break; + case 'Planets': + echo 'module Planets'; + break; + default: + return false; + } + return true; +} + +$plugins->attachHook("userprefs_jbox", " + userprefs_menu_add('My blog', 'Manage blog settings', makeUrlNS('Special', 'Preferences/Blog/Settings')); + userprefs_menu_add('My blog', 'Manage posts', makeUrlNS('Special', 'Preferences/Blog/Posts')); + userprefs_menu_add('My blog', 'Write new post', makeUrlNS('Special', 'Preferences/Blog/Write')); + userprefs_menu_add('My blog', 'Manage my planets', makeUrlNS('Special', 'Preferences/Blog/Planets')); + \$userprefs_menu_links['My blog'] = makeUrlNS('Blog', \$session->username); + "); +$plugins->attachHook("userprefs_body", "return nuggie_user_cp(\$section);");