Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
285 mateuszvis 1
<?php
2
// php reader of AMB files -- turns an AMB book into a web page
3
//
4
// Copyright (C) 2020 Mateusz Viste
5
// http://amb.osdn.io
6
//
7
// MIT license
8
//
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
15
//
16
// The above copyright notice and this permission notice shall be included in all
17
// copies or substantial portions of the Software.
18
//
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
// SOFTWARE.
26
 
27
$VERSION = "20201218";
28
 
29
function getamafile($ambfname, $amafname) {
30
  if (! is_file($ambfname)) return(FALSE);
31
  $fd = fopen($ambfname, "rb");
32
  if ($fd === FALSE) return(FALSE);
33
  // read header (AMB1)
34
  if (fread($fd, 4) !== 'AMB1') return(FALSE);
35
  // read number of ama files inside
36
  $fcount = reset(unpack('v', fread($fd, 2)));
37
  if ($fcount === FALSE) return(FALSE);
38
  // read index until AMA file is found
39
  $offset = 0;
40
  $amalen = 0;
41
  $amafile = '';
42
  for ($i = 0; $i < $fcount; $i++) {
43
    $amafile = rtrim(fread($fd, 12), "\0");
44
    $offset = reset(unpack('V', fread($fd, 4)));
45
    $amalen = reset(unpack('v', fread($fd, 2)));
46
    $bsum = reset(unpack('v', fread($fd, 2)));
47
    if (strcasecmp($amafile, $amafname) == 0) break;
48
  }
49
  if ($i >= $fcount) return(FALSE); // not found
50
  // jump to offset and read file
51
  fseek($fd, $offset);
52
  $result = fread($fd, $amalen);
53
  fclose($fd);
54
  return($result);
55
}
56
 
57
 
58
// converts str into utf-8 using the unicodemap lookup table and returns the resulting (converted) str
59
function txttoutf8($str, $unicodemap) {
60
  $s = str_split($str, 1); // convert the string to a table for
61
  $res = '';
62
  foreach ($s as $c) $res .= $unicodemap[ord($c)]; // convert raw characters into HTML unicode codes
63
  return($res);
64
}
65
 
66
 
67
// MAIN STARTS HERE
68
 
69
 
70
if (empty($_GET['fname'])) {
71
  echo "usage: phpamb.php?fname=file.amb\n";
72
  exit(0);
73
}
74
 
75
$ambfname = $_GET['fname'];
76
$f = 'index.ama'; // default file
77
if (! empty($_GET['f'])) $f = $_GET['f'];
78
 
79
$title = trim(getamafile($ambfname, 'title'));
80
if ($title === FALSE) $title = $ambfname;
81
 
82
$ama = getamafile($ambfname, $f);
83
if ($ama === FALSE) $ama = 'ERROR: FILE NOT FOUND';
84
 
85
// prepare a 256-entries lookup array for unicode encoding
86
$unicodemap = array();
87
for ($i = 0; $i < 128; $i++) $unicodemap[$i] = $i; // low ascii is the same
88
 
89
$unicodemaptemp = unpack('v128', getamafile($ambfname, 'unicode.map'));
90
if ($unicodemaptemp === FALSE) $unicodemaptemp = array_fill(0, 128, 0xfffd);
91
$unicodemap = array_merge($unicodemap, $unicodemaptemp);
92
 
93
/* convert the unicode map so it contains actual html code instead of glyph values */
94
for ($i = 0; $i < 256; $i++) {
95
  if ($unicodemap[$i] < 128) {
96
    $unicodemap[$i] = htmlspecialchars(chr($unicodemap[$i]), ENT_HTML5);
97
  } else {
98
    $unicodemap[$i] = '&#' . $unicodemap[$i] . ';';
99
  }
100
}
101
 
102
 
103
// perform UTF-8 conversion of the title
104
$title = txttoutf8($title, $unicodemap);
105
 
106
 
107
echo "<!DOCTYPE html>\n";
108
echo "<html>\n";
109
echo "<head>\n";
110
echo '  <meta charset="UTF-8">' . "\n";
111
echo "  <title>{$title}</title>\n";
112
echo '  <link rel="stylesheet" href="phpamb.css">' . "\n";
113
echo '  <meta name="viewport" content="width=device-width, initial-scale=1">' . "\n";
114
echo "  <meta name=\"generator\" content=\"phpamb/{$VERSION}\">\n";
115
echo "</head>\n";
116
echo "<body>";
117
 
118
echo "<div><span><a href=\"?fname={$ambfname}\" class=\"liketext\">{$title}</a></span><span>[" . pathinfo($f, PATHINFO_FILENAME) . "]</span></div>\n";
119
 
120
/* detect links first, before any htmlization occurs */
121
$ama = preg_replace('!(https?|ftp)://([-A-Z0-9./_*?&;%=#~:]+)!i', 'LiNkStArTxXx$0LiNkEnDxXx', $ama);
122
 
123
$amacontent = str_split($ama, 1);
124
$escnow = 0;  // next char is an escape code
125
$readlink = 0; // a link target is being read
126
$opentag = ''; // do I have a currently open html tag?
127
 
128
$out = '';
129
foreach ($amacontent as $c) {
130
  // ignore CR
131
  if ($c == "\r") continue;
132
  // is link target being read?
133
  if ($readlink != 0) {
134
    if (($c == "\n") || ($c == ':')) {
135
      $out .= '">';
136
      $opentag = 'a';
137
      $readlink = 0;
138
      if ($c == ':') continue;
139
    } else {
140
      $out .= urlencode($c);
141
    }
142
    continue;
143
  }
144
  //
145
  if ($escnow != 0) {
146
    if ($c == '%') {
147
      $out .= '%';
148
    } else if ($c == 'l') {
149
      $out .= '<a href="' . $_SERVER['PHP_SELF'] . "?fname={$ambfname}&amp;f=";
150
      $readlink = 1;
151
    } else if ($c == 'h') {
152
      $out .= '<h1>';
153
      $opentag = 'h1';
154
    } else if ($c == '!') {
155
      $out .= '<span class="notice">';
156
      $opentag = 'span';
157
    } else if ($c == 'b') {
158
      $out .= '<span class="boring">';
159
      $opentag = 'span';
160
    }
161
    $escnow = 0;
162
    continue;
163
  }
164
  // close </a> if open and got LF or new tag
165
  if ((!empty($opentag)) && (($c == "\n") || ($c == "%"))) {
166
    $out .= "</{$opentag}>";
167
    $opentag = '';
168
  }
169
  //
170
  if ($c == '%') {
171
    $escnow = 1;
172
  } else {
173
    $out .= $unicodemap[ord($c)];  // convert characters into HTML unicode codes
174
  }
175
}
176
 
177
// close open tags
178
if (!empty($opentag)) {
179
  $out .= "</{$opentag}>";
180
  $opentag = '';
181
}
182
 
183
/* postprocessing: find links detected earlier and change them to proper anchors */
184
$out = preg_replace('/LiNkStArTxXx.*LiNkEnDxXx/', '<a href="$0">$0</a>', $out);
185
$out = preg_replace('/(LiNkStArTxXx)|(LiNkEnDxXx)/', '', $out);
186
 
187
echo $out;
188
 
189
echo "</body>\n";
190
echo "</html>\n";
191
 
192
?>