-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.php
212 lines (183 loc) · 5.84 KB
/
lib.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
<?php
/**
* corta texto
* @param string $text
* @param int $nWords
* @param string $type f or r f=forward and r=rewind
* @return string
*/
function cutText($text, $nWords=50, $type='f') {
$array = explode(' ', $text);
$total = count($array);
$newArray = [];
if ($type == 'f') {
$count = 0;
foreach ($array as $value) {
$newArray[] = $value;
if ($count++ >= $nWords) {
if ($count <= $total) {
$newArray[] = ' ...';
}
break;
}
}
}
elseif ($type == 'r') {
$array = array_reverse($array);
$count = 0;
foreach ($array as $value) {
$newArray[] = $value;
if ($count++ >= $nWords) {
if ($count <= $total) {
$newArray[] = ' ...';
}
break;
}
}
$newArray = array_reverse($newArray);
}
return implode(' ', $newArray);
}
/**
* retorna os termos da pesquisa em Array
* @param string $searchString
* @return array $termsArray
*/
function extractSearchTerms($searchString='') {
// tres ou mais letras, até duas serao ignoradas nos termos de pesquisa, exceto uma frase entre aspas
$stopWordsArray = ['das', 'dos', 'que'];
$matches = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $searchString, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$termsArray = [];
if (count(explode(' ', $searchString))> 1) {
$termsArray[] = trim(preg_replace("/[^\p{L}\p{N} \-]+/u", '', $searchString));
}
foreach ($matches as $value) {
$value = trim(preg_replace("/[^\p{L}\p{N} \-]+/u", '', $value));
// algumas palavras serão ignorados
if (strlen($value) >= 3 && !in_array($value, $stopWordsArray) && !in_array($value, $termsArray)) {
$termsArray[] = $value;
}
}
return $termsArray;
}
/**
* retorna o trecho do texto de acordo com o termos
* @param array $termsArray
* @param string $text
* @return array $resultsArray
*/
function resultByTerms($termsArray, $text) {
$resultsArray = [];
foreach ($termsArray as $termo) {
$termo = removeAccents($termo);
$termo = preg_quote($termo);
$textNoAccent = removeAccents($text);
$resultArray = [];
if (preg_match_all("/(\b{$termo}\b)/ui", $textNoAccent, $matches, PREG_OFFSET_CAPTURE)) {
foreach ($matches[0] as $key => $value) {
$resultsArray[] = cutText(mb_substr(mb_substr($text, 0, $value[1]), -1000), 100, 'r') . cutText(mb_substr($text, $value[1], 1000), 100);
}
}
}
return $resultsArray;
}
/**
* converte o texto, remove os acentos das palavras
* @param string $text
* @return array $resultsArray
*/
function removeAccents($text) {
$lwac = ['á','é','í','ó','ú','â','ê','î','ô','û','ã','õ','à','ç','Á','É','Í','Ó','Ú','Â','Ê','Î','Ô','Û','Ã','Õ','À','Ç'];
$lnac = ['a','e','i','o','u','a','e','i','o','u','a','o','a','c','A','E','I','O','U','A','E','I','O','U','A','O','A','C'];
return str_replace($lwac, $lnac, $text);
}
/**
* verifica se está logado
*/
function auth() {
if (!$_SESSION["autenticado"]) {
header("location: login.php");
exit();
}
}
/**
* remove caracteres indesejados
* @param string $text
* @return string
*/
function filterHash($text) {
return preg_replace('/[^A-Za-z0-9]/', '', $text);
}
/**
* remove caracteres indesejados
* @param string $text
* @return string
*/
function filterUploadedFileName($text) {
$text = removeAccents($text);
$text = preg_replace('/[^A-Za-z0-9 \-_\.]/', '', $text);
$text = strtolower(substr($text, -150));
return $text;
}
/**
* remove arquivos temporarios
* @param string $dir
* @param array $listType lista de tipos que serao removidos
* @param int $time segundos de idade que foi criado
*/
function removeOldFiles($dir='./tmp/', $listType=['.pdf'], $time=86400) {
$listType = array_map('strtolower', $listType);
foreach (glob($dir."*") as $file) {
if (time() - filectime($file) > $time && in_array(strtolower(substr($file, -4)), $listType)) {
@unlink($file);
}
}
}
/**
* converte o arquivo pdf para o texto formatado sem os caracteres indesejados
* @param string $arquivo
* @param string $path
* @return string $text
*/
function pdfToFullTextContent($arquivo, $path) {
$text = '';
if (stripos(PHP_OS, 'linux') === 0 && ENABLE_PDFTOTEXT == 1) {
// code for Linux
shell_exec("echo '' > '{$path}output.txt'");
shell_exec("/usr/bin/pdftotext -layout -enc Latin1 '{$path}{$arquivo}' '{$path}output.txt'");
$text = file_get_contents($path . 'output.txt');
$text = trim($text);
}
if (stripos(PHP_OS, 'win') === 0 || stripos(PHP_OS, 'darwin') === 0 || !$text) {
// code for windows || code for OS X
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile($path . $arquivo);
$text = $pdf->getText();
}
$text = \ForceUTF8\Encoding::toUTF8($text);
$text = preg_replace("/[^\p{L}\p{N} \:\-\.\,\?\=\(\)\+\*\&\%\$\#\@\!\º\_]+/u", ' ', $text);
// remove multiple spaces
$text = preg_replace('!\s+!', ' ', $text);
return $text;
}
/**
* apaga todo o conteudo indexado nesse site (arquivos pdf e os dados do Banco de Dados)
*/
function removeAllContent() {
global $db;
$sqlStr = [
"SET FOREIGN_KEY_CHECKS = 0",
"TRUNCATE `disciplina`",
"TRUNCATE `conteudo`",
"SET FOREIGN_KEY_CHECKS = 1",
];
try {
foreach ($sqlStr as $value) {
$result = $db->rawQuery($value);
}
} catch (Exception $e) {
return 0;
}
return 1;
}