Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 149 lines (116 sloc) 4.115 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
<?php
/**
* WePing - Let multiple users post to your social sites using ping.fm
*
*/

$CONF['api_key'] = '695f8bc472032e929a67520eb6694bd9';
require_once('config.php');
error_reporting(E_ALL ^ E_NOTICE);

require_once('HTTPDigest.php');
$HTTPDigest =& new HTTPDigest();
$HTTPDigest->passwordsHashed = false;
$HTTPDigest->nonceLife = 12*60*60; //stay logged in 12 hours
$LOGIN = $HTTPDigest->authenticate($USERS);
if(!$LOGIN){
    $HTTPDigest->send();
    die('Not authenticated');
}


require_once('HTTPClient.php');
header('Content-Type: text/html; charset=UTF-8');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<title><?php echo htmlspecialchars($CONF['title'])?> - WePing</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<script language="JavaScript" type="text/javascript" src="script.js"></script>
<link rel="shortcut icon" type="image/png" href="weping.png" />
</head>
<body>
<div class="wrap">
<h1 id="popup"><?php echo htmlspecialchars($CONF['title'])?></h1>
<?php post(); ?>

<?php postForm(); ?>
<?php flush(); ob_flush() ?>

<?php getLatest(); ?>

<span id="login">/<?php echo htmlspecialchars($LOGIN)?></span>
<a class="foot" href="http://github.com/splitbrain/WePing">WePing</a>
</div>
</body>
</html>
<?php

function post(){
    global $LOGIN;

    if(!$_REQUEST['go']) return;
    $post = trim($_REQUEST['p']);
    if(!$post) return;

    $data = array(
        'post_method' => 'default',
        'body' => $post.' /'.$LOGIN,
    );

    $xml = apiCall('user.post',$data);
    if($xml === false){
        echo '<p class="error">Failed to post message.</p>';
    }else{
        echo '<p class="success">Message posted.</p>';
        $_REQUEST['p'] = '';

        if(!isset($_REQUEST['popup'])) sleep(5);
    }

}

function postForm(){

    echo '<form action="index.php" method="post" accept-charset="UTF-8">';
    echo '<p><textarea name="p" id="p" rows="5" cols="25">'.htmlspecialchars($_REQUEST['p']).'</textarea><br />';
    if(isset($_REQUEST['popup'])) echo '<input type="hidden" name="popup" value="1" />';
    echo '<input type="submit" name="go" value="Send to Services" /><span id="chars"></span></p>';
    echo '</form>';

}

function getLatest(){
    if(isset($_REQUEST['popup'])) return;

    $xml = apiCall('user.latest');
    if(!$xml){
        echo '<p class="error">Failed to fetch recent messages</p>';
        return;
    }

    echo '<ul>';
    foreach($xml->messages->message as $message){
        $body = base64_decode($message->content->body);
        $body = htmlspecialchars($body);
        $body = preg_replace_callback('/((https?|ftp):\/\/[\w-?&;:#~=\.\/\@]+[\w\/])/ui',
                                      'formatLink',$body);

        echo '<li>';
        echo $body;
        echo '<span>'.date('Y-m-d H:i',(int) $message->date['unix']).'</span> ';
        echo '</li>';
    }
    echo '</ul>';
}

function formatLink($match){
    global $CONF;

    $url = $match[1];
    str_replace("\\\\'","'",$url);

    // resolve short url
    if($CONF['longurl']){
        $http = new HTTPClient();
        $xml = $http->get('http://api.longurl.org/v2/expand?format=php&url='.urlencode($url));
        if($xml != false){
            $xml = unserialize($xml);

            $long = $xml['long-url'];
        }
    }

    if(!$long) $long = $url;

    $link = '<a href="'.$url.'" title="'.$long.'">'.$url.'</a>';
    return $link;
}


function apiCall($call,$data=array()){
    global $CONF;
    $http = new HTTPClient();

    $input = array('api_key' => $CONF['api_key'], 'user_app_key' => $CONF['app_key']);
    $input = array_merge($input,$data);

    $xml = $http->post('http://api.ping.fm/v1/'.$call,$input);
    if($xml === false) return $false;

    $xml = new SimpleXMLElement($xml);
    if($xml['status'] != 'OK') return false;

    return $xml;
}
Something went wrong with that request. Please try again.