"PHP" / Говнокод #19926 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 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 <kovalev_s@it-in.ru>
 * @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

Запостил: Keeper Keeper, (Updated )

Комментарии (10) RSS

  • При чем тут "от bitrix"? Изумруды то не его авторов, а его пользователей.
    Ответить
    • Какой движок такие и пользователи. Можешь поискать перлы от самого битрикса.
      Ответить
      • Ну просто с тем же успехом можно назвать это изумрудами от PHP. Какой язык, такие и пользователи.
        Ответить
        • Тебя цепляет, что я открыто говорю как есть что битрикс говно, а его адепты генераторы говнокода?
          Ответить
          • Друзья, не ругайтесь! Вы оба правы. И битрикс говно, и пхп говно
            Ответить
              • Как будто кроме нас больше некому их генерировать. 😀 Вон там M-A-X с своими шлюшными лисопедами на пасьянсе вылез.
                Ответить

Добавить комментарий

Из-за тебя ушел bormand, guest!

    А не использовать ли нам bbcode?


    8