Automatic check for reciprocal links wordpress

This article has been written before more than 24months, information might old.

Pana la urma am scris un script php , care va verifica automat toate link-urile din blogroll , adica toate linkurile din baza de date , daca au link reciproc ( backlink ) , de asemenea se pot seta link-uri care sa fie excluse din verificare si se se poate seta ca link-urile sa fie scoase din blogroll / din pagina daca nu au link reciproc timp de cat seteaza userul , default este setat 3 luni .

Astfel daca o pagina cu care am facut link exchange nu are link-ul meu timp de 3 luni va fi automat setat in baza de date sa nu mai fie vizibil pe blog .
Scriptul trebuie folosit cu crontab ( majoritatea cpanel-urilor sau alte panouri de administrare ofera posibilitatea de a folosi cron jobs ) si pus in executie la cateva zile .

In script sunt cateva variabile la inceput care trebuie setate de user corespunzator .
Scriptul il puteti descarca de aici .

Sau copia de mai jos :

<?php
// Spagheti Script ( really i know OOP too but is not really nedeed here )
// Author azrael-sub7 , You can do anything with it , but if you post this someware please mention me as author
// A little Slow script cause close/open mysql for every function change if you don't like
// Also the whole content of a webiste will be placed in a variable so if a website is very large the script can fail do to to low ram If that happens you can rewrite the checking function to use a small buffer instead . but this method will consume more CPU and less RAM
// You must put this script with crontab or similar daemon/service i recommand to execute this script once in two days or in more days
/// CONFIG  FUT YOUR DATA HERE ... (really is PUT)
$DbHost = "***";// Mysql Host
$DbName = "***";       // WordPress Database Name
$DbUser = "***";     // Mysql Username
$DbPass = "***";   // Mysql Password
$DbPfix = "wp_";      // WordPress Tables Prefix .  In general e "wp_" ...
// OPTION
$Debug = FALSE; // Enable If You want the script to print some shit in LogFile.log
$Enabled = TRUE; // DO the checking ? Set to false will disable this script .
$Statistics = TRUE; // Statistics are in Romanian edit the script if you like to use other language
$YourDomain = "azrael-sub7.ro"; //   Your tdl  aka Domain ...
// Add links to exclude from check &#91; Separate Links with , &#93; ex ; $ExcludeLinks = "http://azrael-sub7.ro/,http://google.ro/";
$ExcludeLinks = "http://azrael-sub7.deviantart.com/,http://metal.iplay.ro,http://www.google.com/profiles/azsub7,http://azrael-sub7.stumbleupon.com/,http://www.gametracker.com/azrael-sub7/,http://www.last.fm/user/azrael-sub7,http://www.youtube.com/user/Azraelsub7,http://www.mywot.com/en/user/33137"; // Separate Links with ,
$EnableHideLink=1; // This Will auto Set in DataBase to not show link that has't your link
$TimeForHide=3600*24*30*3;// Set the For Hide Link if It has't your link time must be unixtimestamp default is 3600*24*30*3 ( two months )
// Do Not Modify below line unless you know what you are doing .
$INSTALL7765231=1;
/// CONFIG



function ExLinks($link){ // Exclude links callback
global $ExcludeLinks;
if($ExcludeLinks != ""){
$exar = explode(",",$ExcludeLinks);
foreach( $exar as $exlink ) if($exlink == $link) return FALSE;
return TRUE;} return TRUE;}


function ClearStatistics(){
$fh = fopen("Statistics.txt", 'w+');
fclose($fh);
}
function WriteStatistics($line){
$fh = fopen("Statistics.txt","a");
fwrite($fh, "&#91; ".date("F j, Y, g:i a")." &#93; ".$line."\n");
fclose($fh);}

function WriteDeleteLog($line){
$fh = fopen("DelLog.log","a");
fwrite($fh, "&#91; ".date("F j, Y, g:i a")." &#93; ".$line."\n");
fclose($fh);}

function WriteLog($line){
$fh = fopen("LogFile.log","a");
fwrite($fh, "&#91; ".date("F j, Y, g:i a")." &#93; ".$line."\n");
fclose($fh);}

