예제 #1
0
파일: syncer.php 프로젝트: huokedu/zentao
<?php

/**
 * The syncer of svn.
 *
 * @copyright   Copyright 2009-2012 青岛易软天创网络科技有限公司 (QingDao Nature Easy Soft Network Technology Co,LTD www.cnezsoft.com)
 * @license     LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author      Chunsheng Wang <*****@*****.**>
 * @package     svn
 * @version     $Id$
 * @link        http://www.zentao.net
 */
error_reporting(E_ALL ^ E_STRICT ^ E_WARNING);
include './config.php';
include './api.class.php';
$syncer = new syncer($config);
$syncer->run();
class syncer
{
    /**
     * The svn binary svnClient.
     * 
     * @var string   
     * @access public
     */
    public $svnClient;
    /**
     * The zentao client.
     * 
     * @var string   
     * @access public
    function _submit(&$sync)
    {
        global $config, $db, $auth, $user;
        if ($sync === false) {
            //submit() was called directly so we need to sync after it
            $sync = new syncer();
            $exec_sync = true;
        } else {
            //submit() was called by topic->submit(), sync there when everything is done
            $exec_sync = false;
        }
        if (!$this->post_id) {
            //new post, set some default values if not set yet
            if (!$this->poster_id) {
                $this->poster_id = $user->data['user_id'];
            }
            if (!$this->poster_ip) {
                $this->poster_ip = $user->ip;
            }
            if (!$this->post_time) {
                $this->post_time = time();
            }
        }
        $this->post_subject = truncate_string($this->post_subject);
        $sql_data = array('poster_id' => $this->poster_id, 'poster_ip' => $this->poster_ip, 'topic_id' => $this->topic_id, 'forum_id' => $this->forum_id, 'post_username' => $this->post_username, 'icon_id' => $this->icon_id, 'post_time' => $this->post_time, 'post_postcount' => $this->post_postcount ? 1 : 0, 'post_visibility' => $this->post_visibility, 'post_reported' => $this->post_reported ? 1 : 0, 'enable_bbcode' => $this->enable_bbcode ? 1 : 0, 'enable_smilies' => $this->enable_smilies ? 1 : 0, 'enable_magic_url' => $this->enable_magic_url ? 1 : 0, 'enable_sig' => $this->enable_sig ? 1 : 0, 'post_subject' => $this->post_subject, 'bbcode_bitfield' => 0, 'bbcode_uid' => '', 'post_text' => $this->post_text, 'post_checksum' => md5($this->post_text), 'post_edit_time' => $this->post_edit_time, 'post_edit_reason' => $this->post_edit_reason, 'post_edit_user' => $this->post_edit_user, 'post_edit_count' => $this->post_edit_count, 'post_edit_locked' => $this->post_edit_locked, 'post_delete_time' => $this->post_delete_time, 'post_delete_reason' => $this->post_delete_reason, 'post_delete_user' => $this->post_delete_user);
        $flags = '';
        generate_text_for_storage($sql_data['post_text'], $sql_data['bbcode_uid'], $sql_data['bbcode_bitfield'], $flags, $this->enable_bbcode, $this->enable_magic_url, $this->enable_smilies);
        if ($this->post_id && $this->topic_id) {
            //edit
            $sql = "SELECT p.*, t.topic_first_post_id, t.topic_last_post_id, t.topic_approved, t.topic_replies\n\t\t\t\t\tFROM " . POSTS_TABLE . " p\n\t\t\t\t\tLEFT JOIN " . TOPICS_TABLE . " t ON (t.topic_id = p.topic_id)\n\t\t\t\t\tWHERE p.post_id=" . intval($this->post_id);
            //$sql = "SELECT * FROM " . POSTS_TABLE . " WHERE post_id=" . intval($this->post_id);
            $result = $db->sql_query($sql);
            $post_data = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            if (!$post_data) {
                trigger_error("post_id={$this->post_id}, but that post does not exist", E_USER_ERROR);
            }
            //check first/last post
            $is_first_post = $post_data['post_id'] == $post_data['topic_first_post_id'];
            $is_last_post = $post_data['post_id'] == $post_data['topic_last_post_id'];
            $db->sql_transaction('begin');
            $sql = "UPDATE " . POSTS_TABLE . " SET " . $db->sql_build_array('UPDATE', $sql_data) . " WHERE post_id=" . $this->post_id;
            $db->sql_query($sql);
            if ($this->topic_id != $post_data['topic_id']) {
                //merge into new topic
                //get new topic's forum id and first/last post time
                $sql = "SELECT forum_id, topic_time, topic_last_post_time\n\t\t\t\t\t\tFROM " . TOPICS_TABLE . "\n\t\t\t\t\t\tWHERE topic_id = {$this->topic_id}";
                $result = $db->sql_query($sql);
                $new_topic_data = $db->sql_fetchrow($result);
                if (!$new_topic_data) {
                    trigger_error("attempted to merge post {$this->post_id} into topic {$this->topic_id}, but that topic does not exist", E_USER_ERROR);
                }
                //sync forum_posts
                //TODO
                if ($new_topic_data['forum_id'] != $post_data['forum_id']) {
                    $sync->add('forum', $post_data['forum_id'], 'forum_posts', $this->post_approved ? -1 : 0);
                    $sync->add('forum', $new_topic_data['forum_id'], 'forum_posts', $this->post_approved ? 1 : 0);
                    if ($this->forum_id != $new_topic_data['forum_id']) {
                        //user changed topic_id but not forum_id, so we saved the wrong one above. correct it via sync
                        $this->forum_id = $new_topic_data['forum_id'];
                        $sync->set('post', $this->post_id, 'forum_id', $this->forum_id);
                    }
                }
                //sync old topic
                $sync->add('topic', $post_data['topic_id'], 'topic_replies', $this->post_approved ? -1 : 0);
                $sync->add('topic', $post_data['topic_id'], 'topic_replies_real', -1);
                $sync->check_topic_empty($post_data['topic_id']);
                //sync new topic
                $sync->add('topic', $this->topic_id, 'topic_replies', $this->post_approved ? 1 : 0);
                $sync->add('topic', $this->topic_id, 'topic_replies_real', 1);
                //sync topic_reported and topic_attachment if applicable
                if ($post_data['post_reported']) {
                    $sync->topic_reported($post_data['topic_id']);
                }
                if ($post_data['post_attachment']) {
                    $sync->topic_attachment($post_data['topic_id']);
                }
                if ($this->post_reported) {
                    $sync->topic_reported($this->topic_id);
                }
                if ($this->post_attachment) {
                    $sync->topic_attachment($this->topic_id);
                }
                if ($is_first_post) {
                    //this was the first post in the old topic, sync it
                    $sync->topic_first_post($post_data['topic_id']);
                    $is_first_post = false;
                    //unset since we dont know status for new topic yet
                }
                if ($is_last_post) {
                    //this was the last post in the old topic, sync it
                    $sync->topic_last_post($post_data['topic_id']);
                    $sync->forum_last_post($post_data['forum_id']);
                    $is_last_post = false;
                    //unset since we dont know status for new topic yet
                }
                if ($this->post_time <= $new_topic_data['topic_time']) {
                    //this will be the first post in the new topic, sync it
                    $sync->topic_first_post($this->topic_id);
                    $is_first_post = true;
                }
                if ($this->post_time >= $new_topic_data['topic_last_post_time']) {
                    //this will be the last post in the new topic, sync it
                    $sync->topic_last_post($this->topic_id);
                    $sync->forum_last_post($this->topic_id);
                    $is_last_post = true;
                }
            } elseif ($is_first_post) {
                $sync->set('topic', $this->topic_id, array('icon_id' => $this->icon_id, 'topic_approved' => $this->post_approved, 'topic_title' => $this->post_subject, 'topic_poster' => $this->poster_id, 'topic_time' => $this->post_time));
            }
            //check if some statistics relevant flags have been changed
            if ($this->post_approved != $post_data['post_approved']) {
                //if topic_id was changed, we've already updated it above.
                if ($this->topic_id == $post_data['topic_id']) {
                    if ($is_first_post) {
                        //first post -> approve/disapprove whole topic if not yet done (should only happen when directly storing the post)
                        if ($this->post_approved != $post_data['topic_approved']) {
                            $sync->add('forum', $this->forum_id, 'forum_topics', $this->post_approved ? 1 : -1);
                            $sync->add('forum', $this->forum_id, 'forum_posts', $this->post_approved ? 1 + $post_data['topic_replies'] : -(1 + $post_data['topic_replies']));
                            $sync->forum_last_post($this->forum_id);
                            //and the total topics+posts
                            set_config('num_topics', $this->post_approved ? $config['num_topics'] + 1 : $config['num_topics'] - 1, true);
                            set_config('num_posts', $this->post_approved ? $config['num_posts'] + (1 + $post_data['topic_replies']) : $config['num_posts'] - (1 + $post_data['topic_replies']), true);
                        }
                    } else {
                        //reply
                        $sync->add('topic', $this->topic_id, 'topic_replies', $this->post_approved ? 1 : -1);
                        $sync->add('forum', $this->forum_id, 'forum_posts', $this->post_approved ? 1 : -1);
                    }
                }
                //update total posts
                if (!$is_first_post) {
                    set_config('num_posts', $this->post_approved ? $config['num_posts'] + 1 : $config['num_posts'] - 1, true);
                }
            }
            /*if($this->post_postcount != $post_data['post_postcount'] && $this->poster_id != ANONYMOUS)
            		{
            			//increase or decrease user_posts
            			$sync->add('user', $this->poster_id, 'user_posts', $this->post_approved ? 1 : -1);
            		}*/
            if ($this->poster_id != $post_data['poster_id'] || $this->post_postcount != $post_data['post_postcount']) {
                if ($post_data['post_postcount'] && $post_data['poster_id'] != ANONYMOUS) {
                    $sync->add('user', $post_data['poster_id'], 'user_posts', -1);
                }
                if ($this->post_postcount && $this->poster_id != ANONYMOUS) {
                    $sync->add('user', $this->poster_id, 'user_posts', 1);
                }
            }
            if ($is_first_post) {
                $sync->topic_first_post($this->topic_id);
            }
            if ($is_last_post) {
                $sync->topic_last_post($this->topic_id);
                $sync->forum_last_post($this->forum_id);
            }
            reindex('edit', $this->post_id, $sql_data['post_text'], $this->post_subject, $this->poster_id, $this->forum_id);
            $db->sql_transaction('commit');
        } elseif ($this->topic_id) {
            //reply
            $sql = "SELECT t.*, f.forum_name\n\t\t\t\t\tFROM " . TOPICS_TABLE . " t\n\t\t\t\t\tLEFT JOIN " . FORUMS_TABLE . " f ON (f.forum_id = t.forum_id)\n\t\t\t\t\tWHERE t.topic_id=" . intval($this->topic_id);
            $result = $db->sql_query($sql);
            $topic_data = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            if (!$topic_data) {
                trigger_error("topic_id={$this->topic_id}, but that topic does not exist", E_USER_ERROR);
            }
            //we need topic_id and forum_id
            $this->forum_id = $topic_data['forum_id'];
            $sql_data['forum_id'] = $this->forum_id;
            $sql_data['topic_id'] = $this->topic_id;
            //make sure we have a post_subject (empty subjects are bad for e.g. approving)
            if ($this->post_subject == '') {
                $this->post_subject = 'Re: ' . $topic_data['topic_title'];
            }
            $db->sql_transaction('begin');
            //insert post
            $sql = "INSERT INTO " . POSTS_TABLE . " " . $db->sql_build_array('INSERT', $sql_data);
            $db->sql_query($sql);
            $this->post_id = $db->sql_nextid();
            //update topic
            if (!$sync->new_topic_flag) {
                $sync->add('topic', $this->topic_id, 'topic_replies', $this->post_approved ? 1 : 0);
                $sync->add('topic', $this->topic_id, 'topic_replies_real', 1);
                $sync->set('topic', $this->topic_id, 'topic_bumped', 0);
                $sync->set('topic', $this->topic_id, 'topic_bumper', 0);
            } else {
                $sync->topic_first_post($this->topic_id);
                $sync->new_topic_flag = false;
            }
            $sync->topic_last_post($this->topic_id);
            //update forum
            if ($this->forum_id != 0) {
                $sync->add('forum', $this->forum_id, 'forum_posts', $this->post_approved ? 1 : 0);
                $sync->forum_last_post($this->forum_id);
            }
            if ($this->post_postcount) {
                //increase user_posts...
                $sync->add('user', $this->poster_id, 'user_posts', 1);
            }
            if ($this->post_approved) {
                //...and total posts
                set_config('num_posts', $config['num_posts'] + 1, true);
            }
            reindex('reply', $this->post_id, $sql_data['post_text'], $this->post_subject, $this->poster_id, $this->forum_id);
            $db->sql_transaction('commit');
            // Mark this topic as posted to
            markread('post', $this->forum_id, $this->topic_id, $this->post_time, $this->poster_id);
            // Mark this topic as read
            // We do not use post_time here, this is intended (post_time can have a date in the past if editing a message)
            markread('topic', $this->forum_id, $this->topic_id, time());
            //
            if ($config['load_db_lastread'] && $user->data['is_registered']) {
                $sql = 'SELECT mark_time
					FROM ' . FORUMS_TRACK_TABLE . '
					WHERE user_id = ' . $user->data['user_id'] . '
						AND forum_id = ' . $this->forum_id;
                $result = $db->sql_query($sql);
                $f_mark_time = (int) $db->sql_fetchfield('mark_time');
                $db->sql_freeresult($result);
            } else {
                if ($config['load_anon_lastread'] || $user->data['is_registered']) {
                    $f_mark_time = false;
                }
            }
            if ($config['load_db_lastread'] && $user->data['is_registered'] || $config['load_anon_lastread'] || $user->data['is_registered']) {
                // Update forum info
                $sql = 'SELECT forum_last_post_time
					FROM ' . FORUMS_TABLE . '
					WHERE forum_id = ' . $this->forum_id;
                $result = $db->sql_query($sql);
                $forum_last_post_time = (int) $db->sql_fetchfield('forum_last_post_time');
                $db->sql_freeresult($result);
                update_forum_tracking_info($this->forum_id, $forum_last_post_time, $f_mark_time, false);
            }
            // Send Notifications
            user_notification('reply', $this->post_subject, $topic_data['topic_title'], $topic_data['forum_name'], $this->forum_id, $this->topic_id, $this->post_id);
        } else {
            //new topic
            $this->_topic = topic::from_post($this);
            $this->_topic->submit(true);
            //PHP4 Compatibility:
            if (version_compare(PHP_VERSION, '5.0.0', '<')) {
                $this->topic_id = $this->_topic->topic_id;
                $this->post_id = $this->_topic->topic_first_post_id;
            }
            $exec_sync = false;
        }
        foreach ($this->attachments as $attachment) {
            $attachment->post_msg_id = $this->post_id;
            $attachment->topic_id = $this->topic_id;
            $attachment->poster_id = $this->poster_id;
            $attachment->in_message = 0;
            $attachment->is_orphan = 0;
            $attachment->submit();
        }
        if ($exec_sync) {
            $sync->execute();
        }
        /*if($sync_topic)
        		{
        			if($this->_topic)
        			{
        				$this->_topic->sync();
        			}
        			else
        			{
        				sync('topic', 'topic_id', $this->topic_id);
        			}
        		}*/
    }
