This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
Automator.php
781 lines (624 loc) · 19.3 KB
/
Automator.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
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2015 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao;
/**
* Provide methods to run automated jobs.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class Automator extends \System
{
/**
* Make the constuctor public
*/
public function __construct()
{
parent::__construct();
}
/**
* Check for new \Contao versions
*/
public function checkForUpdates()
{
if (!is_numeric(BUILD))
{
return;
}
// HOOK: proxy module
if (Config::get('useProxy')) {
$objRequest = new \ProxyRequest();
} else {
$objRequest = new \Request();
}
$objRequest->send(\Config::get('liveUpdateBase') . (LONG_TERM_SUPPORT ? 'lts-version.txt' : 'version.txt'));
if (!$objRequest->hasError())
{
\Config::set('latestVersion', $objRequest->response);
\Config::persist('latestVersion', $objRequest->response);
}
// Add a log entry
$this->log('Checked for Contao updates', __METHOD__, TL_CRON);
}
/**
* Purge the search tables
*/
public function purgeSearchTables()
{
$objDatabase = \Database::getInstance();
// Truncate the tables
$objDatabase->execute("TRUNCATE TABLE tl_search");
$objDatabase->execute("TRUNCATE TABLE tl_search_index");
// Purge the cache folder
$objFolder = new \Folder('system/cache/search');
$objFolder->purge();
// Add a log entry
$this->log('Purged the search tables', __METHOD__, TL_CRON);
}
/**
* Purge the undo table
*/
public function purgeUndoTable()
{
$objDatabase = \Database::getInstance();
// Truncate the table
$objDatabase->execute("TRUNCATE TABLE tl_undo");
// Add a log entry
$this->log('Purged the undo table', __METHOD__, TL_CRON);
}
/**
* Purge the version table
*/
public function purgeVersionTable()
{
$objDatabase = \Database::getInstance();
// Truncate the table
$objDatabase->execute("TRUNCATE TABLE tl_version");
// Add a log entry
$this->log('Purged the version table', __METHOD__, TL_CRON);
}
/**
* Purge the system log
*/
public function purgeSystemLog()
{
$objDatabase = \Database::getInstance();
// Truncate the table
$objDatabase->execute("TRUNCATE TABLE tl_log");
// Add a log entry
$this->log('Purged the system log', __METHOD__, TL_CRON);
}
/**
* Purge the image cache
*/
public function purgeImageCache()
{
// Walk through the subfolders
foreach (scan(TL_ROOT . '/assets/images') as $dir)
{
if ($dir != 'index.html' && strncmp($dir, '.', 1) !== 0)
{
// Purge the folder
$objFolder = new \Folder('assets/images/' . $dir);
$objFolder->purge();
// Restore the index.html file
$objFile = new \File('templates/index.html', true);
$objFile->copyTo('assets/images/' . $dir . '/index.html');
}
}
// Also empty the page cache so there are no links to deleted images
$this->purgePageCache();
// Add a log entry
$this->log('Purged the image cache', __METHOD__, TL_CRON);
}
/**
* Purge the script cache
*/
public function purgeScriptCache()
{
// assets/js and assets/css
foreach (array('assets/js', 'assets/css') as $dir)
{
// Purge the folder
$objFolder = new \Folder($dir);
$objFolder->purge();
// Restore the index.html file
$objFile = new \File('templates/index.html', true);
$objFile->copyTo($dir . '/index.html');
}
// Recreate the internal style sheets
$this->import('StyleSheets');
$this->StyleSheets->updateStylesheets();
// Also empty the page cache so there are no links to deleted scripts
$this->purgePageCache();
// Add a log entry
$this->log('Purged the script cache', __METHOD__, TL_CRON);
}
/**
* Purge the page cache
*/
public function purgePageCache()
{
// Purge the folder
$objFolder = new \Folder('system/cache/html');
$objFolder->purge();
// Add a log entry
$this->log('Purged the page cache', __METHOD__, TL_CRON);
}
/**
* Purge the search cache
*/
public function purgeSearchCache()
{
// Purge the folder
$objFolder = new \Folder('system/cache/search');
$objFolder->purge();
// Add a log entry
$this->log('Purged the search cache', __METHOD__, TL_CRON);
}
/**
* Purge the internal cache
*/
public function purgeInternalCache()
{
// Check whether the cache exists
if (is_dir(TL_ROOT . '/system/cache/dca'))
{
foreach (array('config', 'dca', 'language', 'sql') as $dir)
{
// Purge the folder
$objFolder = new \Folder('system/cache/' . $dir);
$objFolder->delete();
}
}
// Add a log entry
$this->log('Purged the internal cache', __METHOD__, TL_CRON);
}
/**
* Purge the temp folder
*/
public function purgeTempFolder()
{
// Purge the folder
$objFolder = new \Folder('system/tmp');
$objFolder->purge();
// Restore the .gitignore file
$objFile = new \File('system/logs/.gitignore', true);
$objFile->copyTo('system/tmp/.gitignore');
// Add a log entry
$this->log('Purged the temp folder', __METHOD__, TL_CRON);
}
/**
* Regenerate the XML files
*/
public function generateXmlFiles()
{
// Sitemaps
$this->generateSitemap();
// HOOK: add custom jobs
if (isset($GLOBALS['TL_HOOKS']['generateXmlFiles']) && is_array($GLOBALS['TL_HOOKS']['generateXmlFiles']))
{
foreach ($GLOBALS['TL_HOOKS']['generateXmlFiles'] as $callback)
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}();
}
}
// Also empty the page cache so there are no links to deleted files
$this->purgePageCache();
// Add a log entry
$this->log('Regenerated the XML files', __METHOD__, TL_CRON);
}
/**
* Remove old XML files from the share directory
*
* @param boolean $blnReturn If true, only return the finds and don't delete
*
* @return array An array of old XML files
*/
public function purgeXmlFiles($blnReturn=false)
{
$arrFeeds = array();
$objDatabase = \Database::getInstance();
// XML sitemaps
$objFeeds = $objDatabase->execute("SELECT sitemapName FROM tl_page WHERE type='root' AND createSitemap=1 AND sitemapName!=''");
while ($objFeeds->next())
{
$arrFeeds[] = $objFeeds->sitemapName;
}
// HOOK: preserve third party feeds
if (isset($GLOBALS['TL_HOOKS']['removeOldFeeds']) && is_array($GLOBALS['TL_HOOKS']['removeOldFeeds']))
{
foreach ($GLOBALS['TL_HOOKS']['removeOldFeeds'] as $callback)
{
$this->import($callback[0]);
$arrFeeds = array_merge($arrFeeds, $this->{$callback[0]}->{$callback[1]}());
}
}
// Delete the old files
if (!$blnReturn)
{
foreach (scan(TL_ROOT . '/share') as $file)
{
if (is_dir(TL_ROOT . '/share/' . $file))
{
continue; // see #6652
}
$objFile = new \File('share/' . $file, true);
if ($objFile->extension == 'xml' && !in_array($objFile->filename, $arrFeeds))
{
$objFile->delete();
}
}
}
return $arrFeeds;
}
/**
* Generate the Google XML sitemaps
*
* @param integer $intId The root page ID
*/
public function generateSitemap($intId=0)
{
$time = \Date::floorToMinute();
$objDatabase = \Database::getInstance();
$this->purgeXmlFiles();
// Only root pages should have sitemap names
$objDatabase->execute("UPDATE tl_page SET createSitemap='', sitemapName='' WHERE type!='root'");
// Get a particular root page
if ($intId > 0)
{
do
{
$objRoot = $objDatabase->prepare("SELECT * FROM tl_page WHERE id=?")
->limit(1)
->execute($intId);
if ($objRoot->numRows < 1)
{
break;
}
$intId = $objRoot->pid;
}
while ($objRoot->type != 'root' && $intId > 0);
// Make sure the page is published
if (!$objRoot->published || ($objRoot->start != '' && $objRoot->start > $time) || ($objRoot->stop != '' && $objRoot->stop <= ($time + 60)))
{
return;
}
// Check the sitemap name
if (!$objRoot->createSitemap || !$objRoot->sitemapName)
{
return;
}
$objRoot->reset();
}
// Get all published root pages
else
{
$objRoot = $objDatabase->execute("SELECT id, dns, language, useSSL, sitemapName FROM tl_page WHERE type='root' AND createSitemap='1' AND sitemapName!='' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1'");
}
// Return if there are no pages
if ($objRoot->numRows < 1)
{
return;
}
// Create the XML file
while ($objRoot->next())
{
$objFile = new \File('share/' . $objRoot->sitemapName . '.xml', true);
$objFile->truncate();
$objFile->append('<?xml version="1.0" encoding="UTF-8"?>');
$objFile->append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">');
// Set the domain (see #6421)
$strDomain = ($objRoot->useSSL ? 'https://' : 'http://') . ($objRoot->dns ?: \Environment::get('host')) . TL_PATH . '/';
// Find the searchable pages
$arrPages = \Backend::findSearchablePages($objRoot->id, $strDomain, true, $objRoot->language);
// HOOK: take additional pages
if (isset($GLOBALS['TL_HOOKS']['getSearchablePages']) && is_array($GLOBALS['TL_HOOKS']['getSearchablePages']))
{
foreach ($GLOBALS['TL_HOOKS']['getSearchablePages'] as $callback)
{
$this->import($callback[0]);
$arrPages = $this->{$callback[0]}->{$callback[1]}($arrPages, $objRoot->id, true, $objRoot->language);
}
}
// Add pages
foreach ($arrPages as $strUrl)
{
$strUrl = rawurlencode($strUrl);
$strUrl = str_replace(array('%2F', '%3F', '%3D', '%26', '%3A//'), array('/', '?', '=', '&', '://'), $strUrl);
$strUrl = ampersand($strUrl, true);
$objFile->append(' <url><loc>' . $strUrl . '</loc></url>');
}
$objFile->append('</urlset>');
$objFile->close();
// Add a log entry
$this->log('Generated sitemap "' . $objRoot->sitemapName . '.xml"', __METHOD__, TL_CRON);
}
}
/**
* Rotate the log files
*/
public function rotateLogs()
{
$arrFiles = preg_grep('/\.log$/', scan(TL_ROOT . '/system/logs'));
foreach ($arrFiles as $strFile)
{
$objFile = new \File('system/logs/' . $strFile . '.9', true);
// Delete the oldest file
if ($objFile->exists())
{
$objFile->delete();
}
// Rotate the files (e.g. error.log.4 becomes error.log.5)
for ($i=8; $i>0; $i--)
{
$strGzName = 'system/logs/' . $strFile . '.' . $i;
if (file_exists(TL_ROOT . '/' . $strGzName))
{
$objFile = new \File($strGzName, true);
$objFile->renameTo('system/logs/' . $strFile . '.' . ($i+1));
}
}
// Add .1 to the latest file
$objFile = new \File('system/logs/' . $strFile, true);
$objFile->renameTo('system/logs/' . $strFile . '.1');
}
}
/**
* Build the internal cache
*/
public function generateInternalCache()
{
// Purge
$this->purgeInternalCache();
// Rebuild
$this->generateConfigCache();
$this->generateDcaCache();
$this->generateLanguageCache();
$this->generateDcaExtracts();
}
/**
* Create the config cache files
*/
public function generateConfigCache()
{
// Generate the class/template laoder cache file
$objCacheFile = new \File('system/cache/config/autoload.php', true);
$objCacheFile->write('<?php '); // add one space to prevent the "unexpected $end" error
foreach (\ModuleLoader::getActive() as $strModule)
{
$strFile = 'system/modules/' . $strModule . '/config/autoload.php';
if (file_exists(TL_ROOT . '/' . $strFile))
{
$objCacheFile->append(static::readPhpFileWithoutTags($strFile));
}
}
// Close the file (moves it to its final destination)
$objCacheFile->close();
// Generate the module loader cache file
$objCacheFile = new \File('system/cache/config/modules.php', true);
$objCacheFile->write("<?php\n\n");
$objCacheFile->append(sprintf("static::\$active = %s;\n", var_export(\ModuleLoader::getActive(), true)));
$objCacheFile->append(sprintf("static::\$disabled = %s;", var_export(\ModuleLoader::getDisabled(), true)));
// Close the file (moves it to its final destination)
$objCacheFile->close();
// Generate the config cache file
$objCacheFile = new \File('system/cache/config/config.php', true);
$objCacheFile->write('<?php '); // add one space to prevent the "unexpected $end" error
foreach (\ModuleLoader::getActive() as $strModule)
{
$strFile = 'system/modules/' . $strModule . '/config/config.php';
if (file_exists(TL_ROOT . '/' . $strFile))
{
$objCacheFile->append(static::readPhpFileWithoutTags($strFile));
}
}
// Close the file (moves it to its final destination)
$objCacheFile->close();
// Generate the page mapping array
$arrMapper = array();
$objPages = \PageModel::findPublishedRootPages();
if ($objPages !== null)
{
while ($objPages->next())
{
$strBase = ($objPages->dns ?: '*');
if ($objPages->fallback)
{
$arrMapper[$strBase . '/empty.fallback'] = $strBase . '/empty.' . $objPages->language;
}
$arrMapper[$strBase . '/empty.' . $objPages->language] = $strBase . '/empty.' . $objPages->language;
}
}
// Generate the page mapper file
$objCacheFile = new \File('system/cache/config/mapping.php', true);
$objCacheFile->write(sprintf("<?php\n\nreturn %s;\n", var_export($arrMapper, true)));
$objCacheFile->close();
// Add a log entry
$this->log('Generated the config cache', __METHOD__, TL_CRON);
}
/**
* Create the data container cache files
*/
public function generateDcaCache()
{
$arrFiles = array();
// Parse all active modules
foreach (\ModuleLoader::getActive() as $strModule)
{
$strDir = 'system/modules/' . $strModule . '/dca';
if (!is_dir(TL_ROOT . '/' . $strDir))
{
continue;
}
foreach (scan(TL_ROOT . '/' . $strDir) as $strFile)
{
if (strncmp($strFile, '.', 1) !== 0 && substr($strFile, -4) == '.php')
{
$arrFiles[] = substr($strFile, 0, -4);
}
}
}
$arrFiles = array_values(array_unique($arrFiles));
// Create one file per table
foreach ($arrFiles as $strName)
{
// Generate the cache file
$objCacheFile = new \File('system/cache/dca/' . $strName . '.php', true);
$objCacheFile->write('<?php '); // add one space to prevent the "unexpected $end" error
// Parse all active modules
foreach (\ModuleLoader::getActive() as $strModule)
{
$strFile = 'system/modules/' . $strModule . '/dca/' . $strName . '.php';
if (file_exists(TL_ROOT . '/' . $strFile))
{
$objCacheFile->append(static::readPhpFileWithoutTags($strFile));
}
}
// Close the file (moves it to its final destination)
$objCacheFile->close();
}
// Add a log entry
$this->log('Generated the DCA cache', __METHOD__, TL_CRON);
}
/**
* Create the language cache files
*/
public function generateLanguageCache()
{
$arrLanguages = array('en');
$objLanguages = \Database::getInstance()->query("SELECT language FROM tl_member UNION SELECT language FROM tl_user UNION SELECT REPLACE(language, '-', '_') FROM tl_page WHERE type='root'");
// Only cache the languages which are in use (see #6013)
while ($objLanguages->next())
{
if ($objLanguages->language == '')
{
continue;
}
$arrLanguages[] = $objLanguages->language;
// Also cache "de" if "de-CH" is requested
if (strlen($objLanguages->language) > 2)
{
$arrLanguages[] = substr($objLanguages->language, 0, 2);
}
}
$arrLanguages = array_unique($arrLanguages);
foreach ($arrLanguages as $strLanguage)
{
$arrFiles = array();
// Parse all active modules
foreach (\ModuleLoader::getActive() as $strModule)
{
$strDir = 'system/modules/' . $strModule . '/languages/' . $strLanguage;
if (!is_dir(TL_ROOT . '/' . $strDir))
{
continue;
}
foreach (scan(TL_ROOT . '/' . $strDir) as $strFile)
{
if (strncmp($strFile, '.', 1) !== 0 && (substr($strFile, -4) == '.php' || substr($strFile, -4) == '.xlf'))
{
$arrFiles[] = substr($strFile, 0, -4);
}
}
}
$arrFiles = array_values(array_unique($arrFiles));
// Create one file per table
foreach ($arrFiles as $strName)
{
$strCacheFile = 'system/cache/language/' . $strLanguage . '/' . $strName . '.php';
// Add a short header with links to transifex.com
$strHeader = "<?php\n\n"
. "/**\n"
. " * Contao Open Source CMS\n"
. " * \n"
. " * Copyright (c) 2005-2015 Leo Feyer\n"
. " * \n"
. " * Core translations are managed using Transifex. To create a new translation\n"
. " * or to help to maintain an existing one, please register at transifex.com.\n"
. " * \n"
. " * @link http://help.transifex.com/intro/translating.html\n"
. " * @link https://www.transifex.com/projects/p/contao/language/%s/\n"
. " * \n"
. " * @license LGPL-3.0+\n"
. " */\n";
// Generate the cache file
$objCacheFile = new \File($strCacheFile, true);
$objCacheFile->write(sprintf($strHeader, $strLanguage));
// Parse all active modules and append to the cache file
foreach (\ModuleLoader::getActive() as $strModule)
{
$strFile = 'system/modules/' . $strModule . '/languages/' . $strLanguage . '/' . $strName;
if (file_exists(TL_ROOT . '/' . $strFile . '.xlf'))
{
$objCacheFile->append(static::convertXlfToPhp($strFile . '.xlf', $strLanguage));
}
elseif (file_exists(TL_ROOT . '/' . $strFile . '.php'))
{
$objCacheFile->append(static::readPhpFileWithoutTags($strFile . '.php'));
}
}
// Close the file (moves it to its final destination)
$objCacheFile->close();
}
}
// Add a log entry
$this->log('Generated the language cache', __METHOD__, TL_CRON);
}
/**
* Create the DCA extract cache files
*/
public function generateDcaExtracts()
{
$included = array();
$arrExtracts = array();
// Only check the active modules (see #4541)
foreach (\ModuleLoader::getActive() as $strModule)
{
$strDir = 'system/modules/' . $strModule . '/dca';
if (!is_dir(TL_ROOT . '/' . $strDir))
{
continue;
}
foreach (scan(TL_ROOT . '/' . $strDir) as $strFile)
{
// Ignore non PHP files and files which have been included before
if (strncmp($strFile, '.', 1) === 0 || substr($strFile, -4) != '.php' || in_array($strFile, $included))
{
continue;
}
$strTable = substr($strFile, 0, -4);
$objExtract = \DcaExtractor::getInstance($strTable);
if ($objExtract->isDbTable())
{
$arrExtracts[$strTable] = $objExtract;
}
$included[] = $strFile;
}
}
/** @var \DcaExtractor[] $arrExtracts */
foreach ($arrExtracts as $strTable=>$objExtract)
{
// Create the file
$objFile = new \File('system/cache/sql/' . $strTable . '.php', true);
$objFile->write("<?php\n\n");
$objFile->append(sprintf("\$this->arrMeta = %s;\n", var_export($objExtract->getMeta(), true)));
$objFile->append(sprintf("\$this->arrFields = %s;\n", var_export($objExtract->getFields(), true)));
$objFile->append(sprintf("\$this->arrOrderFields = %s;\n", var_export($objExtract->getOrderFields(), true)));
$objFile->append(sprintf("\$this->arrKeys = %s;\n", var_export($objExtract->getKeys(), true)));
$objFile->append(sprintf("\$this->arrRelations = %s;\n", var_export($objExtract->getRelations(), true)));
// Set the database table flag
$objFile->append("\$this->blnIsDbTable = true;", "\n");
// Close the file (moves it to its final destination)
$objFile->close();
}
// Add a log entry
$this->log('Generated the DCA extracts', __METHOD__, TL_CRON);
}
}