1. PHP / Говнокод #11436

    −52

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    require_once '/usr/share/php/smarty/Smarty.class.php';
    class SmartyMegaAPI extends Smarty{
        public function __construct()
        {
            $this->Smarty();
            $this->template_dir = './smarty/templates';
            $this->config_dir = './smarty/config';
            $this->compile_dir = './smarty/templates_c';
            $this->cache_dir = './smarty/cache';
        }
    }

    ООП такое ООП

    brainstorm, 18 Июля 2012

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

    +64

    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
    /**
       * Analyzes the supplied result to see if it was thrown
       * because the access token is no longer valid.  If that is
       * the case, then we destroy the session.
       *
       * @param $result array A record storing the error message returned
       *                      by a failed API call.
       */
      protected function throwAPIException($result) {
        $e = new FacebookApiException($result);
        switch ($e->getType()) {
          // OAuth 2.0 Draft 00 style
          case 'OAuthException':
            // OAuth 2.0 Draft 10 style
          case 'invalid_token':
            // REST server errors are just Exceptions
          case 'Exception':
            $message = $e->getMessage();
            if ((strpos($message, 'Error validating access token') !== false) ||
                (strpos($message, 'Invalid OAuth access token') !== false) ||
                (strpos($message, 'An active access token must be used') !== false)
            ) {
              $this->destroySession();
            }
            break;
        }
    
        throw $e;
      }

    Я даже не зняю, что хуже, определение типа исключения по тексту ошибки, или использование одного и того же исключения, для всех ситуаций.

    ЗЫ: Это SDK от facebook.

    korchasa, 17 Июля 2012

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

    +72

    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
    function valid_gender($str){
    	switch($str){
    		case "male":
    		return "male";
    		break;
    
    		case "female":
    		return "female";
    		break;
    
    		default:
    		return "male";
    		break;
    	}
    }

    Ультимативная проверка переменной на половую принадлежность. Also return'ам нынче доверия нет!

    swiftfooth, 16 Июля 2012

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

    +68

    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
    public function makeCategoryArray() {
            $a = array();
            for($i = 1; $i <=3 ; $i++) {
                $j = $i != 1 ? $i : '';
                $Method = "getCategoryid" . $j;
                $categoryid = $this->$Method();
                if($this->$Method()) {
                    try{
                        $category = BC::Get()->getCompanyService()->getCategoryByID($categoryid);
                        $a[] = array(
                            'name' => $category->getName(),
                            'url' => $category->makeURL()
                        );
                    } catch(Exception $e) {
                        return $a;
                    }
                }
            }
            return $a;
        }

    Эпичный вызов getCategoryid1(), getCategoryid2(), getCategoryid3() когда есть getField(fieldname)...

    max_wp, 13 Июля 2012

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

    −63

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    <?php
    
    global $KONKURS_ADMIN;
    $KONKURS_ADMIN=array('[email protected]',
                         '[email protected]',
                         '[email protected]');

    распределение привелегий в системе епта.

    brainstorm, 12 Июля 2012

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

    +54

    1. 1
    2. 2
    //Вычисляем предыдущий месяц
    $bdate = q1("SELECT '$cdate' - INTERVAL 1 MONTH");

    wds, 12 Июля 2012

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

    +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
    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
    <html>
    <head>
    <style>
       body{
        background-color: #3366CC; /* Цвет фона веб-страницы */
       }
    </style>
    </head>
    <body>
    <?php
    	/*error_reporting(E_ALL);*/
    	class Mirror{
    		public $sizeX;
    		public $sizeY;
    		public $filenameI;
    		public $filenameO;
    		public $red;
    		public $green;
    		public $blue;
    		public $alpha;
    		function __construct($f){
    			$this->filenameI=$f.".png";
    			$this->filenameO=$f."2.png";
    			$size=getimagesize($this->filenameI);
    			$this->sizeX=$size[0];
    			$this->sizeY=$size[1];
    		}
    		function saveImg(){
    			if(!file_exists($this->filenameO)){
    				$xx=$this->sizeX;
    				$yy=($this->sizeY)*2;
    				$im = imagecreatetruecolor($xx, $yy);
    				imageAlphaBlending($im, false);
    				imageSaveAlpha($im, true);
    				for($x=0;$x<$xx;$x++){
    					for($y=0;$y<$yy;$y++){
    						$r=$this->red[$x][$y];
    						$g=$this->green[$x][$y];
    						$b=$this->blue[$x][$y];
    						$alph=$this->alpha[$x][$y];
    						$color=ImageColorAllocateAlpha($im, $r, $g, $b, $alph);
    						imagesetpixel($im, $x, $y, $color);
    					}
    				}
    				imagepng($im, $this->filenameO);
    				imagedestroy($im);
    			}
    			echo "<img src=\"".$this->filenameI."\" /><hr>\n";
    			echo "<img src=\"".$this->filenameO."\" /><br>\n";
    		}
    		function loadImg(){
    			$im = imagecreatefrompng($this->filenameI);
    			for($x=0;$x<$this->sizeX;$x++){
    				for($y=0;$y<$this->sizeY;$y++){
    					$rgb = imagecolorat($im, $x, $y);
    					$color=imagecolorsforindex($im, $rgb);
    					$this->red[$x][$y]=$color['red'];
    					$this->green[$x][$y]=$color['green'];
    					$this->blue[$x][$y]=$color['blue'];
    					$this->alpha[$x][$y]=$color['alpha'];
    				}
    			}
    			imagedestroy($im);
    		}
    		function createMirror(){
    			if(!file_exists($this->filenameO)){
    				$yy=($this->sizeY*2);
    				for($x=0;$x<$this->sizeX;$x++){
    					$alph=0;
    					for($y=$this->sizeY;$y<$yy;$y++){
    						if($alph<126){
    							$alph++;
    						}
    						$this->red[$x][$y]=$this->red[$x][$this->sizeY-($y-$this->sizeY)-1];
    						$this->green[$x][$y]=$this->green[$x][$this->sizeY-($y-$this->sizeY)-1];
    						$this->blue[$x][$y]=$this->blue[$x][$this->sizeY-($y-$this->sizeY)-1];
    						$this->alpha[$x][$y]=$alph+$this->alpha[$x][$this->sizeY-($y-$this->sizeY)-1];
    						if($this->alpha[$x][$y]>127) $this->alpha[$x][$y]=127;
    					}
    				}
    			}
    		}
    	}
    	$filename="alpha";
    	$mirror=new Mirror($filename);
    	$mirror->loadImg();
    	$mirror->createMirror();
    	$mirror->saveImg();
    ?>
    </body>
    </html>

    Делал для одного сайта зеркальное отражение картинки (photoshop использовать не умеем-с). Почему-то сервер часто падал.
    Как оно работает, можно посмотреть здесь http://xcont.com/perc/newperceptron/cell/mirror.php

    xcont, 11 Июля 2012

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

    +48

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    function is_array_assoc($arr) {
        if (is_array($arr)) {
            foreach ($arr as $k => $v) {
                if (is_string($k) || (is_int($k) && $k < 0)) {
                    return 1;
                }
            }
            return 0;
        }
        return -1;
    }

    Взята с php.net проверка массива на ассоциативность.
    Типа array(5 => 4, 8=>9, 1 => 3) не ассоциативный?

    mkusher, 09 Июля 2012

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

    +59

    1. 1
    2. 2
    3. 3
    while (($i <= $additional_limit) && ($i < $available_spaces)) {
        $i++;
       }

    в недрах плагина к wordpress. Прям по-другому минимум двух чисел не найти

    mkusher, 08 Июля 2012

    Комментарии (30)
  10. PHP / Говнокод #11371

    +61

    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
    function myrnd(){
    	$rnd1=rand(48,57);
    	$rnd2=rand(65,90);
    	$rnd3=rand(97,122);
    	$rnd4=rand(1,3);
    	$rnd5="rnd".$rnd4;
    	return $$rnd5;
    }
    function createReffererLink(){
    	$refferer="";
    	for($i=0;$i<12;$i++){
    		$refferer.=chr($this->myrnd());
    	}
    	return $refferer;
    }

    Нашел в своей старой партнерской программе. Угадайте, что делает этот код? ( генерирует реферальную ссылку )

    xcont, 07 Июля 2012

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