Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pinc/RandomRule.inc
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,31 @@ class RandomRule
// delete all existing random rules for this ($document, $langcode)
RandomRule::delete_rules($document, $langcode);

if (($fhandle = fopen($url, "rt")) === false) {
// basic HTTP contexts for the fopen -- mostly a placeholder for
// customization in other branches who want to set specific headers
$context_opts = [
'http' => [
'method' => "GET",
],
];

$context = stream_context_create($context_opts);
if (($fhandle = fopen($url, "rt", context: $context)) === false) {
throw new RuntimeException(sprintf(_("Failed to load rules from %s."), $url));
}

$meta_data = stream_get_meta_data($fhandle);
$status_line = $meta_data["wrapper_data"][0] ?? "";
if (! preg_match("/HTTP\/\S+\s+200\b/", $status_line)) {
throw new RuntimeException(
sprintf(
"%s %s",
sprintf(_("Failed to load rules from %s."), $url),
$status_line
)
);
}

$anchor = $subject = $rule = "";
$in_rule = false;
while ($line = fgets($fhandle)) {
Expand Down
11 changes: 8 additions & 3 deletions tools/site_admin/manage_random_rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@

// do POST actions
if ($action == 'update') {
RandomRule::reload_rules($url, $document, $langcode);
// TRANSLATORS: %3$s is a URL
echo "<p class='warning'>" . sprintf(_('Loaded rules for document %1$s and language %2$s from %3$s.'), RandomRule::$document_values[$document], $langcode, $url) . "</p>";
try {
RandomRule::reload_rules($url, $document, $langcode);

// TRANSLATORS: %3$s is a URL
echo "<p class='warning'>" . sprintf(_('Loaded rules for document %1$s and language %2$s from %3$s.'), RandomRule::$document_values[$document], $langcode, $url) . "</p>";
} catch (RuntimeException $exception) {
echo "<p class='error'>" . $exception->getMessage() . "</p>";
}
} elseif ($action == 'delete') {
RandomRule::delete_rules($document, $langcode);
echo "<p class='warning'>" . sprintf(_('Deleted rules for document %1$s and language %2$s.'), RandomRule::$document_values[$document], $langcode) . "</p>";
Expand Down