\n"; print "\n"; print "\n"; print "
doItLater
This isn't done yet, I'll do It Later!

\n"; } // end doItLater() //// // Returns an array of the media types used on the site. //// function getMediaTypes(){ return array("movies","pictures","games","flash"); } // getMediaTypes() //// // Displays a thumbnail given the media type (movies, pictures, etc.) and id. // If the item does not have a thumbnail, a default is used (determined by mediaType). //// function thumbById($mediaType, $id, $tipped=true){ $defaultThumbs = array("movies" => "./images/thumb_movies.png", "pictures" => "./images/thumb_pictures.png", "games" => "./images/thumb_games.png", "flash" => "./images/thumb_flash.png"); $db = dil_connect(); $queryString = "SELECT * FROM $mediaType WHERE id=$id"; if($result = mysql_query($queryString,$db)){ if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){ $cnt = 0; $thumbUrl = mysql_result($result, $cnt, "url_thumb"); $title = mysql_result($result, $cnt, "title"); $caption = mysql_result($result, $cnt, "caption"); if($thumbUrl == ""){ $thumbUrl = $defaultThumbs[$mediaType]; } dispThumb($mediaType, $thumbUrl, $title, $caption, $id, $tipped); } } } // end thumbById(...) //// // Displays thumbnail given the url. // // TODO: Do a mouse-over tooltip that shows the title and/or // caption (possibly the media type as well). [In progress] //// function dispThumb($mediaType, $thumbUrl, $title, $caption, $id, $tipped=true){ GLOBAL $EXTRA_TOOL_TIPS; // makes the media type singular (chops off 's') and capitalizes first letter $link = "./show".ucfirst(getSingular($mediaType,true)).".php?$id"; $letter = substr($mediaType,0,1); $alt = str_replace('"', "'", $title); if($tipped){ //print "
\n"; print "\"$alt\"/\n"; $caption .= " (".getSingular($mediaType).")"; $letter = substr($mediaType,0,1); $EXTRA_TOOL_TIPS[] = array(TT_ID => $letter.$id, TT_TITLE => $title, TT_CAPTION => $caption); } else { print "$alt\n"; } } // end dispThumb(...) //// // Displays random thumbnails for the given media type. If the mediaType is not given, the results will be a random // mixture of the media-types. // // The media type is not case-sensitive. // // If a 'validAfter' is provided, then caching will be used and the content will have to have been // cached after 'validAfter' or it will be re-generated. An example of using validAfter would be: // randThumbs("", strtotime("-15 minute")); //// function randThumbs($mediaType="", $validAfter=""){ profiler_beginSection(__METHOD__); $db = dil_connect(); $numThumbs = 4; $validTypes = getMediaTypes(); $mediaType = strtolower($mediaType); $alreadyDisplayed = array(); // to prevent duplicates if(($mediaType != "") && (false === in_array($mediaType, $validTypes))){ dispError("Warning: '$mediaType' is not a valid media type for displaying thumbnails

