Hai sa fim practici!
RSS icon Email icon Home icon
  • PHP XML Blogroll sursa: blogspot.com - Suport cache si MYSQL

    Posted on October 18th, 2009 admin No comments

    Am scris acest articol si functie cu scopul de a o implementa intr-un site pentru a afisa ultimele posturi de pe un blog gazduit pe blogspot.com (gen: http://carbonenergy.blogspot.com), cu alte cuvinte: Blogroll

    Este facut in PHP cu suport MYSQL pentru a stoca cache-ul si foloseste simpleXML pentru luare datelor de pe blog.

    Este setat sa actualizeze informatiile la 24 de ore, dar acest interval se poate modifica, regland intervalul de secunde. Prescurteaza textul din titlu si din continut la un numar setat de caractere (acestea pot fi setat in script).

    Folosit scriptul returneaza un array cu informatile despre posturi: date (datetime), link, titlu si continut, timestamp.

    Singura sa problema e ca nu curata posturile vechi din baza de date.  Poate implementez in urmatoarea versiune.

    Exemplu: http://www.carbonenergy.ro/ : vezi partea de stanga a siteului (Blogroll)

    Functia PHP:

    function get_broll ($num = 3) {

    $query = mysql_query(”SELECT `id`, `date`, `title`, `link`, `content`, `tstamp` FROM `cms_broll` ORDER by `date` DESC LIMIT 0, 3″);

    $i = 0;
    while ($row = mysql_fetch_assoc($query)) {
    $brollq[$i] = $row;
    $brollq[$i]['date'] = substr($brollq[$i]['date'], 0, 10);
    $i++;
    }

    $tstamp = time();

    if ((mysql_num_rows($query) == 0) || !isset($brollq[0]['tstamp']) || (($tstamp - $brollq[0]['tstamp']) > 86400)) {

    $xml=@simplexml_load_file(’http://carbonenergy.blogspot.com/feeds/posts/default’);

    if(!$xml){return 0;}

    for ($i = 0; $i < $num ; $i++) {

    $broll[$i]['link'] = $xml->entry[$i]->link[4]->attributes()->href;
    $broll[$i]['date'] = str_replace(’T', ‘ ‘, substr($xml->entry[$i]->published, 0, 19));
    $broll[$i]['title'] = substr($xml->entry[$i]->title, 0, 33);
    if (strlen($xml->entry[$i]->title) > 33) {$broll[$i]['title'] .= ‘…’; }
    $broll[$i]['content'] = substr($xml->entry[$i]->content, 0, 137);
    if (strlen($xml->entry[$i]->content) > 132) {$broll[$i]['content'] .= ‘…’; }

    if ((mysql_num_rows($query) == 0) || ($brollq[$i]['link'] != $broll[$i]['link']) ) {
    $up = mysql_query(”INSERT INTO `cms_broll` (`date`, `title`, `link`, `content`, `tstamp`) VALUES(’”.$broll[$i]['date'].”‘,’”.$broll[$i]['title'].”‘,’”.$broll[$i]['link'].”‘,’”.$broll[$i]['content'].”‘,’”.$tstamp.”‘)”);
    }
    $broll[$i]['date'] = substr($xml->entry[$i]->published, 0, 10);

    }

    } else {
    return $brollq;}

    return $broll;

    }

    Structura tabel de date MYSQL:
    CREATE TABLE IF NOT EXISTS `cms_broll` (
    `id` int(11) NOT NULL auto_increment,
    `date` datetime NOT NULL,
    `title` text NOT NULL,
    `link` text NOT NULL,
    `content` text NOT NULL,
    `tstamp` int(11) NOT NULL,
    UNIQUE KEY `id_2` (`id`),
    UNIQUE KEY `id_3` (`id`),
    KEY `id` (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=34 ;


    – Dumping data for table `cms_broll`

    INSERT INTO `cms_broll` (`id`, `date`, `title`, `link`, `content`, `tstamp`) VALUES
    (33, ‘2009-10-16 02:34:00′, ‘Doua miliarde euro din emisiile d…’, ‘http://carbonenergy.blogspot.com/2009/10/doua-miliarde-euro-din-emisiile-de-co2.html’, ‘Ca sa mai faca rost de niste bani la buget pe timp de criza, Guvernul spera sa obtina doua miliarde euro din vanzarea surplusului de drep…’, 1255794474),
    (32, ‘2009-10-16 02:36:00′, ‘Primul proiect de captare a CO2 i…’, ‘http://carbonenergy.blogspot.com/2009/10/primul-proiect-de-captare-co2-in.html’, ‘Producatorul de gaze Romgaz, transportatorul national de gaze Transgaz si producatorul de energie electrica Complexul Energetic Craiova v…’, 1255794474),
    (31, ‘2009-10-16 02:38:00′, ‘EU: Romania, Bulgaria must cut na…’, ‘http://carbonenergy.blogspot.com/2009/10/eu-romania-bulgaria-must-cut-national.html’, ‘London. The European Commission Friday ruled that Romania and Bulgaria must both reduce their carbon dioxide national allocation plans fo…’, 1255794474);

    Astept comentarii si sugestii.

  • PHP si MYSQL Curs valutar automat EUR/USD - Sursa BNR

    Posted on September 22nd, 2009 admin 3 comments

    Am scris acest articol deoarece cred ca ar putea fi folositor.  “Scriptul” consta intro functie si un tabel care este foarte simplu: ia cursul de schimb de pe http://www.bnr.ro/nbrfxrates.xml numai pentru moneda EURO si USD si le stocheaza intro baza de date.

    De ce?

    Informaţiile preluate trebuie stocate la nivelul aplicaţiei de preluare pentru a evita generarea de trafic redundant. IP-urile care generează un volum de trafic disproporţionat de mare vor fi restricţionate administrativ. Pentru restabilirea accesului puteţi semnala remedierea situaţiei pe adresa de webmaster.

    • evitarea traficului inutil
    • timpi de incarcare si raspuns a paginilor mai bun
    • mai gasiti si voi.

    Uitati si functia:

    function get_rate() {

    $rate['curdate'] = date(”Y-m-d”);

    $query = mysql_query(”SELECT `eur`, `usd` FROM `cms_rate` WHERE `data`=’”.$rate['curdate'].”‘”);

    if (mysql_num_rows($query) == 0) {
    $xml=@file_get_contents(’http://www.bnr.ro/nbrfxrates.xml’, FILE_BINARY);

    if(!$xml){return 0;}
    preg_match(’/”EUR”>(.*)<\/Rate>/’, $xml, $eur);
    preg_match(’/”USD”>(.*)<\/Rate>/’, $xml, $usd);

    $rate['eur'] = $eur[1];
    $rate['usd'] = $usd[1];

    $up = mysql_query(”INSERT INTO `cms_rate` (`data`, `eur`, `usd`) VALUES(’”.$rate['curdate'].”‘,’”.$rate['eur'].”‘,’”.$rate['usd'].”‘)”);

    return $rate;

    } else {

    $res = mysql_fetch_assoc($query);
    $rate['usd'] = $res['usd'];
    $rate['eur'] = $res['eur'];
    return $rate;
    }

    Structura tabelului de date:

    SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;

    – ——————————————————–


    – Table structure for table `cms_rate`

    CREATE TABLE IF NOT EXISTS `cms_rate` (
    `id` int(11) NOT NULL auto_increment,
    `data` date NOT NULL,
    `eur` double NOT NULL,
    `usd` double NOT NULL,
    PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;


    – Dumping data for table `cms_rate`

    INSERT INTO `cms_rate` (`id`, `data`, `eur`, `usd`) VALUES
    (10, ‘2009-09-22′, 4.2665, 2.9123);

    Connexiunea la baza de date o las in seama voastra.

    Success!

  • Public IP Info v 0.2 - script TCL pentru eggdrop

    Posted on March 31st, 2009 admin No comments

    Acest script il puteti folosi pentru eggdrop pentru a va afisa, pe canalul IRC unde a fost declansata comanda, informatii despre un anumit IP.

    Foloseste ca sursa de informatii www.ip2location.com, iar informatiile solicitate le stocheaza intro “baza de date” proprie pentru a reduce solicitarea serverului unde este gazduit botul.

    De mentionat este faptul ca informatiile stocate sunt retinute pentru a o anumita perioada de timp (se poate seta in configuratia scriptului). In acest fel datale nu se vor invechi.

    Scriptul in actiune:

    [13:27] <hwk> !ipinfo 92.81.168.244
    [13:27] <Myth`> hwk IP information for 92.81.168.244: Country: ROMANIA, City: BACAU, ISP: ROMTELECOM DATA NETWORK, Domain: PLATINUM-IFN.RO

    Versiunea 0.1 a acestui script se poate descarca de pe www.egghelp.org, sectiunea TCL Archive, sub numele: Public IP information checker.

    Diferentele intre cele doua versiuni fiind sistemul de stocare temporara a datelor.

    catch {array unset ipinfocache}
    #########################################################################
    #         Public IP Info v0.2 by Hawkee - lowraider1@gmail.com          #
    #-----------------------------------------------------------------------#
    # This script uses www.ip2location.com to check info for an ip adresses #
    #	You can use it in 2 modes: THE FREE ONE or THE Account one	  #
    #										  #
    # The free one works for 20 IP lookups per day, because this is the 	  #
    # maximum number of lookups, ip2location.com offers for unregistered 	  #
    # users - per ip -. Set ip2loc(account) (default) to use this.		  #
    #									         #
    # Changes from version 0.1:							  #
    #		-implemented a cache system to store the info to reduce	  #
    #		resource usage                                            #
    #             -the cache system refreshes the info at a given period of #
    #		time                                                      #
    #										  #
    #										  #
    # Works on all channels, and it can be used by all users. Requires TCL  #
    # HTTP PACK									  #
    #										  #
    # USAGE: !ipinfo <IP>							  #
    # 					HAVE PHUN				  #
    # Email me with suggestions and bug reports at lowraider1@gmail.com 	  #
    #										  #
    #							 grtz HWK @ undernet	  #
    #########################################################################
    
    set ipinfo(cachefile) 	"ipinfo.cache"		;#the cache file
    set ipinfo(cacherefresh) 	"7"					;#the time (in days) to refresh the info for an IP
    
    set ver "0.2 - mod"
    
    package require http
    
    #DO NOT EDIT BELOW FUCKER!#
    
    proc ipinfo:parser {nick uhost hand chan args} {
    
    set ip [string trimright [lindex $args 0] "."]
    
    if {![regexp {^(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])(\.|$)){4}$} $ip]} {
    puthelp "privmsg $chan :$nick NO/Invalid IP pattern. USAGE: !ipinfo 193.193.193.193"
    putlog "IP INFO $chan $nick - INVALID IP PATTERN"
    return
    
    }
    
    set infoip [ipinfo:output $ip]
    set country [lindex $infoip 0]
    set city [lindex $infoip 1]
    set isp [lindex $infoip 2]
    set domain [lindex $infoip 3]
    
    if {![info exists country] || [string equal $country -]} {
    puthelp "privmsg $chan :$nick No information found for IP: \00302$ip\003 please redefine your IP"
    putlog "IP INFO $chan $nick - No results"
    return
    }
    
    puthelp "privmsg $chan :$nick IP information for \00302$ip\003: \002Country:\002 \00304$country\003, \002City:\002 \00304$city\003, \002ISP\002: \00302$isp\003, \002Domain:\002 \00302$domain\003"
    putlog "IPinfo request:  $nick on $chan"
    
    }
    
    proc ipinfo:getinfo {host} {
    global ipinfo ipinfocache
    
    	::http::config -useragent "Mozilla/5.0 ; Gecko"
    
    	set que [::http::formatQuery ipaddresses $host]
       	set http_req [::http::geturl "http://www.ip2location.com/demo.aspx" -timeout 2000 -query $que]
    
    	set data [::http::data $http_req]
    	::http::cleanup $http_req
    
    	regexp {<span id="dgLookup__ctl2_lblICountry">([^<]+)</span></TD>} $data -> country
    	regexp {<span id="dgLookup__ctl2_lblICity">([^<]+)</span></TD>} $data -> city
    	regexp {<span id="dgLookup__ctl2_lblIISP">([^<]+)</span></TD>} $data -> isp
    	regexp {<span id="dgLookup__ctl2_lblIDomain">([^<]+)</span></TD>} $data -> domain
    
    	set info[list $country $city $isp $domain [unixtime]]
    
    	set ipinfocache($host) $info
    
    	return $info
    
    }
    
    proc ipinfo:output {host} {
    global ipinfo ipinfocache
    
    if {[info exists ipinfocache($host)]} {
    	if {[expr {(60*60*24)*$ipinfo(cacherefresh)}] < [expr {[unixtime] - [lindex $ipinfocache($host) 4]}]} {
    		putlog "IPinfo: refreshing cache data for $host"
    		set info [ipinfo:getinfo $host]
    		ipinfo:save
    		return $info
    	} else {
    	return $ipinfocache($host)
    	}
    } else {
    	set info [ipinfo:getinfo $host]
    	ipinfo:save
    	return $info
    }
    
    }
    
    proc ipinfo:save {} {
    global ipinfo ipinfocache
    
    set write [open $ipinfo(cachefile) w]
    puts $write	[list array set ipinfocache [array get ipinfocache]]
    close $write
    
    }
    
    proc ipinfo:read {} {
    global ipinfo ipinfocache
    
    if {[file exists $ipinfo(cachefile)]} {
    	if {![catch {source $ipinfo(cachefile)} cacheerror]} {
    		putlog "IPinfo: cache file successfully loaded"
    	} else {
    		putlog "IPinfo: cache file failed to load -: $cacheerror"
    		putlog "IPinfo: trying to fix cache file: reset" ; ipinfo:save
    
    	}
    } else {
    
    ipinfo:save
    putlog "IPinfo: cache file written - first time use"
    }
    
    }
    
    ipinfo:read
    
    bind pub -|- !ipinfo ipinfo:parser
    
    putlog "Public IP Info $ver by HAWKEE Successfuly loaded"