forked from chaosarium/lwt
-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
session_utility.php
4250 lines (3976 loc) · 138 KB
/
session_utility.php
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* \file
* \brief All the files needed for a LWT session.
*
* By requiring this file, you start a session, connect to the
* database and declare a lot of useful functions.
*
* PHP version 8.1
*
* @package Lwt
* @author HugoFara <hugo.farajallah@protonmail.com>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/docs/php/files/inc-session-utility.html
* @since 2.0.3-fork
*/
require_once __DIR__ . '/database_connect.php';
require_once __DIR__ . '/feeds.php';
require_once __DIR__ . '/tags.php';
/**
* Return navigation arrows to previous and next texts.
*
* @param int $textid ID of the current text
* @param string $url Base URL to append before $textid
* @param bool $onlyann Restrict to annotated texts only
* @param string $add Some content to add before the output
*
* @return string Arrows to previous and next texts.
*/
function getPreviousAndNextTextLinks($textid, $url, $onlyann, $add): string
{
global $tbpref;
$currentlang = validateLang(
(string) processDBParam("filterlang", 'currentlanguage', '', false)
);
$wh_lang = '';
if ($currentlang != '') {
$wh_lang = ' AND TxLgID=' . $currentlang;
}
$currentquery = (string) processSessParam("query", "currenttextquery", '', false);
$currentquerymode = (string) processSessParam(
"query_mode", "currenttextquerymode", 'title,text', false
);
$currentregexmode = getSettingWithDefault("set-regex-mode");
$wh_query = $currentregexmode . 'LIKE ';
if ($currentregexmode == '') {
$wh_query .= convert_string_to_sqlsyntax(
str_replace("*", "%", mb_strtolower($currentquery, 'UTF-8'))
);
} else {
$wh_query .= convert_string_to_sqlsyntax($currentquery);
}
switch ($currentquerymode) {
case 'title,text':
$wh_query=' AND (TxTitle ' . $wh_query . ' OR TxText ' . $wh_query . ')';
break;
case 'title':
$wh_query=' AND (TxTitle ' . $wh_query . ')';
break;
case 'text':
$wh_query=' AND (TxText ' . $wh_query . ')';
break;
}
if ($currentquery=='') {
$wh_query = '';
}
$currenttag1 = validateTextTag(
(string) processSessParam("tag1", "currenttexttag1", '', false),
$currentlang
);
$currenttag2 = validateTextTag(
(string) processSessParam("tag2", "currenttexttag2", '', false),
$currentlang
);
$currenttag12 = (string) processSessParam("tag12", "currenttexttag12", '', false);
$wh_tag1 = null;
$wh_tag2 = null;
if ($currenttag1 == '' && $currenttag2 == '') {
$wh_tag = '';
} else {
if ($currenttag1 != '') {
if ($currenttag1 == -1) {
$wh_tag1 = "group_concat(TtT2ID) IS NULL";
} else {
$wh_tag1 = "concat('/',group_concat(TtT2ID separator '/'),'/') like '%/" . $currenttag1 . "/%'";
}
}
if ($currenttag2 != '') {
if ($currenttag2 == -1) {
$wh_tag2 = "group_concat(TtT2ID) IS NULL";
}
else {
$wh_tag2 = "concat('/',group_concat(TtT2ID separator '/'),'/') like '%/" . $currenttag2 . "/%'";
}
}
if ($currenttag1 != '' && $currenttag2 == '') {
$wh_tag = " having (" . $wh_tag1 . ') ';
}
elseif ($currenttag2 != '' && $currenttag1 == '') {
$wh_tag = " having (" . $wh_tag2 . ') ';
} else {
$wh_tag = " having ((" . $wh_tag1 . ($currenttag12 ? ') AND (' : ') OR (') . $wh_tag2 . ')) ';
}
}
$currentsort = (int) processDBParam("sort", 'currenttextsort', '1', true);
$sorts = array('TxTitle','TxID desc','TxID asc');
$lsorts = count($sorts);
if ($currentsort < 1) {
$currentsort = 1;
}
if ($currentsort > $lsorts) {
$currentsort = $lsorts;
}
if ($onlyann) {
$sql =
'SELECT TxID
FROM (
(' . $tbpref . 'texts
LEFT JOIN ' . $tbpref . 'texttags ON TxID = TtTxID
)
LEFT JOIN ' . $tbpref . 'tags2 ON T2ID = TtT2ID
), ' . $tbpref . 'languages
WHERE LgID = TxLgID AND LENGTH(TxAnnotatedText) > 0 '
. $wh_lang . $wh_query . '
GROUP BY TxID ' . $wh_tag . '
ORDER BY ' . $sorts[$currentsort-1];
}
else {
$sql =
'SELECT TxID
FROM (
(' . $tbpref . 'texts
LEFT JOIN ' . $tbpref . 'texttags ON TxID = TtTxID
)
LEFT JOIN ' . $tbpref . 'tags2 ON T2ID = TtT2ID
), ' . $tbpref . 'languages
WHERE LgID = TxLgID ' . $wh_lang . $wh_query . '
GROUP BY TxID ' . $wh_tag . '
ORDER BY ' . $sorts[$currentsort-1];
}
$list = array(0);
$res = do_mysqli_query($sql);
while ($record = mysqli_fetch_assoc($res)) {
array_push($list, (int) $record['TxID']);
}
mysqli_free_result($res);
array_push($list, 0);
$listlen = count($list);
for ($i=1; $i < $listlen-1; $i++) {
if($list[$i] == $textid) {
if ($list[$i-1] !== 0) {
$title = tohtml(getTextTitle($list[$i-1]));
$prev = '<a href="' . $url . $list[$i-1] . '" target="_top"><img src="icn/navigation-180-button.png" title="Previous Text: ' . $title . '" alt="Previous Text: ' . $title . '" /></a>';
}
else {
$prev = '<img src="icn/navigation-180-button-light.png" title="No Previous Text" alt="No Previous Text" />';
}
if ($list[$i+1] !== 0) {
$title = tohtml(getTextTitle($list[$i+1]));
$next = '<a href="' . $url . $list[$i+1] .
'" target="_top"><img src="icn/navigation-000-button.png" title="Next Text: ' . $title . '" alt="Next Text: ' . $title . '" /></a>';
}
else {
$next = '<img src="icn/navigation-000-button-light.png" title="No Next Text" alt="No Next Text" />';
}
return $add . $prev . ' ' . $next;
}
}
return $add . '<img src="icn/navigation-180-button-light.png" title="No Previous Text" alt="No Previous Text" />
<img src="icn/navigation-000-button-light.png" title="No Next Text" alt="No Next Text" />';
}
/**
* Return an HTML formatted logo of the application.
*
* @since 2.7.0 Do no longer indicate database prefix in logo
*/
function echo_lwt_logo(): void
{
echo '<img class="lwtlogo" src="' . get_file_path('img/lwt_icon.png') . '" title="LWT" alt="LWT logo" />';
}
/**
* Return all different database prefixes that are in use.
*
* @return string[] A list of prefixes.
*
* @psalm-return list<string>
*/
function getprefixes(): array
{
$prefix = array();
$res = do_mysqli_query(
str_replace(
'_',
"\\_",
"SHOW TABLES LIKE " . convert_string_to_sqlsyntax_nonull('%_settings')
)
);
while ($row = mysqli_fetch_row($res)) {
$prefix[] = substr((string) $row[0], 0, -9);
}
mysqli_free_result($res);
return $prefix;
}
/**
* Return the list of media files found in folder, recursively.
*
* @param string $dir Directory to search into.
*
* @return array[] All paths found (matching files and folders) in "paths" and folders in "folders".
*
* @psalm-return array{paths: array, folders: array}
*/
function media_paths_search($dir): array
{
$is_windows = str_starts_with(strtoupper(PHP_OS), "WIN");
$mediadir = scandir($dir);
$formats = array('mp3', 'mp4', 'ogg', 'wav', 'webm');
$paths = array(
"paths" => array($dir),
"folders" => array($dir)
);
// For each item in directory
foreach ($mediadir as $path) {
if (str_starts_with($path, ".") || is_dir($dir . '/' . $path)) {
continue;
}
// Add files to paths
if ($is_windows) {
$encoded = mb_convert_encoding($path, 'UTF-8', 'Windows-1252');
} else {
$encoded = $path;
}
$ex = strtolower(pathinfo($encoded, PATHINFO_EXTENSION));
if (in_array($ex, $formats)) {
$paths["paths"][] = $dir . '/' . $encoded;
}
}
// Do the folder in a second time to get a better ordering
foreach ($mediadir as $path) {
if (str_starts_with($path, ".") || !is_dir($dir . '/' . $path)) {
continue;
}
// For each folder, recursive search
$subfolder_paths = media_paths_search($dir . '/' . $path);
$paths["folders"] = array_merge($paths["folders"], $subfolder_paths["folders"]);
$paths["paths"] = array_merge($paths["paths"], $subfolder_paths["paths"]);
}
return $paths;
}
/**
* Return the paths for all media files.
*
* @return array Paths of media files, in the form array<string, string>
*/
function get_media_paths(): array
{
$answer = array(
"base_path" => basename(getcwd())
);
if (!file_exists('media')) {
$answer["error"] = "does_not_exist";
} else if (!is_dir('media')) {
$answer["error"] = "not_a_directory";
} else {
$paths = media_paths_search('media');
$answer["paths"] = $paths["paths"];
$answer["folders"] = $paths["folders"];
}
return $answer;
}
/**
* Get the different options to display as acceptable media files.
*
* @param string $dir Directory containing files
*
* @return string HTML-formatted OPTION tags
*/
function selectmediapathoptions($dir): string
{
$r = "";
//$r = '<option disabled="disabled">-- Directory: ' . tohtml($dir) . ' --</option>';
$options = media_paths_search($dir);
foreach ($options["paths"] as $op) {
if (in_array($op, $options["folders"])) {
$r .= '<option disabled="disabled">-- Directory: ' . tohtml($op) . '--</option>';
} else {
$r .= '<option value="' . tohtml($op) . '">' . tohtml($op) . '</option>';
}
}
return $r;
}
/**
* Select the path for a media (audio or video).
*
* @param string $f HTML field name for media string in form. Will be used as this.form.[$f] in JS.
*
* @return string HTML-formatted string for media selection
*/
function selectmediapath($f): string
{
$media = get_media_paths();
$r = '<p>
YouTube, Dailymotion, Vimeo or choose a file in "../' . $media["base_path"] . '/media"
<br />
(only mp3, mp4, ogg, wav, webm files shown):
</p>
<p style="display: none;" id="mediaSelectErrorMessage"></p>
<img style="float: right; display: none;" id="mediaSelectLoadingImg" src="icn/waiting2.gif" />
<select name="Dir" style="display: none; width: 200px;"
onchange="{val=this.form.Dir.options[this.form.Dir.selectedIndex].value; if (val != \'\') this.form.'
. $f . '.value = val; this.form.Dir.value=\'\';}">
</select>
<span class="click" onclick="do_ajax_update_media_select();" style="margin-left: 16px;">
<img src="icn/arrow-circle-135.png" title="Refresh Media Selection" alt="Refresh Media Selection" />
Refresh
</span>
<script type="text/javascript">
// Populate fields with data
media_select_receive_data(' . json_encode($media) . ');
</script>';
return $r;
}
// -------------------------------------------------------------
function get_seconds_selectoptions($v): string
{
if (!isset($v) ) {
$v = 5;
}
$r = '';
for ($i=1; $i <= 10; $i++) {
$r .= "<option value=\"" . $i . "\"" . get_selected($v, $i);
$r .= ">" . $i . " sec</option>";
}
return $r;
}
// -------------------------------------------------------------
function get_playbackrate_selectoptions($v): string
{
if (!isset($v) ) {
$v = '10';
}
$r = '';
for ($i=5; $i <= 15; $i++) {
$text = ($i<10 ? (' 0.' . $i . ' x ') : (' 1.' . ($i-10) . ' x ') );
$r .= "<option value=\"" . $i . "\"" . get_selected($v, $i);
$r .= "> " . $text . " </option>";
}
return $r;
}
/**
* @return string|string[]
*
* @psalm-return array<string>|string
*/
function remove_soft_hyphens($str): array|string
{
return str_replace('', '', $str); // first '..' contains Softhyphen 0xC2 0xAD
}
/**
* @return null|string|string[]
*
* @psalm-return array<string>|null|string
*/
function replace_supp_unicode_planes_char($s): array|string|null
{
return preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xE2\x96\x88", $s);
/* U+2588 = UTF8: E2 96 88 = FULL BLOCK = ⬛︎ */
}
// -------------------------------------------------------------
function makeCounterWithTotal($max, $num): string
{
if ($max == 1) {
return '';
}
if ($max < 10) {
return $num . "/" . $max;
}
return substr(
str_repeat("0", strlen($max)) . $num,
-strlen($max)
) . "/" . $max;
}
// -------------------------------------------------------------
function encodeURI($url): string
{
$reserved = array(
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!',
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
);
$unescaped = array(
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
'%40'=>'@','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
);
$score = array(
'%23'=>'#'
);
return strtr(rawurlencode($url), array_merge($reserved, $unescaped, $score));
}
/**
* Echo the path of a file using the theme directory. Echo the base file name of
* file is not found
*
* @param string $filename Filename
*/
function print_file_path($filename): void
{
echo get_file_path($filename);
}
/**
* Get the path of a file using the theme directory
*
* @param string $filename Filename
*
* @return string File path if it exists, otherwise the filename
*/
function get_file_path($filename)
{
$file = getSettingWithDefault('set-theme-dir').preg_replace('/.*\//', '', $filename);
if (file_exists($file)) {
return $file;
}
return $filename;
}
// -------------------------------------------------------------
function get_sepas()
{
static $sepa;
if (!$sepa) {
$sepa = preg_quote(getSettingWithDefault('set-term-translation-delimiters'), '/');
}
return $sepa;
}
// -------------------------------------------------------------
function get_first_sepa()
{
static $sepa;
if (!$sepa) {
$sepa = mb_substr(
getSettingWithDefault('set-term-translation-delimiters'),
0, 1, 'UTF-8'
);
}
return $sepa;
}
/**
* Prepare options for mobile.
*
* @param "0"|"1"|"2" $v Current mobile type
*/
function get_mobile_display_mode_selectoptions($v): string
{
if (!isset($v)) {
$v = "0";
}
$r = "<option value=\"0\"" . get_selected($v, "0");
$r .= ">Auto</option>";
$r .= "<option value=\"1\"" . get_selected($v, "1");
$r .= ">Force Non-Mobile</option>";
$r .= "<option value=\"2\"" . get_selected($v, "2");
$r .= ">Force Mobile</option>";
return $r;
}
// -------------------------------------------------------------
function get_sentence_count_selectoptions($v): string
{
if (!isset($v)) {
$v = 1;
}
$r = "<option value=\"1\"" . get_selected($v, 1);
$r .= ">Just ONE</option>";
$r .= "<option value=\"2\"" . get_selected($v, 2);
$r .= ">TWO (+previous)</option>";
$r .= "<option value=\"3\"" . get_selected($v, 3);
$r .= ">THREE (+previous,+next)</option>";
return $r;
}
// -------------------------------------------------------------
function get_words_to_do_buttons_selectoptions($v): string
{
if (!isset($v)) {
$v = "1";
}
$r = "<option value=\"0\"" . get_selected($v, "0");
$r .= ">I Know All & Ignore All</option>";
$r .= "<option value=\"1\"" . get_selected($v, "1");
$r .= ">I Know All</option>";
$r .= "<option value=\"2\"" . get_selected($v, "2");
$r .= ">Ignore All</option>";
return $r;
}
// -------------------------------------------------------------
function get_regex_selectoptions($v): string
{
if (!isset($v)) {
$v = "";
}
$r = "<option value=\"\"" . get_selected($v, "");
$r .= ">Default</option>";
$r .= "<option value=\"r\"" . get_selected($v, "r");
$r .= ">RegEx</option>";
$r .= "<option value=\"COLLATE 'utf8_bin' r\"" . get_selected($v, "COLLATE 'utf8_bin' r");
$r .= ">RegEx CaseSensitive</option>";
return $r;
}
// -------------------------------------------------------------
function get_tooltip_selectoptions($v): string
{
if (!isset($v)) {
$v = 1;
}
$r = "<option value=\"1\"" . get_selected($v, 1);
$r .= ">Native</option>";
$r .= "<option value=\"2\"" . get_selected($v, 2);
$r .= ">JqueryUI</option>";
return $r;
}
// -------------------------------------------------------------
function get_themes_selectoptions($v): string
{
$themes = glob('themes/*', GLOB_ONLYDIR);
$r = '<option value="themes/Default/">Default</option>';
foreach($themes as $theme){
if($theme!='themes/Default') {
$r.= '<option value="'.$theme.'/" '. get_selected($v, $theme.'/');
$r .= ">". str_replace(array('themes/','_'), array('',' '), $theme) ."</option>";
}
}
return $r;
}
/**
* Get a session value and update it if necessary.
*
* @param string $reqkey If in $_REQUEST, update the session with $_REQUEST[$reqkey]
* @param string $sesskey Field of the session to get or update
* @param string|int $default Default value to return
* @param bool $isnum If true, convert the result to an int
*
* @return string|int The required data unless $isnum is specified
*/
function processSessParam($reqkey, $sesskey, $default, $isnum)
{
if (isset($_REQUEST[$reqkey])) {
$reqdata = trim($_REQUEST[$reqkey]);
$_SESSION[$sesskey] = $reqdata;
$result = $reqdata;
} elseif(isset($_SESSION[$sesskey])) {
$result = $_SESSION[$sesskey];
} else {
$result = $default;
}
if ($isnum) {
$result = (int)$result;
}
return $result;
}
/**
* Get a database value and update it if necessary.
*
* @param string $reqkey If in $_REQUEST, update the database with $_REQUEST[$reqkey]
* @param string $dbkey Field of the database to get or update
* @param string $default Default value to return
* @param bool $isnum If true, convert the result to an int
*
* @return string|int The string data unless $isnum is specified
*/
function processDBParam($reqkey, $dbkey, $default, $isnum)
{
$dbdata = getSetting($dbkey);
if (isset($_REQUEST[$reqkey])) {
$reqdata = trim($_REQUEST[$reqkey]);
saveSetting($dbkey, $reqdata);
$result = $reqdata;
} elseif ($dbdata != '') {
$result = $dbdata;
} else {
$result = $default;
}
if ($isnum) {
$result = (int)$result;
}
return $result;
}
// -------------------------------------------------------------
function getWordTagList($wid, $before=' ', $brack=1, $tohtml=1): string
{
global $tbpref;
$lbrack = $rbrack = '';
if ($brack) {
$lbrack = "[";
$rbrack = "]";
}
$r = get_first_value(
"SELECT IFNULL(
GROUP_CONCAT(DISTINCT TgText ORDER BY TgText separator ', '),
''
) AS value
FROM (
(
{$tbpref}words
LEFT JOIN {$tbpref}wordtags
ON WoID = WtWoID
)
LEFT JOIN {$tbpref}tags
ON TgID = WtTgID
)
WHERE WoID = $wid"
);
if ($r != '') {
$r = $before . $lbrack . $r . $rbrack;
}
if ($tohtml) {
$r = tohtml($r);
}
return $r;
}
/**
* Return the last inserted ID in the database
*
* @return int
*
* @since 2.6.0-fork Officially returns a int in documentation, as it was the case
*/
function get_last_key()
{
return (int)get_first_value('SELECT LAST_INSERT_ID() AS value');
}
/**
* If $value is true, return an HTML-style checked attribute.
*
* @param mixed $value Some value that can be evaluated as a boolean
*
* @return string ' checked="checked" ' if value is true, '' otherwise
*
* @psalm-return ' checked="checked" '|''
*/
function get_checked($value): string
{
if (!isset($value)) {
return '';
}
if ($value) {
return ' checked="checked" ';
}
return '';
}
/**
* Return an HTML attribute if $value is equal to $selval.
*
* @return string ''|' selected="selected" ' Depending if inputs are equal
*
* @psalm-return ' selected="selected" '|''
*/
function get_selected($value, $selval): string
{
if (!isset($value)) {
return '';
}
if ($value == $selval) {
return ' selected="selected" ';
}
return '';
}
/* Functions relative to word tests. */
/**
* Create a projection operator do perform word test.
*
* @param string $key Type of test.
* - 'words': selection from words
* - 'texts': selection from texts
* - 'lang': selection from language
* - 'text': selection from single text
* @param array|int $value Object to select.
*
* @return string SQL projection necessary
*
* @global string $tbpref
*/
function do_test_test_get_projection($key, $value)
{
global $tbpref;
$testsql = null;
switch ($key)
{
case 'words':
$id_string = implode(",", $value);
$testsql = " {$tbpref}words WHERE WoID IN ($id_string) ";
$cntlang = get_first_value(
"SELECT COUNT(DISTINCT WoLgID) AS value
FROM $testsql"
);
if ($cntlang > 1) {
echo "<p>Sorry - The selected terms are in $cntlang languages," .
" but tests are only possible in one language at a time.</p>";
exit();
}
break;
case 'texts':
$id_string = implode(",", $value);
$testsql = " {$tbpref}words, {$tbpref}textitems2
WHERE Ti2LgID = WoLgID AND Ti2WoID = WoID AND Ti2TxID IN ($id_string) ";
$cntlang = get_first_value(
"SELECT COUNT(DISTINCT WoLgID) AS value
FROM $testsql"
);
if ($cntlang > 1) {
echo "<p>Sorry - The selected terms are in $cntlang languages," .
" but tests are only possible in one language at a time.</p>";
exit();
}
break;
case 'lang':
$testsql = " {$tbpref}words WHERE WoLgID = $value ";
break;
case 'text':
$testsql = " {$tbpref}words, {$tbpref}textitems2
WHERE Ti2LgID = WoLgID AND Ti2WoID = WoID AND Ti2TxID = $value ";
break;
default:
my_die("do_test_test.php called with wrong parameters");
break;
}
return $testsql;
}
/**
* Prepare the SQL when the text is a selection.
*
* @param int $selection_type. 2 is words selection and 3 is terms selection.
* @param string $selection_data Comma separated ID of elements to test.
*
* @return string SQL formatted string suitable to projection (inserted in a "FROM ")
*/
function do_test_test_from_selection($selection_type, $selection_data)
{
$data_string_array = explode(",", trim($selection_data, "()"));
$data_int_array = array_map('intval', $data_string_array);
switch ((int)$selection_type) {
case 2:
$test_sql = do_test_test_get_projection('words', $data_int_array);
break;
case 3:
$test_sql = do_test_test_get_projection('texts', $data_int_array);
break;
default:
$test_sql = $selection_data;
$cntlang = get_first_value(
"SELECT COUNT(DISTINCT WoLgID) AS value
FROM $test_sql"
);
if ($cntlang > 1) {
echo "<p>Sorry - The selected terms are in $cntlang languages," .
" but tests are only possible in one language at a time.</p>";
exit();
}
}
return $test_sql;
}
/**
* Make the plus and minus controls in a test table for a word.
*
* @param int $score Score associated to this word
* @param int $status Status for this word
* @param int $wordid Word ID
*
* @return string the HTML-formatted string to use
*/
function make_status_controls_test_table($score, $status, $wordid): string
{
if ($score < 0) {
$scoret = '<span class="red2">' . get_status_abbr($status) . '</span>';
} else {
$scoret = get_status_abbr($status);
}
if ($status <= 5 || $status == 98) {
$plus = '<img src="icn/plus.png" class="click" title="+" alt="+" onclick="changeTableTestStatus(' . $wordid .',true);" />';
} else {
$plus = '<img src="'.get_file_path('icn/placeholder.png').'" title="" alt="" />';
}
if ($status >=1 ) {
$minus = '<img src="icn/minus.png" class="click" title="-" alt="-" onclick="changeTableTestStatus(' . $wordid .',false);" />';
} else {
$minus = '<img src="'.get_file_path('icn/placeholder.png').'" title="" alt="" />';
}
return ($status == 98 ? '' : $minus . ' ') . $scoret . ($status == 99 ? '' : ' ' . $plus);
}
/**
* Return options as HTML code to insert in a language select.
*
* @param string|int|null $v Selected language ID
* @param string $dt Default value to display
*/
function get_languages_selectoptions($v, $dt): string
{
global $tbpref;
$sql = "SELECT LgID, LgName FROM {$tbpref}languages
WHERE LgName<>'' ORDER BY LgName";
$res = do_mysqli_query($sql);
$r = '<option value="" ';
if (!isset($v) || trim((string) $v) == '') {
$r .= 'selected="selected"';
}
$r .= ">$dt</option>";
while ($record = mysqli_fetch_assoc($res)) {
$d = (string) $record["LgName"];
if (strlen($d) > 30 ) {
$d = substr($d, 0, 30) . "...";
}
$r .= "<option value=\"" . $record["LgID"] . "\" " .
get_selected($v, $record["LgID"]) . ">" . tohtml($d) . "</option>";
}
mysqli_free_result($res);
return $r;
}
// -------------------------------------------------------------
function get_languagessize_selectoptions($v): string
{
if (!isset($v)) {
$v = 100;
}
$r = "<option value=\"100\"" . get_selected($v, 100);
$r .= ">100 %</option>";
$r .= "<option value=\"150\"" . get_selected($v, 150);
$r .= ">150 %</option>";
$r .= "<option value=\"200\"" . get_selected($v, 200);
$r .= ">200 %</option>";
$r .= "<option value=\"250\"" . get_selected($v, 250);
$r .= ">250 %</option>";
return $r;
}
// -------------------------------------------------------------
function get_wordstatus_radiooptions($v): string
{
if (!isset($v)) {
$v = 1;
}
$r = "";
$statuses = get_statuses();
foreach ($statuses as $n => $status) {
$r .= '<span class="status' . $n . '" title="' . tohtml($status["name"]) . '">';
$r .= ' <input type="radio" name="WoStatus" value="' . $n . '"';
if ($v == $n) {
$r .= ' checked="checked"';
}
$r .= ' />' . tohtml($status["abbr"]) . " </span> ";
}
return $r;
}
// -------------------------------------------------------------
function get_wordstatus_selectoptions($v, $all, $not9899, $off=true): string
{
if (!isset($v)) {
if ($all) {
$v = "";
} else {
$v = 1;
}
}
$r = "";
if ($all && $off) {
$r .= "<option value=\"\"" . get_selected($v, '');
$r .= ">[Filter off]</option>";
}
$statuses = get_statuses();
foreach ($statuses as $n => $status) {
if ($not9899 && ($n == 98 || $n == 99)) {
continue;
}
$r .= "<option value =\"" . $n . "\"" . get_selected($v, $n!=0?$n:'0');
$r .= ">" . tohtml($status['name']) . " [" .
tohtml($status['abbr']) . "]</option>";
}
if ($all) {
$r .= '<option disabled="disabled">--------</option>';
$status_1_name = tohtml($statuses[1]["name"]);
$status_1_abbr = tohtml($statuses[1]["abbr"]);
$r .= "<option value=\"12\"" . get_selected($v, 12);
$r .= ">" . $status_1_name . " [" . $status_1_abbr . ".." .
tohtml($statuses[2]["abbr"]) . "]</option>";
$r .= "<option value=\"13\"" . get_selected($v, 13);
$r .= ">" . $status_1_name . " [" . $status_1_abbr . ".." .
tohtml($statuses[3]["abbr"]) . "]</option>";
$r .= "<option value=\"14\"" . get_selected($v, 14);
$r .= ">" . $status_1_name . " [" . $status_1_abbr . ".." .
tohtml($statuses[4]["abbr"]) . "]</option>";
$r .= "<option value=\"15\"" . get_selected($v, 15);
$r .= ">Learning/-ed [" . $status_1_abbr . ".." .
tohtml($statuses[5]["abbr"]) . "]</option>";
$r .= '<option disabled="disabled">--------</option>';
$status_2_name = tohtml($statuses[2]["name"]);
$status_2_abbr = tohtml($statuses[2]["abbr"]);
$r .= "<option value=\"23\"" . get_selected($v, 23);
$r .= ">" . $status_2_name . " [" . $status_2_abbr . ".." .
tohtml($statuses[3]["abbr"]) . "]</option>";
$r .= "<option value=\"24\"" . get_selected($v, 24);
$r .= ">" . $status_2_name . " [" . $status_2_abbr . ".." .
tohtml($statuses[4]["abbr"]) . "]</option>";
$r .= "<option value=\"25\"" . get_selected($v, 25);
$r .= ">Learning/-ed [" . $status_2_abbr . ".." .
tohtml($statuses[5]["abbr"]) . "]</option>";
$r .= '<option disabled="disabled">--------</option>';
$status_3_name = tohtml($statuses[3]["name"]);
$status_3_abbr = tohtml($statuses[3]["abbr"]);
$r .= "<option value=\"34\"" . get_selected($v, 34);
$r .= ">" . $status_3_name . " [" . $status_3_abbr . ".." .
tohtml($statuses[4]["abbr"]) . "]</option>";
$r .= "<option value=\"35\"" . get_selected($v, 35);
$r .= ">Learning/-ed [" . $status_3_abbr . ".." .
tohtml($statuses[5]["abbr"]) . "]</option>";
$r .= '<option disabled="disabled">--------</option>';
$r .= "<option value=\"45\"" . get_selected($v, 45);
$r .= ">Learning/-ed [" . tohtml($statuses[4]["abbr"]) . ".." .
tohtml($statuses[5]["abbr"]) . "]</option>";
$r .= '<option disabled="disabled">--------</option>';
$r .= "<option value=\"599\"" . get_selected($v, 599);
$r .= ">All known [" . tohtml($statuses[5]["abbr"]) . "+" .
tohtml($statuses[99]["abbr"]) . "]</option>";
}
return $r;
}
// -------------------------------------------------------------
function get_annotation_position_selectoptions($v): string
{
if (! isset($v) ) { $v = 1;
}
$r = "<option value=\"1\"" . get_selected($v, 1);