Valid types: ".implode($validTypes, ",")); } $optionsExhausted = false; $qStringBeg = ""; // will hold the beginning of the string so that restrictions can be added for re-queries if($mediaType == ""){ $queryString = "SELECT "; foreach($validTypes as $type){ $queryString.= "$type.id,"; } $queryString = substr($queryString, 0, strlen($queryString)-1); // take off the extra comma $queryString.= " FROM "; foreach($validTypes as $type){ $queryString.= "$type,"; } $queryString = substr($queryString, 0, strlen($queryString)-1); // take off the extra comma $qStringBeg = $queryString; } else { $queryString = "SELECT id FROM $mediaType "; $qStringBeg = $queryString; } $queryString.= " ORDER BY RAND() LIMIT $numThumbs"; $style = ""; $style = "style='padding:0px;margin:0;'"; print "
\n"; $typeName = ucfirst($mediaType); if($typeName == "Flash"){$typeName .=" Movies";} print "\n"; print ""; //print ""; } // This uses caching, so get the array and serialize it into a string. if($validAfter != ''){ $dataString = serialize_nested($thumbArray); $cache->cacheValue($RAND_THUMBS_KEY, $dataString); } print "\n"; print "
Random ".(($mediaType=="")?"Stuff":$typeName)."
\n"; include_once 'cache.php'; $cache = new Cache(); $RAND_THUMBS_KEY = "RANDOM_THUMBS_$mediaType"; $thumbArray = array(); if($validAfter != ''){ $dataString= $cache->fetchExpire($RAND_THUMBS_KEY, $validAfter); if($dataString){ // If it exists, it will be a string and we need to turn it back into an array of arrays $thumbArray = deserialize_nested($dataString); } } if(count($thumbArray) == 0){ // since results are no longer guaranteed to be unique from one query to the next, stop looping if no progress was made on a query // this is more of a heuristic than a function (it could cop out early given unlucky random values). If limiting can be done with where clauses w/o breaking it, // that should be done instead (TODO:) $lastNumDisplayed = -1; while((count($alreadyDisplayed) < $numThumbs) && ($optionsExhausted === false) && ($lastNumDisplayed < count($alreadyDisplayed))){ $lastNumDisplayed = count($alreadyDisplayed); if($result = mysql_query($queryString,$db)){ if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){ for($cnt=0; ($cnt<$numRows) && (count($alreadyDisplayed)<$numThumbs); $cnt++){ if($mediaType == ""){ $randIndex = mt_rand(0, count($validTypes)-1); $currType = $validTypes[$randIndex]; $id = mysql_result($result, $cnt, "$currType.id"); $thisEntry = array($currType, $id); if(false === in_array($thisEntry, $alreadyDisplayed)){ $alreadyDisplayed[] = $thisEntry; $thumbArray[] = array($currType, $id); } } else { $id = mysql_result($result, $cnt, "id"); $thisEntry = array($mediaType, $id); if(false === in_array($thisEntry, $alreadyDisplayed)){ $alreadyDisplayed[] = $thisEntry; $thumbArray[] = array($mediaType, $id); } } } // TODO: Decide if this should be used, figure out why it was broken, and either fix or remove it. /*// The where clauses limit the search to the point that no results are returned (not sure why). if(count($alreadyDisplayed) < $numThumbs){ $queryString = $qStringBeg; $queryString.= " WHERE "; foreach($alreadyDisplayed as $entry){ print_r($entry); $queryString .= $entry[0].".id!=".$entry[1]." AND "; } $queryString = substr($queryString, 0, strlen($queryString)-4); // chop off the extra 'AND ' $queryString.= " ORDER BY RAND() LIMIT $numThumbs";//.($numThumbs - count($alreadyDisplayed)); }*/ } else { $optionsExhausted = true; } } } } // Regardless of whether the data came from the cache or was queried, it should be an array of arrays right now, so display it. foreach($thumbArray as $currThumb){ print ""; thumbById($currThumb[0], $currThumb[1]); print "

\n"; profiler_endSection(__METHOD__); } // end randThumbs(...) //// // Lists the titles and captions of items of the given media type. //// function listMedia($mediaType){ $db = dil_connect(); $queryString = "SELECT * FROM $mediaType"; if($result = mysql_query($queryString,$db)){ if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){ for($cnt=0; $cnt<$numRows; $cnt++){ $id = mysql_result($result, $cnt, "id"); $title = mysql_result($result, $cnt, "title"); $caption = mysql_result($result, $cnt, "caption"); // makes the media type singular (chops off 's') and capitalizes first letter $link = "./show".ucfirst(getSingular($mediaType,true)).".php?$id"; print "$title
$caption

\n"; } } } } // end listMedia(...) //// // Displays a simple snapshot of the item (thumbnail, title, stats, etc). //// function snapshot($mediaType, $id){ $db = dil_connect(); $queryString = "SELECT * FROM $mediaType WHERE id=$id LIMIT 1"; if($result = mysql_query($queryString,$db)){ if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){ $cnt=0; $thumbUrl = mysql_result($result, $cnt, "url_thumb"); $title = mysql_result($result, $cnt, "title"); $caption = mysql_result($result, $cnt, "caption"); $singularCaps = ucfirst(getSingular($mediaType,true)); $link = "./show$singularCaps.php?$id"; print "\n"; //print "\n"; print "\n"; print "\n"; print "
"; thumbById($mediaType,$id,false); // tool-tips would be redundant print "\n"; print "$title
$caption\n"; print "
\n"; } } } // end snapshot(...) //// // Creates an authentication value given a seed key and the id of the item to // use this for. //// function createAuth($seedKey, $id){ return md5($id."KatieHarper$seedKey"); // security by obscurity :/... TODO: What do we use this for? This needs to be replaced with a better system now. } // createAuth(...) // Alias to display different types of message boxes. function dispError($err){dispMsg($err, 'error');} function dispSuccess($msg){dispMsg($msg, 'success');} function dispTip($msg){dispMsg($msg, 'tip');} //// // Displays a message-box of type {note,error,success}. //// function dispMsg($msg, $type='note'){ echo "
$msg
\n"; } // end dispMsg() //// // Creates a log of an event where 5 is the least severe defcon level and 1 is the most severe. //// function logEvent($event,$defcon=5){ profiler_beginSection(__METHOD__); $db = dil_connect(); $timeStamp = date("Y-m-d H:i:s"); if($defcon <= 1){ // forwards the most severe events to my email $errorString = "DEFCON $defcon WARNING!
\nTIME: $timeStamp
\nDATA:
\n$event"; $errorString = str_replace("
\n","\r\n", $errorString); // gmail shows html visibly $errorString = str_replace("
","\r\n", $errorString); $errorString = urldecode($errorString); error_log($errorString, 1, "sean.colombo@gmail.com"); // NON-PORTABLE: This should be in a config-file somewhere. } $event = str_replace("'", "\'", $event); // will over-escape sometimes, but otherwise it will under-escape others. sendQuery("INSERT INTO eventLog (defcon,message,timestamp) VALUES ($defcon, '$event' , '$timeStamp')"); profiler_endSection(__METHOD__); } //// // Logs a detailed message about the error with this last query using the default defcon value. //// function logQueryError($preamble, $queryString){ profiler_beginSection(__METHOD__); $err = (($preamble!='')?"$preamble

