Skip to content

Add Retry Limit to Admin Settings #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: MOODLE_311_STABLE
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
defined('MOODLE_INTERNAL') || die();

/**
* The mod_modname module does not store any data.
* The filter_oembed module does not store any data.
*
*/
class provider implements \core_privacy\local\metadata\null_provider {
Expand Down
37 changes: 34 additions & 3 deletions classes/provider/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,39 @@ public function get_oembed_request($text) {
* @param string $url The consumer request URL.
* @return array JSON decoded array.
*/
public function oembed_response($url) {
$ret = download_file_content($url, null, null, true, 300, 20, false, null, false);
return json_decode($ret->results, true);
public function oembed_response($url, $retryno = 0) {
static $cache;

if (!isset($cache)) {
$cache = \cache::make('filter_oembed', 'embeddata');
}

if ($ret = $cache->get(md5($url))) {
return json_decode($ret, true);
}

$curl = new \curl();
$ret = $curl->get($url);

// Check if curl call fails.
if ($curl->errno == CURLE_OK) {
$cache->set(md5($url), $ret);
$result = json_decode($ret, true);
return $result;
}

$retrylimit = get_config('filter_oembed', 'retrylimit');
// Check if error is due to network connection.
if (!in_array($curl->errno, [6, 7, 28])) {
return null;
}
// Try curl call up to $retrylimit times.
usleep(50000);
$retryno = (!is_int($retryno)) ? 0 : $retryno + 1;
if ($retryno >= $retrylimit) {
return null;
}
return $this->oembed_response($url, $retryno);
}

/**
Expand Down Expand Up @@ -224,6 +254,7 @@ protected function endpoints_regex(endpoint $endpoint) {
$find = ['.', '*'];
$replace = ['\.', '.*?'];
$url = str_replace($find, $replace, $url);
$url = str_replace('.*??', '.*?\?', $url);
$regexarr[] = '(' . $url . ')';
}

Expand Down
31 changes: 30 additions & 1 deletion filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function filter($text, array $options = array()) {
return $text;
}

$filtered = $text; // We need to return the original value if regex fails!
$filtered = $this->embed_extra_mappings($text); // We need to return the original value if regex fails!
if ($targettag == 'divtag') {
$search = '/\<div\s[^\>]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/';
} else { // Using 'atag'.
Expand All @@ -84,6 +84,35 @@ public function filter($text, array $options = array()) {
}
}

private function embed_extra_mappings($html) {
$extra = get_config('filter_oembed', 'extra_mappings');
if (empty($extra)) {
return $html;
}

$mappings = explode("\n", trim($extra));
foreach ($mappings as $mapping) {
$mapping = trim($mapping);
$params = explode('=>', $mapping);
if (count($params) != 2) {
if ($params != '') { // Ignore empty lines.
debugging('Invalid extra mapping: '.$mapping);
}
continue;
}

$html = $this->embed_extra_mapping($html, trim($params[0]), trim($params[1]));
}

return $html;
}

private function embed_extra_mapping($html, $url, $endpoint) {
$search = '#<a\s[^>]*href="('.$url.')"(.*?)>(.*?)</a>#is'; // URL to Link filter will create <a> tags.
$html = preg_replace_callback($search, 'self::find_oembeds_callback', $html);
return $html;
}

/**
* Callback function to be used by the main filter
*
Expand Down
5 changes: 5 additions & 0 deletions lang/en/filter_oembed.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@
$string['targettag'] = 'Target tag';
$string['targettag_desc'] = 'What tag type should be filtered - anchors or divs with the oembed class.';
$string['updateproviders'] = 'Update Oembed provider information.';
$string['extramappings'] = 'Extra Mappings';
$string['extramappingsdescription'] = 'Format: <b>REGEX => ENDPOINT</b><br />Where <i>REGEX</i> is what to replace and <i>ENDPOINT</i> is where to fetch the JSON parameters for the embed.';
$string['privacy:metadata'] = 'Oembed filter does not store any personal data.';
$string['once'] = 'Once';
$string['retrylimit'] = 'Limit to retry getting filtered content';
$string['times'] = '{$a} times';
30 changes: 23 additions & 7 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,49 @@

use filter_oembed\service\oembed;

$ADMIN->add('filtersettings', new admin_category('filteroembedfolder', get_string('filtername', 'filter_oembed')));
$settings = new admin_settingpage($section, get_string('settings'));
$ADMIN->add('filtersettings', new admin_category('filteroembedfolder', new lang_string('filtername', 'filter_oembed')));
$settings = new admin_settingpage($section, new lang_string('settings'));

if ($ADMIN->fulltree) {
$targettags = [
'a' => get_string('atag', 'filter_oembed'),
'div' => get_string('divtag', 'filter_oembed')
'a' => new lang_string('atag', 'filter_oembed'),
'div' => new lang_string('divtag', 'filter_oembed')
];

$config = get_config('filter_oembed');

$item = new admin_setting_configtextarea(
'filter_oembed/extra_mappings',
get_string('extramappings', 'filter_oembed'),
get_string('extramappingsdescription', 'filter_oembed'),
'',
PARAM_RAW
);
$settings->add($item);

$item = new admin_setting_configselect(
'filter_oembed/targettag',
get_string('targettag', 'filter_oembed'),
get_string('targettag_desc', 'filter_oembed'),
new lang_string('targettag', 'filter_oembed'),
new lang_string('targettag_desc', 'filter_oembed'),
'atag',
['atag' => 'atag', 'divtag' => 'divtag']
);
$settings->add($item);

$retrylist = array('0' => new lang_string('none'), '1' => new lang_string('once', 'filter_oembed'),
'2' => new lang_string('times', 'filter_oembed', '2'),
'3' => new lang_string('times', 'filter_oembed', '3'));
$item = new admin_setting_configselect('filter_oembed/retrylimit',
new lang_string('retrylimit', 'filter_oembed'), '', '1', $retrylist);
$settings->add($item);

$item = new admin_setting_configcheckbox('filter_oembed/lazyload', new lang_string('lazyload', 'filter_oembed'), '', 1);
$settings->add($item);
}

$ADMIN->add('filteroembedfolder', $settings);

$ADMIN->add('filteroembedfolder', new admin_externalpage('filter_oembed_providers',
get_string('manageproviders', 'filter_oembed'), new moodle_url('/filter/oembed/manageproviders.php')));
new lang_string('manageproviders', 'filter_oembed'), new moodle_url('/filter/oembed/manageproviders.php')));

$settings = null;
Loading