[Mapbender-commits] r8202 - in trunk/mapbender/http: classes
extensions extensions/tmhOAuth extensions/tmhOAuth/examples
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Thu Nov 17 02:29:40 EST 2011
Author: armin11
Date: 2011-11-16 23:29:40 -0800 (Wed, 16 Nov 2011)
New Revision: 8202
Added:
trunk/mapbender/http/classes/class_twitter.php
trunk/mapbender/http/extensions/tmhOAuth/
trunk/mapbender/http/extensions/tmhOAuth/LICENSE
trunk/mapbender/http/extensions/tmhOAuth/README.md
trunk/mapbender/http/extensions/tmhOAuth/examples/
trunk/mapbender/http/extensions/tmhOAuth/examples/auth.php
trunk/mapbender/http/extensions/tmhOAuth/examples/auto_fix_time.php
trunk/mapbender/http/extensions/tmhOAuth/examples/entities.php
trunk/mapbender/http/extensions/tmhOAuth/examples/friends.php
trunk/mapbender/http/extensions/tmhOAuth/examples/images.php
trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_echo.php
trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_flow.php
trunk/mapbender/http/extensions/tmhOAuth/examples/oob.php
trunk/mapbender/http/extensions/tmhOAuth/examples/photo_tweet.php
trunk/mapbender/http/extensions/tmhOAuth/examples/rss.php
trunk/mapbender/http/extensions/tmhOAuth/examples/search.php
trunk/mapbender/http/extensions/tmhOAuth/examples/siggen.php
trunk/mapbender/http/extensions/tmhOAuth/examples/sitestream.php
trunk/mapbender/http/extensions/tmhOAuth/examples/streaming.php
trunk/mapbender/http/extensions/tmhOAuth/examples/streaming_advanced.php
trunk/mapbender/http/extensions/tmhOAuth/examples/tweet.php
trunk/mapbender/http/extensions/tmhOAuth/examples/userstream.php
trunk/mapbender/http/extensions/tmhOAuth/examples/verify.php
trunk/mapbender/http/extensions/tmhOAuth/examples/xauth.php
trunk/mapbender/http/extensions/tmhOAuth/tmhOAuth.php
trunk/mapbender/http/extensions/tmhOAuth/tmhUtilities.php
Modified:
trunk/mapbender/http/classes/class_wms.php
Log:
Enhancement to push update information of mapbenders database to twitter account.
Added: trunk/mapbender/http/classes/class_twitter.php
===================================================================
--- trunk/mapbender/http/classes/class_twitter.php (rev 0)
+++ trunk/mapbender/http/classes/class_twitter.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,37 @@
+<?php
+require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__)."/../../conf/twitter.conf");
+require_once(dirname(__FILE__)."/../extensions/tmhOAuth/tmhOAuth.php");
+
+/**
+ * class to wrap twitter methods
+ *
+ *
+ */
+class twitter {
+ /**
+ *
+ *
+ *
+ *
+ */
+ function postTweet($tweetText) {
+ if (TWITTER_CONSUMER_KEY != '' && TWITTER_CONSUMER_SECRET != '' && TWITTER_ACCESS_TOKEN != '' && TWITTER_ACCESS_TOKEN_SECRET != '') {
+ $connection = new tmhOAuth(array(
+ 'consumer_key' => TWITTER_CONSUMER_KEY,
+ 'consumer_secret' => TWITTER_CONSUMER_SECRET,
+ 'user_token' => TWITTER_ACCESS_TOKEN,
+ 'user_secret' => TWITTER_ACCESS_TOKEN_SECRET,
+ 'curl_proxy' => CONNECTION_PROXY.":".CONNECTION_PORT
+ ));
+ $connection->request('POST',
+ $connection->url('1/statuses/update'),
+ array('status' => $tweetText)
+ );
+ return $connection->response['code'];
+ } else {
+ return false;
+ }
+ }
+}
+?>
Modified: trunk/mapbender/http/classes/class_wms.php
===================================================================
--- trunk/mapbender/http/classes/class_wms.php 2011-11-15 07:50:16 UTC (rev 8201)
+++ trunk/mapbender/http/classes/class_wms.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -12,6 +12,7 @@
require_once dirname(__FILE__) . "/class_administration.php";
require_once dirname(__FILE__) . "/class_georss_factory.php";
require_once dirname(__FILE__) . "/class_mb_exception.php";
+require_once dirname(__FILE__) . "/class_twitter.php";
require_once dirname(__FILE__) . "/../classes/class_universal_wms_factory.php";
class wms {
@@ -102,6 +103,8 @@
# LOGIN
#) . $wmsId;
return LOGIN."/../../mapbender/php/mod_showMetadata.php?resource=wms&id=".$wmsId;
+ //the following may be activated if the metadata for the resources can be accessed over restful urls - maybe with help of apaches mod_rewrite
+ #return "http://www.geoportal.rlp.de/wms/".$wmsId;
}
public static function getLayerMetadataUrl ($layerId) {
#return preg_replace(
@@ -110,6 +113,8 @@
# LOGIN
#) . $wmsId;
return LOGIN."/../../mapbender/php/mod_showMetadata.php?resource=layer&id=".$layerId;
+ //the following may be activated if the metadata for the resources can be accessed over restful urls - maybe with help of apaches mod_rewrite
+ #return "http://www.geoportal.rlp.de/layer/".$layerId;
}
public static function isOwsProxyUrl ($getmap) {
// $e = new mb_notice("isOwsProxyUrl? " . $getmap);
@@ -1664,6 +1669,16 @@
$geoRss->appendTop($geoRssItem);
$geoRss->saveAsFile();
}
+ if (TWITTER_NEWS == true) {
+ //new WMS
+ $twitter_wms = new twitter();
+ //combine text for tweet
+ $tweetText = "NEW WMS: " . $this->wms_title." (".$myWMS.")".self::getWmsMetadataUrl($myWMS).$this->wms_abstract;
+ //reduce to 140 chars
+ $tweetText = substr($tweetText, 0, 139);
+ //push to twitter
+ $result = $twitter_wms->postTweet($tweetText);
+ }
//Changes JW
$this->wms_id = $myWMS;
@@ -2677,6 +2692,34 @@
}
$geoRss->saveAsFile();
}
+ //twitter out changes
+ if (TWITTER_NEWS == true) {
+ //updated WMS
+ $twitter_wms = new twitter();
+ //combine text for tweet
+ $tweetText = "UPDATED WMS: " . $this->wms_title." (".$myWMS.")".self::getWmsMetadataUrl($myWMS).$this->wms_abstract;
+ //reduce to 140 chars
+ $tweetText = substr($tweetText, 0, 139);
+ //push to twitter
+ $result = $twitter_wms->postTweet($tweetText);
+ //for each layer
+ foreach ($newLayerArray as $newLayer) {
+ $twitter_layer = new twitter();
+ $tweetText = "NEW LAYER: " . $currentLayer->layer_title." (".$currentLayer->layer_id.")".self::getLayerMetadataUrl($currentLayer->layer_id)."".$currentLayer->layer_abstract;
+ //reduce to 140 chars
+ $tweetText = substr($tweetText, 0, 139);
+ $result = $twitter_layer->postTweet($tweetText);
+ }
+ foreach ($oldLayerNameArray as $oldLayer) {
+ $twitter_layer = new twitter();
+ $tweetText = "DELETED LAYER: " . $oldLayer['title']." (".$oldLayer['id'].")".self::getLayerMetadataUrl($currentLayer->layer_id)."".$currentLayer->layer_abstract;
+ //reduce to 140 chars
+ $tweetText = substr($tweetText, 0, 139);
+ $result = $twitter_layer->postTweet($tweetText);
+ }
+
+ }
+
return;
}
Added: trunk/mapbender/http/extensions/tmhOAuth/LICENSE
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/LICENSE (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/LICENSE 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
Added: trunk/mapbender/http/extensions/tmhOAuth/README.md
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/README.md (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/README.md 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,121 @@
+# tmhOAuth
+
+An OAuth 1.0A library written in PHP by @themattharris, specifically for use
+with the Twitter API.
+
+**Important**: If you used custom HTTP request headers they should now be defined
+as `'key' => 'value'` pairs instead of complete `'key: value'` strings.
+
+If you previously used version 0.4 be aware the utility functions
+have now been broken into their own file. Before you use version 0.5+ in your app
+test locally to ensure your code doesn't need tmhUtilities included.
+
+**Disclaimer**: This project is a work in progress. Please use the issue tracker
+to report any enhancements or issues you encounter.
+
+## Goals
+
+- Support OAuth 1.0A
+- Use Authorisation headers instead of query string or POST parameters
+- Allow uploading of images
+- Provide enough information to assist with debugging
+
+## Dependancies
+
+The library has been tested with PHP 5.3+ and relies on CURL and hash_hmac. The
+vast majority of hosting providers include these libraries and run with PHP 5.1+.
+
+The code makes use of hash_hmac, which was introduced in PHP 5.1.2. If you version
+of PHP is lower than this you should ask your hosting provider for an update.
+
+## Usage
+
+This will be built out later but for the moment review the examples for ways
+the library can be used. Each example contains instructions on how to use it
+
+## Change History
+
+### 0.56 - 29 September 2011
+- Fixed version reference in the UserAgent
+- Updated tmhUtilities::entify with support for media
+- Updated tmhUtilities::entify with support for multibyte characters. Props: andersonshatch
+
+### 0.55 - 29 September 2011
+- Added support for content encoding. Defaults to whatever localhost supports. Props: yusuke
+
+### 0.54 - 29 September 2011
+- User-Agent is now configurable and includes the current version number of the script
+- Updated the Streaming examples to use SSL
+
+### 0.53 - 15 July 2011
+- Fixed issue where headers were being duplicated if the library was called more than once.
+- Updated examples to fit the new location of access tokens and secrets on dev.twitter.com
+- Added Photo Tweet example
+
+### 0.52 - 06 July 2011
+- Fixed issue where the preference for include_time in create_nonce was being ignored
+
+### 0.51 - 06 July 2011
+- Use isset instead of suppress errors. Props: funkatron
+- Added example of using the Search API
+- Added example of using friends/ids and users/lookup to get details of a users friends
+- Added example of the authorize OAuth webflow
+
+### 0.5 - 29 March 2011
+- Moved utility functions out of the main class and into the tmhUtilities class.
+- Added the ability to send OAuth parameters as part of the querystring or POST body.
+- Section 3.4.1.2 says the url must be lowercase so prepare URL now does this.
+- Added a convenience method for accessing the safe_encode/decode transforms.
+- Updated the examples to use the new utilities library.
+- Added examples for sitestreams and userstreams.
+- Added a more advanced streaming API example.
+
+### 0.4 - 03 March 2011
+- Fixed handling of parameters when using DELETE. Thanks to yusuke for reporting
+- Fixed php_self to handle port numbers other than 80/443. Props: yusuke
+- Updated function pr to use pre only when not running in CLI mode
+- Add support for proxy servers. Props juanchorossi
+- Function request now returns the HTTP status code. Props: kronenthaler
+- Documentation fixes for xAuth. Props: 140dev
+- Some minor code formatting changes
+
+### 0.3 - 28 September 2010
+- Moved entities rendering into the library
+
+### 0.2 - 17 September 2010
+- Added support for the Streaming API
+
+### 0.14 - 17 September 2010
+- Fixed authorisation header for use with OAuth Echo
+
+### 0.13 - 17 September 2010
+- Added use_ssl configuration parameter
+- Fixed config array typo
+- Removed v from the config
+- Remove protocol from the host (configured by use_ssl)
+- Added include for easier debugging
+
+### 0.12 - 17 September 2010
+
+- Moved curl options to config
+- Added the ability for curl to follow redirects, default false
+
+### 0.11 - 17 September 2010
+
+- Fixed a bug in the GET requests
+
+### 0.1 - 26 August 2010
+
+- Initial beta version
+
+## Community
+
+License: Apache 2 (see included LICENSE file)
+
+Follow me on Twitter: <https://twitter.com/intent/follow?screen_name=themattharris>
+Check out the Twitter Developer Resources: <http://dev.twitter.com>
+
+## To Do
+
+- Add good behavior logic to the Streaming API handler - i.e. on disconnect back off
+- Async Curl support
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/auth.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/auth.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/auth.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,112 @@
+<?php
+
+/**
+ * Demonstration of the various OAuth flows. You would typically do this
+ * when an unknown user is first using your application. Instead of storing
+ * the token and secret in the session you would probably store them in a
+ * secure database with their logon details for your website.
+ *
+ * When the user next visits the site, or you wish to act on their behalf,
+ * you would use those tokens and skip this entire process.
+ *
+ * The Sign in with Twitter flow directs users to the oauth/authenticate
+ * endpoint which does not support the direct message permission. To obtain
+ * direct message permissions you must use the "Authorize Application" flows.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+));
+
+$here = tmhUtilities::php_self();
+session_start();
+
+function outputError($tmhOAuth) {
+ echo 'Error: ' . $tmhOAuth->response['response'] . PHP_EOL;
+ tmhUtilities::pr($tmhOAuth);
+}
+
+// reset request?
+if ( isset($_REQUEST['wipe'])) {
+ session_destroy();
+ header("Location: {$here}");
+
+// already got some credentials stored?
+} elseif ( isset($_SESSION['access_token']) ) {
+ $tmhOAuth->config['user_token'] = $_SESSION['access_token']['oauth_token'];
+ $tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
+
+ $code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
+ if ($code == 200) {
+ $resp = json_decode($tmhOAuth->response['response']);
+ echo $resp->screen_name;
+ } else {
+ outputError($tmhOAuth);
+ }
+// we're being called back by Twitter
+} elseif (isset($_REQUEST['oauth_verifier'])) {
+ $tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
+ $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
+
+ $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
+ 'oauth_verifier' => $_REQUEST['oauth_verifier']
+ ));
+
+ if ($code == 200) {
+ $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+ unset($_SESSION['oauth']);
+ header("Location: {$here}");
+ } else {
+ outputError($tmhOAuth);
+ }
+// start the OAuth dance
+} elseif ( isset($_REQUEST['authenticate']) || isset($_REQUEST['authorize']) ) {
+ $callback = isset($_REQUEST['oob']) ? 'oob' : $here;
+
+ $params = array(
+ 'oauth_callback' => $callback
+ );
+
+ if (isset($_REQUEST['force_write'])) :
+ $params['x_auth_access_type'] = 'write';
+ elseif (isset($_REQUEST['force_read'])) :
+ $params['x_auth_access_type'] = 'read';
+ endif;
+
+ $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), $params);
+
+ if ($code == 200) {
+ $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+ $method = isset($_REQUEST['authenticate']) ? 'authenticate' : 'authorize';
+ $force = isset($_REQUEST['force']) ? '&force_login=1' : '';
+ $authurl = $tmhOAuth->url("oauth/{$method}", '') . "?oauth_token={$_SESSION['oauth']['oauth_token']}{$force}";
+ echo '<p>To complete the OAuth flow follow this URL: <a href="'. $authurl . '">' . $authurl . '</a></p>';
+ } else {
+ outputError($tmhOAuth);
+ }
+}
+
+?>
+<ul>
+ <li><a href="?authenticate=1">Sign in with Twitter</a></li>
+ <li><a href="?authenticate=1&force=1">Sign in with Twitter (force login)</a></li>
+ <li><a href="?authorize=1">Authorize Application (with callback)</a></li>
+ <li><a href="?authorize=1&oob=1">Authorize Application (oob - pincode flow)</a></li>
+ <li><a href="?authorize=1&force_read=1">Authorize Application (with callback) (force read-only permissions)</a></li>
+ <li><a href="?authorize=1&force_write=1">Authorize Application (with callback) (force read-write permissions)</a></li>
+ <li><a href="?authorize=1&force=1">Authorize Application (with callback) (force login)</a></li>
+ <li><a href="?wipe=1">Start Over and delete stored tokens</a></li>
+</ul>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/auto_fix_time.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/auto_fix_time.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/auto_fix_time.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * Verify the user token and secret works. If successful we will be given the
+ * details of the user. If not an error explaining why will be returned.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * This example differs from others in that it will reattempt a request if
+ * the timestamp is detected to be off from the Twitter servers.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+// for the demo set the timestamp to yesterday
+$tmhOAuth->config['force_timestamp'] = true;
+$tmhOAuth->config['timestamp'] = strtotime('yesterday');
+
+$code = tmhUtilities::auto_fix_time_request($tmhOAuth, 'GET', $tmhOAuth->url('1/account/verify_credentials'));
+
+if ($code == 200) {
+ if ($tmhOAuth->auto_fixed_time)
+ echo 'Had to auto adjust the time. Please check the date and time is correct on your device/server';
+
+ tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
+} else {
+ tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/entities.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/entities.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/entities.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,87 @@
+<!DOCTYPE html>
+<head>
+ <meta charset="UTF-8" />
+</head>
+<body>
+<?php
+
+/**
+ * Render a very rough timeline with entities included.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$code = $tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/user_timeline'), array(
+ 'include_entities' => '1',
+ 'include_rts' => '1',
+ 'screen_name' => 'themattharris',
+ 'count' => 100,
+));
+
+if ($code == 200) {
+ $timeline = json_decode($tmhOAuth->response['response'], true);
+ foreach ($timeline as $tweet) :
+ $entified_tweet = tmhUtilities::entify($tweet);
+ $is_retweet = isset($tweet['retweeted_status']);
+
+ $diff = time() - strtotime($tweet['created_at']);
+ if ($diff < 60*60)
+ $created_at = floor($diff/60) . ' minutes ago';
+ elseif ($diff < 60*60*24)
+ $created_at = floor($diff/(60*60)) . ' hours ago';
+ else
+ $created_at = date('d M', strtotime($tweet['created_at']));
+
+ $permalink = str_replace(
+ array(
+ '%screen_name%',
+ '%id%',
+ '%created_at%'
+ ),
+ array(
+ $tweet['user']['screen_name'],
+ $tweet['id_str'],
+ $created_at,
+ ),
+ '<a href="http://twitter.com/%screen_name%/%id%">%created_at%</a>'
+ );
+
+ ?>
+ <div id="<?php echo $tweet['id_str']; ?>" style="margin-bottom: 1em">
+ <span>ID: <?php echo $tweet['id_str']; ?></span><br>
+ <span>Orig: <?php echo $tweet['text']; ?></span><br>
+ <span>Entitied: <?php echo $entified_tweet ?></span><br>
+ <small><?php echo $permalink ?><?php if ($is_retweet) : ?>is retweet<?php endif; ?>
+ <span>via <?php echo $tweet['source']?></span></small>
+ </div>
+<?php
+ endforeach;
+} else {
+ tmhUtilities::pr($tmhOAuth->response);
+}
+?>
+</body>
+</html>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/friends.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/friends.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/friends.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,94 @@
+<?php
+
+/**
+ * Retrieve a list of friends for the authenticating user and then lookup
+ * their details using users/lookup.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+define('PAGESIZE', 100);
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+function check_rate_limit($response) {
+ $headers = $response['headers'];
+ if ($headers['x_ratelimit_remaining'] == 0) :
+ $reset = $headers['x_ratelimit_reset'];
+ $sleep = time() - $reset;
+ echo 'rate limited. reset time is ' . $reset . PHP_EOL;
+ echo 'sleeping for ' . $sleep . ' seconds';
+ sleep($sleep);
+ endif;
+}
+
+$cursor = '-1';
+$ids = array();
+
+while (true) :
+ if ($cursor == '0')
+ break;
+
+ $tmhOAuth->request('GET', $tmhOAuth->url('1/friends/ids'), array(
+ 'cursor' => $cursor
+ ));
+
+ // check the rate limit
+ check_rate_limit($tmhOAuth->response);
+
+ if ($tmhOAuth->response['code'] == 200) {
+ $data = json_decode($tmhOAuth->response['response'], true);
+ $ids += $data['ids'];
+ $cursor = $data['next_cursor_str'];
+ } else {
+ echo $tmhOAuth->response['response'];
+ break;
+ }
+endwhile;
+
+// lookup users
+$paging = ceil(count($ids) / PAGESIZE);
+$users = array();
+for ($i=0; $i < $paging ; $i++) {
+ $set = array_slice($ids, $i*PAGESIZE, PAGESIZE);
+
+ $tmhOAuth->request('GET', $tmhOAuth->url('1/users/lookup'), array(
+ 'user_id' => implode(',', $set)
+ ));
+
+ // check the rate limit
+ check_rate_limit($tmhOAuth->response);
+
+ if ($tmhOAuth->response['code'] == 200) {
+ $data = json_decode($tmhOAuth->response['response'], true);
+ $users += $data;
+ } else {
+ echo $tmhOAuth->response['response'];
+ break;
+ }
+
+}
+var_dump($users);
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/images.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/images.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/images.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<head>
+ <meta charset="UTF-8" />
+</head>
+<body>
+<?php
+
+/**
+ * Update the users profile image, or profile background image using OAuth.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+if ( ! empty($_FILES)) {
+
+ require '../tmhOAuth.php';
+ require '../tmhUtilities.php';
+ $tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+ ));
+
+ // note the type and filename are set here as well
+ $params = array(
+ 'image' => "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
+ );
+
+ // if we are setting the background we want it to be displayed
+ if ($_POST['method'] == 'update_profile_background_image')
+ $params['use'] = 'true';
+
+ $code = $tmhOAuth->request('POST', $tmhOAuth->url("1/account/{$_POST['method']}"),
+ $params,
+ true, // use auth
+ true // multipart
+ );
+
+ if ($code == 200) {
+ tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
+ }
+ tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));
+}
+
+?>
+
+<form action="images.php" method="POST" enctype="multipart/form-data">
+ <div>
+ <select name="method" id="method" >
+ <option value="update_profile_image">update_profile_image</option>
+ <option value="update_profile_background_image">update_profile_background_image</option>
+ </select>
+ <input type="file" name="image" />
+ <input type="submit" value="Submit" />
+ </div>
+</form>
+</body>
+</html>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_echo.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_echo.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_echo.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,100 @@
+<?php
+
+/**
+ * Use OAuth Echo to upload a picture to Posterous and then Tweet about it.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+$delegator = 'http://posterous.com/api2/upload.json';
+
+function generate_verify_header($tmhOAuth) {
+ // generate the verify crendentials header -- BUT DON'T SEND
+ // note the https URL change - this is due to posterous requiring https in the X-Auth-Service-Provider
+ $tmhOAuth->config['prevent_request'] = true;
+ $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
+ $tmhOAuth->config['prevent_request'] = false;
+}
+
+function prepare_request($tmhOAuth) {
+ // create the headers for the echo
+ $headers = array(
+ 'X-Auth-Service-Provider' => $tmhOAuth->url('1/account/verify_credentials'),
+ 'X-Verify-Credentials-Authorization' => $tmhOAuth->auth_header,
+ );
+
+ // load the headers for the request
+ $tmhOAuth->headers = $headers;
+ // prepare the request to posterous
+ $params = array(
+ 'media' => "@{$_FILES['file']['tmp_name']};type={$_FILES['file']['type']};filename={$_FILES['file']['name']}",
+ 'message' => 'trying something out'
+ );
+
+ return $params;
+}
+
+function make_request($tmhOAuth, $url, $params, $auth, $multipart) {
+ // make the request, no auth, multipart, custom headers
+ $code = $tmhOAuth->request('POST', $url, $params, $auth, $multipart);
+
+ // Posterous liked it or not?
+ if ($code == 200)
+ return json_decode($tmhOAuth->response['response']);
+
+ return false;
+}
+
+if (TRUE || ! empty($_FILES)) {
+ // IMPORTANT: Posterous requires the host be https://api.twitter.com
+ // versions 0.11+ of tmhOAuth default to SSL so do not need changing
+
+ generate_verify_header($tmhOAuth);
+ $params = prepare_request($tmhOAuth);
+ // post to OAuth Echo provider
+ $resp = make_request($tmhOAuth, $delegator, $params, false, true);
+
+ // post Tweet to Twitter
+ if ($resp !== false) {
+ $params = array(
+ 'status' => 'I just OAuth echoed a picture: ' . $resp->url
+ );
+ $resp = make_request($tmhOAuth, $tmhOAuth->url('1/statuses/update'), $params, true, false);
+
+ if ($resp)
+ tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
+ else
+ echo 'Error: ' . htmlentities($tmhOAuth->response['response']);
+ }
+}
+
+?>
+
+<form action="" method="POST" enctype="multipart/form-data">
+ <div>
+ <input type="file" name="file" />
+ <input type="submit" value="Submit" />
+ </div>
+</form>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_flow.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_flow.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/oauth_flow.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,135 @@
+<?php
+
+/**
+ * Demonstration of the OAuth authorize flow only. You would typically do this
+ * when an unknown user is first using your application and you wish to make
+ * requests on their behalf.
+ *
+ * Instead of storing the token and secret in the session you would probably
+ * store them in a secure database with their logon details for your website.
+ *
+ * When the user next visits the site, or you wish to act on their behalf,
+ * you would use those tokens and skip this entire process.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+));
+
+session_start();
+
+function outputError($tmhOAuth) {
+ echo 'There was an error: ' . $tmhOAuth->response['response'] . PHP_EOL;
+}
+
+function wipe() {
+ session_destroy();
+ header('Location: ' . tmhUtilities::php_self());
+}
+
+
+// Step 1: Request a temporary token
+function request_token($tmhOAuth) {
+ $code = $tmhOAuth->request(
+ 'POST',
+ $tmhOAuth->url('oauth/request_token', ''),
+ array(
+ 'oauth_callback' => tmhUtilities::php_self()
+ )
+ );
+
+ if ($code == 200) {
+ $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+ authorize($tmhOAuth);
+ } else {
+ outputError($tmhOAuth);
+ }
+}
+
+
+// Step 2: Direct the user to the authorize web page
+function authorize($tmhOAuth) {
+ $authurl = $tmhOAuth->url("oauth/authorize", '') . "?oauth_token={$_SESSION['oauth']['oauth_token']}";
+ header("Location: {$authurl}");
+
+ // in case the redirect doesn't fire
+ echo '<p>To complete the OAuth flow please visit URL: <a href="'. $authurl . '">' . $authurl . '</a></p>';
+}
+
+
+// Step 3: This is the code that runs when Twitter redirects the user to the callback. Exchange the temporary token for a permanent access token
+function access_token($tmhOAuth) {
+ $tmhOAuth->config['user_token'] = $_SESSION['oauth']['oauth_token'];
+ $tmhOAuth->config['user_secret'] = $_SESSION['oauth']['oauth_token_secret'];
+
+ $code = $tmhOAuth->request(
+ 'POST',
+ $tmhOAuth->url('oauth/access_token', ''),
+ array(
+ 'oauth_verifier' => $_REQUEST['oauth_verifier']
+ )
+ );
+
+ if ($code == 200) {
+ $_SESSION['access_token'] = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+ unset($_SESSION['oauth']);
+ header('Location: ' . tmhUtilities::php_self());
+ } else {
+ outputError($tmhOAuth);
+ }
+}
+
+
+// Step 4: Now the user has authenticated, do something with the permanent token and secret we received
+function verify_credentials($tmhOAuth) {
+ $tmhOAuth->config['user_token'] = $_SESSION['access_token']['oauth_token'];
+ $tmhOAuth->config['user_secret'] = $_SESSION['access_token']['oauth_token_secret'];
+
+ $code = $tmhOAuth->request(
+ 'GET',
+ $tmhOAuth->url('1/account/verify_credentials')
+ );
+
+ if ($code == 200) {
+ $resp = json_decode($tmhOAuth->response['response']);
+ echo '<h1>Hello ' . $resp->screen_name . '</h1>';
+ echo '<p>The access level of this token is: ' . $tmhOAuth->response['headers']['x_access_level'] . '</p>';
+ } else {
+ outputError($tmhOAuth);
+ }
+}
+
+if (isset($_REQUEST['start'])) :
+ request_token($tmhOAuth);
+elseif (isset($_REQUEST['oauth_verifier'])) :
+ access_token($tmhOAuth);
+elseif (isset($_REQUEST['verify'])) :
+ verify_credentials($tmhOAuth);
+elseif (isset($_REQUEST['wipe'])) :
+ wipe();
+endif;
+
+?>
+
+<p>
+<?php if (isset($_SESSION['access_token'])) : ?>
+ There appears to be some credentials already stored in this browser session.
+ Do you want to <a href="?verify=1">verify the credentials?</a> or
+ <a href="?wipe=1">wipe them and start again</a>.
+<?php else : ?>
+ <a href="?start=1">Authorize with OAuth</a>.
+<?php endif; ?>
+</p>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/oob.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/oob.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/oob.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * Obtain a users token and secret using xAuth.
+ * This example is intended to be run from the command line. To use it:
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) In a terminal or server type:
+ * php /path/to/here/oob.php
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+));
+
+function welcome() {
+ echo <<<EOM
+tmhOAuth PHP Out-of-band.
+This script runs the OAuth flow in out-of-band mode. You will need access to
+a web browser to authorise the application. At the end of this script you will
+be presented with the user token and secret needed to authenticate as the user.
+
+EOM;
+}
+
+function request_token($tmhOAuth) {
+ $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/request_token', ''), array(
+ 'oauth_callback' => 'oob',
+ ));
+
+ if ($code == 200) {
+ $oauth_creds = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+
+ // update with the temporary token and secret
+ $tmhOAuth->config['user_token'] = $oauth_creds['oauth_token'];
+ $tmhOAuth->config['user_secret'] = $oauth_creds['oauth_token_secret'];
+
+ $url = $tmhOAuth->url('oauth/authorize', '') . "?oauth_token={$oauth_creds['oauth_token']}";
+ echo <<<EOM
+
+Copy and paste this URL into your web browser and follower the prompts to get a pin code.
+ {$url}
+
+EOM;
+ } else {
+ echo "There was an error communicating with Twitter. {$tmhOAuth->response['response']}" . PHP_EOL;
+ die();
+ }
+}
+
+function access_token($tmhOAuth, $pin) {
+ $code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
+ 'oauth_verifier' => trim($pin)
+ ));
+
+ if ($code == 200) {
+ $oauth_creds = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+
+ // print tokens
+ echo <<<EOM
+Congratulations, below is the user token and secret for {$oauth_creds['screen_name']}.
+Use these to make authenticated calls to Twitter using the application with
+consumer key: {$tmhOAuth->config['consumer_key']}
+
+User Token: {$oauth_creds['oauth_token']}
+User Secret: {$oauth_creds['oauth_token_secret']}
+
+EOM;
+ } else {
+ echo "There was an error communicating with Twitter. {$tmhOAuth->response['response']}" . PHP_EOL;
+ }
+ var_dump($tmhOAuth);
+ die();
+}
+
+welcome();
+request_token($tmhOAuth);
+$pin = tmhUtilities::read_input('What was the Pin Code?: ');
+access_token($tmhOAuth, $pin);
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/photo_tweet.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/photo_tweet.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/photo_tweet.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,61 @@
+<?php
+
+/**
+ * Tweets a Twitter photo along with a message from the user whose oauth_token
+ * and oauth_secret you use.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the oauth_token/secret of any user who has authorised your application.
+ *
+ * This example is intended to be run from the command line.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Update $image to point to a real image file on your computer.
+ * 5) In a terminal or server type:
+ * php /path/to/here/photo_tweet.php
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+// we're using a hardcoded image path here. You can easily replace this with
+// an uploaded image - see images.php in the examples folder for how to do this
+// 'image = "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
+
+// this is the jpeg file to upload. It should be in the same directory as this file.
+$image = 'image.jpg';
+
+$code = $tmhOAuth->request(
+ 'POST',
+ 'https://upload.twitter.com/1/statuses/update_with_media.json',
+ array(
+ 'media[]' => "@{$image};type=image/jpeg;filename={$image}",
+ 'status' => 'Picture time',
+ ),
+ true, // use auth
+ true // multipart
+);
+
+if ($code == 200) {
+ tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
+} else {
+ tmhUtilities::pr($tmhOAuth->response['response']);
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/rss.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/rss.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/rss.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,41 @@
+<?php
+
+/**
+ * Obtain the home_timeline RSS feed using OAuth
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$code = $tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/home_timeline', 'rss'));
+
+if ($code == 200) {
+ header('Content-Type: application/rss+xml; charset=utf-8');
+ echo $tmhOAuth->response['response'];
+} else {
+ tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/search.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/search.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/search.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,67 @@
+<?php
+
+date_default_timezone_set('GMT');
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array());
+
+$params = array(
+ 'q' => 'Search Query',
+ 'since_id' => 'Get results since this ID (or leave blank for earliest allowed)',
+ 'pages' => 'How many pages should be retrieved?',
+ 'rpp' => 'Results per page (default 15)',
+ 'max_id' => 'Max ID to accept. This isn\'t sent to Search but instead used to filter the received results',
+ 'geocode' => 'Geo co-ordinates (e.g. 37.781157,-122.398720,1mi)',
+ 'lang' => 'Restrict results to a specific language? (en,fr,de etc)'
+);
+
+foreach ($params as $k => $v) :
+ $p[$k] = tmhUtilities::read_input("{$v}: ");
+ if (empty($p[$k]))
+ unset($p[$k]);
+endforeach;
+
+$pages = intval($p['pages']);
+$pages = $pages > 0 ? $pages : 1;
+$results = array();
+
+for ($i=$pages; $i > 0; $i--) {
+ $args = array_intersect_key(
+ $p, array(
+ 'q' => '',
+ 'since_id' => '',
+ 'rpp' => '',
+ 'geocode' => '',
+ 'lang' => ''
+ ));
+ $args['page'] = $i;
+
+ $tmhOAuth->request(
+ 'GET',
+ 'http://search.twitter.com/search.json',
+ $args,
+ false
+ );
+
+ echo "Received page {$i}\t{$tmhOAuth->url}" . PHP_EOL;
+
+ if ($tmhOAuth->response['code'] == 200) {
+ $data = json_decode($tmhOAuth->response['response'], true);
+ $results = array_merge($results, $data['results']);
+ } else {
+ $data = htmlentities($tmhOAuth->response['response']);
+ echo 'There was an error.' . PHP_EOL;
+ var_dump($data);
+ die();
+ }
+}
+
+foreach ($results as $result) {
+ $date = strtotime($result['created_at']);
+ $result['from_user'] = str_pad($result['from_user'], 15, ' ');
+ $result['text'] = str_replace(PHP_EOL, '', $result['text']);
+ echo "{$result['id_str']}\t{$date}\t{$result['from_user']}\t\t{$result['text']}" . PHP_EOL;
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/siggen.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/siggen.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/siggen.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * A script which will generate an OAuth signature for checking with other
+ * libraries/scripts.
+ *
+ * This example is intended to be run from the command line. To use it:
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * http://dev.twitter.com/apps
+ * 2) Note your the consumer key and consumer secret for the application
+ * 3) In a terminal or server type:
+ * php /path/to/here/siggen.php
+ *
+ * @author themattharris
+ *
+ * Known Issues:
+ * * Parameters are not yet supported. To add parameters see the note
+ * in the code
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+
+function welcome() {
+ echo <<<EOM
+tmhOAuth PHP Signature Generator.
+This script generates an OAuth signature from adhoc values.
+No requests are made to the Twitter API.
+
+EOM;
+}
+
+welcome();
+$consumer_key = tmhUtilities::read_input(PHP_EOL . 'Consumer Key' . PHP_EOL);
+$consumer_secret = tmhUtilities::read_input(PHP_EOL . 'Consumer Secret' . PHP_EOL);
+$user_token = tmhUtilities::read_input(PHP_EOL . 'User Token' . PHP_EOL . '(this can be left blank for checking request_token calls)');
+$user_secret = tmhUtilities::read_input(PHP_EOL . 'User Secret' . PHP_EOL . '(this can be left blank for checking request_token calls)');
+$timestamp = tmhUtilities::read_input(PHP_EOL . 'Timestamp' . PHP_EOL . '(leave blank to have this autogenerated)' . PHP_EOL);
+$nonce = tmhUtilities::read_input(PHP_EOL . 'Nonce' . PHP_EOL . '(leave blank to have this autogenerated)' . PHP_EOL);
+$url = tmhUtilities::read_input(PHP_EOL . 'URL' . PHP_EOL . '(e.g. https://api.twitter.com/1/account/verify_credentials.json)' . PHP_EOL);
+$action = tmhUtilities::read_input(PHP_EOL . 'HTTP Action' . PHP_EOL . '(leave blank for GET)' . PHP_EOL);
+
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => $consumer_key,
+ 'consumer_secret' => $consumer_secret,
+ 'user_token' => $user_token,
+ 'user_secret' => $user_secret,
+ 'prevent_request' => true,
+));
+
+if (strlen($nonce) > 0) :
+ $tmhOAuth->config['force_nonce'] = true;
+ $tmhOAuth->config['nonce'] = $nonce;
+endif;
+
+if (strlen($timestamp) > 0) :
+ $tmhOAuth->config['force_timestamp'] = true;
+ $tmhOAuth->config['timestamp'] = $timestamp;
+endif;
+
+$action = strlen($action) > 0 ? strtoupper($action) : 'GET';
+
+// default request
+$tmhOAuth->request($action, $url);
+
+// if you want to use paramters you'll need to do something like this:
+/*
+
+$tmhOAuth->request($action, $url, array(
+ 'param' => 'value',
+));
+
+*/
+
+echo PHP_EOL;
+echo 'Base String:' . $tmhOAuth->base_string;
+echo PHP_EOL;
+echo 'Signing Key:' . $tmhOAuth->signing_key;
+echo PHP_EOL;
+echo 'Auth Header:' . $tmhOAuth->auth_header;
+echo PHP_EOL;
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/sitestream.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/sitestream.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/sitestream.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,52 @@
+<?php
+
+/**
+ * Very basic Site Streams example. In production you would store the
+ * received tweets in a queue or database for later processing.
+ *
+ * Site Streams require you to use the user token/secret of the owner
+ * of the application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) In a terminal or server type:
+ * php /path/to/here/sitestream.php
+ * 5) To stop the Streaming API either press CTRL-C or, in the folder the
+ * script is running from type:
+ * touch STOP
+ *
+ * @author themattharris
+ */
+
+function my_streaming_callback($data, $length, $metrics) {
+ echo $data .PHP_EOL;
+ return file_exists(dirname(__FILE__) . '/STOP');
+}
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$method = "https://sitestream.twitter.com/2b/site.json";
+$params = array(
+ // comma seperated list of user_ids who have authorised your application through OAuth
+ 'follow' => '777925'
+);
+$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
+
+// output any response we get back AFTER the Stream has stopped -- or it errors
+tmhUtilities::pr($tmhOAuth);
+?>
+
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/streaming.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/streaming.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/streaming.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * Very basic streaming API example. In production you would store the
+ * received tweets in a queue or database for later processing.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) In a terminal or server type:
+ * php /path/to/here/streaming.php
+ * 5) To stop the Streaming API either press CTRL-C or, in the folder the
+ * script is running from type:
+ * touch STOP
+ *
+ * @author themattharris
+ */
+
+function my_streaming_callback($data, $length, $metrics) {
+ echo $data .PHP_EOL;
+ return file_exists(dirname(__FILE__) . '/STOP');
+}
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$method = 'https://stream.twitter.com/1/statuses/filter.json';
+
+// show Tweets which contan the word twitter OR have been geo-tagged within
+// the bounding box -122.41,37.77,-122.40,37.78 OR are by themattharris
+
+$params = array(
+ 'track' => 'twitter',
+ // Around Twitter HQ. First param is the SW corner of the bounding box
+ 'locations' => '-122.41,37.77,-122.40,37.78',
+ 'follow' => '777925' // themattharris
+);
+
+$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
+
+// output any response we get back AFTER the Stream has stopped -- or it errors
+tmhUtilities::pr($tmhOAuth);
+
+?>
+
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/streaming_advanced.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/streaming_advanced.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/streaming_advanced.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,95 @@
+<?php
+
+/**
+ * Very basic streaming API example. In production you would store the
+ * received tweets in a queue or database for later processing.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) In a terminal or server type:
+ * php /path/to/here/streaming.php
+ * 5) To stop the Streaming API either press CTRL-C or, in the folder the
+ * script is running from type:
+ * touch STOP
+ *
+ * @author themattharris
+ */
+
+$count=0;
+$first_id=0;
+function my_streaming_callback($data, $length, $metrics) {
+ global $raw;
+ if ($raw) :
+ echo $data;
+ else :
+ $data = json_decode($data, true);
+
+ $date = strtotime($data['created_at']);
+ $data['text'] = str_replace(PHP_EOL, '', $data['text']);
+ $data['user']['screen_name'] = str_pad($data['user']['screen_name'], 15, ' ');
+ echo "{$data['id_str']}\t{$date}\t{$data['user']['screen_name']}\t\t{$data['text']}" . PHP_EOL;
+
+ global $count;
+ $count++;
+
+ global $first_id;
+ if ($first_id==0)
+ $first_id = $data['id_str'];
+ endif;
+
+ global $limit;
+ if ($count==$limit) :
+ return true;
+ endif;
+ return file_exists(dirname(__FILE__) . '/STOP');
+}
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$method = 'https://stream.twitter.com/1/statuses/filter.json';
+$track = tmhUtilities::read_input('Track terms. For multiple terms separate with commas (leave blank for none): ');
+$follow = tmhUtilities::read_input('Follow accounts. For multiple accounts separate with commas (leave blank for none): ');
+$locations = tmhUtilities::read_input('Bounding boxes (leave blank for none): ');
+$delimited = tmhUtilities::read_input('Delimited? (1,t,true): ');
+$limit = tmhUtilities::read_input('Stop after how many tweets? (leave blank for unlimited): ');
+$debug = tmhUtilities::read_input('Debug? (1,t,true): ');
+$raw = tmhUtilities::read_input('Raw output? (1,t,true): ');
+
+$true = array('1','t','true');
+
+$params = array();
+if (strlen($track) > 0)
+ $params['track'] = $track;
+if (strlen($follow) > 0)
+ $params['follow'] = $follow;
+if (strlen($locations) > 0)
+ $params['locations'] = $locations;
+if (in_array($delimited, $true))
+ $params['delimited'] = 'length';
+if (strlen($limit) > 0)
+ $limit = intval($limit);
+$debug = in_array($debug, $true);
+$raw = in_array($raw, $true);
+
+$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
+if ($debug)
+ var_dump($tmhOAuth);
+?>
+
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/tweet.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/tweet.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/tweet.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Tweets a message from the user whose user token and secret you use.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$code = $tmhOAuth->request('POST', $tmhOAuth->url('1/statuses/update'), array(
+ 'status' => 'My Twitter Message'
+));
+
+if ($code == 200) {
+ tmhUtilities::pr(json_decode($tmhOAuth->response['response']));
+} else {
+ tmhUtilities::pr($tmhOAuth->response['response']);
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/userstream.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/userstream.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/userstream.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * Very basic User Streams example. In production you would store the
+ * received tweets in a queue or database for later processing.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) In a terminal or server type:
+ * php /path/to/here/userstream.php
+ * 5) To stop the Streaming API either press CTRL-C or, in the folder the
+ * script is running from type:
+ * touch STOP
+ *
+ * @author themattharris
+ */
+
+function my_streaming_callback($data, $length, $metrics) {
+ echo $data .PHP_EOL;
+ return file_exists(dirname(__FILE__) . '/STOP');
+}
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$method = "https://userstream.twitter.com/2/user.json";
+$params = array(
+ // parameters go here
+);
+$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback', false);
+
+// output any response we get back AFTER the Stream has stopped -- or errors
+tmhUtilities::pr($tmhOAuth);
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/verify.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/verify.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/verify.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Verify the user token and secret works. If successful we will be given the
+ * details of the user. If not an error explaining why will be returned.
+ *
+ * Although this example uses your user token/secret, you can use
+ * the user token/secret of any user who has authorised your application.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) From the application details page copy the access token and access token
+ * secret into the place in this code marked with (A_USER_TOKEN
+ * and A_USER_SECRET)
+ * 4) Visit this page using your web browser.
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+ 'user_token' => 'A_USER_TOKEN',
+ 'user_secret' => 'A_USER_SECRET',
+));
+
+$code = $tmhOAuth->request('GET', $tmhOAuth->url('1/account/verify_credentials'));
+
+if ($code == 200) {
+ echo 'The access level of this token is: ' . $tmhOAuth->response['headers']['x_access_level'] . PHP_EOL;
+ tmhUtilities::pr($tmhOAuth->response);
+} else {
+ tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/examples/xauth.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/examples/xauth.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/examples/xauth.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,41 @@
+<?php
+
+/**
+ * Obtain a users token and secret using xAuth.
+ * Twitter must have granted you xAuth access to use this.
+ *
+ * Instructions:
+ * 1) If you don't have one already, create a Twitter application on
+ * https://dev.twitter.com/apps
+ * 2) From the application details page copy the consumer key and consumer
+ * secret into the place in this code marked with (YOUR_CONSUMER_KEY
+ * and YOUR_CONSUMER_SECRET)
+ * 3) Fill in the username and password of the user you wish to obtain
+ * the user token and secret for.
+ * 4) In a terminal or server type:
+ * php /path/to/here/xauth.php
+ *
+ * @author themattharris
+ */
+
+require '../tmhOAuth.php';
+require '../tmhUtilities.php';
+$tmhOAuth = new tmhOAuth(array(
+ 'consumer_key' => 'YOUR_CONSUMER_KEY',
+ 'consumer_secret' => 'YOUR_CONSUMER_SECRET',
+));
+
+$code = $tmhOAuth->request('POST', $tmhOAuth->url('oauth/access_token', ''), array(
+ 'x_auth_username' => '',
+ 'x_auth_password' => '',
+ 'x_auth_mode' => 'client_auth'
+));
+
+if ($code == 200) {
+ $tokens = $tmhOAuth->extract_params($tmhOAuth->response['response']);
+ tmhUtilities::pr($tokens);
+} else {
+ tmhUtilities::pr(htmlentities($tmhOAuth->response['response']));
+}
+
+?>
\ No newline at end of file
Added: trunk/mapbender/http/extensions/tmhOAuth/tmhOAuth.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/tmhOAuth.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/tmhOAuth.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,612 @@
+<?php
+/**
+ * tmhOAuth
+ *
+ * An OAuth 1.0A library written in PHP.
+ * The library supports file uploading using multipart/form as well as general
+ * REST requests. OAuth authentication is sent using the an Authorization Header.
+ *
+ * @author themattharris
+ * @version 0.56
+ *
+ * 29 September 2011
+ */
+class tmhOAuth {
+ const VERSION = 0.56;
+
+ /**
+ * Creates a new tmhOAuth object
+ *
+ * @param string $config, the configuration to use for this request
+ */
+ function __construct($config) {
+ $this->params = array();
+ $this->headers = array();
+ $this->auto_fixed_time = false;
+ $this->buffer = null;
+
+ // default configuration options
+ $this->config = array_merge(
+ array(
+ 'user_agent' => 'tmhOAuth ' . self::VERSION . ' - //github.com/themattharris/tmhOAuth',
+ 'consumer_key' => '',
+ 'consumer_secret' => '',
+ 'user_token' => '',
+ 'user_secret' => '',
+ 'use_ssl' => true,
+ 'host' => 'api.twitter.com',
+ 'debug' => false,
+ 'force_nonce' => false,
+ 'nonce' => false, // used for checking signatures. leave as false for auto
+ 'force_timestamp' => false,
+ 'timestamp' => false, // used for checking signatures. leave as false for auto
+ 'oauth_version' => '1.0',
+
+ // you probably don't want to change any of these curl values
+ 'curl_connecttimeout' => 30,
+ 'curl_timeout' => 10,
+ // for security you may want to set this to TRUE. If you do you need
+ // to install the servers certificate in your local certificate store.
+ 'curl_ssl_verifypeer' => false,
+ 'curl_followlocation' => false, // whether to follow redirects or not
+ // support for proxy servers
+ 'curl_proxy' => false, // really you don't want to use this if you are using streaming
+ 'curl_proxyuserpwd' => false, // format username:password for proxy, if required
+ 'curl_encoding' => '', // leave blank for all supported formats, else use gzip, deflate, identity
+
+ // streaming API
+ 'is_streaming' => false,
+ 'streaming_eol' => "\r\n",
+ 'streaming_metrics_interval' => 60,
+
+ // header or querystring. You should always use header
+ // this is just to help me debug other developers
+ // implementations
+ 'as_header' => true,
+ ),
+ $config
+ );
+ }
+
+ /**
+ * Generates a random OAuth nonce.
+ * If 'force_nonce' is true a nonce is not generated and the value in the configuration will be retained.
+ *
+ * @param string $length how many characters the nonce should be before MD5 hashing. default 12
+ * @param string $include_time whether to include time at the beginning of the nonce. default true
+ * @return void
+ */
+ private function create_nonce($length=12, $include_time=true) {
+ if ($this->config['force_nonce'] == false) {
+ $sequence = array_merge(range(0,9), range('A','Z'), range('a','z'));
+ $length = $length > count($sequence) ? count($sequence) : $length;
+ shuffle($sequence);
+
+ $prefix = $include_time ? microtime() : '';
+ $this->config['nonce'] = md5(substr($prefix . implode($sequence), 0, $length));
+ }
+ }
+
+ /**
+ * Generates a timestamp.
+ * If 'force_timestamp' is true a nonce is not generated and the value in the configuration will be retained.
+ *
+ * @return void
+ */
+ private function create_timestamp() {
+ $this->config['timestamp'] = ($this->config['force_timestamp'] == false ? time() : $this->config['timestamp']);
+ }
+
+ /**
+ * Encodes the string or array passed in a way compatible with OAuth.
+ * If an array is passed each array value will will be encoded.
+ *
+ * @param mixed $data the scalar or array to encode
+ * @return $data encoded in a way compatible with OAuth
+ */
+ private function safe_encode($data) {
+ if (is_array($data)) {
+ return array_map(array($this, 'safe_encode'), $data);
+ } else if (is_scalar($data)) {
+ return str_ireplace(
+ array('+', '%7E'),
+ array(' ', '~'),
+ rawurlencode($data)
+ );
+ } else {
+ return '';
+ }
+ }
+
+ /**
+ * Decodes the string or array from it's URL encoded form
+ * If an array is passed each array value will will be decoded.
+ *
+ * @param mixed $data the scalar or array to decode
+ * @return $data decoded from the URL encoded form
+ */
+ private function safe_decode($data) {
+ if (is_array($data)) {
+ return array_map(array($this, 'safe_decode'), $data);
+ } else if (is_scalar($data)) {
+ return rawurldecode($data);
+ } else {
+ return '';
+ }
+ }
+
+ /**
+ * Returns an array of the standard OAuth parameters.
+ *
+ * @return array all required OAuth parameters, safely encoded
+ */
+ private function get_defaults() {
+ $defaults = array(
+ 'oauth_version' => $this->config['oauth_version'],
+ 'oauth_nonce' => $this->config['nonce'],
+ 'oauth_timestamp' => $this->config['timestamp'],
+ 'oauth_consumer_key' => $this->config['consumer_key'],
+ 'oauth_signature_method' => 'HMAC-SHA1',
+ );
+
+ // include the user token if it exists
+ if ( $this->config['user_token'] )
+ $defaults['oauth_token'] = $this->config['user_token'];
+
+ // safely encode
+ foreach ($defaults as $k => $v) {
+ $_defaults[$this->safe_encode($k)] = $this->safe_encode($v);
+ }
+
+ return $_defaults;
+ }
+
+ /**
+ * Extracts and decodes OAuth parameters from the passed string
+ *
+ * @param string $body the response body from an OAuth flow method
+ * @return array the response body safely decoded to an array of key => values
+ */
+ function extract_params($body) {
+ $kvs = explode('&', $body);
+ $decoded = array();
+ foreach ($kvs as $kv) {
+ $kv = explode('=', $kv, 2);
+ $kv[0] = $this->safe_decode($kv[0]);
+ $kv[1] = $this->safe_decode($kv[1]);
+ $decoded[$kv[0]] = $kv[1];
+ }
+ return $decoded;
+ }
+
+ /**
+ * Prepares the HTTP method for use in the base string by converting it to
+ * uppercase.
+ *
+ * @param string $method an HTTP method such as GET or POST
+ * @return void value is stored to a class variable
+ * @author themattharris
+ */
+ private function prepare_method($method) {
+ $this->method = strtoupper($method);
+ }
+
+ /**
+ * Prepares the URL for use in the base string by ripping it apart and
+ * reconstructing it.
+ *
+ * Ref: 3.4.1.2
+ *
+ * @param string $url the request URL
+ * @return void value is stored to a class variable
+ * @author themattharris
+ */
+ private function prepare_url($url) {
+ $parts = parse_url($url);
+
+ $port = isset($parts['port']) ? $parts['port'] : false;
+ $scheme = $parts['scheme'];
+ $host = $parts['host'];
+ $path = isset($parts['path']) ? $parts['path'] : false;
+
+ $port or $port = ($scheme == 'https') ? '443' : '80';
+
+ if (($scheme == 'https' && $port != '443')
+ || ($scheme == 'http' && $port != '80')) {
+ $host = "$host:$port";
+ }
+ $this->url = strtolower("$scheme://$host$path");
+ }
+
+ /**
+ * Prepares all parameters for the base string and request.
+ * Multipart parameters are ignored as they are not defined in the specification,
+ * all other types of parameter are encoded for compatibility with OAuth.
+ *
+ * @param array $params the parameters for the request
+ * @return void prepared values are stored in class variables
+ */
+ private function prepare_params($params) {
+ // do not encode multipart parameters, leave them alone
+ if ($this->config['multipart']) {
+ $this->request_params = $params;
+ $params = array();
+ }
+
+ // signing parameters are request parameters + OAuth default parameters
+ $this->signing_params = array_merge($this->get_defaults(), (array)$params);
+
+ // Remove oauth_signature if present
+ // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
+ if (isset($this->signing_params['oauth_signature'])) {
+ unset($this->signing_params['oauth_signature']);
+ }
+
+ // Parameters are sorted by name, using lexicographical byte value ordering.
+ // Ref: Spec: 9.1.1 (1)
+ uksort($this->signing_params, 'strcmp');
+
+ // encode. Also sort the signed parameters from the POST parameters
+ foreach ($this->signing_params as $k => $v) {
+ $k = $this->safe_encode($k);
+ $v = $this->safe_encode($v);
+ $_signing_params[$k] = $v;
+ $kv[] = "{$k}={$v}";
+ }
+
+ // auth params = the default oauth params which are present in our collection of signing params
+ $this->auth_params = array_intersect_key($this->get_defaults(), $_signing_params);
+ if (isset($_signing_params['oauth_callback'])) {
+ $this->auth_params['oauth_callback'] = $_signing_params['oauth_callback'];
+ unset($_signing_params['oauth_callback']);
+ }
+
+ // request_params is already set if we're doing multipart, if not we need to set them now
+ if ( ! $this->config['multipart'])
+ $this->request_params = array_diff_key($_signing_params, $this->get_defaults());
+
+ // create the parameter part of the base string
+ $this->signing_params = implode('&', $kv);
+ }
+
+ /**
+ * Prepares the OAuth signing key
+ *
+ * @return void prepared signing key is stored in a class variables
+ */
+ private function prepare_signing_key() {
+ $this->signing_key = $this->safe_encode($this->config['consumer_secret']) . '&' . $this->safe_encode($this->config['user_secret']);
+ }
+
+ /**
+ * Prepare the base string.
+ * Ref: Spec: 9.1.3 ("Concatenate Request Elements")
+ *
+ * @return void prepared base string is stored in a class variables
+ */
+ private function prepare_base_string() {
+ $base = array(
+ $this->method,
+ $this->url,
+ $this->signing_params
+ );
+ $this->base_string = implode('&', $this->safe_encode($base));
+ }
+
+ /**
+ * Prepares the Authorization header
+ *
+ * @return void prepared authorization header is stored in a class variables
+ */
+ private function prepare_auth_header() {
+ $this->headers = array();
+ uksort($this->auth_params, 'strcmp');
+ if (!$this->config['as_header']) :
+ $this->request_params = array_merge($this->request_params, $this->auth_params);
+ return;
+ endif;
+
+ foreach ($this->auth_params as $k => $v) {
+ $kv[] = "{$k}=\"{$v}\"";
+ }
+ $this->auth_header = 'OAuth ' . implode(', ', $kv);
+ $this->headers['Authorization'] = $this->auth_header;
+ }
+
+ /**
+ * Signs the request and adds the OAuth signature. This runs all the request
+ * parameter preparation methods.
+ *
+ * @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
+ * @param string $url the request URL without query string parameters
+ * @param array $params the request parameters as an array of key=value pairs
+ * @param string $useauth whether to use authentication when making the request.
+ */
+ private function sign($method, $url, $params, $useauth) {
+ $this->prepare_method($method);
+ $this->prepare_url($url);
+ $this->prepare_params($params);
+
+ // we don't sign anything is we're not using auth
+ if ($useauth) {
+ $this->prepare_base_string();
+ $this->prepare_signing_key();
+
+ $this->auth_params['oauth_signature'] = $this->safe_encode(
+ base64_encode(
+ hash_hmac(
+ 'sha1', $this->base_string, $this->signing_key, true
+ )));
+
+ $this->prepare_auth_header();
+ }
+ }
+
+ /**
+ * Make an HTTP request using this library. This method doesn't return anything.
+ * Instead the response should be inspected directly.
+ *
+ * @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
+ * @param string $url the request URL without query string parameters
+ * @param array $params the request parameters as an array of key=value pairs
+ * @param string $useauth whether to use authentication when making the request. Default true.
+ * @param string $multipart whether this request contains multipart data. Default false
+ */
+ function request($method, $url, $params=array(), $useauth=true, $multipart=false) {
+ $this->config['multipart'] = $multipart;
+
+ $this->create_nonce();
+ $this->create_timestamp();
+
+ $this->sign($method, $url, $params, $useauth);
+ return $this->curlit();
+ }
+
+ /**
+ * Make a long poll HTTP request using this library. This method is
+ * different to the other request methods as it isn't supposed to disconnect
+ *
+ * Using this method expects a callback which will receive the streaming
+ * responses.
+ *
+ * @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
+ * @param string $url the request URL without query string parameters
+ * @param array $params the request parameters as an array of key=value pairs
+ * @param string $callback the callback function to stream the buffer to.
+ */
+ function streaming_request($method, $url, $params=array(), $callback='') {
+ if ( ! empty($callback) ) {
+ if ( ! function_exists($callback) ) {
+ return false;
+ }
+ $this->config['streaming_callback'] = $callback;
+ }
+ $this->metrics['start'] = time();
+ $this->metrics['interval_start'] = $this->metrics['start'];
+ $this->metrics['tweets'] = 0;
+ $this->metrics['last_tweets'] = 0;
+ $this->metrics['bytes'] = 0;
+ $this->metrics['last_bytes'] = 0;
+ $this->config['is_streaming'] = true;
+ $this->request($method, $url, $params);
+ }
+
+ /**
+ * Handles the updating of the current Streaming API metrics.
+ */
+ function update_metrics() {
+ $now = time();
+ if (($this->metrics['interval_start'] + $this->config['streaming_metrics_interval']) > $now)
+ return false;
+
+ $this->metrics['tps'] = round( ($this->metrics['tweets'] - $this->metrics['last_tweets']) / $this->config['streaming_metrics_interval'], 2);
+ $this->metrics['bps'] = round( ($this->metrics['bytes'] - $this->metrics['last_bytes']) / $this->config['streaming_metrics_interval'], 2);
+
+ $this->metrics['last_bytes'] = $this->metrics['bytes'];
+ $this->metrics['last_tweets'] = $this->metrics['tweets'];
+ $this->metrics['interval_start'] = $now;
+ return $this->metrics;
+ }
+
+ /**
+ * Utility function to create the request URL in the requested format
+ *
+ * @param string $request the API method without extension
+ * @param string $format the format of the response. Default json. Set to an empty string to exclude the format
+ * @return string the concatenation of the host, API version, API method and format
+ */
+ function url($request, $format='json') {
+ $format = strlen($format) > 0 ? ".$format" : '';
+ $proto = $this->config['use_ssl'] ? 'https:/' : 'http:/';
+
+ // backwards compatibility with v0.1
+ if (isset($this->config['v']))
+ $this->config['host'] = $this->config['host'] . '/' . $this->config['v'];
+
+ return implode('/', array(
+ $proto,
+ $this->config['host'],
+ $request . $format
+ ));
+ }
+
+ /**
+ * Public access to the private safe decode/encode methods
+ *
+ * @param string $text the text to transform
+ * @param string $mode the transformation mode. either encode or decode
+ * @return the string as transformed by the given mode
+ */
+ function transformText($text, $mode='encode') {
+ return $this->{"safe_$mode"}($text);
+ }
+
+ /**
+ * Utility function to parse the returned curl headers and store them in the
+ * class array variable.
+ *
+ * @param object $ch curl handle
+ * @param string $header the response headers
+ * @return the string length of the header
+ */
+ private function curlHeader($ch, $header) {
+ $i = strpos($header, ':');
+ if ( ! empty($i) ) {
+ $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
+ $value = trim(substr($header, $i + 2));
+ $this->response['headers'][$key] = $value;
+ }
+ return strlen($header);
+ }
+
+ /**
+ * Utility function to parse the returned curl buffer and store them until
+ * an EOL is found. The buffer for curl is an undefined size so we need
+ * to collect the content until an EOL is found.
+ *
+ * This function calls the previously defined streaming callback method.
+ *
+ * @param object $ch curl handle
+ * @param string $data the current curl buffer
+ */
+ private function curlWrite($ch, $data) {
+ $l = strlen($data);
+ if (strpos($data, $this->config['streaming_eol']) === false) {
+ $this->buffer .= $data;
+ return $l;
+ }
+
+ $buffered = explode($this->config['streaming_eol'], $data);
+ $content = $this->buffer . $buffered[0];
+
+ $this->metrics['tweets']++;
+ $this->metrics['bytes'] += strlen($content);
+
+ if ( ! function_exists($this->config['streaming_callback']))
+ return 0;
+
+ $metrics = $this->update_metrics();
+ $stop = call_user_func(
+ $this->config['streaming_callback'],
+ $content,
+ strlen($content),
+ $metrics
+ );
+ $this->buffer = $buffered[1];
+ if ($stop)
+ return 0;
+
+ return $l;
+ }
+
+ /**
+ * Makes a curl request. Takes no parameters as all should have been prepared
+ * by the request method
+ *
+ * @return void response data is stored in the class variable 'response'
+ */
+ private function curlit() {
+ // method handling
+ switch ($this->method) {
+ case 'POST':
+ break;
+ default:
+ // GET, DELETE request so convert the parameters to a querystring
+ if ( ! empty($this->request_params)) {
+ foreach ($this->request_params as $k => $v) {
+ // Multipart params haven't been encoded yet.
+ // Not sure why you would do a multipart GET but anyway, here's the support for it
+ if ($this->config['multipart']) {
+ $params[] = $this->safe_encode($k) . '=' . $this->safe_encode($v);
+ } else {
+ $params[] = $k . '=' . $v;
+ }
+ }
+ $qs = implode('&', $params);
+ $this->url = strlen($qs) > 0 ? $this->url . '?' . $qs : $this->url;
+ $this->request_params = array();
+ }
+ break;
+ }
+
+ // configure curl
+ $c = curl_init();
+ curl_setopt_array($c, array(
+ CURLOPT_USERAGENT => $this->config['user_agent'],
+ CURLOPT_CONNECTTIMEOUT => $this->config['curl_connecttimeout'],
+ CURLOPT_TIMEOUT => $this->config['curl_timeout'],
+ CURLOPT_RETURNTRANSFER => TRUE,
+ CURLOPT_SSL_VERIFYPEER => $this->config['curl_ssl_verifypeer'],
+ CURLOPT_FOLLOWLOCATION => $this->config['curl_followlocation'],
+ CURLOPT_PROXY => $this->config['curl_proxy'],
+ CURLOPT_ENCODING => $this->config['curl_encoding'],
+ CURLOPT_URL => $this->url,
+ // process the headers
+ CURLOPT_HEADERFUNCTION => array($this, 'curlHeader'),
+ CURLOPT_HEADER => FALSE,
+ CURLINFO_HEADER_OUT => true,
+ ));
+
+ if ($this->config['curl_proxyuserpwd'] !== false)
+ curl_setopt($c, CURLOPT_PROXYUSERPWD, $this->config['curl_proxyuserpwd']);
+
+ if ($this->config['is_streaming']) {
+ // process the body
+ $this->response['content-length'] = 0;
+ curl_setopt($c, CURLOPT_TIMEOUT, 0);
+ curl_setopt($c, CURLOPT_WRITEFUNCTION, array($this, 'curlWrite'));
+ }
+
+ switch ($this->method) {
+ case 'GET':
+ break;
+ case 'POST':
+ curl_setopt($c, CURLOPT_POST, TRUE);
+ break;
+ default:
+ curl_setopt($c, CURLOPT_CUSTOMREQUEST, $this->method);
+ }
+
+ if ( ! empty($this->request_params) ) {
+ // if not doing multipart we need to implode the parameters
+ if ( ! $this->config['multipart'] ) {
+ foreach ($this->request_params as $k => $v) {
+ $ps[] = "{$k}={$v}";
+ }
+ $this->request_params = implode('&', $ps);
+ }
+ curl_setopt($c, CURLOPT_POSTFIELDS, $this->request_params);
+ } else {
+ // CURL will set length to -1 when there is no data, which breaks Twitter
+ $this->headers['Content-Type'] = '';
+ $this->headers['Content-Length'] = '';
+ }
+
+ // CURL defaults to setting this to Expect: 100-Continue which Twitter rejects
+ $this->headers['Expect'] = '';
+
+ if ( ! empty($this->headers)) {
+ foreach ($this->headers as $k => $v) {
+ $headers[] = trim($k . ': ' . $v);
+ }
+ curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
+ }
+
+ if (isset($this->config['prevent_request']) && false == $this->config['prevent_request'])
+ return;
+
+ // do it!
+ $response = curl_exec($c);
+ $code = curl_getinfo($c, CURLINFO_HTTP_CODE);
+ $info = curl_getinfo($c);
+ curl_close($c);
+
+ // store the response
+ $this->response['code'] = $code;
+ $this->response['response'] = $response;
+ $this->response['info'] = $info;
+ return $code;
+ }
+}
+
+?>
Added: trunk/mapbender/http/extensions/tmhOAuth/tmhUtilities.php
===================================================================
--- trunk/mapbender/http/extensions/tmhOAuth/tmhUtilities.php (rev 0)
+++ trunk/mapbender/http/extensions/tmhOAuth/tmhUtilities.php 2011-11-17 07:29:40 UTC (rev 8202)
@@ -0,0 +1,251 @@
+<?php
+/**
+ * tmhUtilities
+ *
+ * Helpful utility and Twitter formatting functions
+ *
+ * @author themattharris
+ * @version 0.2
+ *
+ * 29 September 2011
+ */
+class tmhUtilities {
+ /**
+ * Entifies the tweet using the given entities element
+ *
+ * @param array $tweet the json converted to normalised array
+ * @return the tweet text with entities replaced with hyperlinks
+ */
+ function entify($tweet, &$replacements=array()) {
+ $encoding = mb_internal_encoding();
+ mb_internal_encoding("UTF-8");
+
+ $keys = array();
+ // $replacements = array();
+ $is_retweet = false;
+
+ if (isset($tweet['retweeted_status'])) {
+ $tweet = $tweet['retweeted_status'];
+ $is_retweet = true;
+ }
+
+ if (!isset($tweet['entities'])) {
+ return $tweet['text'];
+ }
+
+ // prepare the entities
+ foreach ($tweet['entities'] as $type => $things) {
+ foreach ($things as $entity => $value) {
+ $tweet_link = "<a href=\"http://twitter.com/{$tweet['user']['screen_name']}/statuses/{$tweet['id']}\">{$tweet['created_at']}</a>";
+
+ switch ($type) {
+ case 'hashtags':
+ $href = "<a href=\"http://twitter.com/search?q=%23{$value['text']}\">#{$value['text']}</a>";
+ break;
+ case 'user_mentions':
+ $href = "@<a href=\"http://twitter.com/{$value['screen_name']}\" title=\"{$value['name']}\">{$value['screen_name']}</a>";
+ break;
+ case 'urls':
+ case 'media':
+ $url = empty($value['expanded_url']) ? $value['url'] : $value['expanded_url'];
+ $display = isset($value['display_url']) ? $value['display_url'] : str_replace('http://', '', $url);
+ // Not all pages are served in UTF-8 so you may need to do this ...
+ $display = urldecode(str_replace('%E2%80%A6', '…', urlencode($display)));
+ $href = "<a href=\"{$value['url']}\">{$display}</a>";
+ break;
+ }
+ $keys[$value['indices']['0']] = mb_substr(
+ $tweet['text'],
+ $value['indices']['0'],
+ $value['indices']['1'] - $value['indices']['0']
+ );
+ $replacements[$value['indices']['0']] = $href;
+ }
+ }
+
+ ksort($replacements);
+ $replacements = array_reverse($replacements, true);
+ $entified_tweet = $tweet['text'];
+ foreach ($replacements as $k => $v) {
+ // $entified_tweet = substr_replace($entified_tweet, $v, $k, strlen($keys[$k]));
+ $entified_tweet = mb_substr($entified_tweet, 0, $k).$v.mb_substr($entified_tweet, $k + strlen($keys[$k]));
+ }
+ $replacements = array(
+ 'replacements' => $replacements,
+ 'keys' => $keys
+ );
+
+ mb_internal_encoding($encoding);
+ return $entified_tweet;
+ }
+
+ /**
+ * Returns the current URL. This is instead of PHP_SELF which is unsafe
+ *
+ * @param bool $dropqs whether to drop the querystring or not. Default true
+ * @return string the current URL
+ */
+ function php_self($dropqs=true) {
+ $url = sprintf('%s://%s%s',
+ empty($_SERVER['HTTPS']) ? (@$_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http') : 'http',
+ $_SERVER['SERVER_NAME'],
+ $_SERVER['REQUEST_URI']
+ );
+
+ $parts = parse_url($url);
+
+ $port = $_SERVER['SERVER_PORT'];
+ $scheme = $parts['scheme'];
+ $host = $parts['host'];
+ $path = @$parts['path'];
+ $qs = @$parts['query'];
+
+ $port or $port = ($scheme == 'https') ? '443' : '80';
+
+ if (($scheme == 'https' && $port != '443')
+ || ($scheme == 'http' && $port != '80')) {
+ $host = "$host:$port";
+ }
+ $url = "$scheme://$host$path";
+ if ( ! $dropqs)
+ return "{$url}?{$qs}";
+ else
+ return $url;
+ }
+
+ function is_cli() {
+ return (PHP_SAPI == 'cli' && empty($_SERVER['REMOTE_ADDR']));
+ }
+
+ /**
+ * Debug function for printing the content of an object
+ *
+ * @param mixes $obj
+ */
+ function pr($obj) {
+
+ if (!self::is_cli())
+ echo '<pre style="word-wrap: break-word">';
+ if ( is_object($obj) )
+ print_r($obj);
+ elseif ( is_array($obj) )
+ print_r($obj);
+ else
+ echo $obj;
+ if (!self::is_cli())
+ echo '</pre>';
+ }
+
+ /**
+ * Make an HTTP request using this library. This method is different to 'request'
+ * because on a 401 error it will retry the request.
+ *
+ * When a 401 error is returned it is possible the timestamp of the client is
+ * too different to that of the API server. In this situation it is recommended
+ * the request is retried with the OAuth timestamp set to the same as the API
+ * server. This method will automatically try that technique.
+ *
+ * This method doesn't return anything. Instead the response should be
+ * inspected directly.
+ *
+ * @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
+ * @param string $url the request URL without query string parameters
+ * @param array $params the request parameters as an array of key=value pairs
+ * @param string $useauth whether to use authentication when making the request. Default true.
+ * @param string $multipart whether this request contains multipart data. Default false
+ */
+ function auto_fix_time_request($tmhOAuth, $method, $url, $params=array(), $useauth=true, $multipart=false) {
+ $tmhOAuth->request($method, $url, $params, $useauth, $multipart);
+
+ // if we're not doing auth the timestamp isn't important
+ if ( ! $useauth)
+ return;
+
+ // some error that isn't a 401
+ if ($tmhOAuth->response['code'] != 401)
+ return;
+
+ // some error that is a 401 but isn't because the OAuth token and signature are incorrect
+ // TODO: this check is horrid but helps avoid requesting twice when the username and password are wrong
+ if (stripos($tmhOAuth->response['response'], 'password') !== false)
+ return;
+
+ // force the timestamp to be the same as the Twitter servers, and re-request
+ $tmhOAuth->auto_fixed_time = true;
+ $tmhOAuth->config['force_timestamp'] = true;
+ $tmhOAuth->config['timestamp'] = strtotime($tmhOAuth->response['headers']['date']);
+ return $tmhOAuth->request($method, $url, $params, $useauth, $multipart);
+ }
+
+ /**
+ * Asks the user for input and returns the line they enter
+ *
+ * @param string $prompt the text to display to the user
+ * @return the text entered by the user
+ */
+ function read_input($prompt) {
+ echo $prompt;
+ $handle = fopen("php://stdin","r");
+ $data = fgets($handle);
+ return trim($data);
+ }
+
+ /**
+ * Get a password from the shell.
+ *
+ * This function works on *nix systems only and requires shell_exec and stty.
+ *
+ * @param boolean $stars Wether or not to output stars for given characters
+ * @return string
+ * @url http://www.dasprids.de/blog/2008/08/22/getting-a-password-hidden-from-stdin-with-php-cli
+ */
+ function read_password($prompt, $stars=false) {
+ echo $prompt;
+ $style = shell_exec('stty -g');
+
+ if ($stars === false) {
+ shell_exec('stty -echo');
+ $password = rtrim(fgets(STDIN), "\n");
+ } else {
+ shell_exec('stty -icanon -echo min 1 time 0');
+ $password = '';
+ while (true) :
+ $char = fgetc(STDIN);
+ if ($char === "\n") :
+ break;
+ elseif (ord($char) === 127) :
+ if (strlen($password) > 0) {
+ fwrite(STDOUT, "\x08 \x08");
+ $password = substr($password, 0, -1);
+ }
+ else
+ fwrite(STDOUT, "*");
+ $password .= $char;
+ endif;
+ endwhile;
+ }
+
+ // Reset
+ shell_exec('stty ' . $style);
+ echo PHP_EOL;
+ return $password;
+ }
+
+ /**
+ * Check if one string ends with another
+ *
+ * @param string $haystack the string to check inside of
+ * @param string $needle the string to check $haystack ends with
+ * @return true if $haystack ends with $needle, false otherwise
+ */
+ function endswith($haystack, $needle) {
+ $haylen = strlen($haystack);
+ $needlelen = strlen($needle);
+ if ($needlelen > $haylen)
+ return false;
+
+ return substr_compare($haystack, $needle, -$needlelen) === 0;
+ }
+}
+
+?>
\ No newline at end of file
More information about the Mapbender_commits
mailing list