Subversion Repositories SvarDOS

Rev

Rev 285 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 285 Rev 659
Line 1... Line 1...
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
Line 22... Line 22...
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
Line 85... Line 89...
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";
Line 118... Line 122...
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 = '';
Line 168... Line 176...
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)) {