, LJ username: tgies * Released under a Creative Commons Attribution-ShareAlike 2.5 license. * See http://creativecommons.org/licenses/by-sa/2.5/ for more info. * Put simply: Make derivative works, give me credit, let everyone else do the same. * Thanks (in fairly random order): * Sean "Redlof" Murray: Some questions. LJ username: redlof * Paul "Slugbug" Millar: Assisted in determining which code comments were hilarious. * See his LJ: tehslugbug, or his website: wgiowrb.net * Suuji "Suujita" Tanaka: Provided much-needed distraction and interruption. * Catering. Baselessly insulted programming skills for pure hilarity value. * Made a few small contributions here and there, increasing overall speed. * See her LJ: suujita * Alex Musayev: His lj_mugs userpic gallery generator was used as a reference for * the caching and fdata-getting subsystems. Very significant to the project. * See his LJ: dreikanter, or his website: alex.asis.ru * Alan V: Wrote the LJ quiz script that inspired this whole thing. Told me where * to get the friends list of a given user (livejournal.com/misc/fdata.bml?user=USER). * See his website: alanv.org * Rev. Joe "G-Flex" Comire: Contributed questions. LJ user: gohanmastaflex * Corey "csmith "Sasuke"" Smith: Some questions. LJ user: sasuke73 */ //i am quizzin' bev //do you like fun //ok //well these are pretty much the options I guess //I mean some guy told me that they were I think //okay so I lied about the guy telling me they are //questions file define("QUESTIONS_FILE", "d/questions.txt"); //where do we cache the friends lists! answer me NOW define("CACHE_DIR", "d/cache"); //cleverly I log those who have taken the quiz! the intelligent may view it define("USER_LOG", "d/logs/users.txt"); //THIS CACHE TASTES SORT OF STALE, MINERVA! define("CACHE_SUSTAIN", 3600); //there is no damned way you actually have that many friends //quit trying to crash me //bitch define("MAX_FRIENDS", 100); //TEE HEE //define("TODAYS_BEAT_FREQUENCY_HZ", 81); //it is a matter of public record that I am William N. God, Esq. //okay first it is of UTMOST IMPORTANCE that we probably have some functions //I personally like functions wherein they perform a task or procedure //let's get some of those in here //guys you need PHP_Compat require('file_put_contents.php'); //random get function make_seed() { list($usec, $sec) = explode(' ', microtime()); return (float) $sec + ((float) $usec * 100000); } //mersenne-twisted shuffle //thank you Japan function mt_shuffle($array){ $rand_array = array(); $tmp_array = array(); mt_srand(make_seed()); foreach($array as $ar_key => $v){ $rand_array[$ar_key] = mt_rand(0,count($array)); } asort($rand_array,SORT_NUMERIC); $i=0; foreach($rand_array as $r_key => $value){ $tmp_array[$i++] = $array[$r_key]; } return $tmp_array; } //this one gets the array of friends one way or another function friends_array($ljuser) { //firstly we are attempting the cache $filename = CACHE_DIR . '/' . $ljuser . '.cache'; if (file_exists($filename) && (time() - filemtime($filename) < CACHE_SUSTAIN)) { $arrfriends = unserialize(file_get_contents($filename)); } else { //there was no cache! the method is to get it and then cache it $arrfriends = download_friends_array($ljuser); file_put_contents($filename, serialize($arrfriends)); } return $arrfriends; } //this downloads fdata and builds the array function download_friends_array($ljuser) { //we are going to need some TCP/IP over here $socket = fsockopen("www.livejournal.com", 80, $errno, $errstr, 30); if (! $socket) { die("

Error #$errno connecting to www.livejournal.com: $errstr

Check back later.\n"); } //hush, my darling, be still, my darling, the server's on the phone fputs($socket, "GET /misc/fdata.bml?user=" . $ljuser . " HTTP/1.0\r\n"); fputs($socket, "Host: www.livejournal.com\r\n"); fputs($socket, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($socket, "User-Agent: http://tgies.thehappythrix.com/lj/lj_quiz.php; tony@thehappythrix.com\r\n\r\n"); //that oughta hold the little bastards //generally the basic idea is to read the fdata now $fdata = ""; //initialize THAT crap while (! feof($socket)) { $fdata .= fread($socket, 4096); //just so you know um that '.=' operator is like "append it" } fclose($socket); //HE DIDN'T EVEN KISS ME GOODNIGHT! $arrfdata = explode("\n", $fdata); unset($fdata); //forcibly reclaim memory $arrfriends = array(); //A LITTLE INITIALIZATION NEVER HURT ANYONE! WELL THERE WAS THAT INCIDENT WITH FLABBY NED foreach ($arrfdata as $item) { //i refuse to explain any of this (i can do whatever i want and nobody can stop me) //UPDATE: okay fine I'll explain it //first we take the first character of each line $type = $item[0]; //okay now we chop a few kinds of shit off of the ends of each line $item = trim($item, " \t\n\r><"); //if the line in question started with ">", it is a friend's name, and goes in the array if ($type == '>') { $friends[] = $item; } } $friends = mt_shuffle($friends); //man do not even worry about all the friends if there are too many if (count($friends) > MAX_FRIENDS) { print("

