Exemple #1
0
function displayItem($action = '')
{
    //print 'ACTION: '.$action.'<br>';
    switch ($action) {
        case "intro":
            showIntro();
            break;
        case "clearsession":
            session_destroy();
            break;
        case "logout":
            session_destroy();
            break;
        case "authenticates":
            if (0) {
                /* If oauth_token is missing get it */
                if ($_REQUEST['oauth_token'] != NULL && $_SESSION['oauth_state'] === 'start') {
                    $_SESSION['oauth_state'] = $state = 'returned';
                }
                print "State: " . $state . "<br>";
                switch ($state) {
                    default:
                        /* Create TwitterOAuth object with app key/secret */
                        $to = new TwitterOAuth($consumer_key, $consumer_secret);
                        /* Request tokens from twitter */
                        $tok = $to->getRequestToken();
                        /* Save tokens for later */
                        $_SESSION['oauth_request_token'] = $token = $tok['oauth_token'];
                        $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];
                        $_SESSION['oauth_state'] = "start";
                        /* Build the authorization URL */
                        $request_link = $to->getAuthorizeURL($token);
                        /* Build link that gets user to twitter to authorize the app */
                        $content = 'Click on the link to go to twitter to authorize your account.';
                        $content .= '<a href="' . $request_link . '">' . $request_link . '</a>';
                        break;
                    case 'returned':
                        /* If the access tokens are already set skip to the API call */
                        if ($_SESSION['oauth_access_token'] === NULL && $_SESSION['oauth_access_token_secret'] === NULL) {
                            /* Create TwitterOAuth object with app key/secret and token key/secret from default phase */
                            $to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_request_token'], $_SESSION['oauth_request_token_secret']);
                            /* Request access tokens from twitter */
                            $tok = $to->getAccessToken();
                            /* Save the access tokens. Normally these would be saved in a database for future use. */
                            $_SESSION['oauth_access_token'] = $tok['oauth_token'];
                            $_SESSION['oauth_access_token_secret'] = $tok['oauth_token_secret'];
                        }
                        /* Random copy */
                        $content = 'your account should now be registered with twitter. Check here:<br />';
                        $content .= '<a href="https://twitter.com/account/connections">https://twitter.com/account/connections</a>';
                        /* Create TwitterOAuth with app key/secret and user access key/secret */
                        $to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
                        /* Run request on twitter API as user. */
                        //Nathaniel's Additions
                        $to = new TwitterOAuth($consumer_key, $consumer_secret, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
                        $xml = new SimpleXMLElement($to->OAuthRequest('https://twitter.com/account/verify_credentials.xml', array(), 'GET'));
                        print_r($to->OAuthRequest('https://twitter.com/account/verify_credentials.xml', array(), 'GET'));
                        //print "|" . $_SESSION['oauth_access_token'] . " -- " . $_SESSION['oauth_access_token_secret'] . "|<br>";
                        $userobj = new User($xml, $_SESSION['oauth_access_token'], $_SESSION['oauth_access_token_secret']);
                        //print "|" . $_SESSION['oauth_access_token'] . " -- " . $_SESSION['oauth_access_token_secret'] . "|<br>";
                        //session_start();
                        $_SESSION['userLoggedInID'] = (string) $userobj->userid;
                        $userobj->display();
                        $db = new DB();
                        $db->open();
                        $db->insertUser($userobj);
                        $db->close();
                        break;
                }
                print 'User ID: ' . $_SESSION['userLoggedInID'] . '<br>';
                print_r($content);
            }
            break;
        case "loginas":
            if (!$_GET["id"]) {
                print 'Missing login id';
            }
            $db = new DB();
            $db->open();
            $thisuser = $db->getUserByID($_GET["id"]);
            $db->close();
            logInUser($thisuser);
            $thisuser->display();
            print 'Welcome ' . $_SESSION['userLoggedInName'] . '  <a href="./index.php?act=logout">Log Out</a><br>';
            break;
        case "login":
            print '
			<form name="login" action="index.php" method="get">
			Username:
			<input type="text" name="user" /><br>
			Password:
			<input type="password" name="pass"/><br>
			<input type="hidden" name="act" value="handlelogin"/>
			<input type="submit" value="Submit" />
			</form>
		';
            $text = $_GET["text"];
            break;
        case "handlelogin":
            if (!$_GET["user"] || !$_GET["pass"]) {
                print '<b> Log in to TweetSampler: </b><br>';
                print '
			<form name="login" action="index.php" method="get">
			Username:
			<input type="text" name="user" /><br>
			Password:
			<input type="password" name="pass"/><br>
			<input type="hidden" name="act" value="handlelogin"/>
			<input type="submit" value="Submit" />
			</form>
			';
            } else {
                $db = new DB();
                $db->open();
                if ($db->getUserLoggedIn($_GET["user"], $_GET["pass"])) {
                    print 'Success';
                } else {
                    print 'Failure';
                }
                $db->close();
            }
            break;
        case "updatestatus":
            print '<form name="input" action="" method="post">
		Tweet Content:<br>
		<textarea onkeyup="lengthchange(this);" id="tweettext" cols="50" rows="3"></textarea><br>
		Remaining: <span id="remaining">140</span> characters 
		<input type="button" value="Post" onClick="javascript:submitPost(\'' . $_SESSION["userLoggedInScreenName"] . '\');"/>
		</form>
		<span id="aftersubmit"></span>';
            break;
        case "oldupdatestatus":
            $t = new twitter();
            $text = $_GET["text"];
            echo "<b>Update Status: <b><br>";
            echo $text;
            $tweet = $t->update($text);
            if ($tweet != NULL) {
                $tweet->display();
            } else {
                print 'Error - Status update not posted.';
            }
            break;
        case "ajaxupdatestatus":
            $t = new twitter();
            $text = $_GET["text"];
            echo "<b>Update Status: <b><br>";
            echo $text;
            $t->update($text);
            break;
        case "updatetweets":
            $t = new twitter();
            echo "<b>Update Tweets: <b><br>";
            $t->showZoomedTweets(0, 300);
            break;
        case "deletetweets":
            $db = new DB();
            $db->open();
            echo "<b>Delete Tweets: <b><br>";
            $db->deleteAllTweets();
            $db->close();
            break;
        case "deleteusertweets":
            $db = new DB();
            $db->open();
            echo "<b>Delete User Tweets: <b><br>";
            $db->deleteUserTweets();
            $db->close();
            break;
        case "readtweet":
            $db = new DB();
            $db->open();
            $id = $_GET["id"];
            $db->readTweetByID($id);
            $db->close();
            break;
        case "showallusers":
            $db = new DB();
            $db->open();
            echo "<b>Show All Users: <b><br>";
            $db->getAllUsers();
            $db->close();
            break;
        case "showzoomedtweets":
            print '<div class="slider" id="slider01">
			<div class="left"></div>
			<div class="right"></div>
			<img src="img/knob.png" width="31" height="15" />
		</div>
		<div id="results">Results</div>';
            //Show zoomedTweets
            //for($i=1;$i<=20;$i++){
            //	print "<a href='./index.php?act=showzoomedtweets&zoom=". $i ."'> ". $i ." </a>";
            //	if($i != 20){
            //		print "|";
            //	} else {
            //		print "<br>";
            //	}
            //}
            //$db = new DB();
            //$db->open();
            //echo "<b>Show Zoomed Tweets: <b><br>";
            //$zoom = $_GET["zoom"];
            //$db->getZoomedTweets($zoom);
            //$db->close();
            break;
        case "showalltweets":
            $db = new DB();
            $db->open();
            echo "<b>Show All Tweets: </b><br>";
            $db->getAllTweetsUserBlind();
            $db->close();
            break;
        case "showallmytweets":
            $db = new DB();
            $db->open();
            echo "<b>Show All Tweets: </b><br>";
            $db->getAllTweets();
            $db->close();
            break;
        case "showunreadtweets":
            $db = new DB();
            $db->open();
            echo "<b>Show Unread Tweets: </b><br>";
            $db->getAllUnreadTweets();
            //$db->getXUnreadTweets();
            $db->close();
            break;
        case "showreadtweets":
            $db = new DB();
            $db->open();
            echo "<b>Show read Tweets: </b><br>";
            $db->getAllReadTweets();
            $db->close();
            break;
        case "showlocaltweet":
            $db = new DB();
            $db->open();
            echo "<b>Show Tweet by ID: </b><br>";
            $tweetid = $_GET["id"];
            if ($tweetid == NULL) {
                print "No tweetid entered.  Please try again";
                break;
            }
            $tweet = $db->getTweetByID($tweetid, $_SESSION['userLoggedInID']);
            if ($tweet == -1) {
                print 'Ooops - Tweet not found locally<br>';
            } else {
                $tweet->display();
            }
            $db->close();
            break;
        case "showlocaluser":
            print ' showlocaluser';
            $db = new DB();
            $db->open();
            echo "<b>Show User by ID: </b><br>";
            $userid = $_GET["id"];
            $user = $db->getUserByID($userid);
            $user->display();
            $db->close();
            break;
        default:
            showIntro();
    }
}