/**
 * Adds a new author account
 *
 * @access public
 * @param   string  New username
 * @param   string  New password
 * @param   string  The realname of the user
 * @param   string  The email address of the user
 * @param   int     The userlevel of a user
 * @return  int     The new user ID of the added author
 */
function serendipity_addAuthor($username, $password, $realname, $email, $userlevel = 0, $hashtype = 1)
{
    global $serendipity;
    $password = serendipity_hash($password);
    $query = "INSERT INTO {$serendipity['dbPrefix']}authors (username, password, realname, email, userlevel, hashtype)\n                        VALUES  ('" . serendipity_db_escape_string($username) . "',\n                                 '" . serendipity_db_escape_String($password) . "',\n                                 '" . serendipity_db_escape_String($realname) . "',\n                                 '" . serendipity_db_escape_String($email) . "',\n                                 '" . serendipity_db_escape_String($userlevel) . "',\n                                 '" . serendipity_db_escape_String($hashtype) . "'\n                                 )";
    serendipity_db_query($query);
    $cid = serendipity_db_insert_id('authors', 'authorid');
    $data = array('authorid' => $cid, 'username' => $username, 'realname' => $realname, 'email' => $email);
    serendipity_insertPermalink($data, 'author');
    return $cid;
}
Example #2
0
/**
 * Create a new Category
 *
 * @access public
 * @param   string  The new category name
 * @param   string  The new category description
 * @param   int     The category owner
 * @param   string  An icon representing the category
 * @param   int     A possible parentid to a category
 * @return  int     The new category's ID
 */
function serendipity_addCategory($name, $desc, $authorid, $icon, $parentid)
{
    global $serendipity;
    $query = "INSERT INTO {$serendipity['dbPrefix']}category\n                    (category_name, category_description, authorid, category_icon, parentid, category_left, category_right)\n                  VALUES\n                    ('" . serendipity_db_escape_string($name) . "',\n                     '" . serendipity_db_escape_string($desc) . "',\n                      " . (int) $authorid . ",\n                     '" . serendipity_db_escape_string($icon) . "',\n                      " . (int) $parentid . ",\n                       0,\n                       0)";
    serendipity_db_query($query);
    $cid = serendipity_db_insert_id('category', 'categoryid');
    serendipity_plugin_api::hook_event('backend_category_addNew', $cid);
    $data = array('categoryid' => $cid, 'category_name' => $name, 'category_description' => $desc);
    serendipity_insertPermalink($data, 'category');
    return $cid;
}
/**
 * Inserts a new entry into the database or updates an existing entry
 *
 * Another central function, that parses, prepares and commits changes to an entry
 *
 * @access public
 * @param   array       The new/modified entry data.
 * @return  mixed       Integer with new entry ID if successfull, a string or array if error(s).
 */
