function dok_ask_dup_song($VARS, $update, $theme_path)
{
    global $SONGS_LABELS;
    if (!is_array($VARS['duplicates']) || !sizeof($VARS['duplicates'])) {
        $t = dok_error_template(MSG_ERR_NO_DUP_SONG);
        return array($t, sprintf(MSG_TITLE_DUP_SONG, ''));
    }
    if (!$VARS['label'] || $VARS['label'] == 0) {
        $new_song_label = MSG_LABEL_NONE;
    } else {
        $new_song_label = $SONGS_LABELS[$VARS['label']]['label'];
    }
    if (!$VARS['track'] || $VARS['track'] == 0) {
        $new_song_track = MSG_ALBUM_NEXT_TRACK;
    } else {
        $new_song_track = $VARS['track'];
    }
    /*
     *find related songs
     *
     */
    //$res = dok_oquery('select * from '.dok_tn('rel_songs').' where song_id1 =
    $t = new template($theme_path);
    $t->set_file('page', 'song_dup.tpl');
    $t->set_block('page', 'duplicate', 'dup_block');
    $query = 'select * from ' . dok_tn('song') . ' where id in(' . implode(',', $VARS['duplicates']) . ')';
    $res = mysql_query($query);
    $relations = dok_songs_links_array();
    while ($row = mysql_fetch_array($res)) {
        $t->set_var(dok_song_format($row));
        $relations_select = '<select name="link[' . $row['id'] . ']"><option value="" SELECTED>' . MSG_RELATION_NONE . '</option>' . "\n";
        foreach ($relations as $key => $val) {
            $relations_select .= '<option value="' . $key . '">' . $val . '</option>' . "\n";
        }
        $relations_select .= '</select>';
        $t->set_var('SONG_RELATION_SELECT', $relations_select);
        $t->parse('dup_block', 'duplicate', 'true');
    }
    if ($update == 'create_song') {
        $res = mysql_query('select name from ' . dok_tn('album') . ' where id = ' . $VARS['album']);
        if (!mysql_numrows($res)) {
            $album = MSG_UNKNOWN;
        } else {
            $album = mysql_result($res, 0, 'name');
        }
        $res = mysql_query('select name from ' . dok_tn('artist') . ' where id = ' . $VARS['artist']);
        if (!mysql_numrows($res)) {
            $artist = MSG_UNKNOWN;
        } else {
            $artist = mysql_result($res, 0, 'name');
        }
        $t->set_var(array('NEW_SONG_NAME' => $VARS['name'], 'NEW_SONG_COMMENT' => dok_textarea_2_db($VARS['comment']), 'NEW_SONG_TRACK' => $new_song_track, 'NEW_SONG_LENGTH' => $VARS['length'], 'NEW_SONG_LABEL' => $new_song_label, 'NEW_SONG_ARTIST' => $artist, 'NEW_SONG_ALBUM' => $album, 'NEW_SONG_GENRE' => dok_genre_name($VARS['genre']), 'NEW_SONG_RELEASE' => dok_year2str($VARS['release'])));
    } else {
        $t->set_var(array('NEW_SONG_NAME' => $VARS['name'], 'NEW_SONG_COMMENT' => dok_textarea_2_db($VARS['comment']), 'NEW_SONG_TRACK' => $new_song_track, 'NEW_SONG_LENGTH' => dok_sec2str($VARS['length']), 'NEW_SONG_GENRE' => dok_genre_name($VARS['genre']), 'NEW_SONG_LABEL' => $new_song_label, 'NEW_SONG_ARTIST' => '', 'NEW_SONG_ALBUM' => '', 'NEW_SONG_RELEASE' => dok_year2str($VARS['release'])));
    }
    $yes_form = '<form method="post" action="' . $_SERVER['PHP_SELF'] . '"><input type=hidden name="update" value="' . $update . '">';
    $yes_form .= '<input type=hidden name="dup_checked" value="1">';
    if ($update == 'update_song') {
        $yes_form .= '<input type=hidden name="id" value="' . $VARS['id'] . '">';
    }
    $yes_form .= '<input type=hidden name="artist" value="' . $VARS['artist'] . '">';
    $yes_form .= '<input type=hidden name="album" value="' . $VARS['album'] . '">';
    $yes_form .= '<input type=hidden name="track" value="' . str_replace('"', '&quot;', $VARS['track']) . '">';
    $yes_form .= '<input type=hidden name="name" value="' . str_replace('"', '&quot;', $VARS['name']) . '">';
    $yes_form .= '<input type=hidden name="length" value="' . str_replace('"', '&quot;', $VARS['length']) . '">';
    $yes_form .= '<input type=hidden name="release" value="' . str_replace('"', '&quot;', $VARS['release']) . '">';
    $yes_form .= '<input type=hidden name="comment" value="' . str_replace('"', '&quot;', $VARS['comment']) . '">';
    $yes_form .= '<input type=hidden name="label" value="' . str_replace('"', '&quot;', $VARS['label']) . '">';
    $yes_form .= '<input type=hidden name="genre" value="' . $VARS['genre'] . '">';
    $t->set_var('SONG_RECORD_FORM', $yes_form);
    return array($t, MSG_TITLE_DUP_SONG);
}
function dok_update_song()
{
    global $VARS, $SONGS_LABELS;
    if (!isset($VARS['id']) || !is_numeric($VARS['id']) || $VARS['id'] < 1) {
        dok_msg(MSG_ERR_SONG_NOT_FOUND_UPDATE, 'dok_update_song', 'e');
        return false;
    }
    $res = mysql_query('select * from ' . dok_tn('song') . ' where id = ' . $VARS['id']);
    if (!mysql_numrows($res)) {
        dok_msg(MSG_ERR_SONG_NOT_FOUND_UPDATE, 'dok_update_song', 'e');
        return false;
    }
    $song = mysql_fetch_array($res);
    $set = array();
    $name = ucwords(trim($VARS['name']));
    if (is_string($name) && strlen($name) && $name != $song['name']) {
        if (!$VARS['dup_checked']) {
            //check if name is already known
            $res = dok_oquery('select id from ' . dok_tn('song') . ' where name = \'' . addslashes($name) . '\' and id != ' . $VARS['id']);
            if ($res->numrows()) {
                $VARS['duplicates'] = $res->fetch_col_array('id');
                return 'ask_dup_song';
            }
        }
        $set[] = 'name = \'' . addslashes($name) . '\'';
    }
    $comment = dok_textarea_2_db($VARS['comment']);
    if ($comment != $song['comment']) {
        $set[] = 'comment = \'' . addslashes($VARS['comment']) . '\'';
    }
    if (!isset($VARS['release']) || !is_numeric($VARS['release']) || $VARS['release'] < 1901 || $VARS['release'] > 2155) {
        $VARS['release'] = 0;
    }
    if ($VARS['release'] != $song['release']) {
        $set[] = 'release = ' . $VARS['release'];
    }
    $length = 0;
    if (isset($VARS['length'])) {
        if (preg_match('/:/', $VARS['length'])) {
            $test = explode(':', $VARS['length']);
            if (sizeof($test) > 1) {
                $sec = 0;
                if (is_numeric($test[0])) {
                    $sec = $test[0] * 60;
                }
                if (is_numeric($test[1])) {
                    $sec += $test[1];
                }
                $length = $sec;
            }
        } elseif (is_numeric($VARS['length']) && $VARS['length'] > 0) {
            $length = $VARS['length'];
        }
    }
    if ($length != $song['length']) {
        $set[] = 'length = ' . $length;
    }
    if (is_numeric($VARS['genre']) && $VARS['genre'] >= 0 && $VARS['genre'] != $song['genre']) {
        $set[] = 'genre = ' . $VARS['genre'];
    }
    if (is_numeric($VARS['label']) && $VARS['label'] != $song['label'] && (in_array($VARS['label'], array_keys($SONGS_LABELS)) && strlen($SONGS_LABELS[$VARS['label']]['label']) || $VARS['label'] == 0)) {
        $set[] = 'label = ' . $VARS['label'];
    }
    //	print_r($set);
    if (sizeof($set)) {
        $res = dok_uquery('update ' . dok_tn('song') . ' set ' . implode(',', $set) . ' where id = ' . $VARS['id']);
    }
    if ($res) {
        $VARS['nohit'] = 1;
        return 'view_song';
    } else {
        dok_msg(MSG_ERR_DB_UPDATE_FAILED, 'dok_update_song', 'e');
        return false;
    }
}
function dok_create_song()
{
    global $VARS, $GENRES, $USER, $SONGS_LABELS;
    if (!isset($VARS['name']) || !strlen(trim($VARS['name']))) {
        dok_msg(MSG_ERR_SONG_NO_NAME, 'dok_create_song', 'e');
        return false;
    }
    if (!is_numeric($VARS['album']) || $VARS['album'] < 1) {
        $VARS['album'] = 0;
    }
    $res = mysql_query('select name from ' . dok_tn('album') . ' where id = ' . $VARS['album']);
    if (!mysql_numrows($res)) {
        dok_msg(MSG_ERR_NO_ALBUM_NAME, 'dok_create_song', 'e');
        return false;
    }
    $album_name = mysql_result($res, 0, 'name');
    $_SESSION['song_select_album'] = $VARS['album'];
    if (!is_numeric($VARS['artist']) || $VARS['artist'] < 1) {
        $VARS['artist'] = 0;
    }
    $res = mysql_query('select name from ' . dok_tn('artist') . ' where id = ' . $VARS['artist']);
    if (!mysql_numrows($res)) {
        dok_msg(MSG_ERR_NO_ARTIST_NAME, 'dok_create_song', 'e');
        return false;
    }
    $artist_name = mysql_result($res, 0, 'name');
    $_SESSION['song_select_artist'] = $VARS['artist'];
    $song_name = substr($VARS['name'], 0, 255);
    if (!$VARS['dup_checked']) {
        $res = dok_oquery('select id from ' . dok_tn('song') . ' where name = \'' . addslashes($song_name) . '\'');
        if ($res->numrows()) {
            //dok_msg(MSG_ERR_SONG_DUP_NAME,'dok_create_song','e');
            $VARS['duplicates'] = $res->fetch_col_array('id');
            return 'ask_dup_song';
        }
    }
    $song_name = ucwords($song_name);
    if ($VARS['album_track'] != 'text') {
        $t_res = mysql_query('select max(track) as m from ' . dok_tn('rel_song_album') . ' where album_id = ' . $VARS['album']);
        $VARS['track'] = mysql_result($t_res, 0, 'm') + 1;
        $_SESSION['album_track'] = 'next';
    } else {
        if (!is_numeric($VARS['track']) || $VARS['track'] < 1) {
            dok_msg(MSG_ERR_NO_TRACK, 'dok_create_song', 'e');
            return false;
        }
        $_SESSION['album_track'] = 'text';
    }
    $res = mysql_query('select song_id from ' . dok_tn('rel_song_album') . ' where album_id = ' . $VARS['album'] . ' and track = ' . $VARS['track']);
    if (mysql_numrows($res)) {
        $dup_song_id = mysql_result($res, 0, 'song_id');
        $res = mysql_query('select name from ' . dok_tn('song') . ' where id = ' . $dup_song_id);
        $dup_song_name = mysql_result($res, 0, 'name');
        dok_msg(sprintf(MSG_ERR_SONG_TRACK_DUP, $dup_song_name, $VARS['track']), 'dok_create_song', 'e');
        return false;
    }
    //check comment
    $comment = dok_textarea_2_db($VARS['comment']);
    if (!isset($VARS['release']) || !is_numeric($VARS['release']) || $VARS['release'] < 1901 || $VARS['release'] > 2155) {
        $VARS['release'] = 0;
    }
    $length = dok_str2sec($VARS['length']);
    if ($VARS['genre'] >= sizeof($GENRES)) {
        $genre = 0;
    } else {
        $genre = $VARS['genre'];
        $_SESSION['song_select_genre'] = $genre;
    }
    if ($VARS['label'] && $VARS['label'] > 0 && in_array($VARS['label'], array_keys($SONGS_LABELS)) && strlen($SONGS_LABELS[$VARS['label']]['label'])) {
        $label = $VARS['label'];
    } else {
        $label = 0;
    }
    if (DOK_ENABLE_USER) {
        $creation_uid = $USER->id;
    } else {
        $creation_uid = 0;
    }
    //add
    $res = dok_uquery('insert into ' . dok_tn('song') . ' (name, length, creation, creation_uid, release, comment, genre, label) values (\'' . addslashes($song_name) . '\', ' . $length . ', ' . time() . ',' . $creation_uid . ',' . $VARS['release'] . ',\'' . addslashes($comment) . '\',' . $genre . ', ' . $label . ')');
    if (!$res) {
        echo mysql_error();
        return false;
    }
    $my_id = mysql_insert_id();
    $res = dok_uquery('insert into ' . dok_tn('rel_song_artist') . ' (song_id, artist_id) values (' . $my_id . ',' . $VARS['artist'] . ')');
    if (!$res) {
        echo mysql_error();
        return false;
    }
    $res = dok_uquery('insert into ' . dok_tn('rel_song_album') . ' (song_id, album_id,track ) values (' . $my_id . ',' . $VARS['album'] . ',' . $VARS['track'] . ')');
    if (!$res) {
        echo mysql_error();
        return false;
    }
    $VARS['id'] = $my_id;
    $VARS['nohit'] = 1;
    if (sizeof($VARS['link'])) {
        $links = array_keys(dok_songs_links_array());
        foreach ($VARS['link'] as $key => $val) {
            if (is_numeric($key) && $key > 0 && strlen($val) && in_array($val, $links)) {
                $res = mysql_query('select name from ' . dok_tn('song') . ' where id = ' . $key);
                if (mysql_numrows($res)) {
                    $res = dok_song_link_add($VARS['id'], $key, $val);
                }
            }
        }
    }
    return 'view_song';
}