cvs: pearweb /include/Damblan Trackback.php /public_html package-info.php /public_html/trackback trackback-admin.php trackback.php
| From: | Tobias Schlitt | Date: | Tue, 01 Mar 2005 10:15:41 +0000 |
| Subject: | cvs: pearweb /include/Damblan Trackback.php /public_html package-info.php /public_html/trackback trackback-admin.php trackback.php | ||
| Groups: | php.pear.cvs php.pear.core | ||
| Request: | Send a blank email to pear-cvs+get-29941@lists.php.net to get a copy of this message | ||
toby Tue Mar 1 05:15:41 2005 EDT
Modified files:
/pearweb/include/Damblan Trackback.php
/pearweb/public_html package-info.php
/pearweb/public_html/trackback trackback-admin.php trackback.php
Log:
* Switching Damblan_Trackback to Services_Trackback 0.4.0.
* Added IP check to Damblan_Trackback (every IP may only post 3 trackbacks in 10 minutes).
* Removed overloading.
* Temporarely deactivated trackbacks to install Services_Trackback 0.4.0.
http://cvs.php.net/diff.php/pearweb/include/Damblan/Trackback.php?r1=1.6&r2=1.7&ty=u Index: pearweb/include/Damblan/Trackback.php diff -u pearweb/include/Damblan/Trackback.php:1.6 pearweb/include/Damblan/Trackback.php:1.7 --- pearweb/include/Damblan/Trackback.php:1.6 Sat Jan 29 12:47:37 2005 +++ pearweb/include/Damblan/Trackback.php Tue Mar 1 05:15:40 2005 @@ -10,7 +10,7 @@ * @var int * @since */ - var $_timestamp; + var $_timestamp = 0; /** * Boolean flag, if the trackback has been improved by a PEAR developer, yet. @@ -18,10 +18,23 @@ * @var bool * @since */ - var $_approved; + var $_approved = false; /** - * __construct + * The IP adress from where the trackback has been send. + * + * @var string + * @since + */ + var $_ip = ''; + + // How many trackbacks may be posted in a specific timespan? + var $_repostCount = 3; + // In which timespan? + var $_repostTimespan = 600; // 10 minutes + + /** + * Constructor * Overwriten constructor to get the timestamp while creating a trackback. * The timestamp is used as the primary key for the trackback table, in * combination with the trackback ID (which is the package name). @@ -42,33 +55,78 @@ * @param bool $approved Wether the trackback is approved, yet. Default is false. * @return void */ - function Damblan_Trackback($data, $timestamp, $approved = false) + function Damblan_Trackback($data) + { + foreach ($data as $key => $val) { + if ($key == 'approved') + $val = ($val == 'true'); + $this->set($key, $val); + } + } + + /** + * Check for possible spam + * Checks the database for recent trackbacks from the same IP. + * Every IP is allows to post $this->_repostCount times in + * $this->_repostTimespan seconds. + * + * @since + * @param object(DB) $dbh The database handle. + * @return mixed Bool true, if no repost, else or on error, PEAR::Error() + */ + function checkRepost(&$dbh) { - $this->_fromArray($data); - $this->_timestamp = (int)$timestamp; - $this->_approved = $approved; + $sql = 'SELECT COUNT(timestamp) FROM trackbacks WHERE + ip = '.$dbh->quoteSmart($this->get('ip')).' AND + timestamp > '.$dbh->quoteSmart($this->get('timestamp') - $this->_repostTimespan).' + GROUP BY ip'; + $res = $dbh->getOne($sql); + if (PEAR::isError($res)) { + return PEAR::raiseError('Database error, please try again later.'); + } + if ($res >= $this->_repostCount) { + return PEAR::raiseError('We only allow '.$this->_repostCount.' trackbacks in '.($this->_repostTimespan / 60).' minutes from a single host. Please try again later.'); + } + return true; } /** - * __get - * Overwritten __get() to receive timestamp and improved state correctly. + * get + * Overwritten get() to receive timestamp, ip and approved state correctly. * * @since * @access public * @param mixed $key The name of the property to receive. * @return mixed $val The value of the property. */ - function __get($key, &$val) + function get($key) { $testKey = '_'.$key; if (isset($this->$testKey)) { - $val = $this->$testKey; - return true; - } else if (isset($this->_data[$key])) { - $val = $this->_data[$key]; + return $this->$testKey; + } else { + return Services_Trackback::get($key); + } + } + + /** + * set + * Overwritten set() to set timestamp, ip and approved state correctly. + * + * @since + * @access public + * @param mixed $key The name of the property to receive. + * @return mixed $val The value of the property. + */ + function set($key, $value) + { + $testKey = '_'.$key; + if (isset($this->$testKey)) { + $this->$testKey = $value; return true; + } else { + return Services_Trackback::set($key, $value); } - return false; } /** @@ -87,16 +145,17 @@ $necessaryData = array('id', 'title', 'url', 'excerpt', 'blog_name'); $this->_checkData($necessaryData); $data = $this->_getDecodedData($necessaryData); - $this->_timestamp = time(); +// $this->set('timestamp', time()); $approved = ($this->_approved) ? 'true' : 'false'; - $sql = "INSERT INTO trackbacks (id, title, url, excerpt, blog_name, approved, timestamp) VALUES ( - ".$dbh->quoteSmart($data['id']).", - ".$dbh->quoteSmart($data['title']).", - ".$dbh->quoteSmart($data['url']).", - ".$dbh->quoteSmart($data['excerpt']).", - ".$dbh->quoteSmart($data['blog_name']).", - ".$dbh->quoteSmart($approved).", - ".$dbh->quoteSmart($this->_timestamp)." + $sql = "INSERT INTO trackbacks (id, title, url, excerpt, blog_name, approved, timestamp, ip) VALUES ( + ".$dbh->quoteSmart($this->get('id')).", + ".$dbh->quoteSmart($this->get('title')).", + ".$dbh->quoteSmart($this->get('url')).", + ".$dbh->quoteSmart($this->get('excerpt')).", + ".$dbh->quoteSmart($this->get('blog_name')).", + ".$dbh->quoteSmart($this->get('approved')).", + ".$dbh->quoteSmart($this->get('timestamp')).", + ".$dbh->quoteSmart($this->get('ip'))." )"; $res = $dbh->query($sql); if (DB::isError($res)) { @@ -131,9 +190,9 @@ $data = $this->_getDecodedData($necessaryData); - $sql = "SELECT id, title, excerpt, blog_name, url, timestamp, approved FROM trackbacks WHERE - id = ".$dbh->quoteSmart($data['id'])." - AND timestamp = ".$dbh->quoteSmart($this->_timestamp); + $sql = "SELECT id, title, excerpt, blog_name, url, timestamp, approved, ip FROM trackbacks WHERE + id = ".$dbh->quoteSmart($this->get('id'))." + AND timestamp = ".$dbh->quoteSmart($this->get('timestamp')); $res = $dbh->getRow($sql, null, DB_FETCHMODE_ASSOC); @@ -144,18 +203,10 @@ } foreach ($res as $key => $val) { - if (($key != 'timestamp') && ($key != 'approved')) { - $this->_data[$key] = $val; - } else { - switch ($key) { - case 'approved': - $this->_approved = ($val == 'true'); - break; - default: - $key = '_'.$key; - $this->$key = $val; - } + if ($key == 'approved') { + $val = ($val == 'true'); } + $this->set($key, $val); } return true; } @@ -182,8 +233,8 @@ return $data; } $sql = "UPDATE trackbacks SET approved = ".$dbh->quoteSmart('true')." WHERE - id = ".$dbh->quoteSmart($data['id'])." - AND timestamp = ".$dbh->quoteSmart($this->_timestamp); + id = ".$dbh->quoteSmart($this->get('id'))." + AND timestamp = ".$dbh->quoteSmart($this->get('timestamp')); $res = $dbh->query($sql) ; if (DB::isError($res)) { return PEAR::raiseError('Could not approve trackback.'); @@ -208,13 +259,13 @@ if (PEAR::isError($data)) { return $data; } - $res = $this->load($dbh, $this->_timestamp); + $res = $this->load($dbh, $this->get('timestamp')); if (PEAR::isError($res)) { return $res; } $sql = "DELETE FROM trackbacks WHERE - id = ".$dbh->quoteSmart($data['id'])." - AND timestamp = ".$dbh->quoteSmart($this->_timestamp); + id = ".$dbh->quoteSmart($this->get('id'))." + AND timestamp = ".$dbh->quoteSmart($this->get('timestamp')); $res = $dbh->query($sql) ; if (DB::isError($res)) { return PEAR::raiseError('Could not delete trackback: '.$res->getMessage()); @@ -240,7 +291,7 @@ */ function listTrackbacks(&$dbh, $id, $approvedOnly = true, $orderBy = 'timestamp DESC', $limit = 10) { - $sql = 'SELECT id, title, excerpt, blog_name, url, timestamp, approved FROM trackbacks WHERE + $sql = 'SELECT id, title, excerpt, blog_name, url, timestamp, approved, ip FROM trackbacks WHERE id LIKE '.$dbh->quoteSmart($id); if ($approvedOnly) { $sql .= ' AND approved = '.$dbh->quoteSmart('true'); @@ -253,15 +304,50 @@ } $ret = array(); foreach ($res as $row) { - $ts = $row['timestamp']; - unset($row['timestamp']); - $app = ($row['approved'] == 'true'); - unset($row['approved']); - $ret[] = new Damblan_Trackback($row, $ts, $app); + $ret[] = new Damblan_Trackback($row); } return $ret; } - + + /** + * recentTrackbacks + * Get a list of trackbacks independent from the package ID. + * + * @since + * @access public + * @static + * @param object(DB) $dbh The database connection object (PEAR::DB). + * @param int $offset Start position for the trackback list. + * @param int $number Number of trackbacks to list. + * @param bool $approvedOnly Wether to fetch only approved trackbacks (default is true). + * @return array Array of PEAR_Trackback objects. + * @throws Exception If no results are received. + */ + function recentTrackbacks(&$dbh, $offset = 0, $number = 10, $approvedOnly = true) + { + if ($offset < 0) { + return PEAR::raiseError('Offset out of range. Offset must be integer >= 0.'); + } + if (($number < 1) || ($number > 50)) { + return PEAR::raiseError('Number out of range. Number must be integer between 1 and 50.'); + } + $sql = 'SELECT id, title, excerpt, blog_name, url, timestamp, approved, ip + FROM trackbacks'; + if ($approvedOnly) { + $sql .= ' WHERE approved = '.$dbh->quoteSmart('true'); + } + $sql .= ' ORDER BY timestamp DESC LIMIT ' . $offset . ',' . $number; + $res = $dbh->getAll($sql, null, DB_FETCHMODE_ASSOC); + if (DB::isError($res)) { + return PEAR::raiseError('Could not receive trackback list: '.$res->getMessage()); + } + $ret = array(); + foreach ($res as $row) { + $ret[] = new Damblan_Trackback($row); + } + return $ret; + } + /** * Get maintainers to inform of a trackback (the lead maintainers of a package). * @@ -273,7 +359,7 @@ */ function getMaintainers () { - $maintainers = maintainer::get($this->id, true); + $maintainers = maintainer::get($this->get('id'), true); $res = array(); foreach ($maintainers as $maintainer => $data) { $tmpUser = user::info($maintainer, 'email'); @@ -284,9 +370,6 @@ } return $res; } - } -overload('Damblan_Trackback'); - ?> http://cvs.php.net/diff.php/pearweb/public_html/package-info.php?r1=1.98&r2=1.99&ty=u Index: pearweb/public_html/package-info.php diff -u pearweb/public_html/package-info.php:1.98 pearweb/public_html/package-info.php:1.99 --- pearweb/public_html/package-info.php:1.98 Sun Feb 13 19:43:37 2005 +++ pearweb/public_html/package-info.php Tue Mar 1 05:15:40 2005 @@ -16,7 +16,7 @@ | Authors: Martin Jansen <mj@php.net> | | Tomas V.V.Cox <cox@idecnet.com> | +----------------------------------------------------------------------+ - $Id: package-info.php,v 1.98 2005/02/14 00:43:37 dufuz Exp $ + $Id: package-info.php,v 1.99 2005/03/01 10:15:40 toby Exp $ */ require_once 'Damblan/Trackback.php'; @@ -131,20 +131,31 @@ // Information about the latest release below the summary $versions = array_keys($pkg['releases']); -$uri = (isset($redirected) && $redirected === true) ? preg_replace('@/package(/[^/]+)/redirected@', '\1', $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; +// Workaround for my dev-env (running on 81) +$port = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT']; + +/* -$url = 'http://'.$_SERVER['SERVER_NAME'].$uri; + Disabling trackbacks for the moment, because of Services_Trackback update. + +// Preparing trackback data +$uri = (isset($redirected) && $redirected === true) ? preg_replace('@/package(/[^/]+)/redirected@', '\1', $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; +$url = 'http://'.$_SERVER['SERVER_NAME'].$port.$uri; // Get trackback autodiscovery code $tmpTrackback = Services_Trackback::create(array( 'id' => $name, 'url' => $url, 'title' => 'Package :: ' . htmlspecialchars($name), - 'trackback_url' => 'http://'.$_SERVER['SERVER_NAME'].'/trackback/trackback.php?id='.$name, + 'trackback_url' => 'http://'.$_SERVER['SERVER_NAME'].$port.'/trackback/trackback.php?id='.$name, )); $trackbackRDF = $tmpTrackback->getAutodiscoveryCode(); +*/ + +$trackbackRDF = ''; + // }}} // {{{ page header @@ -416,6 +427,12 @@ // }}} } elseif ($action == 'trackbacks') { +PEAR::raiseError('Sorry, the trackback section is temporarely disabled because of updates.'); + +/* + + Disabling trackbacks for the moment, because of Services_Trackback update. + // Generate trackback list $trackbacks = Damblan_Trackback::listTrackbacks($dbh, $name, !$trackbackIsAdmin); @@ -423,7 +440,7 @@ when a weblog entry is created, which is related to the package. If you want to learn more about trackbacks, please take a look at " <a href="http://www.movabletype.org/trackback/beginners/">A Beginner\'s Guide to TrackBack</a>" (by movabletype.org).</p>'; - print '<p>The trackback URL for this package is: <a href="'.$tmpTrackback->trackback_url.'">'.$tmpTrackback->trackback_url.'</a>'; + print '<p>The trackback URL for this package is: <a href="'.$tmpTrackback->get('trackback_url').'">'.$tmpTrackback->get('trackback_url').'</a>'; if ($trackbackIsAdmin) { print '<div class="explain">You may manipulate the trackbacks of this package. In contrast to normal users, you see approved and pending trackbacks </div>'; @@ -440,7 +457,7 @@ print 'Weblog:'; print '</th>'; print '<td class="ulcell" style="width:100%">'; - print $trackback->blog_name; + print $trackback->get('blog_name'); print '</td>'; print '</tr>'; @@ -450,7 +467,7 @@ print 'Approved:'; print '</th>'; print '<td class="ulcell">'; - print ($trackback->approved) ? '<b>yes</b>' : '<b>no</b>'; + print ($trackback->get('approved')) ? '<b>yes</b>' : '<b>no</b>'; print '</td>'; print '</tr>'; } @@ -459,7 +476,7 @@ print 'Title:'; print '</th>'; print '<td class="ulcell">'; - print '<a href="'.$trackback->url.'">'.$trackback->title.'</a>'; + print '<a href="'.$trackback->get('url').'">'.$trackback->get('title').'</a>'; print '</td>'; print '</tr>'; @@ -468,7 +485,7 @@ print 'Date:'; print '</th>'; print '<td class="ulcell">'; - print make_utc_date($trackback->timestamp, 'Y-m-d'); + print make_utc_date($trackback->get('timestamp'), 'Y-m-d'); print '</td>'; print '</tr>'; @@ -476,19 +493,28 @@ print '<th class="others">'; print '</th>'; print '<td class="ulcell">'; - print $trackback->excerpt; + print $trackback->get('excerpt'); print '</td>'; print '</tr>'; if ($trackbackIsAdmin) { print '<tr>'; print '<th class="others">'; + print 'IP:'; print '</th>'; print '<td class="ulcell">'; - if (!$trackback->approved) { - print '[<a href="/trackback/trackback-admin.php?action=approve&id='.$trackback->id.'×tamp='.$trackback->timestamp.'">Approve</a>] '; + print $trackback->get('ip'); + print '</td>'; + print '</tr>'; + + print '<tr>'; + print '<th class="others">'; + print '</th>'; + print '<td class="ulcell">'; + if (!$trackback->get('approved')) { + print '[<a href="/trackback/trackback-admin.php?action=approve&id='.$trackback->get('id').'×tamp='.$trackback->get('timestamp').'">Approve</a>] '; } - print '[<a href="/trackback/trackback-admin.php?action=delete&id='.$trackback->id.'×tamp='.$trackback->timestamp.'">Delete</a>]'; + print '[<a href="/trackback/trackback-admin.php?action=delete&id='.$trackback->get('id').'×tamp='.$trackback->get('timestamp').'">Delete</a>]'; print '</td>'; print '</tr>'; } @@ -497,6 +523,9 @@ } print '</table>'; + +*/ + } // }}} http://cvs.php.net/diff.php/pearweb/public_html/trackback/trackback-admin.php?r1=1.8&r2=1.9&ty=u Index: pearweb/public_html/trackback/trackback-admin.php diff -u pearweb/public_html/trackback/trackback-admin.php:1.8 pearweb/public_html/trackback/trackback-admin.php:1.9 --- pearweb/public_html/trackback/trackback-admin.php:1.8 Wed Feb 23 01:29:41 2005 +++ pearweb/public_html/trackback/trackback-admin.php Tue Mar 1 05:15:41 2005 @@ -17,7 +17,7 @@ | Authors: Tobias Schlitt <toby@php.net> | | | +----------------------------------------------------------------------+ - $Id: trackback-admin.php,v 1.8 2005/02/23 06:29:41 cellog Exp $ + $Id: trackback-admin.php,v 1.9 2005/03/01 10:15:41 toby Exp $ */ auth_require('pear.dev'); @@ -37,7 +37,7 @@ include_once 'Damblan/Trackback.php'; include_once 'Damblan/Mailer.php'; -$trackback = new Damblan_Trackback(array('id' => $id), $timestamp); +$trackback = new Damblan_Trackback(array('id' => $id, 'timestamp' => $timestamp)); $res = $trackback->load($dbh); $error = false; @@ -58,13 +58,13 @@ } $mailData = array( - 'id' => $trackback->id, - 'blog_name' => $trackback->blog_name, - 'title' => $trackback->title, - 'url' => $trackback->url, - 'excerpt' => $trackback->excerpt, - 'date' => make_utc_date($trackback->timestamp), - 'timestamp' => $trackback->timestamp, + 'id' => $trackback->get('id'), + 'blog_name' => $trackback->get('blog_name'), + 'title' => $trackback->get('title'), + 'url' => $trackback->get('url'), + 'excerpt' => $trackback->get('excerpt'), + 'date' => make_utc_date($trackback->get('timestamp')), + 'timestamp' => $trackback->get('timestamp'), 'user' => $_COOKIE['PEAR_USER'], ); @@ -81,7 +81,7 @@ break; case 'delete': - $msg = '<div class="warnings">Really <a href="/trackback/trackback-admin.php?action=delete_verified&id='.$trackback->id.'×tamp='.$trackback->timestamp.'">delete</a> trackback '.$timestamp.' for '.$id.'?</div>'; + $msg = '<div class="warnings">Really <a href="/trackback/trackback-admin.php?action=delete_verified&id='.$trackback->get('id').'×tamp='.$trackback->get('timestamp').'">delete</a> trackback '.$timestamp.' for '.$id.'?</div>'; // Confirmation of the delete action, no auto redirect $relocator = ''; @@ -107,3 +107,5 @@ echo $msg; echo '<p>You should be redirected to the packages trackback page in 5 seconds. if this does not work, please click <a href="http://' . PEAR_CHANNELNAME . '/package/'.$id.'/trackbacks">here</a>.</p>'; response_footer(); + +?> http://cvs.php.net/diff.php/pearweb/public_html/trackback/trackback.php?r1=1.7&r2=1.8&ty=u Index: pearweb/public_html/trackback/trackback.php diff -u pearweb/public_html/trackback/trackback.php:1.7 pearweb/public_html/trackback/trackback.php:1.8 --- pearweb/public_html/trackback/trackback.php:1.7 Wed Feb 23 01:29:41 2005 +++ pearweb/public_html/trackback/trackback.php Tue Mar 1 05:15:41 2005 @@ -17,7 +17,7 @@ | Authors: Tobias Schlitt <toby@php.net> | | | +----------------------------------------------------------------------+ - $Id: trackback.php,v 1.7 2005/02/23 06:29:41 cellog Exp $ + $Id: trackback.php,v 1.8 2005/03/01 10:15:41 toby Exp $ */ require_once 'Damblan/Trackback.php'; require_once 'Damblan/Mailer.php'; @@ -53,7 +53,11 @@ } // Creating new trackback -$trackback = new Damblan_Trackback(array('id' => $id), time()); +$trackback = new Damblan_Trackback(array( + 'id' => $id, + 'timestamp' => time(), + 'ip' => $_SERVER['REMOTE_ADDR'], +)); $res = $trackback->receive(); if (PEAR::isError($res)) { @@ -61,6 +65,13 @@ exit; } +// Check for possible spam and stop it. +$res = $trackback->checkRepost($dbh); +if (PEAR::isError($res)) { + echo Services_Trackback::getResponseError($res->getMessage(), 1); +} + + $res = $trackback->save($dbh); if (PEAR::isError($res)) { echo Services_Trackback::getResponseError('Your trackback could not be saved, please try again or inform the administrator.', 1); @@ -68,13 +79,13 @@ } $mailData = array( - 'id' => $trackback->id, - 'blog_name' => $trackback->blog_name, - 'title' => $trackback->title, - 'url' => $trackback->url, - 'excerpt' => $trackback->excerpt, - 'date' => make_utc_date($trackback->timestamp), - 'timestamp' => $trackback->timestamp, + 'id' => $trackback->get('id'), + 'blog_name' => $trackback->get('blog_name'), + 'title' => $trackback->get('title'), + 'url' => $trackback->get('url'), + 'excerpt' => $trackback->get('excerpt'), + 'date' => make_utc_date($trackback->get('timestamp')), + 'timestamp' => $trackback->get('timestamp'), ); $mailer = Damblan_Mailer::create('Trackback_New', $mailData);
http://cvs.php.net/diff.php/pearweb/include/Damblan/Trackback.php?r1=1.6&r2=1.7&ty=u Index: pearweb/include/Damblan/Trackback.php diff -u pearweb/include/Damblan/Trackback.php:1.6 pearweb/include/Damblan/Trackback.php:1.7 --- pearweb/include/Damblan/Trackback.php:1.6 Sat Jan 29 12:47:37 2005 +++ pearweb/include/Damblan/Trackback.php Tue Mar 1 05:15:40 2005 @@ -10,7 +10,7 @@ * @var int * @since */ - var $_timestamp; + var $_timestamp = 0; /** * Boolean flag, if the trackback has been improved by a PEAR developer, yet. @@ -18,10 +18,23 @@ * @var bool * @since */ - var $_approved; + var $_approved = false; /** - * __construct + * The IP adress from where the trackback has been send. + * + * @var string + * @since + */ + var $_ip = ''; + + // How many trackbacks may be posted in a specific timespan? + var $_repostCount = 3; + // In which timespan? + var $_repostTimespan = 600; // 10 minutes + + /** + * Constructor * Overwriten constructor to get the timestamp while creating a trackback. * The timestamp is used as the primary key for the trackback table, in * combination with the trackback ID (which is the package name). @@ -42,33 +55,78 @@ * @param bool $approved Wether the trackback is approved, yet. Default is false. * @return void */ - function Damblan_Trackback($data, $timestamp, $approved = false) + function Damblan_Trackback($data) + { + foreach ($data as $key => $val) { + if ($key == 'approved') + $val = ($val == 'true'); + $this->set($key, $val); + } + } + + /** + * Check for possible spam + * Checks the database for recent trackbacks from the same IP. + * Every IP is allows to post $this->_repostCount times in + * $this->_repostTimespan seconds. + * + * @since + * @param object(DB) $dbh The database handle. + * @return mixed Bool true, if no repost, else or on error, PEAR::Error() + */ + function checkRepost(&$dbh) { - $this->_fromArray($data); - $this->_timestamp = (int)$timestamp; - $this->_approved = $approved; + $sql = 'SELECT COUNT(timestamp) FROM trackbacks WHERE + ip = '.$dbh->quoteSmart($this->get('ip')).' AND + timestamp > '.$dbh->quoteSmart($this->get('timestamp') - $this->_repostTimespan).' + GROUP BY ip'; + $res = $dbh->getOne($sql); + if (PEAR::isError($res)) { + return PEAR::raiseError('Database error, please try again later.'); + } + if ($res >= $this->_repostCount) { + return PEAR::raiseError('We only allow '.$this->_repostCount.' trackbacks in '.($this->_repostTimespan / 60).' minutes from a single host. Please try again later.'); + } + return true; } /** - * __get - * Overwritten __get() to receive timestamp and improved state correctly. + * get + * Overwritten get() to receive timestamp, ip and approved state correctly. * * @since * @access public * @param mixed $key The name of the property to receive. * @return mixed $val The value of the property. */ - function __get($key, &$val) + function get($key) { $testKey = '_'.$key; if (isset($this->$testKey)) { - $val = $this->$testKey; - return true; - } else if (isset($this->_data[$key])) { - $val = $this->_data[$key]; + return $this->$testKey; + } else { + return Services_Trackback::get($key); + } + } + + /** + * set + * Overwritten set() to set timestamp, ip and approved state correctly. + * + * @since + * @access public + * @param mixed $key The name of the property to receive. + * @return mixed $val The value of the property. + */ + function set($key, $value) + { + $testKey = '_'.$key; + if (isset($this->$testKey)) { + $this->$testKey = $value; return true; + } else { + return Services_Trackback::set($key, $value); } - return false; } /** @@ -87,16 +145,17 @@ $necessaryData = array('id', 'title', 'url', 'excerpt', 'blog_name'); $this->_checkData($necessaryData); $data = $this->_getDecodedData($necessaryData); - $this->_timestamp = time(); +// $this->set('timestamp', time()); $approved = ($this->_approved) ? 'true' : 'false'; - $sql = "INSERT INTO trackbacks (id, title, url, excerpt, blog_name, approved, timestamp) VALUES ( - ".$dbh->quoteSmart($data['id']).", - ".$dbh->quoteSmart($data['title']).", - ".$dbh->quoteSmart($data['url']).", - ".$dbh->quoteSmart($data['excerpt']).", - ".$dbh->quoteSmart($data['blog_name']).", - ".$dbh->quoteSmart($approved).", - ".$dbh->quoteSmart($this->_timestamp)." + $sql = "INSERT INTO trackbacks (id, title, url, excerpt, blog_name, approved, timestamp, ip) VALUES ( + ".$dbh->quoteSmart($this->get('id')).", + ".$dbh->quoteSmart($this->get('title')).", + ".$dbh->quoteSmart($this->get('url')).", + ".$dbh->quoteSmart($this->get('excerpt')).", + ".$dbh->quoteSmart($this->get('blog_name')).", + ".$dbh->quoteSmart($this->get('approved')).", + ".$dbh->quoteSmart($this->get('timestamp')).", + ".$dbh->quoteSmart($this->get('ip'))." )"; $res = $dbh->query($sql); if (DB::isError($res)) { @@ -131,9 +190,9 @@ $data = $this->_getDecodedData($necessaryData); - $sql = "SELECT id, title, excerpt, blog_name, url, timestamp, approved FROM trackbacks WHERE - id = ".$dbh->quoteSmart($data['id'])." - AND timestamp = ".$dbh->quoteSmart($this->_timestamp); + $sql = "SELECT id, title, excerpt, blog_name, url, timestamp, approved, ip FROM trackbacks WHERE + id = ".$dbh->quoteSmart($this->get('id'))." + AND timestamp = ".$dbh->quoteSmart($this->get('timestamp')); $res = $dbh->getRow($sql, null, DB_FETCHMODE_ASSOC); @@ -144,18 +203,10 @@ } foreach ($res as $key => $val) { - if (($key != 'timestamp') && ($key != 'approved')) { - $this->_data[$key] = $val; - } else { - switch ($key) { - case 'approved': - $this->_approved = ($val == 'true'); - break; - default: - $key = '_'.$key; - $this->$key = $val; - } + if ($key == 'approved') { + $val = ($val == 'true'); } + $this->set($key, $val); } return true; } @@ -182,8 +233,8 @@ return $data; } $sql = "UPDATE trackbacks SET approved = ".$dbh->quoteSmart('true')." WHERE - id = ".$dbh->quoteSmart($data['id'])." - AND timestamp = ".$dbh->quoteSmart($this->_timestamp); + id = ".$dbh->quoteSmart($this->get('id'))." + AND timestamp = ".$dbh->quoteSmart($this->get('timestamp')); $res = $dbh->query($sql) ; if (DB::isError($res)) { return PEAR::raiseError('Could not approve trackback.'); @@ -208,13 +259,13 @@ if (PEAR::isError($data)) { return $data; } - $res = $this->load($dbh, $this->_timestamp); + $res = $this->load($dbh, $this->get('timestamp')); if (PEAR::isError($res)) { return $res; } $sql = "DELETE FROM trackbacks WHERE - id = ".$dbh->quoteSmart($data['id'])." - AND timestamp = ".$dbh->quoteSmart($this->_timestamp); + id = ".$dbh->quoteSmart($this->get('id'))." + AND timestamp = ".$dbh->quoteSmart($this->get('timestamp')); $res = $dbh->query($sql) ; if (DB::isError($res)) { return PEAR::raiseError('Could not delete trackback: '.$res->getMessage()); @@ -240,7 +291,7 @@ */ function listTrackbacks(&$dbh, $id, $approvedOnly = true, $orderBy = 'timestamp DESC', $limit = 10) { - $sql = 'SELECT id, title, excerpt, blog_name, url, timestamp, approved FROM trackbacks WHERE + $sql = 'SELECT id, title, excerpt, blog_name, url, timestamp, approved, ip FROM trackbacks WHERE id LIKE '.$dbh->quoteSmart($id); if ($approvedOnly) { $sql .= ' AND approved = '.$dbh->quoteSmart('true'); @@ -253,15 +304,50 @@ } $ret = array(); foreach ($res as $row) { - $ts = $row['timestamp']; - unset($row['timestamp']); - $app = ($row['approved'] == 'true'); - unset($row['approved']); - $ret[] = new Damblan_Trackback($row, $ts, $app); + $ret[] = new Damblan_Trackback($row); } return $ret; } - + + /** + * recentTrackbacks + * Get a list of trackbacks independent from the package ID. + * + * @since + * @access public + * @static + * @param object(DB) $dbh The database connection object (PEAR::DB). + * @param int $offset Start position for the trackback list. + * @param int $number Number of trackbacks to list. + * @param bool $approvedOnly Wether to fetch only approved trackbacks (default is true). + * @return array Array of PEAR_Trackback objects. + * @throws Exception If no results are received. + */ + function recentTrackbacks(&$dbh, $offset = 0, $number = 10, $approvedOnly = true) + { + if ($offset < 0) { + return PEAR::raiseError('Offset out of range. Offset must be integer >= 0.'); + } + if (($number < 1) || ($number > 50)) { + return PEAR::raiseError('Number out of range. Number must be integer between 1 and 50.'); + } + $sql = 'SELECT id, title, excerpt, blog_name, url, timestamp, approved, ip + FROM trackbacks'; + if ($approvedOnly) { + $sql .= ' WHERE approved = '.$dbh->quoteSmart('true'); + } + $sql .= ' ORDER BY timestamp DESC LIMIT ' . $offset . ',' . $number; + $res = $dbh->getAll($sql, null, DB_FETCHMODE_ASSOC); + if (DB::isError($res)) { + return PEAR::raiseError('Could not receive trackback list: '.$res->getMessage()); + } + $ret = array(); + foreach ($res as $row) { + $ret[] = new Damblan_Trackback($row); + } + return $ret; + } + /** * Get maintainers to inform of a trackback (the lead maintainers of a package). * @@ -273,7 +359,7 @@ */ function getMaintainers () { - $maintainers = maintainer::get($this->id, true); + $maintainers = maintainer::get($this->get('id'), true); $res = array(); foreach ($maintainers as $maintainer => $data) { $tmpUser = user::info($maintainer, 'email'); @@ -284,9 +370,6 @@ } return $res; } - } -overload('Damblan_Trackback'); - ?> http://cvs.php.net/diff.php/pearweb/public_html/package-info.php?r1=1.98&r2=1.99&ty=u Index: pearweb/public_html/package-info.php diff -u pearweb/public_html/package-info.php:1.98 pearweb/public_html/package-info.php:1.99 --- pearweb/public_html/package-info.php:1.98 Sun Feb 13 19:43:37 2005 +++ pearweb/public_html/package-info.php Tue Mar 1 05:15:40 2005 @@ -16,7 +16,7 @@ | Authors: Martin Jansen <mj@php.net> | | Tomas V.V.Cox <cox@idecnet.com> | +----------------------------------------------------------------------+ - $Id: package-info.php,v 1.98 2005/02/14 00:43:37 dufuz Exp $ + $Id: package-info.php,v 1.99 2005/03/01 10:15:40 toby Exp $ */ require_once 'Damblan/Trackback.php'; @@ -131,20 +131,31 @@ // Information about the latest release below the summary $versions = array_keys($pkg['releases']); -$uri = (isset($redirected) && $redirected === true) ? preg_replace('@/package(/[^/]+)/redirected@', '\1', $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; +// Workaround for my dev-env (running on 81) +$port = ($_SERVER['SERVER_PORT'] == 80) ? '' : ':'.$_SERVER['SERVER_PORT']; + +/* -$url = 'http://'.$_SERVER['SERVER_NAME'].$uri; + Disabling trackbacks for the moment, because of Services_Trackback update. + +// Preparing trackback data +$uri = (isset($redirected) && $redirected === true) ? preg_replace('@/package(/[^/]+)/redirected@', '\1', $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']; +$url = 'http://'.$_SERVER['SERVER_NAME'].$port.$uri; // Get trackback autodiscovery code $tmpTrackback = Services_Trackback::create(array( 'id' => $name, 'url' => $url, 'title' => 'Package :: ' . htmlspecialchars($name), - 'trackback_url' => 'http://'.$_SERVER['SERVER_NAME'].'/trackback/trackback.php?id='.$name, + 'trackback_url' => 'http://'.$_SERVER['SERVER_NAME'].$port.'/trackback/trackback.php?id='.$name, )); $trackbackRDF = $tmpTrackback->getAutodiscoveryCode(); +*/ + +$trackbackRDF = ''; + // }}} // {{{ page header @@ -416,6 +427,12 @@ // }}} } elseif ($action == 'trackbacks') { +PEAR::raiseError('Sorry, the trackback section is temporarely disabled because of updates.'); + +/* + + Disabling trackbacks for the moment, because of Services_Trackback update. + // Generate trackback list $trackbacks = Damblan_Trackback::listTrackbacks($dbh, $name, !$trackbackIsAdmin); @@ -423,7 +440,7 @@ when a weblog entry is created, which is related to the package. If you want to learn more about trackbacks, please take a look at " <a href="http://www.movabletype.org/trackback/beginners/">A Beginner\'s Guide to TrackBack</a>" (by movabletype.org).</p>'; - print '<p>The trackback URL for this package is: <a href="'.$tmpTrackback->trackback_url.'">'.$tmpTrackback->trackback_url.'</a>'; + print '<p>The trackback URL for this package is: <a href="'.$tmpTrackback->get('trackback_url').'">'.$tmpTrackback->get('trackback_url').'</a>'; if ($trackbackIsAdmin) { print '<div class="explain">You may manipulate the trackbacks of this package. In contrast to normal users, you see approved and pending trackbacks </div>'; @@ -440,7 +457,7 @@ print 'Weblog:'; print '</th>'; print '<td class="ulcell" style="width:100%">'; - print $trackback->blog_name; + print $trackback->get('blog_name'); print '</td>'; print '</tr>'; @@ -450,7 +467,7 @@ print 'Approved:'; print '</th>'; print '<td class="ulcell">'; - print ($trackback->approved) ? '<b>yes</b>' : '<b>no</b>'; + print ($trackback->get('approved')) ? '<b>yes</b>' : '<b>no</b>'; print '</td>'; print '</tr>'; } @@ -459,7 +476,7 @@ print 'Title:'; print '</th>'; print '<td class="ulcell">'; - print '<a href="'.$trackback->url.'">'.$trackback->title.'</a>'; + print '<a href="'.$trackback->get('url').'">'.$trackback->get('title').'</a>'; print '</td>'; print '</tr>'; @@ -468,7 +485,7 @@ print 'Date:'; print '</th>'; print '<td class="ulcell">'; - print make_utc_date($trackback->timestamp, 'Y-m-d'); + print make_utc_date($trackback->get('timestamp'), 'Y-m-d'); print '</td>'; print '</tr>'; @@ -476,19 +493,28 @@ print '<th class="others">'; print '</th>'; print '<td class="ulcell">'; - print $trackback->excerpt; + print $trackback->get('excerpt'); print '</td>'; print '</tr>'; if ($trackbackIsAdmin) { print '<tr>'; print '<th class="others">'; + print 'IP:'; print '</th>'; print '<td class="ulcell">'; - if (!$trackback->approved) { - print '[<a href="/trackback/trackback-admin.php?action=approve&id='.$trackback->id.'×tamp='.$trackback->timestamp.'">Approve</a>] '; + print $trackback->get('ip'); + print '</td>'; + print '</tr>'; + + print '<tr>'; + print '<th class="others">'; + print '</th>'; + print '<td class="ulcell">'; + if (!$trackback->get('approved')) { + print '[<a href="/trackback/trackback-admin.php?action=approve&id='.$trackback->get('id').'×tamp='.$trackback->get('timestamp').'">Approve</a>] '; } - print '[<a href="/trackback/trackback-admin.php?action=delete&id='.$trackback->id.'×tamp='.$trackback->timestamp.'">Delete</a>]'; + print '[<a href="/trackback/trackback-admin.php?action=delete&id='.$trackback->get('id').'×tamp='.$trackback->get('timestamp').'">Delete</a>]'; print '</td>'; print '</tr>'; } @@ -497,6 +523,9 @@ } print '</table>'; + +*/ + } // }}} http://cvs.php.net/diff.php/pearweb/public_html/trackback/trackback-admin.php?r1=1.8&r2=1.9&ty=u Index: pearweb/public_html/trackback/trackback-admin.php diff -u pearweb/public_html/trackback/trackback-admin.php:1.8 pearweb/public_html/trackback/trackback-admin.php:1.9 --- pearweb/public_html/trackback/trackback-admin.php:1.8 Wed Feb 23 01:29:41 2005 +++ pearweb/public_html/trackback/trackback-admin.php Tue Mar 1 05:15:41 2005 @@ -17,7 +17,7 @@ | Authors: Tobias Schlitt <toby@php.net> | | | +----------------------------------------------------------------------+ - $Id: trackback-admin.php,v 1.8 2005/02/23 06:29:41 cellog Exp $ + $Id: trackback-admin.php,v 1.9 2005/03/01 10:15:41 toby Exp $ */ auth_require('pear.dev'); @@ -37,7 +37,7 @@ include_once 'Damblan/Trackback.php'; include_once 'Damblan/Mailer.php'; -$trackback = new Damblan_Trackback(array('id' => $id), $timestamp); +$trackback = new Damblan_Trackback(array('id' => $id, 'timestamp' => $timestamp)); $res = $trackback->load($dbh); $error = false; @@ -58,13 +58,13 @@ } $mailData = array( - 'id' => $trackback->id, - 'blog_name' => $trackback->blog_name, - 'title' => $trackback->title, - 'url' => $trackback->url, - 'excerpt' => $trackback->excerpt, - 'date' => make_utc_date($trackback->timestamp), - 'timestamp' => $trackback->timestamp, + 'id' => $trackback->get('id'), + 'blog_name' => $trackback->get('blog_name'), + 'title' => $trackback->get('title'), + 'url' => $trackback->get('url'), + 'excerpt' => $trackback->get('excerpt'), + 'date' => make_utc_date($trackback->get('timestamp')), + 'timestamp' => $trackback->get('timestamp'), 'user' => $_COOKIE['PEAR_USER'], ); @@ -81,7 +81,7 @@ break; case 'delete': - $msg = '<div class="warnings">Really <a href="/trackback/trackback-admin.php?action=delete_verified&id='.$trackback->id.'×tamp='.$trackback->timestamp.'">delete</a> trackback '.$timestamp.' for '.$id.'?</div>'; + $msg = '<div class="warnings">Really <a href="/trackback/trackback-admin.php?action=delete_verified&id='.$trackback->get('id').'×tamp='.$trackback->get('timestamp').'">delete</a> trackback '.$timestamp.' for '.$id.'?</div>'; // Confirmation of the delete action, no auto redirect $relocator = ''; @@ -107,3 +107,5 @@ echo $msg; echo '<p>You should be redirected to the packages trackback page in 5 seconds. if this does not work, please click <a href="http://' . PEAR_CHANNELNAME . '/package/'.$id.'/trackbacks">here</a>.</p>'; response_footer(); + +?> http://cvs.php.net/diff.php/pearweb/public_html/trackback/trackback.php?r1=1.7&r2=1.8&ty=u Index: pearweb/public_html/trackback/trackback.php diff -u pearweb/public_html/trackback/trackback.php:1.7 pearweb/public_html/trackback/trackback.php:1.8 --- pearweb/public_html/trackback/trackback.php:1.7 Wed Feb 23 01:29:41 2005 +++ pearweb/public_html/trackback/trackback.php Tue Mar 1 05:15:41 2005 @@ -17,7 +17,7 @@ | Authors: Tobias Schlitt <toby@php.net> | | | +----------------------------------------------------------------------+ - $Id: trackback.php,v 1.7 2005/02/23 06:29:41 cellog Exp $ + $Id: trackback.php,v 1.8 2005/03/01 10:15:41 toby Exp $ */ require_once 'Damblan/Trackback.php'; require_once 'Damblan/Mailer.php'; @@ -53,7 +53,11 @@ } // Creating new trackback -$trackback = new Damblan_Trackback(array('id' => $id), time()); +$trackback = new Damblan_Trackback(array( + 'id' => $id, + 'timestamp' => time(), + 'ip' => $_SERVER['REMOTE_ADDR'], +)); $res = $trackback->receive(); if (PEAR::isError($res)) { @@ -61,6 +65,13 @@ exit; } +// Check for possible spam and stop it. +$res = $trackback->checkRepost($dbh); +if (PEAR::isError($res)) { + echo Services_Trackback::getResponseError($res->getMessage(), 1); +} + + $res = $trackback->save($dbh); if (PEAR::isError($res)) { echo Services_Trackback::getResponseError('Your trackback could not be saved, please try again or inform the administrator.', 1); @@ -68,13 +79,13 @@ } $mailData = array( - 'id' => $trackback->id, - 'blog_name' => $trackback->blog_name, - 'title' => $trackback->title, - 'url' => $trackback->url, - 'excerpt' => $trackback->excerpt, - 'date' => make_utc_date($trackback->timestamp), - 'timestamp' => $trackback->timestamp, + 'id' => $trackback->get('id'), + 'blog_name' => $trackback->get('blog_name'), + 'title' => $trackback->get('title'), + 'url' => $trackback->get('url'), + 'excerpt' => $trackback->get('excerpt'), + 'date' => make_utc_date($trackback->get('timestamp')), + 'timestamp' => $trackback->get('timestamp'), ); $mailer = Damblan_Mailer::create('Trackback_New', $mailData);