lildude / phpSmug

:camera: phpSmug is a simple object orientated wrapper for the new SmugMug API v2, written in PHP.
https://lildude.github.io/phpSmug
MIT License
42 stars 19 forks source link

Undefined offset in Client.php __call() caused by empty URI arguments #43

Closed danielmreck closed 6 years ago

danielmreck commented 6 years ago

Hello,

When certain unusual URI queries are passed to phpSmug, we sometimes receive the following error:

PHP Notice: Undefined offset: 1 in /vendor/lildude/phpsmug/lib/phpSmug/Client.php on line 139

This is caused when contiguous '&' characters appear in the query statement, effectively creating empty arguments, as seen here between arg1 and arg2:

...?arg1=value1&&arg2=value2

The PHP notice can be corrected by stripping out these empty arguments, by inserting the following at line 137 in phpsmug/lib/phpSmug/Client.php, just before the foreach loop: $pairs = array_diff($pairs,['']);

Here is the context:

# Cater for any args passed in via `?whatever=foo`
if (strpos($url, '?') !== false) {
  $pairs = explode('&', explode('?', $url)[1]);
  $pairs = array_diff($pairs,['']);   // remove empty args caused by adjacent &s in $url
  foreach ($pairs as $pair) {
    list($key, $value) = explode('=', $pair);
    $this->request_options['query'][$key] = $value;
  }
}

I'm sorry I can't formally fork and unit test this right now, although this fix is working in my project.

lildude commented 6 years ago

Ooof, nice catch. I didn’t consider double &. Should be a simple change. I’ll whip up a PR when I’ve got a free moment.

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity in 14 days. It will be closed in 2 days if no further activity occurs. Thank you for your contributions.

lildude commented 6 years ago

Down stalebot.