-
-
Save Stanton/599db38634c9ba2418d0 to your computer and use it in GitHub Desktop.
Image licence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Image license | |
* | |
* Used to check for a specific machine tag which allows us to display appropriate | |
* image attribution. | |
* | |
* Usage: | |
* | |
* <?php print imageLicense($imageURL); ?> | |
* | |
* If this becomes useful, I may abstract it further into a general machine tag | |
* parser, but for now I'm stopping myself from over-engineering things. | |
* | |
* NOTE: | |
* This requires JaduMultumediaItemTags.tag to be set to varchar(255) | |
* Jadu have made this change for us in the 1.10 trunk, but we'll need to patch any | |
* older instances with the following query. | |
* | |
* ALTER TABLE JaduMultimediaItemTags CHANGE tag tag varchar(255); | |
* | |
* @author Paul Stanton <p.m.s.stanton@leeds.ac.uk> | |
* | |
* @param string $imageURL The image filename. | |
* @return string HTML string containing marked-up license data. | |
*/ | |
function imageLicense($image_url) { | |
// Make sure that we have access to the JaduMultimediaItems class | |
require_once('multimedia/JaduMultimediaItems.php'); | |
/* Retrieve the JaduMultimediaItems.id (which is different to the JaduImages.id | |
of the same item). */ | |
$image_id = getImageProperty($image_url, 'id'); | |
// Grab the multimedia object based on the imageID we just got. | |
$image_object = getMultimediaItemByImageID($image_id); | |
// Grab the image's tags from JaduMultimediaItemTags. | |
$tags = $image_object->getTags(); | |
/* | |
Set up a pattern to match the following format: | |
namespace:predicate="value" | |
The namespace for image licenses will always be 'license', I'm currently | |
assuming a use case along the lines of: | |
license:creativecommons="(c) 2009 Greg Grossmeier, used under a Creative | |
Commons Attribution-ShareAlike license: | |
http://creativecommons.org/licenses/by-sa/3.0/" | |
(note, as Jadu doesn't really convert htmlentities properly, we can't use the | |
raw © symbol, as this gets removed on saving to the database, nor can we use | |
© as this gets converted to © the next time the record is viewed in the | |
control center, then stripped on save. To get around this we'll convert any | |
instances of '(c)' to © in a bit. | |
The predicate will, currently, be one of two values: | |
* copyright | |
* creativecommons | |
This allows us to display appropriate (c) or (cc) image dependant on the | |
license used. | |
*/ | |
$license_pattern = '/license:([\w]+)="(.+)"/'; | |
// Perform the regex match, populating $matches with the values. | |
preg_match($license_pattern, $tags, $matches); | |
// Grab our required values from the machine tag. | |
$license_type = $matches[1]; // predicate. | |
$license_string = $matches[2]; // value. | |
// If either value is empty, fail. | |
if (empty($license_type) || empty($license_string)) { | |
return false; | |
} | |
// Otherwise we're good to go, pass the values to the HTML helper. | |
return imageLicenseToHtml($license_type, $license_string); | |
} | |
/** | |
* HTML helper for image license function. | |
* | |
* @param string $license_type The license type (durr). | |
* @param string $license_string The actual license data. | |
* @return string An HTML string ready for the browser. | |
*/ | |
function imageLicenseToHtml($license_type = '', $license_string = '') { | |
// Grubers' Liberal URL-matching regex. | |
$url_pattern = | |
'#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#i'; | |
// Wrap any match in anchor text, using backreferences. | |
$url_replacement = | |
'<a href="${1}" rel="license nofollow">${1}</a>'; | |
// Perform the regex replacement, making links clickable. | |
$license_string = preg_replace( | |
$url_pattern, | |
$url_replacement, | |
$license_string | |
); | |
// Replace any instance of (c) or (C) with the correct HTML entity. | |
$license_string = str_ireplace('(c)', '©', $license_string); | |
// Build the output string. | |
switch ($license_type) { | |
case 'creativecommons': | |
$output = '<p xmlns:dct="http://purl.org/dc/terms/" | |
xmlns:cc="http://creativecommons.org/ns#">' . | |
$license_string . '</p>'; | |
break; | |
default: | |
$output = '<p>' . $license_string . '</p>'; | |
break; | |
} | |
/* Finally, wrap it up in a div, bit crude, but compartmentalises it from the | |
rest of the code around Jadu */ | |
$output = '<div class="license ' . $license_type . '">' . $output . "</div>"; | |
// Send it on it's merry way. | |
return $output; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment