query($sql); } function f($field) { return $this->row[$field]; } //these methods must be overrided by derived classes function query($sql) { return false; } function get_insert_id() { return 0; } function next_record() { return false; } function get_db_error() {return "";} } /////////////////////////////////////////////////////////////////////////////// // MySQL database support class DB_mysql extends DB { var $dblink; var $dbresult; function DB_mysql($sql="") { $this->dblink=mysql_pconnect( $GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"]) or die("Unable to connect to database"); mysql_select_db($GLOBALS["dbname"], $this->dblink) or die("Unable to select database {$GLOBALS[dbname]}"); $this->DB($sql); } function query($sql) { $this->dbresult=mysql_query($sql, $this->dblink); if (!$this->dbresult) { die("Query failure: ".mysql_error()."
$sql"); } return $this->dbresult; } function get_insert_id() { return mysql_insert_id($this->dblink); } function next_record() { $this->row=mysql_fetch_array($this->dbresult); return $this->row!=FALSE; } function get_db_error() { return mysql_last_error(); } } /////////////////////////////////////////////////////////////////////////////// // Postgres database support (provided by Tim Hunter) // /* //more work required to make PostGres work, feel free to submit a patch class DB_postgres extends DB { var $dblink; var $dbresult; function DB_postgres($sql="") { $this->dblink=pg_connect( "host=$GLOBALS[dbhost] ". "dbname=$GLOBALS[dbname] ". "user=$GLOBALS[dbuser] ". "password=$GLOBALS[dbpass]") or die("Unable to connect to database"); $this->DB($sql); } function query($sql) { $this->dbresult=pg_exec($sql); if (!$this->dbresult) { die("Query failure: ".$this->get_db_error()."
$sql
"); } return $this->dbresult; } function get_insert_id() { $sql = "select currval('pastebin_pid_seq')"; $result = pg_fetch_array($this->query($sql)); return $result[0]; } function next_record() { $this->row=pg_fetch_array($this->dbresult); return $this->row!=FALSE; } function get_db_error() { return pg_last_error(); } } */ /////////////////////////////////////////////////////////////////////////////// // syntax highlighers // //simple syntax hilighter and base class for extended ones class SyntaxHighlighter_none { //highlight for viewing function highlight($text) { return "".nl2br(htmlentities($text)).""; } //preprocess input before db storage function preprocess($text) { return $text; } } //php syntax highlighter class SyntaxHighlighter_php extends SyntaxHighlighter_none { function highlight($php) { //get php to do the hard work ob_start(); @highlight_string($php); $code = ob_get_contents(); ob_end_clean(); // Hyperlink keywords - we could have a table or array or // interesting keywords, but that would be a bit laborious. // Instead, we just for things that look like function calls... // this has the downside that it links // user defined functions too, but what the hell. It's only // a few lines of code.... $keycol=ini_get("highlight.keyword"); $manual="http://www.php.net/manual-lookup.php?lang=en&pattern="; $code=preg_replace( //match a highlighted keyword '{([\w_]+)(\s*)'. //followed by a bracket '(\s*\s*\()}m', //and replace with manual hyperlink '$1$2$3', $code); return $code; } function preprocess($code) { //ensure code has begin and end tags somewhere $code = trim($code); if (strpos($code, '') === false) $code .= "\n?>"; return $code; } } /////////////////////////////////////////////////////////////////////////////// // global functions // function smart_addslashes($str) { if (get_magic_quotes_gpc()) return $str; else return addslashes($str); } function shorturl($id) { return sprintf("http://$_SERVER[HTTP_HOST]".$GLOBALS["url_format"], $id); } /////////////////////////////////////////////////////////////////////////////// // global variables // $dbclass="DB_".$dbsystem; $db=new $dbclass; /////////////////////////////////////////////////////////////////////////////// // garbage collection // // 5% chance of trigging garbage collection - remove the oldest posts // leaving most recent 50 posts remaining if(rand()%100 < 5) { $db->query("select count(*) as cnt from pastebin"); if($db->next_record()) { $delete_count=$db->f("cnt")-$max_posts; if ($delete_count>0) { //build a one-shot statement to delete old posts $sql="delete from pastebin where pid in ("; $sep=""; $db->query("select * from pastebin order by posted asc limit $delete_count"); while ($db->next_record()) { $sql.=$sep.$db->f("pid"); $sep=","; } $sql.=")"; //delete extra posts $db->query($sql); } } } /////////////////////////////////////////////////////////////////////////////// // process new posting // $errors=array(); if (isset($_POST["paste"])) { //set/clear the persistName cookie if ($_POST["remember"]) { //set cookie if not set if (!isset($_COOKIE["persistName"])) setcookie ("persistName", $_POST["poster"], time()+3600*24*365); } else { //clear cookie if set if (isset($_COOKIE["persistName"])) setcookie ("persistName", "", 0); } if (strlen($_POST["code"])) { $poster=strip_tags($_POST["poster"]); if (strlen($poster)==0) $poster="Anonymous"; //wrap the code at 80 columns - this is how it looked in the textarea //and ensures we keep a nice layout on the page //$code = wordwrap($_POST["code"], 80, "\n", 1); //use syntax highlighter to preprocess input... $hclass="SyntaxHighlighter_".$syntax_highlighter; $highlighter=new $hclass; $code=$highlighter->preprocess($code); //now insert.. $parent_pid=0; if (isset($_POST["parent_pid"])) $parent_pid=intval($_POST["parent_pid"]); $sql="insert into pastebin (poster, posted, code, parent_pid) values (". "'".smart_addslashes($poster)."',". "now(),". "'".smart_addslashes($code)."',". "$parent_pid". ");"; $db->query($sql); $id=$db->get_insert_id(); //now redirect, making refresh easier header("Location:".shorturl($id)); } else { $errors[]="No code specified"; } } /////////////////////////////////////////////////////////////////////////////// // view source code // if (isset($_GET["showsource"])) { switch($_GET["showsource"]) { case "php": $script=$_SERVER["SCRIPT_FILENAME"]; $fp=fopen($script,"r"); $contents=fread($fp, filesize($script)); fclose($fp); //remove passwords $contents=preg_replace('{(\\$db....)=".*?";}', '$1="*****";', $contents); $highlighter=new SyntaxHighlighter_php; $contents=$highlighter->highlight($contents); //hyperlink css $contents=str_replace('pastebin.css', 'pastebin.css', $contents); echo $contents; break; case "css": $css=str_replace(".php", ".css", $_SERVER["SCRIPT_FILENAME"]); $fp=fopen($css,"r"); $contents=fread($fp, filesize($css)); fclose($fp); $highlighter=new SyntaxHighlighter_none; echo $highlighter->highlight($contents); break; } exit; } /////////////////////////////////////////////////////////////////////////////// // HTML page output // echo "\n"; ?> <?php echo $title ?>

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

