- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
<?php
/**
* Provides URL shortening functionality, like tinyurl.com, bit.ly, ow.ly and other popular services.
* (c) 2011, it-in, http://it-in.ru
* @author Sergey Kovalev <[email protected]>
* @version 1.0
*/
/**
* Basic URL path, to which short code will be added.
*/
define("BASE_SHORT_PATH", "http://it-in.ru/~");
/**
* ID of the infoblock which holds information about shortned URLs.
*/
define("TINYURL_IBLOCK_ID", 11);
Class TinyURL
{
/**
* Converts decimal number to any base
* @param integer $num Your decimal integer
* @param integer $base Base to which you wish to convert $num (leave it 0 if you are providing $index or omit if you're using default (62))
* @param string $index If you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "zyxwvu")
* @return string
* @link http://www.php.net/manual/ru/function.base-convert.php#52450
*/
private static function dec2any( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
}
$out = "";
for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
/**
* Converts number in any base to decimal
* @param integer $num Your custom-based number (string) (ex.: "11011101")
* @param integer $base Base with which $num was encoded (leave it 0 if you are providing $index or omit if you're using default (62))
* @param string $index If you wish to use the default list of digits (0-1a-zA-Z), omit this option, otherwise provide a string (ex.: "abcdef")
* @return integer
* @link http://www.php.net/manual/ru/function.base-convert.php#52450
*/
private static function any2dec( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base );
}
$out = 0;
$len = strlen( $num ) - 1;
for ( $t = 0; $t <= $len; $t++ ) {
$out = $out + strpos( $index, substr( $num, $t, 1 ) ) * pow( $base, $len - $t );
}
return $out;
}
/**
* Shortens URL.
* @param string $url Absolute URL to be shortened, like http://www.yandex.ru.
* @return string
*/
public static function shorten($url)
{
CModule::IncludeModule("iblock") || die("Couldn't load one of the required modules. Error fe51e037.");
// Check if there is already shortened version of the required URL.
$res = CIBlockElement::GetList(
array(),
array('IBLOCK_ID' => TINYURL_IBLOCK_ID, 'PREVIEW_TEXT' => $url),
false,
false,
array('ID')
);
if($ob = $res->GetNextElement())
{
$arFields = $ob->GetFields();
return BASE_SHORT_PATH . self::dec2any($arFields['ID']);
}
// Shorten new URL and create a record in database.
$el = new CIBlockElement;
$ELEMENT_ID = $el->Add(array(
'IBLOCK_ID' => TINYURL_IBLOCK_ID,
'NAME' => $url,
'PREVIEW_TEXT' => $url,
'PREVIEW_TEXT_TYPE' => 'html',
));
if($ELEMENT_ID)
return BASE_SHORT_PATH . self::dec2any($ELEMENT_ID);
else
die($el->LAST_ERROR);
}
/**
* Converts short code to full URL, e.g. 8UdA -> http://yandex.ru.
* @param string $short_code
* @return string Full URL.
Продолжаем копаться в недрах гитхаба в поисках изумрудов от bitrix.
Данное творение некого адепта битрикса (из it-in, http://it-in.ru) для создания tinyurl