forked from fidian/php-pdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwister.php
More file actions
582 lines (499 loc) · 16.9 KB
/
Copy pathtwister.php
File metadata and controls
582 lines (499 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
<?PHP
/* Examples for PHP-PDB library - HTML to DOC converter
*
* Copyright (C) 2001 - PHP-PDB development team
* Licensed under the GNU LGPL software license.
* See the doc/LEGAL file for more information
* See https://github.com/fidian/php-pdb for more information about the library
*/
/* Variables possibly passed in:
* action = 'convert' to convert, 'download' to get a file
*
* If action == 'convert'
* Source = 'File' or 'URL' for designating where the source is
* if Source == 'File'
* filedata = the file ??
* if Source == 'URL'
* urldata = the URL
* SourceType = 'HTML', 'Text', 'Gutenberg'
* If SourceType == 'Text'
* RewrapParagraphs = checkbox for rewrapping paragraphs
* If SourceType == 'Gutenberg'
* BreakOnChapter = checkbox for breaking the file on chapters
* TargetType = 'DOC', 'SmallBASIC'
* if TargetType == 'DOC'
* TitleOfDoc = Title
* UncompressedDOC = true/false to generate an uncompressed doc
* if TargetType == 'SmallBASIC'
* TitleOfBasicFile = Title of basic file to generate
*
* If action == 'download'
* file = name of the file in $SavedFiles
*/
if (file_exists('../../php-pdb.inc')) {
include('../../php-pdb.inc');
include('../../modules/doc.inc');
include('../../modules/smallbasic.inc');
} elseif (file_exists('./php-pdb.inc')) {
include('./php-pdb.inc');
include('./modules/doc.inc');
include('./modules/smallbasic.inc');
}
include('../functions.inc');
// START HERE
// Two minutes should be more than enough.
// If nothing else, it should stop the server from being
// brought to its knees by a snippet of bad code.
// (Speaking from experience.)
set_time_limit(1200);
include('./filters/text.inc');
include('./filters/html.inc');
include('./filters/gutenberg.inc');
session_start();
$tiny_self = strtolower($HTTP_HOST);
$MyFilename = $PHP_SELF;
$pos = strrpos($MyFilename, '/');
$MyFilename = substr($MyFilename, $pos + 1);
if (isset($action) && $action == "download") {
DownloadPDB();
} elseif (isset($action) && $action == 'erase') {
StartHTML('File Erased');
ErasePDB();
ShowInitialForm();
} elseif (isset($action) && $action == "convert") {
ConvertFile();
} else {
ShowInitialHelp();
ShowInitialForm();
}
exit();
function StartHTML($title = '') {
if ($title == '')
$title = 'Twister';
?><html><head><title><?PHP echo $title ?></title></head>
<body bgcolor="#FFFFFF">
<p align=center><b><font size="+3"><?PHP echo $title ?></font></b></p>
<?PHP
}
function ShowInitialHelp() {
StartHTML();
?><p>Twister is a conversion tool to convert web pages, and text files into
Palm DOC format. SmallBASIC .BAS files can be converted to SmallBASIC for
Palm OS format. It can be later be easily extended to write to zTXT and other
formats, once PHP-PDB has classes for accessing them. To start the
conversion process, just fill out the form below. The file will be
converted and you will see links to download the converted file.</p>
<p>Twister is quite new and may not work perfectly. If you experience
problems or if something is not converted quite right (like the HTML looks
goofy when converted to text), the just contact the <a
href="https://github.com/fidian/php-pdb">PHP-PDB development team</a> and
we will see what we can do.</p>
<?PHP if (! ini_get('allow_url_fopen')) { ?>
<P><b>The URL converter doesn't work on sites with "allow_url_fopen" turned
off, but it should work for you if you download
<a href="https://github.com/fidian/php-pdb">PHP-PDB</a>
and have it running on your own site. <font size="-1">(Sorry.)</font></p>
<?PHP
}
}
function ShowInitialForm() {
global $MyFilename, $Source, $SourceType, $RewrapParagraphs,
$BreakOnChapter, $TargetType, $TitleOfDoc, $filedata,
$urldata, $UncompressedDoc, $TitleOfBasicFile;
echo "<br>\n";
ShowDownloadLinks();
?><form action="<?PHP echo $MyFilename
?>" method="post" enctype="multipart/form-data">
<input type=hidden name=action value="convert">
<table border=1 align=center cellpadding=5 cellspacing=0>
<tr>
<td align=right><b>Source:</b></td>
<td><?PHP if (ini_get('allow_url_fopen')) { ?>
<input type=radio name="Source" value="URL"<?PHP
if (! isset($Source) || $Source != 'File') echo ' checked'; ?>>
URL: <input type=input name=urldata size=60<?PHP
if (isset($urldata)) echo ' value="' . htmlspecialchars($urldata)
. '"'; ?>><br>
<?PHP } ?>
<input type=radio name="Source" value="File"<?PHP
if ((isset($Source) && $Source == 'File') ||
! ini_get('allow_url_fopen'))
echo ' checked'; ?>>
File: <input type=file name=filedata size=45<?PHP
if (isset($filedata)) echo ' value="' . htmlspecialchars($filedata)
. '"'; ?>>
</tr>
<tr>
<td align=right><b>Source Type:</b></td>
<td><input type=radio name="SourceType" value="HTML"<?PHP
if (! isset($SourceType) || ($SourceType != 'Text' &&
$SourceType != 'Gutenberg')) echo ' checked'; ?>>
HTML<br>
<br>
<input type=radio name="SourceType" value="Text"<?PHP
if (isset($SourceType) && $SourceType == 'Text') echo ' checked'; ?>>
Text<br>
<input type=checkbox name="RewrapParagraphs"<?PHP
if (isset($RewrapParagraphs) && $RewrapParagraphs) echo ' checked'; ?>>
Rewrap paragraphs<br>
<br>
<input type=radio name="SourceType" value="Gutenberg"<?PHP
if (isset($SourceType) && $SourceType == 'Gutenberg')
echo ' checked'; ?>>
Project Gutenberg Text (<a href="http://promo.net/pg">What is
this?</a>)<br>
Create a new DOC file every
<input type=text name="BreakOnChapter" size=4 value="<?PHP
if (isset($BreakOnChapter)) {
settype($BreakOnChapter, 'integer');
echo htmlspecialchars($BreakOnChapter);
} else
echo '0'; ?>"> chapters<br>
<i>(Use 0 to disable)
</td>
</tr>
<tr>
<td align=right><b>Convert Into:</b></td>
<td><input type=radio name="TargetType" value="DOC"<?PHP
if (! isset($TargetType) ||
($TargetType != 'SmallBASIC' && $TargetType != 'zTXT'))
echo ' checked'; ?>> DOC<br>
DOC Title:
<input type=text name="TitleOfDoc" value="<?PHP
if (isset($TitleOfDoc)) echo htmlspecialchars($TitleOfDoc); ?>">
<br>
<input type=checkbox name="UncompressedDoc">
Don't compress DOC file (faster to convert, larger file)
<br><br>
<input type=radio name="TargetType" value="zTXT"<?PHP
if (isset($TargetType) && $TargetType == 'zTXT')
echo ' checked'; ?>> zTXT (Weasel Reader)<br>
zTXT Title:
<input type=text name="TitleOfzTXT" value="<?PHP
if (isset($TitleOfzTXT)) echo htmlspecialchars($TitleOfzTXT); ?>">
<br><br>
<input type=radio name="TargetType" value="SmallBASIC"<?PHP
if (isset($TargetType) && $TargetType == 'SmallBASIC')
echo ' checked'; ?>> SmallBASIC<br>
Name of File:
<input type=text name="TitleOfBasicFile" value="<?PHP
if (isset($TitleOfBasicFile))
echo htmlspecialchars($TitleOfBasicFile); ?>">
</td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="Convert File!"></td>
</tr>
</table>
</form>
<?PHP
TinyFooter();
}
function ConvertFile() {
global $TitleOfDoc, $TitleOfBasicFile, $TargetType;
StartHTML('Converting File');
if (ConversionSanityChecks()) {
ShowInitialForm();
return;
}
$filedata = GetTheFile();
if ($filedata === false) {
ShowInitialForm();
return;
}
$rawData = ConvertFromFormat($filedata);
if ($rawData === false) {
ShowInitialForm();
return;
}
if ($TargetType == 'SmallBASIC')
$DaTitle = $TitleOfBasicFile;
if ($TargetType == 'DOC')
$DaTitle = $TitleOfDoc;
if ($TargetType == 'zTXT')
$DaTitle = $TitleOfzTXT;
if (is_array($rawData)) {
foreach ($rawData as $index => $d) {
StoreAsPRC($DaTitle . ' [' . ($index + 1) . '/' .
count($rawData) . ']', $d);
}
} else {
StoreAsPRC($DaTitle, $rawData);
}
ShowStatus("Conversion complete!");
ShowInitialForm();
}
// Returns true on error
function ConversionSanityChecks() {
global $TargetType, $TitleOfDoc, $Source, $filedata, $urldata,
$SourceType, $TitleOfBasicFile;
if (! isset($TargetType) || ($TargetType != 'DOC' && $TargetType !=
'SmallBASIC' && $TargetType != 'zTXT')) {
ShowError('Invalid target type.');
return true;
}
if ($TargetType == 'DOC' && (! isset($TitleOfDoc) || $TitleOfDoc == '')) {
ShowError('You must specify a title for the DOC file.');
return true;
}
if ($TargetType == 'zTXT' && (! isset($TitleOfzTXT) || $TitleOfzTXT == '')) {
ShowError('You must specify a title for the zTXT file.');
return true;
}
if ($TargetType == 'SmallBASIC' && (! isset($TitleOfBasicFile) ||
$TitleOfBasicFile == '')) {
ShowError('You must specify a file name for the SmallBASIC file.');
return true;
}
if (! isset($Source) || ($Source != 'File' && $Source != 'URL')) {
ShowError('Invalid source.');
return true;
}
if ($Source == 'File' && ! $filedata) {
ShowError('Invalid file source. Make sure to upload a file.');
return true;
}
if ($Source == 'URL' && ! $urldata) {
ShowError('Invalid URL source. Make sure to specify a URL.');
return true;
}
if ($SourceType != 'Text' && $SourceType != 'HTML' &&
$SourceType != 'Gutenberg') {
ShowError('Invalid source type.');
return true;
}
return false;
}
function ShowInTable($Msg, $BGColor, $border) {
echo "<table align=center width=60% bgcolor=\"" . $BGColor .
"\" border=1 cellpadding=$border cellspacing=2>";
echo "<tr><td align=center>";
echo "<B>" . nl2br(htmlspecialchars($Msg)) . "</B>";
echo "</td></tr></table>\n";
}
function ShowError($Err) {
echo "<br>\n";
ShowInTable($Err, '#FFA0A0', 10);
echo "<br>\n";
}
function ShowStatus($Str) {
ShowInTable($Str, '#A0FFA0', 4);
flush();
}
// We care about three digits or so.
// 1 byte
// 135 bytes
// 1.23 k
// 12.3 k
// 123 k
// and so on
function ShowSize($bytes) {
$tags = array('b', 'k', 'meg', 'gig', 'terra');
$index = 0;
while ($bytes > 999 && isset($tags[$index + 1])) {
$bytes /= 1024;
$index ++;
}
$rounder = 1;
if ($bytes < 10)
$rounder *= 10;
if ($bytes < 100)
$rounder *= 10;
$bytes *= $rounder;
settype($bytes, 'integer');
$bytes /= $rounder;
return $bytes . ' ' . $tags[$index];
}
function GetTheFile() {
global $Source, $urldata, $HTTP_POST_FILES, $filedata;
if ($Source == 'File') {
$fp = @fopen($HTTP_POST_FILES['filedata']['tmp_name'], 'r');
if (! $fp) {
ShowError("I am unable to open the uploaded file. Weird.");
return false;
}
$d = '';
while (! feof($fp)) {
$d .= fread($fp, 8192);
}
ShowStatus('File loaded. Total size is ' . ShowSize(strlen($d)) .
".\nStarting conversion of the file.");
fclose($fp);
return $d;
}
if ($Source == 'URL') {
$IsGood = false;
foreach (array("http://", "ftp://") as $match) {
if (strcasecmp(substr($urldata, 0, strlen($match)), $match) == 0)
$IsGood = true;
}
if (! $IsGood) {
ShowError("Unacceptable URL. It must begin with http:// or " .
"ftp://");
return false;
}
$fp = @fopen($urldata, "r");
if (! $fp) {
ShowError("Unable to read from the URL specified:\n" . $urldata);
return false;
}
ShowStatus('Downloading file. Please be patient.');
$d = '';
while (! feof($fp)) {
$d .= fread($fp, 8192);
}
ShowStatus('File loaded. Total size is ' . ShowSize(strlen($d)) .
".\nStarting conversion of the file.");
fclose($fp);
return $d;
}
ShowError('Invalid source.');
return false;
}
function ConvertFromFormat($filedata) {
global $SourceType, $RewrapParagraphs, $BreakOnChapter;
if ($SourceType == 'Text') {
if (! isset($RewrapParagraphs))
$RewrapParagraphs = false;
return FilterText($filedata, $RewrapParagraphs);
}
if ($SourceType == 'HTML') {
return FilterHtml($filedata);
}
if ($SourceType == 'Gutenberg') {
if (! isset($BreakOnChapter))
$BreakOnChapter = false;
return FilterGutenberg($filedata, $BreakOnChapter);
}
ShowError('Invalid source type.');
return false;
}
function StoreAsPRC($title, $rawData) {
// echo "<h1>$title</h1>\n<pre>$rawData\n</pre>\n"; return;
global $SavedPDB, $UncompressedDoc, $TargetType, $CompressWarningDisplayed;
$fileName = preg_replace('/[^-a-zA-Z_0-9]/', '_', $title);
if (! isset($SavedPDB) || ! is_array($SavedPDB)) {
$SavedPDB = array();
session_register('SavedPDB');
}
$SavedInfo['Title'] = $title;
$SavedInfo['Type'] = $TargetType;
$SavedInfo['Time'] = time();
if ($TargetType == 'DOC') {
if (isset($UncompressedDoc) && $UncompressedDoc)
$prc = new PalmDoc($title, false);
else
$prc = new PalmDoc($title);
$prc->AddText($rawData);
if (!isset($CompressWarningDisplayed)) {
$WarningTime = '';
if (count($prc->Records) > 5)
$WarningTime = 'a bit';
if (count($prc->Records) > 10)
$WarningTime = 'a while';
if (count($prc->Records) > 15)
$WarningTime = 'a long time';
if (count($prc->Records) > 25)
$WarningTime = 'a very long time';
if (count($prc->Records) > 40)
$WarningTime = 'enough time to write a poem';
if (count($prc->Records) > 60)
$WarningTime = 'enough time for you learn a new hobby';
if (count($prc->Records) > 100)
$WarningTime = 'so long that PHP will likely time out and ' .
'kill this conversion';
if ($WarningTime != '')
ShowStatus("Compressing the DOC.\nThis could take " .
$WarningTime . '.');
$CompressWarningDisplayed = true;
}
} elseif ($TargetType == 'zTXT') {
$prc = new PalmzTXT($title);
$prc->AddText($rawData);
} elseif ($TargetType == 'SmallBASIC') {
$prc = new PalmSmallBASIC($title);
$result = $prc->ConvertFromText($rawData);
if (is_array($result)) {
ShowError('Error converting the file. The section "' .
$result[1] . '" is too big. It must be broken into ' .
'multiple sections for the conversion to work.');
return;
}
}
ob_start();
$prc->WriteToStdout();
$prc = ob_get_contents();
ob_end_clean();
$SavedInfo['Data'] = $prc;
$key = $fileName;
$num = 1;
while (isset($SavedPDB[$key])) {
$num ++;
$key = $fileName . '-' . $num;
}
$SavedPDB[$key] = $SavedInfo;
}
function ShowDownloadLinks() {
global $SavedPDB, $MyFilename;
if (! isset($SavedPDB) || ! is_array($SavedPDB)) {
$SavedPDB = array();
session_register('SavedPDB');
}
krsort($SavedPDB);
if (count($SavedPDB)) {
?>
<table align=center border=1 cellpadding=5 cellspacing=0>
<tr bgcolor="#FFD0D0">
<th>Title</th>
<th>Type</th>
<th>Size</th>
<th>When Created</th>
<th> </th>
<th> </th>
</tr>
<?PHP
foreach ($SavedPDB as $key => $SavedInfo) {
?> <tr>
<td><?PHP echo $SavedInfo['Title'] ?></td>
<td><?PHP echo $SavedInfo['Type'] ?></td>
<td><?PHP echo ShowSize(strlen($SavedInfo['Data'])) ?></td>
<td><?PHP echo date("g:i:s a", $SavedInfo['Time']) ?></td>
<td><a href="<?PHP echo $MyFilename
?>?action=download&file=<?PHP echo $key ?>">Download</a></td>
<td><a href="<?PHP echo $MyFilename
?>?action=erase&file=<?PHP echo $key ?>">Erase</a></td>
</tr>
<?PHP
}
echo "</table>\n";
}
}
function ErasePDB() {
global $file, $SavedPDB;
if (isset($SavedPDB) && is_array($SavedPDB) && isset($SavedPDB[$file]))
unset($SavedPDB[$file]);
}
function DownloadPDB() {
global $file, $SavedPDB, $HTTP_USER_AGENT;
if (! isset($SavedPDB) || ! is_array($SavedPDB) ||
! isset($SavedPDB[$file])) {
StartHTML('Error Downloading File');
ShowError('The specified file no longer is saved. It most ' .
'likely expired.');
ShowInitialForm();
return;
}
$filename = $file . '.pdb';
if (strstr($HTTP_USER_AGENT, 'compatible; MSIE ') !== false &&
strstr($HTTP_USER_AGENT, 'Opera') === false) {
// IE doesn't properly download attachments. This should work
// pretty well for IE 5.5 SP 1
header("Content-Disposition: inline; filename=$filename");
header("Content-Type: application/download; name=\"$filename\"");
} else {
// Use standard headers for Netscape, Opera, etc.
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/x-pilot; name=\"$filename\"");
}
echo $SavedPDB[$file]['Data'];
}