summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStein Magne Bjorklund2017-07-13 21:00:16 (GMT)
committerStein Magne Bjorklund2017-07-13 21:00:16 (GMT)
commitc0b6980975ba1528fba1d8788a14918bb0f4ae33 (patch)
tree046c934b6c2789b3532a5f61e30ede69da902295
parent628d70fa3f906c43e3e5decc783f7bedcd44a0e0 (diff)
Support flickr short URL formHEAD7.x-2.0-alpha57.x-2.x
Flickr introduced a short URL form. Default format for sharing photos and albums.
-rw-r--r--includes/media_flickr.utilities.inc71
1 files changed, 58 insertions, 13 deletions
diff --git a/includes/media_flickr.utilities.inc b/includes/media_flickr.utilities.inc
index 924f2a9..a7d9ecd 100644
--- a/includes/media_flickr.utilities.inc
+++ b/includes/media_flickr.utilities.inc
@@ -200,7 +200,7 @@ function _media_flickr_photoset_exists($photoset_id) {
}
/**
- * Find user ID based on Flickr user name.
+ * Find Flickr user ID.
*
* @param array $parts
*
@@ -212,8 +212,12 @@ function _media_flickr_get_user_id($parts) {
if (isset($parts['p'])) {
$media_id = $parts['p'];
- $media_info = _media_flickr_sets_request('flickr.photos.getInfo',
- ['photo_id' => $media_id]);
+ $media_info = _media_flickr_sets_request(
+ 'flickr.photos.getInfo',
+ [
+ 'photo_id' => $media_id,
+ ]
+ );
if ($media_info['@attributes']['stat'] != 'fail') {
$user_id = $media_info['photo']['owner']['@attributes']['nsid'];
}
@@ -232,21 +236,20 @@ function _media_flickr_get_user_id($parts) {
}
/**
- * @param $parts
+ * Build the streamwrapper URI if the remote media exists.
+
+ * @param array $parts
*
- * @return string
+ * @return string $uri
*/
function _media_flickr_build_uri($parts) {
- $user_name = '';
- if (isset($parts['u'])) {
- $user_name = $parts['u'];
- $user_id = _media_flickr_get_user_id($parts);
- if ($user_id) {
- $parts['u'] = isset($user_id) ? $user_id : $parts['u'];
- }
+ $user_id = _media_flickr_get_user_id($parts);
+
+ if ($user_id) {
+ $parts['u'] = isset($user_id) ? $user_id : $parts['u'];
}
- if ($user_name) {
+ if ($parts['u']) {
if (isset($parts['p'])) {
$uri = file_stream_wrapper_uri_normalize('flickr://u/' . $parts['u'] . "/p/" . $parts['p']);
if (_media_flickr_photo_exists($parts['p'])) {
@@ -276,6 +279,23 @@ function _media_flickr_build_uri($parts) {
* The unique URI for the file, based on its stream wrapper, or NULL.
*/
function _media_flickr_media_parse($url) {
+ // Flickr short form URL.
+ $patterns = array('@flic\.kr/(p|s)/([0-9A-Za-z]+)@i');
+ foreach ($patterns as $pattern) {
+ preg_match($pattern, $url, $matches);
+ if (isset($matches[1])) {
+ $long_id = _media_flickr_shorturl($matches[2]);
+ if ($long_id) {
+ if ($matches[1] == 'p') {
+ return _media_flickr_build_uri(array('p' => $long_id));
+ }
+ else {
+ return _media_flickr_build_uri(array('s' => $long_id));
+ }
+ }
+ }
+ }
+
// The following are single images.
$patterns = array(
'@flickr\.com/photos/([0-9A-Za-z\@\&\$_-]+)/([0-9]+)/in/photostream@i',
@@ -326,3 +346,28 @@ function _media_flickr_media_parse($url) {
}
}
}
+
+/**
+ * Convert Flickr short form ID to long form. Short form is a base58
+ * with a reduced alphabet.
+ *
+ * @param $num string
+ * ID parsed from short URL form.
+ *
+ * @return int
+ * Full media ID.
+ */
+function _media_flickr_shorturl($num) {
+ $alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
+ $decoded = 0;
+ $multi = 1;
+
+ while (strlen($num) > 0) {
+ $digit = $num[strlen($num)-1];
+ $decoded += $multi * strpos($alphabet, $digit);
+ $multi = $multi * strlen($alphabet);
+ $num = substr($num, 0, -1);
+ }
+
+ return $decoded;
+}