$label, 'javascriptCode' => $javascriptCode)); } // modify the viewer-preset-URLs.php file to add preset URLs for the viewer's form include 'viewer-preset-URLs.php'; $pgnOnly = get_param("pgnOnly", "po", ""); $generateParameter = get_param("generateParameter", "gp", ""); if (($pgnOnly == "true") || ($pgnOnly == "t")) { if (!get_pgn()) { header("HTTP/1.1 204 No Content"); } header("content-type: application/x-chess-pgn"); header("content-disposition: inline; filename=games.pgn"); if ($http_response_header_last_modified) { header($http_response_header_last_modified); } if ($pgnText) { print $pgnText; } } elseif (($generateParameter == "true") || ($generateParameter == "t")) { header("content-type: text/html; charset=utf-8"); $pgnUrl = get_param("pgnData", "pd", ""); if ($pgnUrl == "") { $pgnUrl = get_param("pgnUrl", "pu", ""); } $pgnLink = $_SERVER['SCRIPT_NAME'] . urlencode("?po=t&pd=" . $pgnUrl); print("
"); } else { header("content-type: text/html; charset=utf-8"); if (!($goToView = get_pgn())) { $pgnText = preg_match("/^error:/", $pgnStatus) ? '[Event ""] [Site ""] [Date ""] [Round ""] [White ""] [Black ""] [Result ""] { error loading PGN data, click square A8 for more details }' : $startPosition; } print_header(); print_form(); check_tmpDir(); print_menu("board"); print_chessboard_one(); print_menu("moves"); print_chessboard_two(); print_footer(); print_menu("bottom"); print_html_close(); } function get_param($param, $shortParam, $default) { if (isset($_REQUEST[$param])) { return $_REQUEST[$param]; } if (isset($_REQUEST[$shortParam])) { return $_REQUEST[$shortParam]; } return $default; } function http_parse_headers($headerFields) { global $http_response_header_status, $http_response_header_last_modified; $retVal = array(); foreach ($headerFields as $field) { if (preg_match('/([^:]+): (.+)/m', $field, $match)) { $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1]))); if (isset($retVal[$match[1]])) { $retVal[$match[1]] = array($retVal[$match[1]], $match[2]); } else { $retVal[$match[1]] = trim($match[2]); } } else if (preg_match('/^\S+\s+\d+\s/m', $field)) { $retVal["status"] = $field; } } if (isset($retVal["status"])) { $http_response_header_status = $retVal["status"]; } if (isset($retVal["Last-Modified"])) { $http_response_header_last_modified = "Last-Modified: " . $retVal["Last-Modified"]; } return $retVal; } function http_response_header_isInvalid() { global $http_response_header_status; return $http_response_header_status ? preg_match("/^\S+\s+[45]\d\d\s/", $http_response_header_status) : FALSE; } function get_pgn() { global $pgnText, $pgnTextbox, $pgnUrl, $pgnFileName, $pgnFileSize, $pgnStatus, $tmpDir, $debugHelpText, $pgnDebugInfo; global $fileUploadLimitIniText, $fileUploadLimitText, $fileUploadLimitBytes, $startPosition, $goToView, $zipSupported; global $http_response_header_status, $http_response_header_last_modified; $pgnDebugInfo = $pgnDebugInfo . get_param("debug", "d", ""); $pgnText = get_param("pgnText", "pt", ""); $pgnUrl = get_param("pgnData", "pd", ""); if ($pgnUrl == "") { $pgnUrl = get_param("pgnUrl", "pu", ""); } if ($pgnText) { $pgnStatus = "info: games from textbox input"; $pgnTextbox = $pgnText = str_replace("\\\"", "\"", $pgnText); $pgnText = preg_replace("/\[/", "\n\n[", $pgnText); $pgnText = preg_replace("/\]/", "]\n\n", $pgnText); $pgnText = preg_replace("/([012\*])(\s*)(\[)/", "$1\n\n$3", $pgnText); $pgnText = preg_replace("/\]\s*\[/", "]\n[", $pgnText); $pgnText = preg_replace("/^\s*\[/", "[", $pgnText); $pgnText = preg_replace("/\n[\s*\n]+/", "\n\n", $pgnText); $pgnTextbox = $pgnText; return TRUE; } else if ($pgnUrl) { $pgnStatus = "info: games from $pgnUrl"; $isPgn = preg_match("/\.(pgn|txt)$/i", preg_replace("/[?#].*$/", "", $pgnUrl)); $isZip = preg_match("/\.zip$/i", preg_replace("/[?#].*$/", "", $pgnUrl)); if ($isZip) { if (!$zipSupported) { $pgnStatus = "error: zipfile support unavailable, unable to open $pgnUrl"; return FALSE; } else { $tempZipName = tempnam($tmpDir, "pgn4webViewer_"); // $pgnUrlOpts tries forcing following location redirects // depending on server configuration, the script might still fail if the ZIP URL is redirected $pgnUrlOpts = array("http" => array("follow_location" => TRUE, "max_redirects" => 20)); $pgnUrlHandle = @fopen($pgnUrl, "rb", false, stream_context_create($pgnUrlOpts)); if (!$pgnUrlHandle) { $pgnStatus = "error: failed to get $pgnUrl: file not found or server error"; return FALSE; } else { $tempZipHandle = fopen($tempZipName, "wb"); $copiedBytes = stream_copy_to_stream($pgnUrlHandle, $tempZipHandle, $fileUploadLimitBytes + 1, 0); fclose($pgnUrlHandle); fclose($tempZipHandle); if (isset($http_response_header)) { http_parse_headers($http_response_header); } if ((($copiedBytes > 0) && ($copiedBytes <= $fileUploadLimitBytes)) && (!http_response_header_isInvalid())) { $pgnSource = $tempZipName; } else { $pgnStatus = "error: failed to get $pgnUrl: " . (http_response_header_isInvalid() ? "server error: $http_response_header_status" : "file not found, file size exceeds $fileUploadLimitText form limit, $fileUploadLimitIniText server limit or server error"); if (($tempZipName) && (file_exists($tempZipName))) { unlink($tempZipName); } return FALSE; } } } } else { $pgnSource = $pgnUrl; } } elseif (count($_FILES) == 0) { $pgnStatus = "info: no games supplied"; return FALSE; } elseif ($_FILES['pgnFile']['error'] === UPLOAD_ERR_OK) { $pgnFileName = $_FILES['pgnFile']['name']; $pgnStatus = "info: games from file $pgnFileName"; $pgnFileSize = $_FILES['pgnFile']['size']; if ($pgnFileSize == 0) { $pgnStatus = "info: failed uploading games: file not found, file empty or upload error"; return FALSE; } elseif ($pgnFileSize > $fileUploadLimitBytes) { $pgnStatus = "error: failed uploading games: file size exceeds $fileUploadLimitText limit"; return FALSE; } else { $isPgn = preg_match("/\.(pgn|txt)$/i",$pgnFileName); $isZip = preg_match("/\.zip$/i",$pgnFileName); $pgnSource = $_FILES['pgnFile']['tmp_name']; } } else { $pgnStatus = "error: failed uploading games: "; switch ($_FILES['pgnFile']['error']) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $pgnStatus = $pgnStatus . "file size exceeds $fileUploadLimitText form limit or $fileUploadLimitIniText server limit"; break; case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: $pgnStatus = $pgnStatus . "file missing or truncated"; break; case UPLOAD_ERR_NO_TMP_DIR: case UPLOAD_ERR_CANT_WRITE: case UPLOAD_ERR_EXTENSION: $pgnStatus = $pgnStatus . "server error"; break; default: $pgnStatus = $pgnStatus . "unknown upload error"; break; } return FALSE; } if ($isZip) { if ($zipSupported) { if ($pgnUrl) { $zipFileString = $pgnUrl; } else { $zipFileString = "zip file"; } $pgnZip = zip_open($pgnSource); if (is_resource($pgnZip)) { while (is_resource($zipEntry = zip_read($pgnZip))) { if (zip_entry_open($pgnZip, $zipEntry)) { if (preg_match("/\.pgn$/i",zip_entry_name($zipEntry))) { $pgnText = $pgnText . zip_entry_read($zipEntry, zip_entry_filesize($zipEntry)) . "\n\n\n"; } zip_entry_close($zipEntry); } else { $pgnStatus = "error: failed reading $zipFileString content"; zip_close($pgnZip); if ((isset($tempZipName)) && ($tempZipName) && (file_exists($tempZipName))) { unlink($tempZipName); } return FALSE; } } zip_close($pgnZip); if ((isset($tempZipName)) && ($tempZipName) && (file_exists($tempZipName))) { unlink($tempZipName); } if (!$pgnText) { $pgnStatus = "error: games not found in $zipFileString"; return FALSE; } } else { if ((isset($tempZipName)) && ($tempZipName) && (file_exists($tempZipName))) { unlink($tempZipName); } $pgnStatus = "error: failed opening $zipFileString"; return FALSE; } } else { $pgnStatus = "error: ZIP support unavailable from this server, only PGN files are supported"; return FALSE; } } elseif ($isPgn) { if ($pgnUrl) { $pgnFileString = $pgnUrl; } else { $pgnFileString = "pgn file"; } $pgnText = @file_get_contents($pgnSource, NULL, NULL, 0, $fileUploadLimitBytes + 1); if (isset($http_response_header)) { http_parse_headers($http_response_header); } if ((!$pgnText) || (($pgnUrl) && (http_response_header_isInvalid()))) { $pgnStatus = "error: failed reading $pgnFileString: " . (http_response_header_isInvalid() ? "server error: $http_response_header_status" : "file not found or server error"); return FALSE; } if ((strlen($pgnText) == 0) || (strlen($pgnText) > $fileUploadLimitBytes)) { $pgnStatus = "error: failed reading $pgnFileString: file size exceeds $fileUploadLimitText form limit, $fileUploadLimitIniText server limit or server error"; return FALSE; } } elseif ($pgnSource) { if ($zipSupported) { $pgnStatus = "error: only PGN and ZIP (zipped pgn) files are supported"; } else { $pgnStatus = "error: only PGN files are supported, ZIP support unavailable from this server"; } return FALSE; } $pgnText = str_replace(array("&", "<", ">"), array("&", "<", ">"), $pgnText); return TRUE; } function check_tmpDir() { global $pgnText, $pgnTextbox, $pgnUrl, $pgnFileName, $pgnFileSize, $pgnStatus, $tmpDir, $debugHelpText, $pgnDebugInfo; global $fileUploadLimitIniText, $fileUploadLimitText, $fileUploadLimitBytes, $startPosition, $goToView, $zipSupported; $unexpectedFiles = ""; $tmpDirHandle = opendir($tmpDir); while($entryName = readdir($tmpDirHandle)) { if (($entryName !== ".") && ($entryName !== "..") && ($entryName !== "index.html")) { if ((time() - filemtime($tmpDir . "/" . $entryName)) > 3600) { $unexpectedFiles = $unexpectedFiles . " " . $entryName; } } } closedir($tmpDirHandle); if ($unexpectedFiles) { $pgnDebugInfo = $pgnDebugInfo . "\\n" . "clean temporary directory " . $tmpDir . ":" . $unexpectedFiles; } } function print_menu($item) { print <<