From bc7b6bba89c46716723df7f80a471b8eb2945dae Mon Sep 17 00:00:00 2001 From: Larry Lewis Date: Sat, 31 Jan 2015 14:14:07 +1100 Subject: [PATCH] Allow prefix propogation --- permalinks.php | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/permalinks.php b/permalinks.php index 00a2254..4b97d40 100755 --- a/permalinks.php +++ b/permalinks.php @@ -72,9 +72,11 @@ function permalink_cms_onInstallCreateSchemas() { // dynamically modify permalink schema to so "http://HOSTNAME/" and "/" shows before and after permalink field function permalink_cms_onEditShowFieldPrefix($tableName, $recordNum) { if (!array_key_exists('permalink', $GLOBALS['schema'])) { return; } // skip for sections without permalink fields + $requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue']; + if( $requiredPrefix AND substr( $requiredPrefix, -1 ) != '/' ) $requiredPrefix .= '/'; // update permalink prefix/suffix to show current hostname (whatever that may be) - $GLOBALS['schema']['permalink']['fieldPrefix'] = "http://" . htmlencode( $_SERVER['HTTP_HOST'] ) . PREFIX_URL . '/'; + $GLOBALS['schema']['permalink']['fieldPrefix'] = "http://" . htmlencode( $_SERVER['HTTP_HOST'] ) . PREFIX_URL . '/' . $requiredPrefix; $GLOBALS['schema']['permalink']['description'] = '/'; } @@ -85,10 +87,8 @@ function permalink_cms_onPreSaveAutopopulate($tableName, $isNewRecord, $oldRecor if ($tableName == '_permalinks') { return; } // skip when admin is directly editing permalinks table // is autopopulate needed? - $permalinkText = @$_REQUEST['permalink']; - $requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue']; - $doAutopopulate = ($permalinkText == '' || $permalinkText == $requiredPrefix); - if (!$doAutopopulate) { return; } + $permalinkText = trim( @$_REQUEST['permalink'] ); + if($permalinkText != '') { return; } // get auto populate text $autopopulateText = ''; @@ -102,7 +102,7 @@ function permalink_cms_onPreSaveAutopopulate($tableName, $isNewRecord, $oldRecor } // get permalink text - $autopopulateText = _permalink_generatePermalink( $inputText, $requiredPrefix ); + $autopopulateText = _permalink_generatePermalink( $inputText ); if ($autopopulateText != '') { break; } // stop as soon as we have something } if ($autopopulateText == '') { return; } // if we couldn't auto-generate anything then return (the user will get regular errors to either fill in title or permalink) @@ -200,9 +200,10 @@ function permalink_cms_onSaveErrorChecking($tableName, $recordExists, $oldRecord // error checking $requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue']; + if( $requiredPrefix AND substr( $requiredPrefix, -1 ) != '/' ) $requiredPrefix .= '/'; if (!$permalinkText) { die("You must enter a value for 'permalink'!"); } - elseif ($permalinkText == $requiredPrefix) { die("You must enter a value for 'permalink' after '" .htmlencode($requiredPrefix). "'!"); } - elseif (!startsWith($requiredPrefix, $permalinkText)) { die("Permalink must start with '" .htmlencode($requiredPrefix). "'!"); } + //elseif ($permalinkText == $requiredPrefix) { die("You must enter a value for 'permalink' after '" .htmlencode($requiredPrefix). "'!"); } + //elseif (!startsWith($requiredPrefix, $permalinkText)) { die("Permalink must start with '" .htmlencode($requiredPrefix). "'!"); } elseif (preg_match("|[^\w\-\./]|i", $permalinkText)) { die("Permalink can only contain these chars (a-z0-9/-.)!"); } elseif (preg_match("!^/|/$!", $permalinkText)) { die("Permalink can not start or end with a slash!"); } elseif (preg_match("!\.{2,}!", $permalinkText)) { die("Permalink can not include '..'!"); } @@ -220,7 +221,9 @@ function permalink_cms_onSaveUpdatePermalinks($tableName, $isNewRecord, $oldReco if ($tableName == '_permalinks') { return; } // skip when admin is directly editing permalinks table // get permalink - $permalinkText = @$_REQUEST['permalink']; + $requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue']; + if( $requiredPrefix AND substr( $requiredPrefix, -1 ) != '/' ) $requiredPrefix .= '/'; + $permalinkText = $requiredPrefix . @$_REQUEST['permalink']; $permalinkRecord = mysql_get('_permalinks', null, array('permalink' => $permalinkText)); // check if permalink already exists in database $iaAlreadyUsed = $permalinkRecord && $permalinkRecord['recordNum'] != @$_REQUEST['num']; @@ -255,7 +258,12 @@ function permalink_viewers_updateLinks($rows, $listDetails, $schema) { // update _link to point to permalink foreach (array_keys($rows) as $index) { $row = &$rows[$index]; - if ($row['permalink']) { $row['_link'] = PREFIX_URL . '/' .$row['permalink']. '/'; } + if ($row['permalink']) + { + $requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue']; + if( $requiredPrefix AND substr( $requiredPrefix, -1 ) != '/' ) $requiredPrefix .= '/'; + $row['_link'] = strtolower( PREFIX_URL . '/' .$requiredPrefix.$row['permalink']. '/' ); + } unset($row); } -- 1.9.3 (Apple Git-50)