/**
 * Checks to see if the current URL matches the correct one, redirects to the
 * corrected URL if not with a 301 Moved Permanently.
 */
function fix_path_redirect()
{
    if (MOD_REWRITE) {
        $sfx = IM_SUFFIX;
        $request_uri = urldecode($_SERVER['REQUEST_URI']);
        $i = strpos($request_uri, '?');
        if ($i !== false) {
            $params = substr($request_uri, $i + 1);
            $request_uri = substr($request_uri, 0, $i);
        } else {
            $params = '';
        }
        if (strlen($sfx) > 0 && in_context(ZP_IMAGE) && substr($request_uri, -strlen($sfx)) != $sfx) {
            $redirecturl = zpurl(true, NULL, NULL, NULL, $params);
            header("HTTP/1.0 301 Moved Permanently");
            header("Status: 301 Moved Permanently");
            header('Location: ' . FULLWEBPATH . '/' . $redirecturl);
            exit;
        }
    }
}
function zp_handle_comment()
{
    global $_zp_current_image, $_zp_current_album, $_zp_comment_stored, $_zp_current_zenpage_news, $_zp_current_zenpage_page;
    $activeImage = false;
    $comment_error = 0;
    $cookie = zp_getCookie('zenphoto');
    if (isset($_POST['comment'])) {
        // ZENPAGE:  if else constructs added
        if (getOption('zp_plugin_zenpage')) {
            //zenpage_news = new ZenpageNews();
            //$zenpage_pages = new ZenpagePage();
            $zenpage_news_context = isPage(ZENPAGE_NEWS);
            $zenpage_pages_context = isPage(ZENPAGE_PAGES);
        } else {
            $zenpage_news_context = FALSE;
            $zenpage_pages_context = FALSE;
        }
        if ($zenpage_news_context) {
            $redirectTo = FULLWEBPATH . '/index.php?p=' . ZENPAGE_NEWS . '&title=' . $_zp_current_zenpage_news->getTitlelink();
        } else {
            if ($zenpage_pages_context) {
                $redirectTo = FULLWEBPATH . '/index.php?p=' . ZENPAGE_NEWS . '&title=' . $_zp_current_zenpage_page->getTitlelink();
            } else {
                $redirectTo = FULLWEBPATH . '/' . zpurl();
            }
        }
        if (in_context(ZP_ALBUM) || $zenpage_news_context || $zenpage_pages_context) {
            $p_name = sanitize($_POST['name'], 3);
            if (isset($_POST['email'])) {
                $p_email = sanitize($_POST['email'], 3);
            } else {
                $p_email = "";
            }
            if (isset($_POST['website'])) {
                $p_website = sanitize($_POST['website'], 3);
            } else {
                $p_website = "";
            }
            $p_comment = sanitize($_POST['comment'], 1);
            $p_server = sanitize($_SERVER['REMOTE_ADDR'], 3);
            if (isset($_POST['code'])) {
                $code1 = sanitize($_POST['code'], 3);
                $code2 = sanitize($_POST['code_h'], 3);
            } else {
                $code1 = '';
                $code2 = '';
            }
            $p_private = isset($_POST['private']);
            $p_anon = isset($_POST['anon']);
            if (isset($_POST['imageid'])) {
                //used (only?) by the tricasa hack to know which image the client is working with.
                $activeImage = zp_load_image_from_id(strip_tags($_POST['imageid']));
                if ($activeImage !== false) {
                    $commentadded = $activeImage->addComment($p_name, $p_email, $p_website, $p_comment, $code1, $code2, $p_server, $p_private, $p_anon);
                    $redirectTo = $activeImage->getImageLink();
                }
            } else {
                // ZENPAGE: if else change
                if (in_context(ZP_IMAGE) and in_context(ZP_ALBUM)) {
                    $commentobject = $_zp_current_image;
                    $redirectTo = $_zp_current_image->getImageLink();
                } else {
                    if (!in_context(ZP_IMAGE) and in_context(ZP_ALBUM)) {
                        $commentobject = $_zp_current_album;
                        $redirectTo = $_zp_current_album->getAlbumLink();
                    } else {
                        if ($zenpage_news_context) {
                            $commentobject = $_zp_current_zenpage_news;
                            $redirectTo = FULLWEBPATH . '/index.php?p=' . ZENPAGE_NEWS . '&title=' . $_zp_current_zenpage_news->getTitlelink();
                        } else {
                            if ($zenpage_pages_context) {
                                $commentobject = $_zp_current_zenpage_page;
                                $redirectTo = FULLWEBPATH . '/index.php?p=' . ZENPAGE_NEWS . '&title=' . $_zp_current_zenpage_page->getTitlelink();
                            }
                        }
                    }
                }
                $commentadded = $commentobject->addComment($p_name, $p_email, $p_website, $p_comment, $code1, $code2, $p_server, $p_private, $p_anon);
            }
            if ($commentadded == 2) {
                $comment_error = 0;
                if (isset($_POST['remember'])) {
                    // Should always re-cookie to update info in case it's changed...
                    $info = array($p_name, $p_email, $p_website, '', false, $p_private, $p_anon);
                    zp_setcookie('zenphoto', implode('|~*~|', $info), time() + COOKIE_PESISTENCE, '/');
                } else {
                    zp_setcookie('zenphoto', '', time() - 368000, '/');
                }
                //use $redirectTo to send users back to where they came from instead of booting them back to the gallery index. (default behaviour)
                //TODO: this does not work for IIS. How to detect IIS server and just fall through?
                // if you are running IIS, delete the next two lines
                header('Location: ' . $redirectTo);
                exit;
            } else {
                $_zp_comment_stored = array($p_name, $p_email, $p_website, $p_comment, false, $p_private, $p_anon);
                if (isset($_POST['remember'])) {
                    $_zp_comment_stored[4] = true;
                }
                $comment_error = 1 + $commentadded;
                // ZENPAGE: if statements added
                if ($activeImage !== false and !$zenpage_news_context and !$zenpage_pages_context) {
                    // tricasa hack? Set the context to the image on which the comment was posted
                    $_zp_current_image = $activeImage;
                    $_zp_current_album = $activeImage->getAlbum();
                    set_context(ZP_IMAGE | ZP_ALBUM | ZP_INDEX);
                }
            }
        }
    } else {
        if (!empty($cookie)) {
            // Comment form was not submitted; get the saved info from the cookie.
            $_zp_comment_stored = explode('|~*~|', stripslashes($cookie));
            $_zp_comment_stored[4] = true;
            if (!isset($_zp_comment_stored[5])) {
                $_zp_comment_stored[5] = false;
            }
            if (!isset($_zp_comment_stored[6])) {
                $_zp_comment_stored[6] = false;
            }
        } else {
            $_zp_comment_stored = array('', '', '', '', false, false, false);
        }
    }
    return $comment_error;
}