2012-10-11 08:00:15 +00:00
|
|
|
<?
|
2013-04-17 08:00:58 +00:00
|
|
|
class Format {
|
2013-01-03 08:00:30 +00:00
|
|
|
/**
|
|
|
|
* Torrent Labels
|
|
|
|
* Map a common display string to a CSS class
|
|
|
|
* Indexes are lower case
|
|
|
|
* Note the "tl_" prefix for "torrent label"
|
|
|
|
*
|
|
|
|
* There are five basic types:
|
|
|
|
* * tl_free (leech status)
|
|
|
|
* * tl_snatched
|
|
|
|
* * tl_reported
|
|
|
|
* * tl_approved
|
|
|
|
* * tl_notice (default)
|
|
|
|
*
|
|
|
|
* @var array Strings
|
|
|
|
*/
|
|
|
|
private static $TorrentLabels = array(
|
|
|
|
'default' => 'tl_notice',
|
|
|
|
'snatched' => 'tl_snatched',
|
|
|
|
|
2013-04-18 08:00:54 +00:00
|
|
|
'freeleech' => 'tl_free',
|
|
|
|
'neutral leech' => 'tl_free tl_neutral',
|
|
|
|
'personal freeleech'=> 'tl_free tl_personal',
|
2013-01-03 08:00:30 +00:00
|
|
|
|
2013-04-18 08:00:54 +00:00
|
|
|
'reported' => 'tl_reported',
|
|
|
|
'bad tags' => 'tl_reported tl_bad_tags',
|
|
|
|
'bad folders' => 'tl_reported tl_bad_folders',
|
|
|
|
'bad file names'=> 'tl_reported tl_bad_file_names',
|
2013-01-03 08:00:30 +00:00
|
|
|
|
2013-04-18 08:00:54 +00:00
|
|
|
'cassette approved' => 'tl_approved tl_cassete',
|
|
|
|
'lossy master approved' => 'tl_approved tl_lossy_master',
|
|
|
|
'lossy web approved' => 'tl_approved tl_lossy_web'
|
2013-01-03 08:00:30 +00:00
|
|
|
);
|
2013-02-22 08:00:24 +00:00
|
|
|
|
2012-10-11 08:00:15 +00:00
|
|
|
/**
|
|
|
|
* Shorten a string
|
|
|
|
*
|
|
|
|
* @param $Str string to cut
|
|
|
|
* @param $Length cut at length
|
|
|
|
* @param $Hard force cut at length instead of at closest word
|
|
|
|
* @param $ShowDots Show dots at the end
|
|
|
|
* @return string formatted string
|
|
|
|
*/
|
|
|
|
public static function cut_string($Str, $Length, $Hard = false, $ShowDots = true) {
|
|
|
|
if (mb_strlen($Str, 'UTF-8') > $Length) {
|
|
|
|
if ($Hard == 0) {
|
|
|
|
// Not hard, cut at closest word
|
|
|
|
$CutDesc = mb_substr($Str, 0, $Length, 'UTF-8');
|
|
|
|
$DescArr = explode(' ', $CutDesc);
|
|
|
|
if (count($DescArr) > 1) {
|
|
|
|
array_pop($DescArr);
|
|
|
|
$CutDesc = implode(' ', $DescArr);
|
|
|
|
}
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($ShowDots) {
|
2013-08-28 23:08:41 +00:00
|
|
|
//TODO: should we replace the three dots with an ellipsis character?
|
2013-04-17 08:00:58 +00:00
|
|
|
$CutDesc .= '...';
|
|
|
|
}
|
2012-10-11 08:00:15 +00:00
|
|
|
} else {
|
|
|
|
$CutDesc = mb_substr($Str, 0, $Length, 'UTF-8');
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($ShowDots) {
|
|
|
|
$CutDesc .= '...';
|
|
|
|
}
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
return $CutDesc;
|
|
|
|
} else {
|
|
|
|
return $Str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the CSS class corresponding to a ratio
|
|
|
|
*
|
|
|
|
* @param $Ratio ratio to get the css class for
|
|
|
|
* @return string the CSS class corresponding to the ratio range
|
|
|
|
*/
|
|
|
|
public static function get_ratio_color($Ratio) {
|
|
|
|
if ($Ratio < 0.1) { return 'r00'; }
|
|
|
|
if ($Ratio < 0.2) { return 'r01'; }
|
|
|
|
if ($Ratio < 0.3) { return 'r02'; }
|
|
|
|
if ($Ratio < 0.4) { return 'r03'; }
|
|
|
|
if ($Ratio < 0.5) { return 'r04'; }
|
|
|
|
if ($Ratio < 0.6) { return 'r05'; }
|
|
|
|
if ($Ratio < 0.7) { return 'r06'; }
|
|
|
|
if ($Ratio < 0.8) { return 'r07'; }
|
|
|
|
if ($Ratio < 0.9) { return 'r08'; }
|
|
|
|
if ($Ratio < 1) { return 'r09'; }
|
|
|
|
if ($Ratio < 2) { return 'r10'; }
|
|
|
|
if ($Ratio < 5) { return 'r20'; }
|
|
|
|
return 'r50';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates and formats a ratio.
|
|
|
|
*
|
|
|
|
* @param int $Dividend AKA numerator
|
|
|
|
* @param int $Divisor
|
|
|
|
* @param boolean $Color if true, ratio will be coloured.
|
2012-10-27 08:00:09 +00:00
|
|
|
* @return string formatted ratio HTML
|
2012-10-11 08:00:15 +00:00
|
|
|
*/
|
2013-04-17 08:00:58 +00:00
|
|
|
public static function get_ratio_html($Dividend, $Divisor, $Color = true) {
|
2012-10-27 08:00:09 +00:00
|
|
|
$Ratio = self::get_ratio($Dividend, $Divisor);
|
|
|
|
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($Ratio === false) {
|
|
|
|
return '--';
|
|
|
|
}
|
|
|
|
if ($Ratio === '∞') {
|
2013-10-26 08:00:58 +00:00
|
|
|
return '<span class="tooltip r99" title="Infinite">∞</span>';
|
2013-04-17 08:00:58 +00:00
|
|
|
}
|
|
|
|
if ($Color) {
|
2013-10-26 08:00:58 +00:00
|
|
|
$Ratio = sprintf('<span class="tooltip %s" title="%s">%s</span>',
|
2012-10-27 08:00:09 +00:00
|
|
|
self::get_ratio_color($Ratio),
|
|
|
|
self::get_ratio($Dividend, $Divisor, 5),
|
|
|
|
$Ratio
|
|
|
|
);
|
2013-04-17 08:00:58 +00:00
|
|
|
}
|
2012-10-27 08:00:09 +00:00
|
|
|
|
2012-10-11 08:00:15 +00:00
|
|
|
return $Ratio;
|
|
|
|
}
|
|
|
|
|
2012-10-27 08:00:09 +00:00
|
|
|
/**
|
|
|
|
* Returns ratio
|
|
|
|
* @param int $Dividend
|
|
|
|
* @param int $Divisor
|
2013-08-28 23:08:41 +00:00
|
|
|
* @param int $Decimal floor to n decimals (e.g. subtract .005 to floor to 2 decimals)
|
2012-11-09 08:00:18 +00:00
|
|
|
* @return boolean|string
|
2012-10-27 08:00:09 +00:00
|
|
|
*/
|
2013-09-08 08:00:57 +00:00
|
|
|
public static function get_ratio($Dividend, $Divisor, $Decimal = 2) {
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($Divisor == 0 && $Dividend == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($Divisor == 0) {
|
|
|
|
return '∞';
|
|
|
|
}
|
|
|
|
return number_format(max($Dividend / $Divisor - (0.5 / pow(10, $Decimal)), 0), $Decimal);
|
2012-10-27 08:00:09 +00:00
|
|
|
}
|
2012-10-11 08:00:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the query string of the current page, minus the parameters in $Exclude
|
|
|
|
*
|
|
|
|
* @param array $Exclude Query string parameters to leave out, or blank to include all parameters.
|
|
|
|
* @return An HTML sanatized query string
|
|
|
|
*/
|
|
|
|
public static function get_url($Exclude = false) {
|
|
|
|
if ($Exclude !== false) {
|
|
|
|
$QueryItems = array();
|
|
|
|
parse_str($_SERVER['QUERY_STRING'], $QueryItems);
|
|
|
|
|
|
|
|
foreach ($QueryItems AS $Key => $Val) {
|
2013-04-21 08:00:52 +00:00
|
|
|
if (!in_array(strtolower($Key), $Exclude)) {
|
2012-10-11 08:00:15 +00:00
|
|
|
$Query[$Key] = $Val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($Query)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return display_str(http_build_query($Query));
|
|
|
|
} else {
|
|
|
|
return display_str($_SERVER['QUERY_STRING']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds what page we're on and gives it to us, as well as the LIMIT clause for SQL
|
|
|
|
* Takes in $_GET['page'] as an additional input
|
|
|
|
*
|
|
|
|
* @param $PerPage Results to show per page
|
|
|
|
* @param $DefaultResult Optional, which result's page we want if no page is specified
|
|
|
|
* If this parameter is not specified, we will default to page 1
|
|
|
|
*
|
2013-08-28 23:08:41 +00:00
|
|
|
* @return array(int, string) What page we are on, and what to use in the LIMIT section of a query
|
2013-04-21 08:00:52 +00:00
|
|
|
* e.g. "SELECT [...] LIMIT $Limit;"
|
2012-10-11 08:00:15 +00:00
|
|
|
*/
|
|
|
|
public static function page_limit($PerPage, $DefaultResult = 1) {
|
|
|
|
if (!isset($_GET['page'])) {
|
2013-04-21 08:00:52 +00:00
|
|
|
$Page = ceil($DefaultResult / $PerPage);
|
|
|
|
if ($Page == 0) {
|
|
|
|
$Page = 1;
|
|
|
|
}
|
|
|
|
$Limit = $PerPage;
|
2012-10-11 08:00:15 +00:00
|
|
|
} else {
|
|
|
|
if (!is_number($_GET['page'])) {
|
|
|
|
error(0);
|
|
|
|
}
|
|
|
|
$Page = $_GET['page'];
|
2013-04-21 08:00:52 +00:00
|
|
|
if ($Page <= 0) {
|
|
|
|
$Page = 1;
|
|
|
|
}
|
2013-08-28 23:08:41 +00:00
|
|
|
$Limit = $PerPage * $Page - $PerPage . ", $PerPage";
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
2013-04-21 08:00:52 +00:00
|
|
|
return array($Page, $Limit);
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A9 magic. Some other poor soul can write the phpdoc.
|
2013-04-21 08:00:52 +00:00
|
|
|
// For data stored in memcached catalogues (giant arrays), e.g. forum threads
|
|
|
|
public static function catalogue_limit($Page, $PerPage, $CatalogueSize = 500) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$CatalogueID = floor(($PerPage * $Page - $PerPage) / $CatalogueSize);
|
|
|
|
$CatalogueLimit = ($CatalogueID * $CatalogueSize).", $CatalogueSize";
|
2012-10-11 08:00:15 +00:00
|
|
|
return array($CatalogueID, $CatalogueLimit);
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:00:58 +00:00
|
|
|
public static function catalogue_select($Catalogue, $Page, $PerPage, $CatalogueSize = 500) {
|
2013-04-21 08:00:52 +00:00
|
|
|
return array_slice($Catalogue, (($PerPage * $Page - $PerPage) % $CatalogueSize), $PerPage, true);
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Get pages
|
|
|
|
* Returns a page list, given certain information about the pages.
|
|
|
|
*
|
|
|
|
* @param int $StartPage: The current record the page you're on starts with.
|
2013-04-17 08:00:58 +00:00
|
|
|
* e.g. if you're on page 2 of a forum thread with 25 posts per page, $StartPage is 25.
|
2012-10-11 08:00:15 +00:00
|
|
|
* If you're on page 1, $StartPage is 0.
|
|
|
|
* @param int $TotalRecords: The total number of records in the result set.
|
2013-04-17 08:00:58 +00:00
|
|
|
* e.g. if you're on a forum thread with 152 posts, $TotalRecords is 152.
|
2012-10-11 08:00:15 +00:00
|
|
|
* @param int $ItemsPerPage: Self-explanatory. The number of records shown on each page
|
2013-04-17 08:00:58 +00:00
|
|
|
* e.g. if there are 25 posts per forum page, $ItemsPerPage is 25.
|
2012-10-11 08:00:15 +00:00
|
|
|
* @param int $ShowPages: The number of page links that are shown.
|
2013-04-17 08:00:58 +00:00
|
|
|
* e.g. If there are 20 pages that exist, but $ShowPages is only 11, only 11 links will be shown.
|
2012-10-11 08:00:15 +00:00
|
|
|
* @param string $Anchor A URL fragment to attach to the links.
|
2013-04-17 08:00:58 +00:00
|
|
|
* e.g. '#comment12'
|
2012-10-11 08:00:15 +00:00
|
|
|
* @return A sanitized HTML page listing.
|
|
|
|
*/
|
2013-04-17 08:00:58 +00:00
|
|
|
public static function get_pages($StartPage, $TotalRecords, $ItemsPerPage, $ShowPages = 11, $Anchor = '') {
|
2012-10-11 08:00:15 +00:00
|
|
|
global $Document, $Method, $Mobile;
|
2013-08-28 23:08:41 +00:00
|
|
|
$Location = "$Document.php";
|
2012-10-11 08:00:15 +00:00
|
|
|
$StartPage = ceil($StartPage);
|
|
|
|
$TotalPages = 0;
|
|
|
|
if ($TotalRecords > 0) {
|
2013-04-17 08:00:58 +00:00
|
|
|
$StartPage = min($StartPage, ceil($TotalRecords / $ItemsPerPage));
|
2012-10-11 08:00:15 +00:00
|
|
|
|
|
|
|
$ShowPages--;
|
2013-04-17 08:00:58 +00:00
|
|
|
$TotalPages = ceil($TotalRecords / $ItemsPerPage);
|
2012-10-11 08:00:15 +00:00
|
|
|
|
|
|
|
if ($TotalPages > $ShowPages) {
|
2013-04-17 08:00:58 +00:00
|
|
|
$StartPosition = $StartPage - round($ShowPages / 2);
|
2012-10-11 08:00:15 +00:00
|
|
|
|
|
|
|
if ($StartPosition <= 0) {
|
|
|
|
$StartPosition = 1;
|
|
|
|
} else {
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($StartPosition >= ($TotalPages - $ShowPages)) {
|
|
|
|
$StartPosition = $TotalPages - $ShowPages;
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 08:00:58 +00:00
|
|
|
$StopPage = $ShowPages + $StartPosition;
|
2012-10-11 08:00:15 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$StopPage = $TotalPages;
|
|
|
|
$StartPosition = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$StartPosition = max($StartPosition, 1);
|
|
|
|
|
2013-08-28 23:08:41 +00:00
|
|
|
$QueryString = self::get_url(array('page', 'post'));
|
2012-10-11 08:00:15 +00:00
|
|
|
if ($QueryString != '') {
|
2013-08-28 23:08:41 +00:00
|
|
|
$QueryString = "&$QueryString";
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$Pages = '';
|
|
|
|
|
|
|
|
if ($StartPage > 1) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= "<a href=\"$Location?page=1$QueryString$Anchor\"><strong><< First</strong></a> ";
|
|
|
|
$Pages .= "<a href=\"$Location?page=".($StartPage - 1).$QueryString.$Anchor.'" class="pager_prev"><strong>< Prev</strong></a> | ';
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
//End change
|
|
|
|
|
|
|
|
if (!$Mobile) {
|
|
|
|
for ($i = $StartPosition; $i <= $StopPage; $i++) {
|
|
|
|
if ($i != $StartPage) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= "<a href=\"$Location?page=$i$QueryString$Anchor\">";
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= '<strong>';
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($i * $ItemsPerPage > $TotalRecords) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= ((($i - 1) * $ItemsPerPage) + 1)."-$TotalRecords";
|
2012-10-11 08:00:15 +00:00
|
|
|
} else {
|
2013-04-17 08:00:58 +00:00
|
|
|
$Pages .= ((($i - 1) * $ItemsPerPage) + 1).'-'.($i * $ItemsPerPage);
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 08:00:58 +00:00
|
|
|
$Pages .= '</strong>';
|
2012-10-11 08:00:15 +00:00
|
|
|
if ($i != $StartPage) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= '</a>';
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
if ($i < $StopPage) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= ' | ';
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$Pages .= $StartPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($StartPage && $StartPage < $TotalPages) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Pages .= " | <a href=\"$Location?page=".($StartPage + 1).$QueryString.$Anchor.'" class="pager_next"><strong>Next ></strong></a> ';
|
|
|
|
$Pages .= "<a href=\"$Location?page=$TotalPages$QueryString$Anchor\"><strong> Last >></strong></a>";
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($TotalPages > 1) {
|
|
|
|
return $Pages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a size in bytes as a human readable string in KiB/MiB/...
|
2013-08-28 23:08:41 +00:00
|
|
|
* Note: KiB, MiB, etc. are the IEC units, which are in base 2.
|
|
|
|
* KB, MB are the SI units, which are in base 10.
|
2012-10-11 08:00:15 +00:00
|
|
|
*
|
|
|
|
* @param int $Size
|
|
|
|
* @param int $Levels Number of decimal places. Defaults to 2, unless the size >= 1TB, in which case it defaults to 4.
|
|
|
|
* @return string formatted number.
|
|
|
|
*/
|
|
|
|
public static function get_size($Size, $Levels = 2) {
|
2013-08-28 23:08:41 +00:00
|
|
|
$Units = array(' B', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB');
|
|
|
|
$Size = (double)$Size;
|
2013-04-21 08:00:52 +00:00
|
|
|
for ($Steps = 0; abs($Size) >= 1024; $Size /= 1024, $Steps++) {
|
|
|
|
}
|
2012-10-11 08:00:15 +00:00
|
|
|
if (func_num_args() == 1 && $Steps >= 4) {
|
|
|
|
$Levels++;
|
|
|
|
}
|
2012-10-27 08:00:09 +00:00
|
|
|
return number_format($Size, $Levels) . $Units[$Steps];
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-21 08:00:52 +00:00
|
|
|
* Format a number as a multiple of its highest power of 1000 (e.g. 10035 -> '10.04k')
|
2012-10-11 08:00:15 +00:00
|
|
|
*
|
|
|
|
* @param int $Number
|
|
|
|
* @return string formatted number.
|
|
|
|
*/
|
|
|
|
public static function human_format($Number) {
|
|
|
|
$Steps = 0;
|
2013-04-17 08:00:58 +00:00
|
|
|
while ($Number >= 1000) {
|
2012-10-11 08:00:15 +00:00
|
|
|
$Steps++;
|
2013-04-17 08:00:58 +00:00
|
|
|
$Number = $Number / 1000;
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
switch ($Steps) {
|
|
|
|
case 0: return round($Number); break;
|
2013-08-28 23:08:41 +00:00
|
|
|
case 1: return round($Number, 2).'k'; break;
|
|
|
|
case 2: return round($Number, 2).'M'; break;
|
|
|
|
case 3: return round($Number, 2).'G'; break;
|
|
|
|
case 4: return round($Number, 2).'T'; break;
|
|
|
|
case 5: return round($Number, 2).'P'; break;
|
2012-10-11 08:00:15 +00:00
|
|
|
default:
|
2013-08-28 23:08:41 +00:00
|
|
|
return round($Number, 2).'E + '.$Steps * 3;
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a formatted string of a size, get the number of bytes it represents.
|
|
|
|
*
|
2013-04-21 08:00:52 +00:00
|
|
|
* @param string $Size formatted size string, e.g. 123.45k
|
|
|
|
* @return Number of bytes it represents, e.g. (123.45 * 1024)
|
2012-10-11 08:00:15 +00:00
|
|
|
*/
|
|
|
|
public static function get_bytes($Size) {
|
2013-06-18 08:00:48 +00:00
|
|
|
list($Value, $Unit) = sscanf($Size, "%f%s");
|
2012-10-11 08:00:15 +00:00
|
|
|
$Unit = ltrim($Unit);
|
|
|
|
if (empty($Unit)) {
|
|
|
|
return $Value ? round($Value) : 0;
|
|
|
|
}
|
2013-04-21 08:00:52 +00:00
|
|
|
switch (strtolower($Unit[0])) {
|
2012-10-11 08:00:15 +00:00
|
|
|
case 'k': return round($Value * 1024);
|
|
|
|
case 'm': return round($Value * 1048576);
|
|
|
|
case 'g': return round($Value * 1073741824);
|
|
|
|
case 't': return round($Value * 1099511627776);
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse the effects of display_str - un-sanitize HTML.
|
|
|
|
* Use sparingly.
|
|
|
|
*
|
|
|
|
* @param string $Str the string to unsanitize
|
|
|
|
* @return unsanitized string
|
|
|
|
*/
|
|
|
|
// Use sparingly
|
|
|
|
public static function undisplay_str($Str) {
|
|
|
|
return mb_convert_encoding($Str, 'UTF-8', 'HTML-ENTITIES');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echo data sent in a GET form field, useful for text areas.
|
|
|
|
*
|
|
|
|
* @param string $Index the name of the form field
|
|
|
|
* @param boolean $Return if set to true, value is returned instead of echoed.
|
|
|
|
* @return Sanitized value of field index if $Return == true
|
|
|
|
*/
|
|
|
|
public static function form($Index, $Return = false) {
|
|
|
|
if (!empty($_GET[$Index])) {
|
|
|
|
if ($Return) {
|
|
|
|
return display_str($_GET[$Index]);
|
|
|
|
} else {
|
|
|
|
echo display_str($_GET[$Index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-02-23 08:00:22 +00:00
|
|
|
* Convenience function to echo out selected="selected" and checked="checked" so you don't have to.
|
2012-10-11 08:00:15 +00:00
|
|
|
*
|
|
|
|
* @param $Name the name of the option in the select (or field in $Array)
|
|
|
|
* @param $Value the value that the option must be for the option to be marked as selected or checked
|
2013-02-23 08:00:22 +00:00
|
|
|
* @param $Attribute The value returned/echoed is $Attribute="$Attribute" with a leading space
|
2012-10-11 08:00:15 +00:00
|
|
|
* @param $Array The array the option is in, defaults to GET.
|
|
|
|
* @return
|
|
|
|
*/
|
2013-04-17 08:00:58 +00:00
|
|
|
public static function selected($Name, $Value, $Attribute = 'selected', $Array = array()) {
|
2012-10-11 08:00:15 +00:00
|
|
|
if (empty($Array)) {
|
2012-10-12 08:00:19 +00:00
|
|
|
$Array = $_GET;
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
2013-04-17 08:00:58 +00:00
|
|
|
if (isset($Array[$Name]) && $Array[$Name] !== '') {
|
2012-10-11 08:00:15 +00:00
|
|
|
if ($Array[$Name] == $Value) {
|
2013-08-28 23:08:41 +00:00
|
|
|
echo " $Attribute=\"$Attribute\"";
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-18 08:00:19 +00:00
|
|
|
/**
|
2013-02-23 08:00:22 +00:00
|
|
|
* Return a CSS class name if certain conditions are met. Mainly useful to mark links as 'active'
|
2012-11-18 08:00:19 +00:00
|
|
|
*
|
|
|
|
* @param mixed $Target The variable to compare all values against
|
|
|
|
* @param mixed $Tests The condition values. Type and dimension determines test type
|
|
|
|
* Scalar: $Tests must be equal to $Target for a match
|
|
|
|
* Vector: All elements in $Tests must correspond to equal values in $Target
|
|
|
|
* 2-dimensional array: At least one array must be identical to $Target
|
|
|
|
* @param string $ClassName CSS class name to return
|
|
|
|
* @param bool $AddAttribute Whether to include the "class" attribute in the output
|
2013-08-28 23:08:41 +00:00
|
|
|
* @param string $UserIDKey Key in _REQUEST for a user ID parameter, which if given will be compared to G::$LoggedUser[ID]
|
2012-11-18 08:00:19 +00:00
|
|
|
*
|
|
|
|
* @return class name on match, otherwise an empty string
|
|
|
|
*/
|
|
|
|
public static function add_class($Target, $Tests, $ClassName, $AddAttribute, $UserIDKey = false) {
|
2013-08-28 23:08:41 +00:00
|
|
|
if ($UserIDKey && isset($_REQUEST[$UserIDKey]) && G::$LoggedUser['ID'] != $_REQUEST[$UserIDKey]) {
|
2012-11-18 08:00:19 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$Pass = true;
|
|
|
|
if (!is_array($Tests)) {
|
|
|
|
// Scalars are nice and easy
|
|
|
|
$Pass = $Tests === $Target;
|
|
|
|
} elseif (!is_array($Tests[0])) {
|
|
|
|
// Test all values in vectors
|
|
|
|
foreach ($Tests as $Type => $Part) {
|
|
|
|
if (!isset($Target[$Type]) || $Target[$Type] !== $Part) {
|
|
|
|
$Pass = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Loop to the end of the array or until we find a matching test
|
|
|
|
foreach ($Tests as $Test) {
|
|
|
|
$Pass = true;
|
|
|
|
// If $Pass remains true after this test, it's a match
|
|
|
|
foreach ($Test as $Type => $Part) {
|
|
|
|
if (!isset($Target[$Type]) || $Target[$Type] !== $Part) {
|
|
|
|
$Pass = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($Pass) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$Pass) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
if ($AddAttribute) {
|
2013-08-28 23:08:41 +00:00
|
|
|
return " class=\"$ClassName\"";
|
2012-11-18 08:00:19 +00:00
|
|
|
}
|
|
|
|
return " $ClassName";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-11 08:00:15 +00:00
|
|
|
/**
|
|
|
|
* Detect the encoding of a string and transform it to UTF-8.
|
|
|
|
*
|
|
|
|
* @param string $Str
|
|
|
|
* @return UTF-8 encoded version of $Str
|
|
|
|
*/
|
|
|
|
public static function make_utf8($Str) {
|
2013-04-17 08:00:58 +00:00
|
|
|
if ($Str != '') {
|
|
|
|
if (self::is_utf8($Str)) {
|
|
|
|
$Encoding = 'UTF-8';
|
|
|
|
}
|
|
|
|
if (empty($Encoding)) {
|
2013-06-18 08:00:48 +00:00
|
|
|
$Encoding = mb_detect_encoding($Str, 'UTF-8, ISO-8859-1');
|
2013-04-17 08:00:58 +00:00
|
|
|
}
|
|
|
|
if (empty($Encoding)) {
|
|
|
|
$Encoding = 'ISO-8859-1';
|
|
|
|
}
|
|
|
|
if ($Encoding == 'UTF-8') {
|
|
|
|
return $Str;
|
|
|
|
} else {
|
2013-06-18 08:00:48 +00:00
|
|
|
return @mb_convert_encoding($Str, 'UTF-8', $Encoding);
|
2013-04-17 08:00:58 +00:00
|
|
|
}
|
2012-10-11 08:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magical function.
|
|
|
|
*
|
|
|
|
* @param string $Str function to detect encoding on.
|
|
|
|
* @return true if the string is in UTF-8.
|
|
|
|
*/
|
|
|
|
public static function is_utf8($Str) {
|
|
|
|
return preg_match('%^(?:
|
2013-08-28 23:08:41 +00:00
|
|
|
[\x09\x0A\x0D\x20-\x7E] // ASCII
|
|
|
|
| [\xC2-\xDF][\x80-\xBF] // non-overlong 2-byte
|
|
|
|
| \xE0[\xA0-\xBF][\x80-\xBF] // excluding overlongs
|
|
|
|
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} // straight 3-byte
|
|
|
|
| \xED[\x80-\x9F][\x80-\xBF] // excluding surrogates
|
|
|
|
| \xF0[\x90-\xBF][\x80-\xBF]{2} // planes 1-3
|
|
|
|
| [\xF1-\xF3][\x80-\xBF]{3} // planes 4-15
|
|
|
|
| \xF4[\x80-\x8F][\x80-\xBF]{2} // plane 16
|
2012-10-11 08:00:15 +00:00
|
|
|
)*$%xs', $Str
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-01-03 08:00:30 +00:00
|
|
|
/**
|
|
|
|
* Modified accessor for the $TorrentLabels array
|
2013-02-22 08:00:24 +00:00
|
|
|
*
|
2013-01-03 08:00:30 +00:00
|
|
|
* Converts $text to lowercase and strips non-word characters
|
2013-02-22 08:00:24 +00:00
|
|
|
*
|
2013-01-03 08:00:30 +00:00
|
|
|
* @param string $text Search string
|
|
|
|
* @return string CSS class(es)
|
|
|
|
*/
|
2013-04-21 08:00:52 +00:00
|
|
|
public static function find_torrent_label_class ($text) {
|
2013-01-03 08:00:30 +00:00
|
|
|
$index = mb_eregi_replace('(?:[^\w\d\s]+)', '', strtolower($text));
|
2013-04-17 08:00:58 +00:00
|
|
|
if (isset(self::$TorrentLabels[$index])) {
|
|
|
|
return self::$TorrentLabels[$index];
|
|
|
|
} else {
|
|
|
|
return self::$TorrentLabels['default'];
|
|
|
|
}
|
2013-01-03 08:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a strong element that notes the torrent's state.
|
2013-04-21 08:00:52 +00:00
|
|
|
* E.g.: snatched/freeleech/neutral leech/reported
|
2013-01-03 08:00:30 +00:00
|
|
|
*
|
|
|
|
* The CSS class is infered using find_torrent_label_class($text)
|
|
|
|
*
|
|
|
|
* @param string $text Display text
|
|
|
|
* @param string $class Custom CSS class
|
|
|
|
* @return string Strong element
|
|
|
|
*/
|
2013-04-21 08:00:52 +00:00
|
|
|
public static function torrent_label ($text, $class = '') {
|
2013-04-17 08:00:58 +00:00
|
|
|
if (empty($class)) {
|
|
|
|
$class = self::find_torrent_label_class($text);
|
|
|
|
}
|
2013-10-25 08:00:59 +00:00
|
|
|
return sprintf('<strong class="torrent_label %1$s" title="%2$s" style="white-space: nowrap;">%2$s</strong>',
|
2013-01-03 08:00:30 +00:00
|
|
|
display_str($class), display_str($text));
|
|
|
|
}
|
2013-02-25 21:16:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a CSS class name from a Category ID
|
|
|
|
* @global array $Categories
|
|
|
|
* @param int|string $CategoryID This number will be subtracted by one
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-04-21 08:00:52 +00:00
|
|
|
public static function css_category ($CategoryID = 1) {
|
2013-02-25 21:16:55 +00:00
|
|
|
global $Categories;
|
|
|
|
return 'cats_' . strtolower(str_replace(array('-', ' '), '',
|
|
|
|
$Categories[$CategoryID - 1]));
|
|
|
|
}
|
2013-04-17 08:00:58 +00:00
|
|
|
}
|