-
Notifications
You must be signed in to change notification settings - Fork 0
/
pglaf.phh
749 lines (629 loc) · 22.3 KB
/
pglaf.phh
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
<?php
require_once('db.php');
require_once('blocks.phh');
abstract class Mode
{
const Development = 1;
const Production = 3;
}
if (str_endswith($_SERVER['SERVER_NAME'], 'pglaf.org'))
{
define('MODE', Mode::Production);
}
else
{
define('MODE', Mode::Development);
}
// $tpv_root_dir:
// Absolute path of directory containing (indirectly) all uploaded TP+V files.
// $tpv_root_url:
// Quasi-absolute URL that resolves to $tpv_root_dir.
// $spooldir:
// Absolute path of directory containing uploaded ebook files
// (and their accompanying info.txt files).
switch (MODE)
{
case Mode::Development:
$tpv_root_dir = "/home/michael/pg/pglaf/web_dyn";
$tpv_root_url = "/pglaf.web_dyn";
$spooldir = "/home/michael/pg/pglaf/priv_dyn/upload.spool";
$timezone = 'America/New_York';
break;
case Mode::Production:
$tpv_root_dir = "/htdocs/copy";
$tpv_root_url = "//copy.pglaf.org";
$spooldir = "/home/spool";
$timezone = 'America/Los_Angeles';
break;
default:
assert(FALSE);
}
$early_tpv_dir = "$tpv_root_dir/NEW";
// Absolute path of directory containing those TP+V files
// that users upload via 'Upload a Single File'
// (i.e., prior to submitting a clearance request).
$uplogfile = "/home/copy/upload.log"; // log upload events
$copylogfile = "/home/copy/copysubmit.log"; // log upload events
$tmpdir = "/tmp";
$clearedfile = "/home/cleared/cleared6.gbn"; // Feb 4 2014
// set the default timezone to use. gbn 20090816
date_default_timezone_set($timezone);
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
abstract class Status
{
const Unadjudicated = 1;
const Accepted = 2;
const Rejected = 5;
const Cancelled = 8;
const Text_Uploaded = 9;
}
// Store common stuff here so we don't need to look up static items:
$label_for_status_id_ = array(
Status::Unadjudicated => "AwaitingClearance",
Status::Accepted => "Cleared OK",
Status::Rejected => "Not OK",
Status::Cancelled => "Cancelled",
Status::Text_Uploaded => "Submitted",
);
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
class Config {
function __construct() {
$this->endtag = ""; // "" for html, " /" for xhtml
$this->domain = "copy.pglaf.org";
$this->css_url = "/css-server.php";
$this->page_encoding = "ISO-8859-1";
$this->dtd_public = "-//W3C//DTD HTML 4.01//EN";
$this->dtd_system = "http://www.w3.org/TR/html4/strict.dtd";
setlocale (LC_ALL, 'en_US');
set_include_path ('.:..');
}
}
$config = new Config ();
class Page {
function header ($caption) {
global $page_header, $config;
$search = array ("<script_title>", "<page_encoding>", "<dtd_public>", "<dtd_system>");
$replace = array ($caption, $config->page_encoding,
$config->dtd_public, $config->dtd_system);
echo (str_replace ($search, $replace, $page_header));
}
function footer () {
global $page_footer;
echo ($page_footer);
}
}
$page = new Page ();
function pageheader ($caption) {
echo "
<style>
table.compact th {text-align: left;}
</style>
";
// Currently, $page->header doesn't produce any output.
// Eventually, it will, but until then, I want some output during testing
// so that I can detect any changes.
if (MODE == Mode::Development) testing_comment("pageheadder:\n$caption\n");
global $page;
$page->header ($caption);
}
function pagefooter () {
global $page;
$page->footer ();
}
$script_abs_dir = dirname($_SERVER['SCRIPT_FILENAME']);
$template = file_get_contents("$script_abs_dir/template.html.inc");
$template = str_replace ("<script_date>", date ("Y-m-d H:i:s", getlastmod()), $template);
// gbn:
// list ($page_header, $page_footer) = explode ("<script_output>", $template);
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function get_application()
// This code services two 'applications',
// one at copy.pglaf.org and one at upload.pglaf.org.
// Sometimes the code needs to know which one it's being used for.
// (But we should probably try to reduce those cases.)
{
// On *.pglaf.org, the discriminator is in the server_name.
// Testing at home, the server is always 'localhost',
// so the discriminator is in the script_name
// (specifically, in the directory path, not the name of the script file necessarily).
// So we concatenate the two and look for the discriminator in that.
$quasi_request_url = $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
if (str_contains($quasi_request_url, 'upload'))
{
return 'upload';
}
elseif (str_contains($quasi_request_url, 'copy'))
{
return 'copy';
}
else
{
// Can't happen?
return 'copy';
}
}
function show_footer()
{
$app = get_application();
echo "
<hr>
<p>This system uses cookies to track who you are.
Be sure to <a href=\"logout.php\">logout</a> when done, if you are using
a public terminal.
</p>
<p>Trouble or questions?
";
if ($app == 'copy')
{
echo "Please email <i>copyright2024 AT pglaf.org,</i>
including details such as your clearance username and the item ID";
}
elseif ($app == 'upload')
{
echo "Please email <i>copyright2024 AT pglaf.org</i> for clearance questions,
or <i>pgww AT lists.pglaf.org</i> for production or status questions.";
}
else
{
assert(FALSE);
}
echo "
</p>
";
$discussion_url = "https://www.pgdp.net/phpBB3/viewtopic.php?f=27&t=63371";
echo "
<p>
<span style='color:red'>New!</span> (May 2017)
The web apps at copy.pglaf.org and upload.pglaf.org are undergoing some changes.
If you encounter a bug,
or have a comment about the software,
you're welcome to join the discussion at
<a href='$discussion_url'>this forum topic</a>.
(If you're not registered at pgdp.net,
you can send your feedback to <i>copyright2024 AT pglaf.org</i>.)
</p>
";
echo "
<ul>
<li>2018-01-01: You can now supply a scan-set URL when submitting a clearance request.</li>
<li>2017-05-29: New feature: If you use HTTP to access the login and new-user screens, they will now offer you a link to login/signup via HTTPS instead.</li>
<li>2017-05-12: New feature: <a href='//copy.pglaf.org/status.php'>status.php</a> now lets you fetch a clearance record if you know its key.</li>
<li>2017-05-04: Maintenance install: future-proofing and bugfixing. Users should see little to no difference.</li>
</ul>
";
echo "
</body>
</html>
";
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// People use some weird-ass filenames. Make more Unix-friendly:
// Note that this function is also referenced from /htdocs/epubmaker/*
function fix_filename($inname) {
// Get just the filename part, not the directory part (if any):
$inname = str_replace("\\", "/", $inname); // Any dos directories?
$inname = basename($inname); // Just the filename part
# $inname = ereg_replace("[^-_a-zA-Z0-9.]", "", $inname);
$inname = preg_replace("/[^-_a-zA-Z0-9.]/", "", $inname);
// Safety: If that removed everything, just give it a random
if (0 == strlen($inname)) {
$inname = time() . rand(1000,9999); //gmp_strval(gmp_random(1));
}
return($inname);
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function require_logged_in_user()
{
if (isset($_SESSION['pgusername']))
{
// The sender is a logged-in user.
return;
}
// The sender is not logged-in.
// Don't let them proceed.
$script_name = $_SERVER['SCRIPT_NAME'];
if (str_endswith($script_name, 'copy.php'))
{
print "<p>You are not logged in.</p>\n\n";
echo_login_form();
print "<a href='usernew.php'>New user?</a>\n";
show_footer();
}
elseif (str_endswith($script_name,'upload.php'))
{
print "<p>You are not logged in.</p>\n\n";
echo_login_form(TRUE);
print "<a href='//copy.pglaf.org/usernew.php'>New user?</a>\n";
show_footer();
}
else
{
print "<p><font color='red'>You need to <a href='copy.php'>login</a> first.</p>\n";
}
exit;
}
// -----------------------------------------------------------------------------
function echo_login_form($upload_kludge=FALSE)
{
encourage_https_for('login');
print "<form method='POST' accept-charset='UTF-8'>\n";
print "Username: <input type='text' size=50 name='username'><br>\n";
print "Password: <input type='password' size=50 name='userpass'><br>\n";
print "<input type='submit' name='login' value='login'>\n";
print "<input type='submit' name='logout' value='logout'>\n";
if ($upload_kludge)
{
$esc_in_ok_key = eha(@$_REQUEST['in_ok_key']);
print "<input type='hidden' name='in_ok_key' value='$esc_in_ok_key'>\n";
}
print "</form>\n\n";
}
function encourage_https_for($verb)
{
if (@$_SERVER['REQUEST_SCHEME'] != 'https')
{
$url = "https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$esc_url = eha($url);
print "<p style='width: 32em; padding: 3px; border: solid red 1px'>\n";
print "You can use the form on this page to $verb using HTTP,\n";
print "but we encourage you to securely <a href='$esc_url'>$verb using HTTPS</a> instead.\n";
print "</p>\n";
}
}
// -----------------------------------------------------------------------------
function handle_login_attempt()
// If the current request is an attempt to login
// (i.e., a submission from the login form),
// handle it.
{
if (isset ($_SESSION['pgusername']))
{
// The sender is a logged-in user,
// so the current request can't be a login attempt.
return;
}
$username = get_username_param($_REQUEST, 'username');
$userpass = get_password_param($_REQUEST, 'userpass');
if ($username == '')
{
// The sender didn't provide login credentials,
// so they're just viewing the page as a not-logged-in visitor.
return;
}
$row = confirm_credentials($username, $userpass);
$username_id=$row[0];
$firstname=$row[3];
unset($_REQUEST['login']);
$expire = time() + 24 * 3600 * 365; // 1 year
setcookie ("pgusername", $username, $expire, "/", "pglaf.org", 0);
setcookie ("pgusernameid", $username_id, $expire, "/", "pglaf.org", 0);
setcookie ("pgfirstname", $firstname, $expire, "/", "pglaf.org", 0);
$_SESSION['pgusername'] = $username;
$_SESSION['pgusernameid'] = $username_id;
$_SESSION['pgfirstname'] = $firstname;
// Reload the page with new values:
if (@$_REQUEST['in_ok_key'] != '') {
header ("Location: " . $_SERVER['PHP_SELF'] . "?in_ok_key=" . $_REQUEST['in_ok_key']);
} else {
header ("Location: " . $_SERVER['PHP_SELF']);
}
exit;
// Because after you send a "Location" header, there's no point sending anything else.
}
// -----------------------------------------------------------------------------
function confirm_credentials($username, $password=NULL)
{
if (is_null($password))
{
// Don't need to check password.
// I.e., just checking that $username is (still) in the 'users' table.
$condition = "(username='$username')";
}
else
{
$esc_password = db_escape(htmlspecialchars($password));
// It doesn't make sense to send it through 'htmlspecialchars',
// but we have to, because their password was sent through 'htmlspecialchars'
// when they signed up, so that's what's in the database.
// And we can't 'fix' the database, because what's there is a *hash* of the password,
// and there's no way to convert that to a hash of a 'fixed' password
// without knowing the password, which we don't.
// And we can't ask the users to reset their passwords,
// because there's no mechanism for them to do that.
$condition = "(username='$username') and (password=password('$esc_password'))";
}
$query = "SELECT * from users where $condition";
$result = db_query_od($query);
$row = db_fetch_row($result);
if (!$row)
{
// The result-set has no rows.
// I.e., the 'users' table has no row with the given username+password.
print "<p><font color=\"red\">Username/password not found[1]. Please <a href='{$_SERVER['SCRIPT_NAME']}'>try again</a>, or create a <a href=\"//copy.pglaf.org/usernew.php\">new username</a></font></p>";
exit;
}
if ($row[1] != $username)
{
// The 'users' table *does* have a matching row,
// but its 'username' field is not PHP-equal to $username.
// I think this can only happen if the username given just now
// is a 'case-variant' of the username given at signup.
// (I.e., they're equal only if you ignore case,
// which is what MySQL does by default.)
print "<p><font color=\"red\">Username/password not found[0]. Please try again, or create a <a href=\"//copy.pglaf.org/usernew.php\">new username</a></font></p>";
exit;
}
return $row;
}
// -----------------------------------------------------------------------------
function require_admin_user()
{
$login_username=$_SESSION['pgusername'];
if (!is_admin_user($login_username)) {
print "<p><font color=\"red\">Administrative access denied for $login_username</font></p>";
exit;
}
}
// -----------------------------------------------------------------------------
function is_admin_user($username)
{
// Hard-code who is allowed to admin:
$admin_usernames = array(
'gbnewby',
'JulietS',
'jmdyck',
'srjfoo',
'widger',
'ccx074',
);
return in_array($username, $admin_usernames, TRUE);
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function echo_nav_bar($menu_items=array())
{
assert(isset($_SESSION['pgusername']));
echo "<p>You are: <font color='green'>Logged in</font>"
. " as " . ehe($_SESSION['pgfirstname'])
. " [" . $_SESSION['pgusername'] . "]";
$menu_items = array('logout.php' => 'logout') + $menu_items;
foreach ($menu_items as $url => $label)
{
echo " (<a href='$url'>$label</a>)";
}
echo "</p>\n\n";
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function echo_clearance_action_form()
{
echo <<< _EOS_
<form method="POST" action="copy.php" accept-charset="UTF-8">
<input type="radio" name="userchoice" value="upload">
<a href="/cgi-bin/upfiles">Upload groups of page images for clearance</a><br>
<input type="radio" name="userchoice" value="newentry" checked>
<a href="submit.php">Submit a new clearance request</a><br>
<input type="radio" name="userchoice" value="status">
<a href="status.php">Get status of my prior clearance requests</a><br>
<input type="submit" name="selected" value="Go!">
</form>
_EOS_;
}
function handle_clearance_action_form()
// If the current request is a submission from the clearance action form,
// handle it.
{
if (isset ($_REQUEST['selected'])) {
if ($_REQUEST['userchoice'] == "upload") {
header ("Location: /cgi-bin/upfiles");
exit;
}
if ($_REQUEST['userchoice'] == "newentry") {
header ("Location: submit.php");
exit;
}
if ($_REQUEST['userchoice'] == "status") {
header ("Location: status.php");
exit;
}
}
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function echo_filtering_form($default_username)
{
print "<form action=\"admin.php\" method=\"get\" accept-charset=\"UTF-8\">\n";
print "<p>Menu:\n";
print "\n<br>Get username: <input type=\"text\" size=20 name=\"alternate_username\"";
if (strlen($default_username) > 0) {
print " value=$default_username";
}
print ">\n";
global $label_for_status_id_;
foreach ($label_for_status_id_ as $status_id => $label)
{
if ($label == 'Submitted')
continue;
// Why do we skip this option?
// Is it because the list might be long?
print "<input type=\"radio\" name=\"status_id\" value=\"$status_id\">$label\n";
}
if (!str_endswith($_SERVER['SCRIPT_NAME'], 'viewall.php'))
{
print "... <a href=\"viewall.php\">Everything</a>";
}
print "<input type=\"submit\" value=\"Submit\">\n";
print "</form>\n";
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function echo_clearance_key_form()
{
print "<form method=\"POST\" action=\"upload.php\" accept-charset=\"UTF-8\">\n";
print "<p>Provide your <i>Clearance OK key</i> to upload a completed eBook:";
print "<br><input type=\"text\" name=\"in_ok_key\" size=\"32\">\n";
print "<input type=\"submit\" name=\"submit\" value=\"Go!\">\n";
print "</form>\n";
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function get_username_param($array, $key)
{
return get_param_by_regex('/^[-_0-9a-zA-Z]+$/', $array, $key, FALSE, '', TRUE, '');
}
function get_password_param($array, $key)
{
return get_param_by_regex('/^.+$/', $array, $key, FALSE, '', TRUE, '');
}
function get_request_id_param($array, $key)
{
return get_param_by_regex('/^\d+$/', $array, $key, FALSE, '', TRUE, 'DIE');
}
function get_request_key_param($array, $key)
{
return get_param_by_regex('/^[0-9a-z-]+$/', $array, $key, FALSE, '', TRUE, 'DIE');
}
function get_status_id_param($array, $key)
{
return get_param_by_regex('/^[12589]$/', $array, $key, FALSE, strval(Status::Unadjudicated), TRUE, 'DIE');
}
function get_param_by_regex(
$regex, $array, $key,
$complain_if_unset, $result_if_unset,
$complain_if_bad, $result_if_bad)
{
assert_options(ASSERT_BAIL, TRUE);
assert(is_string($regex));
assert(is_array($array));
assert(is_string($key));
assert(is_bool($complain_if_unset));
assert(is_string($result_if_unset));
assert(is_bool($complain_if_bad));
assert(is_string($result_if_bad));
if (isset($array[$key]))
{
$arg = $array[$key];
// Trim whitespace (because copy-and-paste sometimes includes extra whitespace)
$arg = trim($arg);
if (is_string($arg) && preg_match($regex, $arg))
{
return $arg;
}
else
{
if ($complain_if_bad)
{
$esc_arg = ehe($arg);
echo "<p style='color:red'>Value passed to parameter '$key' is invalid: '$esc_arg'</p>\n";
}
if ($result_if_bad == 'DIE')
{
exit;
}
else
{
return $result_if_bad;
}
}
}
else
{
if ($complain_if_unset)
{
echo "<p style='color:red'>Parameter '$key' was not set.</p>\n";
}
if ($result_if_unset == 'DIE')
{
exit;
}
else
{
return $result_if_unset;
}
}
}
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function send_email($to_addr, $subject, $body, $more_headers)
{
if (MODE == Mode::Development)
{
testing_comment("The following email would have been sent:
================================================================================
To: $to_addr
Subject: $subject
$more_headers
$body
================================================================================
");
}
else
{
mail($to_addr, $subject, $body, $more_headers);
}
}
// -----------------------------------------------------------------------------
function append_to_log($filename, $log_entry)
{
if (MODE == Mode::Development)
{
testing_comment("would have appended to $filename:\n$log_entry");
}
else
{
$handle = fopen($filename, "a"); if (!$handle) return FALSE;
$w = fwrite($handle, $log_entry); if (!$w) return FALSE;
fclose($handle);
}
return TRUE;
}
// -----------------------------------------------------------------------------
function create_file($filename, $permissions, $content)
{
if (MODE == Mode::Development)
{
testing_comment("would have created file $filename with permissions $permissions and content:\n$content");
}
else
{
$handle = fopen($filename, "w");
fwrite($handle, $content);
fclose($handle);
chmod($filename, $permissions);
}
}
// -----------------------------------------------------------------------------
function testing_comment($content)
{
assert(MODE == Mode::Development);
$escaped_content = preg_replace('/--/', '~~', $content);
echo "<!--\nTESTING: $escaped_content-->\n";
}
// -----------------------------------------------------------------------------
function str_startswith($subject, $prefix)
// Return TRUE iff $subject starts with $prefix.
{
return ( strncmp( $subject, $prefix, strlen($prefix) ) == 0 );
}
function str_endswith($subject, $suffix)
// Return TRUE iff $subject ends with $suffix.
{
return ( substr($subject, -strlen($suffix)) == $suffix );
}
// gbn 2022-10-26: PHP 8.1 complained about this. It's a library
// function, so I don't know why it's also defined here.
// //function str_contains($haystack, $needle)
// // Return TRUE iff $haystack contains $needle.
// // {
// // return ( strpos($haystack, $needle) !== FALSE );
// // }
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function eha($s)
// Escape for HTML Attribute
// Entity-encode $s so that it's safe to use as the value of an attribute in HTML
// (assuming that the result will elsewhere be embedded in either single- or double-quotes).
{
return htmlspecialchars($s, ENT_QUOTES, 'ISO-8859-1');
}
function ehe($s)
// Escape for HTML Element
// Entity-encode $s so that it's safe to use in textual content of an HTML element.
{
return htmlspecialchars($s, ENT_NOQUOTES, 'ISO-8859-1');
}
// vim: sw=2 ts=2 expandtab