예제 #3
0
    function submit($submit_posts = true)
    {
        global $config, $db, $auth, $user, $phpbb_container;
        if (!$this->topic_id && count($this->posts) == 0) {
            trigger_error('cannot create a topic without posts', E_USER_ERROR);
        }
        if (!$this->topic_id) {
            // new post, set some default values if not set yet
            if (!$this->topic_poster) {
                $this->topic_poster = $user->data['user_id'];
            }
            if (!$this->topic_time) {
                $this->topic_time = time();
            }
            $this->posts[0]->post_subject = $this->topic_title;
        }
        if ($this->forum_id == 0) {
            // no forum id known, can only insert as global announcement
            $this->topic_type = POST_GLOBAL;
        }
        $this->topic_title = truncate_string($this->topic_title);
        $sql_data = array('icon_id' => $this->icon_id, 'topic_attachment' => $this->topic_attachment ? 1 : 0, 'topic_reported' => $this->topic_reported ? 1 : 0, 'topic_posts_approved' => $this->topic_posts_approved, 'topic_posts_unapproved' => $this->topic_posts_unapproved, 'topic_posts_softdeleted' => $this->topic_posts_softdeleted, 'topic_delete_time' => $this->topic_delete_time, 'topic_delete_reason' => $this->topic_delete_reason, 'topic_delete_user' => $this->topic_delete_user, 'topic_status' => $this->topic_status, 'topic_moved_id' => $this->topic_moved_id, 'topic_type' => $this->topic_type, 'topic_time_limit' => $this->topic_time_limit, 'topic_title' => $this->topic_title, 'topic_time' => $this->topic_time, 'topic_poster' => $this->topic_poster, 'topic_first_post_id' => $this->topic_first_post_id, 'topic_first_poster_name' => $this->topic_first_poster_name, 'topic_first_poster_colour' => $this->topic_first_poster_colour, 'topic_last_post_id' => $this->topic_last_post_id, 'topic_last_poster_id' => $this->topic_last_poster_id, 'topic_last_poster_name' => $this->topic_last_poster_name, 'topic_last_poster_colour' => $this->topic_last_poster_colour, 'topic_last_post_subject' => $this->topic_last_post_subject, 'topic_last_post_time' => $this->topic_last_post_time, 'topic_last_view_time' => $this->topic_last_view_time, 'topic_bumped' => $this->topic_bumped ? 1 : 0, 'topic_bumper' => $this->topic_bumper, 'poll_title' => $this->poll_title, 'poll_start' => $this->poll_start, 'poll_length' => $this->poll_length, 'poll_max_options' => $this->poll_max_options, 'poll_vote_change' => $this->poll_vote_change ? 1 : 0);
        $sync = new syncer();
        if ($this->topic_id) {
            // edit
            $sql = "SELECT *\n\t\t\t\t\tFROM " . TOPICS_TABLE . "\n\t\t\t\t\tWHERE topic_id=" . intval($this->topic_id);
            $result = $db->sql_query($sql);
            $topic_data = $db->sql_fetchrow($result);
            $db->sql_freeresult($result);
            if (!$topic_data) {
                trigger_error("topic_id={$this->topic_id}, but that topic does not exist", E_USER_ERROR);
            }
            $db->sql_transaction('begin');
            $sql = "UPDATE " . TOPICS_TABLE . " SET " . $db->sql_build_array('UPDATE', $sql_data) . " WHERE topic_id=" . $this->topic_id;
            $db->sql_query($sql);
            // move to another forum -> also move posts and update statistics
            if ($this->forum_id != $topic_data['forum_id']) {
                $sql = "UPDATE " . POSTS_TABLE . " SET forum_id=" . $this->forum_id . " WHERE topic_id=" . $this->topic_id;
                $db->sql_query($sql);
                // old forum
                if ($topic_data['forum_id'] != 0) {
                    if ($topic_data['topic_visibility'] == ITEM_APPROVED) {
                        $sync->add('forum', $topic_data['forum_id'], 'forum_topics_approved', -1);
                    } elseif ($topic_data['topic_visibility'] == ITEM_UNAPPROVED || $topic_data['topic_visibility'] == ITEM_REAPPROVE) {
                        $sync->add('forum', $topic_data['forum_id'], 'forum_topics_unapproved', -1);
                    } elseif ($topic_data['topic_visibility'] == ITEM_DELETED) {
                        $sync->add('forum', $topic_data['forum_id'], 'forum_topics_softdeleted', -1);
                    }
                    $sync->add('forum', $topic_data['forum_id'], 'forum_posts_approved', -$topic_data['topic_posts_approved']);
                    $sync->add('forum', $topic_data['forum_id'], 'forum_posts_unapproved', -$topic_data['topic_posts_unapproved']);
                    $sync->add('forum', $topic_data['forum_id'], 'forum_posts_softdeleted', -$topic_data['topic_posts_softdeleted']);
                    $sync->forum_last_post($topic_data['forum_id']);
                }
                // new forum
                if ($this->forum_id != 0) {
                    if ($this->topic_visibility == ITEM_APPROVED) {
                        $sync->add('forum', $this->forum_id, 'forum_topics_approved', 1);
                    } elseif ($this->topic_visibility == ITEM_UNAPPROVED) {
                        $sync->add('forum', $this->forum_id, 'forum_topics_unapproved', 1);
                    } elseif ($this->topic_visibility == ITEM_DELETED) {
                        $sync->add('forum', $this->forum_id, 'forum_topics_deleted', 1);
                    }
                    $sync->add('forum', $this->forum_id, 'forum_posts_approved', $this->topic_posts_approved);
                    $sync->add('forum', $this->forum_id, 'forum_posts_unapproved', $this->topic_posts_unapproved);
                    $sync->add('forum', $this->forum_id, 'forum_posts_softdeleted', $this->topic_posts_softdeleted);
                    $sync->forum_last_post($this->forum_id);
                }
            }
            // TODO
            // Sync numbers:
            $phpbb_content_visibility = $phpbb_container->get('content.visibility');
            $phpbb_content_visibility->set_topic_visibility($this->topic_visibility, $this->topic_id, $this->forum_id, $this->topic_delete_user, $this->topic_delete_time, $this->topic_delete_reason);
            // //same with total topics+posts
            // if($topic_data['topic_visibility'] == ITEM_APPROVED)
            // {
            // set_config('num_topics', $config['num_topics'] - 1, true);
            // set_config('num_posts', $config['num_posts'] - (1 + $this->topic_posts_approved), true);
            // }
            // elseif($this->topic_visibility == ITEM_APPROVED)
            // {
            // set_config('num_topics', $config['num_topics'] + 1, true);
            // set_config('num_posts', $config['num_posts'] + (1 + $this->topic_posts_approved), true);
            // }
            // }
            $db->sql_transaction('commit');
        } else {
            // new topic
            $sql_data['forum_id'] = $this->forum_id;
            $sql_data['topic_first_post_id'] = 0;
            $sql_data['topic_last_post_id'] = 0;
            $sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_data);
            $db->sql_query($sql);
            $this->topic_id = $db->sql_nextid();
            if ($this->forum_id != 0) {
                if ($this->topic_visibility == ITEM_APPROVED) {
                    $sync->add('forum', $this->forum_id, 'forum_topics_approved', 1);
                } elseif ($this->topic_visibility == ITEM_UNAPPROVED) {
                    $sync->add('forum', $this->forum_id, 'forum_topics_unapproved', 1);
                } elseif ($this->topic_visibility == ITEM_DELETED) {
                    $sync->add('forum', $this->forum_id, 'forum_topics_deleted', 1);
                }
                $sync->add('forum', $this->forum_id, 'forum_posts_approved', $this->topic_posts_approved);
                $sync->add('forum', $this->forum_id, 'forum_posts_unapproved', $this->topic_posts_unapproved);
                $sync->add('forum', $this->forum_id, 'forum_posts_softdeleted', $this->topic_posts_softdeleted);
                $sync->forum_last_post($this->forum_id);
            }
            $phpbb_content_visibility = $phpbb_container->get('content.visibility');
            $phpbb_content_visibility->set_topic_visibility($this->topic_visibility, $this->topic_id, $this->forum_id, $this->topic_delete_user, $this->topic_delete_time, $this->topic_delete_reason);
            // total topics
            if ($this->topic_visibility == ITEM_APPROVED) {
                set_config('num_topics', $config['num_topics'] + 1, true);
            }
            $sync->new_topic_flag = true;
        }
        // insert or update poll
        if (isset($this->poll_options) && !empty($this->poll_options)) {
            $cur_poll_options = array();
            if ($this->poll_start && isset($topic_data)) {
                $sql = 'SELECT * FROM ' . POLL_OPTIONS_TABLE . '
					WHERE topic_id = ' . $this->topic_id . '
					ORDER BY poll_option_id';
                $result = $db->sql_query($sql);
                $cur_poll_options = array();
                while ($row = $db->sql_fetchrow($result)) {
                    $cur_poll_options[] = $row;
                }
                $db->sql_freeresult($result);
            }
            $sql_insert_ary = array();
            for ($i = 0, $size = sizeof($this->poll_options); $i < $size; $i++) {
                if (trim($this->poll_options[$i])) {
                    if (empty($cur_poll_options[$i])) {
                        $sql_insert_ary[] = array('poll_option_id' => (int) $i, 'topic_id' => (int) $this->topic_id, 'poll_option_text' => (string) $this->poll_options[$i]);
                    } else {
                        if ($this->poll_options[$i] != $cur_poll_options[$i]) {
                            $sql = "UPDATE " . POLL_OPTIONS_TABLE . "\n\t\t\t\t\t\t\tSET poll_option_text = '" . $db->sql_escape($this->poll_options[$i]) . "'\n\t\t\t\t\t\t\tWHERE poll_option_id = " . $cur_poll_options[$i]['poll_option_id'] . "\n\t\t\t\t\t\t\t\tAND topic_id = " . $this->topic_id;
                            $db->sql_query($sql);
                        }
                    }
                }
            }
            $db->sql_multi_insert(POLL_OPTIONS_TABLE, $sql_insert_ary);
            if (sizeof($this->poll_options) < sizeof($cur_poll_options)) {
                $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
					WHERE poll_option_id >= ' . sizeof($this->poll_options) . '
						AND topic_id = ' . $this->topic_id;
                $db->sql_query($sql);
            }
        }
        // delete poll if we had one and poll_start is 0 now
        if (isset($topic_data) && $topic_data['poll_start'] && $this->poll_start == 0) {
            $sql = 'DELETE FROM ' . POLL_OPTIONS_TABLE . '
				WHERE topic_id = ' . $this->topic_id;
            $db->sql_query($sql);
            $sql = 'DELETE FROM ' . POLL_VOTES_TABLE . '
				WHERE topic_id = ' . $this->topic_id;
            $db->sql_query($sql);
        }
        // End poll
        if ($submit_posts && count($this->posts)) {
            // find and sync first post
            if ($sync->new_topic_flag) {
                // test if var was not set in post
                $first_post = $this->posts[0];
                if ($first_post->post_subject == '') {
                    $first_post->post_subject = $this->topic_title;
                }
                if (!$first_post->poster_id) {
                    $first_post->poster_id = $this->topic_poster;
                }
                if ($this->topic_visibility != ITEM_APPROVED) {
                    $first_post->post_visibility = ITEM_UNAPPROVED;
                }
            } elseif ($topic_data && $this->topic_first_post_id != 0) {
                foreach ($this->posts as $post) {
                    if ($post->post_id == $this->topic_first_post_id) {
                        // test if var has been changed in topic. this is like the
                        // else($submit_posts) below, but the user might have changed the
                        // post object but not the topic, so we can't just overwrite them
                        $first_post = $post;
                        if ($this->topic_title != $topic_data['topic_title']) {
                            $first_post->post_subject = $this->topic_title;
                        }
                        if ($this->topic_time != $topic_data['topic_time']) {
                            $first_post->post_time = $this->topic_time;
                        }
                        if ($this->topic_poster != $topic_data['topic_poster']) {
                            $first_post->poster_id = $this->topic_poster;
                            $first_post->post_username = $this->topic_poster == ANONYMOUS ? $this->topic_first_poster_name : '';
                        }
                        if ($this->topic_approved != $topic_data['topic_approved']) {
                            $first_post->post_approved = $this->topic_approved;
                        }
                        break;
                    }
                }
            }
            // TODO sort by post_time in case user messed with it
            foreach ($this->posts as $post) {
                $post->_topic = $this;
                $post->topic_id = $this->topic_id;
                $post->forum_id = $this->forum_id;
                // if(!$post->poster_id) $post->poster_id = $this->topic_poster;
                $post->submit_without_sync($sync);
            }
        } else {
            // sync first post if user edited topic only
            $sync->set('post', $this->topic_first_post_id, array('post_subject' => $this->topic_title, 'post_time' => $this->topic_time, 'poster_id' => $this->topic_poster, 'post_username' => $this->topic_poster == ANONYMOUS ? $this->topic_first_poster_name : '', 'post_visibility' => $this->topic_visibility, 'post_reported' => $this->topic_reported));
        }
        $sync->execute();
        // refresh $this->topic_foo variables...
        $this->refresh_statvars();
    }