<?php
/*
*Fnc Class
*Oğuz Koç
*12 aralık 2010
*/
class Fnc
{
	
	/*
    * Sayfanın tam adresini çıktı olarak verir (protokol, site adresi, port ve bulunan sayfa)
    * Örnek: http://siteadresi.com:1234/anasayfa.php (port 80 ise gösterilmez)
    */
    public static function tamurl()
    {
        $s = empty($_SERVER['HTTPS']) ? '' : ($_SERVER['HTTPS'] == 'on') ? 's' : '';
        $protocol = substr(strtolower($_SERVER['SERVER_PROTOCOL']), 0, strpos(strtolower($_SERVER['SERVER_PROTOCOL']), '/')) . $s;
        $port = ($_SERVER['SERVER_PORT'] == '80') ? '' : (":".$_SERVER['SERVER_PORT']);
        return $protocol . "://" . $_SERVER['HTTP_HOST'] . $port . $_SERVER['REQUEST_URI'];
    }

    /*
    * İstenilen JS kütüphanesini yükler
    * Varolan kütüphaneler: jquery, jqueryui, swfobject, mootools, scriptaculous, dojo
    * Örnekler:
    * jskutuphane('jquery', 'src'); Çıktısı: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    * jskutuphane('jquery'); Çıktısı: http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js
    */
    public static function jskutuphane($tur = null, $neyapalim = null) 
    {
		if($tur !== '') {
		if($tur == 'jquery') { $ne = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js';
		}elseif($tur == 'jqueryui') { $ne = 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js';
	    }elseif($tur == 'swfobject') { $ne = 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js';
	    }elseif($tur == 'mootools') { $ne = 'http://ajax.googleapis.com/ajax/libs/mootools/1.3.0/mootools-yui-compressed.js';
	    }elseif($tur == 'scriptaculous') { $ne = 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js';
	    }elseif($tur == 'dojo') { $ne = 'http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js'; }
	    if($neyapalim == 'src') { $ne = '<script type="text/javascript" src="'.$ne.'"></script>'; }
	    return $ne;
	    }else{
		return false;
    	}
	}
	
	
	/*
    * Veriyi temizler
    * Örnek: supur('<script>alert("xss");</script>');
    */
    public static function supur($veri)
    {
	    $veri = trim($veri);
	    $veri = mysql_real_escape_string($veri);
	    $veri = strip_tags($veri);
	    $veri = htmlspecialchars($veri);
	    return $veri;
    }

    /*
    * TC kimlik numarası doğrulama fonksiyonu
    * Örnek: if(!tckimlikdogrula('12345678910')) { echo 'TC Kimlik Numarası Yanlış.'; }
    */
	public static function tckimlikdogrula($tcno)
	{
    	if(strlen($tcno) < 11){ return false; }
    	if($tcno[0] == '0'){ return false; }
    	$toplam = ($tcno[0] + $tcno[2] + $tcno[4] + $tcno[6] + $tcno[8]) * 7;
    	$cikar = $toplam - ($tcno[1] + $tcno[3] + $tcno[5] + $tcno[7]);
    	$mod = $cikar % 10;
    	if($mod != $tcno[9]){ return false; }
    	$hanelertoplami = '';
    	for($i = 0 ; $i < 10 ; $i++){ $hanelertoplami += $tcno[$i];  }
    	if($hanelertoplami % 10 != $tcno[10]){ return false; }
	    return true;
	} 

