<?php

//Definitions

//Your Sub-reddit! Nothing more than what comes after the /r/ (lower case please)

$sub_reddit = '' ;

//A moderator account must be the one to set users flair, info must be plaintext

$mod_account = '' ;

$mod_password = '' ;

//for now: go to http://reddit . com/api/me . json while logged into the defined account, and copy the listed modhash

$mod_hash = '' ;

//This is the temporary file where the login cookie will be stored to process the form, it must be created before use

$cookiejar = 'cookies.txt' ;

// Define the Encryption key for the PM

$key = '' ;

//Sprite Image as listed on the sub-reddit example: sprite-icons

$sprite_image = '' ;

//The URL of the sprite for the CSS

$sprite_url = '' ;

//Define the height of your sprite in pixels

$sprite_height = ;

//Define if the sprite should be :before or :after in the CSS

$sprite_position = 'before' ;

//Define the subject of the sent PM

$subject = 'Flair Confirmation -- Automatic Message do not Respond' ;

//Enter the names of your Flair here as an array, this is text that will be displayed on the selector

$flair = array (

) ;

function encode ( $string , $key ) {

$key = sha1 ( $key ) ;

$strLen = strlen ( $string ) ;

$keyLen = strlen ( $key ) ;

for ( $i = 0 ; $i < $strLen ; $i ++ ) {

$ordStr = ord ( substr ( $string , $i , 1 ) ) ;

if ( $j == $keyLen ) $j = 0 ;

$ordKey = ord ( substr ( $key , $j , 1 ) ) ;

$j ++;

$hash .= strrev ( base_convert ( dechex ( $ordStr + $ordKey ) , 16 , 36 ) ) ;

}

return $hash ;

}

function decode ( $string , $key ) {

$key = sha1 ( $key ) ;

$strLen = strlen ( $string ) ;

$keyLen = strlen ( $key ) ;

for ( $i = 0 ; $i < $strLen ; $i += 2 ) {

$ordStr = hexdec ( base_convert ( strrev ( substr ( $string , $i , 2 ) ) , 36 , 16 ) ) ;

if ( $j == $keyLen ) $j = 0 ;

$ordKey = ord ( substr ( $key , $j , 1 ) ) ;

$j ++;

$hash .= chr ( $ordStr - $ordKey ) ;

}

return $hash ;

}

//Login as the moderator account

$ch = curl_init ( ) ;

//Define the data to send as reddit's login

$post_data = 'user=' . $mod_account . '&passwd=' . $mod_password . '&api_type=json' ;

$options = array (

CURLOPT_URL => 'http://www.reddit.com/api/login/' ,

CURLOPT_RETURNTRANSFER => 1 ,

CURLOPT_POST => 1 ,

CURLOPT_POSTFIELDS => $post_data ,

CURLOPT_COOKIEJAR => $cookiejar

) ;

curl_setopt_array ( $ch , $options ) ;

$output = curl_exec ( $ch ) ;

curl_close ( $ch ) ;

if ( ! empty ( $_POST ) ) {

//Create the key= to encode

$key2encode = 'r=' . $sub_reddit . '&name=' . $_POST [ 'name' ] ;

if ( isset ( $_POST [ 'text' ] ) ) $key2encode .= '&text=' . $_POST [ 'text' ] ;

if ( isset ( $_POST [ 'css_class' ] ) ) $key2encode .= '&css_class=' . $_POST [ 'css_class' ] ;

$key2encode = encode ( $key2encode , $key ) ;

//The Default Message example: http://reddithon . com/flair . php

$message = 'http://' . $_SERVER [ 'SERVER_NAME' ] . $_SERVER [ 'PHP_SELF' ] . '?key=' . $key2encode ;

$ch = curl_init ( ) ;

$post_data = 'to=' . $_POST [ 'name' ] . '&subject=' . $subject . '&text=' . $message . '&uh=' . $mod_hash ;

$options = array (

CURLOPT_URL => 'http://www.reddit.com/api/compose' ,

CURLOPT_RETURNTRANSFER => 1 ,

CURLOPT_POST => 1 ,

CURLOPT_POSTFIELDS => $post_data ,

CURLOPT_COOKIEFILE => $cookiejar

) ;

curl_setopt_array ( $ch , $options ) ;

$comment = curl_exec ( $ch ) ;

curl_close ( $ch ) ;

?>

<!-- Redirect to your sub-reddit -->

<script type='text/javascript'>

window.location = 'http://reddit.com/r/ <?php echo $sub_reddit ; ?> '

</script>

<?php

}

