-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRichTextPlugin.php
More file actions
executable file
·307 lines (275 loc) · 10 KB
/
RichTextPlugin.php
File metadata and controls
executable file
·307 lines (275 loc) · 10 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* RichTextPlugin.php - A single-page HTML5 WYSIWYG editor for Stud.IP.
*
* This file contains the plugin's main class.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Robert Costa <zabbarob@gmail.com>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
require_once 'Purifier.php';
require_once 'Utils.php';
use RichTextPlugin\Purifier as Purifier;
use RichTextPlugin\Utils as Utils;
/**
* Initializes and displays the plugin.
**/
class RichTextPlugin extends StudIPPlugin implements StandardPlugin
{
protected $navlink = '/course/rich'; // plugin location in tab navigation bar
protected $assets; // URL of assets folder; set in __construct()
protected $edit_permission = 'autor'; // minimum permission level for editing
/**
* Constructor of the class.
*
* Configures the navigation link where the plugin can be reached in
* Stud.IP and does some general plugin initialization stuff.
*/
public function __construct() {
parent::__construct();
$this->template_factory = new Flexi_TemplateFactory($this->getPluginPath() . '/templates');
$this->assets = $this->getPluginURL() . '/assets/';
}
/**
* Loads stylesheets and scripts needed for executing the plugin.
*/
public function initialize () {
PageLayout::addStylesheet($this->assets . 'styles.css');
$this->addScript('script.js');
}
/**
* Implements abstract method of base class.
*/
public function getIconNavigation($course_id, $last_visit, $user_id) {
if (!$this->isActivated($course_id)) {
return;
}
$icon = new AutoNavigation(_('RichText'), PluginEngine::getLink($this, array(), "show"));
$icon->setImage($this->getIcon('grey'));
$icon->setTitle(_('RichText-Editor'));
return $icon;
}
/**
* Implements abstract method of base class.
*/
public function getInfoTemplate($course_id) {
}
/**
* Implements abstract method of base class.
*/
public function getTabNavigation($course_id) {
if (!$this->isActivated($course_id)) {
return;
}
$navigation = new AutoNavigation(_('RichText'));
$navigation->setURL(PluginEngine::GetLink($this, array(), 'show'));
Navigation::addItem($this->navlink, $navigation);
$this->setTabNavigationIcon('white');
}
/**
* Implements abstract method of base class.
*/
public function getNotificationObjects($course_id, $since, $user_id) {
}
public function test_action() {
Utils\testGetMediaUrl();
}
/**
* Sets the fields in the plugin's show.php template to correct values.
*/
public function show_action() {
if (Request::submitted('save')) {
CSRFProtection::verifyUnsafeRequest();
$this->setBody(Request::get('body'));
}
$this->actionHeader();
$this->renderBodyTemplate('show');
}
/**
* Initialize edit_ckeditor.php template for editing the page.
*/
public function edit_action() {
Utils\verifyPermission($this->edit_permission);
$this->actionHeader();
$this->addScript('jquery.autosize-min.js');
$this->addScript('vendor/jquery.ui.widget.js');
$this->addScript('jquery.iframe-transport.js');
$this->addScript('jquery.fileupload.js');
$this->addScript('ckeditor/ckeditor.js');
$this->renderBodyTemplate('edit');
}
public function benchmark_action() {
$db = DBManager::get();
$stmt = $db->prepare('SELECT body FROM wiki WHERE range_id like "8%"');
$stmt->execute();
$time_formatReady = 0;
$time_purifier = 0;
$count = 0;
$hist = Array(
100 => [0, 0, 0],
1000 => [0, 0, 0],
10000 => [0, 0, 0],
100000 => [0, 0, 0],
'greater' => [0, 0, 0]);
function p($count, $time_formatReady, $time_purifier, $hist) {
echo '<p>';
echo "count $count formatReady $time_formatReady purifier $time_purifier";
echo '<br>';
echo json_encode($hist);
echo '</p>';
}
while (($column = $stmt->fetchColumn()) !== FALSE) {
//echo "<p>$column</p>"; flush(); ob_flush();
$count += 1;
$start = microtime(TRUE);
$html = formatReady($column);
$formatReady = microtime(TRUE) - $start;
$start = microtime(TRUE);
$clean_html = Purifier\purify($html);
$purifier = microtime(TRUE) - $start;
$time_formatReady += $formatReady;
$time_purifier += $purifier;
$len = strlen($column);
$hist_len = &$hist['greater'];
if ($len < 100) {
$hist_len = &$hist[100];
} else if ($len < 1000) {
$hist_len = &$hist[1000];
} else if ($len < 10000) {
$hist_len = &$hist[10000];
} else if ($len < 100000) {
$hist_len = &$hist[100000];
}
$hist_len[0] += 1;
$hist_len[1] += $formatReady;
$hist_len[2] += $purifier;
if (($count % 10) == 0) {
p($count, $time_formatReady, $time_purifier, $hist);
flush();
ob_flush();
}
}
p($count, $time_formatReady, $time_purifier, $hist);
}
/**
* Opens, initializes and renders a template that gets the DB's text.
* @params string $file Template file name, ommitting path.
*/
protected function renderBodyTemplate($file) {
$template = $this->template_factory->open($file);
$template->set_layout($GLOBALS['template_factory']->open('layouts/base'));
$template->body = $this->getBody();
echo $template->render();
}
/**
* Handle file upload requests.
*/
public function post_file_action() {
Utils\verifyPostRequest();
$this->verifyUnsafeRequest();
// store uploaded files as StudIP documents
$response = array(); // data for HTTP response
$folder_id = Utils\getFolderId(
'RichText',
studip_utf8decode(_('Durch das RichText-Plugin hochgeladene Dateien.')));
foreach (Utils\FILES() as $file) {
try {
$newfile = Utils\uploadFile($file, $folder_id);
$response['files'][] = Array(
'name' => utf8_encode($newfile['filename']),
'type' => $file['type'],
'url' => GetDownloadLink($newfile->getId(), $newfile['filename']));
} catch (AccessDeniedException $e) { // creation of Stud.IP doc failed
$response['files'][] = Array(
'name' => $file['name'],
'type' => $file['type'],
'error' => $e->getMessage());
}
}
Utils\sendAsJson($response);
}
/**
* Retrieve text body from database.
* @return mixed Text from database or FALSE if there is no text.
*/
protected function getBody() {
$db = DBManager::get();
$stmt = $db->prepare('SELECT body FROM plugin_rich_text WHERE range_id = ?');
$stmt->execute(array(Utils\getSeminarId()));
return Purifier\purify($stmt->fetchColumn());
}
/**
* Store text body in database.
* @param string $body Text that is stored in the database.
*/
protected function setBody($body) {
$clean_body = Purifier\purify($body);
$db = DBManager::get();
$stmt = $db->prepare("REPLACE INTO plugin_rich_text VALUES(?, ?)");
$stmt->execute(array(Utils\getSeminarId(), $clean_body));
}
/**
* Point to a JavaScript file in HTML <head>.
* @param string $path Path to the file.
*/
protected function addScript($path) {
PageLayout::addHeadElement('script', array(
'src' => $this->assets . $path,
'charset' => 'utf-8'), '');
}
/**
* Set the plugin icon in Stud.IP's navigation bar.
* @param string $color Color in which the icon is displayed.
*/
protected function setTabNavigationIcon($color) {
$this->getTabNavigationItem()->setImage($this->getIcon($color));
}
/**
* Get the plugin's navigation bar item entry.
* @return object Navigation bar item.
*/
protected function getTabNavigationItem() {
return Navigation::getItem($this->navlink);
}
/**
* Get image path to the plugin icon.
* @param string $color The color of the icon.
* return string Image path to the icon.
*/
protected function getIcon($color) {
return Assets::image_path('icons/16/' . $color . '/forum.png');
}
/**
* Executes functions that have to be called by each action handler.
*/
protected function actionHeader() {
Navigation::activateItem($this->navlink);
$this->setTabNavigationIcon('black');
$this->addSubNavigation(_('Lesen'), 'show');
if (Utils\hasPermission($this->edit_permission)) {
$this->addSubNavigation(_('Bearbeiten'), 'edit');
}
}
/**
* Add the given item to the subnavigation of this object.
* @param string $title Title displayed to user.
* @param string $action Executed action when title is clicked.
*/
protected function addSubNavigation($title, $action) {
$this->getTabNavigationItem()->addSubNavigation($action, new Navigation(
$title, PluginEngine::getLink($this, array(), $action)));
}
/**
* Verify that user has edit permission and correct security token.
*/
public function verifyUnsafeRequest() {
Utils\verifyPermission($this->edit_permission);
CSRFProtection::verifyUnsafeRequest();
}
}