Skip to content

Commit

Permalink
MDL-49497 curl: Allow configurable User-Agent.
Browse files Browse the repository at this point in the history
Thanks to Kartik Yadav for assistance with this patch.
  • Loading branch information
Dave Cooper authored and stronk7 committed Jun 23, 2015
1 parent 6d59f63 commit 300fcae
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2683,7 +2683,8 @@ class curl {
public $emulateredirects = null;

/** @var array cURL options */
private $options;
protected $options;

/** @var string Proxy host */
private $proxy_host = '';
/** @var string Proxy auth */
Expand Down Expand Up @@ -2975,7 +2976,7 @@ private function formatHeader($ch, $header) {
* @param array $options
* @return resource The curl handle
*/
private function apply_opt($curl, $options) {
protected function apply_opt($curl, $options) {
// Clean up
$this->cleanopt();
// set cookie
Expand Down Expand Up @@ -3004,15 +3005,36 @@ private function apply_opt($curl, $options) {
}

$this->setopt($options);
// reset before set options

// Reset before set options.
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
// set headers

// Setting the User-Agent based on options provided.
$useragent = '';

if (!empty($options['CURLOPT_USERAGENT'])) {
$useragent = $options['CURLOPT_USERAGENT'];
} else if (!empty($this->options['CURLOPT_USERAGENT'])) {
$useragent = $this->options['CURLOPT_USERAGENT'];
} else {
$useragent = 'MoodleBot/1.0';
}

// Set headers.
if (empty($this->header)) {
$this->setHeader(array(
'User-Agent: MoodleBot/1.0',
'User-Agent: ' . $useragent,
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection: keep-alive'
));
} else if (!in_array('User-Agent: ' . $useragent, $this->header)) {
// Remove old User-Agent if one existed.
// We have to partial search since we don't know what the original User-Agent is.
if ($match = preg_grep('/User-Agent.*/', $this->header)) {
$key = array_keys($match)[0];
unset($this->header[$key]);
}
$this->setHeader(array('User-Agent: ' . $useragent));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);

Expand Down

0 comments on commit 300fcae

Please sign in to comment.