-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
217 lines (172 loc) · 5.25 KB
/
Copy pathlib.php
File metadata and controls
217 lines (172 loc) · 5.25 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
<?php
include_once("./config.php");
function fnGenerateFilterRules($aFilterRules)
{
$sSQL = "";
foreach ($aFilterRules as $aRule) {
$aRule = (array) $aRule;
if ($aRule["op"] == "contains") {
$sSQL .= " {$aRule["field"]} LIKE '%{$aRule["value"]}%' ";
}
}
return $sSQL;
}
function fnPagination($iPage, $iRows)
{
$iF = ($iPage-1)*$iRows;
return " LIMIT {$iF}, {$iRows}";
}
function fnFindImagesURLs($sContent)
{
$aResult = [];
if (preg_match_all("@<img[^>]*?src\s*=\s*[\"']([^>]*?)[\"'][^>]*?>@", $sContent, $aM)) {
$aResult = array_merge($aResult, $aM[1]);
}
if (preg_match_all("@!\\[[^\\]]*?\\]\\(([^\\)]*?)\\)@", $sContent, $aM)) {
$aResult = array_merge($aResult, $aM[1]);
}
return $aResult;
}
function fnDownloadFileFromURL($sURL, $sFilePath)
{
if (ini_get('allow_url_fopen')) {
copy($sURL, $sFilePath);
} else {
//This is the file where we save the information
$fp = fopen ($sFilePath, 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
// make sure to set timeout to a high enough value
// if this is too low the download will be interrupted
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
function fnUploadImages($aImages)
{
$aResult = [];
foreach ($aImages as $sURL) {
$sFileName = basename($sURL);
preg_match("/\\.(\w+)$/", $sFileName, $aM);
$sExt = $aM[1];
$oFile = R::dispense(T_IMAGES);
$oFile->created_at = date("Y-m-d H:i:s");
$oFile->updated_at = date("Y-m-d H:i:s");
$oFile->timestamp = time();
$oFile->name = $sFileName;
$oFile->type = $sExt;
$oFile->filename = $oFile->timestamp.".".$sExt;
R::store($oFile);
$sFilePath = P_FIP."/".$oFile->filename;
$sRelFilePath = P_BIP."/".$oFile->filename;
$aResult[$sURL] = $sRelFilePath;
fnDownloadFileFromURL($sURL, $sFilePath);
}
return $aResult ;
}
function fnUploadFromContent(&$sContent)
{
$aImages = fnFindImagesURLs($sContent);
$aImages = fnUploadImages($aImages);
foreach ($aImages as $sURL => $sPath) {
$sContent = str_replace($sURL, $sPath, $sContent);
}
}
function fnZipDataFolder()
{
$sFilePath = "/tmp/{$_SERVER['SERVER_NAME']}_".PROJECT."_".time().".zip";
fnZipFolder(PROJECT_PATH, $sFilePath);
return $sFilePath;
}
function fnZipFolder($sFolder, $sFilePath)
{
// Get real path for our folder
$rootPath = realpath($sFolder);
// Initialize archive object
$zip = new ZipArchive();
$zip->open($sFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
}
function fnBuildRecursiveCategoriesList(&$aResult, $aCategories)
{
$aResult = [];
foreach ($aCategories as $oCategory) {
$aTreeChildren = [];
$aChildren = R::findAll(T_CATEGORIES, " tcategories_id = {$oCategory->id}");
fnBuildRecursiveCategoriesList($aTreeChildren, $aChildren);
$aNotes = R::findAll(T_NOTES, " tcategories_id = {$oCategory->id}");
$aResult[] = [
'id' => $oCategory->id,
'name' => $oCategory->name,
'children' => $aTreeChildren,
'children_notes' => $aNotes,
];
}
}
function fnGetSelectedProjectPath()
{
$sDir = fnGetSelectedDatabase();
return DATA_PATH."{$sDir}/";
}
function fnPreparePath($sPath)
{
$sDir = fnGetSelectedProjectPath();
return "{$sDir}{$sPath}";
}
function fnGetDatabasePath()
{
$sDir = fnGetSelectedProjectPath();
return "{$sDir}db/dbfile.db";
}
function fnGetDatabasesList()
{
$aProjects = glob(DATA_PATH."*");
return $aProjects;
}
function fnGetDatabasesListForDatalist()
{
$aList = fnGetDatabasesList();
$aList = array_map(function ($aI) { return [ "text" => $aI ]; }, $aList);
return $aList;
}
function fnSelectDatabase($sDatabaseName)
{
$_SESSION["database"] = $sDatabaseName;
}
function fnGetSelectedDatabase()
{
return $_SESSION["database"] ?: "default";
}
function fnConnectDatabase()
{
if (Config::$aOptions["database"]["schema"] == "sqlite") {
$sPath = fnGetDatabasePath();
R::setup("sqlite:{$sPath}");
}
if(!R::testConnection()) die('No DB connection!');
}