Subversion Repositories SvarDOS

Rev

Rev 659 | Details | Compare with Previous | 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
//
659 mateusz.vi 4
// Copyright (C) 2020-2022 Mateusz Viste
285 mateuszvis 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
 
659 mateusz.vi 27
$VERSION = "20220212";
285 mateuszvis 28
 
29
function getamafile($ambfname, $amafname) {
659 mateusz.vi 30
  if (! is_file($ambfname)) {
31
    // if its a flat dir, just load the file
32
    if (is_dir($ambfname)) return(file_get_contents($ambfname . '/' . $amafname));
33
    return(FALSE);
34
  }
285 mateuszvis 35
  $fd = fopen($ambfname, "rb");
36
  if ($fd === FALSE) return(FALSE);
37
  // read header (AMB1)
38
  if (fread($fd, 4) !== 'AMB1') return(FALSE);
39
  // read number of ama files inside
40
  $fcount = reset(unpack('v', fread($fd, 2)));
41
  if ($fcount === FALSE) return(FALSE);
42
  // read index until AMA file is found
43
  $offset = 0;
44
  $amalen = 0;
45
  $amafile = '';
46
  for ($i = 0; $i < $fcount; $i++) {
47
    $amafile = rtrim(fread($fd, 12), "\0");
48
    $offset = reset(unpack('V', fread($fd, 4)));
49
    $amalen = reset(unpack('v', fread($fd, 2)));
50
    $bsum = reset(unpack('v', fread($fd, 2)));
51
    if (strcasecmp($amafile, $amafname) == 0) break;
52
  }
53
  if ($i >= $fcount) return(FALSE); // not found
54
  // jump to offset and read file
55
  fseek($fd, $offset);
56
  $result = fread($fd, $amalen);
57
  fclose($fd);
58
  return($result);
59
}
60
 
61
 
62
// converts str into utf-8 using the unicodemap lookup table and returns the resulting (converted) str
63
function txttoutf8($str, $unicodemap) {
64
  $s = str_split($str, 1); // convert the string to a table for
65
  $res = '';
66
  foreach ($s as $c) $res .= $unicodemap[ord($c)]; // convert raw characters into HTML unicode codes
67
  return($res);
68
}
69
 
70
 
71
// MAIN STARTS HERE
72
 
73
 
74
if (empty($_GET['fname'])) {
75
  echo "usage: phpamb.php?fname=file.amb\n";
76
  exit(0);
77
}
78
 
79
$ambfname = $_GET['fname'];
80
$f = 'index.ama'; // default file
81
if (! empty($_GET['f'])) $f = $_GET['f'];
82
 
83
$title = trim(getamafile($ambfname, 'title'));
84
if ($title === FALSE) $title = $ambfname;
85
 
86
$ama = getamafile($ambfname, $f);
87
if ($ama === FALSE) $ama = 'ERROR: FILE NOT FOUND';
88
 
89
// prepare a 256-entries lookup array for unicode encoding
90
$unicodemap = array();
91
for ($i = 0; $i < 128; $i++) $unicodemap[$i] = $i; // low ascii is the same
92
 
93
$unicodemaptemp = unpack('v128', getamafile($ambfname, 'unicode.map'));
659 mateusz.vi 94
if ($unicodemaptemp === FALSE) {
95
  $unicodemap = FALSE;
96
} else {
97
  $unicodemap = array_merge($unicodemap, $unicodemaptemp);
98
  /* convert the unicode map so it contains actual html code instead of glyph values */
99
  for ($i = 0; $i < 256; $i++) {
100
    if ($unicodemap[$i] < 128) {
101
      $unicodemap[$i] = htmlspecialchars(chr($unicodemap[$i]), ENT_HTML5);
102
    } else {
103
      $unicodemap[$i] = '&#' . $unicodemap[$i] . ';';
104
    }
285 mateuszvis 105
  }
659 mateusz.vi 106
  // perform UTF-8 conversion of the title
107
  $title = txttoutf8($title, $unicodemap);
285 mateuszvis 108
}
109
 