function GetLinks(){ // Returns Array Of links
Global $DbHost,$DbName,$DbUser,$DbPass,$DbPfix,$Debug;
if(!$Debug){ // Debug Not Enabled
$con = mysql_connect($DbHost,$DbUser,$DbPass);
mysql_select_db($DbName);
$sqlq = mysql_query("SELECT `link_url` FROM `".$DbPfix."links` WHERE `link_visible`='Y'",$con);$res = array();
while ($row = mysql_fetch_array($sqlq)) $res&#91;&#93; = $row&#91;"link_url"&#93;;
mysql_close($con);
$res = array_filter($res,"ExLinks");
return $res;
}else{
$con = mysql_connect($DbHost,$DbUser,$DbPass) or WriteLog("GetLinks -> Connect to mysql server could not be initialized cause :".mysql_error());
if(!mysql_select_db($DbName)) WriteLog("GetLinks -> DataBase could not be selected ! ");
$sqlq = mysql_query("SELECT `link_url` FROM `".$DbPfix."links`",$con);
if(!$sqlq)  WriteLog("GetLinks -> Query could not be executed ! ");
while ($row = mysql_fetch_array($sqlq)) $res[] = $row["link_url"];
mysql_close($con);
$res = array_filter($res,"ExLinks");
return $res;}}


function ChkBackLinkFO4( $url , $yl ){ // Check for link on page
  $fhandle = @fopen( $url , "rb");
  if(isset($fhandle)){
  if($fhandle == FALSE) return FALSE;
  $c = '';
  while(!feof($fhandle)){
  $c .= fread($fhandle, 1024);
  } fclose($fhandle);
  if($c=='') return FALSE;
  $pm = preg_match_all("/<a&#91;^>]*".$yl."[^>]*>/",$c,$arr);
  if($pm==FALSE) return FALSE;
  if($pm>0)return TRUE; return FALSE;}return FALSE;}

  function DoCheck(){ // Do list checking
  Global $DbHost,$DbName,$DbUser,$DbPass,$DbPfix,$Debug,$YourDomain, $INSTALL7765231,$EnableHideLink,$ExcludeLinks,$TimeForHide;
  ClearStatistics();
  if($ExcludeLinks != "") WriteStatistics("Link-uri Excluse :".$ExcludeLinks);
  if($INSTALL7765231){
  $Links = GetLinks();
  ini_set('max_execution_time', 10*count($Links)) ; // 10 secunde pentru fiecare link .
  foreach($Links as $Link){
  if(ChkBackLinkFO4($Link,$YourDomain)){
  if(!$Debug){
  $con = mysql_connect($DbHost,$DbUser,$DbPass);
  mysql_select_db($DbName);
  $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `lstckres`='1',`lstcktime`='".time()."',`fspcktime`='".time()."' WHERE `link_url`='".$Link."'");
  $sqlq = mysql_query("SELECT `lstckres`, `lstcktime`,`fspcktime` FROM `".$DbPfix."links` WHERE `link_url`='".$Link."'");
  $rrr = mysql_fetch_row($sqlq);
  WriteStatistics("Link url : ".$Link." [ s-a gasit link reciproc ]- [ Ultima Cautare : ".date("F j, Y, g:i a",$rrr[1])."] ");
  mysql_close($con);
  }else{
  WriteLog("DoCheck -> CHECK LINK : ".$Link." HAS RETURN 1 ");
  $con = mysql_connect($DbHost,$DbUser,$DbPass) or WriteLog("DoCheck -> Connect to mysql server could not be initialized cause :".mysql_error());
  if(!mysql_select_db($DbName))WriteLog("DoCheck -> DataBase could not be selected ! ");
  $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `lstckres`='1',`lstcktime`='".time()."',`fspcktime`='".time()."' WHERE `link_url`='".$Link."'");
  if(!$sqlq)  WriteLog("DoCheck -> UPDATE Query could not be executed ! ");
  $sqlq = mysql_query("SELECT `lstckres`, `lstcktime`,`fspcktime` FROM `".$DbPfix."links` WHERE `link_url`='".$Link."'");
  if(!$sqlq)  WriteLog("DoCheck -> SELECT Query could not be executed ! ");
  $rrr = mysql_fetch_row($sqlq);
  WriteStatistics("Link url : ".$Link." [ s-a gasit link reciproc ]- [ Ultima Cautare : ".date("F j, Y, g:i a",$rrr[1])."] ");
  mysql_close($con);
  }}else{if(!$Debug){
  $con = mysql_connect($DbHost,$DbUser,$DbPass);
  mysql_select_db($DbName);
  $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `lstckres`='0',`lstcktime`='".time()."' WHERE `link_url`='".$Link."'");
  $sqlq = mysql_query("SELECT `fspcktime` FROM `".$DbPfix."links` WHERE `link_url`='".$Link."'");
  $rrr = mysql_fetch_row($sqlq);
  if( $rrr[0]=='' || !is_numeric($rrr[0])) $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `fspcktime`='".time()."' WHERE `link_url`='".$Link."'");
  $sqlq = mysql_query("SELECT `lstckres`, `lstcktime`,`fspcktime` FROM `".$DbPfix."links` WHERE `link_url`='".$Link."'");
  $rrr = mysql_fetch_row($sqlq);
  if($EnableHideLink){
  WriteStatistics("Link url : ".$Link." [ nu s-a gasit link reciproc ]- [ Ultima Cautare : ".date("F j, Y, g:i a",$rrr[1])."] [ Link-ul va fi scos la data de : ".date("F j, Y, g:i a",$rrr[2]+$TimeForHide)." ] ");
  if(time() > ($rrr[2]+$TimeForHide)){
  $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `link_visible`='N' WHERE `link_url`='".$Link."'");
  WriteDeleteLog("Link-ul : ".$Link." a fost sters pentru ca nu a fost gasit link reciproc timp de 3 luni .");
  }}else {
  WriteStatistics("Link url : ".$Link." [ nu s-a gasit link reciproc ]- [ Ultima Cautare : ".date("F j, Y, g:i a",$rrr[1])."] ");
  }mysql_close($con);
  }else{
  WriteLog("DoCheck -> CHECK LINK : ".$Link." HAS RETURN 0 ");
  $con = mysql_connect($DbHost,$DbUser,$DbPass)or WriteLog("DoCheck -> Connect to mysql server could not be initialized cause :".mysql_error());
  if(!mysql_select_db($DbName))WriteLog("DoCheck -> DataBase could not be selected ! ");
  $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `lstckres`='0',`lstcktime`='".time()."' WHERE `link_url`='".$Link."'");
  if(!$sqlq)  WriteLog("DoCheck -> Update Query could not be executed ! ");
  $sqlq = mysql_query("SELECT `fspcktime` FROM `".$DbPfix."links` WHERE `link_url`='".$Link."'");
  $rrr = mysql_fetch_row($sqlq);
  if( $rrr[0]=='' || !is_numeric($rrr[0])) $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `fspcktime`='".time()."' WHERE `link_url`='".$Link."'");
  if(!$sqlq)  WriteLog("DoCheck -> SELECT Query could not be executed ! ");
  $sqlq = mysql_query("SELECT `lstckres`, `lstcktime`,`fspcktime` FROM `".$DbPfix."links` WHERE `link_url`='".$Link."'");
  $rrr = mysql_fetch_row($sqlq);
  if(!$sqlq) WriteLog("DoCheck -> SELECT Query could not be executed ! ");
  if($EnableHideLink){
  WriteStatistics("Link url : ".$Link." [ nu s-a gasit link reciproc ]- [ Ultima Cautare : ".date("F j, Y, g:i a",$rrr[1])."] [ Link-ul va fi scos la data de : ".date("F j, Y, g:i a",$rrr[2]+$TimeForHide)." ] ");
  if(time() > ($rrr[2]+$TimeForHide)){
  $sqlq = mysql_query("UPDATE `".$DbPfix."links` SET `link_visible`='N' WHERE `link_url`='".$Link."'");
  if(!$sqlq) WriteLog("DoCheck -> UPDATE Query could not be executed ! ");
  WriteDeleteLog("Link-ul : ".$Link." a fost sters pentru ca nu a fost gasit link reciproc timp de 3 luni .");
  }}else {
  WriteStatistics("Link url : ".$Link." [ nu s-a gasit link reciproc ]- [ Ultima Cautare : ".date("F j, Y, g:i a",$rrr[1])."] ");
  }mysql_close($con);}}}}else{if($Debug) WriteLog("DoCheck -> NOT INSTALLED PLEASE RUN INSTALL ! ");}}


