function CheckTopicExpired($id) { $sql = GetSQL(); $result = $sql->safequery("SELECT state,goods,bads,time FROM Topics WHERE id={$id}"); $row = $result->fetch_row(); if ($row === NULL) { throw new Exception('Invalid page.'); } if ($row[0] == TopicStates::Old) { return 2; } if ($row[0] == TopicStates::Deleted) { return 1; } if ($row[0] != TopicStates::Live) { throw new Exception('Invalid page.'); } return CheckTopicExpired2($id, $row[1], $row[2], $row[3]); }
function GetNewPage() { global $SLOTS, $g_account, $apath; $sql = GetSQL(); $result = $sql->safequery("LOCK TABLES Topics WRITE, TopicVotes READ, Accounts WRITE"); $result = $sql->safequery('SELECT id,state,time,vote,goods,bads FROM Topics LEFT JOIN TopicVotes ON (topicid=id AND TopicVotes.account=' . $g_account->id . ') WHERE (state=' . TopicStates::Live . ' OR state=' . TopicStates::Composing . ') LIMIT ' . $SLOTS); if ($result->num_rows < $SLOTS) { if (time() > $g_account->lastcompose + $GLOBALS['COMPOSE_DELAY']) { // chance to make a new //if( mt_rand( 0, $SLOTS-1 ) >= $result->num_rows ) { // start new composition $sql->safequery('INSERT INTO Topics ( account,state,time ) VALUES ( ' . $g_account->id . ', ' . TopicStates::Composing . ', ' . time() . ')'); $result = $sql->safequery('SELECT LAST_INSERT_ID()'); $row = $result->fetch_row(); $g_account->page = $row[0]; $g_account->lastcompose = time(); $sql->safequery('UPDATE Accounts SET page=' . $g_account->page . ', lastcompose=' . $g_account->lastcompose . ' WHERE id=' . $g_account->id); $sql->safequery('UNLOCK TABLES'); return; //} } } $sql->safequery('UNLOCK TABLES'); $choices = array(); while ($row = $result->fetch_assoc()) { if ($row['state'] == TopicStates::Composing) { if (time() >= $row['time'] + $GLOBALS['COMPOSE_TIMEOUT']) { // delete timed-out composition. // we double-check that it is still in composition mode. $sql->safequery('DELETE FROM Topics WHERE id=' . $row['id'] . ' AND state=' . TopicStates::Composing); continue; } } else { if ($row['state'] == TopicStates::Live) { if (CheckTopicExpired2($row['id'], $row['goods'], $row['bads'], $row['time'])) { continue; } // skip downvoted topics. if (!is_null($row['vote']) && $row['vote'] == 0) { continue; } $choices[] = $row['id']; } } } if (empty($choices)) { $g_account->page = 0; SaveAccount($g_account, 0); return; } $choice = $choices[mt_rand(0, count($choices) - 1)]; $g_account->page = $choice; SaveAccount($g_account, $g_account->page); }