Tag Archives: how to

Empty postfix mail queue

A quick way to empty your postfix mail queue (for whatever reason) can be done as follows fromout your console as root or equal:

 

root@localhost: for i in `mailq|grep '@' |awk {'print $1'}|grep -v '@'`; do postsuper -d $i ; done

Be sure you know what you are doing, this cannot be undone! You’re deleting all your messages in the current mail queue!

PHP Twitter OAuth integration

This is a small how-to for integrating Twitter status updates into your website using PHP.

After succesfully using basic authentication for several months, twitter introduced a new authentication method Oauth as of August 31 2010.

This meant that i had to look for a solution to rewrite several lines of code to enable the website to post tweets using the new method. This is a small howto to integrate the new OAuth method using a quite old PHP 5 version.

Prerequisites

  • Basic programming skills
  • Web- and FTP server access
  • PHP 5.1.6+
  • PEAR JSON Class (if you’re not on PHP >=5.2.0)
  • Registered application at dev.twitter.com
  • Oauth token and Oauth secret
  • Abraham Williams’ PHP Oauth library

Installation

Well, let’s get started, first and most important thing to do is to let Twitter know that you’re creating an application that want’s to post tweets:

  1. Go to http://dev.twitter.com and make sure you’ve signed in by using your already existing twitter account
  2. Register a new app using the button on the upper righthand side of the page
  3. Make sure you fill all the required field with genuine information, specifically pay attention to the following fields/choices:
    1. Application Type -> Choose for ‘Browser’
    2. Callback URL -> Choose a path where you will install your php files; http://<your.tld>/<path>/callback.php
    3. Default Access type -> Choose for ‘Read & Write’
  4. Click Register and agree with the terms of serivce, your application is now registered!
  5. An important page is shown now, make sure you keep the listed details somewhere safe, for this howto you will need the following:
    1. Consumer key
    2. Consumer secret
    3. My Access Token (not listed directly, but can be viewed by clicking the ‘My Access Token’ on the right side of the page.

The application is now registered at dev.twitter.com, please make sure that you download the following files:

If you’re not on PHP >=5.2.0:

Now we’re at the point that we can create the twitter application on the webserver:

  • Unzip both downloaded files and upload them to your webserver (make sure you’re on the right PHP version using the PEAR JSON Class)
  • If not on PHP >= 5.2.0 make sure you include the following piece of code somewhere before getting at the twitter part/including the Oauth library: (sorry for being a bit vage)
if (!function_exists('json_decode')) {
    function json_decode($content, $assoc=false) {
        require_once '/JSON.php';
        if ($assoc) {
            $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        }
        else {
            $json = new Services_JSON;
        }
        return $json-&gt;decode($content);
    }
}

Configurion of OAuth Library

At this point you have to configure the Oauth library to let it communicate with the Twitter API:

  • Go to the path on your FTP server where you’ve uploaded both or separeate classes
  • Edit config.php and make sure you list your customer key, consumer secret and callback url as you listed it at dev.twitter.com
  • Edit twitteroauth.php in the twitteroauth folder, make sure you change the following piece of code:
/**    * Set API URLS    */
function accessTokenURL()  { return 'https://api.twitter.com/oauth/access_token'; }
function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
function authorizeURL()    { return 'https://twitter.com/oauth/authorize'; }
function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }

To:

/**    * Set API URLS    */
function accessTokenURL()  { return 'https://api.twitter.com/oauth/access_token'; }
function authenticateURL() { return 'https://api.twitter.com/oauth/authenticate'; }
function authorizeURL()    { return 'https://api.twitter.com/oauth/authorize'; }
function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }

Integration with your PHP Code

Now integrate this piece of code into your php page where you want to post tweets to Twitter:

function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
	$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
	return $connection;
}
 
// Create a connection with Twitter API using access tokens
$connection = getConnectionWithAccessToken("YOUR_OAUTH_TOKEN", "YOUR_OAUTH_TOKEN_SECRET");
 
// Post message to twitter
$content = $connection->;post('statuses/update', array('status' =>; 'YOUR_TWEET'));

