@@ -7,20 +7,20 @@ |
||
| 7 | 7 | class CwpHtmlEditorConfig extends DataExtension |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @return string |
|
| 12 | - * |
|
| 13 | - * Override the default HtmlEditorConfig from 'cms' to 'cwp' defined in cwp-core/_config.php |
|
| 14 | - * However if the group has a custom editor configuration set, use that instead. |
|
| 15 | - */ |
|
| 16 | - public function getHtmlEditorConfig() |
|
| 17 | - { |
|
| 18 | - $originalConfig = $this->owner->getField("HtmlEditorConfig"); |
|
| 10 | + /** |
|
| 11 | + * @return string |
|
| 12 | + * |
|
| 13 | + * Override the default HtmlEditorConfig from 'cms' to 'cwp' defined in cwp-core/_config.php |
|
| 14 | + * However if the group has a custom editor configuration set, use that instead. |
|
| 15 | + */ |
|
| 16 | + public function getHtmlEditorConfig() |
|
| 17 | + { |
|
| 18 | + $originalConfig = $this->owner->getField("HtmlEditorConfig"); |
|
| 19 | 19 | |
| 20 | - if ($originalConfig) { |
|
| 21 | - return $originalConfig; |
|
| 22 | - } |
|
| 20 | + if ($originalConfig) { |
|
| 21 | + return $originalConfig; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - return 'cwp'; |
|
| 25 | - } |
|
| 24 | + return 'cwp'; |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -53,12 +53,12 @@ discard block |
||
| 53 | 53 | $failures = $meantimeLoginAttempts->filter(['Status' => 'Failure'])->count(); |
| 54 | 54 | $IPs = array_unique($meantimeLoginAttempts->column('IP')); |
| 55 | 55 | |
| 56 | - if ($attempts == 1) { |
|
| 56 | + if ($attempts==1) { |
|
| 57 | 57 | $statusString = $failures ? "a failed" : "a successful"; |
| 58 | 58 | $message = "In the last $elapsed $statusString login attempt to your account was " |
| 59 | 59 | . "registered. The attempt was made from ${IPs[0]}. "; |
| 60 | 60 | } else { |
| 61 | - if ($failures == $attempts) { |
|
| 61 | + if ($failures==$attempts) { |
|
| 62 | 62 | $statusString = $failures ? "failed" : "successful"; |
| 63 | 63 | $message = "In the last $elapsed $attempts $statusString login " |
| 64 | 64 | . "attempts to your account were registered. "; |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | $date = $lastLoginAttempt->Created; |
| 86 | 86 | $message = "Last login attempt to your account was on $lastLoginAttempt->Created " |
| 87 | 87 | . "from $lastLoginAttempt->IP"; |
| 88 | - $message .= $lastLoginAttempt->Status == 'Failure' ? " and was successful." : "and has failed."; |
|
| 88 | + $message .= $lastLoginAttempt->Status=='Failure' ? " and was successful." : "and has failed."; |
|
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | |
@@ -19,81 +19,81 @@ |
||
| 19 | 19 | class LoginAttemptNotifications extends Extension |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * |
|
| 24 | - * @return mixed null |
|
| 25 | - */ |
|
| 26 | - public function init() |
|
| 27 | - { |
|
| 22 | + /** |
|
| 23 | + * |
|
| 24 | + * @return mixed null |
|
| 25 | + */ |
|
| 26 | + public function init() |
|
| 27 | + { |
|
| 28 | 28 | |
| 29 | - // Exclude default admin. |
|
| 30 | - $member = Security::getCurrentUser(); |
|
| 31 | - if (!$member || !$member->ID) { |
|
| 32 | - return; |
|
| 33 | - } |
|
| 29 | + // Exclude default admin. |
|
| 30 | + $member = Security::getCurrentUser(); |
|
| 31 | + if (!$member || !$member->ID) { |
|
| 32 | + return; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - $message = null; |
|
| 36 | - $session = $this->owner->getRequest()->getSession(); |
|
| 35 | + $message = null; |
|
| 36 | + $session = $this->owner->getRequest()->getSession(); |
|
| 37 | 37 | |
| 38 | - Requirements::javascript('cwp/cwp-core:javascript/LoginAttemptNotifications.js'); |
|
| 39 | - $sessionLastVisited = $session->get('LoginAttemptNotifications.SessionLastVisited'); |
|
| 40 | - if ($sessionLastVisited) { |
|
| 41 | - // Session already in progress. Show all attempts since the session was last visited. |
|
| 38 | + Requirements::javascript('cwp/cwp-core:javascript/LoginAttemptNotifications.js'); |
|
| 39 | + $sessionLastVisited = $session->get('LoginAttemptNotifications.SessionLastVisited'); |
|
| 40 | + if ($sessionLastVisited) { |
|
| 41 | + // Session already in progress. Show all attempts since the session was last visited. |
|
| 42 | 42 | |
| 43 | - $meantimeLoginAttempts = LoginAttempt::get()->filter([ |
|
| 44 | - 'MemberID' => $member->ID, |
|
| 45 | - 'Created:GreaterThan' => $sessionLastVisited |
|
| 46 | - ]); |
|
| 43 | + $meantimeLoginAttempts = LoginAttempt::get()->filter([ |
|
| 44 | + 'MemberID' => $member->ID, |
|
| 45 | + 'Created:GreaterThan' => $sessionLastVisited |
|
| 46 | + ]); |
|
| 47 | 47 | |
| 48 | - $attempts = $meantimeLoginAttempts->count(); |
|
| 49 | - if ($attempts) { |
|
| 50 | - $lastVisitedObj = DBDatetime::create(); |
|
| 51 | - $lastVisitedObj->setValue($sessionLastVisited); |
|
| 52 | - $elapsed = $lastVisitedObj->TimeDiff(); |
|
| 53 | - $failures = $meantimeLoginAttempts->filter(['Status' => 'Failure'])->count(); |
|
| 54 | - $IPs = array_unique($meantimeLoginAttempts->column('IP')); |
|
| 48 | + $attempts = $meantimeLoginAttempts->count(); |
|
| 49 | + if ($attempts) { |
|
| 50 | + $lastVisitedObj = DBDatetime::create(); |
|
| 51 | + $lastVisitedObj->setValue($sessionLastVisited); |
|
| 52 | + $elapsed = $lastVisitedObj->TimeDiff(); |
|
| 53 | + $failures = $meantimeLoginAttempts->filter(['Status' => 'Failure'])->count(); |
|
| 54 | + $IPs = array_unique($meantimeLoginAttempts->column('IP')); |
|
| 55 | 55 | |
| 56 | - if ($attempts == 1) { |
|
| 57 | - $statusString = $failures ? "a failed" : "a successful"; |
|
| 58 | - $message = "In the last $elapsed $statusString login attempt to your account was " |
|
| 59 | - . "registered. The attempt was made from ${IPs[0]}. "; |
|
| 60 | - } else { |
|
| 61 | - if ($failures == $attempts) { |
|
| 62 | - $statusString = $failures ? "failed" : "successful"; |
|
| 63 | - $message = "In the last $elapsed $attempts $statusString login " |
|
| 64 | - . "attempts to your account were registered. "; |
|
| 65 | - } else { |
|
| 66 | - $message = "In the last $elapsed $attempts login attempts to your " |
|
| 67 | - . "account were registered, of which $failures failed. "; |
|
| 68 | - } |
|
| 56 | + if ($attempts == 1) { |
|
| 57 | + $statusString = $failures ? "a failed" : "a successful"; |
|
| 58 | + $message = "In the last $elapsed $statusString login attempt to your account was " |
|
| 59 | + . "registered. The attempt was made from ${IPs[0]}. "; |
|
| 60 | + } else { |
|
| 61 | + if ($failures == $attempts) { |
|
| 62 | + $statusString = $failures ? "failed" : "successful"; |
|
| 63 | + $message = "In the last $elapsed $attempts $statusString login " |
|
| 64 | + . "attempts to your account were registered. "; |
|
| 65 | + } else { |
|
| 66 | + $message = "In the last $elapsed $attempts login attempts to your " |
|
| 67 | + . "account were registered, of which $failures failed. "; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - $message .= "The attempts were from " . implode(', ', $IPs) . '. '; |
|
| 70 | + $message .= "The attempts were from " . implode(', ', $IPs) . '. '; |
|
| 71 | 71 | |
| 72 | - // TODO: add this call to action in a way that doesn't break out of the availabel space. Fix CSS? |
|
| 73 | - // $message .= "If you suspect somebody else might be trying to access |
|
| 74 | - // . "your account, please contact support."; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - // New session - show last login attempt. |
|
| 79 | - // TODO: this currently does NOT surface to the frontend in any way. |
|
| 80 | - $lastLoginAttempt = LoginAttempt::get()->filter([ |
|
| 81 | - 'MemberID' => $member->ID |
|
| 82 | - ])->sort('Created DESC')->First(); |
|
| 72 | + // TODO: add this call to action in a way that doesn't break out of the availabel space. Fix CSS? |
|
| 73 | + // $message .= "If you suspect somebody else might be trying to access |
|
| 74 | + // . "your account, please contact support."; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + // New session - show last login attempt. |
|
| 79 | + // TODO: this currently does NOT surface to the frontend in any way. |
|
| 80 | + $lastLoginAttempt = LoginAttempt::get()->filter([ |
|
| 81 | + 'MemberID' => $member->ID |
|
| 82 | + ])->sort('Created DESC')->First(); |
|
| 83 | 83 | |
| 84 | - if ($lastLoginAttempt) { |
|
| 85 | - $date = $lastLoginAttempt->Created; |
|
| 86 | - $message = "Last login attempt to your account was on $lastLoginAttempt->Created " |
|
| 87 | - . "from $lastLoginAttempt->IP"; |
|
| 88 | - $message .= $lastLoginAttempt->Status == 'Failure' ? " and was successful." : "and has failed."; |
|
| 89 | - } |
|
| 90 | - } |
|
| 84 | + if ($lastLoginAttempt) { |
|
| 85 | + $date = $lastLoginAttempt->Created; |
|
| 86 | + $message = "Last login attempt to your account was on $lastLoginAttempt->Created " |
|
| 87 | + . "from $lastLoginAttempt->IP"; |
|
| 88 | + $message .= $lastLoginAttempt->Status == 'Failure' ? " and was successful." : "and has failed."; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - $session->set( |
|
| 93 | - 'LoginAttemptNotifications.SessionLastVisited', |
|
| 94 | - DBDatetime::now()->Format(DBDatetime::ISO_DATETIME) |
|
| 95 | - ); |
|
| 92 | + $session->set( |
|
| 93 | + 'LoginAttemptNotifications.SessionLastVisited', |
|
| 94 | + DBDatetime::now()->Format(DBDatetime::ISO_DATETIME) |
|
| 95 | + ); |
|
| 96 | 96 | |
| 97 | - $this->owner->getResponse()->addHeader('X-LoginAttemptNotifications', $message); |
|
| 98 | - } |
|
| 97 | + $this->owner->getResponse()->addHeader('X-LoginAttemptNotifications', $message); |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -18,57 +18,57 @@ |
||
| 18 | 18 | |
| 19 | 19 | class CwpAtomFeed extends RSSFeed |
| 20 | 20 | { |
| 21 | - public function __construct( |
|
| 22 | - SS_List $entries, |
|
| 23 | - $link, |
|
| 24 | - $title, |
|
| 25 | - $description = null, |
|
| 26 | - $titleField = "Title", |
|
| 27 | - $descriptionField = "Content", |
|
| 28 | - $authorField = null, |
|
| 29 | - $lastModified = null, |
|
| 30 | - $etag = null |
|
| 31 | - ) { |
|
| 32 | - parent::__construct( |
|
| 33 | - $entries, |
|
| 34 | - $link, |
|
| 35 | - $title, |
|
| 36 | - $description, |
|
| 37 | - $titleField, |
|
| 38 | - $descriptionField, |
|
| 39 | - $authorField, |
|
| 40 | - $lastModified |
|
| 41 | - ); |
|
| 21 | + public function __construct( |
|
| 22 | + SS_List $entries, |
|
| 23 | + $link, |
|
| 24 | + $title, |
|
| 25 | + $description = null, |
|
| 26 | + $titleField = "Title", |
|
| 27 | + $descriptionField = "Content", |
|
| 28 | + $authorField = null, |
|
| 29 | + $lastModified = null, |
|
| 30 | + $etag = null |
|
| 31 | + ) { |
|
| 32 | + parent::__construct( |
|
| 33 | + $entries, |
|
| 34 | + $link, |
|
| 35 | + $title, |
|
| 36 | + $description, |
|
| 37 | + $titleField, |
|
| 38 | + $descriptionField, |
|
| 39 | + $authorField, |
|
| 40 | + $lastModified |
|
| 41 | + ); |
|
| 42 | 42 | |
| 43 | - $this->setTemplate(__CLASS__); |
|
| 44 | - } |
|
| 43 | + $this->setTemplate(__CLASS__); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Include an link to the feed |
|
| 48 | - * |
|
| 49 | - * @param string $url URL of the feed |
|
| 50 | - * @param string $title Title to show |
|
| 51 | - */ |
|
| 52 | - public static function linkToFeed($url, $title = null) |
|
| 53 | - { |
|
| 54 | - $title = Convert::raw2xml($title); |
|
| 55 | - Requirements::insertHeadTags( |
|
| 56 | - '<link rel="alternate" type="application/atom+xml" title="' . $title . |
|
| 57 | - '" href="' . $url . '" />' |
|
| 58 | - ); |
|
| 59 | - } |
|
| 46 | + /** |
|
| 47 | + * Include an link to the feed |
|
| 48 | + * |
|
| 49 | + * @param string $url URL of the feed |
|
| 50 | + * @param string $title Title to show |
|
| 51 | + */ |
|
| 52 | + public static function linkToFeed($url, $title = null) |
|
| 53 | + { |
|
| 54 | + $title = Convert::raw2xml($title); |
|
| 55 | + Requirements::insertHeadTags( |
|
| 56 | + '<link rel="alternate" type="application/atom+xml" title="' . $title . |
|
| 57 | + '" href="' . $url . '" />' |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Output the feed to the browser |
|
| 63 | - * |
|
| 64 | - * @return DBHTMLText |
|
| 65 | - */ |
|
| 66 | - public function outputToBrowser() |
|
| 67 | - { |
|
| 68 | - $output = parent::outputToBrowser(); |
|
| 69 | - $response = Controller::curr()->getResponse(); |
|
| 70 | - $response->addHeader("Content-Type", "application/atom+xml"); |
|
| 61 | + /** |
|
| 62 | + * Output the feed to the browser |
|
| 63 | + * |
|
| 64 | + * @return DBHTMLText |
|
| 65 | + */ |
|
| 66 | + public function outputToBrowser() |
|
| 67 | + { |
|
| 68 | + $output = parent::outputToBrowser(); |
|
| 69 | + $response = Controller::curr()->getResponse(); |
|
| 70 | + $response->addHeader("Content-Type", "application/atom+xml"); |
|
| 71 | 71 | |
| 72 | - return $output; |
|
| 73 | - } |
|
| 72 | + return $output; |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -16,48 +16,48 @@ |
||
| 16 | 16 | class RichLinksExtension extends Extension |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - private static $casting = [ |
|
| 23 | - 'RichLinks' => 'HTMLText' |
|
| 24 | - ]; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function RichLinks() |
|
| 30 | - { |
|
| 31 | - // Note: |
|
| 32 | - // Assume we can use Regexes because the link will always be formatted |
|
| 33 | - // in the same way coming from the CMS. |
|
| 34 | - |
|
| 35 | - $content = $this->owner->value; |
|
| 36 | - |
|
| 37 | - // Find all file links for processing. |
|
| 38 | - preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches); |
|
| 39 | - |
|
| 40 | - // Attach the file type and size to each of the links. |
|
| 41 | - for ($i = 0; $i < count($matches[0]); $i++) { |
|
| 42 | - $file = DataObject::get_by_id('File', $matches[1][$i]); |
|
| 43 | - if ($file) { |
|
| 44 | - $size = $file->getSize(); |
|
| 45 | - $ext = strtoupper($file->getExtension()); |
|
| 46 | - // Replace the closing </a> tag with the size span (and reattach the closing tag). |
|
| 47 | - $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4) |
|
| 48 | - . "<span class='fileExt'> [$ext, $size]</span></a>"; |
|
| 49 | - $content = str_replace($matches[0][$i], $newLink, $content); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // Inject extra attributes into the external links. |
|
| 54 | - $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU'; |
|
| 55 | - $replacement = sprintf( |
|
| 56 | - '$1class="external" rel="external" title="%s" $2<span class="nonvisual-indicator">(external link)</span>$3', |
|
| 57 | - _t(__CLASS__ . '.OpenLinkTitle', 'Open external link') |
|
| 58 | - ); |
|
| 59 | - $content = preg_replace($pattern, $replacement, $content, -1); |
|
| 60 | - |
|
| 61 | - return $content; |
|
| 62 | - } |
|
| 19 | + /** |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + private static $casting = [ |
|
| 23 | + 'RichLinks' => 'HTMLText' |
|
| 24 | + ]; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function RichLinks() |
|
| 30 | + { |
|
| 31 | + // Note: |
|
| 32 | + // Assume we can use Regexes because the link will always be formatted |
|
| 33 | + // in the same way coming from the CMS. |
|
| 34 | + |
|
| 35 | + $content = $this->owner->value; |
|
| 36 | + |
|
| 37 | + // Find all file links for processing. |
|
| 38 | + preg_match_all('/<a.*href="\[file_link,id=([0-9]+)\].*".*>.*<\/a>/U', $content, $matches); |
|
| 39 | + |
|
| 40 | + // Attach the file type and size to each of the links. |
|
| 41 | + for ($i = 0; $i < count($matches[0]); $i++) { |
|
| 42 | + $file = DataObject::get_by_id('File', $matches[1][$i]); |
|
| 43 | + if ($file) { |
|
| 44 | + $size = $file->getSize(); |
|
| 45 | + $ext = strtoupper($file->getExtension()); |
|
| 46 | + // Replace the closing </a> tag with the size span (and reattach the closing tag). |
|
| 47 | + $newLink = substr($matches[0][$i], 0, strlen($matches[0][$i]) - 4) |
|
| 48 | + . "<span class='fileExt'> [$ext, $size]</span></a>"; |
|
| 49 | + $content = str_replace($matches[0][$i], $newLink, $content); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // Inject extra attributes into the external links. |
|
| 54 | + $pattern = '/(<a.*)(href=\"https?:\/\/[^\"]*\"[^>]*>.*)(<\/a>)/iU'; |
|
| 55 | + $replacement = sprintf( |
|
| 56 | + '$1class="external" rel="external" title="%s" $2<span class="nonvisual-indicator">(external link)</span>$3', |
|
| 57 | + _t(__CLASS__ . '.OpenLinkTitle', 'Open external link') |
|
| 58 | + ); |
|
| 59 | + $content = preg_replace($pattern, $replacement, $content, -1); |
|
| 60 | + |
|
| 61 | + return $content; |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -12,97 +12,97 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class InitialisationMiddleware implements HTTPMiddleware |
| 14 | 14 | { |
| 15 | - use Configurable; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Disable the automatically added 'X-XSS-Protection' header that is added to all responses. This should be left |
|
| 19 | - * alone in most circumstances to include the header. Refer to Mozilla Developer Network for more information: |
|
| 20 | - * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection |
|
| 21 | - * |
|
| 22 | - * @config |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - private static $xss_protection_disabled = false; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
|
| 29 | - * which will be automatically picked up by curl. This means RestfulService and raw curl |
|
| 30 | - * requests should work out of the box. Stream-based requests need extra manual configuration. |
|
| 31 | - * Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
|
| 32 | - * |
|
| 33 | - * @config |
|
| 34 | - * @var bool |
|
| 35 | - */ |
|
| 36 | - private static $egress_proxy_default_enabled = true; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
|
| 40 | - * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
|
| 41 | - * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
|
| 42 | - * |
|
| 43 | - * @config |
|
| 44 | - * @var string[] |
|
| 45 | - */ |
|
| 46 | - private static $egress_proxy_exclude_domains = [ |
|
| 47 | - 'services.cwp.govt.nz', |
|
| 48 | - 'localhost', |
|
| 49 | - ]; |
|
| 50 | - |
|
| 51 | - public function process(HTTPRequest $request, callable $delegate) |
|
| 52 | - { |
|
| 53 | - $response = $delegate($request); |
|
| 54 | - |
|
| 55 | - if ($this->config()->get('egress_proxy_default_enabled')) { |
|
| 56 | - $this->configureEgressProxy(); |
|
| 57 | - } |
|
| 15 | + use Configurable; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Disable the automatically added 'X-XSS-Protection' header that is added to all responses. This should be left |
|
| 19 | + * alone in most circumstances to include the header. Refer to Mozilla Developer Network for more information: |
|
| 20 | + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection |
|
| 21 | + * |
|
| 22 | + * @config |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + private static $xss_protection_disabled = false; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Enable egress proxy. This works on the principle of setting http(s)_proxy environment variables, |
|
| 29 | + * which will be automatically picked up by curl. This means RestfulService and raw curl |
|
| 30 | + * requests should work out of the box. Stream-based requests need extra manual configuration. |
|
| 31 | + * Refer to https://www.cwp.govt.nz/guides/core-technical-documentation/common-web-platform-core/en/how-tos/external_http_requests_with_proxy |
|
| 32 | + * |
|
| 33 | + * @config |
|
| 34 | + * @var bool |
|
| 35 | + */ |
|
| 36 | + private static $egress_proxy_default_enabled = true; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Configure the list of domains to bypass proxy by setting the NO_PROXY environment variable. |
|
| 40 | + * 'services.cwp.govt.nz' needs to be present for Solr and Docvert internal CWP integration. |
|
| 41 | + * 'localhost' is necessary for accessing services on the same instance such as tika-server for text extraction. |
|
| 42 | + * |
|
| 43 | + * @config |
|
| 44 | + * @var string[] |
|
| 45 | + */ |
|
| 46 | + private static $egress_proxy_exclude_domains = [ |
|
| 47 | + 'services.cwp.govt.nz', |
|
| 48 | + 'localhost', |
|
| 49 | + ]; |
|
| 50 | + |
|
| 51 | + public function process(HTTPRequest $request, callable $delegate) |
|
| 52 | + { |
|
| 53 | + $response = $delegate($request); |
|
| 54 | + |
|
| 55 | + if ($this->config()->get('egress_proxy_default_enabled')) { |
|
| 56 | + $this->configureEgressProxy(); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - $this->configureProxyDomainExclusions(); |
|
| 60 | - |
|
| 61 | - if (!$this->config()->get('xss_protection_disabled') && $response) { |
|
| 62 | - $response->addHeader('X-XSS-Protection', '1; mode=block'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - return $response; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
| 70 | - * variables that are used to configure it. |
|
| 71 | - */ |
|
| 72 | - protected function configureEgressProxy() |
|
| 73 | - { |
|
| 74 | - if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 75 | - || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
| 76 | - ) { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
| 81 | - $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 82 | - |
|
| 83 | - Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
|
| 84 | - Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
| 89 | - */ |
|
| 90 | - protected function configureProxyDomainExclusions() |
|
| 91 | - { |
|
| 92 | - $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
| 93 | - if (empty($noProxy)) { |
|
| 94 | - return; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - if (!is_array($noProxy)) { |
|
| 98 | - $noProxy = [$noProxy]; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Merge with exsiting if needed. |
|
| 102 | - if (Environment::getEnv('NO_PROXY')) { |
|
| 103 | - $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
|
| 107 | - } |
|
| 59 | + $this->configureProxyDomainExclusions(); |
|
| 60 | + |
|
| 61 | + if (!$this->config()->get('xss_protection_disabled') && $response) { |
|
| 62 | + $response->addHeader('X-XSS-Protection', '1; mode=block'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + return $response; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * If the outbound egress proxy details have been defined in environment variables, configure the proxy |
|
| 70 | + * variables that are used to configure it. |
|
| 71 | + */ |
|
| 72 | + protected function configureEgressProxy() |
|
| 73 | + { |
|
| 74 | + if (!Environment::getEnv('SS_OUTBOUND_PROXY') |
|
| 75 | + || !Environment::getEnv('SS_OUTBOUND_PROXY_PORT') |
|
| 76 | + ) { |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $proxy = Environment::getEnv('SS_OUTBOUND_PROXY'); |
|
| 81 | + $proxyPort = Environment::getEnv('SS_OUTBOUND_PROXY_PORT'); |
|
| 82 | + |
|
| 83 | + Environment::setEnv('http_proxy', $proxy . ':' . $proxyPort); |
|
| 84 | + Environment::setEnv('https_proxy', $proxy . ':' . $proxyPort); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Configure any domains that should be excluded from egress proxy rules and provide them to the environment |
|
| 89 | + */ |
|
| 90 | + protected function configureProxyDomainExclusions() |
|
| 91 | + { |
|
| 92 | + $noProxy = $this->config()->get('egress_proxy_exclude_domains'); |
|
| 93 | + if (empty($noProxy)) { |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + if (!is_array($noProxy)) { |
|
| 98 | + $noProxy = [$noProxy]; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Merge with exsiting if needed. |
|
| 102 | + if (Environment::getEnv('NO_PROXY')) { |
|
| 103 | + $noProxy = array_merge(explode(',', Environment::getEnv('NO_PROXY')), $noProxy); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + Environment::setEnv('NO_PROXY', implode(',', array_unique($noProxy))); |
|
| 107 | + } |
|
| 108 | 108 | } |