function Install(){ // ADD 3 COLUMNS TO WORDPRESS LiNK TABLES ( INSTALL Cica ... )
Global $DbHost,$DbName,$DbUser,$DbPass,$DbPfix,$Debug,$INSTALL7765231;
if(!$INSTALL7765231){
if(!$Debug){
$con = mysql_connect($DbHost,$DbUser,$DbPass);
mysql_select_db($DbName);
$sqlq = mysql_query("ALTER TABLE `".$DbPfix."links` ADD COLUMN ( lstckres VARCHAR(255) NOT NULL ,lstcktime VARCHAR(255) NOT NULL ,fspcktime VARCHAR(255) NOT NULL )");
mysql_close($con);
if($sqlq){
$fgc = file_get_contents($_SERVER['SCRIPT_FILENAME']);
if($fgc != FALSE){
$fgc = preg_replace("/\$I{1}NSTALL7765231=0;/","$INSTALL7765231=1;",$fgc);
$fh = fopen($_SERVER['SCRIPT_FILENAME'], "w");
fwrite($fh,$fgc);
fclose($fh);
}}}else{
$con = mysql_connect($DbHost,$DbUser,$DbPass) or WriteLog("INSTALL -> Connect to mysql server could not be initialized cause : ".mysql_error());
 if(!mysql_select_db($DbName)) WriteLog("INSTALL -> DataBase could not be selected ! ");
$sqlq = mysql_query("ALTER TABLE `".$DbPfix."links` ADD COLUMN ( lstckres VARCHAR(255) NOT NULL ,lstcktime VARCHAR(255) NOT NULL ,fspcktime VARCHAR(255) NOT NULL )");
mysql_close($con);
if(!$sqlq) WriteLog("INSTALL -> Query could not be executed ( maybe already installed ) ! ");
else {
$fgc = file_get_contents($_SERVER['SCRIPT_FILENAME']);
if($fgc != FALSE){
$fgc = preg_replace('/\$I{1}NSTALL7765231=0;/','\$INSTALL7765231=1;',$fgc);
if($fgc == FALSE) WriteLog("INSTALL -> PREG REPLACE FAILED ! ");
$fh = fopen($_SERVER['SCRIPT_FILENAME'], "w");
if(!$fh) WriteLog("INSTALL -> OPEN FILE FAILED ! ");
else  WriteLog("INSTALL -> HAS SUCCEEDED ! ");
fwrite($fh,$fgc);
fclose($fh);
}}}}else{if($Debug){
$con = mysql_connect($DbHost,$DbUser,$DbPass) or WriteLog("INSTALL -> Connect to mysql server could not be initialized cause :".mysql_error());
if(!mysql_select_db($DbName)) WriteLog("INSTALL -> DataBase could not be selected ! ");
$sqlq = mysql_query("SELECT `lstckres`,`lstcktime`,`fspcktime` FROM `".$DbPfix."links`",$con);
mysql_close($con);
if(!$sqlq){ // Nu e instalat .
$fgc = file_get_contents($_SERVER['SCRIPT_FILENAME']);
if($fgc != FALSE){
$fgc = preg_replace('/\$I{1}NSTALL7765231=1;/','\$INSTALL7765231=0;',$fgc);
if($fgc == FALSE) WriteLog("INSTALL -> PREG REPLACE FAILED ! ");
$fh = fopen($_SERVER['SCRIPT_FILENAME'], "w");
if(!$fh) WriteLog("INSTALL -> OPEN FILE FAILED ! ");
else  WriteLog("INSTALL -> HAS SUCCEEDED ! ");
fwrite($fh,$fgc);
fclose($fh);
}Install();
}}else{
$con = mysql_connect($DbHost,$DbUser,$DbPass) ; mysql_select_db($DbName);
$sqlq = mysql_query("SELECT `lstckres`,`lstcktime`,`fspcktime` FROM `".$DbPfix."links`",$con);
mysql_close($con);
if(!$sqlq){ // Nu e instalat .
$fgc = file_get_contents($_SERVER['SCRIPT_FILENAME']);
if($fgc != FALSE){
$fgc = preg_replace('/\$I{1}NSTALL7765231=1;/','\$INSTALL7765231=0;',$fgc);
$fh = fopen($_SERVER['SCRIPT_FILENAME'], "w");
fwrite($fh,$fgc);
fclose($fh);
}Install();
}}}}