To debug the connection between the Twitter API and your code, use for example print_r($content).

I used the following sites to be able to make the above howto:

SSH Tunneling on XP

Bitvise Tunnelier is an excellent tool for SSH Tunneling and Terminal access over SSH. Tunnelier supplies a secure SSL connection to a SSH enabled system.

I personally use tunnelier to access my resources (shares, sites, proxy) at home fromout a secure tunnel which is setup to a SSH enabled machine. Tunnelier offers client-to-server forwarding which enables portmapping from your local machine to the remote machine.

For portmapping purposes i used the Microsoft Loopback Adapter with a specific (for me applicable) ip-address 1.2.3.4 .

Here’s a quick how to to get this solution working, download links are listed inside the how to.

1. Installing the Microsoft Loopback Adapter:

To manually install the Microsoft Loopback adapter in Windows XP, follow these steps:

  1. Click Start, and then click Control Panel.
  2. If you are in Classic view, click Switch to Category View under Control Panel in the left pane.
  3. Double-click Printers and Other Hardware, and then click Next.
  4. Under See Also in the left pane, click Add Hardware,and then click Next.
  5. Click Yes, I have already connected the hardware, and then click Next.
  6. At the bottom of the list, click Add a new hardware device, and then click Next.
  7. Click Install the hardware that I manually select from a list, and then click Next.
  8. Click Network adapters, and then click Next.
  9. In the Manufacturer box, click Microsoft.
  10. In the Network Adapter box, click Microsoft Loopback Adapter, and then clickNext.
  11. Click Finish.

After the adapter is installed successfully, you can manually configure its options, as with any other adapter. If the TCP/IP properties are configured to use DHCP, the adapter will eventually use an autonet address (169.254.x.x/16) because the adapter is not actually connected to any physical media.

Note By default, TCP/IP properties are configured to use DHCP.

2. Configure the Microsoft Loopback Adapter for usage with Tunnelier

  • Installing the Loopback Adapter:
    1. Open Start, Control Panel, Add Hardware.
    2. Click Next on the introduction dialog box.
    3. Select ‘Yes, I have already connected the hardware’ and click Next.
    4. Scroll to the bottom of the list of hardware, select ‘Add a new hardware device’, then click Next.
    5. Select ‘Install the hardware that I manually select from a list (Advanced)’, then click Next.
    6. Select ‘Network adapters’, then click Next.
    7. Select ‘Microsoft’ as the manufacturer, select ‘Microsoft Loopback Adapter’ as the adapter, then click Next.
    8. Click Next to confim the installation.
    9. Click Finish to complete the installation.
  • Configure the newly created adapter:
    1. Open Start, Control Panel, Network Connections.
    2. Select the newly created connection (should be named ‘Local Area Connection N’, where N is its order number).
    3. Right click on the selected connection and choose Properties from the menu.
    4. Confirm that ‘Microsoft Loopback Adapter’ (or ‘Microsoft Loopback Adapter #N’) is displayed in the ‘Connect Using:’ field. If it isn’t, return to step 2 and retry properties for another adapter.
    5. Uncheck (disable) everything except ‘Internet Protocol (TCP/IP)’.
    6. Select ‘Internet Protocol (TCP/IP)’, then click Properties to open ‘Internet Protocol (TCP/IP) Properties’.
    7. Select ‘Use the following IP address:’ and fill in the ‘IP address:’ and ‘Subnet mask:’ fields (e.g. 10.10.10.10, 255.255.255.0).
    8. Click Advanced to open ‘Advanced TCP/IP Settings’.
    9. Switch to WINS and select ‘Disable NetBIOS over TCP/IP’.
    10. Click OK to close ‘Advanced TCP/IP Settings’.
    11. Click OK to close the ‘Internet Protocol (TCP/IP) Properties’.
    12. Optionally (on XP only) uncheck (disable) ‘Notify me when this connection has limited or no conectivity’ to remove the connection warning icon from the system notification area.
    13. Click OK to close the connection properties.
  • Sources: Bitvise, Microsoft