User Tools

Site Tools


tutorial2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial2 [2019/04/24 13:54]
bangingheads
tutorial2 [2019/04/24 13:58]
bangingheads
Line 1: Line 1:
-~~INCLFORM~~ 
 <php> <php>
-echo "hello";+/* 
 +Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 
 +Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at 
 +    http://​aws.amazon.com/​apache2.0/​ 
 +or in the "​license"​ file accompanying this file. This file 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. 
 +*/
  
 +require '/​var/​www/​bangingheads.net/​api/​twitch.php';​
 +function getRandom($n) { 
 +    $characters = '​0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; ​
 +    $randomString = ''; ​
 +  ​
 +    for ($i = 0; $i < $n; $i++) { 
 +        $index = rand(0, strlen($characters) - 1); 
 +        $randomString .= $characters[$index]; ​
 +    } 
 +  ​
 +    return $randomString; ​
 +
 +$provider = new TwitchProvider([
 +    '​clientId' ​               => '​io6q886ojt4qv4qjx39io6z2dg2g3w', ​    // The client ID assigned when you created your application
 +    '​clientSecret' ​           => '​r7ntc98exwtys9rs7qdoqt1eliichr',​ // The client secret assigned when you created your application
 +    '​redirectUri' ​            => '​https://​www.bangingheads.net/​doku.php?​id=tutorial2', ​ // Your redirect URL you specified when you created your application
 +    '​scopes' ​                 => [''​] ​ // The scopes you would like to request
 +]);
 +// If we don't have an authorization code then get one
 +if (!isset($_GET['​code'​])) {
 +    // Fetch the authorization URL from the provider, and store state in session
 +    $authorizationUrl = $provider->​getAuthorizationUrl();​
 +    $_SESSION['​oauth2state'​] = $provider->​getState();​
 +    // Display link to start auth flow
 +    echo "<​html><​p>​Twitch authentication is required to generate a token.</​p><​p>​This stops other people from editing your account settings as anybody can if they know your token.</​p><​p>​Please do not share your information with anyone else, if somebody else would like to use the commands, have them create their own.</​p><​a href=\"​$authorizationUrl\"​ target=\"​_blank\"><​img src=\"​https://​www.bangingheads.net/​api/​connecttwitch.png\"/​ width=\"​200\"​ height=\"​40\"></​a></​html>";​
 +    //exit;
 +// Check given state against previously stored one to mitigate CSRF attack
 +} elseif (empty($_GET['​state'​]) || (isset($_SESSION['​oauth2state'​]) && $_GET['​state'​] !== $_SESSION['​oauth2state'​])) {
 +    if (isset($_SESSION['​oauth2state'​])) {
 +        unset($_SESSION['​oauth2state'​]);​
 +    }
 +    exit('​Invalid state'​);​
 +} else {
 +    try {
 +        // Get an access token using authorization code grant.
 +        $accessToken = $provider->​getAccessToken('​authorization_code',​ [
 +            '​code'​ => $_GET['​code'​]
 +        ]);
 +        // Using the access token, get user profile
 +        $resourceOwner = $provider->​getResourceOwner($accessToken);​
 +        $user = $resourceOwner->​toArray();​
 + $twitchusername = $user['​data'​][0]['​display_name'​];​
 +
 + $mysql = mysqli_connect('​localhost','​phpmyadmin','​SpaceDomination2018!','​palmer_api'​);​
 + $query="​SELECT * FROM `channels` WHERE `channel` ='​$twitchusername'";​
 + $rowSQL = mysqli_query($mysql,​ $query);
 + $row = mysqli_fetch_array($rowSQL);​
 + if(isset($row['​summoner'​])){$haschannel=1;​}else{$haschannel=0;​}
 + if($haschannel){
 + $authentication = $row['​authentication'​];​
 + }else{
 + $authentication = getRandom(10);​
 + mysqli_query($mysql,​ "​INSERT INTO `palmer_api`.`channels` (`id`, `channel`, `summoner`, `region`, `defaultregion`,​ `authentication`) VALUES (NULL, '​$twitchusername',​ '​null',​ '​NA',​ '​NA',​ '​$authentication'​);"​);​
 + }
 +        echo '<​html>';​
 + echo '<​style type="​text/​css">​pre {
 +    overflow: auto;
 +    overflow-x: auto;
 +    overflow-y: auto;
 +    word-wrap: normal;
 +    border: 1px solid #ccc;
 +    border-top-color:​ rgb(204, 204, 204);
 +    border-top-style:​ solid;
 +    border-top-width:​ 1px;
 +    border-right-color:​ rgb(204, 204, 204);
 +    border-right-style:​ solid;
 +    border-right-width:​ 1px;
 +    border-bottom-color:​ rgb(204, 204, 204);
 +    border-bottom-style:​ solid;
 +    border-bottom-width:​ 1px;
 +    border-left-color:​ rgb(204, 204, 204);
 +    border-left-style:​ solid;
 +    border-left-width:​ 1px;
 +    border-image-source:​ initial;
 +    border-image-slice:​ initial;
 +    border-image-width:​ initial;
 +    border-image-outset:​ initial;
 +    border-image-repeat:​ initial;
 +    border-radius:​ 2px;
 +    border-top-left-radius:​ 2px;
 +    border-top-right-radius:​ 2px;
 +    border-bottom-right-radius:​ 2px;
 +    border-bottom-left-radius:​ 2px;
 +    box-shadow: inset 0 0 0.5em #ccc;
 +    padding: .7em 1em;
 +    padding-top:​ 0.7em;
 +    padding-right:​ 1em;
 +    padding-bottom:​ 0.7em;
 +    padding-left:​ 1em;
 +}</​style>';​
 + echo '<​table>';​
 +        echo '<​tr><​th>​Username</​th><​td>'​ . htmlspecialchars($user['​data'​][0]['​display_name'​]) . '</​td></​tr>';​
 + echo '<​tr><​th>​API Key</​th><​td>'​. $authentication . '</​td></​tr>';​
 +        echo '</​table>';​
 + echo '<​br /><​h1>​Nightbot</​h1><​br />';​
 + echo '<​b>​Rank</​b>';​
 + echo "<​pre>​!addcom !rank \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=rank&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Winrate</​b>';​
 + echo "<​pre>​!addcom !winrate \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=winrate&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​MMR</​b>';​
 + echo "<​pre>​!addcom !mmr \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=MMR&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Matchup</​b>';​
 + echo "<​pre>​!addcom !matchup \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=matchup&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​OP.GG</​b>';​
 + echo "<​pre>​!addcom !opgg \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=opgg&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Set Summoner (Moderator Only)</​b>';​
 + echo "<​pre>​!addcom !setsummoner -ul=moderator \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=setsummoner&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Set Default Region (Broadcaster Only)</​b>';​
 + echo "<​pre>​!addcom !setregion -ul=owner \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\$(querystring)&​method=setregion&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​br /><​h1>​StreamElements</​h1><​br />';​
 + echo '<​b>​Rank</​b>';​
 + echo "<​pre>​!command add !rank \${customapi.https://​www.bangingheads.net/​api.php?​summoner=&​method=rank&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​Rank Of</​b>';​
 + echo "<​pre>​!command add !rankof \${customapi.https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=rank&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​Winrate</​b>';​
 + echo "<​pre>​!command add !winrate \${customapi.https://​www.bangingheads.net/​api.php?​summoner=&​method=winrate&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​Winrate Of</​b>';​
 + echo "<​pre>​!command add !winrateof \${customapi.https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=winrate&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​MMR</​b>';​
 + echo "<​pre>​!command add !mmr \${customapi.https://​www.bangingheads.net/​api.php?​summoner=&​method=MMR&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​MMR Of</​b>';​
 + echo "<​pre>​!command add !mmrof \${customapi.https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=rank&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​Matchup</​b>';​
 + echo "<​pre>​!command add !matchup \${customapi.https://​www.bangingheads.net/​api.php?​summoner=&​method=matchup&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​Matchup Of</​b>';​
 + echo "<​pre>​!command add !matchupof \${customapi.https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=matchup&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​OP.GG</​b>';​
 + echo "<​pre>​!command add !opgg \${customapi.https://​www.bangingheads.net/​api.php?​summoner=&​method=opgg&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​OP.GG Of</​b>';​
 + echo "<​pre>​!command add !opggof \${customapi.https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=matchup&​channel=\${channel}&​key=$authentication}</​pre><​br />";​
 + echo '<​b>​Set Summoner (Moderator Only)</​b>';​
 + echo "<​pre>​!command add !setsummoner -level 500 \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=setsummoner&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Set Default Region (Broadcaster Only)</​b>';​
 + echo "<​pre>​!command add !setregion -level 1500 \$(urlfetch https://​www.bangingheads.net/​api.php?​summoner=\${1:​}&​method=setregion&​channel=\$(channel)&​key=$authentication)</​pre><​br />";​
 + echo '<​br /><​h1>​StreamLabs (Ankhbot)</​h1><​br />';​
 + echo '<​b>​Rank</​b>';​
 + echo "<​pre>​!command add !rank \$readapi(https://​www.bangingheads.net/​api.php?​summoner=&​method=rank&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Rank Of</​b>';​
 + echo "<​pre>​!command add !rankof \$readapi(https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=rank&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Winrate</​b>';​
 + echo "<​pre>​!command add !winrate \$readapi(https://​www.bangingheads.net/​api.php?​summoner=&​method=winrate&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Winrate Of</​b>';​
 + echo "<​pre>​!command add !winrateof \$readapi(https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=winrate&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​MMR</​b>';​
 + echo "<​pre>​!command add !mmr \$readapi(https://​www.bangingheads.net/​api.php?​summoner=&​method=MMR&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​MMR Of</​b>';​
 + echo "<​pre>​!command add !mmrof \$readapi(https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=mmr&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Matchup</​b>';​
 + echo "<​pre>​!command add !matchup \$readapi(https://​www.bangingheads.net/​api.php?​summoner=&​method=matchup&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Matchup Of</​b>';​
 + echo "<​pre>​!command add !matchupof \$readapi(https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=matchup&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​OP.GG</​b>';​
 + echo "<​pre>​!command add !opgg \$readapi(https://​www.bangingheads.net/​api.php?​summoner=&​method=opgg&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​OP.GG Of</​b>';​
 + echo "<​pre>​!command add !opggof \$readapi(https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=opgg&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Set Summoner (Moderator Only)</​b>';​
 + echo "<​pre>​!command add !setsummoner +m \$(readapi https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=setsummoner&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 + echo '<​b>​Set Default Region (Broadcaster Only)</​b>';​
 + echo "<​pre>​!command add !setregion +c \$readapi(https://​www.bangingheads.net/​api.php?​summoner=\$msg&​method=setregion&​channel=\$mychannel&​key=$authentication)</​pre><​br />";​
 +
 + echo '</​html>';​
 +        // You can now create authenticated API requests through the provider.
 +        //$request = $provider->​getAuthenticatedRequest(
 +        //    '​GET',​
 +        //    '​https://​api.twitch.tv/​kraken/​user',​
 +        //    $accessToken
 +        //);
 +
 +    } catch (Exception $e) {
 +        exit('​Caught exception: '​.$e->​getMessage());​
 +    }
 +}
 </​php>​ </​php>​
tutorial2.txt · Last modified: 2019/04/24 13:58 by bangingheads