function serendipity_updertEntry($entry)
{
    global $serendipity;
    include_once S9Y_INCLUDE_PATH . 'include/functions_entries_admin.inc.php';
    $errors = array();
    serendipity_plugin_api::hook_event('backend_entry_updertEntry', $errors, $entry);
    if (count($errors) > 0) {
        // Return error message(s)
        return implode("\n", $errors);
    }
    serendipity_plugin_api::hook_event('backend_entry_presave', $entry);
    $categories = $entry['categories'];
    unset($entry['categories']);
    $newEntry = 0;
    $exflag = 0;
    if (isset($entry['properties'])) {
        unset($entry['properties']);
    }
    if (!is_numeric($entry['timestamp'])) {
        $entry['timestamp'] = time();
    }
    /* WYSIWYG-editor inserts empty ' ' for extended body; this is reversed here */
    if (isset($entry['extended']) && (trim($entry['extended']) == '' || trim($entry['extended']) == '<br />' || trim($entry['extended']) == '<p></p>' || str_replace(array("\r", "\n", "\t", "", "<br />", "<p>", "</p>", "<br>"), array('', '', '', '', '', '', '', ''), trim($entry['extended'])) == '')) {
        $entry['extended'] = '';
    }
    if (strlen($entry['extended'])) {
        $exflag = 1;
    }
    $entry['exflag'] = $exflag;
    if (!is_numeric($entry['id'])) {
        /* we need to insert */
        unset($entry['id']);
        $entry['comments'] = 0;
        if (!isset($entry['last_modified']) || !is_numeric($entry['last_modified'])) {
            $entry['last_modified'] = $entry['timestamp'];
        }
        // New entries need an author
        $entry['author'] = $serendipity['user'];
        if (!isset($entry['authorid']) || empty($entry['authorid'])) {
            $entry['authorid'] = $serendipity['authorid'];
        }
        if (!$_SESSION['serendipityRightPublish']) {
            $entry['isdraft'] = 'true';
        }
        if (!isset($entry['allow_comments'])) {
            $entry['allow_comments'] = 'false';
        }
        if (!isset($entry['moderate_comments'])) {
            $entry['moderate_comments'] = 'false';
        }
        $res = serendipity_db_insert('entries', $entry);
        if ($res) {
            $entry['id'] = $serendipity['lastSavedEntry'] = serendipity_db_insert_id('entries', 'id');
            if (is_array($categories)) {
                foreach ($categories as $cat) {
                    if (is_numeric($cat)) {
                        serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entrycat (entryid, categoryid) VALUES ({$entry['id']}, {$cat})");
                    }
                }
            }
            serendipity_insertPermalink($entry);
        } else {
            //Some error message here
            return ENTRIES_NOT_SUCCESSFULLY_INSERTED;
        }
        $newEntry = 1;
    } else {
        /* we need to update */
        // Get settings from entry if already in DB, which should not be alterable with POST methods
        $_entry = serendipity_fetchEntry('id', $entry['id'], 1, 1);
        $entry['authorid'] = $_entry['authorid'];
        if (isset($serendipity['GET']['adminModule']) && $serendipity['GET']['adminModule'] == 'entries' && $entry['authorid'] != $serendipity['authorid'] && !serendipity_checkPermission('adminEntriesMaintainOthers')) {
            // Only chiefs and admins can change other's entry. Else update fails.
            return;
        }
        if (!$_SESSION['serendipityRightPublish']) {
            $entry['isdraft'] = 'true';
        }
        if (is_array($categories)) {
            serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}entrycat WHERE entryid={$entry['id']}");
            foreach ($categories as $cat) {
                serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}entrycat (entryid, categoryid) VALUES ({$entry['id']}, {$cat})");
            }
        }
        //if (!serendipity_db_bool($entry['isdraft']) && !serendipity_db_bool($_entry['isdraft'])) {
        $entry['last_modified'] = time();
        //}
        $res = serendipity_db_update('entries', array('id' => $entry['id']), $entry);
        $newEntry = 0;
        serendipity_updatePermalink($entry);
    }
    if (is_string($res)) {
        return $res;
    }
    // Reset session data, so that a reload to this frame should not happen!
    $_SESSION['save_entry']['id'] = (int) $entry['id'];
    if (!serendipity_db_bool($entry['isdraft'])) {
        serendipity_plugin_api::hook_event('frontend_display', $entry, array('no_scramble' => true, 'from' => 'functions_entries:updertEntry'));
        $drafted_entry = $entry;
    }
    serendipity_purgeEntry($entry['id'], $entry['timestamp']);
    if (!serendipity_db_bool($entry['isdraft']) && $entry['timestamp'] <= serendipity_serverOffsetHour()) {
        // When saving an entry, first all references need to be gathered. But trackbacks to them
        // shall only be send at the end of the execution flow. However, certain plugins depend on
        // the existance of handled references. Thus we store the current references at this point,
        // execute the plugins and then reset the found references to the original state.
        serendipity_handle_references($entry['id'], $serendipity['blogTitle'], $drafted_entry['title'], $drafted_entry['body'] . $drafted_entry['extended'], true);
    }
    // Send publish tags if either a new article has been inserted from scratch, or if the entry was previously
    // stored as draft and is now published
    $entry['categories'] =& $categories;
    if (!serendipity_db_bool($entry['isdraft']) && ($newEntry || serendipity_db_bool($_entry['isdraft']))) {
        serendipity_plugin_api::hook_event('backend_publish', $entry, $newEntry);
    } else {
        serendipity_plugin_api::hook_event('backend_save', $entry, $newEntry);
    }
    if (!serendipity_db_bool($entry['isdraft']) && $entry['timestamp'] <= serendipity_serverOffsetHour()) {
        // Now that plugins are executed, we go ahead into the Temple of Doom and send possibly failing trackbacks.
        // First, original list of references is restored (inside the function call)
        serendipity_handle_references($entry['id'], $serendipity['blogTitle'], $drafted_entry['title'], $drafted_entry['body'] . $drafted_entry['extended'], false);
    }
    return (int) $entry['id'];
}
/**
 * Build all permalinks for all current entries, authors and categories
 *
 * @access public
 * @return null
 */
function serendipity_buildPermalinks()
{
    global $serendipity;
    $entries = serendipity_db_query("SELECT id, title, timestamp FROM {$serendipity['dbPrefix']}entries");
    if (is_array($entries)) {
        serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}permalinks WHERE type = 'entry'");
        foreach ($entries as $entry) {
            serendipity_insertPermalink($entry, 'entry');
        }
    }
    $authors = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}authors");
    if (is_array($authors)) {
        serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}permalinks WHERE type = 'author'");
        foreach ($authors as $author) {
            serendipity_insertPermalink($author, 'author');
        }
    }
    $categories = serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}category");
    if (is_array($categories)) {
        serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}permalinks WHERE type = 'category'");
        foreach ($categories as $category) {
            serendipity_insertPermalink($category, 'category');
        }
    }
}