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
|
<?php
namespace app\controllers\admin;
use Registry;
use Render;
use app\models\cms\Course_model;
class Course_admin {
## -------------------------------------------------------------------------
##
## COURSE (category) METHODS
##
## -------------------------------------------------------------------------
/** breadcrumbs
* ---
* responds to ajax GET: /admin/api/categories
*/
public static function breadcrumbs()
{
$tree = proxy( // cache-get categories
[\app\models\cms\Course_model::class, 'category_tree'],
[], CACHE_ROOT_TTL
);
$breadcrumbs = proxy( // cache-get breadcrumbs
[\app\models\cms\Course_model::class, 'breadcrumbs'],
[$tree], CACHE_ROOT_TTL
);
Render::json($breadcrumbs); // send as json
}
/** add course
* ---
* @param void (get data from POST)
*/
public static function add_course()
{
// insert new category; id = new category id
$id = Registry::use('database')->query(
"INSERT INTO course (parent_id, label) VALUES (:parent, :label)",
[
':parent' => Registry::get('REQUEST')->POST['parent_id'],
':label' => Registry::get('REQUEST')->POST['label']
]
)->lastInsertID();
$cache = self::update_courses_cache(); // update category caches
return $cache['breadcrumbs']; // return
}
/** update course
* ---
* @param void (get data from POST)
*/
public static function update_course()
{
Registry::use('database')->query(
"UPDATE course SET parent_id = :parent, label = :label
WHERE id = :id",
[
':id' => Registry::get('REQUEST')->POST['id'],
':parent' => Registry::get('REQUEST')->POST['parent_id'],
':label' => Registry::get('REQUEST')->POST['label']
]
);
$cache = self::update_courses_cache();
return $cache['breadcrumbs'];
}
/** update courses cache
* --- -- -- - - -
* Shall run if anything changes to courses
*
* @param void
* @return (array): the two course caches
*/
public static function update_courses_cache()
{
$tree = proxy( // cache-get categories
[\app\models\cms\Course_model::class, 'category_tree'],
[], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE
);
$breadcrumbs = proxy( // cache-get breadcrumbs
[\app\models\cms\Course_model::class, 'breadcrumbs'],
[$tree], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE
);
return [
'tree' => $tree,
'breadcrumbs' => $breadcrumbs
];
}
## -------------------------------------------------------------------------
##
## FILE METHODS
##
## -------------------------------------------------------------------------
/** upload_file
* upload the file to the file system
*
* the method reads the POST and FILES array
* to retrieve all needed parametres
*
* + $_FILES[file]
* + $_POST[folder] : lesson's ID
* + $_POST[reference] : reference type
*/
public static function upload_file()
{
$post = Registry::get('REQUEST')->POST;
$files = Registry::get('REQUEST')->FILES;
// Checks before uploading the file
////////////////////////////////////////////////////////////////////////
// ** 1: file is upladed to temporary folder ---------------------------
if (! is_uploaded_file($files['file']['tmp_name'])) {
return ['success' => false]; // bye!
}
// ** 2: File belongs to the allowed MIME types ------------------------
$allowed_file_types = array(
'application/pdf',
'image/png', 'image/jpeg',
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
);
// Recomended MIME type checking via mime_content_type():
$mime_type = mime_content_type($files['file']['tmp_name']);
if (! in_array($mime_type, $allowed_file_types)) { // File type NOT allowed ...
return ['success' => false]; // bye!
}
$file_name = $files['file']['name'];
$file_type = $files['file']['type']; // do not take it for granted
$file_size = $files['file']['size'];
$file_tmp = $files['file']['tmp_name'];
$bare_name = pathinfo($file_name, PATHINFO_FILENAME);
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
// ** 3: filename or size checks may be added --------------------------
if ($file_name == "") {
return ['success' => false]; // bye!
}
// READY to finaly save/upload the file to CDN /////////////////////////
$folder = MEDIA_STORAGE_ROOT . $post['folder'];
if (!file_exists($folder)) { // create folder if not exists
mkdir($folder, 0757, true);
}
// print_r([
// 'dir' => $folder,
// 'file' => $bare_name,
// 'ext' => $file_ext,
// 'type' => $file_type
// ]); die();
$relative_filename = $post['folder']
.'/'
. strtolower(self::clear_file_name($bare_name) .'.'. $file_ext);
$store_filename = MEDIA_STORAGE_ROOT . $relative_filename;
if (move_uploaded_file($files["file"]["tmp_name"], $store_filename)) {
return [
'success' => true,
'path' => $store_filename,
'type' => $mime_type
];
} else { return ['success' => false]; }
}
/** clear_file_name
* replace greek characters and strip symbols
*/
private static function clear_file_name($str)
{
$el = mb_split( "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωάέήίόύώϊϋς ", "");
$en = str_split("ABGDEZHUIKLMNJOPRSTYFXCVabgdezhuiklmnjoprstyfxcvaehioyviys-");
$strip = str_split("!@#$%^&*()+~`[]{};'/<>?=\"");
return str_replace($strip, '', str_replace($el, $en, $str));
}
/** define_media
*
* create a record in media table
*
* @param $data
* @return id (int): id of created media record
*/
public static function define_media($data)
{
$sql = "INSERT INTO wiki_media (`title`, `type`, `path`)
VALUES (:title, :mimetype, :filepath)";
$stmt = $this->pdo->prepare($sql);
$stmt->execute([
'title' => $data['title'],
'mimetype' => $data['type'],
'filepath' => $data['path']
]);
$inserted_id = $this->pdo->lastInsertId();
return $inserted_id;
}
}
|