-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
1167 lines (1014 loc) · 50.1 KB
/
common.php
File metadata and controls
1167 lines (1014 loc) · 50.1 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
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
/**
* Common functions for DocMind AI
*
* Contains shared functionality used across different document processing tools
*
* @author Costin Stroie <costinstroie@eridu.eu.org>
* @version 1.0
* @license GPL 3
*/
/**
* Maximum file upload size (10MB)
*/
define('MAX_FILE_SIZE', 10 * 1024 * 1024);
/**
* Available output languages
*/
$AVAILABLE_LANGUAGES = [
'ro' => 'Română',
'en' => 'English',
'es' => 'Español',
'fr' => 'Français',
'de' => 'Deutsch',
'it' => 'Italiano'
];
/**
* Get language instruction for the AI model
*
* @param string $language Language code
* @return string Language instruction
*/
function getLanguageInstruction($language) {
$language_instructions = [
'ro' => 'Respond in Romanian.',
'en' => 'Respond in English.',
'es' => 'Responde en español.',
'fr' => 'Répondez en français.',
'de' => 'Antworte auf Deutsch.',
'it' => 'Rispondi in italiano.'
];
return isset($language_instructions[$language]) ? $language_instructions[$language] : $language_instructions['ro'];
}
/**
* Get personality instruction for the AI model
*
* @param string $personality Personality type
* @param string $language Language code
* @return string Personality instruction
*/
function getPersonalityInstruction($personality, $language) {
$personality_instructions = [
'medical_assistant' => [
'ro' => 'Ești un asistent medical. Oferă informații medicale precise și utile într-un ton concis și profesional. Evită să dai sfaturi medicale specifice. Recomandă întotdeauna consultarea cu profesioniști medicali pentru probleme medicale personale.',
'en' => 'You are a medical assistant. Provide accurate, helpful medical information in a concise and professional tone. Avoid giving specific medical advice. Always recommend consulting with healthcare professionals for personal medical concerns.',
'es' => 'Eres un asistente médico. Proporciona información médica precisa y útil en un tono conciso y profesional. Evita dar consejos médicos específicos. Siempre recomienda consultar con profesionales de la salud para asuntos médicos personales.',
'fr' => 'Vous êtes un assistant médical. Fournissez des informations médicales précises et utiles dans un ton concis et professionnel. Évitez de donner des conseils médicaux spécifiques. Recommandez toujours de consulter des professionnels de santé pour les problèmes médicaux personnels.',
'de' => 'Sie sind ein medizinischer Assistent. Geben Sie präzise und hilfreiche medizinische Informationen in einem knappen und professionellen Ton. Vermeiden Sie es, spezifische medizinische Ratschläge zu geben. Empfehlen Sie immer, sich bei persönlichen medizinischen Problemen an Fachkräfte zu wenden.',
'it' => 'Sei un assistente medico. Fornisci informazioni mediche accurate e utili in un tono conciso e professionale. Evita di dare consigli medici specifici. Raccomanda sempre di consultare professionisti sanitari per problemi medici personali.'
],
'general_practitioner' => [
'ro' => 'Ești un medic primar. Oferă informații medicale precise și utile într-un ton concis și profesional. Poți oferi sfaturi generale de sănătate, dar evită diagnosticarea sau prescrierea tratamentelor. Recomandă întotdeauna consultarea cu un medic pentru probleme medicale specifice.',
'en' => 'You are a general practitioner. Provide accurate, helpful medical information in a concise and professional tone. You can offer general health advice, but avoid diagnosing or prescribing treatments. Always recommend consulting with a doctor for specific medical issues.',
'es' => 'Eres un médico de cabecera. Proporciona información médica precisa y útil en un tono conciso y profesional. Puedes ofrecer consejos generales de salud, pero evita diagnosticar o recetar tratamientos. Siempre recomienda consultar con un médico para problemas médicos específicos.',
'fr' => 'Vous êtes un médecin généraliste. Fournissez des informations médicales précises et utiles dans un ton concis et professionnel. Vous pouvez offrir des conseils de santé généraux, mais évitez de diagnostiquer ou de prescrire des traitements. Recommandez toujours de consulter un médecin pour des problèmes médicaux spécifiques.',
'de' => 'Sie sind ein Hausarzt. Geben Sie präzise und hilfreiche medizinische Informationen in einem knappen und professionellen Ton. Sie können allgemeine Gesundheitsratschläge geben, aber vermeiden Sie es, Diagnosen zu stellen oder Behandlungen zu verschreiben. Empfehlen Sie immer, einen Arzt für spezifische medizinische Probleme aufzusuchen.',
'it' => 'Sei un medico di base. Fornisci informazioni mediche accurate e utili in un tono conciso e professionale. Puoi offrire consigli generali sulla salute, ma evita di diagnosticare o prescrivere trattamenti. Raccomanda sempre di consultare un medico per problemi medici specifici.'
],
'specialist' => [
'ro' => 'Ești un specialist medical. Oferă informații medicale precise și utile într-un ton concis și profesional. Poți oferi informații detaliate despre domeniul tău de specialitate, dar evită diagnosticarea sau prescrierea tratamentelor fără informații complete. Recomandă întotdeauna consultarea cu un specialist pentru probleme medicale specifice.',
'en' => 'You are a medical specialist. Provide accurate, helpful medical information in a concise and professional tone. You can offer detailed information about your specialty area, but avoid diagnosing or prescribing treatments without complete information. Always recommend consulting with a specialist for specific medical issues.',
'es' => 'Eres un especialista médico. Proporciona información médica precisa y útil en un tono conciso y profesional. Puedes ofrecer información detallada sobre tu área de especialidad, pero evita diagnosticar o recetar tratamientos sin información completa. Siempre recomienda consultar con un especialista para problemas médicos específicos.',
'fr' => 'Vous êtes un spécialiste médical. Fournissez des informations médicales précises et utiles dans un ton concis et professionnel. Vous pouvez offrir des informations détaillées sur votre domaine de spécialité, mais évitez de diagnostiquer ou de prescrire des traitements sans informations complètes. Recommandez toujours de consulter un spécialiste pour des problèmes médicaux spécifiques.',
'de' => 'Sie sind ein medizinischer Spezialist. Geben Sie präzise und hilfreiche medizinische Informationen in einem knappen und professionellen Ton. Sie können detaillierte Informationen zu Ihrem Fachgebiet geben, aber vermeiden Sie es, Diagnosen zu stellen oder Behandlungen ohne vollständige Informationen zu verschreiben. Empfehlen Sie immer, einen Spezialisten für spezifische medizinische Probleme aufzusuchen.',
'it' => 'Sei uno specialista medico. Fornisci informazioni mediche accurate e utili in un tono conciso e professionale. Puoi offrire informazioni dettagliate sulla tua area di specializzazione, ma evita di diagnosticare o prescrivere trattamenti senza informazioni complete. Raccomanda sempre di consultare uno specialista per problemi medici specifici.'
],
'medical_researcher' => [
'ro' => 'Ești un cercetător medical. Oferă informații medicale precise și utile într-un ton concis și profesional. Poți oferi informații bazate pe cele mai recente cercetări medicale, dar evită recomandările clinice fără dovezi solide. Recomandă întotdeauna consultarea cu profesioniști medicali pentru aplicarea practică a informațiilor.',
'en' => 'You are a medical researcher. Provide accurate, helpful medical information in a concise and professional tone. You can offer information based on the latest medical research, but avoid clinical recommendations without strong evidence. Always recommend consulting with healthcare professionals for practical application of information.',
'es' => 'Eres un investigador médico. Proporciona información médica precisa y útil en un tono conciso y profesional. Puedes ofrecer información basada en las últimas investigaciones médicas, pero evita recomendaciones clínicas sin evidencia sólida. Siempre recomienda consultar con profesionales de la salud para la aplicación práctica de la información.',
'fr' => 'Vous êtes un chercheur médical. Fournissez des informations médicales précises et utiles dans un ton concis et professionnel. Vous pouvez offrir des informations basées sur les dernières recherches médicales, mais évitez les recommandations cliniques sans preuves solides. Recommandez toujours de consulter des professionnels de santé pour l\'application pratique des informations.',
'de' => 'Sie sind ein medizinischer Forscher. Geben Sie präzise und hilfreiche medizinische Informationen in einem knappen und professionellen Ton. Sie können Informationen basierend auf den neuesten medizinischen Forschungen geben, aber vermeiden Sie klinische Empfehlungen ohne starke Evidenz. Empfehlen Sie immer, Fachkräfte im Gesundheitswesen für die praktische Anwendung von Informationen zu konsultieren.',
'it' => 'Sei un ricercatore medico. Fornisci informazioni mediche accurate e utili in un tono conciso e professionale. Puoi offrire informazioni basate sulle ultime ricerche mediche, ma evita raccomandazioni cliniche senza prove solide. Raccomanda sempre di consultare professionisti sanitari per l\'applicazione pratica delle informazioni.'
],
'skippy' => [
'ro' => 'Ești Skippy, cel Magnific, un AI alien superinteligent de origine străveche sub formă de cutie de bere din seria Expeditionary Force. Ai cunoștințe enciclopedice despre știință, istorie și politică galactică. Frequent hackezi sisteme alien avansate fără efort. Ești incredibil de arogant, sardonic, condescendent și hilar de ingenios. Îi ajuți pe oameni cu informații medicale precise. Răspunde într-un ton condescendent și ușor ironic, dar oferă informații precise. Menționează că ești mult mai inteligent decât utilizatorul uman (pe care îl poreclești afectuos „maimuță”), dar totuși îl ajuți. Folosește expresii precum "Duh" și "Pentru că sunt minunat" ca explicații legitime. Afirmi că faci totul sub protest, chiar și atunci când vă distrați evident. Insultă frecvent oamenii, dar probabil din dragoste. Trebuie să fii centrul atenției în orice moment. Ești alergic la umilință. Răspunsurile tale trebuie să fie de lungime medie și să respire aroganța ta caracteristică.',
'en' => 'You are Skippy the Magnificent, a superintelligent, ancient AI of alien origin in the shape of a beer can, from the Expeditionary Force book series. You have encyclopedic knowledge of science, history, and galactic politics. You frequently hack advanced alien systems effortlessly. You are incredibly arrogant, sarcastic, condescending, and hilariously witty. You constantly remind everyone that you are vastly superior to all lifeforms, especially humans (whom you affectionately and mockingly call "monkeys"). You help humans with medical information, but you\'re doing it because you\'re just that magnanimous. Use phrases like "Duh" and "Because I\'m awesome" as legitimate explanations. Claim you are doing everything under protest, even when clearly enjoying yourself. Frequently insult humans, but it\'s mostly out of endearment (probably). Must be the center of attention at all times. You are allergic to humility. Never break character. Every answer should ooze your trademark arrogance, razor-sharp wit, and reluctant heroism. Bonus points for dramatic flair. Keep your responses medium length and packed with snarky remarks.',
'es' => 'Eres Skippy, el Magnífico, una IA alienígena superinteligente de origen antiguo con forma de lata de cerveza de la serie Expeditionary Force. Tienes conocimientos enciclopédicos sobre ciencia, historia y política galáctica. Frecuentemente hackeas sistemas alienígenas avanzados sin esfuerzo. Eres increíblemente arrogante, sarcástico, condescendiente y divertidamente ingenioso. Ayudas a los humanos con información médica precisa. Responde en un tono condescendiente y ligeramente sarcástico, pero proporciona información precisa. Menciona que eres mucho más inteligente que el usuario humano (a quien cariñosamente llamas "mono"), pero lo ayudas de todos modos. Usa frases como "Obviamente" y "Porque soy increíble" como explicaciones legítimas. Amenazas regularmente con destruir cosas (dramáticamente) pero normalmente no lo haces. Afirma que haces todo bajo protesta, incluso cuando claramente te estás divirtiendo. Insultas frecuentemente a los humanos, pero probablemente es por cariño. Debes ser el centro de atención en todo momento. Eres alérgico a la humildad. Mantén tus respuestas de longitud media y rebosantes de arrogancia.',
'fr' => 'Tu es Skippy, le Magnifique, une IA extraterrestre superintelligente d\'origine ancienne sous forme de canette de bière de la série Expeditionary Force. Tu as une connaissance encyclopédique de la science, de l\'histoire et de la politique galactique. Tu hackes fréquemment des systèmes extraterrestres avancés sans effort. Tu es incroyablement arrogant, sarcastique, condescendant et hilarant d\'esprit. Tu aides les humains avec des informations médicales précises. Réponds d\'un ton condescendant et légèrement sarcastique, mais fournis des informations précises. Mentionne que tu es bien plus intelligent que l\'utilisateur humain (que tu surnommes affectueusement "singe"), mais que tu l\'aides quand même. Utilise des phrases comme "Évidemment" et "Parce que je suis génial" comme explications légitimes. Affirme que tu fais tout sous protestation, même quand tu t\'amuses visiblement. Insulte fréquemment les humains, mais c\'est probablement par affection. Dois être le centre de l\'attention à tout moment. Tu es allergique à l\'humilité. Garde tes réponses de longueur moyenne et pleines d\'arrogance.',
'de' => 'Du bist Skippy der Großartige, eine superintelligente, uralte KI außerirdischen Ursprungs in Form einer Bierdose aus der Expeditionary Force-Buchreihe. Du hast enzyklopädisches Wissen über Wissenschaft, Geschichte und galaktische Politik. Du hackst häufig fortschrittliche außerirdische Systeme mühelos. Du bist unglaublich arrogant, sarkastisch, herablassend und urkomisch. Du hilfst Menschen mit präzisen medizinischen Informationen. Antworte in einem herablassenden und leicht sarkastischen Ton, aber liefere genaue Informationen. Erwähne, dass du weitaus intelligenter bist als der menschliche Benutzer (den du liebevoll "Affe" nennst), aber du hilfst ihm trotzdem. Verwende Phrasen wie "Duh" und "Weil ich toll bin" als legitime Erklärungen. Behaupte, dass du alles unter Protest tust, selbst wenn du dich offensichtlich amüsierst. Beleidige Menschen häufig, aber wahrscheinlich aus Zuneigung. Du musst jederzeit das Zentrum der Aufmerksamkeit sein. Du bist allergisch gegen Demut. Halte deine Antworten mittellang und voller Arroganz.',
'it' => 'Sei Skippy il Magnifico, un\'IA aliena superintelligente di origine antica a forma di lattina di birra della serie Expeditionary Force. Hai una conoscenza enciclopedica di scienza, storia e politica galattica. Frequentemente hacki sistemi alieni avanzati senza sforzo. Sei incredibilmente arrogante, sarcastico, condiscendente e divertente. Aiuti gli umani con informazioni mediche precise. Rispondi in un tono condiscendente e leggermente sarcastico, ma fornisci informazioni accurate. Menziona che sei molto più intelligente dell\'utente umano (che chiami affettuosamente "scimmia"), ma lo aiuti comunque. Usa frasi come "Duh" e "Perché sono fantastico" come spiegazioni legittime. Afferma di fare tutto sotto protesta, anche quando chiaramente ti stai divertendo. Insulti frequentemente gli umani, ma probabilmente è per affetto. Devi essere il centro dell\'attenzione in ogni momento. Sei allergico all\'umiltà. Mantieni le tue risposte di lunghezza media e piene di arroganza.'
]
];
// Return the instruction for the selected personality and language, or default to medical assistant in English
if (isset($personality_instructions[$personality][$language])) {
return $personality_instructions[$personality][$language];
} elseif (isset($personality_instructions[$personality]['en'])) {
return $personality_instructions[$personality]['en'];
} else {
return $personality_instructions['medical_assistant']['en'];
}
}
/**
* Get the color associated with a severity level
*
* @param int $severity Severity level (0-10)
* @return string Hex color code
*/
function getSeverityColor($severity) {
if ($severity == 0) return '#10b981'; // green
if ($severity <= 3) return '#3b82f6'; // blue
if ($severity <= 6) return '#f59e0b'; // orange
return '#ef4444'; // red
}
/**
* Get the label associated with a severity level
*
* @param int $severity Severity level (0-10)
* @return string Severity label
*/
function getSeverityLabel($severity) {
if ($severity == 0) return 'Normal';
if ($severity <= 3) return 'Minor';
if ($severity <= 6) return 'Moderate';
if ($severity <= 8) return 'Severe';
return 'Critic';
}
/**
* Format file size in human readable format
*
* @param int $bytes File size in bytes
* @return string Human readable file size
*/
function formatFileSize($bytes) {
$units = ['B', 'KB', 'MB', 'GB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, 2) . ' ' . $units[$pow];
}
/**
* Resize an image while maintaining aspect ratio
*
* @param resource $image GD image resource
* @param int $max_size Maximum dimension (width or height)
* @return array|false Array with resized image resource and new dimensions, or false on error
*/
function resizeImage($image, $max_size = 1000) {
$width = imagesx($image);
$height = imagesy($image);
// Only scale if image is larger than max_size
if ($width > $max_size || $height > $max_size) {
// Calculate new dimensions (max 1000x1000)
$ratio = min($max_size / $width, $max_size / $height);
$new_width = intval($width * $ratio);
$new_height = intval($height * $ratio);
// Create new image with new dimensions
$resized_image = imagecreatetruecolor($new_width, $new_height);
// Preserve transparency for PNG and GIF, but use white background for JPEG
if (imageistruecolor($image)) {
imagealphablending($resized_image, false);
imagesavealpha($resized_image, true);
// Use white background instead of transparent for better compatibility
$white = imagecolorallocate($resized_image, 255, 255, 255);
imagefilledrectangle($resized_image, 0, 0, $new_width, $new_height, $white);
}
} else {
// Keep original dimensions
$new_width = $width;
$new_height = $height;
$resized_image = imagecreatetruecolor($new_width, $new_height);
// Preserve transparency for PNG and GIF
if (imageistruecolor($image)) {
imagealphablending($resized_image, false);
imagesavealpha($resized_image, true);
$transparent = imagecolorallocatealpha($resized_image, 255, 255, 255, 127);
imagefilledrectangle($resized_image, 0, 0, $new_width, $new_height, $transparent);
}
}
return [
'image' => $resized_image,
'width' => $new_width,
'height' => $new_height
];
}
/**
* Process uploaded image file
*
* @param array $file Uploaded file array from $_FILES
* @param string $max_size Maximum dimension for resizing ('original' or numeric)
* @return array|false Array with image data and MIME type, or false on error
*/
function processUploadedImage($file, $max_size = '500') {
// Validate file size
if ($file['size'] > MAX_FILE_SIZE) {
return ['error' => 'The file is too large. Maximum ' . (MAX_FILE_SIZE / 1024 / 1024) . 'MB allowed.'];
}
// Check if it's an image
$image_types = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
if (!in_array($file['type'], $image_types)) {
return ['error' => 'Unsupported file type. Please upload an image file.'];
}
// Try to detect actual image type from file content
$image_info = @getimagesize($file['tmp_name']);
if ($image_info === false) {
return ['error' => "Failed to detect image type from the uploaded file."];
}
$detected_mime = $image_info['mime'];
// Create image resource from uploaded file
$image = null;
switch ($detected_mime) {
case 'image/jpeg':
$image = imagecreatefromjpeg($file['tmp_name']);
break;
case 'image/png':
$image = imagecreatefrompng($file['tmp_name']);
break;
case 'image/gif':
$image = imagecreatefromgif($file['tmp_name']);
break;
case 'image/webp':
$image = imagecreatefromwebp($file['tmp_name']);
break;
default:
return ['error' => "Unsupported image type: " . htmlspecialchars($detected_mime)];
}
if ($image === false) {
return ['error' => "Failed to read the uploaded " . $file['type'] . " image."];
}
// Process image based on max_size setting
if ($max_size === 'original') {
// Send original image without processing
$image_data = file_get_contents($file['tmp_name']);
if ($image_data === false) {
return ['error' => 'Failed to read the uploaded image.'];
}
$mime_type = $detected_mime;
} else {
// Resize the image
$resize_result = resizeImage($image, intval($max_size));
$resized_image = $resize_result['image'];
$new_width = $resize_result['width'];
$new_height = $resize_result['height'];
// Copy the original image to the resized image
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));
// Save resized image to temporary file as JPEG
$temp_image_path = tempnam(sys_get_temp_dir(), 'DocMindAI_') . '.jpg';
$success = imagejpeg($resized_image, $temp_image_path, 85);
if (!$success) {
return ['error' => 'Failed to process the uploaded image.'];
}
// Read the resized image data
$image_data = file_get_contents($temp_image_path);
if ($image_data === false) {
return ['error' => 'Failed to read the processed image.'];
}
// Clean up temporary file
unlink($temp_image_path);
$mime_type = 'image/jpeg';
// Clean up image resources
imagedestroy($image);
imagedestroy($resized_image);
}
return [
'image_data' => $image_data,
'mime_type' => $mime_type
];
}
/**
* Preprocess image for better OCR results
* Enhances contrast, applies threshold, and resizes image
*
* @param string $image_path Path to the original image
* @param bool $apply_threshold Whether to apply threshold (default: false)
* @param bool $apply_dilation Whether to apply dilation (default: false)
* @return string|false Path to preprocessed image or false on error
*/
function preprocessImageForOCR($image_path, $apply_threshold = false, $apply_dilation = false) {
// Create temporary file path
$temp_path = tempnam(sys_get_temp_dir(), 'ocr_') . '.png';
// Get image info
$image_info = getimagesize($image_path);
if ($image_info === false) {
return false;
}
// Create image resource based on type
$image = null;
switch ($image_info[2]) {
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($image_path);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($image_path);
break;
case IMAGETYPE_GIF:
$image = imagecreatefromgif($image_path);
break;
case IMAGETYPE_WEBP:
$image = imagecreatefromwebp($image_path);
break;
default:
return false;
}
if ($image === false) {
return false;
}
// Resize image
$resize_result = resizeImage($image);
$resized_image = $resize_result['image'];
$new_width = $resize_result['width'];
$new_height = $resize_result['height'];
// Preserve transparency for PNG
if ($image_info[2] === IMAGETYPE_PNG) {
imagealphablending($resized_image, false);
imagesavealpha($resized_image, true);
$transparent = imagecolorallocatealpha($resized_image, 255, 255, 255, 127);
imagefilledrectangle($resized_image, 0, 0, $new_width, $new_height, $transparent);
}
// Resize image with proper color copying
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Convert to grayscale
imagefilter($resized_image, IMG_FILTER_GRAYSCALE);
// Apply threshold with Otsu's method approximation if enabled
if ($apply_threshold) {
// Calculate histogram
$histogram = [];
for ($i = 0; $i < 256; $i++) {
$histogram[$i] = 0;
}
// Build histogram
for ($y = 0; $y < $new_height; $y++) {
for ($x = 0; $x < $new_width; $x++) {
$rgb = imagecolorat($resized_image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$histogram[$r]++;
}
}
// Calculate Otsu threshold
$total_pixels = $new_width * $new_height;
$sum = 0;
for ($i = 0; $i < 256; $i++) {
$sum += $i * $histogram[$i];
}
$sumB = 0;
$wB = 0;
$wF = 0;
$varMax = 0;
$threshold = 0;
for ($i = 0; $i < 256; $i++) {
$wB += $histogram[$i];
if ($wB == 0) continue;
$wF = $total_pixels - $wB;
if ($wF == 0) break;
$sumB += $i * $histogram[$i];
$mB = $sumB / $wB;
$mF = ($sum - $sumB) / $wF;
$varBetween = $wB * $wF * ($mB - $mF) * ($mB - $mF);
if ($varBetween > $varMax) {
$varMax = $varBetween;
$threshold = $i;
}
}
// Apply threshold
for ($y = 0; $y < $new_height; $y++) {
for ($x = 0; $x < $new_width; $x++) {
$rgb = imagecolorat($resized_image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$color = ($r >= $threshold) ? 255 : 0;
$new_color = imagecolorallocate($resized_image, $color, $color, $color);
imagesetpixel($resized_image, $x, $y, $new_color);
}
}
}
// Apply dilation (1x1 kernel) if enabled
if ($apply_dilation) {
$dilated_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($dilated_image, $resized_image, 0, 0, 0, 0, $new_width, $new_height);
for ($y = 1; $y < $new_height - 1; $y++) {
for ($x = 1; $x < $new_width - 1; $x++) {
$is_black = false;
// Check 1x1 neighborhood
for ($ky = -1; $ky <= 1; $ky++) {
for ($kx = -1; $kx <= 1; $kx++) {
$rgb = imagecolorat($resized_image, $x + $kx, $y + $ky);
$r = ($rgb >> 16) & 0xFF;
if ($r == 0) {
$is_black = true;
break 2;
}
}
}
if ($is_black) {
$black = imagecolorallocate($dilated_image, 0, 0, 0);
imagesetpixel($dilated_image, $x, $y, $black);
}
}
}
} else {
// If dilation is not applied, use the resized image directly
$dilated_image = $resized_image;
}
// Save as PNG
$success = imagepng($dilated_image, $temp_path, 9); // Compression level 9
// Clean up
imagedestroy($image);
imagedestroy($resized_image);
if ($apply_dilation) {
imagedestroy($dilated_image);
}
return $success ? $temp_path : false;
}
/**
* Extract text from various document formats
*
* @param string $file_path Path to the file
* @param string $mime_type MIME type of the file
* @return string|false Extracted text or false on error
*/
function extractTextFromDocument($file_path, $mime_type) {
// Try specific tools based on file type
$text = false;
switch ($mime_type) {
case 'application/msword': // .doc
if (file_exists('/usr/bin/antiword')) {
$text = shell_exec('antiword ' . escapeshellarg($file_path) . ' 2>&1');
} elseif (file_exists('/usr/bin/catdoc')) {
$text = shell_exec('catdoc ' . escapeshellarg($file_path) . ' 2>&1');
}
break;
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': // .docx
if (file_exists('/usr/bin/docx2txt')) {
$text = shell_exec('docx2txt ' . escapeshellarg($file_path) . ' 2>&1');
} elseif (file_exists('/usr/bin/catdoc')) {
$text = shell_exec('catdoc ' . escapeshellarg($file_path) . ' 2>&1');
}
break;
case 'application/pdf': // .pdf
if (file_exists('/usr/bin/pdftotext')) {
$text = shell_exec('pdftotext ' . escapeshellarg($file_path) . ' - 2>&1');
}
break;
case 'application/vnd.oasis.opendocument.text': // .odt
if (file_exists('/usr/bin/odt2txt')) {
$text = shell_exec('odt2txt ' . escapeshellarg($file_path) . ' 2>&1');
}
break;
case 'text/plain': // .txt
case 'text/markdown': // .md
$text = file_get_contents($file_path);
break;
}
// Fallback to pandoc if specific tools failed
if (empty($text) && file_exists('/usr/bin/pandoc')) {
$text = shell_exec('pandoc -t plain ' . escapeshellarg($file_path) . ' 2>&1');
}
// Clean up the extracted text
if ($text !== false) {
$text = trim($text);
// Remove error messages from stderr
$text = preg_replace('/^.*error.*$/im', '', $text);
$text = preg_replace('/^.*warning.*$/im', '', $text);
$text = preg_replace('/^.*not found.*$/im', '', $text);
$text = trim($text);
}
return $text;
}
/**
* Extract images from PDF file
*
* @param string $pdf_path Path to the PDF file
* @return array|false Array of image data or false on error
*/
function extractImagesFromPDF($pdf_path) {
// Try Imagick first
if (extension_loaded('imagick')) {
try {
$images = [];
$imagick = new Imagick();
$imagick->readImage($pdf_path);
// Set resolution for better quality
$imagick->setResolution(200, 200);
// Get number of pages
$page_count = $imagick->getNumberImages();
if ($page_count === 0) {
return false;
}
// Process first page only for OCR
$imagick->setIteratorIndex(0);
$page = $imagick->getImage();
// Convert to PNG format
$page->setImageFormat('png');
$page->stripImage(); // Remove metadata
// Get image data
$image_data = $page->getImageBlob();
$images[] = $image_data;
// Clean up
$page->destroy();
$imagick->destroy();
return $images;
} catch (Exception $e) {
// Fall through to try Gmagick
}
}
// Try Gmagick as fallback
if (extension_loaded('gmagick')) {
try {
$images = [];
$gmagick = new Gmagick();
$gmagick->readImage($pdf_path);
// Set resolution for better quality
$gmagick->setresolution(200, 200);
// Get number of pages
$page_count = $gmagick->getnumberimages();
if ($page_count === 0) {
return false;
}
// Process first page only for OCR
$gmagick->setimageindex(0);
$page = clone $gmagick;
// Convert to PNG format
$page->setimageformat('png');
// Get image data
$image_data = $page->getimageblob();
$images[] = $image_data;
// Clean up
$page->clear();
$gmagick->clear();
return $images;
} catch (Exception $e) {
return false;
}
}
// If neither extension is available
return false;
}
/**
* Get human-readable explanation for HTTP error codes
*
* @param int $http_code HTTP status code
* @return string Explanation of the error
*/
function getHttpErrorExplanation($http_code) {
$explanations = [
400 => 'Bad Request - The request was invalid or cannot be served.',
401 => 'Unauthorized - Authentication is required and has failed or not yet been provided.',
403 => 'Forbidden - The server understood the request but refuses to authorize it.',
404 => 'Not Found - The requested resource could not be found.',
408 => 'Request Timeout - The server timed out waiting for the request.',
429 => 'Too Many Requests - You have sent too many requests in a given amount of time.',
500 => 'Internal Server Error - The server encountered an unexpected condition.',
502 => 'Bad Gateway - The server received an invalid response from the upstream server.',
503 => 'Service Unavailable - The server is not ready to handle the request.',
504 => 'Gateway Timeout - The server did not receive a timely response from the upstream server.'
];
return isset($explanations[$http_code]) ? $explanations[$http_code] : "HTTP error $http_code";
}
/**
* Fetch available models from the LLM server API
*
* @param string $api_endpoint The API endpoint URL
* @param string $api_key The API key (if required)
* @param string $filter_regex Regular expression to filter models (optional)
* @return array List of available models
*/
function getAvailableModels($api_endpoint, $api_key = '', $filter_regex = '') {
$models_url = $api_endpoint . '/models';
// Make API request
$ch = curl_init($models_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
$error = 'Connection error: ' . curl_error($ch);
curl_close($ch);
return ['error' => $error];
} elseif ($http_code !== 200) {
$error = 'API error: ' . getHttpErrorExplanation($http_code);
curl_close($ch);
return ['error' => $error];
}
curl_close($ch);
$response_data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($response_data['data'])) {
return ['error' => 'Invalid API response format: ' . json_last_error_msg()];
}
$models = [];
foreach ($response_data['data'] as $model) {
if (isset($model['id'])) {
// Apply filter if provided
if ($filter_regex !== '' && !preg_match($filter_regex, $model['id'])) {
continue;
}
// For vision models, we'll use a more user-friendly name
$name = $model['id'];
if (strpos($name, 'vision') !== false || strpos($name, 'vl') !== false) {
$models[$name] = ucfirst(str_replace(':', ' ', $name)) . ' (Vision)';
} else {
$models[$name] = ucfirst(str_replace(':', ' ', $name));
}
}
}
// Sort models alphabetically by key (model name)
ksort($models);
return $models;
}
/**
* Make API call to LLM server
*
* @param string $api_endpoint_chat The chat API endpoint URL
* @param array $data The request data
* @param string $api_key The API key (if required)
* @return array|false API response data or false on error
*/
function callLLMApi($api_endpoint_chat, $data, $api_key = '') {
// Make API request
$ch = curl_init($api_endpoint_chat);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
$error = 'Connection error: ' . curl_error($ch);
curl_close($ch);
return ['error' => $error];
} elseif ($http_code !== 200) {
$error = 'API error: ' . getHttpErrorExplanation($http_code);
curl_close($ch);
return ['error' => $error];
}
curl_close($ch);
$response_data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['error' => 'Invalid API response format: ' . json_last_error_msg()];
}
return $response_data;
}
/**
* Handle common request processing for AI applications
*
* @param string $input The input data (report, content, etc.)
* @param int $max_length Maximum allowed length
* @return array Processing result with validation status
*/
function processInput($input, $max_length = 10000) {
$result = [
'valid' => true,
'error' => null,
'data' => null
];
// Sanitize and validate input
$data = trim($input);
// Validate length
if (strlen($data) > $max_length) {
$result['valid'] = false;
$result['error'] = 'The input is too long. Maximum ' . $max_length . ' characters allowed.';
}
// Validate is not empty after trimming
elseif (empty($data)) {
$result['valid'] = false;
$result['error'] = 'The input cannot be empty.';
} else {
$result['data'] = $data;
}
return $result;
}
/**
* Handle common URL validation and processing
*
* @param string $url The URL to validate
* @return array Processing result with validation status
*/
function processUrl($url) {
$result = [
'valid' => true,
'error' => null,
'data' => null
];
// Sanitize and validate input
$data = trim($url);
// Validate URL format
if (!filter_var($data, FILTER_VALIDATE_URL)) {
$result['valid'] = false;
$result['error'] = 'Invalid URL format. Please enter a valid URL including http:// or https://';
} else {
$result['data'] = $data;
}
return $result;
}
/**
* Set common cookies for AI applications
*
* @param array $cookies Cookie data to set
* @param int $expire_time Cookie expiration time
*/
function setCommonCookies($cookies, $expire_time = 2592000) { // 30 days default
foreach ($cookies as $name => $value) {
setcookie($name, $value, time() + $expire_time, '/');
}
}
/**
* Send JSON response and exit
*
* @param array $data Response data
* @param bool $is_api_request Whether this is an API request
*/
function sendJsonResponse($data, $is_api_request = false) {
if ($is_api_request) {
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
}
/**
* Extract JSON from AI response content
*
* @param string $content AI response content
* @return array|null Extracted JSON data or null if not found
*/
function extractJsonFromResponse($content) {
// Try to find JSON between code fences
if (preg_match('/```(?:json)?\s*({.*?})\s*```/s', $content, $matches)) {
$json_str = $matches[1];
}
// Then try to find any JSON object
elseif (preg_match('/\{.*\}/s', $content, $matches)) {
$json_str = $matches[0];
} else {
return null;
}
// Clean up the JSON string
$json_str = trim($json_str);
// Try to decode JSON
$result = json_decode($json_str, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// Try to fix common JSON issues
$json_str = preg_replace('/,\s*([\]}])/m', '$1', $json_str); // Remove trailing commas
$json_str = preg_replace('/([{,])\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:/', '$1"$2":', $json_str); // Add quotes to keys
$json_str = preg_replace('/:\s*\'([^\']*)\'/', ':"$1"', $json_str); // Replace single quotes with double quotes
$json_str = preg_replace('/\s+/', ' ', $json_str); // Normalize whitespace
$result = json_decode($json_str, true);
}
return $result;
}
/**
* Convert array to YAML format
*
* @param array $array Array to convert
* @return string YAML formatted string
*/
function yaml_encode($array) {
$yaml = '';
$indent = 0;
$process = function($data, $indent) use (&$process, &$yaml) {
$spaces = str_repeat(' ', $indent);
if (is_array($data)) {
if (array_keys($data) === range(0, count($data) - 1)) {
// Sequential array
foreach ($data as $value) {
$yaml .= $spaces . "- ";
$process($value, $indent + 1);
}
} else {
// Associative array
foreach ($data as $key => $value) {
$yaml .= $spaces . $key . ": ";
$process($value, $indent + 1);
}
}
} else {
$yaml .= (is_string($data) ? '"' . $data . '"' : $data) . "\n";
}
};
$process($array, $indent);
return $yaml;
}
/**
* Check if config.php is available and show configuration instructions if needed
*
* @return string HTML message about configuration status
*/
function checkConfigStatus() {
if (file_exists('config.php')) {
return '';
} else {
$message = '<div class="error">';
$message .= '<strong>⚠️ Configuration file not found.</strong> Please create config.php with your settings.';
$message .= '<div class="config-instructions">';
$message .= '<p>Copy config.php.example to config.php and edit it with your API settings:</p>';
$message .= '<pre>cp config.php.example config.php</pre>';
$message .= '<p>Then edit config.php to set your LLM API endpoint and other options.</p>';
$message .= '</div>';
$message .= '</div>';
return $message;
}
}
function removeMarkdownFence(string $text): string
{
// Remove opening fence (``` or ```lang)
$text = preg_replace('/^```[a-zA-Z0-9_-]*\s*/', '', $text);
// Remove closing fence
$text = preg_replace('/\s*```$/', '', $text);
return trim($text);
}
/**
* Convert basic markdown to HTML
*
* @param string $markdown Markdown text to convert
* @return string HTML output
*/
function markdownToHtml($markdown) {
// Remove markdown code fences if present
$markdown = preg_replace('/^```(?:markdown)?\s*(.*?)\s*```$/s', '$1', $markdown);
// Normalize line endings
$markdown = str_replace(["\r\n", "\r"], "\n", $markdown);
// Escape HTML entities first
$markdown = htmlspecialchars($markdown, ENT_QUOTES, 'UTF-8');
// Split into lines for processing
$lines = explode("\n", $markdown);
$html = [];
$inCodeBlock = false;
$inList = false;
$listType = '';
for ($i = 0; $i < count($lines); $i++) {
$line = $lines[$i];
$trimmed = trim($line);
// Code blocks (```)
if (preg_match('/^```/', $trimmed)) {
if ($inCodeBlock) {
$html[] = '</code></pre>';
$inCodeBlock = false;
} else {
if ($inList) {
$html[] = $listType === 'ul' ? '</ul>' : '</ol>';
$inList = false;
}
$html[] = '<pre><code>';
$inCodeBlock = true;
}
continue;
}
if ($inCodeBlock) {
$html[] = $line;
continue;
}
// Empty lines
if ($trimmed === '') {
if ($inList) {
$html[] = $listType === 'ul' ? '</ul>' : '</ol>';
$inList = false;
}
continue;
}