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

    +157

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    //@see http://ru2.php.net/manual/en/function.exif-imagetype.php
    if (!function_exists('exif_imagetype')) {
        function exif_imagetype($filename) {
            if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
                if ($type > 0) {
                    return $type;
                }
            }
            return false;
        }
    }

    Скопипастил этот код к себе в проект из мануала с php.net для определения типа картинки, т.к на сервере не было экстеншина Exif.
    Много лишних переменных, но в ломак было переписывать, запузырил как есть.

    govnomes, 12 Февраля 2011

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

    +162

    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
    if(strLen($arParams["BLOG_VAR"])<=0)
    	$arParams["BLOG_VAR"] = "blog";
    if(strLen($arParams["PAGE_VAR"])<=0)
    	$arParams["PAGE_VAR"] = "page";
    if(strLen($arParams["USER_VAR"])<=0)
    	$arParams["USER_VAR"] = "id";
    if(strLen($arParams["POST_VAR"])<=0)
    	$arParams["POST_VAR"] = "id";
    	
    $arParams["PATH_TO_BLOG"] = trim($arParams["PATH_TO_BLOG"]);
    if(strlen($arParams["PATH_TO_BLOG"])<=0)
    	$arParams["PATH_TO_BLOG"] = htmlspecialchars($APPLICATION->GetCurPage()."?".$arParams["PAGE_VAR"]."=blog&".$arParams["BLOG_VAR"]."=#blog#");
    
    $arParams["PATH_TO_BLOG_CATEGORY"] = trim($arParams["PATH_TO_BLOG_CATEGORY"]);
    if(strlen($arParams["PATH_TO_BLOG_CATEGORY"])<=0)
    	$arParams["PATH_TO_BLOG_CATEGORY"] = htmlspecialchars($APPLICATION->GetCurPage()."?".$arParams["PAGE_VAR"]."=blog&".$arParams["BLOG_VAR"]."=#blog#"."&category=#category_id#");
    	
    $arParams["PATH_TO_POST_EDIT"] = trim($arParams["PATH_TO_POST_EDIT"]);
    if(strlen($arParams["PATH_TO_POST_EDIT"])<=0)
    	$arParams["PATH_TO_POST_EDIT"] = htmlspecialchars($APPLICATION->GetCurPage()."?".$arParams["PAGE_VAR"]."=post_edit&".$arParams["BLOG_VAR"]."=#blog#&".$arParams["POST_VAR"]."=#post_id#");
    
    $arParams["PATH_TO_USER"] = trim($arParams["PATH_TO_USER"]);
    if(strlen($arParams["PATH_TO_USER"])<=0)
    	$arParams["PATH_TO_USER"] = htmlspecialchars($APPLICATION->GetCurPage()."?".$arParams["PAGE_VAR"]."=user&".$arParams["USER_VAR"]."=#user_id#");

    Из проекта на битрикс, обратите внимание на импровизацию в вариантах написания функции strlen D)))
    Код успешно работает в продакшене более года.

    govnomes, 11 Февраля 2011

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

    +152

    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
    $mime_types = array('jad'  => 'text/vnd.sun.j2me.app-descriptor',
                            'jar'  => 'application/java-archive',
                            'txt'  => 'text/plain',
                            'sis'  => 'application/vnd.symbian.install',
                            'cab'  => 'application/vnd.ms-cab-compressed',
                            'zip'  => 'application/x-zip', 
                            'gz'   => 'application/x-gzip',
                            'tgz'  => 'application/x-gzip',
                            'bz'   => 'application/x-bzip', 
                            'bz2'  => 'application/x-bzip',
                            '7z'   => 'application/x-7z-compressed',
                            'rar'  => 'application/x-rar-compressed',
                            'doc'  => 'application/msword',
                            'pdf'  => 'application/pdf', 
                            'mp3'  => 'audio/mpeg', 
                            'wav'  => 'audio/x-wav',
                            'wma'  => 'audio/x-ms-wma',
                            'avi'  => 'video/x-msvideo',
                            '3gp'  => 'video/3gpp', 
                            'wmv'  => 'video/x-ms-wmv', 
                            'mpg'  => 'video/mpeg', 
                            'gif'  => 'image/gif', 
                            'jpg'  => 'image/jpeg',
                            'jpe'  => 'image/jpeg', 
                            'jpeg' => 'image/jpeg',
    	           );
    
    	$mime_type = (array_key_exist(pathinfo($filepath, PATHINFO_EXTENSION), $mime_types)) ? $mime_types[pathinfo($filepath, PATHINFO_EXTENSION)] : 'application/octet-stream';
    		
    	header('Content-Type: ' . $mime_type . ';');
                  header('content-disposition: attachment; filename="' . basename($filepath) . '";');
                  readfile($download);

    Использую сие для определения Content-Type перед отдачей файла для загрузки.
    И тут меня орашарашили тем, что прямо в лицо сказали, что сие - говнокод, а я - говнокодер всея Руси.
    Что такие дела делаются функциями и вообще что за говно, тут можно без массива.
    Неужто?

    7ion, 11 Февраля 2011

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

    +159

    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
    <?php
    	if($showCapcha != 'showCapcha_0') {
    		echo 'Решите пример: ';
    		$secretCode_1 = rand(1 , 9);
    		$secretCode_2 = rand(1 , 9);
    		$secretCode = $secretCode_1 + $secretCode_2;
    		echo "<strong>" . $secretCode_1 . "</strong> + <strong>" . $secretCode_2 . "</strong> = ";
    		echo '
    			<input id="button_form_contact" type="text" name="secretCode" value="" />
    			<input type="hidden" name="secretCodeHide" value="'.$secretCode.'" />
    		';
    	}
    ?>
    <input id="button_form_contact" type="submit" value=" <?php echo MOD_CON_AM_GO_GO__; ?> ">

    Всё хватит придумывать капчи! Всё уже придумано xDDDD

    nethak, 11 Февраля 2011

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

    +163

    1. 1
    2. 2
    3. 3
    function isInt($val) {
        return is_int($val) || (string)(int)$val === (string)$val;
    }

    Проверка на целое число

    govnomes, 11 Февраля 2011

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

    +156

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    # Преоброзование расширения в MIME тип
    function replace_type_in_mime($type) {
    	$type = strtolower($type);
    	$res = array("323" => "text/h323", "acx" => "application/internet-property-stream", "ai" => "application/postscript", "aif" => "audio/x-aiff", "aifc" => "audio/x-aiff", "aiff" => "audio/x-aiff", "asf" => "video/x-ms-asf", "asr" => "video/x-ms-asf", "asx" => "video/x-ms-asf", "au" => "audio/basic", "avi" => "video/x-msvideo", "axs" => "application/olescript", "bas" => "text/plain", "bcpio" => "application/x-bcpio", "bin" => "application/octet-stream", "bmp" => "image/bmp", "c" => "text/plain", "cat" => "application/vnd.ms-pkiseccat", "cdf" => "application/x-cdf", "cer" => "application/x-x509-ca-cert", "class" => "application/octet-stream", "clp" => "application/x-msclip", "cmx" => "image/x-cmx", "cod" => "image/cis-cod", "cpio" => "application/x-cpio", "crd" => "application/x-mscardfile", "crl" => "application/pkix-crl", "crt" => "application/x-x509-ca-cert", "csh" => "application/x-csh", "css" => "text/css", "dcr" => "application/x-director", "der" => "application/x-x509-ca-cert", "dir" => "application/x-director", "dll" => "application/x-msdownload", "dms" => "application/octet-stream", "doc" => "application/msword", "dot" => "application/msword", "dvi" => "application/x-dvi", "dxr" => "application/x-director", "eps" => "application/postscript", "etx" => "text/x-setext", "evy" => "application/envoy", "exe" => "application/octet-stream", "fif" => "application/fractals", "flr" => "x-world/x-vrml", "gif" => "image/gif", "gtar" => "application/x-gtar", "gz" => "application/x-gzip", "h" => "text/plain", "hdf" => "application/x-hdf", "hlp" => "application/winhlp", "hqx" => "application/mac-binhex40", "hta" => "application/hta", "htc" => "text/x-component", "htm" => "text/html", "html" => "text/html", "htt" => "text/webviewhtml", "ico" => "image/x-icon", "ief" => "image/ief", "iii" => "application/x-iphone", "ins" => "application/x-internet-signup", "isp" => "application/x-internet-signup", "jfif" => "image/pipeg", "jpe" => "image/jpeg", "jpeg" => "image/jpeg", "jpg" => "image/jpeg", "js" => "application/x-javascript", "latex" => "application/x-latex", "lha" => "application/octet-stream", "lsf" => "video/x-la-asf", "lsx" => "video/x-la-asf", "lzh" => "application/octet-stream", "m13" => "application/x-msmediaview", "m14" => "application/x-msmediaview", "m3u" => "audio/x-mpegurl", "man" => "application/x-troff-man", "mdb" => "application/x-msaccess", "me" => "application/x-troff-me", "mht" => "message/rfc822", "mhtml" => "message/rfc822", "mid" => "audio/mid", "mny" => "application/x-msmoney", "mov" => "video/quicktime", "movie" => "video/x-sgi-movie", "mp2" => "video/mpeg", "mp3" => "audio/mpeg", "mpa" => "video/mpeg", "mpe" => "video/mpeg", "mpeg" => "video/mpeg", "mpg" => "video/mpeg", "mpp" => "application/vnd.ms-project", "mpv2" => "video/mpeg", "ms" => "application/x-troff-ms", "mvb" => "application/x-msmediaview", "nws" => "message/rfc822", "oda" => "application/oda", "p10" => "application/pkcs10", "p12" => "application/x-pkcs12", "p7b" => "application/x-pkcs7-certificates", "p7c" => "application/x-pkcs7-mime", "p7m" => "application/x-pkcs7-mime", "p7r" => "application/x-pkcs7-certreqresp", "p7s" => "application/x-pkcs7-signature", "pbm" => "image/x-portable-bitmap", "pdf" => "application/pdf", "pfx" => "application/x-pkcs12", "pgm" => "image/x-portable-graymap", "pko" => "application/ynd.ms-pkipko", "pma" => "application/x-perfmon", "pmc" => "application/x-perfmon", "pml" => "application/x-perfmon", "pmr" => "application/x-perfmon", "pmw" => "application/x-perfmon", "pnm" => "image/x-portable-anymap");
    	if($ret == null) {
    		$ret = "application/x-unknown-content-type";
    	} else {
    		$ret = $res[$type];
    	}
    	return $ret;
    }

    Не знаю смешно ли это или нет, но разобраться в одной километровой строке будет ооочень интересно))))
    Я вас огорчу, пришлось урезать на несколько тысяч символов эту дистанцию))

    nethak, 11 Февраля 2011

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

    +157

    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
    function getAvailablePrivileges(Workset_Model_Object_Interface $resource, $where = null, $order = null, $limit = null, $offset = null, $offsetByPage = false) {
    
            if (true == $offsetByPage) {
                $offset = $this->getPageOffset($limit, $offset);
            }
    
    	$table = $this->_getTable();
    	$select = $table->prepareSelect($where, $order, $limit, $offset);
    
            $iselect = $this->_getTable()->select();
    
            $iselect->from(
                array('m' => $this->_getTable()->info(Zend_Db_Table_Abstract::NAME)),
                array('id')
            )
           ->join(
                array('i' => $this->_getTable($this->_linkedObjectTableClass)->info(Zend_Db_Table_Abstract::NAME)),
                "i.privilege_id = m.id",
                array()
            )
            ->where(
                'i.resource_id = ?', $resource->getId()
            );
    
            $select->where("id not in(?)", new Zend_Db_Expr($iselect->assemble()));
    
            $rowset = $table->fetchAll($select);
    
            return $this->getIterator($rowset, array(
                'countRows' => $this->_countAllRecords,
                'filter' => $select
            ));
    
        }

    Из проекта на Zend

    govnomes, 11 Февраля 2011

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

    +157

    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
    if($amount > 0) :
    	if (is_dir($directory)) {
    		if ($open_dir = opendir($directory)) {
    			$a = 0; $b = 0;
    			while (false !== ($file = readdir($open_dir))) {
    				if ($file != "." && $file != ".." && !is_dir($directory."/".$file)) {
    					$a++;
    					if($a > $min) {
    						$b++;
    						echo "<tr id=\"am-list-input\" valign=\"center\" height: 20px;><td align=\"left\" width=\"10%\">";
    						echo " <a href=\"index.php?am=mod[uploader]&delete=$file\"><img src=\"../images/mini_icons/del.png\" /></a> ";
    						if(in_array(strtolower(getExtension($file)), $gud_types)) echo "<span OnClick=\"CaricaFoto('".$directory."/".$file."')\" OnMouseOver=\"Tip('<img style="max-width: 400px" src="".$directory."/".$file."" >')\"><img style=\"cursor: help\" src=\"../images/mini_icons/display.png\" /></span>";
    						if(in_array(strtolower(getExtension($file)), explode(',', strtolower("mp3,wma,waw,amr,ape,bin,flac,m4a,mdi,ram")))) echo "<img src=\"modules/mod[uploader]/assets/type/sound.png\" />";
    						if(in_array(strtolower(getExtension($file)), explode(',', strtolower("3gp,avi,dat,flv,ifo,m4v,mkv,mov,mp4,rm,vob,wmv")))) echo "<img src=\"modules/mod[uploader]/assets/type/video.png\" />";
    						if(!in_array(strtolower(getExtension($file)), explode(',', strtolower("3gp,avi,dat,flv,ifo,m4v,mkv,mov,mp4,rm,vob,wmv,mp3,wma,waw,amr,ape,bin,flac,m4a,mdi,ram,jpg,jpeg,png,gif")))) echo "<img src=\"modules/mod[uploader]/assets/type/all.png\" />";
    						echo "</td><td width=\"70%\"><span style=\"display: block; cursor: pointer; color: #446eb8; font-weight: bold; line-height: 16px;\" onClick=\"document.getElementById('onclick').value='".str_replace("..", $conf_global["base_url"], $directory)."/".$file."'\" />".substr($file, 0, 100)."</span></td><td width=\"20%\">".formatsize(filesize($directory.DS.$file))."</td></tr>";
    						if($b == $max) : break; endif;
    					}
    				}
    			}
    		closedir($open_dir);
    		}
    	}
    endif;

    Грех не посмеятся над своими старыми проектами =)))

    nethak, 11 Февраля 2011

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

    +172

    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
    /* Теперь задаём сами функции перекодировки translita в кириллицу и обратно. Вот код: */
    
    function ruslat ($string) # Задаём функцию перекодировки кириллицы в транслит.
    {
    $string = ereg_replace("ж","zh",$string);
    $string = ereg_replace("ё","yo",$string);
    $string = ereg_replace("й","i",$string);
    $string = ereg_replace("ю","yu",$string);
    $string = ereg_replace("ь","'",$string);
    $string = ereg_replace("ч","ch",$string);
    $string = ereg_replace("щ","sh",$string);
    $string = ereg_replace("ц","c",$string);
    $string = ereg_replace("у","u",$string);
    $string = ereg_replace("к","k",$string);
    $string = ereg_replace("е","e",$string);
    $string = ereg_replace("н","n",$string);
    $string = ereg_replace("г","g",$string);
    $string = ereg_replace("ш","sh",$string);
    $string = ereg_replace("з","z",$string);
    $string = ereg_replace("х","h",$string);
    $string = ereg_replace("ъ","''",$string);
    $string = ereg_replace("ф","f",$string);
    $string = ereg_replace("ы","y",$string);
    $string = ereg_replace("в","v",$string);
    $string = ereg_replace("а","a",$string);
    $string = ereg_replace("п","p",$string);
    $string = ereg_replace("р","r",$string);
    $string = ereg_replace("о","o",$string);
    $string = ereg_replace("л","l",$string);
    $string = ereg_replace("д","d",$string);
    $string = ereg_replace("э","yе",$string);
    $string = ereg_replace("я","jа",$string);
    $string = ereg_replace("с","s",$string);
    $string = ereg_replace("м","m",$string);
    $string = ereg_replace("и","i",$string);
    $string = ereg_replace("т","t",$string);
    $string = ereg_replace("б","b",$string);
    $string = ereg_replace("Ё","yo",$string);
    $string = ereg_replace("Й","I",$string);
    $string = ereg_replace("Ю","YU",$string);
    $string = ereg_replace("Ч","CH",$string);
    $string = ereg_replace("Ь","'",$string);
    $string = ereg_replace("Щ","SH'",$string);
    $string = ereg_replace("Ц","C",$string);
    $string = ereg_replace("У","U",$string);
    $string = ereg_replace("К","K",$string);
    $string = ereg_replace("Е","E",$string);
    $string = ereg_replace("Н","N",$string);
    $string = ereg_replace("Г","G",$string);
    $string = ereg_replace("Ш","SH",$string);
    $string = ereg_replace("З","Z",$string);
    $string = ereg_replace("Х","H",$string);
    $string = ereg_replace("Ъ","''",$string);
    $string = ereg_replace("Ф","F",$string);
    $string = ereg_replace("Ы","Y",$string);
    $string = ereg_replace("В","V",$string);
    $string = ereg_replace("А","A",$string);
    $string = ereg_replace("П","P",$string);
    $string = ereg_replace("Р","R",$string);
    $string = ereg_replace("О","O",$string);
    $string = ereg_replace("Л","L",$string);
    $string = ereg_replace("Д","D",$string);
    $string = ereg_replace("Ж","Zh",$string);
    $string = ereg_replace("Э","Ye",$string);
    $string = ereg_replace("Я","Ja",$string);
    $string = ereg_replace("С","S",$string);
    $string = ereg_replace("М","M",$string);
    $string = ereg_replace("И","I",$string);
    $string = ereg_replace("Т","T",$string);
    $string = ereg_replace("Б","B",$string);
    return $string;
    }

    Нашел сие чудо на: http://protoplex.ru/lib/?showid=89

    code_master, 10 Февраля 2011

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

    +166

    1. 1
    2. 2
    $pif=explode('?',$_SERVER['REQUEST_URI']);
    $tym=explode('=',$pif[1]);

    Вот такой вот способ получить значение переменной из GET запроса вида:
    http://127.0.0.1/component/index.php?name=r37.edoc

    isergey, 10 Февраля 2011

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