Subversion Repositories SvarDOS

Rev

Rev 285 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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