Code:
function createTopic($subject, $body, $posterInfo, $boardID, $attachments=array(), $icon = 'xx', $timestamp = 0)
{
global $db_prefix, $modSettings;
// Do some setup work
if ( $timestamp <= 0 || !is_numeric($timestamp) )
$timestamp = time();
if ( empty($subject) || empty($body) || empty($boardID) || empty($posterInfo) )
return false;
// die("icon = '$icon'");
if ( !is_array($posterInfo) )
{
if ( is_numeric($posterInfo) ) // ok its the ID
$where = " ID_MEMBER = $posterInfo";
else // ok its the name
$where = " realName = '$posterInfo'";
$res = db_query("
SELECT ID_MEMBER, realName, emailAddress
FROM {$db_prefix}members
WHERE $where
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($res) == 0 )
return false;
$row = mysql_fetch_row($res);
$posterInfo = array (
'id' => $row[0],
'name' => $row[1],
'email' => $row[2]
);
mysql_free_result($res);
}
// Create the topic
db_query("
INSERT INTO {$db_prefix}topics
(ID_BOARD, ID_MEMBER_STARTED, ID_MEMBER_UPDATED)
VALUES ($boardID, $posterInfo[id], $posterInfo[id])",
__FILE__, __LINE__);
$topicID = mysql_insert_id();
// Create the post
db_query("
INSERT INTO {$db_prefix}messages
(ID_TOPIC, ID_BOARD, posterTime, ID_MEMBER, subject, posterName,
posterEmail, posterIP, body, icon)
VALUES ( $topicID, $boardID, $timestamp, $posterInfo[id], '$subject',
'$posterInfo[name]', '$posterInfo[email]', '$posterInfo[ip]',
'$body', '$icon')",
__FILE__, __LINE__);
$msgID = mysql_insert_id();
// Update the topic
db_query("
UPDATE {$db_prefix}topics
SET ID_FIRST_MSG = $msgID, ID_LAST_MSG = $msgID
WHERE ID_TOPIC = $topicID
LIMIT 1",
__FILE__, __LINE__);
// Update the board
db_query("
UPDATE {$db_prefix}boards
SET ID_LAST_MSG = $msgID, numTopics = numTopics + 1, numPosts = numPosts + 1
WHERE ID_BOARD = $boardID",
__FILE__, __LINE__);
// Add in attachments
if ( !empty ( $attachments) && is_writable($modSettings['attachmentUploadDir']))
{
foreach($attachments AS $attach)
{
if ( !is_uploaded_file($attach['tmp_name']) || !file_exists($attach['tmp_name']) )
{
continue;
}
if ( empty($modSettings['attachmentEncryptFilenames']) )
$attach['name'] = getAttachmentFilename($attach['name'], false, true);
$attach['name'] = basename($attach['name']);
if ( empty($modSettings['attachmentEncryptFilenames']) )
{
$res = db_query("
SELECT ID_ATTACH
FROM {$db_prefix}attachments
WHERE filename = '" . strtolower($attach['name']) . "'
LIMIT 1", __FILE__, __LINE__);
$num = mysql_num_rows($res);
mysql_free_result($res);
if ( $num > 0 )
{
continue;
}
}
db_query("
INSERT INTO {$db_prefix}attachments (
ID_MSG, ID_MEMBER, filename, size )
VALUES ( $msgID, $posterInfo[id], '$attach[name]', $attach[size] )",
__FILE__, __LINE__);
$attach['dest_name'] = $modSettings['attachmentUploadDir'] . "/" . getAttachmentFilename($attach['name'], db_insert_id(), true);
move_uploaded_file($attach['tmp_name'], $attach['dest_name']);
@chmod($attach['dest_name'], 0644);
}
}
// check to see if we are counting posts
$res = db_query("
SELECT countPosts
FROM {$db_prefix}boards
WHERE ID_BOARD=$boardID
LIMIT 1",
__FILE__, __LINE__);
list($countPost) = mysql_fetch_row($res);
mysql_free_result($res);
// if so increment
if ( empty($countPost) )
db_query("UPDATE {$db_prefix}members SET posts = posts + 1 WHERE ID_MEMBER=$posterInfo[id]", __FILE__, __LINE__);
updateStats('topic');
updateStats('message');
}