1<?php
2
3/*
4 *  pgn4web javascript chessboard
5 *  copyright (C) 2009-2014 Paolo Casaschi
6 *  see README file and http://pgn4web.casaschi.net
7 *  for credits, license and more details
8 */
9
10error_reporting(E_ALL | E_STRICT);
11
12include "pgn-encoder.php";
13
14function get_param($param, $shortParam, $default) {
15  if (isset($_REQUEST[$param])) { return $_REQUEST[$param]; }
16  if (isset($_REQUEST[$shortParam])) { return $_REQUEST[$shortParam]; }
17  return $default;
18}
19
20$pgnText = get_param("pgnText", "pt", "");
21
22if ($pgnText) {
23  $pgnText = str_replace(array("&", "<", ">"), array("&amp;", "&lt;", "&gt;"), $pgnText);
24  $pgnTextBox = $pgnText;
25
26  $pgnText = str_replace("\\\"", "\"", $pgnText);
27
28  $pgnText = preg_replace("/\[/", "\n\n[", $pgnText);
29  $pgnText = preg_replace("/\]/", "]\n\n", $pgnText);
30  $pgnText = preg_replace("/([012\*])(\s*)(\[)/", "$1\n\n$3", $pgnText);
31  $pgnText = preg_replace("/\]\s*\[/", "]\n[", $pgnText);
32  $pgnText = preg_replace("/^\s*\[/", "[", $pgnText);
33  $pgnText = preg_replace("/\n[\s*\n]+/", "\n\n", $pgnText);
34} else {
35  $pgnText = <<<END
36
37
38 [White ""]
39 [Black ""]
40 [Result ""]
41 [Date ""]
42 [Event ""]
43 [Site ""]
44 [Round ""]
45
46 {please enter your PGN games in the textbox and then click the button}
47
48END;
49
50  $pgnTextBox = $pgnText;
51}
52
53$pgnLength = strlen($pgnTextBox);
54
55$pgnEncoded = EncodePGN($pgnText);
56
57$pgnEncodedLength = strlen($pgnEncoded);
58
59$compressionRatio = round(100 * $pgnEncodedLength / $pgnLength) . "%";
60
61$frameUrl = "board.html?am=l&d=1000&ss=26&ps=d&pf=d&lcs=YeiP&dcs=Qcij&bbcs=D91v&hm=n&hcs=Udiz&bd=s&cbcs=YeiP&ctcs=\$\$\$\$&hd=j&md=f&tm=13&fhcs=\$\$\$\$&fhs=13&fmcs=\$\$\$\$&fccs=v71\$&hmcs=Qcij&fms=13&fcs=m&cd=i&bcs=____&fp=13&hl=t&fh=b&fw=p&pe=" . $pgnEncoded;
62
63$frameUrlLength = strlen($frameUrl);
64
65$thisScript = $_SERVER['SCRIPT_NAME'];
66
67print <<<END
68<!DOCTYPE HTML>
69<html>
70
71<head>
72
73<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
74
75<title>pgn4web PGN encoder/decoder php example</title>
76
77<link rel="icon" sizes="16x16" href="pawn.ico" />
78
79</head>
80
81<body style="font-family: sans-serif; margin:20px; padding:0px;">
82
83<h1 style="margin-top:0px; padding-top:0px;">pgn4web PGN encoder/decoder php example</h1>
84
85<center>
86
87<iframe src="$frameUrl"
88 height="312" width="900" frameborder="0" scrolling="no" marginheight="0" marginwidth="0">
89your web browser and/or your host do not support iframes as required to display the chessboard
90</iframe>
91
92<form action="$thisScript" method="POST">
93<input type="submit" style="width:900px;" value="pgn4web PGN encoder/decoder php example">
94<textarea id="pgnText" name="pgnText" style="height:300px; width:900px; margin:13px;">$pgnTextBox</textarea>
95</form>
96
97<div style="width:900px; text-align:left; font-size:66%;">PGN:$pgnLength &nbsp; &nbsp; encoded:$pgnEncodedLength &nbsp; &nbsp; ratio:$compressionRatio &nbsp; &nbsp; url:$frameUrlLength</div>
98
99</center>
100
101</body>
102
103</html>
104
105END;
106
107?>
108