110
 
111
echo "<!DOCTYPE html>\n";
112
echo "<html>\n";
113
echo "<head>\n";
114
echo '  <meta charset="UTF-8">' . "\n";
115
echo "  <title>{$title}</title>\n";
116
echo '  <link rel="stylesheet" href="phpamb.css">' . "\n";
117
echo '  <meta name="viewport" content="width=device-width, initial-scale=1">' . "\n";
118
echo "  <meta name=\"generator\" content=\"phpamb/{$VERSION}\">\n";
119
echo "</head>\n";
120
echo "<body>";
121
 
122
echo "<div><span><a href=\"?fname={$ambfname}\" class=\"liketext\">{$title}</a></span><span>[" . pathinfo($f, PATHINFO_FILENAME) . "]</span></div>\n";
123
 
124
/* detect links first, before any htmlization occurs */
125
$ama = preg_replace('!(https?|ftp)://([-A-Z0-9./_*?&;%=#~:]+)!i', 'LiNkStArTxXx$0LiNkEnDxXx', $ama);
126
 
659 mateusz.vi 127
if ($unicodemap !== FALSE) {
128
  $amacontent = str_split($ama, 1);
129
} else {
130
  $amacontent = mb_str_split($ama, 1, 'utf-8');
131
}
285 mateuszvis 132
$escnow = 0;  // next char is an escape code
133
$readlink = 0; // a link target is being read
134
$opentag = ''; // do I have a currently open html tag?
135
 
136
$out = '';
137
foreach ($amacontent as $c) {
138
  // ignore CR
139
  if ($c == "\r") continue;
140
  // is link target being read?
141
  if ($readlink != 0) {
142
    if (($c == "\n") || ($c == ':')) {
143
      $out .= '">';
144
      $opentag = 'a';
145
      $readlink = 0;
146
      if ($c == ':') continue;
147
    } else {
148
      $out .= urlencode($c);
149
    }
150
    continue;
151
  }
152
  //
153
  if ($escnow != 0) {
154
    if ($c == '%') {
155
      $out .= '%';
156
    } else if ($c == 'l') {
157
      $out .= '<a href="' . $_SERVER['PHP_SELF'] . "?fname={$ambfname}&amp;f=";
158
      $readlink = 1;
159
    } else if ($c == 'h') {
160
      $out .= '<h1>';
161
      $opentag = 'h1';
162
    } else if ($c == '!') {
163
      $out .= '<span class="notice">';
164
      $opentag = 'span';
165
    } else if ($c == 'b') {
166
      $out .= '<span class="boring">';
167
      $opentag = 'span';
168
    }
169
    $escnow = 0;
170
    continue;
171
  }
172
  // close </a> if open and got LF or new tag
173
  if ((!empty($opentag)) && (($c == "\n") || ($c == "%"))) {
174
    $out .= "</{$opentag}>";
175
    $opentag = '';
176
  }
177
  //
178
  if ($c == '%') {
179
    $escnow = 1;
180
  } else {
659 mateusz.vi 181
    if ($unicodemap !== FALSE) {
182
      $out .= $unicodemap[ord($c)];  // convert characters into HTML unicode codes
183
    } else {
857 mateusz.vi 184
      $out .= htmlspecialchars($c);
659 mateusz.vi 185
    }
285 mateuszvis 186
  }
187
}
188
 
189
// close open tags
190
if (!empty($opentag)) {
191
  $out .= "</{$opentag}>";
192
  $opentag = '';
193
}
194
 
195
/* postprocessing: find links detected earlier and change them to proper anchors */
196
$out = preg_replace('/LiNkStArTxXx.*LiNkEnDxXx/', '<a href="$0">$0</a>', $out);
197
$out = preg_replace('/(LiNkStArTxXx)|(LiNkEnDxXx)/', '', $out);
198
 
199
echo $out;
200
 
201
echo "</body>\n";
202
echo "</html>\n";
203
 
204
?>