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
namespace app\controllers\cms;
use Registry;
use Render;
use app\controllers\Auth;
use app\models\cms\Course_model;
use app\extends\Cache_service;
class Course {
/** PERMITION
* -------------------------------------------------------------------------
*/
/** is admin or permited
*
* shortcut method for checking access authorization
*
* returns true if user belogns to the admin group
* OR has the specified permition/privilege
*
* @param $privilege_id (int)
*
* NOTE:
* unlike the other authorization methods ...
* $privilege_id is NOT an array but a single privilege id
*
* @return true|false
*/
private static function is_admin_or_permited( $privilege_id )
{
return (
Auth::in_admin_group()
|| Auth::hasPermition([ $privilege_id ])
);
}
/** FILES
* -------------------------------------------------------------------------
*/
/** serve file by file_path
* (request is valid only for admin users)
*
* @param $file_path (string): relative file path
* GET @param type (string) : media-type of file
*/
public static function serve_file($file_path)
{
$file = self::get_file_attributes($file_path);
// if user is authorized
if (self::is_admin_or_permited($file['privilege_id'])) {
$media_type = Registry::get('REQUEST')->GET['type']; // get media-type
$real_path = MEDIA_STORAGE_ROOT . $file_path; // construct real path
if (!file_exists($real_path)) {
Render::view('error/404');
} else {
Render::file($real_path, $media_type);
}
}
}
/** get_file_attributes
*
* returns attributes of a file
* (medias are proxied for speed optimization)
*
* @param $path (string) : file path
* @return $file attributes --or-- false
*/
private static function get_file_attributes($path)
{
$medias = Cache_service::files_attributes();
foreach($medias as $key => $medi) {
if ($medi['path'] == $path) {
return $medi;
}
}
return false;
}
/** COURSES
* -------------------------------------------------------------------------
*/
/** course
* prepare and render the course view
*
* @param $id : course id
*/
public static function course($id)
{
$id = intval($id); // course id
// collect all data needed for course view
// --- -- -- - - -
$cache = Cache_service::courses_struct();
$tree = $cache['tree'];
$breadcrumbs = $cache['breadcrumbs'];
$lessons = Course_model::lessons_of_course(intval($id));
// find parent_ids of current category (to open the menu tree)
// ---
$course_path = [];
foreach($breadcrumbs[$id]['parents'] as $key => $parent) {
$course_path[] = intval($parent['id']);
}
$course_path[] = $id;
// then Render
// --- -- -- - - -
Render::view('templates/course', [
'id' => $id,
'title' => $breadcrumbs[ $id ]['rec']['label'],
'categories' => $tree,
'breadcrumbs' => $breadcrumbs,
'lessons' => $lessons,
'course_path' => $course_path
]);
}
/** LESSONS
* ------------------------------------------------------------------------
*/
/** lesson
*
* check if user is authorized to view the content;
* if so, prepare and render the lesson view
*
* @param $id : lesson id
*/
public static function lesson($id)
{
$id = intval($id); // lesson id
$lesson = Course_model::lesson($id); // get lesson
$course_id = $lesson['course_id']; // get course id
$pri_id = $lesson['privilege_id']; // privilege needed
if (self::is_admin_or_permited($pri_id)) {
// collect all data needed for course view
// --- -- -- - - -
$cache = Cache_service::courses_struct();
$tree = $cache['tree'];
$breadcrumbs = $cache['breadcrumbs'];
// find parent_ids of current category (to open the menu tree)
// ---
$course_path = [];
foreach($breadcrumbs[$course_id]['parents'] as $key => $parent) {
$course_path[] = intval($parent['id']);
}
$course_path[] = intval($course_id);
// then Render
// --- -- -- - - -
Render::view('templates/lesson', [
// main content
'id' => $id,
'course_id' => $course_id,
'title' => $lesson['title'],
'lesson' => $lesson,
// needed for side panel and breadcrunbs
'categories' => $tree,
'breadcrumbs' => $breadcrumbs,
'course_path' => $course_path
]);
} else { // user is not authorized
Render::view('error/general', [
'title' => 'Δεν έχετε πρόσβαση',
'message' => 'Θα πρέπει να αγοράσετε το πακέτο πρόσβασης '
. $pri_id
.' για να δείτε το περιεχόμενο!
<br><br>
Αγόρασε τώρα το <button class="btn">πακέτο πρόσβασης '. $pri_id .'</button>'
]);
}
}
}
|