diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..1fcccf9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "openphoto-php"] + path = openphoto-php + url = https://github.com/openphoto/openphoto-php diff --git a/README.txt b/README.txt index 9239027..43cb1c7 100644 --- a/README.txt +++ b/README.txt @@ -3,7 +3,7 @@ Contributors: randyjensen,randyhoyt Tags: openphoto,media Requires at least: 3.2 Tested up to: 3.3 -Stable tag: 0.9.3 +Stable tag: 0.9.4 Insert photos from your OpenPhoto installation into your WordPress content through the media manager. @@ -26,6 +26,11 @@ Find the project on [GitHub](https://github.com/openphoto/openphoto-wordpress "O == Changelog == += 0.9.4 = +* Including openphoto-php as a Git submodule +* Use title from OpenPhoto instead of filename +* Handle change in OpenPhoto API related to value of pathOriginal + = 0.9.3 = * Force image sizes to be generated so image src will persist. * Add option for image size referenced by the File URL button. diff --git a/openphoto-php/OAuthSimple.php b/openphoto-php/OAuthSimple.php deleted file mode 100644 index c510b8b..0000000 --- a/openphoto-php/OAuthSimple.php +++ /dev/null @@ -1,424 +0,0 @@ -ing uses OAuth without that minimal set of - * signatures. - * - * If you want to use the higher order security that comes from the - * OAuth token (sorry, I don't provide the functions to fetch that because - * sites aren't horribly consistent about how they offer that), you need to - * pass those in either with .signatures() or as an argument to the - * .sign() or .getHeaderString() functions. - * - * Example: - - sign(Array('path'=>'http://example.com/rest/', - 'parameters'=> 'foo=bar&gorp=banana', - 'signatures'=> Array( - 'api_key'=>'12345abcd', - 'shared_secret'=>'xyz-5309' - ))); - ?> - Some Link; - - * - * that will sign as a "GET" using "SHA1-MAC" the url. If you need more than - * that, read on, McDuff. - */ - - /** OAuthSimple creator - * - * Create an instance of OAuthSimple - * - * @param api_key {string} The API Key (sometimes referred to as the consumer key) This value is usually supplied by the site you wish to use. - * @param shared_secret (string) The shared secret. This value is also usually provided by the site you wish to use. - */ - function OAuthSimple ($APIKey = "",$sharedSecret=""){ - if (!empty($APIKey)) - $this->_secrets{'consumer_key'}=$APIKey; - if (!empty($sharedSecret)) - $this->_secrets{'shared_secret'}=$sharedSecret; - $this->_default_signature_method="HMAC-SHA1"; - $this->_action="GET"; - $this->_nonce_chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - return $this; - } - - /** reset the parameters and url - * - */ - function reset() { - $this->_parameters=null; - $this->path=null; - $this->sbs=null; - return $this; - } - - /** set the parameters either from a hash or a string - * - * @param {string,object} List of parameters for the call, this can either be a URI string (e.g. "foo=bar&gorp=banana" or an object/hash) - */ - function setParameters ($parameters=Array()) { - - if (is_string($parameters)) - $parameters = $this->_parseParameterString($parameters); - if (empty($this->_parameters)) - $this->_parameters = $parameters; - elseif (!empty($parameters)) - $this->_parameters = array_merge($this->_parameters,$parameters); - if (empty($this->_parameters['oauth_nonce'])) - $this->_getNonce(); - if (empty($this->_parameters['oauth_timestamp'])) - $this->_getTimeStamp(); - if (empty($this->_parameters['oauth_consumer_key'])) - $this->_getApiKey(); - if (empty($this->_parameters['oauth_token'])) - $this->_getAccessToken(); - if (empty($this->_parameters['oauth_signature_method'])) - $this->setSignatureMethod(); - if (empty($this->_parameters['oauth_version'])) - $this->_parameters['oauth_version']="1.0"; - //error_log('parameters: '.print_r($this,1)); - return $this; - } - - // convienence method for setParameters - function setQueryString ($parameters) { - return $this->setParameters($parameters); - } - - /** Set the target URL (does not include the parameters) - * - * @param path {string} the fully qualified URI (excluding query arguments) (e.g "http://example.org/foo") - */ - function setURL ($path) { - if (empty($path)) - throw new OAuthSimpleException('No path specified for OAuthSimple.setURL'); - $this->_path=$path; - return $this; - } - - /** convienence method for setURL - * - * @param path {string} see .setURL - */ - function setPath ($path) { - return $this->_path=$path; - } - - /** set the "action" for the url, (e.g. GET,POST, DELETE, etc.) - * - * @param action {string} HTTP Action word. - */ - function setAction ($action) { - if (empty($action)) - $action = 'GET'; - $action = strtoupper($action); - if (preg_match('/[^A-Z]/',$action)) - throw new OAuthSimpleException('Invalid action specified for OAuthSimple.setAction'); - $this->_action = $action; - return $this; - } - - /** set the signatures (as well as validate the ones you have) - * - * @param signatures {object} object/hash of the token/signature pairs {api_key:, shared_secret:, oauth_token: oauth_secret:} - */ - function signatures ($signatures) { - if (!empty($signatures) && !is_array($signatures)) - throw new OAuthSimpleException('Must pass dictionary array to OAuthSimple.signatures'); - if (!empty($signatures)){ - if (empty($this->_secrets)) { - $this->_secrets=Array(); - } - $this->_secrets=array_merge($this->_secrets,$signatures); - } - // Aliases - if (isset($this->_secrets['api_key'])) - $this->_secrets['consumer_key'] = $this->_secrets['api_key']; - if (isset($this->_secrets['access_token'])) - $this->_secrets['oauth_token'] = $this->_secrets['access_token']; - if (isset($this->_secrets['access_secret'])) - $this->_secrets['oauth_secret'] = $this->_secrets['access_secret']; - if (isset($this->_secrets['access_token_secret'])) - $this->_secrets['oauth_secret'] = $this->_secrets['access_token_secret']; - // Gauntlet - if (empty($this->_secrets['consumer_key'])) - throw new OAuthSimpleException('Missing required consumer_key in OAuthSimple.signatures'); - if (empty($this->_secrets['shared_secret'])) - throw new OAuthSimpleException('Missing requires shared_secret in OAuthSimple.signatures'); - if (!empty($this->_secrets['oauth_token']) && empty($this->_secrets['oauth_secret'])) - throw new OAuthSimpleException('Missing oauth_secret for supplied oauth_token in OAuthSimple.signatures'); - return $this; - } - - function setTokensAndSecrets($signatures) { - return $this->signatures($signatures); - } - - /** set the signature method (currently only Plaintext or SHA-MAC1) - * - * @param method {string} Method of signing the transaction (only PLAINTEXT and SHA-MAC1 allowed for now) - */ - function setSignatureMethod ($method="") { - if (empty($method)) - $method = $this->_default_signature_method; - $method = strtoupper($method); - switch($method) - { - case 'PLAINTEXT': - case 'HMAC-SHA1': - $this->_parameters['oauth_signature_method']=$method; - break; - default: - throw new OAuthSimpleException ("Unknown signing method $method specified for OAuthSimple.setSignatureMethod"); - } - return $this; - } - - /** sign the request - * - * note: all arguments are optional, provided you've set them using the - * other helper functions. - * - * @param args {object} hash of arguments for the call - * {action, path, parameters (array), method, signatures (array)} - * all arguments are optional. - */ - function sign($args=array()) { - if (!empty($args['action'])) - $this->setAction($args['action']); - if (!empty($args['path'])) - $this->setPath($args['path']); - if (!empty($args['method'])) - $this->setSignatureMethod($args['method']); - if (!empty($args['signatures'])) - $this->signatures($args['signatures']); - if (empty($args['parameters'])) - $args['parameters']=array(); // squelch the warning. - $this->setParameters($args['parameters']); - $normParams = $this->_normalizedParameters(); - $this->_parameters['oauth_signature'] = $this->_generateSignature($normParams); - return Array( - 'parameters' => $this->_parameters, - 'signature' => $this->_oauthEscape($this->_parameters['oauth_signature']), - 'signed_url' => $this->_path . '?' . $this->_normalizedParameters(), - 'header' => $this->getHeaderString(), - 'sbs'=> $this->sbs - ); - } - - /** Return a formatted "header" string - * - * NOTE: This doesn't set the "Authorization: " prefix, which is required. - * I don't set it because various set header functions prefer different - * ways to do that. - * - * @param args {object} see .sign - */ - function getHeaderString ($args=array()) { - if (empty($this->_parameters['oauth_signature'])) - $this->sign($args); - - $result = 'OAuth '; - - foreach ($this->_parameters as $pName=>$pValue) - { - if (strpos($pName,'oauth_') !== 0) - continue; - if (is_array($pValue)) - { - foreach ($pValue as $val) - { - $result .= $pName .'="' . $this->_oauthEscape($val) . '", '; - } - } - else - { - $result .= $pName . '="' . $this->_oauthEscape($pValue) . '", '; - } - } - return preg_replace('/, $/','',$result); - } - - // Start private methods. Here be Dragons. - // No promises are kept that any of these functions will continue to exist - // in future versions. - function _parseParameterString ($paramString) { - $elements = explode('&',$paramString); - $result = array(); - foreach ($elements as $element) - { - list ($key,$token) = explode('=',$element); - if ($token) - $token = urldecode($token); - if (!empty($result[$key])) - { - if (!is_array($result[$key])) - $result[$key] = array($result[$key],$token); - else - array_push($result[$key],$token); - } - else - $result[$key]=$token; - } - //error_log('Parse parameters : '.print_r($result,1)); - return $result; - } - - function _oauthEscape($string) { - if ($string === 0) - return 0; - if (strlen($string) == 0) - return ''; - if (is_array($string)) - throw new OAuthSimpleException('Array passed to _oauthEscape'); - $string = rawurlencode($string); - $string = str_replace('+','%20',$string); - $string = str_replace('!','%21',$string); - $string = str_replace('*','%2A',$string); - $string = str_replace('\'','%27',$string); - $string = str_replace('(','%28',$string); - $string = str_replace(')','%29',$string); - return $string; - } - - function _getNonce($length=5) { - $result = ''; - $cLength = strlen($this->_nonce_chars); - for ($i=0; $i < $length; $i++) - { - $rnum = rand(0,$cLength); - $result .= substr($this->_nonce_chars,$rnum,1); - } - $this->_parameters['oauth_nonce'] = $result; - return $result; - } - - function _getApiKey() { - if (empty($this->_secrets['consumer_key'])) - { - throw new OAuthSimpleException('No consumer_key set for OAuthSimple'); - } - $this->_parameters['oauth_consumer_key']=$this->_secrets['consumer_key']; - return $this->_parameters['oauth_consumer_key']; - } - - function _getAccessToken() { - if (!isset($this->_secrets['oauth_secret'])) - return ''; - if (!isset($this->_secrets['oauth_token'])) - throw new OAuthSimpleException('No access token (oauth_token) set for OAuthSimple.'); - $this->_parameters['oauth_token'] = $this->_secrets['oauth_token']; - return $this->_parameters['oauth_token']; - } - - function _getTimeStamp() { - return $this->_parameters['oauth_timestamp'] = time(); - } - - function _normalizedParameters() { - $elements = array(); - $ra = 0; - ksort($this->_parameters); - foreach ( $this->_parameters as $paramName=>$paramValue) { - if(strpos($paramValue, '@') === 0 && file_exists(substr($paramValue, 1))) - { - continue; - } - elseif (preg_match('/\w+_secret/',$paramName)) - { - continue; - } - elseif (is_array($paramValue)) - { - sort($paramValue); - foreach($paramValue as $element) - array_push($elements,$this->_oauthEscape($paramName).'='.$this->_oauthEscape($element)); - continue; - } - array_push($elements,$this->_oauthEscape($paramName).'='.$this->_oauthEscape($paramValue)); - } - return join('&',$elements); - } - - function _generateSignature () { - $secretKey = ''; - if(isset($this->_secrets['shared_secret'])) - $secretKey = $this->_oauthEscape($this->_secrets['shared_secret']); - $secretKey .= '&'; - if(isset($this->_secrets['oauth_secret'])) - $secretKey .= $this->_oauthEscape($this->_secrets['oauth_secret']); - switch($this->_parameters['oauth_signature_method']) - { - case 'PLAINTEXT': - return urlencode($secretKey); - - case 'HMAC-SHA1': - $this->sbs = $this->_oauthEscape($this->_action).'&'.$this->_oauthEscape($this->_path).'&'.$this->_oauthEscape($this->_normalizedParameters()); - //error_log('SBS: '.$sigString); - return base64_encode(hash_hmac('sha1',$this->sbs,$secretKey,true)); - - default: - throw new OAuthSimpleException('Unknown signature method for OAuthSimple'); - } - } -} -?> diff --git a/openphoto-php/OpenPhotoOAuth.php b/openphoto-php/OpenPhotoOAuth.php deleted file mode 100644 index 8993df6..0000000 --- a/openphoto-php/OpenPhotoOAuth.php +++ /dev/null @@ -1,133 +0,0 @@ -host = $host; - $this->consumerKey = $consumerKey; - $this->consumerSecret = $consumerSecret; - $this->token = $token; - $this->tokenSecret = $tokenSecret; - } - - public function get($endpoint, $params = null) - { - $curlEndpoint = $endpoint; - if(!empty($params)) - $curlEndpoint .= sprintf('?%s', http_build_query($params)); - $ch = curl_init($this->constructEndpoint($curlEndpoint, true, $params)); - if(!empty($this->consumerKey)) - { - $client = new OAuthSimple($this->consumerKey, $this->consumerSecret); - $request = $client->sign( - array( - 'action' => 'GET', - 'path' => $this->constructEndpoint($endpoint), - 'version' => '1.0a', - 'parameters' => $params, - 'signatures' => - array( - 'consumer_key' => $this->consumerKey, - 'consumer_secret' => $this->consumerSecret, - 'access_token' => $this->token, - 'access_secret' => $this->tokenSecret - ) - ) - ); - curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$request['header']}")); - } - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $resp = curl_exec($ch); - curl_close($ch); - return $resp; - } - - public function getAccessToken($params = null) - { - $client = new OAuthSimple($this->consumerKey, $this->consumerSecret); - } - - public function getAuthorizeUrl($token, $params = null) - { - $client = new OAuthSimple($this->consumerKey, $this->consumerSecret); - $request = $client->sign( - array( - 'path' => '', - 'parameters' => $params, - 'version' => '1.0a', - 'signatures' => array( - 'consumer_key' => $this->consumerKey, - 'consumer_secret' => $this->consumerSecret - ) - ) - ); - return $request['signed_url']; - } - - public function getRequestToken($params = null) - { - - } - - public function post($endpoint, $params = null) - { - $ch = curl_init($this->constructEndpoint($endpoint, true)); - if(!empty($this->consumerKey)) - { - $client = new OAuthSimple($this->consumerKey, $this->consumerSecret); - $request = $client->sign( - array( - 'action' => 'POST', - 'path' => $this->constructEndpoint($endpoint), - 'version' => '1.0a', - 'parameters' => $params, - 'signatures' => - array( - 'consumer_key' => $this->consumerKey, - 'consumer_secret' => $this->consumerSecret, - 'access_token' => $this->token, - 'access_secret' => $this->tokenSecret - ) - ) - ); - curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$request['header']}")); - } - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_POST, 1); - if(!empty($params)) - curl_setopt($ch, CURLOPT_POSTFIELDS, $params); - $resp = curl_exec($ch); - curl_close($ch); - return $resp; - } - - private function constructEndpoint($endpoint, $includeConsumerKey = false) - { - if($includeConsumerKey) - { - if(stristr($endpoint, '?') === false) - return sprintf('http://%s%s?oauth_consumer_key=%s', $this->host, $endpoint, $this->consumerKey); - else - return sprintf('http://%s%s&oauth_consumer_key=%s', $this->host, $endpoint, $this->consumerKey); - } - else - { - return sprintf('http://%s%s', $this->host, $endpoint); - } - } -} diff --git a/openphoto-php/Readme.markdown b/openphoto-php/Readme.markdown deleted file mode 100644 index 0168892..0000000 --- a/openphoto-php/Readme.markdown +++ /dev/null @@ -1,97 +0,0 @@ -Open Photo API / PHP Library -======================= -#### OpenPhoto, a photo service for the masses - ----------------------------------------- - - -### How to use the library - -To use the library you need to first include `OpenPhotoOAuth.php`, then instantiate an instance of the class and start making calls. - - include 'OpenPhotoOAuth.php'; - $client = new OpenPhotoOAuth($host, $consumerKey, $consumerSecret, $token, $tokenSecret); - $resp = $client->get('/photos/list.json'); - $resp = $client->post('/photo/62/update.json', array('tags' => 'tag1,tag2')); - ----------------------------------------- - - -### Using from the command line - -Make sure that the `openphoto` file is executable. - - chown o+x openphoto - -You'll then want to export your secrets to the environment. -We suggest putting them in a file and sourcing it prior to running `openphoto` commands. -Click here for instructions on getting credentials. - - # env.sh - export consumerKey=your_consumer_key - export consumerSecret=your_consumer_secret - export token=your_access_token - export tokenSecret=your_access_token_secret - -You'll need to source that file once for each terminal session. - - source env.sh - -These are the options you can pass to the shell program. - - -h hostname # default=localhost - -e endpoint # default=/photos/list.json - -X method # default=GET - -F params # i.e. -F 'title=my title' -F 'tags=mytag1,mytag1' - -p # pretty print the json - -v # verbose output - --encode # base 64 encode the photo - -Now you can run commands to the OpenPhoto API from your shell! - - ./openphoto -h current.openphoto.me -p -e /photo/62/view.json -F 'returnSizes=20x20' - { - "message" : "Photo 62", - "code" : 200, - "result" : { - "tags" : [ - - ], - "id" : "62", - "appId" : "current.openphoto.me", - "pathBase" : "\/base\/201108\/1312956581-opmeqViHrD.jpg", - "dateUploadedMonth" : "08", - "dateTakenMonth" : "08", - "exifCameraMake" : "", - "dateTaken" : "1312956581", - "title" : "Tomorrowland Main Stage 2011", - "height" : "968", - "description" : "", - "creativeCommons" : "BY-NC", - "dateTakenYear" : "2011", - "dateUploadedDay" : "09", - "longitude" : "4", - "host" : "opmecurrent.s3.amazonaws.com", - "hash" : "0455675a8c42148238b81ed1d8db655c45ae055a", - "status" : "1", - "width" : "1296", - "dateTakenDay" : "09", - "permission" : "1", - "pathOriginal" : "\/original\/201108\/1312956581-opmeqViHrD.jpg", - "size" : "325", - "dateUploadedYear" : "2011", - "views" : "0", - "latitude" : "50.8333", - "dateUploaded" : "1312956583", - "exifCameraModel" : "", - "Name" : "62", - "path20x20" : "http:\/\/current.openphoto.me\/photo\/62\/create\/ceb90\/20x20.jpg" - } - } - - -#### Getting your credentials - -You can get your credentals by clicking on the arrow next to your email address once you're logged into your site and then clicking on settings. -If you don't have any credentials then you can create one for yourself by going to `/v1/oauth/flow`. -Once completed go back to the settings page and you should see the credential you just created diff --git a/openphoto-php/openphoto b/openphoto-php/openphoto deleted file mode 100755 index b6b8aa6..0000000 --- a/openphoto-php/openphoto +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/php -get($endpoint, $fields); -elseif($method == 'post') - $resp = $client->post($endpoint, $fields); - -if($verbose) - echo sprintf("==========\nMethod: %s\nHost: %s\nEndpoint: %s\n==========\n\n", $method, $host, $endpoint); - -if($pretty) - echo indent($resp); -else - echo $resp; - -if($verbose || $pretty) - echo "\n"; - -// from https://gist.github.com/906036 -function indent($json) { - - $result = ''; - $pos = 0; - $strLen = strlen($json); - $indentStr = ' '; - $newLine = "\n"; - $prevChar = ''; - $outOfQuotes = true; - - for ($i=0; $i<=$strLen; $i++) { - - // Grab the next character in the string. - $char = substr($json, $i, 1); - - // Put spaces in front of : - if ($outOfQuotes && $char == ':' && $prevChar != ' ') { - $result .= ' '; - } - - if ($outOfQuotes && $char != ' ' && $prevChar == ':') { - $result .= ' '; - } - - // Are we inside a quoted string? - if ($char == '"' && $prevChar != '\\') { - $outOfQuotes = !$outOfQuotes; - - // If this character is the end of an element, - // output a new line and indent the next line. - } else if(($char == '}' || $char == ']') && $outOfQuotes) { - $result .= $newLine; - $pos --; - for ($j=0; $j<$pos; $j++) { - $result .= $indentStr; - } - } - - // Add the character to the result string. - $result .= $char; - - // If the last character was the beginning of an element, - // output a new line and indent the next line. - if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { - $result .= $newLine; - if ($char == '{' || $char == '[') { - $pos ++; - } - - for ($j = 0; $j < $pos; $j++) { - $result .= $indentStr; - } - } - - $prevChar = $char; - } - - return $result; -} diff --git a/openphoto-wordpress.php b/openphoto-wordpress.php index 570be8b..be33dc0 100644 --- a/openphoto-wordpress.php +++ b/openphoto-wordpress.php @@ -1,7 +1,7 @@ {"photo".$sizes['thumbnail']}[0]; $src["medium"] = $photo->{"photo".$sizes['medium']}[0]; $src["large"] = $photo->{"photo".$sizes['large']}[0]; - $src["original"] = 'http://'.$photo->host.$photo->pathOriginal; + $src["original"] = $photo->pathOriginal; + if (strpos($src["original"],"http")===false) $src["original"] = 'http://'.$photo->host.$photo->pathOriginal; // in older versions of the API, pathOriginal did not have the full address + + + + + if ("" == $photo->title) { + $info = pathinfo(basename($src["original"])); + $photo->title = basename($src["original"],'.'.$info['extension']); + } echo '
'; echo ''; @@ -216,22 +225,18 @@ class WP_OpenPhoto { echo 'Hide'; echo ''; echo '
'; - if (! empty($photo->title) ) { - echo $photo->title; - } else { - substr(strrchr($photo->pathOriginal, "/"), 1 ); - } + echo esc_attr($photo->title); echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; @@ -242,8 +247,8 @@ class WP_OpenPhoto { echo ''; echo ''; echo ''; - echo ''; - echo ''; + echo ''; + echo ''; echo ''; echo ''; echo ''; @@ -310,7 +315,7 @@ class WP_OpenPhoto { echo ''; } echo ''; - echo '
'; + echo '
'; echo ''; echo ''; echo '';
'; - echo '

'; + echo '

'; //echo '

'; echo '
'; - echo '

File name: '.substr(strrchr($photo->pathOriginal, "/"), 1 ).'

'; - echo '

File type: .'.substr(strrchr($photo->pathOriginal, "."), 1 ).'

'; + echo '

File name: '.$src["original"].'

'; + echo '

File type: .'.$src["original"].'

'; echo '

Upload date: '.date('F d Y', (int) $photo->dateUploaded).'

'; echo '

Dimensions: '.$photo->width.' × '.$photo->height.'

'; echo '