function Uninstall(){     // DELETE THE ADDed Columns in _links
Global $DbHost,$DbName,$DbUser,$DbPass,$DbPfix,$Debug;
if(!$Debug){
$con = mysql_connect($DbHost,$DbUser,$DbPass);
mysql_select_db($DbName);
$sqlq = mysql_query("SELECT `lstckres`,`lstcktime`,`fspcktime` FROM `".$DbPfix."links`",$con);
if($sqlq){ // E instalat .
$sqlq = mysql_query("ALTER TABLE `".$DbPfix."links` DROP COLUMN ( lstckres  ,lstcktime ,fspcktime )");
}mysql_close($con);}else {
$con = mysql_connect($DbHost,$DbUser,$DbPass) or WriteLog("UNINSTALL -> Connect to mysql server could not be initialized cause :".mysql_error());
if(!mysql_select_db($DbName)) WriteLog("UNINSTALL -> DataBase could not be selected ! ");
$sqlq = mysql_query("SELECT `lstckres`,`lstcktime`,`fspcktime` FROM `".$DbPfix."links`",$con);
if($sqlq){ // E instalat .
$sqlq = mysql_query("ALTER TABLE `".$DbPfix."links` DROP COLUMN ( lstckres  ,lstcktime ,fspcktime )");
if($sqlq) WriteLog("UNINSTALL -> HAS SUCCEEDED  ! ");
else WriteLog("UNINSTALL -> HAS FAILED  ! ");
}else WriteLog("UNINSTALL -> ALREADY UNINSTALLED ! ");
mysql_close($con);
}}


if($Enabled){
Install();
DoCheck();
}else{
Uninstall();
}

?>
Share the joy

Leave a Reply