":''); $err.= "$queryString

\n"; $err .= "mysql_error: ".mysql_error()."\n"; logEvent($err); profiler_endSection(__METHOD__); } // end logQueryError() //// // Displays an error message with details about an error that occured with the // given query. The standard error information is preceeded with the // content of 'preamble'. // // Example: // $queryString = "INSERT INTO users (username) VALUES ('example')"; // ... // queryError("Could not create new user.", $queryString); //// function queryError($preamble, $queryString){ $err = (($preamble!='')?"$preamble

":''); $err.= "$queryString

\n"; $err .= "mysql_error: ".mysql_error()."\n"; dispError($err); } // end queryError() //// // Records where the user logs in from. This is helpful in case there are any reports of scams on the site. // // TODO: Figure out if this function can be moved into "User" class or if it is more desirable to just get rid of it. // If this function is kept, then the 'logins' table should be added to the offhandway.sql setup script. //// function recordLogin(){ $db = dil_connect(); $PHP_SELF = $_SERVER['PHP_SELF']; $page = $PHP_SELF; $user = getUser(); $username = $user->getUsername(); if(($username == "") && (isset($_SESSION['user']))){ $username = $_SESSION['user']. "__[FAILED]"; } $ip = $_SERVER['REMOTE_ADDR']; $queryString = "SELECT COUNT(*) FROM logins WHERE username=\"$username\" AND ip=\"$ip\""; if($result = mysql_query($queryString,$db)){ if(mysql_num_rows($result) > 0){ $count = mysql_result($result,0,"COUNT(*)"); $queryString = "INSERT INTO logins (username,ip,numLogins) VALUES (\"$username\", \"$ip\", 1)"; if($count>0){ $queryString = "UPDATE logins SET numLogins=numLogins+1 WHERE username=\"$username\" AND ip=\"$ip\""; } mysql_query($queryString,$db); } } else { logEvent("Unable to record login with query:
$queryString"); } } // end recordLogin() //// // Sends a query and returns true on success false on failure. // Nothing sophisticated here, just makes the code shorter by saving the need // for other pieces of code to get the global connection to the db. //// function sendQuery($queryString){ profiler_beginSection(__METHOD__); $db = dil_connect(); if(WHICH_SERVER == SERVER_DEV){ $retVal = mysql_query($queryString, $db); if(!$retVal){ dispError("SERVER_DEV ERROR:
$queryString

mysql_error:".mysql_error()); } } else { $retVal = mysql_query($queryString, $db); } profiler_endSection(__METHOD__); return $retVal; } // end sendQuery() //// // Returns the result of a mySQL query that only has one result (one column and one row) //// function simpleQuery($queryString){ profiler_beginSection(__METHOD__); $db = dil_connect(); $retVal = ""; if($result = mysql_query($queryString,$db)){ if(mysql_num_rows($result) > 0){ if($myRow = mysql_fetch_row($result)){ $retVal = $myRow[0]; } } } profiler_endSection(__METHOD__); return $retVal; } // end simpleQuery() //// // Displays the given string in the most basic Pedlr page. //// function simplePage($text, $title=''){ $simple = new Page($title); $simple->header(); echo $text; $simple->bottom(); } // end simplePage() ?>