Exemple #1
0
function insertArticle()
{
    global $tbl_1d_article, $tbl_1d_pictures;
    global $SUBS, $PARAM, $MSG;
    global $SNIMKA, $SNIMKA_name;
    //file upload
    global $PHOTOS, $adminID;
    //from session
    if ($PARAM['Add'] == 1) {
        $SUBS['ID'] = $PARAM['ID'];
        //da ze zapische, ako e podadeno
        $SUBS['ERROR'] = '';
        if ($PARAM['SECTION'] == '0') {
            $SUBS['ERROR'] = $MSG[20070];
        } else {
            $SUBS['SECTION' . strToUpper($PARAM['SECTION'])] = ' SELECTED';
        }
        if ($PARAM['TITLE'] != '') {
            //zaglavie
            $SUBS['TITLE'] = htmlEncode($PARAM['TITLE']);
        } else {
            $SUBS['ERROR'] = $MSG[20069];
        }
        if ($PARAM['INFO'] != '') {
            $SUBS['INFO'] = htmlEncode($PARAM['INFO']);
        } else {
            if ($SUBS['ERROR'] == '') {
                $SUBS['ERROR'] = $MSG[20071];
            }
        }
        if ($SUBS['ERROR'] == '') {
            //wsichko e ok, zapiswane w DB
            if ($PARAM['ID'] != '') {
                $query = "UPDATE {$tbl_1d_article} SET\n\t\t\t\t\tTitle = " . dbQuote($PARAM['TITLE']) . ",\n\t\t\t\t\tCaption = " . dbQuote($PARAM['INFO']) . ",\n\t\t\t\t\ttsLast = " . dbQuote(time()) . "\n\t\t\t\t\tWHERE ID=" . $PARAM['ID'];
            } else {
                $query = "INSERT INTO {$tbl_1d_article}\n\t\t\t\t(Title, Caption, tsLast, tsWhen, adminID)\n\t\t\t\tVALUES\n\t\t\t\t(" . dbQuote($PARAM['TITLE']) . ",\n\t\t\t\t" . dbQuote($PARAM['INFO']) . ",\n\t\t\t\t" . dbQuote(time()) . ", " . dbQuote(getNextWeek()) . ", {$adminID} )";
            }
            $result = runQuery($query, 'insertArticle()', 'ADD_TEXT_ARTILE_INFO');
            if ($PARAM['ID'] == '') {
                $PARAM['ID'] = mysql_insert_id();
                //zaradi snimkite ot PHOTOs
                if (session_is_registered('PHOTOS') && is_array($PHOTOS)) {
                    for ($i = 0; $i < count($PHOTOS[session_id()]); $i++) {
                        if ($INFO = checkPicture($PHOTOS[session_id()][$i])) {
                            fixPicture($PHOTOS[session_id()][$i], 'article', $PARAM['ID'], $INFO);
                        }
                    }
                }
                unset($PHOTOS[session_id()]);
            }
            //prezarejdane
            if ($PARAM['Photo'] != 1) {
                $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20018&ID=" . $PARAM['ID'];
                printPage('_admin_done.htmlt');
                return;
            }
        } else {
            $SUBS['ERROR'] = fileParse('_admin_error.htmlt');
        }
    }
    if ($PARAM['Photo'] == 1) {
        //dobawi snimka
        if (!session_is_registered('PHOTOS')) {
            //izpolzwa se ako nyama oschte registrirano ID
            session_register('PHOTOS');
            $PHOTOS = array();
        }
        $SUBS['ERROR'] = '';
        if ($SNIMKA == 'none') {
            $SUBS['ERROR'] = $MSG[20019];
        }
        if (!($INFO = checkPicture($SNIMKA))) {
            //pass INFO as parameter to fixPicture
            $SUBS['ERROR'] = $MSG[20020];
        }
        if ($SUBS['ERROR'] != '') {
            $SUBS['ERROR'] = fileParse('_admin_error.htmlt');
        } else {
            //do tha job
            if ($PARAM['ID'] == '') {
                $where = getAdmSetting('TEMPORARY_DIR') . session_id() . md5($INFO[0] + $INFO[1]) . $SNIMKA_name;
                if (@copy($SNIMKA, $where)) {
                    if (!@in_array($where, $PHOTOS[session_id()])) {
                        if (!is_array($PHOTOS[session_id()])) {
                            $PHOTOS[session_id()] = array();
                        }
                        $PHOTOS[session_id()][] = $where;
                    }
                } else {
                    setLogAndStatus("Writing", $SNIMKA, 0, "insertArticle()", 'WRITE_SESSION_PICS');
                }
            } else {
                fixPicture($SNIMKA, 'article', $PARAM['ID'], $INFO);
                //prezarejdane
                $SUBS['COMMAND'] = $PARAM['cmd'] . "&err=20022&ID=" . $PARAM['ID'];
                printPage('_admin_done.htmlt');
                return;
            }
        }
    }
    if ($PARAM['Delete'] != '') {
        //iztrij snimka
        if ($PARAM['ID'] != '') {
            //get thumbnail
            $query = "SELECT\tURL, ID\n\t\t\t\tFROM {$tbl_1d_pictures}\n\t\t\t\tWHERE RefID = " . dbQuote($PARAM['Delete']) . "\n\t\t\t\t\tAND RefType= 'thumb' ";
            $result = runQuery($query, 'insetFilm()', 'GET_THUMBS');
            if ($row = db_fetch_row($result)) {
                if (!@unlink(getAdmSetting('UPLOAD_DIR') . $row[0])) {
                    setLogAndStatus("Erasing", getAdmSetting('UPLOAD_DIR') . $row[0], 0, "insertArticle()", 'DEL_THUMBS');
                }
                //erase thumbnail
                $query = "DELETE FROM {$tbl_1d_pictures}\n\t\t\t\t\tWHERE ID = {$row['1']}";
                $result = runQuery($query, 'insertArticle()', 'DEL_THUMBS_DB');
            }
            $query = "SELECT\tURL, ID\n\t\t\t\tFROM {$tbl_1d_pictures}\n\t\t\t\tWHERE ID = " . dbQuote($PARAM['Delete']);
            $result = runQuery($query, 'insertArticle()', 'GET_PIC');
            if ($row = db_fetch_row($result)) {
                if (!@unlink(getAdmSetting('UPLOAD_DIR') . $row[0])) {
                    setLogAndStatus("Erasing", getAdmSetting('UPLOAD_DIR') . $row[0], 0, "insertArticle()", 'DEL_PICS');
                }
                //erase pic
                $query = "DELETE FROM {$tbl_1d_pictures}\n\t\t\t\t\tWHERE ID = {$row['1']}";
                $result = runQuery($query, 'insertArticle()', 'DEL_PICS');
            }
        } else {
            if (!@unlink(getAdmSetting('UPLOAD_DIR') . $PHOTOS[session_id()][$PARAM['Delete']])) {
                //from session
                setLogAndStatus("Erasing", getAdmSetting('UPLOAD_DIR') . $PHOTOS[session_id()][$PARAM['Delete']], 0, "insertArticle()", 'DEL_SESSIONS');
            }
            unset($PHOTOS[session_id()][$PARAM['Delete']]);
        }
    }
    //display
    $SUBS['SECTION' . strToUpper($PARAM['w'])] = ' SELECTED';
    if ($PARAM['Add'] != 1) {
        //podgotowka za pokazwane
        $query = "SELECT\tTitle,\n\t\t\t\tType,\n\t\t\t\tCaption\n\t\t\tFROM {$tbl_1d_article}\n\t\t\tWHERE ID = " . dbQuote($PARAM['ID']);
        $result = runQuery($query, 'insertArticle()', 'GET_TEXT_ARTICLE_INFO');
        if ($row = db_fetch_row($result)) {
            $SUBS['TITLE'] = htmlEncode($row[0]);
            $SUBS['SECTION' . strToUpper($row[1])] = ' SELECTED';
            $SUBS['INFO'] = htmlEncode($row[2]);
        } else {
            if ($PARAM['ID'] != '' && $SUBS['ERROR'] == '') {
                $SUBS['ERROR'] = $MSG[20081];
                $SUBS['ERROR'] = fileParse('_admin_error.htmlt');
            }
            $PARAM['ID'] = '';
        }
    }
    //pokaji snimkite
    if ($PARAM['ID'] == '') {
        for ($i = 0; $i < count($PHOTOS[session_id()]); $i++) {
            $SUBS['URL'] = $PHOTOS[session_id()][$i];
            $SUBS['IND'] = $i;
            $SUBS['THUMB'] = " &nbsp; " . $MSG[20024] . " " . ($i + 1);
            $SUBS['SNIMKAS'] .= fileParse('_admin_edit_film_snimka.htmlt');
        }
    } else {
        $query = "SELECT\tURL,\n\t\t\t\tWidth,\n\t\t\t\tHeight,\n\t\t\t\tID\n\t\t\tFROM {$tbl_1d_pictures}\n\t\t\tWHERE (RefID LIKE " . dbQuote($PARAM['ID']) . ")\n\t\t\t\tAND RefType = 'article' ";
        $result = runQuery($query, 'insertArticle()', 'GET_PICS_ARTICLE_INFO');
        $upload = getAdmSetting('UPLOAD_DIR');
        $SUBS['UPLOAD'] = $upload;
        while ($row = db_fetch_row($result)) {
            $query = "SELECT\tURL,\n\t\t\t\t\tWidth,\n\t\t\t\t\tHeight\n\t\t\t\tFROM {$tbl_1d_pictures}\n\t\t\t\tWHERE (RefID = {$row['3']}) AND RefType = 'thumb' ";
            $res = runQuery($query, 'insertArtcile()', 'GET_ARTICLE_THUMB');
            $thumb = db_fetch_row($res);
            $SUBS['URL'] = $row[0];
            $SUBS['IND'] = $row[3];
            $SUBS['THUMB'] = "<img border=\"0\" width=\"{$thumb['1']}\" height=\"{$thumb['2']}\" src=\"{$upload}{$thumb['0']}\">";
            $SUBS['SNIMKAS'] .= fileParse('_admin_edit_article_snimka.htmlt');
        }
    }
    ////----[Mrasnika's] Edition 13.10.2002
    $SUBS['FILM_NAV'] = fileParse('_admin_edit_article2.htmlt');
    $SUBS['ID'] = $PARAM['ID'];
    if ($PARAM['err'] != '' && $SUBS['ERROR'] == '') {
        $SUBS['ERROR'] = $MSG[$PARAM['err']];
        $SUBS['ERROR'] = fileParse('_admin_error.htmlt');
    }
    printPage('_admin_edit_article.htmlt');
}
function fixPicture($filename, $type, $id, $PICPARAM)
{
    global $tbl_1d_pictures;
    $OK = 0;
    $name = $type . "_" . time() . "_" . md5($PICPARAM[0] . $PICPARAM[1] . $filename);
    switch ($type) {
        //margins
        case 'dvd':
            $MARGINS['IMG_WIDTH'] = getAdmSetting('DVD_WIDTH');
            $MARGINS['IMG_HEIGHT'] = getAdmSetting('DVD_HEIGHT');
            break;
        case 'video':
            $MARGINS['IMG_WIDTH'] = getAdmSetting('VIDEO_WIDTH');
            $MARGINS['IMG_HEIGHT'] = getAdmSetting('VIDEO_HEIGHT');
            break;
        case 'thumb':
            $MARGINS['IMG_WIDTH'] = getAdmSetting('THUMB_WIDTH');
            $MARGINS['IMG_HEIGHT'] = getAdmSetting('THUMB_HEIGHT');
            break;
        case 'article':
            $MARGINS['IMG_WIDTH'] = getAdmSetting('ARTICLE_WIDTH');
            $MARGINS['IMG_HEIGHT'] = getAdmSetting('ARTICLE_HEIGHT');
            break;
        default:
            //CASE film
            $MARGINS['IMG_WIDTH'] = getAdmSetting('IMG_WIDTH');
            $MARGINS['IMG_HEIGHT'] = getAdmSetting('IMG_HEIGHT');
    }
    switch ($PICPARAM[2]) {
        case 3:
            $src = @ImageCreateFromPNG($filename);
        case 2:
            if ($PICPARAM[2] == 2) {
                $src = @ImageCreateFromJPEG($filename);
            }
            if ($PICPARAM[0] < $MARGINS['IMG_WIDTH']) {
                if ($PICPARAM[1] < $MARGINS['IMG_HEIGHT']) {
                    $OK = 1;
                } else {
                    $percent = $PICPARAM[1] / $MARGINS['IMG_HEIGHT'];
                }
            } else {
                if ($PICPARAM[1] < $MARGINS['IMG_HEIGHT']) {
                    $percent = $PICPARAM[0] / $MARGINS['IMG_WIDTH'];
                } else {
                    $img_per[0] = $PICPARAM[0] / $MARGINS['IMG_WIDTH'];
                    $img_per[1] = $PICPARAM[1] / $MARGINS['IMG_HEIGHT'];
                    $percent = max($img_per);
                }
            }
            $new = getAdmSetting('UPLOAD_DIR') . "{$name}.JPG";
            switch ($type) {
                case 'thumb':
                    $dst = @ImageCreateFromJPEG(getAdmSetting('TEMPLATES_DIR') . 'default.jpg');
                    $img_per[0] = $PICPARAM[0] / $MARGINS['IMG_WIDTH'];
                    $img_per[1] = $PICPARAM[1] / $MARGINS['IMG_HEIGHT'];
                    $percent = min($img_per);
                    $img_2[0] = $MARGINS['IMG_WIDTH'] * $percent;
                    $img_2[1] = $MARGINS['IMG_HEIGHT'] * $percent;
                    $img_cor[0] = ($PICPARAM[0] - $MARGINS['IMG_WIDTH'] * $percent) / 2;
                    $img_cor[1] = ($PICPARAM[1] - $MARGINS['IMG_HEIGHT'] * $percent) / 2;
                    ImageCopyResampled($dst, $src, getAdmSetting('THUMB_LEFT'), getAdmSetting('THUMB_TOP'), $img_cor[0], $img_cor[1], $MARGINS['IMG_WIDTH'], $MARGINS['IMG_HEIGHT'], $img_2[0], $img_2[1]);
                    ImageJPEG($dst, $new);
                    $PICPARAM = getImageSize($new);
                    break;
                case 'video':
                case 'dvd':
                    $dst = @ImageCreateTrueColor($MARGINS['IMG_WIDTH'], $MARGINS['IMG_HEIGHT']);
                    $img_per[0] = $PICPARAM[0] / $MARGINS['IMG_WIDTH'];
                    $img_per[1] = $PICPARAM[1] / $MARGINS['IMG_HEIGHT'];
                    $percent = min($img_per);
                    $img_2[0] = $MARGINS['IMG_WIDTH'] * $percent;
                    $img_2[1] = $MARGINS['IMG_HEIGHT'] * $percent;
                    $img_cor[0] = ($PICPARAM[0] - $MARGINS['IMG_WIDTH'] * $percent) / 2;
                    $img_cor[1] = ($PICPARAM[1] - $MARGINS['IMG_HEIGHT'] * $percent) / 2;
                    ImageCopyResampled($dst, $src, 0, 0, $img_cor[0], $img_cor[1], $MARGINS['IMG_WIDTH'], $MARGINS['IMG_HEIGHT'], $img_2[0], $img_2[1]);
                    ImageJPEG($dst, $new);
                    $PICPARAM = getImageSize($new);
                    break;
                default:
                    if ($OK == 1) {
                        //no resize
                        @ImageJPEG($src, $new);
                    } else {
                        $img_2[0] = round($PICPARAM[0] / $percent);
                        $img_2[1] = round($PICPARAM[1] / $percent);
                        $dst = @ImageCreateTrueColor($img_2[0], $img_2[1]);
                        ImageCopyResampled($dst, $src, 0, 0, 0, 0, $img_2[0], $img_2[1], $PICPARAM[0], $PICPARAM[1]);
                        //src w,h
                        ImageJPEG($dst, $new);
                        $PICPARAM[0] = $img_2[0];
                        $PICPARAM[1] = $img_2[1];
                    }
                    break;
            }
            $query = "INSERT INTO {$tbl_1d_pictures}\n\t\t\t\t\t(URL, RefID, RefType, Width, Height) VALUES\n\t\t\t\t\t(" . dbQuote("{$name}.JPG") . ", {$id}, " . dbQuote($type) . ", {$PICPARAM['0']}, {$PICPARAM['1']})";
            $result = runQuery($query, 'fixPicture()', 'INSERT_PIC');
            $id = mysql_insert_id();
            switch ($type) {
                //thumbnails
                case 'article':
                case 'film':
                    fixPicture($new, 'thumb', $id, $PICPARAM);
                    break;
            }
            return 1;
            break;
        default:
            return 0;
    }
}