1. JavaScript / Говнокод #11526

    +148

    1. 1
    2. 2
    3. 3
    4. 4
    $("#select_id :selected").attr("selected", false);
    $("#select_id option[value='" + new_value + "']").attr("selected", true);
    вместо
    $("#select_id").val(new_value);

    splinter89, 03 Августа 2012

    Комментарии (4)
  2. PHP / Говнокод #11525

    +56

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    $cur_url=$_SERVER['REQUEST_URI'];
    if ($cur_url=='/') { 
    	$cur_url=$_SERVER['REQUEST_URI']; 
    	if ($cur_url=='/') { // Точно-точно адрес не равен слэшу
    		echo '';// После всех проверок можно с уверенностью вывести пустую строку
    	}
    }
    $cur_url2=$_SERVER['REQUEST_URI'];// И ещё разок
    if ($cur_url2!='/') { 
    	echo '';
    }

    Из движка одного московского портала, директор которого пытается нас убедить что там нормальный код

    RedMonkey, 03 Августа 2012

    Комментарии (3)
  3. C# / Говнокод #11524

    +140

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    FileInfo file = new FileInfo(fileName);
    FileSecurity fSecurity = File.GetAccessControl(fileName);
    
    foreach (FileSystemAccessRule permissions in fSecurity.GetAccessRules(true, true, typeof(NTAccount)))
    {
         string login = permissions.IdentityReference.Translate(typeof(NTAccount)).Value.ToString();
         string permiss =  permissions.FileSystemRights.ToString();
    
         if ((login != "логин") && (login != "логин") && (login != "логин"))
         {
             fSecurity.SetAccessRuleProtection(true, false);
             fSecurity.RemoveAccessRule(new FileSystemAccessRule(login, permissions.FileSystemRights, AccessControlType.Allow));
         }
         fSecurity.AddAccessRule(new FileSystemAccessRule("логин", FileSystemRights.FullControl, AccessControlType.Allow));
         fSecurity.AddAccessRule(new FileSystemAccessRule("логин", FileSystemRights.FullControl, AccessControlType.Allow));
    
    }
    File.SetAccessControl(fileName, fSecurity);

    vertu, 03 Августа 2012

    Комментарии (11)
  4. Java / Говнокод #11523

    +90

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    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
    public enum TimeUnit {
       NANOSECONDS {
            public long toNanos(long d)   { return d; }
            public long toMicros(long d)  { return d/(C1/C0); }
            public long toMillis(long d)  { return d/(C2/C0); }
            public long toSeconds(long d) { return d/(C3/C0); }
            public long toMinutes(long d) { return d/(C4/C0); }
            public long toHours(long d)   { return d/(C5/C0); }
            public long toDays(long d)    { return d/(C6/C0); }
            public long convert(long d, TimeUnit u) { return u.toNanos(d); }
            int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
        }
    ....
        public long convert(long sourceDuration, TimeUnit sourceUnit) {
            throw new AbstractMethodError();
        }
    
     
        public long toNanos(long duration) {
            throw new AbstractMethodError();
        }
    
      
        public long toMicros(long duration) {
            throw new AbstractMethodError();
        }
    
        public long toMillis(long duration) {
            throw new AbstractMethodError();
        }
    
     
        public long toSeconds(long duration) {
            throw new AbstractMethodError();
        }
    
      
        public long toMinutes(long duration) {
            throw new AbstractMethodError();
        }
    
      
        public long toHours(long duration) {
            throw new AbstractMethodError();
        }
    
     
        public long toDays(long duration) {
            throw new AbstractMethodError();
        }
    
      
        abstract int excessNanos(long d, long m);
    }

    Но зачем?

    3.14159265, 02 Августа 2012

    Комментарии (61)
  5. ActionScript / Говнокод #11522

    −92

    1. 1
    2. 2
    3. 3
    public function xor(lhs:Boolean, rhs:Boolean):Boolean {
     return !( lhs && rhs ) && ( lhs || rhs );
    }

    Из http://as3snippets.blogspot.com/2010/09/logical-xor.html

    Как известно, в AS3 есть численный оператор XOR ^, а вот для логических значений ^^ нет. Поэтому ребята придумали такую конструкцию (и ещё вариант return Boolean(int(a) ^ int(b)); в комментах), и только через год какой-то чувак догадался что XOR для логических значений всё-таки есть и называется !=

    makc3d, 02 Августа 2012

    Комментарии (26)
  6. C# / Говнокод #11521

    +141

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    foreach (var item in text.Split(' ')) {
    	if (!string.IsNullOrEmpty(item)) {
    		text = item;
    		break;
    	}
    }

    переменная text всегда содержит несколько пробелов и затем идентификатор.

    paladin80, 02 Августа 2012

    Комментарии (64)
  7. C++ / Говнокод #11520

    +20

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    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
    class TCP1251ToUTF16StringConverter
    {
    public:
    	static WideChar convert(Char Source)
    	{
    		WideChar Result=static_cast<unsigned char>(Source);
    		const WideChar Russian_YO=static_cast<unsigned char>('Ё');
    		const WideChar Russian_yo=static_cast<unsigned char>('ё');
    		const WideChar RussianWide_YO=L'Ё';
    		const WideChar RussianWide_yo=L'ё';
    		const WideChar Russian_A=static_cast<unsigned char>('А');
    		const WideChar RussianWide_A=L'А';
    		const unsigned int AmountOfSymbols=0x40;
    		if(Result==Russian_YO)
    			return RussianWide_YO;
    		if(Result==Russian_yo)
    			return RussianWide_yo;
    		if(Result>=Russian_A&&Result<Russian_A+AmountOfSymbols)
    			return (Result-Russian_A+RussianWide_A);
    		return Result;
    	};
    	
    	static void convert(PwideChar UTF16StringDestination, PChar CP1251WinEngRusStringSource, const size_t TextLength)
    	{
    		assert(CP1251WinEngRusStringSource!=NULL);
    		size_t i=0;
    		for(;;)
    		{
    			if(i>=TextLength)
    				break;
    			assert(i<TextLength);
    			Char CP1251SourceChar=CP1251WinEngRusStringSource[i];
    			if(CP1251SourceChar=='\0')
    				break;
    			UTF16StringDestination[i]=convert(CP1251SourceChar);
    			++i;			
    		};
    		UTF16StringDestination[i]=L'\0';
    		assert(i<=TextLength);
    	};
    	
    	static std::wstring convert(const std::string& CP1251WinEngRusStringSource)
    	{
    		assert(CP1251WinEngRusStringSource.c_str()!=NULL);
    		std::wstring UTF16StringDestination;
    		std::transform(CP1251WinEngRusStringSource.begin(), CP1251WinEngRusStringSource.end(), std::inserter(UTF16StringDestination, UTF16StringDestination.end())/*std::back_inserter(UTF16StringDestination)*//*VC 6.0 compatible*/, makePointerToFunction(convertChar));
    		return UTF16StringDestination;
    	};
    	
    private:
    	static WideChar convertChar(char Source)
    	{
    		return convert(Source);
    	};
    };
    
    template<const size_t MaxAmountOfChar>
    class TCP1251ToUTF16StringInPlaceConverter
    {
    public:
    	TCP1251ToUTF16StringInPlaceConverter(PChar CP1251WinEngRusStringSource)
    	{
    		STATIC_ASSERT(MaxAmountOfChar>0, MaxAmountOfChar_must_be_above_zero);
    		TCP1251ToUTF16StringConverter::convert(&(_buffer[0]), CP1251WinEngRusStringSource, MaxAmountOfChar);
    	};
    	
    	TCP1251ToUTF16StringInPlaceConverter(PChar CP1251WinEngRusStringSource, const size_t TextLength)
    	{
    		STATIC_ASSERT(MaxAmountOfChar>0, MaxAmountOfChar_must_be_above_zero);
    		assert(TextLength<=MaxAmountOfChar);
    		TCP1251ToUTF16StringConverter::convert(&(_buffer[0]), CP1251WinEngRusStringSource, TextLength);
    	};
    	
    	void convert(PChar CP1251WinEngRusStringSource)
    	{
    		TCP1251ToUTF16StringConverter::convert(&(_buffer[0]), CP1251WinEngRusStringSource, MaxAmountOfChar);
    	};
    	
    	void convert(PChar CP1251WinEngRusStringSource, const size_t TextLength)
    	{
    		assert(TextLength<=MaxAmountOfChar);
    		TCP1251ToUTF16StringConverter::convert(&(_buffer[0]), CP1251WinEngRusStringSource, TextLength);
    	};
    	
    	PWideChar Get(void) const
    	{
    		return &(_buffer[0]);
    	};
    	
    	PwideChar Get(void)
    	{
    		return &(_buffer[0]);
    	};
    	
    	wideChar _buffer[MaxAmountOfChar+1];
    };

    USB, 02 Августа 2012

    Комментарии (43)
  8. PHP / Говнокод #11519

    +65

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    function draw_text() {  
            // ....
            /* remove background color */
            imagecolortransparent($im_text, $bg_color);
            return $im_text;
            imagedestroy($im_text);
    }

    Функция вывода текста CAPTCHA в modx Evolution.

    MaXL, 02 Августа 2012

    Комментарии (3)
  9. PHP / Говнокод #11518

    +56

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    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
    if(isset($_POST['btnsubmitup']))
    		{
    			for ($i = "0"; Arr::get($_POST, 'id'.$i, ''); $i++) {
    			if (Arr::get($_POST, 'up'.$i, '') == '1') {
    				$p1=-1;
    	//			$uploaddir = '/img/brands/';
    				$a = Arr::get($_POST, 'id'.$i, '');
    	//			$p1 = Upload::save($_FILES['photo'.$i], $uploaddir.$a.'.jpg', './', 0777);
    
    					$rand=rand(1000000,9999999);
    				$uploaddir = '/img_carpets/collection/';
    				$uploaddir2 = 'img_carpets/collection/';
    				$p1 = Upload::save($_FILES['file1'.$i], $uploaddir.'ID-'.$rand.'-1.jpg', './', 0777);
    				$p2 = Upload::save($_FILES['file2'.$i], $uploaddir.'ID-'.$rand.'-2.jpg', './', 0777);
    				$p3 = Upload::save($_FILES['file3'.$i], $uploaddir.'ID-'.$rand.'-3.jpg', './', 0777);
    				$p4 = Upload::save($_FILES['file4'.$i], $uploaddir.'ID-'.$rand.'-4.jpg', './', 0777);
    	//			if ($p1!="0") { $p1=$rand; }
    	//			if ($p2!="0") { $p2=$rand; }
    	//			if ($p3!="0") { $p3=$rand; }
    	//			if ($p4!="0") { $p4=$rand; }
    					$im2=Image::factory($uploaddir2.'back.png');
    
    // -> и так далее

    Начал разбирать библиотеку (фреймворк скорее - kohanaframework) одного сайта, дабы сделать нормальную админку
    Дошел до процедуры сохранения картинок. Я посмотрел, по какому же алгоритму сохраняются картинки (формирование имени файла)
    И опупел!
    ** $rand=rand(1000000,9999999); **
    В базе поле для сохранения имени картинки - не уникально.
    Т.е., разраб решил поиграть в рулетку, анука генератор чисел выберет еще раз одно и то же число, и перезапишет картинку у товара. ))))
    А оператор админки будет чесать репу - тут же работало а тут и нет )

    topilnik, 01 Августа 2012

    Комментарии (38)
  10. C++ / Говнокод #11517

    +15

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    //file systeminfo.cpp
    #include "../SystemInfoManager.h"
    struct : public SystemInfoManager
    {
    //some atriburtes
    //some methods
       void setSomeparametrs()///
    {
    /*....*/
    }
    ///ect...
    
    } System;
    
    
    SystemInfoManager * instance()
    {
           return &System;
    }

    Мое имя затрет история, но мои славные дела будут жить, пытайте меня дальше гниды из гестапо!

    Psionic, 01 Августа 2012

    Комментарии (2)