profile.php
27.1 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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
<?php
/**
* @copyright Copyright (c) 2009-2020 Ryan Demmer. All rights reserved
* @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* JCE is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses
*/
defined('JPATH_PLATFORM') or die;
require JPATH_SITE . '/components/com_jce/editor/libraries/classes/editor.php';
require JPATH_ADMINISTRATOR . '/components/com_jce/helpers/plugins.php';
require JPATH_ADMINISTRATOR . '/components/com_jce/helpers/profiles.php';
/**
* Item Model for a Profile.
*
* @since 1.6
*/
class JceModelProfile extends JModelAdmin
{
/**
* The type alias for this content type.
*
* @var string
*
* @since 3.2
*/
public $typeAlias = 'com_jce.profile';
/**
* The prefix to use with controller messages.
*
* @var string
*
* @since 1.6
*/
protected $text_prefix = 'COM_JCE';
/**
* Returns a Table object, always creating it.
*
* @param type $type The table type to instantiate
* @param string $prefix A prefix for the table class name. Optional
* @param array $config Configuration array for model. Optional
*
* @return JTable A database object
*
* @since 1.6
*/
public function getTable($type = 'Profiles', $prefix = 'JceTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
/* Override to prevent plugins from processing form data */
protected function preprocessData($context, &$data, $group = 'system')
{
if (!isset($data->config)) {
return;
}
$config = $data->config;
if (is_string($config)) {
$config = json_decode($config, true);
}
if (empty($config)) {
return;
}
// editor parameters
if (isset($config['editor'])) {
if (!empty($config['editor']['toolbar_theme']) && $config['editor']['toolbar_theme'] === 'mobile') {
$config['editor']['toolbar_theme'] = 'default.touch';
}
if (isset($config['editor']['relative_urls']) && !isset($config['editor']['convert_urls'])) {
$config['editor']['convert_urls'] = $config['editor']['relative_urls'] == 0 ? 'absolute' : 'relative';
}
}
$data->config = $config;
}
/**
* Method to allow derived classes to preprocess the form.
*
* @param JForm $form A JForm object
* @param mixed $data The data expected for the form
* @param string $group The name of the plugin group to import (defaults to "content")
*
* @see JFormField
* @since 1.6
*
* @throws Exception if there is an error in the form event
*/
protected function preprocessForm(JForm $form, $data, $group = 'content')
{
if (!empty($data)) {
$registry = new JRegistry($data->config);
// process individual fields to remove default value if required
$fields = $form->getFieldset();
foreach ($fields as $field) {
$name = $field->getAttribute('name');
// get the field group and add the field name
$group = (string) $field->group;
// must be a grouped parameter, eg: editor, imgmanager etc.
if (!$group) {
continue;
}
// create key from group and name
$group = $group . '.' . $name;
// explode group to array
$parts = explode('.', $group);
// remove "config" from group name so it matches params data object
if ($parts[0] === "config") {
array_shift($parts);
$group = implode('.', $parts);
}
// reset the "default" attribute value if a value is set
if ($registry->exists($group)) {
$form->setFieldAttribute($name, 'default', '', (string) $field->group);
}
}
}
// allow plugins to process form, eg: MediaField etc.
parent::preprocessForm($form, $data);
}
public function getForm($data = array(), $loadData = true)
{
JFormHelper::addFieldPath('JPATH_ADMINISTRATOR/components/com_jce/models/fields');
// Get the setup form.
$form = $this->loadForm('com_jce.profile', 'profile', array('control' => 'jform', 'load_data' => false));
if (!$form) {
return false;
}
JFactory::getLanguage()->load('com_jce_pro', JPATH_SITE);
// editor manifest
$manifest = __DIR__ . '/forms/editor.xml';
// load editor manifest
if (is_file($manifest)) {
if ($editor_xml = simplexml_load_file($manifest)) {
$form->setField($editor_xml, 'config');
}
}
// pro manifest
$manifest = WF_EDITOR_LIBRARIES . '/pro/xml/image.xml';
// load pro manifest
if (is_file($manifest)) {
if ($pro_xml = simplexml_load_file($manifest)) {
$form->setField($pro_xml, 'config');
}
}
$data = $this->loadFormData();
// Allow for additional modification of the form, and events to be triggered.
// We pass the data because plugins may require it.
$this->preprocessForm($form, $data);
// Load the data into the form after the plugins have operated.
$form->bind($data);
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form
*
* @since 1.6
*/
protected function loadFormData()
{
$data = $this->getItem();
// convert 0 value to string containing both options
if (empty($data->area)) {
$data->area = '1,2';
}
$data->device = explode(',', $data->device);
if (!empty($data->components)) {
$data->components = explode(',', $data->components);
$data->components_select = 1;
}
$data->types = explode(',', $data->types);
$table = JTable::getInstance('user');
$users = array();
foreach (explode(',', $data->users) as $id) {
if ($table->load((int) $id)) {
$user = new StdClass();
$user->value = $id;
$user->text = htmlspecialchars($table->name, ENT_COMPAT, 'UTF-8');
$users[] = $user;
}
}
$data->users = $users;
$data->config = $data->params;
$this->preprocessData('com_jce.profiles', $data);
return $data;
}
public function getRows()
{
$data = $this->getItem();
$array = array();
$rows = explode(';', $data->rows);
$plugins = $this->getButtons();
$i = 1;
foreach ($rows as $row) {
$groups = array();
// remove spacers
$row = str_replace(array('|', 'spacer'), '', $row);
foreach (explode('spacer', $row) as $group) {
// get items in group
$items = explode(',', $group);
$buttons = array();
// remove duplicates
$items = array_unique($items);
foreach ($items as $x => $item) {
if ($item === 'spacer') {
unset($items[$x]);
continue;
}
// not in the list...
if (empty($item) || array_key_exists($item, $plugins) === false) {
continue;
}
// must be assigned...
if (!$plugins[$item]->active) {
continue;
}
// assign icon
$buttons[] = $plugins[$item];
}
$groups[] = $buttons;
}
$array[$i] = $groups;
++$i;
}
return $array;
}
/**
* An array of buttons not in the current editor layout.
*
* @return array
*/
public function getAvailableButtons()
{
$plugins = $this->getButtons();
$available = array_filter($plugins, function ($plugin) {
return !$plugin->active;
});
return $available;
}
public function getAdditionalPlugins()
{
$plugins = $this->getButtons();
$additional = array_filter($plugins, function ($plugin) {
return $plugin->editable && !$plugin->row;
});
return $additional;
}
public function getButtons()
{
$commands = $this->getCommands();
$plugins = $this->getPlugins();
return array_merge($commands, $plugins);
}
public function getCommands()
{
static $commands;
if (empty($commands)) {
$data = $this->getItem();
$rows = preg_split('#[;,]#', $data->rows);
$commands = array();
foreach (JcePluginsHelper::getCommands() as $name => $command) {
// set as active
$command->active = in_array($name, $rows);
$command->icon = explode(',', $command->icon);
// set default empty value
$command->image = '';
// ui class, default is blank
if (empty($command->class)) {
$command->class = '';
}
// cast row to integer
$command->row = (int) $command->row;
// cast editable to integer
$command->editable = (int) $command->editable;
// translate title
$command->title = JText::_($command->title);
// translate description
$command->description = JText::_($command->description);
$command->name = $name;
$commands[$name] = $command;
}
}
// merge plugins and commands
return $commands;
}
public function getPlugins()
{
static $plugins;
if (empty($plugins)) {
$plugins = array();
$data = $this->loadFormData();
// array or profile plugin items
$rows = explode(',', $data->plugins);
// remove duplicates
$rows = array_unique($rows);
// only need plugins with xml files
foreach (JcePluginsHelper::getPlugins() as $name => $plugin) {
$plugin->icon = empty($plugin->icon) ? array() : explode(',', $plugin->icon);
// set as active if it is in the profile
$plugin->active = in_array($plugin->name, $rows);
// ui class, default is blank
if (empty($plugin->class)) {
$plugin->class = '';
}
$plugin->class = preg_replace_callback('#\b([a-z0-9]+)-([a-z0-9]+)\b#', function ($matches) {
return 'mce' . ucfirst($matches[1]) . ucfirst($matches[2]);
}, $plugin->class);
// translate title
$plugin->title = JText::_($plugin->title);
// translate description
$plugin->description = JText::_($plugin->description);
// cast row to integer
$plugin->row = (int) $plugin->row;
// cast editable to integer
$plugin->editable = (int) $plugin->editable;
// plugin extensions
$plugin->extensions = array();
if (is_file($plugin->manifest)) {
$plugin->form = $this->loadForm('com_jce.profile.' . $plugin->name, $plugin->manifest, array('control' => 'jform[config]', 'load_data' => true), true, '//extension');
$plugin->formclass = 'options-grid-form options-grid-form-full';
$fieldsets = $plugin->form->getFieldsets();
// no parameter fields
if (empty($fieldsets)) {
$plugin->form = false;
$plugins[$name] = $plugin;
continue;
}
// bind data to the form
$plugin->form->bind($data->params);
$extensions = JcePluginsHelper::getExtensions();
foreach ($extensions as $type => $items) {
$item = new StdClass;
$item->name = '';
$item->title = '';
$item->manifest = WF_EDITOR_LIBRARIES . '/xml/config/' . $type . '.xml';
$item->context = '';
array_unshift($items, $item);
foreach ($items as $p) {
// check for plugin fieldset using xpath, as fieldset can be empty
$fieldset = $plugin->form->getXml()->xpath('(//fieldset[@name="plugin.' . $type . '"])');
// not supported, move along...
if (empty($fieldset)) {
continue;
}
$context = (string) $fieldset[0]->attributes()->context;
// check for a context, eg: images, web, video
if ($context && !in_array($p->context, explode(',', $context))) {
continue;
}
if (is_file($p->manifest)) {
$path = array($plugin->name, $type, $p->name);
// create new extension object
$extension = new StdClass;
// set extension name as the plugin name
$extension->name = $p->name;
// set extension title
$extension->title = $p->title;
// load form
$extension->form = $this->loadForm('com_jce.profile.' . implode('.', $path), $p->manifest, array('control' => 'jform[config][' . $plugin->name . '][' . $type . ']', 'load_data' => true), true, '//extension');
$extension->formclass = 'options-grid-form options-grid-form-full';
// get fieldsets if any
$fieldsets = $extension->form->getFieldsets();
foreach ($fieldsets as $fieldset) {
// load form
$plugin->extensions[$type][$p->name] = $extension;
if (!isset($data->params[$plugin->name])) {
continue;
}
if (!isset($data->params[$plugin->name][$type])) {
continue;
}
// bind data to the form
$extension->form->bind($data->params[$plugin->name][$type]);
}
}
}
}
}
// add to array
$plugins[$name] = $plugin;
}
}
return $plugins;
}
/**
* Prepare and sanitise the table data prior to saving.
*
* @param JTable $table A reference to a JTable object
*
* @since 1.6
*/
protected function prepareTable($table)
{
$date = JFactory::getDate();
$user = JFactory::getUser();
foreach ($table->getProperties() as $key => $value) {
switch ($key) {
case 'name':
case 'description':
$value = filter_var($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
break;
case 'device':
$value = implode(',', filter_var_array($value, FILTER_SANITIZE_STRING));
break;
case 'area':
if (is_array($value)) {
if (count($value) === 2) {
$value = 0;
} else {
$value = $value[0];
}
}
$value = $value;
break;
case 'components':
if (is_array($value)) {
$value = implode(',', filter_var_array($value, FILTER_SANITIZE_STRING));
}
break;
case 'types':
case 'users':
if (is_string($value)) {
$value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
}
if (is_array($value)) {
$value = implode(',', filter_var_array($value, FILTER_SANITIZE_NUMBER_INT));
}
break;
case 'plugins':
$value = preg_replace('#[^\w,]+#', '', $value);
break;
case 'rows':
$value = preg_replace('#[^\w,;]+#', '', $value);
break;
case 'params':
break;
}
$table->$key = $value;
}
if (empty($table->id)) {
// Set ordering to the last item if not set
if (empty($table->ordering)) {
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('MAX(ordering)')
->from($db->quoteName('#__wf_profiles'));
$db->setQuery($query);
$max = $db->loadResult();
$table->ordering = $max + 1;
}
}
}
public function validate($form, $data, $group = null)
{
$filter = JFilterInput::getInstance();
// get unfiltered config data
$config = isset($data['config']) ? $data['config'] : array();
// get layout rows and plugins data
$rows = isset($data['rows']) ? $data['rows'] : '';
$plugins = isset($data['plugins']) ? $data['plugins'] : '';
// clean layout rows and plugins data
$data['rows'] = $filter->clean($rows, 'STRING');
$data['plugins'] = $filter->clean($plugins, 'STRING');
// add back config data
$data['params'] = $filter->clean($config, 'ARRAY');
if (empty($data['components']) || empty($data['components_select'])) {
$data['components'] = '';
}
if (empty($data['users'])) {
$data['users'] = '';
}
if (empty($data['types'])) {
$data['types'] = '';
}
return $data;
}
private static function cleanParamData($data)
{
// clean up link plugin parameters
array_walk($data, function(&$params, $plugin) {
if ($plugin === "link") {
if (isset($params['dir'])) {
if (!empty($params['dir']) && empty($params['direction'])) {
$params['direction'] = $params['dir'];
}
unset($params['dir']);
}
}
if (is_array($params) && WFUtility::is_associative_array($params)) {
array_walk($params, function(&$value, $key) {
if (is_string($value) && WFUtility::isJson($value)) {
$value = json_decode($value, true);
}
});
}
});
return $data;
}
/**
* Method to save the form data.
*
* @param array The form data
*
* @return bool True on success
*
* @since 2.7
*/
public function save($data)
{
$app = JFactory::getApplication();
// get profile table
$table = $this->getTable();
// Alter the title for save as copy
if ($app->input->get('task') == 'save2copy') {
// Alter the title
$name = $data['name'];
while ($table->load(array('name' => $name))) {
if ($name == $table->name) {
$name = Joomla\String\StringHelper::increment($name);
}
}
$data['name'] = $name;
$data['published'] = 0;
}
$key = $table->getKeyName();
$pk = (!empty($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
if ($pk && $table->load($pk)) {
if (empty($data['rows'])) {
$data['rows'] = $table->rows;
}
if (empty($data['plugins'])) {
$data['plugins'] = $table->plugins;
}
$json = array();
$params = empty($table->params) ? '' : $table->params;
// convert params to json data array
$params = (array) json_decode($params, true);
$plugins = isset($data['plugins']) ? $data['plugins'] : $table->plugins;
// get plugins
$items = explode(',', $plugins);
// add "editor"
$items[] = 'editor';
// make sure we have a value
if (empty($data['params'])) {
$data['params'] = array();
}
$data['params'] = self::cleanParamData($data['params']);
// data for editor and plugins
foreach ($items as $item) {
// add config data
if (array_key_exists($item, $data['params'])) {
$value = $data['params'][$item];
// clean and add to json array for merging
$json[$item] = filter_var_array($value, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
}
}
// merge and encode as json string
$data['params'] = json_encode(WFUtility::array_merge_recursive_distinct($params, $json));
}
if (parent::save($data)) {
return true;
}
return false;
}
public function copy($ids)
{
// Check for request forgeries
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$table = $this->getTable();
foreach ($ids as $id) {
if (!$table->load($id)) {
$this->setError($table->getError());
} else {
$name = JText::sprintf('WF_PROFILES_COPY_OF', $table->name);
$table->name = $name;
$table->id = 0;
$table->published = 0;
}
// Check the row.
if (!$table->check()) {
$this->setError($table->getError());
return false;
}
// Store the row.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}
}
return true;
}
public function export($ids)
{
$db = JFactory::getDBO();
$buffer = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
$buffer .= "\n" . '<export type="profiles">';
$buffer .= "\n\t" . '<profiles>';
$private = array('id', 'checked_out', 'checked_out_time');
foreach ($ids as $id) {
$table = $this->getTable();
if (!$table->load($id)) {
continue;
}
$buffer .= "\n\t\t";
$buffer .= '<profile>';
$fields = $table->getProperties();
foreach ($fields as $key => $value) {
// skip some stuff
if (in_array($key, $private)) {
continue;
}
// set published to 0
if ($key === "published") {
$value = 0;
}
if ($key == 'params') {
$buffer .= "\n\t\t\t" . '<' . $key . '>' . trim($value) . '</' . $key . '>';
} else {
$buffer .= "\n\t\t\t" . '<' . $key . '>' . JceProfilesHelper::encodeData($value) . '</' . $key . '>';
}
}
$buffer .= "\n\t\t</profile>";
}
$buffer .= "\n\t</profiles>";
$buffer .= "\n</export>";
// set_time_limit doesn't work in safe mode
if (!ini_get('safe_mode')) {
@set_time_limit(0);
}
$name = 'jce_editor_profile_' . date('Y_m_d') . '.xml';
$app = JFactory::getApplication();
$app->allowCache(false);
$app->setHeader('Content-Transfer-Encoding', 'binary');
$app->setHeader('Content-Type', 'text/xml');
$app->setHeader('Content-Disposition', 'attachment;filename="' . $name . '";');
// set output content
$app->setBody($buffer);
// stream to client
echo $app->toString();
jexit();
}
/**
* Process XML restore file.
*
* @param object $xml
*
* @return bool
*/
public function import()
{
// Check for request forgeries
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
jimport('joomla.filesystem.file');
$app = JFactory::getApplication();
$tmp = $app->getCfg('tmp_path');
jimport('joomla.filesystem.file');
$file = $app->input->files->get('profile_file', null, 'raw');
// check for valid uploaded file
if (empty($file) || !is_uploaded_file($file['tmp_name'])) {
$app->enqueueMessage(JText::_('WF_PROFILES_UPLOAD_NOFILE'), 'error');
return false;
}
if ($file['error'] || $file['size'] < 1) {
$app->enqueueMessage(JText::_('WF_PROFILES_UPLOAD_NOFILE'), 'error');
return false;
}
// sanitize the file name
$name = JFile::makeSafe($file['name']);
if (empty($name)) {
$app->enqueueMessage(JText::_('WF_PROFILES_IMPORT_ERROR'), 'error');
return false;
}
// Build the appropriate paths.
$config = JFactory::getConfig();
$destination = $config->get('tmp_path') . '/' . $name;
$source = $file['tmp_name'];
// Move uploaded file.
JFile::upload($source, $destination, false, true);
if (!is_file($destination)) {
$app->enqueueMessage(JText::_('WF_PROFILES_UPLOAD_FAILED'), 'error');
return false;
}
$result = JceProfilesHelper::processImport($destination);
if ($result === false) {
$app->enqueueMessage(JText::_('WF_PROFILES_IMPORT_ERROR'), 'error');
return false;
}
$app->enqueueMessage(JText::sprintf('WF_PROFILES_IMPORT_SUCCESS', $result));
return true;
}
}