You have too many friends. Only the first " . MAX_FRIENDS . " of them will be used in the quiz.\n"); $friends = array_slice($friends, 0, MAX_FRIENDS); } return $friends; } //get the questions from the file of it function loadquestions($file = QUESTIONS_FILE, $numq = 0) { $questions = file($file); if (! $questions) { die("

oh oh my God the um questions file did not open
we are all going to die\n"); } $questions = mt_shuffle($questions); if ((is_numeric($numq)) && ($numq > 0) && ($numq <= count($questions))) { $questions = array_slice($questions, 0, intval($numq)); } return $questions; } //okay this will return the next friend every time and um at the end it shuffles and starts anew function drawcard($deck) { //we need to remember where we are static $num = 0; $lastcard = count($deck) - 1; if ($num > $lastcard) { $deck = mt_shuffle($deck); $num = 0; } $card = $deck[$num]; $num++; return $card; } //log the user function loguser($ljuser) { $file = fopen(USER_LOG, 'a'); flock($file, LOCK_EX); fwrite($file, "$ljuser\n"); fclose($file); } //THAT SHOULD COVER THE DAMNED FUNCTIONS //okay if no name has been POST'd then ask for it if (empty($_POST['ljuser'])) { //okay here we are going to escape out of PHP and into Just HTML //however the stuff outside of PHP is still going to be conditional //it is magic ?>

Please enter your LiveJournal username:

How many questions would you like to answer? (1-100, default 100)

 * lj_quiz.php: LJ Funk Quiz 2000
 * LiveJournal survey/quiz/meme engine
 * Release 2.3, September 7, 2005
 * (C) 2005 Tony Gies , LJ username: tgies
 * Released under a Creative Commons Attribution-ShareAlike 2.5 license.
 * See http://creativecommons.org/licenses/by-sa/2.5/ for more info.
 * Put simply: Make derivative works, give me credit, let everyone else do the same.
 * Thanks (in fairly random order):
 * Sean "Redlof" Murray: Some questions. LJ username: redlof
 * Paul "Slugbug" Millar: Assisted in determining which code comments were hilarious.
 *   See his LJ: tehslugbug, or his website: wgiowrb.net
 * Suuji "Suujita" Tanaka: Provided much-needed distraction and interruption.
 *   Catering. Baselessly insulted programming skills for pure hilarity value.
 *   Made a few small contributions here and there, increasing overall speed.
 *   See her LJ: suujita
 * Alex Musayev: His lj_mugs userpic gallery generator was used as a reference for
 *   the caching and fdata-getting subsystems. Very significant to the project.
 *   See his LJ: dreikanter, or his website: alex.asis.ru
 * Alan V: Wrote the LJ quiz script that inspired this whole thing. Told me where
 *   to get the friends list of a given user (livejournal.com/misc/fdata.bml?user=USER).
 *   See his website: alanv.org
 * Rev. Joe "G-Flex" Comire: Contributed questions. LJ user: gohanmastaflex
 * Corey "csmith "Sasuke"" Smith: Some questions. LJ user: sasuke73
Either you have no friends listed, you provided an invalid LJ username, or something generically broke while attempting to get the friends list.
\nIf you are sure this should not be happening, let me know and I'll clear it up for you: tonygies1374 on AIM, tony@thehappythrix.com on E-mail, tgies on LiveJournal."); } //if we are still here, the user is "legit"; log him/her loguser($ljuser); //get ready to roll out the questions $questions = loadquestions(QUESTIONS_FILE, $_POST['numq']); //start the form print("
\n"); //iteration counter to number the questions $i = 1; //okay now loop through the questions kids foreach ($questions as $aquestion) { $aquestion = trim($aquestion); $friend = drawcard($friends); //spit out the question and the textbox $out = " $i. "; $ljuserhref = "" . $friend . ''; $out .= sprintf(stripslashes($aquestion), $ljuserhref); print("$out \n"); //also um now would be a good time to start assembling the LJ code so do that in a hidden field print(" $i. "); $ljusertag = ''; printf(stripslashes($aquestion), $ljusertag); print("\">
\n"); //word that be it $i++; } //more than EVER HOUR AFTER HOUR WORK IS NEVER OVER //okay now it is time to finish the dang form for crap's sake print(" \n\n\n"); //GOD that finishes the part where we actually ask the questions //now we FINALLY do what happens if the questions are answered //FINALLY //do it harder make it faster } else { //okay well pretty much we just spit out a string to the effect of "okay go away" //and also a textarea with the code for the pasting in der LJ print("

Done. Just paste this in an LJ entry:
\n\n

view the source of this here script\n\n"); //well man I guess that's it for THIS PHP game //now to write the DAMN questions //:((( } ?>