/
TemplateModel.php
1914 lines (1635 loc) · 46.5 KB
/
TemplateModel.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
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package Joomla.Administrator
* @subpackage com_templates
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Templates\Administrator\Model;
\defined('_JEXEC') or die;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Image\Image;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\FormModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\Component\Templates\Administrator\Helper\TemplateHelper;
use Joomla\Database\ParameterType;
/**
* Template model class.
*
* @since 1.6
*/
class TemplateModel extends FormModel
{
/**
* The information in a template
*
* @var \stdClass
* @since 1.6
*/
protected $template = null;
/**
* The path to the template
*
* @var \stdClass
* @since 3.2
*/
protected $element = null;
/**
* Internal method to get file properties.
*
* @param string $path The base path.
* @param string $name The file name.
*
* @return object
*
* @since 1.6
*/
protected function getFile($path, $name)
{
$temp = new \stdClass;
if ($template = $this->getTemplate())
{
$temp->name = $name;
$temp->id = urlencode(base64_encode($path . $name));
return $temp;
}
}
/**
* Method to store file information.
*
* @param string $path The base path.
* @param string $name The file name.
* @param stdClass $template The std class object of template.
*
* @return object StdClass object.
*
* @since 4.0.0
*/
protected function storeFileInfo($path, $name, $template)
{
$temp = new \stdClass;
$temp->id = base64_encode($path . $name);
$temp->client = $template->client_id;
$temp->template = $template->element;
$temp->extension_id = $template->extension_id;
if ($coreFile = $this->getCoreFile($path . $name, $template->client_id))
{
$temp->coreFile = md5_file($coreFile);
}
else
{
$temp->coreFile = null;
}
return $temp;
}
/**
* Method to get all template list.
*
* @return object stdClass object
*
* @since 4.0.0
*/
public function getTemplateList()
{
// Get a db connection.
$db = Factory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select the required fields from the table
$query->select(
$this->getState(
'list.select',
'a.extension_id, a.name, a.element, a.client_id'
)
);
$query->from($db->quoteName('#__extensions', 'a'))
->where($db->quoteName('a.enabled') . ' = 1')
->where($db->quoteName('a.type') . ' = ' . $db->quote('template'));
// Reset the query.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$results = $db->loadObjectList();
return $results;
}
/**
* Method to get all updated file list.
*
* @param boolean $state The optional parameter if you want unchecked list.
* @param boolean $all The optional parameter if you want all list.
* @param boolean $cleanup The optional parameter if you want to clean record which is no more required.
*
* @return object stdClass object
*
* @since 4.0.0
*/
public function getUpdatedList($state = false, $all = false, $cleanup = false)
{
// Get a db connection.
$db = Factory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select the required fields from the table
$query->select(
$this->getState(
'list.select',
'a.template, a.hash_id, a.extension_id, a.state, a.action, a.client_id, a.created_date, a.modified_date'
)
);
$template = $this->getTemplate();
$query->from($db->quoteName('#__template_overrides', 'a'));
if (!$all)
{
$teid = (int) $template->extension_id;
$query->where($db->quoteName('extension_id') . ' = :teid')
->bind(':teid', $teid, ParameterType::INTEGER);
}
if ($state)
{
$query->where($db->quoteName('state') . ' = 0');
}
$query->order($db->quoteName('a.modified_date') . ' DESC');
// Reset the query.
$db->setQuery($query);
// Load the results as a list of stdClass objects.
$pks = $db->loadObjectList();
if ($state)
{
return $pks;
}
$results = array();
foreach ($pks as $pk)
{
$client = ApplicationHelper::getClientInfo($pk->client_id);
$path = Path::clean($client->path . '/templates/' . $pk->template . base64_decode($pk->hash_id));
if (file_exists($path))
{
$results[] = $pk;
}
elseif ($cleanup)
{
$cleanupIds = array();
$cleanupIds[] = $pk->hash_id;
$this->publish($cleanupIds, -3, $pk->extension_id);
}
}
return $results;
}
/**
* Method to get a list of all the core files of override files.
*
* @return array An array of all core files.
*
* @since 4.0.0
*/
public function getCoreList()
{
// Get list of all templates
$templates = $this->getTemplateList();
// Initialize the array variable to store core file list.
$this->coreFileList = array();
$app = Factory::getApplication();
foreach ($templates as $template)
{
$client = ApplicationHelper::getClientInfo($template->client_id);
$element = Path::clean($client->path . '/templates/' . $template->element . '/');
$path = Path::clean($element . 'html/');
if (is_dir($path))
{
$this->prepareCoreFiles($path, $element, $template);
}
else
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_TEMPLATE_FOLDER_NOT_FOUND'), 'error');
return false;
}
}
// Sort list of stdClass array.
usort(
$this->coreFileList,
function ($a, $b)
{
return strcmp($a->id, $b->id);
}
);
return $this->coreFileList;
}
/**
* Prepare core files.
*
* @param string $dir The path of the directory to scan.
* @param string $element The path of the template element.
* @param stdClass $template The stdClass object of template.
*
* @return array
*
* @since 4.0.0
*/
public function prepareCoreFiles($dir, $element, $template)
{
$dirFiles = scandir($dir);
foreach ($dirFiles as $key => $value)
{
if (in_array($value, array('.', '..', 'node_modules')))
{
continue;
}
if (is_dir($dir . $value))
{
$relativePath = str_replace($element, '', $dir . $value);
$this->prepareCoreFiles($dir . $value . '/', $element, $template);
}
else
{
$ext = pathinfo($dir . $value, PATHINFO_EXTENSION);
$allowedFormat = $this->checkFormat($ext);
if ($allowedFormat === true)
{
$relativePath = str_replace($element, '', $dir);
$info = $this->storeFileInfo('/' . $relativePath, $value, $template);
if ($info)
{
$this->coreFileList[] = $info;
}
}
}
}
return;
}
/**
* Method to update status of list.
*
* @param array $ids The base path.
* @param array $value The file name.
* @param integer $exid The template extension id.
*
* @return integer Number of files changed.
*
* @since 4.0.0
*/
public function publish($ids, $value, $exid)
{
$db = Factory::getDbo();
foreach ($ids as $id)
{
if ($value === -3)
{
$deleteQuery = $db->getQuery(true)
->delete($db->quoteName('#__template_overrides'))
->where($db->quoteName('hash_id') . ' = :hashid')
->where($db->quoteName('extension_id') . ' = :exid')
->bind(':hashid', $id)
->bind(':exid', $exid, ParameterType::INTEGER);
try
{
// Set the query using our newly populated query object and execute it.
$db->setQuery($deleteQuery);
$result = $db->execute();
}
catch (\RuntimeException $e)
{
return $e;
}
}
elseif ($value === 1 || $value === 0)
{
$updateQuery = $db->getQuery(true)
->update($db->quoteName('#__template_overrides'))
->set($db->quoteName('state') . ' = :state')
->where($db->quoteName('hash_id') . ' = :hashid')
->where($db->quoteName('extension_id') . ' = :exid')
->bind(':state', $value, ParameterType::INTEGER)
->bind(':hashid', $id)
->bind(':exid', $exid, ParameterType::INTEGER);
try
{
// Set the query using our newly populated query object and execute it.
$db->setQuery($updateQuery);
$result = $db->execute();
}
catch (\RuntimeException $e)
{
return $e;
}
}
}
return $result;
}
/**
* Method to get a list of all the files to edit in a template.
*
* @return array A nested array of relevant files.
*
* @since 1.6
*/
public function getFiles()
{
$result = array();
if ($template = $this->getTemplate())
{
$app = Factory::getApplication();
$client = ApplicationHelper::getClientInfo($template->client_id);
$path = Path::clean($client->path . '/templates/' . $template->element . '/');
$lang = Factory::getLanguage();
// Load the core and/or local language file(s).
$lang->load('tpl_' . $template->element, $client->path) ||
$lang->load('tpl_' . $template->element, $client->path . '/templates/' . $template->element);
$this->element = $path;
if (!is_writable($path))
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_DIRECTORY_NOT_WRITABLE'), 'error');
}
if (is_dir($path))
{
$result = $this->getDirectoryTree($path);
}
else
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_TEMPLATE_FOLDER_NOT_FOUND'), 'error');
return false;
}
// Clean up override history
$this->getUpdatedList(false, true, true);
}
return $result;
}
/**
* Get the directory tree.
*
* @param string $dir The path of the directory to scan
*
* @return array
*
* @since 3.2
*/
public function getDirectoryTree($dir)
{
$result = array();
$dirFiles = scandir($dir);
foreach ($dirFiles as $key => $value)
{
if (!in_array($value, array('.', '..', 'node_modules')))
{
if (is_dir($dir . $value))
{
$relativePath = str_replace($this->element, '', $dir . $value);
$result['/' . $relativePath] = $this->getDirectoryTree($dir . $value . '/');
}
else
{
$ext = pathinfo($dir . $value, PATHINFO_EXTENSION);
$allowedFormat = $this->checkFormat($ext);
if ($allowedFormat == true)
{
$relativePath = str_replace($this->element, '', $dir);
$info = $this->getFile('/' . $relativePath, $value);
$result[] = $info;
}
}
}
}
return $result;
}
/**
* Method to get the core file of override file
*
* @param string $file Override file
* @param integer $client_id Client Id
*
* @return string $corefile The full path and file name for the target file, or boolean false if the file is not found in any of the paths.
*
* @since 4.0.0
*/
public function getCoreFile($file, $client_id)
{
$app = Factory::getApplication();
$filePath = Path::clean($file);
$explodeArray = explode(DIRECTORY_SEPARATOR, $filePath);
// Only allow html/ folder
if ($explodeArray['1'] !== 'html')
{
return false;
}
$fileName = basename($filePath);
$type = $explodeArray['2'];
$client = ApplicationHelper::getClientInfo($client_id);
$componentPath = Path::clean($client->path . '/components/');
$modulePath = Path::clean($client->path . '/modules/');
$layoutPath = Path::clean(JPATH_ROOT . '/layouts/');
// For modules
if (stristr($type, 'mod_') !== false)
{
$folder = $explodeArray['2'];
$htmlPath = Path::clean($modulePath . $folder . '/tmpl/');
$fileName = $this->getSafeName($fileName);
$coreFile = Path::find($htmlPath, $fileName);
return $coreFile;
}
elseif (stristr($type, 'com_') !== false)
{
// For components
$folder = $explodeArray['2'];
$subFolder = $explodeArray['3'];
$fileName = $this->getSafeName($fileName);
// The new scheme, if a view has a tmpl folder
$newHtmlPath = Path::clean($componentPath . $folder . '/tmpl/' . $subFolder . '/');
if (!$coreFile = Path::find($newHtmlPath, $fileName))
{
// The old scheme, the views are directly in the component/tmpl folder
$oldHtmlPath = Path::clean($componentPath . $folder . '/views/' . $subFolder . '/tmpl/');
$coreFile = Path::find($oldHtmlPath, $fileName);
return $coreFile;
}
return $coreFile;
}
elseif (stristr($type, 'layouts') !== false)
{
// For Jlayouts
$subtype = $explodeArray['3'];
if (stristr($subtype, 'com_'))
{
$folder = $explodeArray['3'];
$subFolder = array_slice($explodeArray, 4, -1);
$subFolder = implode(DIRECTORY_SEPARATOR, $subFolder);
$htmlPath = Path::clean($componentPath . $folder . '/layouts/' . $subFolder);
$fileName = $this->getSafeName($fileName);
$coreFile = Path::find($htmlPath, $fileName);
return $coreFile;
}
elseif (stristr($subtype, 'joomla') || stristr($subtype, 'libraries') || stristr($subtype, 'plugins'))
{
$subFolder = array_slice($explodeArray, 3, -1);
$subFolder = implode(DIRECTORY_SEPARATOR, $subFolder);
$htmlPath = Path::clean($layoutPath . $subFolder);
$fileName = $this->getSafeName($fileName);
$coreFile = Path::find($htmlPath, $fileName);
return $coreFile;
}
}
return false;
}
/**
* Creates a safe file name for the given name.
*
* @param string $name The filename
*
* @return string $fileName The filtered name without Date
*
* @since 4.0.0
*/
private function getSafeName($name)
{
if (strpos($name, '-') !== false && preg_match('/[0-9]/', $name))
{
// Get the extension
$extension = File::getExt($name);
// Remove ( Date ) from file
$explodeArray = explode('-', $name);
$size = count($explodeArray);
$date = $explodeArray[$size - 2] . '-' . str_replace('.' . $extension, '', $explodeArray[$size - 1]);
if ($this->validateDate($date))
{
$nameWithoutExtension = implode('-', array_slice($explodeArray, 0, -2));
// Filtered name
$name = $nameWithoutExtension . '.' . $extension;
}
}
return $name;
}
/**
* Validate Date in file name.
*
* @param string $date Date to validate.
*
* @return boolean Return true if date is valid and false if not.
*
* @since 4.0.0
*/
private function validateDate($date)
{
$format = 'Ymd-His';
$valid = Date::createFromFormat($format, $date);
return $valid && $valid->format($format) === $date;
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
*
* @since 1.6
*/
protected function populateState()
{
$app = Factory::getApplication();
// Load the User state.
$pk = $app->input->getInt('id');
$this->setState('extension.id', $pk);
// Load the parameters.
$params = ComponentHelper::getParams('com_templates');
$this->setState('params', $params);
}
/**
* Method to get the template information.
*
* @return mixed Object if successful, false if not and internal error is set.
*
* @since 1.6
*/
public function &getTemplate()
{
if (empty($this->template))
{
$pk = (int) $this->getState('extension.id');
$db = $this->getDbo();
$app = Factory::getApplication();
// Get the template information.
$query = $db->getQuery(true)
->select($db->quoteName(['extension_id', 'client_id', 'element', 'name', 'manifest_cache']))
->from($db->quoteName('#__extensions'))
->where($db->quoteName('extension_id') . ' = :pk')
->where($db->quoteName('type') . ' = ' . $db->quote('template'))
->bind(':pk', $pk, ParameterType::INTEGER);
$db->setQuery($query);
try
{
$result = $db->loadObject();
}
catch (\RuntimeException $e)
{
$app->enqueueMessage($e->getMessage(), 'warning');
$this->template = false;
return false;
}
if (empty($result))
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND'), 'error');
$this->template = false;
}
else
{
$this->template = $result;
}
}
return $this->template;
}
/**
* Method to check if new template name already exists
*
* @return boolean true if name is not used, false otherwise
*
* @since 2.5
*/
public function checkNewName()
{
$db = $this->getDbo();
$name = $this->getState('new_name');
$query = $db->getQuery(true)
->select('COUNT(*)')
->from($db->quoteName('#__extensions'))
->where($db->quoteName('name') . ' = :name')
->bind(':name', $name);
$db->setQuery($query);
return ($db->loadResult() == 0);
}
/**
* Method to check if new template name already exists
*
* @return string name of current template
*
* @since 2.5
*/
public function getFromName()
{
return $this->getTemplate()->element;
}
/**
* Method to check if new template name already exists
*
* @return boolean true if name is not used, false otherwise
*
* @since 2.5
*/
public function copy()
{
$app = Factory::getApplication();
if ($template = $this->getTemplate())
{
$client = ApplicationHelper::getClientInfo($template->client_id);
$fromPath = Path::clean($client->path . '/templates/' . $template->element . '/');
// Delete new folder if it exists
$toPath = $this->getState('to_path');
if (Folder::exists($toPath))
{
if (!Folder::delete($toPath))
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_COULD_NOT_WRITE'), 'error');
return false;
}
}
// Copy all files from $fromName template to $newName folder
if (!Folder::copy($fromPath, $toPath) || !$this->fixTemplateName())
{
return false;
}
return true;
}
else
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_INVALID_FROM_NAME'), 'error');
return false;
}
}
/**
* Method to delete tmp folder
*
* @return boolean true if delete successful, false otherwise
*
* @since 2.5
*/
public function cleanup()
{
// Clear installation messages
$app = Factory::getApplication();
$app->setUserState('com_installer.message', '');
$app->setUserState('com_installer.extension_message', '');
// Delete temporary directory
return Folder::delete($this->getState('to_path'));
}
/**
* Method to rename the template in the XML files and rename the language files
*
* @return boolean true if successful, false otherwise
*
* @since 2.5
*/
protected function fixTemplateName()
{
// Rename Language files
// Get list of language files
$result = true;
$files = Folder::files($this->getState('to_path'), '.ini', true, true);
$newName = strtolower($this->getState('new_name'));
$template = $this->getTemplate();
$oldName = $template->element;
$manifest = json_decode($template->manifest_cache);
foreach ($files as $file)
{
$newFile = '/' . str_replace($oldName, $newName, basename($file));
$result = File::move($file, dirname($file) . $newFile) && $result;
}
// Edit XML file
$xmlFile = $this->getState('to_path') . '/templateDetails.xml';
if (File::exists($xmlFile))
{
$contents = file_get_contents($xmlFile);
$pattern[] = '#<name>\s*' . $manifest->name . '\s*</name>#i';
$replace[] = '<name>' . $newName . '</name>';
$pattern[] = '#<language(.*)' . $oldName . '(.*)</language>#';
$replace[] = '<language${1}' . $newName . '${2}</language>';
$contents = preg_replace($pattern, $replace, $contents);
$result = File::write($xmlFile, $contents) && $result;
}
return $result;
}
/**
* Method to get the record form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return Form|boolean A Form object on success, false on failure
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
$app = Factory::getApplication();
// Codemirror or Editor None should be enabled
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('COUNT(*)')
->from('#__extensions as a')
->where(
'(a.name =' . $db->quote('plg_editors_codemirror') .
' AND a.enabled = 1) OR (a.name =' .
$db->quote('plg_editors_none') .
' AND a.enabled = 1)'
);
$db->setQuery($query);
$state = $db->loadResult();
if ((int) $state < 1)
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED'), 'warning');
}
// Get the form.
$form = $this->loadForm('com_templates.source', 'source', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
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->getSource();
$this->preprocessData('com_templates.source', $data);
return $data;
}
/**
* Method to get a single record.
*
* @return mixed Object on success, false on failure.
*
* @since 1.6
*/
public function &getSource()
{
$app = Factory::getApplication();
$item = new \stdClass;
if (!$this->template)
{
$this->getTemplate();
}
if ($this->template)
{
$input = Factory::getApplication()->input;
$fileName = base64_decode($input->get('file'));
$client = ApplicationHelper::getClientInfo($this->template->client_id);
try
{
$filePath = Path::check($client->path . '/templates/' . $this->template->element . '/' . $fileName);
}
catch (\Exception $e)
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND'), 'error');
return;
}
if (file_exists($filePath))
{
$item->extension_id = $this->getState('extension.id');
$item->filename = Path::clean($fileName);
$item->source = file_get_contents($filePath);
$item->filePath = Path::clean($filePath);
if ($coreFile = $this->getCoreFile($fileName, $this->template->client_id))
{
$item->coreFile = $coreFile;
$item->core = file_get_contents($coreFile);
}
}
else
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND'), 'error');
}
}
return $item;
}
/**
* Method to store the source file contents.
*
* @param array $data The source data to save.
*
* @return boolean True on success, false otherwise and internal error set.
*
* @since 1.6
*/
public function save($data)
{
// Get the template.
$template = $this->getTemplate();
if (empty($template))
{
return false;
}
$app = Factory::getApplication();
$fileName = base64_decode($app->input->get('file'));
$client = ApplicationHelper::getClientInfo($template->client_id);
$filePath = Path::clean($client->path . '/templates/' . $template->element . '/' . $fileName);
// Include the extension plugins for the save events.
PluginHelper::importPlugin('extension');
$user = get_current_user();
chown($filePath, $user);
Path::setPermissions($filePath, '0644');
// Try to make the template file writable.
if (!is_writable($filePath))
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_WRITABLE'), 'warning');
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_FILE_PERMISSIONS', Path::getPermissions($filePath)), 'warning');
if (!Path::isOwner($filePath))
{
$app->enqueueMessage(Text::_('COM_TEMPLATES_CHECK_FILE_OWNERSHIP'), 'warning');
}
return false;
}
// Make sure EOL is Unix
$data['source'] = str_replace(array("\r\n", "\r"), "\n", $data['source']);
$return = File::write($filePath, $data['source']);
if (!$return)
{
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME', $fileName), 'error');
return false;
}
// Get the extension of the changed file.
$explodeArray = explode('.', $fileName);
$ext = end($explodeArray);
if ($ext == 'less')
{
$app->enqueueMessage(Text::sprintf('COM_TEMPLATES_COMPILE_LESS', $fileName));
}