view php source
Errors"; echo "
"; } /////////////////////////////////////////////////////////////////////////////// // show a post // if (isset($_REQUEST["show"])) { $db->query("select *,date_format(posted, '%a %D %b %H:%i') as postdate ". "from pastebin where pid='{$_REQUEST[show]}';"); if ($db->next_record()) { //show a quick reference url, poster and parents echo "

"; //echo shorturl($db->f("pid"))."
"; echo "Posted by ".$db->f("poster"); echo " ".$db->f("postdate"); if ($db->f("parent_pid")>0) { $db2=new $dbclass; $db2->query("select pid,poster, ". "date_format(posted, '%a %D %b %H:%i') as posted ". "from pastebin where pid=".$db->f("parent_pid")); if ($db2->next_record()) { echo " (modification of posting from "; echo "f("pid"))."\">"; echo $db2->f("poster"); echo " "; //echo $db2->f("posted"); echo ")"; } } echo "

"; //use configured highlighter... $hclass="SyntaxHighlighter_".$syntax_highlighter; $highlighter=new $hclass; $code=$highlighter->highlight($db->f("code")); //build a line numbering string $lines=""; $codeline=explode("
", $code); $linecount = count($codeline); for($l=1; $l<=$linecount; $l++) { $lines.=sprintf("%03d 
", $l); } $lines.="
"; //output echo ""; echo ""; echo "
$lines$code
"; //store the code for later editing $editcode=$db->f("code"); //any amendments? $count=0; $db->query("select pid,poster,". "date_format(posted, '%a %D %b %H:%i') as posted ". "from pastebin where parent_pid=".$_REQUEST['show']. " order by posted desc;"); while ($db->next_record()) { if ($count++ == 0) echo "
The following amendments have been posted:"; echo "

Submit a correction or amendment below. (click here to make a fresh posting)"; } else { echo "Unknown post id, it may have been deleted
"; } } else { echo "

New posting

"; } /////////////////////////////////////////////////////////////////////////////// // submission form // $poster=$_COOKIE["persistName"]; if (strlen($poster)) $remember="checked=\"checked\""; else $remember=""; ?>
"> "/> Name

/>Remember my name in a cookie

Code: To ensure legibility, keep your code lines under 80 characters long.
Include comments to indicate what you need feedback on.