diff --git a/core/Core.php b/core/Core.php index f925d49e7..5b231b871 100755 --- a/core/Core.php +++ b/core/Core.php @@ -91,6 +91,13 @@ if(!isset($_SERVER['HTTP_HOST'])) { if($_GET) stripslashes_recursively($_GET); if($_POST) stripslashes_recursively($_POST); } + + /** + * Fix HTTP_HOST from reverse proxies + */ + if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { + $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; + } } /** diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index c65ddaff3..0fe5d55c5 100755 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -1341,6 +1341,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } } + echo "=======\nUPDATING LINK TRACKING FOR $this->Title\n=========\n"; + SS_Backtrace::backtrace(); $this->LinkTracking()->setByIDList($linkedPages); $this->ImageTracking()->setByIDList($linkedFiles); @@ -1505,29 +1507,29 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid */ function rewriteFileURL($old, $new) { $fields = $this->inheritedDatabaseFields(); - foreach($fields as $fieldName => $fieldType) { - if ($fieldType != 'HTMLText') continue; - - $original = clone $this; - - // Draft site - $this->$fieldName = str_replace($old, $new, $this->$fieldName, $numReplaced); - if($numReplaced) $this->write(); - + // Update the content without actually creating a new version + foreach(array("SiteTree_Live", "SiteTree") as $table) { // Published site - $published = DB::query("SELECT * FROM \"SiteTree_Live\" WHERE \"ID\" = $this->ID")->record(); + $published = DB::query("SELECT * FROM \"$table\" WHERE \"ID\" = $this->ID")->record(); $origPublished = $published; - - // TODO: This doesn't work for HTMLText fields on other tables. - if(isset($published[$fieldName])) { - $published[$fieldName] = str_replace($old, $new, $published[$fieldName], $numReplaced); - if($numReplaced) { - DB::query("UPDATE \"SiteTree_Live\" SET \"$fieldName\" = '" - . Convert::raw2sql($published[$fieldName]) . "' WHERE \"ID\" = $this->ID"); - $publishedClass = $origPublished['ClassName']; - $origPublishedObj = new $publishedClass($origPublished); - $this->extend('onRenameLinkedAsset', $origPublishedObj); + foreach($fields as $fieldName => $fieldType) { + if ($fieldType != 'HTMLText') continue; + + // TODO: This doesn't work for HTMLText fields on other tables. + if(isset($published[$fieldName])) { + $published[$fieldName] = str_replace($old, $new, $published[$fieldName], $numReplaced); + if($numReplaced) { + DB::query("UPDATE \"$table\" SET \"$fieldName\" = '" + . Convert::raw2sql($published[$fieldName]) . "' WHERE \"ID\" = $this->ID"); + + // Tell static caching to update itself + if($table == 'SiteTree_Live') { + $publishedClass = $origPublished['ClassName']; + $origPublishedObj = new $publishedClass($origPublished); + $this->extend('onRenameLinkedAsset', $origPublishedObj); + } + } } } } diff --git a/filesystem/File.php b/filesystem/File.php index de628e021..0fa24a831 100755 --- a/filesystem/File.php +++ b/filesystem/File.php @@ -51,7 +51,15 @@ class File extends DataObject { * @see Upload->allowedExtensions * @var array */ - public static $allowed_extensions = array(); + public static $allowed_extensions = array( + 'html','htm','xhtml','js','css', + 'png','gif','jpg','jpeg', + 'mov','mkv','mp3','m4a', + 'doc','docx','txt','rtf','xls','xlsx','pages', + 'ppt','pptx','pps','csv', + 'zip','zipx','sit','sitx','gz','pkg','dmg','hqx', + 'xml','pdf', + ); /** * If this is true, then restrictions set in $allowed_max_file_size and @@ -269,14 +277,6 @@ class File extends DataObject { parent::onBeforeWrite(); if(!$this->Name) $this->Name = "new-" . strtolower($this->class); - - if($brokenPages = $this->BackLinkTracking()) { - foreach($brokenPages as $brokenPage) { - Notifications::event("BrokenLink", $brokenPage, $brokenPage->OwnerID); - $brokenPage->HasBrokenFile = true; - $brokenPage->write(); - } - } } /** @@ -633,7 +633,11 @@ class File extends DataObject { function validate() { if(!File::$apply_restrictions_to_admin && Permission::check('ADMIN')) { - if(!in_array(strtolower(pathinfo($this->Name, PATHINFO_EXTENSION)), self::$allowed_extensions)) { + $extension = strtolower(pathinfo($this->Name, PATHINFO_EXTENSION)); + + if($extension && !in_array($extension, self::$allowed_extensions)) { + $exts = self::$allowed_extensions; + sort($exts); $message = sprintf( _t( 'File.INVALIDEXTENSION', @@ -641,7 +645,7 @@ class File extends DataObject { PR_MEDIUM, 'Argument 1: Comma-separated list of valid extensions' ), - implode(',',self::$allowed_extensions) + implode(', ',$exts) ); return new ValidationResult(false, $message); } diff --git a/tests/FileLinkTrackingTest.php b/tests/FileLinkTrackingTest.php index 2b62211b7..287b54045 100644 --- a/tests/FileLinkTrackingTest.php +++ b/tests/FileLinkTrackingTest.php @@ -16,6 +16,7 @@ class FileLinkTrackingTest extends SapphireTest { $testFiles = array( '/assets/testscript-test-file.pdf', '/assets/renamed-test-file.pdf', + '/assets/renamed-test-file-second-time.pdf', ); foreach($testFiles as $file) { if(file_exists(Director::baseFolder().$file)) unlink(Director::baseFolder().$file); @@ -33,6 +34,8 @@ class FileLinkTrackingTest extends SapphireTest { $this->assertContains('', DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value()); + $this->assertContains('', + DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value()); } function testFileLinkRewritingOnVirtualPages() { @@ -55,7 +58,46 @@ class FileLinkTrackingTest extends SapphireTest { DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $svp->ID")->value()); $this->assertContains('', DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $svp->ID")->value()); - } + } + + function testLinkRewritingOnAPublishedPageDoesntMakeItEditedOnDraft() { + // Publish the source page + $page = $this->objFromFixture('Page', 'page1'); + $this->assertTrue($page->doPublish()); + $this->assertFalse($page->IsModifiedOnStage); + + // Rename the file + $file = $this->objFromFixture('File', 'file1'); + $file->Name = 'renamed-test-file.pdf'; + + // Caching hack + Versioned::prepopulate_versionnumber_cache('SiteTree', 'Stage', array($page->ID)); + Versioned::prepopulate_versionnumber_cache('SiteTree', 'Live', array($page->ID)); + + // Confirm that the page hasn't gone green. + $this->assertFalse($page->IsModifiedOnStage); + } + + function testTwoFileRenamesInARowWork() { + $page = $this->objFromFixture('Page', 'page1'); + $this->assertTrue($page->doPublish()); + $this->assertContains('', + DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value()); + + // Rename the file twice + $file = $this->objFromFixture('File', 'file1'); + $file->Name = 'renamed-test-file.pdf'; + $file->write(); + + $file->Name = 'renamed-test-file-second-time.pdf'; + $file->write(); + + // Confirm that the correct image is shown in both the draft and live site + $this->assertContains('', + DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value()); + $this->assertContains('', + DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value()); + } } ?>