    /*
    * Twitter gibi verilen tarihi x gün önce, x saat önce şeklinde çıktı verir
    * Örnek: 2 dakika önce, 1 saat önce
    */
    public static function time2str($ts)
    {
        if(!ctype_digit($ts))
            $ts = strtotime($ts);
        $diff = time() - $ts;
        if($diff == 0)
            return 'şimdi';
        elseif($diff > 0)
        {
            $day_diff = floor($diff / 86400);
            if($day_diff == 0)
            {
                if($diff < 60) return 'birkaç saniye önce';
                if($diff < 120) return '1 dakika önce';
                if($diff < 3600) return floor($diff / 60) . ' dakika önce';
                if($diff < 7200) return '1 saat önce';
                if($diff < 86400) return floor($diff / 3600) . ' saat önce';
            }
            if($day_diff == 1) return 'Dün';
            if($day_diff < 7) return $day_diff . ' gün önce';
            if($day_diff < 31) return ceil($day_diff / 7) . ' hafta önce';
            if($day_diff < 60) return 'geçen ay';
            $ret = date('F Y', $ts);
            return ($ret == 'December 1969') ? '' : $ret;
        }
        else
        {
            $diff = abs($diff);
            $day_diff = floor($diff / 86400);
            if($day_diff == 0)
            {
                if($diff < 120) return 'birkaç saniye önce';
                if($diff < 3600) return ' ' . floor($diff / 60) . ' dakika önce';
                if($diff < 7200) return 'az önce';
                if($diff < 86400) return '' . floor($diff / 3600) . ' saat önce';
            }
            if($day_diff == 1) return 'Yarın';
            if($day_diff < 4) return date('l', $ts);
            if($day_diff < 7 + (7 - date('w'))) return 'haftaya';
            if(ceil($day_diff / 7) < 4) return '' . ceil($day_diff / 7) . ' hafta sonra';
            if(date('n', $ts) == date('n') + 1) return 'bir ay sonra';
            $ret = date('F Y', $ts);
            return ($ret == 'December 1969') ? '' : $ret;
        }
    }
    
    
    /*
    * Verilen tarihi belirtilen formata göre gösterir
    * Örnek: tarihdonustur('2010-06-30', 'd-m-Y'); çıktısı: 30-06-2010
    */
    public static function tarihdonustur($tarih = null, $format = null)
    {
        if(is_null($format))
            $format = 'd-m-Y H:i:s';
        if(is_null($tarih))
            $tarih = time();
        if(ctype_digit($tarih) === true)
            return date($format, $tarih);
        else
            return date($format, strtotime($tarih));
    }

