Agora_Driver::saveMessage PHP Method

saveMessage() public method

Saves the message.
public saveMessage ( array $info ) : mixed
$info array Array containing all the message data to save.
return mixed Message ID on success or PEAR_Error on failure.
    public function saveMessage($info)
    {
        /* Check if the thread is locked before changing anything. */
        if ($info['message_parent_id'] && $this->isThreadLocked($info['message_parent_id'])) {
            return PEAR::raiseError(_("This thread has been locked."));
        }
        /* Check post permissions. */
        if (!$this->hasPermission(Horde_Perms::EDIT)) {
            return PEAR::raiseError(sprintf(_("You don't have permission to post messages in forum %s."), $this->_forum_id));
        }
        if (empty($info['message_id'])) {
            /* Get thread parents */
            // TODO message_thread is always parent root, probably can use it here.
            if ($info['message_parent_id'] > 0) {
                $parents = $this->_db->selectValue('SELECT parents FROM ' . $this->_threads_table . ' WHERE message_id = ?', array($info['message_parent_id']));
                $info['parents'] = $parents . ':' . $info['message_parent_id'];
                $info['message_thread'] = $this->getThreadRoot($info['message_parent_id']);
            } else {
                $info['parents'] = '';
                $info['message_thread'] = 0;
            }
            /* Create new message */
            $sql = 'INSERT INTO ' . $this->_threads_table . ' (forum_id, message_thread, parents, ' . 'message_author, message_subject, body, attachments, ' . 'message_timestamp, message_modifystamp, ip) ' . ' VALUES (?, ?, ?, ?, ?, ?, 0, ?, ?, ?)';
            $author = $GLOBALS['registry']->getAuth() ? $GLOBALS['registry']->getAuth() : $info['posted_by'];
            $values = array($this->_forum_id, $info['message_thread'], $info['parents'], $author, $this->convertToDriver($info['message_subject']), $this->convertToDriver($info['message_body']), $_SERVER['REQUEST_TIME'], $_SERVER['REQUEST_TIME'], $_SERVER['REMOTE_ADDR']);
            try {
                $info['message_id'] = $this->_db->insert($sql, $values);
            } catch (Horde_Db_Exception $e) {
                throw new Agora_Exception($e->getMessage());
            }
            /* Update last message in forum, but only if it is not moderated */
            if (!$this->_forum['forum_moderated']) {
                // Send the new post to the distribution address
                if ($this->_forum['forum_distribution_address']) {
                    Agora::distribute($info['message_id']);
                }
                /* Update cached message/thread counts and last poster */
                $this->_lastInForum($this->_forum_id, $info['message_id'], $author, $_SERVER['REQUEST_TIME']);
                $this->_forumSequence($this->_forum_id, 'message', '+');
                if ($info['message_thread']) {
                    $this->_sequence($info['message_thread'], '+');
                    $this->_lastInThread($info['message_thread'], $info['message_id'], $author, $_SERVER['REQUEST_TIME']);
                } else {
                    $this->_forumSequence($this->_forum_id, 'thread', '+');
                }
            }
        } else {
            // TODO clearing cache for editing doesn't work
            /* Update message data */
            $sql = 'UPDATE ' . $this->_threads_table . ' SET ' . 'message_subject = ?, body = ?, message_modifystamp = ? WHERE message_id = ?';
            $values = array($this->convertToDriver($info['message_subject']), $this->convertToDriver($info['message_body']), $_SERVER['REQUEST_TIME'], $info['message_id']);
            try {
                $this->_db->execute($sql, $values);
            } catch (Horde_Db_Exception $e) {
                throw new Agora_Exception($e->getMessage());
            }
            /* Get message thread for cache expiration */
            $info['message_thread'] = $this->getThreadRoot($info['message_id']);
        }
        /* Handle attachment saves or deletions. */
        if (!empty($info['message_attachment']) || !empty($info['attachment_delete'])) {
            $vfs = Agora::getVFS();
            if ($vfs instanceof PEAR_Error) {
                return $vfs;
            }
            $vfs_dir = Agora::VFS_PATH . $this->_forum_id . '/' . $info['message_id'];
            /* Check if delete requested or new attachment loaded, and delete
             * any existing one. */
            if (!empty($info['attachment_delete'])) {
                $sql = 'SELECT file_id FROM agore_files WHERE message_id = ?';
                foreach ($this->_db->selectValues($sql, array($info['message_id'])) as $file_id) {
                    if ($vfs->exists($vfs_dir, $file_id)) {
                        $delete = $vfs->deleteFile($vfs_dir, $file_id);
                        if ($delete instanceof PEAR_Error) {
                            return $delete;
                        }
                    }
                }
                try {
                    $this->_db->execute('DELETE FROM agore_files WHERE message_id = ?', array($info['message_id']));
                } catch (Horde_Db_Exception $e) {
                    throw new Agora_Exception($e->getMessage());
                }
                $attachments = 0;
            }
            /* Save new attachment information. */
            if (!empty($info['message_attachment'])) {
                $file_sql = 'INSERT INTO agora_files (file_name, file_type, file_size, message_id) VALUES (?, ?, ?, ?)';
                $file_data = array($info['message_attachment']['name'], $info['message_attachment']['type'], $info['message_attachment']['size'], $info['message_id']);
                try {
                    $file_id = $this->_db->insert($file_sql, $file_data);
                } catch (Horde_Db_Exception $e) {
                    throw new Agora_Exception($e->getMessage());
                }
                $result = $vfs->write($vfs_dir, $file_id, $info['message_attachment']['file'], true);
                if ($result instanceof PEAR_Error) {
                    return $result;
                }
                $attachments = 1;
            }
            $sql = 'UPDATE ' . $this->_threads_table . ' SET attachments = ? WHERE message_id = ?';
            try {
                $this->_db->execute($sql, array($attachments, $info['message_id']));
            } catch (Horde_Db_Exception $e) {
                throw new Agora_Exception($e->getMessage());
            }
        }
        /* Update cache */
        $this->_updateCacheState($info['message_thread']);
        return $info['message_id'];
    }