Create a gist now

Instantly share code, notes, and snippets.

anonymous /topics_anywhere.php
Created Jan 24, 2010

Embed
<?php
$dbms = '';
$dbhost = '';
$dbport = '';
$dbname = '';
$dbuser = '';
$dbpasswd = '';
$table_prefix = '';
$limit = 10; // Topics limit
$length = 30; // Title length limit (in characters)
// Set an exception handler
set_exception_handler('handle_exception');
if (version_compare(PHP_VERSION, '5.0.0', '<'))
{
throw new Exception('Wymagane PHP 5 lub nowsze!');
}
if (!class_exists('PDO'))
{
throw new Exception('Wymagane rozszerzenie PHP Data Objects (PDO)!');
}
/**
* @param object $exception
* @return string
*/
function handle_exception($exception)
{
echo $exception->getMessage();
}
/**
* @param string $format
* @param int $timestamp
* @param int $timezone
* @param bool $dst
* @return string
*/
function format_date($format, $timestamp, $timezone, $dst)
{
$l10n = array(
'Tomorrow' => 'jutro',
'Today' => 'dzisiaj',
'Yesterday' => 'wczoraj',
'Ago' => array(
0 => 'niecałą minutę temu',
1 => '%d min temu',
2 => '%d min temu',
60 => '1 godzinę temu'
),
'Monday' => 'poniedziałek',
'Tuesday' => 'wtorek',
'Wednesday' => 'środa',
'Thursday' => 'czwartek',
'Friday' => 'piątek',
'Saturday' => 'sobota',
'Sunday' => 'niedziela',
'Mon' => 'pn',
'Tue' => 'wt',
'Wed' => 'śr',
'Thu' => 'czw',
'Fri' => 'pt',
'Sat' => 'sob',
'Sun' => 'ndz',
'January' => 'stycznia',
'February' => 'lutego',
'March' => 'marca',
'April' => 'kwietnia',
'May' => 'maja',
'June' => 'czerwca',
'July' => 'lipca',
'August' => 'sierpnia',
'September' => 'września',
'October' => 'października',
'November' => 'listopada',
'December' => 'grudnia',
'Jan' => 'sty',
'Feb' => 'lut',
'Mar' => 'mar',
'Apr' => 'kwie',
'May_short' => 'maja',
'Jun' => 'cze',
'Jul' => 'lip',
'Aug' => 'sie',
'Sep' => 'wrz',
'Oct' => 'paź',
'Nov' => 'lis',
'Dec' => 'gru',
);
$zone_offset = $timezone * 3600 + $dst * 3600;
$midnight = gmmktime(0, 0, 0) - $zone_offset;
$delta = time() - $timestamp;
$date = array(
'is_short' => strpos($format, '|'),
'format_short' => substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1),
'format_long' => str_replace('|', '', $format)
);
// Some languages use different terms for the long and short format of May
if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
{
$l10n['May'] = $l10n['May_short'];
}
if ($date['is_short'] !== false)
{
// Relative dates in minutes and hours, e.g. 5 minutes ago
if ($delta < 60)
{
return $l10n['Ago'][0];
}
else if ($delta < 120)
{
return sprintf($l10n['Ago'][1], floor($delta / 60));
}
else if ($delta < 3600 )
{
return sprintf($l10n['Ago'][2], floor($delta / 60));
}
else if ($delta < 7200 )
{
return sprintf($l10n['Ago'][60], floor($delta / 3600));
}
// Relative dates in days, e.g. Today, 18:04
if ($timestamp > $midnight + 86400 || $timestamp > $midnight - 86400)
{
if ($timestamp > $midnight + 86400)
{
$day = 'Tomorrow';
}
else if ($timestamp > $midnight)
{
$day = 'Today';
}
else if ($timestamp > $midnight - 86400)
{
$day = 'Yesterday';
}
return str_replace('||', $l10n[$day], strtr(gmdate($date['format_short'], $timestamp + $zone_offset), $l10n));
}
}
return strtr(gmdate($date['format_long'], $timestamp + $zone_offset), $l10n);
}
switch ($dbms)
{
case 'mysql':
case 'mysqli':
case 'postgres':
$dsn = ($dbms === 'postgres') ? 'pgsql:' : 'mysql:';
if (!empty($dbhost))
{
$dsn .= "host=$dbhost;";
}
if (!empty($dbport))
{
$dsn .= "port=$dbport;";
}
if (!empty($dbname))
{
$dsn .= "dbname=$dbname;";
}
break;
case 'oracle':
$dsn = 'oci:';
if (!empty($dbname))
{
$dsn .= "dbname=$dbname;";
}
break;
case 'sqlite':
$dsn = 'sqlite2:';
if (!empty($dbhost))
{
$dsn .= $dbhost;
}
if (!empty($dbport))
{
$dsn .= ":$dbport";
}
break;
}
$pdo = new PDO($dsn, $dbuser, $dbpasswd);
switch ($dbms)
{
case 'mysql':
case 'mysqli':
$pdo->exec('SET NAMES utf8');
break;
}
$sql = "SELECT *
FROM {$table_prefix}config
WHERE config_name = 'board_dst'
OR config_name = 'board_timezone'
OR config_name = 'default_dateformat'
OR config_name = 'script_path'
OR config_name = 'server_name'
OR config_name = 'server_protocol'";
$result = $pdo->query($sql);
// Make configuration array
while ($row = $result->fetch())
{
$config[$row['config_name']] = $row['config_value'];
}
// Create basic forum path
$path = $config['server_protocol'] . $config['server_name'] . $config['script_path'];
// Add "/" at the end of path if missing
if (substr($path, -1) !== '/')
{
$path .= '/';
}
$sql = "SELECT topic_id, forum_id, topic_title, topic_last_poster_id, topic_last_poster_name, topic_last_poster_colour, topic_last_post_time
FROM {$table_prefix}topics
WHERE topic_approved = 1
ORDER BY topic_last_post_time DESC
LIMIT $limit";
$result = $pdo->query($sql);
echo '<ul>';
foreach ($result as $row)
{
// @todo Make this code more readable
echo '<li>';
echo '<a href="', $path, 'viewtopic.php?f=', $row['forum_id'], '&t=', $row['topic_id'], '" title="', $row['topic_title'], '">', mb_strimwidth($row['topic_title'], 0, $length, '', 'UTF-8'), '</a>';
echo ' przez <a href="', $path, 'memberlist.php?mode=viewprofile&u=', $row['topic_last_poster_id'], '"';
if (!empty($row['topic_last_poster_colour']))
{
echo 'style="font-weight: bold; color: ', $row['topic_last_poster_colour'], '"';
}
echo '>', $row['topic_last_poster_name'], '</a> – ', format_date($config['default_dateformat'], $row['topic_last_post_time'], $config['board_timezone'], $config['board_dst']);
echo '</li>';
}
echo '</ul>';
$result->closeCursor();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment