forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentService.php
152 lines (122 loc) · 4.42 KB
/
DocumentService.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
<?php
/**
* DocumentService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <[email protected]>
* @author Brady Miller <[email protected]>
* @copyright Copyright (c) 2018 Matthew Vita <[email protected]>
* @copyright Copyright (c) 2018 Brady Miller <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use Particle\Validator\Validator;
class DocumentService
{
public function __construct()
{
}
public function isValidPath($path)
{
$docPathParts = explode("/", $path);
unset($docPathParts[0]);
$categoriesSql = " SELECT parent, id";
$categoriesSql .= " FROM categories";
$categoriesSql .= " WHERE replace(LOWER(name), ' ', '') = ?";
$lastParent = null;
$isValidPath = true;
foreach ($docPathParts as $index => $part) {
$categoryResults = sqlQuery($categoriesSql, str_replace("_", "", $part));
if ($index === 1) {
$lastParent = $categoryResults["id"];
continue;
}
if ($categoryResults["parent"] === $lastParent) {
$lastParent = $categoryResults["id"];
} else {
$isValidPath = false;
break;
}
}
return $isValidPath;
}
public function getLastIdOfPath($path)
{
$docPathParts = explode("/", $path);
$lastInPath = end($docPathParts);
$sql = " SELECT id";
$sql .= " FROM categories";
$sql .= " WHERE replace(LOWER(name), ' ', '') = ?";
$results = sqlQuery($sql, str_replace("_", "", $lastInPath));
return $results['id'];
}
public function getAllAtPath($pid, $path)
{
if (!$this->isValidPath($path)) {
return false;
}
$categoryId = $this->getLastIdOfPath($path);
$documentsSql = " SELECT doc.url, doc.id, doc.mimetype, doc.docdate";
$documentsSql .= " FROM documents doc";
$documentsSql .= " JOIN categories_to_documents ctd on ctd.document_id = doc.id";
$documentsSql .= " WHERE ctd.category_id = ? and doc.foreign_id = ?";
$documentResults = sqlStatement($documentsSql, array($categoryId, $pid));
$fileResults = array();
while ($row = sqlFetchArray($documentResults)) {
array_push($fileResults, array(
"filename" => basename($row["url"]),
"id" => $row["id"],
"mimetype" => $row["mimetype"],
"docdate" => $row["docdate"]
));
}
return $fileResults;
}
public function insertAtPath($pid, $path, $fileData)
{
if (!$this->isValidPath($path)) {
return false;
}
$categoryId = $this->getLastIdOfPath($path);
$nextIdResult = sqlQuery("SELECT MAX(id)+1 as id FROM documents");
$insertDocSql = " INSERT INTO documents SET";
$insertDocSql .= " id=?,";
$insertDocSql .= " type='file_url',";
$insertDocSql .= " size=?,";
$insertDocSql .= " date=NOW(),";
$insertDocSql .= " url=?,";
$insertDocSql .= " mimetype=?,";
$insertDocSql .= " foreign_id=?";
sqlInsert(
$insertDocSql,
array(
$nextIdResult["id"],
$fileData["size"],
$GLOBALS['oer_config']['documents']['repository'] . $categoryId . "/" . $fileData["name"],
$fileData["type"],
$pid
)
);
$cateToDocsSql = " INSERT INTO categories_to_documents SET";
$cateToDocsSql .= " category_id=?,";
$cateToDocsSql .= " document_id=?";
sqlInsert(
$cateToDocsSql,
array(
$categoryId,
$nextIdResult["id"]
)
);
$newPath = $GLOBALS['oer_config']['documents']['repository'] . "/" . $categoryId;
if (!file_exists($newPath)) {
mkdir($newPath, 0700, true);
}
$moved = move_uploaded_file($fileData["tmp_name"], $newPath . "/" . $fileData["name"]);
return $moved;
}
public function getFile($pid, $did)
{
return sqlQuery("SELECT url FROM documents WHERE id = ? AND foreign_id = ?", array($did, $pid));
}
}