    /*
    * Telefonu uzunluğuna (xxx) xxx-xxxx veya xxx-xxxx olarak dönüştürür
    * Örnek: teleformat('5771234576'); çıktısı: (577) 123-4576
    */
    public static function teleformat($telefon)
    {
        $telefon = preg_replace("/[^0-9]/", '', $telefon);

        if(strlen($telefon) == 7)
            return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $telefon);
        elseif(strlen($telefon) == 10)
            return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $telefon);
        else
            return $telefon;
    }

    /*
    * Kullanıcıyı belirtilen URL'e yönlendirir
    * header Location yönlendirmesinden tek farkı eğer site içi yönlendirmeyse otomatik olarak site adresini ekler
    * Örnek: yonlendir('index.html');
    */
    public static function yonlendir($url = null)
    {
        if(is_null($url)) $url = $_SERVER['PHP_SELF'];
        header("Location: $url");
        exit();
    }
    
    
    /*
    * Belirtilen yazının sonuna slash ekler
    * Örnek: slash('https://www.r10.net'); çıktısı: https://www.r10.net/
    */
    public static function slash($yazi)
    {
        return rtrim($yazi, '/') . '/';
    }

    /*
    * Belirtilen yazının sonundaki slashı kaldırır
    * Örnek: slashsil('https://www.r10.net/'); çıktısı: https://www.r10.net
    */
    public static function unslash($str)
    {
        return rtrim($str, '/');
    }

    /*
    * Magic quotes düzeltir
    */
    public static function magicquotesfix($yazi = '')
    {
        if(is_null($yazi) || $yazi == '') return null;
        if(!get_magic_quotes_gpc()) return $yazi;
        return is_array($yazi) ? array_map('slashfix', $yazi) : stripslashes($yazi);
    }

    /*
    * Boyutunu K, M, G şeklinde çevirir
    * Örnek: boyutcevir('2048'); çıktısı: 2M
    */
    public static function boyutcevir($sayi, $round = 0)
    {
        $tipler = array('','K','M','G','T','P','E','Z','Y');
        while($sayi >= 1000)
        {
            $sayi /= 1024;
            array_shift($tipler);
        }
        return round($sayi, $round) . array_shift($tipler) . 'B';
    }

    /*
    * cURL bağlantı fonksiyonu
    * Yazan: İlyas BAT (http://www.ilyasbat.com.tr/)
    * Örnekler:
    * baglan('http://www.ilyasbat.com.tr'); // Cookie ve ref girmeden bağlanma
    * baglan('http://www.ilyasbat.com.tr','cookie.txt'); // Cookie bilgilerini alarak bağlanma. Ftpnizin ilgili klasöründe cookie.txt dosyası olmalı ve dosyanın chmodu 777 olmalı
    * baglan('http://www.ilyasbat.com.tr','cookie.txt','http://www.google.com'); // Hem cookieyi alıp hem de ref gönderme ref olarak http://www.google.com görünür.
    */
    public static function baglan($feed,$coo=null,$ref=null)
    {
        /*Fonksiyon İlyas BAT tarafından yazılmıştır.
        http://www.ilyasbat.com.tr*/
        $ch = curl_init();
        $timeout = 0;
        curl_setopt ($ch, CURLOPT_URL, $feed);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
        if(!empty($coo))
        {
            curl_setopt($ch, CURLOPT_COOKIEFILE, $coo);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $coo);
        }
        if(empty($ref))
        {
            curl_setopt($ch, CURLOPT_REFERER,$feed);
        }
        else
        {
            curl_setopt($ch, CURLOPT_REFERER,$ref);
        }
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $veri= curl_exec($ch);
        curl_close($ch);
        return $veri;
    }

    /*
    * Sayfayı şifre korumalı yapar
    * Örnek: sifrekoruma('admin', '12345', 'Yönetim Paneli');
    */
    public static function sifrekoruma($un, $pw, $realm = "Şifreli Sayfa")
    {
        if(!(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_USER'] == $un && $_SERVER['PHP_AUTH_PW'] == $pw))
        {
            header('WWW-Authenticate: Basic realm="' . $realm . '"');
            header('Status: 401 Unauthorized');
            exit();
        }
    }

    /*
    * Verilen linki is.gd link kısaltma servisi ile kısaltır
    * Örnek: linkkisalt('https://www.r10.net/index.php'); çıktısı: http://is.gd/izOgm
    */
    public static function linkkisalt($neyi)
    {
        $ne=baglan("http://is.gd/api.php?longurl=".$neyi);
        return $ne;
    }

    /*
    * Emailin gravatar avatarını gösterir
    * Örnek: gravatar('yorumcu@eposta.com', '60'); Çıktısı: Epostaya kayıtlı avatarı 60x60px boyutlarında gösterir
    */
    public static function gravatar($email, $boyut, $varsayilan, $reyting)
    {
        $ne = '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).'&default='.$varsayilan.'&size='.$boyut.'&rating='.$reyting.'" width="'.$boyut.'px" height="'.$boyut.'px" />';
        return $ne;
    }

    /*
    * Türkçe ve özel karakterleri SEF linklere uygun yapar
    * Örnek: trsil('Ubuntu en iyi açık kaynak işletim sistemidir'); Çıktısı: Ubuntu-en-iyi-acik-kaynak-isletim-sistemidir
    */
    public static function trsil ($q)
    {
        $q = str_replace ('ç', 'c', $q);
        $q = str_replace ('Ç', 'C', $q);
        $q = str_replace ('Ğ', 'G', $q);
        $q = str_replace ('ğ', 'g', $q);
        $q = str_replace ('İ', 'I', $q);
        $q = str_replace ('ı', 'i', $q);
        $q = str_replace ('ş', 's', $q);
        $q = str_replace ('Ş', 'S', $q);
        $q = str_replace ('Ö', 'O', $q);
        $q = str_replace ('ö', 'o', $q);
        $q = str_replace ('Ü', 'U', $q);
        $q = str_replace ('ü', 'u', $q);
        $q = str_replace (' ', '-', $q);
        $q = str_replace ('.', '-', $q);
        $q = str_replace ('\'', '', $q);
        $q = str_replace ('/', '', $q);
        $q = str_replace ('__', '-', $q);
        $q = str_replace ('!', '', $q);
        $q = str_replace ('&', '', $q);
        $q = str_replace ('?', '', $q);
        $q = str_replace ('*', '', $q);
        $q = str_replace ('=', '', $q);
        $q = str_replace ('(', '', $q);
        $q = str_replace (')', '', $q);
        $q = str_replace ('%', '', $q);
        $q = str_replace ('?', '', $q);
        $q = str_replace (':', '', $q);
        $q = str_replace ('---', '-', $q);
        $q = str_replace ('--', '-', $q);
        $q = str_replace ('@', '', $q);
        $q = str_replace ('[', '', $q);
        $q = str_replace (']', '', $q);
        $q = str_replace ('#', '', $q);
        $q = str_replace (('' . '$'), '', $q);
        return $q;
    }

    /*
    * Dosyanın mime tipini verir
    * Örnek: mimetip('Ajdar Anık - Çikita Muz.mp3'); Çıktısı: audio/mpeg
    */
    public static function mimetip($dosya, $varsayilan = 'application/octet-stream')
    {
        $mime_tipleri = 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',
                              'flac'    => 'audio/flac',
                              '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',
                              'oga'     => 'audio/ogg',
                              'ogg'     => 'audio/ogg',
                              'ogv'     => 'video/ogg',
                              'ogx'     => 'application/ogg',
                              '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',
                              'pot'     => 'application/vnd.ms-powerpoint',
                              'ppm'     => 'image/x-portable-pixmap',
                              'pps'     => 'application/vnd.ms-powerpoint',
                              'ppt'     => 'application/vnd.ms-powerpoint',
                              'prf'     => 'application/pics-rules',
                              'ps'      => 'application/postscript',
                              'pub'     => 'application/x-mspublisher',
                              'qt'      => 'video/quicktime',
                              'ra'      => 'audio/x-pn-realaudio',
                              'ram'     => 'audio/x-pn-realaudio',
                              'ras'     => 'image/x-cmu-raster',
                              'rgb'     => 'image/x-rgb',
                              'rmi'     => 'audio/mid',
                              'roff'    => 'application/x-troff',
                              'rtf'     => 'application/rtf',
                              'rtx'     => 'text/richtext',
                              'scd'     => 'application/x-msschedule',
                              'sct'     => 'text/scriptlet',
                              'setpay'  => 'application/set-payment-initiation',
                              'setreg'  => 'application/set-registration-initiation',
                              'sh'      => 'application/x-sh',
                              'shar'    => 'application/x-shar',
                              'sit'     => 'application/x-stuffit',
                              'snd'     => 'audio/basic',
                              'spc'     => 'application/x-pkcs7-certificates',
                              'spl'     => 'application/futuresplash',
                              'src'     => 'application/x-wais-source',
                              'sst'     => 'application/vnd.ms-pkicertstore',
                              'stl'     => 'application/vnd.ms-pkistl',
                              'stm'     => 'text/html',
                              'svg'     => "image/svg+xml",
                              'sv4cpio' => 'application/x-sv4cpio',
                              'sv4crc'  => 'application/x-sv4crc',
                              't'       => 'application/x-troff',
                              'tar'     => 'application/x-tar',
                              'tcl'     => 'application/x-tcl',
                              'tex'     => 'application/x-tex',
                              'texi'    => 'application/x-texinfo',
                              'texinfo' => 'application/x-texinfo',
                              'tgz'     => 'application/x-compressed',
                              'tif'     => 'image/tiff',
                              'tiff'    => 'image/tiff',
                              'tr'      => 'application/x-troff',
                              'trm'     => 'application/x-msterminal',
                              'tsv'     => 'text/tab-separated-values',
                              'txt'     => 'text/plain',
                              'uls'     => 'text/iuls',
                              'ustar'   => 'application/x-ustar',
                              'vcf'     => 'text/x-vcard',
                              'vrml'    => 'x-world/x-vrml',
                              'wav'     => 'audio/x-wav',
                              'wcm'     => 'application/vnd.ms-works',
                              'wdb'     => 'application/vnd.ms-works',
                              'wks'     => 'application/vnd.ms-works',
                              'wmf'     => 'application/x-msmetafile',
                              'wps'     => 'application/vnd.ms-works',
                              'wri'     => 'application/x-mswrite',
                              'wrl'     => 'x-world/x-vrml',
                              'wrz'     => 'x-world/x-vrml',
                              'xaf'     => 'x-world/x-vrml',
                              'xbm'     => 'image/x-xbitmap',
                              'xla'     => 'application/vnd.ms-excel',
                              'xlc'     => 'application/vnd.ms-excel',
                              'xlm'     => 'application/vnd.ms-excel',
                              'xls'     => 'application/vnd.ms-excel',
                              'xlt'     => 'application/vnd.ms-excel',
                              'xlw'     => 'application/vnd.ms-excel',
                              'xof'     => 'x-world/x-vrml',
                              'xpm'     => 'image/x-xpixmap',
                              'xwd'     => 'image/x-xwindowdump',
                              'z'       => 'application/x-compress',
                              'zip'     => 'application/zip');
        $uzanti = pathinfo($dosya, PATHINFO_EXTENSION);
        return isset($mime_tipleri[$uzanti]) ? $mime_tipleri[$uzanti] : $varsayilan;
    }
    
    
    
}// end fnc class

?>

Class halinide ben ekliyeyim , kullanmak için sayfanıza dahil edin.

Örnek kullanım

Fnc::teleformat('5771234576');
Fnc::supur('<script>alert("xss");</script>');
Fnc::tarihdonustur('2010-06-30', 'd-m-Y');