dblink=mysql_pconnect( $CONF["localhost"], $CONF["rseterni_paste"], $CONF["a64042640"]) or die("Unable to connect to database"); mysql_select_db($CONF["rseterni_paste"], $this->dblink) or die("Unable to select database {$GLOBALS[dbname]}"); } /** * How many posts on domain $subdomain? * access public */ function getPostCount($subdomain) { $this->_query('select count(*) as cnt from pastebin where domain=?', $subdomain); return $this->_next_record() ? $this->_f('cnt') : 0; } /** * Delete oldest $deletecount posts from $subdomain * access public */ function trimDomainPosts($subdomain, $deletecount) { //build a one-shot statement to delete old posts $sql='delete from pastebin where pid in ('; $sep=''; $this->_query("select * from pastebin where domain=? order by posted asc limit $deletecount", $subdomain); while ($this->_next_record()) { $sql.=$sep.$this->_f('pid'); $sep=','; } $sql.=')'; //delete extra posts $this->_query($sql); } /** * Delete all expired posts * access public */ function deleteExpiredPosts() { $this->_query("delete from pastebin where expires is not null and now() > expires"); } /** * Add post and return id * access public */ function addPost($poster,$subdomain,$format,$code,$parent_pid,$expiry_flag) { //figure out expiry time switch ($expiry_flag) { case 'd'; $expires="DATE_ADD(NOW(), INTERVAL 1 DAY)"; break; case 'f'; $expires="NULL"; default: case 'm'; $expires="DATE_ADD(NOW(), INTERVAL 1 MONTH)"; break; } $this->_query('insert into pastebin (poster, domain, posted, format, code, parent_pid, expires,expiry_flag) '. "values (?, ?, now(), ?, ?, ?, $expires, ?)", $poster,$subdomain,$format,$code,$parent_pid, $expiry_flag); $id=$this->_get_insert_id(); return $id; } /** * Return entire pastebin row for given id/subdomdain * access public */ function getPost($id, $subdomain) { $this->_query('select *,date_format(posted, \'%a %D %b %H:%i\') as postdate '. 'from pastebin where pid=? and domain=?', $id, $subdomain); if ($this->_next_record()) return $this->row; else return false; } /** * Return summaries for $count posts ($count=0 means all) * access public */ function getRecentPostSummary($subdomain, $count) { $limit=$count?"limit $count":""; $posts=array(); $this->_query("select pid,poster,unix_timestamp()-unix_timestamp(posted) as age, ". "date_format(posted, '%a %D %b %H:%i') as postdate ". "from pastebin ". "where domain=? ". "order by posted desc, pid desc $limit", $subdomain); while ($this->_next_record()) { $posts[]=$this->row; } return $posts; } /** * Get follow up posts for a particular post * access public */ function getFollowupPosts($pid, $limit=5) { //any amendments? $childposts=array(); $this->_query("select pid,poster,". "date_format(posted, '%a %D %b %H:%i') as postfmt ". "from pastebin where parent_pid=? ". "order by posted limit $limit", $pid); while ($this->_next_record()) { $childposts[]=$this->row; } return $childposts; } /** * Save formatted code for a post * access public */ function saveFormatting($pid, $codefmt, $codecss) { $this->_query("update pastebin set codefmt=?,codecss=? where pid=?", $codefmt, $codecss, $pid); } /** * execute query - show be regarded as private to insulate the rest of * the application from sql differences * @access private */ function _query($sql) { //been passed more parameters? do some smart replacement if (func_num_args() > 1) { //query contains ? placeholders, but it's possible the //replacement string have ? in too, so we replace them in //our sql with something more unique $q=md5(uniqid(rand(), true)); $sql=str_replace('?', $q, $sql); $args=func_get_args(); for ($i=1; $i<=count($args); $i++) { $sql=preg_replace("/$q/", "'".preg_quote(mysql_real_escape_string($args[$i]))."'", $sql,1); } //we shouldn't have any $q left, but it will help debugging if we change them back! $sql=str_replace($q, '?', $sql); } $this->dbresult=mysql_query($sql, $this->dblink); if (!$this->dbresult) { die("Query failure: ".mysql_error()."
$sql"); } return $this->dbresult; } /** * get next record after executing _query * @access private */ function _next_record() { $this->row=mysql_fetch_array($this->dbresult); return $this->row!=FALSE; } /** * get result column $field * @access private */ function _f($field) { return $this->row[$field]; } /** * get last insertion id * @access private */ function _get_insert_id() { return mysql_insert_id($this->dblink); } /** * get last error * @access public */ function get_db_error() { return mysql_last_error(); } } ?>