-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.php
134 lines (101 loc) · 3.34 KB
/
process.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
<?php
include_once "mysql.php";
include_once "common.php";
include_once "check.php";
function setV($var, $enforce) {
global $conn;
if (!isset($_POST[$var])) { echo json_encode(array("code"=>1)); die(); }
$temp = $conn->real_escape_string($_POST[$var]);
if ($temp == "") { echo json_encode(array("code"=>1)); die(); }
if ($enforce) {
if ($var == "sch") $var = "school"; //Inconsistencies in database ><
$q = $conn->query("SELECT type FROM meta WHERE id = '$temp'");
if ($q->num_rows == 0) { echo json_encode(array("code"=>5)); die(); }
$r = $q->fetch_assoc();
if ($r['type'] != $var) { echo json_encode(array("code"=>5)); die(); }
}
return $temp;
}
$u = $conn->real_escape_string($_SESSION['user']->userid);
$title = setV("title", false);
$level = setV("level", true);
$sch = setV("sch", true);
$subj = setV("subj", true);
if (isset($_POST['pass'])) {
$pass = sha1($conn->real_escape_string($_POST['pass']));
}
else {
$pass = "";
}
if (!isset($_POST['notes'])) { echo json_encode(array("code"=>4)); die(); }
$f = $_POST['notes'];
$file = new File($f, $conn);
if ($file->error == 4) {
echo json_encode(array("code"=>4)); die();
}
if ($file->error == 5) {
echo json_encode(array("code"=>5)); die();
}
/*
if (!isset($_POST['subj'])) return 1;
$subj = $_POST['subj'];
foreach($subj as $a) {
$a = $conn->real_escape_string($a);
}
if (count($subj) == 0) return 1;
*/
$filename = "uploads/" . time() . $file->originalname;
if (!move_uploaded_file($file->servername, $filename)) {
{ echo json_encode(array("code"=>2)); die(); }
}
$hash = hash_file("md5", $filename);
$r = $conn->query("SELECT id FROM notes WHERE hash = '$hash' AND password=''")->num_rows;
if ($r != 0) {
{ echo json_encode(array("code"=>3, "file"=>"$r")); die(); }
}
//Credits
//MS Doc + slsx for catdoc
// http://www.wagner.pp.ru/~vitus/software/catdoc/
// http://stackoverflow.com/questions/5671988/how-to-extract-just-plain-text-from-doc-docx-files-unix
//PDF
// https://gist.github.com/smalot/6183152
// pdftotext
$str = "";
if ($file->getType() == "doc") {
$str = exec("catdoc '". escapeshellcmd($filename) . "'");
}
if ($file->getType() == "docx") {
$str = exec("unzip -p '" . escapeshellcmd($filename) . "' word/document.xml | sed -e 's/<\/w:p>/\n/g; s/<[^>]\{1,\}>//g; s/[^[:print:]\n]\{1,\}//g'");
}
if ($file->getType() == "pdf") {
include "pdfparser.php";
$parser = new PdfParser();
$str = $parser->parseFile($filename);
$im = new imagick($filename);
$im->setImageFormat('jpg');
$imdata = base64_encode($im);
}
$textarr1 = preg_split("/\s+/", $str);
$textarr = [];
foreach ($textarr1 as $b) {
if (!in_array($b, $textarr)) {
$textarr[$b] = 1;
}
else {
$textarr[$b]+=1;
}
}
$a = $file->originalname;
$b = $filename;
$c = $file->filesize;
$d = $conn->real_ecape_string($file->getType());
if (!isset($im)) $im = "";
$conn->query("INSERT INTO notes VALUES (NULL, '$a', '$b', $c, '$d', $u, 1,1,'$pass', '$hash', NULL, '$title', $level, $sch, $subj, '$im')");
$r = $conn->insert_id;
foreach($textarr as $key=>$value) {
$key = $conn->real_ecape_string($key);
$value = $conn->real_escape_string($value);
$conn->query("INSERT INTO indextable VALUES(NULL, $r, $value, '$key'");
}
echo json_encode(array("code"=>0, "file"=>"$r"));
?>