if ( isset ( $_GET [ 'key' ] ) ) {

//Decode the sent key

$post_data = decode ( $_GET [ 'key' ] , $key ) . '&uh=' . $mod_hash ;

$ch = curl_init ( ) ;

$options = array (

CURLOPT_URL => 'http://www.reddit.com/api/flair' ,

CURLOPT_RETURNTRANSFER => 1 ,

CURLOPT_POST => 1 ,

CURLOPT_POSTFIELDS => $post_data ,

CURLOPT_COOKIEFILE => $cookiejar

) ;

curl_setopt_array ( $ch , $options ) ;

$output = curl_exec ( $ch ) ;

curl_close ( $ch ) ;

?>

<!-- Redirect to your sub-reddit -->

<script type='text/javascript'>

window.location = 'http://reddit.com/r/ <?php echo $sub_reddit ; ?> '

</script>

<?php

}

?>

<head>

<title>Your Sub-Reddit's Flair Bot!</title>

<style>

.sprite-icon {

/* MUST SET BACKGROUND IMAGE */

background-image: url( <?php echo $sprite_url ?> );

width: 28px;

height: 28px;

display: inline-block;

margin: 1px;

margin-left: 0px;

}

#flair {

width: 225px;

margin: 0;

float: left;

}

</style>

</head>

<body>

<?php $minimum_pieces_of_flair = count ( $flair ) ; ?>

<!-- Created by m4rx -->

<div id='container'>

<div id='reddit-select'>

<form action='' method='POST'>

<div class='flair-text'>

<label>Flair Text:</label>

<input type='text' size='20' name='text'/>

</div>

<div id='username' class='reddit-account'>

<label>Reddit Account Name:</label>

<input style='' size='20' type='text' name='name'/>*Case Sensitive

</div>

<div class='sprite-select'>

<h3>Select Flair</h3>

<ul id='sprite-list'>

<?php

$temp = 0 ;

$height = 0 ;

if ( isset ( $_GET [ 'css' ] ) ) {

echo ' . flair:before { background: url(%%' . $sprite_image . '%%) no-repeat scroll -9999px -9999px transparent; display: inline-block; height: ' . $sprite_height . 'px; width: ' . $sprite_height . 'px; margin-right: 3px; vertical-align: middle;}<br />' ;

}

while ( $temp < $minimum_pieces_of_flair ) {

if ( isset ( $_GET [ 'css' ] ) ) {

echo '.flair-' . strtolower ( str_replace ( ' ' , '-' , $flair [ $temp ] ) ) . ':before {content: ""; background-position: 0 ' . $height . 'px;}<br />' ;

}

else {

?>

<li id='flair'>

<input name='css_class' type='radio' id=' <?php echo $flair [ $temp ] ?> ' style='display: inline; float: left; margin-top: 7px;' value=' <?php echo strtolower ( str_replace ( ' ' , '-' , $flair [ $temp ] ) ) ; ?> ' />

<label for=' <?php echo $flair [ $temp ] ?> '>

<div class='sprite-icon' style='margin-right: 2px; float: left; background-position: 0px <?php echo $height ; ?> px;'></div>

<div class='flair-name'> <?php echo $flair [ $temp ] ; ?> </div>

</label>

</li>

<?php

}

$height = ( $height - $sprite_height ) ;

$temp = ( $temp + 1 ) ;

}

?>

</ul>

</div>

<div class='save' style='float:right; padding: 10px;'>

<input type = 'submit' value = 'Save'/>

</div>

</form>

</div>

</div><!-- #container -->