<?

error_reporting ( 0 ) ;

set_time_limit ( 0 ) ; // just in case

$ch = curl_init ( ) ;

curl_setopt ( $ch , CURLOPT_COOKIEJAR , 'cookie.txt' ) ;

curl_setopt ( $ch , CURLOPT_COOKIEFILE , 'cookie.txt' ) ;

curl_setopt ( $ch , CURLOPT_URL , 'http://www.reddit.com/api/login' ) ;

curl_setopt ( $ch , CURLOPT_POST , 1 ) ;

curl_setopt ( $ch , CURLOPT_POSTFIELDS , 'op=login-main&#####&passwd=#####' ) ; // log in to icon bot account

curl_exec ( $ch ) ;

curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ;

curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 ) ;

curl_setopt ( $ch , CURLOPT_URL , 'http://www.reddit.com/message/unread/.json' ) ;

$jsonresp = curl_exec ( $ch ) ; // grab contents of bot's inbox

curl_close ( $ch ) ;

$last_check = file_get_contents ( 'last_check.txt' ) ; // while not strictly necessary, reddit sometimes leaves read messages marked as unread, and so without this check you'd be wasting bandwidth by repeating a user's request

$json = json_decode ( $jsonresp , true ) ;

$inbox = $json [ 'data' ] [ 'children' ] ;

$user = array ( ) ;

$message = array ( ) ;

for ( $x = 0 ; $x < count ( $inbox ) ; $x ++ )

{

$subtime = $inbox [ $x ] [ 'data' ] [ 'created_utc' ] * 1 ;

if ( $subtime > $last_check ) // kill this if statement if don't mind duplicates

{

$user [ $x ] = $inbox [ $x ] [ 'data' ] [ 'author' ] ;

$message [ $user [ $x ] ] = $inbox [ $x ] [ 'data' ] [ 'body' ] ;

}

}

if ( count ( $user ) == 0 ) // no sense continuing if there are no new icons

die ( ) ;

$usrcss = file ( 'user_styles.css' , FILE_IGNORE_NEW_LINES ) ; // this, obviously, is where user CSS will go

$declarations = count ( $usrcss ) ;

for ( $x = 0 ; $x < count ( $user ) ; $x ++ )

for ( $y = 0 ; $y < $declarations ; $y ++ )

if ( strpos ( $usrcss [ $y ] , $id [ $user [ $x ] ] ) !== false )

unset ( $usrcss [ $y ] ) ; // delete old declaration if the user already has an icon

$usrcss = implode ( "

" , $usrcss ) ;

for ( $x = 0 ; $x < count ( $user ) ; $x ++ )

{

$icon = $message [ $user [ $x ] ] ;

$usrcss .= "

.author[href$='/{ $user [ $x ]}']:before {" . ###YOURCSSHERE### . "}";

}

$handle = fopen ( 'user_styles.css' , 'w' ) ;

fwrite ( $handle , $usrcss ) ;

fclose ( $handle ) ;

$ch = curl_init ( ) ;

curl_setopt ( $ch , CURLOPT_COOKIEJAR , 'cookie.txt' ) ;

curl_setopt ( $ch , CURLOPT_COOKIEFILE , 'cookie.txt' ) ;

curl_setopt ( $ch , CURLOPT_URL , 'http://www.reddit.com/post/login' ) ;

curl_setopt ( $ch , CURLOPT_POST , 1 ) ;

curl_setopt ( $ch , CURLOPT_POSTFIELDS , 'op=login-main&user=#####&passwd=#####' ) ; // log in to moderator account

curl_exec ( $ch ) ;

$finalcss = file_get_contents ( 'non_user_styles.css' ) . $usrcss ; // I keep all the non-user icon CSS in a separate file for easy combining of the two

curl_setopt ( $ch , CURLOPT_COOKIEJAR , 'cookie.txt' ) ;

curl_setopt ( $ch , CURLOPT_COOKIEFILE , 'cookie.txt' ) ;

curl_setopt ( $ch , CURLOPT_URL , 'http://reddit.com/api/subreddit_stylesheet' ) ;

curl_setopt ( $ch , CURLOPT_HTTPHEADER , array ( 'Host: www.reddit.com' , 'Pragma: no-cache' , 'Cache-Control: no-cache' , 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' , 'X-Requested-With: XMLHttpRequest' , 'Referer: http://www.reddit.com/r/anarchism/about/stylesheet' ) ) ;

curl_setopt ( $ch , CURLOPT_POST , 1 ) ;

curl_setopt ( $ch , CURLOPT_POSTFIELDS , "r=anarchism&op=save&thumbbucket=&stylesheet_contents=" . str_replace ( array ( '%0D%0A' , '%28' , '%29' ) , array ( '%0A' , '(' , ')' ) , urlencode ( $finalcss ) ) . "&uh=s2p0h5otgk692cf7bc7047bd116aa1178a883513bbe804dfd2" ) ; // that "uh" bit at the end is tricky; it's different per subreddit; I recommend using Firefox's Live HTTP Headers plugin to submit the stylesheet, then check the posted data to find your value and replace mine

curl_exec ( $ch ) ;

curl_close ( $ch ) ;

$cur = time ( ) ;

$handle = fopen ( 'last_check.txt' , 'w' ) ;

fwrite ( $handle , $cur ) ;

fclose ( $handle ) ;