-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathextension.driver.php
577 lines (451 loc) · 17.3 KB
/
extension.driver.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
<?php
require_once(TOOLKIT . '/class.datasourcemanager.php');
require_once(TOOLKIT . '/class.entrymanager.php');
require_once(TOOLKIT . '/class.eventmanager.php');
require_once(TOOLKIT . '/class.pagemanager.php');
Class Extension_Dashboard extends Extension{
public function install() {
return Symphony::Database()->query("CREATE TABLE `tbl_dashboard_panels` (
`id` int(11) NOT NULL auto_increment,
`label` varchar(255) default NULL,
`type` varchar(255) default NULL,
`config` text,
`placement` varchar(255) default NULL,
`sort_order` int(11) default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM");
}
public function uninstall() {
return Symphony::Database()->query("DROP TABLE `tbl_dashboard_panels`");
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/backend/',
'delegate' => 'InitaliseAdminPageHead',
'callback' => 'append_assets'
),
array(
'page' => '/backend/',
'delegate' => 'AdminPagePreGenerate',
'callback' => 'page_pre_generate'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelRender',
'callback' => 'render_panel'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelOptions',
'callback' => 'dashboard_panel_options'
),
array(
'page' => '/backend/',
'delegate' => 'DashboardPanelTypes',
'callback' => 'dashboard_panel_types'
),
array(
'page' => '/system/authors/',
'delegate' => 'AddDefaultAuthorAreas',
'callback' => 'author_default_section'
)
);
}
public function fetchNavigation() {
return array(
array(
'name' => __('Dashboard'),
'type' => 'content',
'children' => array(
array(
'link' => '/index/',
'name' => __('Dashboard'),
'visible' => 'yes'
),
)
)
);
}
public function append_assets($context) {
$page = Administration::instance()->Page;
$page->addStylesheetToHead(URL . '/extensions/dashboard/assets/dashboard.backend.css', 'screen', 666);
$page->addScriptToHead(URL . '/extensions/dashboard/assets/dashboard.backend.js', 667);
}
public function author_default_section($context) {
$context['options'][] = array(
'/extension/dashboard/', //value
($context['default_area'] == '/extension/dashboard/'), //selected
__('Dashboard') // label
);
}
public static function getPanels() {
return Symphony::Database()->fetch('SELECT * FROM tbl_dashboard_panels ORDER BY sort_order ASC');
}
public static function getPanel($panel_id) {
return Symphony::Database()->fetchRow(0, "SELECT * FROM tbl_dashboard_panels WHERE id='{$panel_id}'");
}
public static function deletePanel($panel_id) {
return Symphony::Database()->query("DELETE FROM tbl_dashboard_panels WHERE id='{$panel_id}'");
}
public static function updatePanelOrder($id, $placement, $sort_order) {
$sql = sprintf(
"UPDATE tbl_dashboard_panels SET
placement = '%s',
sort_order = '%d'
WHERE id = '%d'",
Symphony::Database()->cleanValue($placement),
Symphony::Database()->cleanValue($sort_order),
(int)$id
);
return Symphony::Database()->query($sql);
}
public static function savePanel($panel, $config) {
if (!isset($panel['id']) || empty($panel['id'])) {
$max_sort_order = (int)reset(Symphony::Database()->fetchCol('max_sort_order', 'SELECT MAX(sort_order) AS `max_sort_order` FROM tbl_dashboard_panels'));
Symphony::Database()->query(sprintf(
"INSERT INTO tbl_dashboard_panels
(label, type, config, placement, sort_order)
VALUES('%s','%s','%s','%s','%d')",
Symphony::Database()->cleanValue($panel['label']),
Symphony::Database()->cleanValue($panel['type']),
Symphony::Database()->cleanValue(serialize($config)),
Symphony::Database()->cleanValue($panel['placement']),
$max_sort_order + 1
));
return Symphony::Database()->getInsertID();
}
else {
Symphony::Database()->query(sprintf(
"UPDATE tbl_dashboard_panels SET
label = '%s',
config = '%s',
placement = '%s'
WHERE id = '%d'",
Symphony::Database()->cleanValue($panel['label']),
Symphony::Database()->cleanValue(serialize($config)),
Symphony::Database()->cleanValue($panel['placement']),
(int)$panel['id']
));
return (int)$panel['id'];
}
}
public static function buildPanelHTML($p) {
$panel = new XMLElement('div', NULL, array('class' => 'panel', 'id' => 'id-' . $p['id']));
$panel->appendChild(new XMLElement('a', __('Edit'), array('class' => 'panel-edit', 'href' => URL . '/symphony/extension/dashboard/panel_config/?id=' . $p['id'] . '&type=' . $p['type'])));
$panel->appendChild(new XMLElement('h3', (($p['label'] == '') ? __('Untitled Panel') : $p['label']) . ('<span>'.__('drag to re-order').'</span>')));
$panel_inner = new XMLElement('div', NULL, array('class' => 'panel-inner'));
/**
* Ask panel extensions to render their panel HTML.
*
* @delegate DashboardPanelRender
* @param string $context
* '/backend/'
* @param string $type
* @param array $config
* @param XMLElement $panel
*/
Symphony::ExtensionManager()->notifyMembers('DashboardPanelRender', '/backend/', array(
'type' => $p['type'],
'config' => unserialize($p['config']),
'label' => $p['label'],
'id' => $p['id'],
'panel' => &$panel_inner
));
$panel->setAttribute('class', 'panel ' . $p['type']);
$panel->appendChild($panel_inner);
return $panel;
}
public static function buildPanelOptions($type, $panel_id, $errors) {
$panel_config = self::getPanel($panel_id);
$form = null;
/**
* Ask panel extensions to render their options HTML.
*
* @delegate DashboardPanelOptions
* @param string $context
* '/backend/'
* @param string $type
* @param XMLElement $form
* @param array $existing_config
* @param array $errors
*/
Symphony::ExtensionManager()->notifyMembers('DashboardPanelOptions', '/backend/', array(
'type' => $type,
'form' => &$form,
'existing_config' => unserialize($panel_config['config']),
'label' => $panel_config['label'],
'id' => $panel_config['id'],
'errors' => $errors
));
return $form;
}
public static function validatePanelOptions($type, $panel_id) {
$panel_config = self::getPanel($panel_id);
$errors = array();
/**
* Ask panel extensions to validate their options.
*
* @delegate DashboardPanelValidate
* @param string $context
* '/backend/'
* @param string $type
* @param array $errors
* @param array $existing_config
*/
Symphony::ExtensionManager()->notifyMembers('DashboardPanelValidate', '/backend/', array(
'type' => $type,
'errors' => &$errors,
'existing_config' => unserialize($panel_config['config']),
'label' => $panel_config['label'],
'id' => $panel_config['id']
));
return $errors;
}
public function dashboard_panel_types($context) {
$context['types']['datasource_to_table'] = __('Data Source to Table');
$context['types']['rss_reader'] = __('RSS Reader');
$context['types']['html_block'] = __('HTML Block');
$context['types']['markdown_text'] = __('Markdown Text');
$context['types']['symphony_overview'] = __('Symphony Overview');
}
public function dashboard_panel_options($context) {
$config = $context['existing_config'];
switch($context['type']) {
case 'datasource_to_table':
$datasources = array();
foreach(DatasourceManager::listAll() as $ds) {
$datasources[] = array(
$ds['handle'],
($config['datasource'] == $ds['handle']),
$ds['name']
);
}
$fieldset = new XMLElement('fieldset', NULL, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', __('Data Source to Table')));
$label = Widget::Label(__('Data Source'), Widget::Select('config[datasource]', $datasources));
$fieldset->appendChild($label);
$context['form'] = $fieldset;
break;
case 'rss_reader':
$fieldset = new XMLElement('fieldset', NULL, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', __('RSS Reader')));
$label = Widget::Label(__('Feed URL'), Widget::Input('config[url]', $config['url']));
$fieldset->appendChild($label);
$label = Widget::Label(__('Items to display'), Widget::Select('config[show]',
array(
array(
'label' => __('Full view'),
'options' => array(
array('full-all', ($config['show'] == 'full-all'), __('All items')),
array('full-3', ($config['show'] == 'full-3'), '3 ' . __('items')),
array('full-5', ($config['show'] == 'full-5'), '5 ' . __('items')),
array('full-10', ($config['show'] == 'full-10'), '10 ' . __('items'))
)
),
array(
'label' => __('List view'),
'options' => array(
array('list-all', ($config['show'] == 'list-all'), __('All items')),
array('list-3', ($config['show'] == 'list-3'), '3 ' . __('items')),
array('list-5', ($config['show'] == 'list-5'), '5 ' . __('items')),
array('list-10', ($config['show'] == 'list-10'), '10 ' . __('items'))
)
),
)
));
$fieldset->appendChild($label);
$label = Widget::Label(__('Cache (minutes)'), Widget::Input('config[cache]', (string)(int)$config['cache']));
$fieldset->appendChild($label);
$context['form'] = $fieldset;
break;
case 'html_block':
$fieldset = new XMLElement('fieldset', NULL, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', __('HTML Block')));
$label = Widget::Label(__('Page URL'), Widget::Input('config[url]', $config['url']));
$fieldset->appendChild($label);
$label = Widget::Label(__('Cache (minutes)'), Widget::Input('config[cache]', (string)(int)$config['cache']));
$fieldset->appendChild($label);
$context['form'] = $fieldset;
break;
case 'markdown_text':
$fieldset = new XMLElement('fieldset', NULL, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', __('Markdown Text Block')));
$formatters = array();
foreach(TextformatterManager::listAll() as $tf) {
$formatters[] = array(
$tf['handle'],
($config['formatter'] == $tf['handle']),
$tf['name']
);
}
$fieldset = new XMLElement('fieldset', NULL, array('class' => 'settings'));
$fieldset->appendChild(new XMLElement('legend', __('Markdown Text')));
$label = Widget::Label(__('Text Formatter'), Widget::Select('config[formatter]', $formatters));
$fieldset->appendChild($label);
$label = Widget::Label(__('Text'), Widget::Textarea('config[text]', 6, 25, $config['text']));
$fieldset->appendChild($label);
$context['form'] = $fieldset;
break;
}
}
public function render_panel($context) {
$config = $context['config'];
switch($context['type']) {
case 'datasource_to_table':
$ds = DatasourceManager::create($config['datasource'], NULL, false);
if (!$ds) {
$context['panel']->appendChild(new XMLElement('div', __(
'The Data Source with the name <code>%s</code> could not be found.',
array($config['datasource'])
)));
return;
}
$param_pool = array();
$xml = $ds->grab($param_pool);
if(!$xml) return;
$xml = $xml->generate();
require_once(TOOLKIT . '/class.xsltprocess.php');
$proc = new XsltProcess();
$data = $proc->process(
$xml,
file_get_contents(EXTENSIONS . '/dashboard/lib/datasource-to-table.xsl')
);
$context['panel']->appendChild(new XMLElement('div', $data));
break;
case 'rss_reader':
require_once(TOOLKIT . '/class.gateway.php');
require_once(CORE . '/class.cacheable.php');
$cache_id = md5('rss_reader_cache' . $config['url']);
$cache = new Cacheable(Administration::instance()->Database());
$data = $cache->check($cache_id);
if(!$data) {
$ch = new Gateway;
$ch->init();
$ch->setopt('URL', $config['url']);
$ch->setopt('TIMEOUT', 6);
$new_data = $ch->exec();
$writeToCache = true;
if ((int)$config['cache'] > 0) {
$cache->write($cache_id, $new_data, $config['cache']);
}
$xml = $new_data;
if (empty($xml) && $data) $xml = $data['data'];
} else {
$xml = $data['data'];
}
if(!$xml) $xml = '<error>' . __('Error: could not retrieve panel XML feed.') . '</error>';
require_once(TOOLKIT . '/class.xsltprocess.php');
$proc = new XsltProcess();
$data = $proc->process(
$xml,
file_get_contents(EXTENSIONS . '/dashboard/lib/rss-reader.xsl'),
array('show' => $config['show'])
);
$context['panel']->appendChild(new XMLElement('div', $data));
break;
case 'html_block':
require_once(TOOLKIT . '/class.gateway.php');
require_once(CORE . '/class.cacheable.php');
$cache_id = md5('html_block_' . $config['url']);
$cache = new Cacheable(Administration::instance()->Database());
$data = $cache->check($cache_id);
if(!$data) {
$ch = new Gateway;
$ch->init();
$ch->setopt('URL', $config['url']);
$ch->setopt('TIMEOUT', 6);
$new_data = $ch->exec();
$writeToCache = true;
if ((int)$config['cache'] > 0) {
$cache->write($cache_id, $new_data, $config['cache']);
}
$html = $new_data;
if (empty($html) && $data) $html = $data['data'];
} else {
$html = $data['data'];
}
if(!$html) $html = '<p class="invalid">' . __('Error: could not retrieve panel HTML.') . '</p>';
$context['panel']->appendChild(new XMLElement('div', $html));
break;
case 'symphony_overview':
$container = new XMLElement('div');
$dl = new XMLElement('dl');
$dl->appendChild(new XMLElement('dt', __('Website Name')));
$dl->appendChild(new XMLElement('dd', Symphony::Configuration()->get('sitename', 'general')));
$current_version = Symphony::Configuration()->get('version', 'symphony');
require_once(TOOLKIT . '/class.gateway.php');
$ch = new Gateway;
$ch->init();
$ch->setopt('URL', 'https://api.github.com/repos/symphonycms/symphony-2/tags');
$ch->setopt('TIMEOUT', $timeout);
$repo_tags = $ch->exec();
// tags request found
if(!empty($repo_tags)) {
$repo_tags = @json_decode($repo_tags);
$tags = array();
if (!$repo_tags || !is_array($repo_tags)) {
$latest_version = $current_version;
} else {
foreach($repo_tags as $tag) {
// remove tags that contain strings
if(preg_match('/[a-zA]/i', $tag->name)) continue;
$tags[] = $tag->name;
}
natsort($tags);
rsort($tags);
$latest_version = reset($tags);
}
}
// request for tags failed, assume current version is latest
else {
$latest_version = $current_version;
}
$needs_update = version_compare($latest_version, $current_version, '>');
$dl->appendChild(new XMLElement('dt', __('Version')));
$dl->appendChild(new XMLElement(
'dd',
$current_version . (($needs_update) ? ' (<a href="http://getsymphony.com/download/releases/version/'.$latest_version.'/">' . __('Latest is %s', array($latest_version)) . "</a>)" : '')
));
// Display PHP version
$ver = (float)phpversion();
$version = phpversion();
if ($ver > 7.0) {
$dl->appendChild(new XMLElement('dt', __('PHP Version')));
$dl->appendChild(new XMLElement('dd', '<span style="color:green">' . $version . '</span>'));
} elseif ($ver === 7.0) {
$dl->appendChild(new XMLElement('dt', __('PHP Version')));
$dl->appendChild(new XMLElement('dd', '<span style="color:orange">' . $version . '<br />Please go to your server management and check if a newer PHP version is available.</span>'));
} else {
$dl->appendChild(new XMLElement('dt', __('PHP Version')));
$dl->appendChild(new XMLElement('dd', '<span style="color:red">' . $version . '<br />Your PHP version is outdated. For security reasons, please go to your server management and set the current PHP version (> 7.0) for this host.</ span>'));
}
$container->appendChild(new XMLElement('h4', __('Configuration')));
$container->appendChild($dl);
$entries = 0;
foreach(SectionManager::fetch() as $section) {
$entries += EntryManager::fetchCount($section->get('id'));
}
$dl = new XMLElement('dl');
$dl->appendChild(new XMLElement('dt', __('Sections')));
$dl->appendChild(new XMLElement('dd', (string)count(SectionManager::fetch())));
$dl->appendChild(new XMLElement('dt', __('Entries')));
$dl->appendChild(new XMLElement('dd', (string)$entries));
$dl->appendChild(new XMLElement('dt', __('Data Sources')));
$dl->appendChild(new XMLElement('dd', (string)count(DatasourceManager::listAll())));
$dl->appendChild(new XMLElement('dt', __('Events')));
$dl->appendChild(new XMLElement('dd', (string)count(EventManager::listAll())));
$dl->appendChild(new XMLElement('dt', __('Pages')));
$dl->appendChild(new XMLElement('dd', (string)count(PageManager::fetch())));
$container->appendChild(new XMLElement('h4', __('Statistics')));
$container->appendChild($dl);
$context['panel']->appendChild($container);
break;
case 'markdown_text':
$formatter = TextformatterManager::create($config['formatter']);
$html = $formatter->run($config['text']);
$context['panel']->appendChild(new XMLElement('div', $html));
break;
}
}
}