1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15 class StaticExporter extends Controller {
16 function init() {
17 parent::init();
18
19 $canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
20 if(!$canAccess) return Security::permissionFailure($this);
21 }
22
23
24 function Link($action = null) {
25 return "StaticExporter/$action";
26 }
27
28 function index() {
29 echo "<h1>"._t('StaticExporter.NAME','Static exporter')."</h1>";
30 echo $this->StaticExportForm()->forTemplate();
31 }
32
33 function StaticExportForm() {
34 return new Form($this, 'StaticExportForm', new FieldSet(
35
36 new TextField('baseurl', _t('StaticExporter.BASEURL','Base URL'))
37 ), new FieldSet(
38 new FormAction('export', _t('StaticExporter.EXPORTTO','Export to that folder'))
39 ));
40 }
41
42 function export() {
43
44 if(isset($_REQUEST['baseurl'])) {
45 $base = $_REQUEST['baseurl'];
46 if(substr($base,-1) != '/') $base .= '/';
47 Director::setBaseURL($base);
48 }
49
50
51 $tmpBaseFolder = TEMP_FOLDER . '/static-export';
52 $tmpFolder = (project()) ? "$tmpBaseFolder/" . project() : "$tmpBaseFolder/site";
53 if(!file_exists($tmpFolder)) Filesystem::makeFolder($tmpFolder);
54 $baseFolderName = basename($tmpFolder);
55
56
57 $f1 = ASSETS_PATH;
58 $f2 = Director::baseFolder() . '/' . project();
59 `cd $tmpFolder; ln -s $f1; ln -s $f2`;
60
61
62 $pages = DataObject::get("SiteTree");
63 foreach($pages as $page) {
64 $subfolder = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/');
65 $contentfile = "$tmpFolder/" . trim($page->RelativeLink(null, true), '/') . '/index.html';
66
67
68 if(!file_exists($subfolder)) {
69 Filesystem::makeFolder($subfolder);
70 }
71
72
73 Requirements::clear();
74 $link = Director::makeRelative($page->Link());
75 $response = Director::test($link);
76
77
78 if($fh = fopen($contentfile, 'w')) {
79 fwrite($fh, $response->getBody());
80 fclose($fh);
81 }
82 }
83
84
85 copy("$tmpFolder/home/index.html", "$tmpFolder/index.html");
86
87
88 `cd $tmpBaseFolder; tar -czhf $baseFolderName.tar.gz $baseFolderName`;
89 $archiveContent = file_get_contents("$tmpBaseFolder/$baseFolderName.tar.gz");
90
91
92 Filesystem::removeFolder($tmpBaseFolder);
93
94
95 $response = SS_HTTPRequest::send_file($archiveContent, "$baseFolderName.tar.gz", 'application/x-tar-gz');
96 echo $response->output();
97 }
98
99 }
100
101 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-