Файл: seo.class.php
Строк: 405
<?php
/**
* @package SEO Class
* @author Jan Voшнљek <loneswan@loneswan.net>
* @version 1.1
*/
// variable used by the google pagerank function
define('GOOGLE_MAGIC', 0xE6359A60);
class SeoHelper
{
/**
@var string text to work with */
private $text;
/**
@var int minimal keyword length*/
private $minKeywordLength = 4;
/**
@var int default description length
Note: Google shows 156 Characters (Including Spaces) for Meta Description. Yahoo shows 161.
*/
private $defaultDescLength = 150;
/**
@var string default link path for tagcloud
Note: <keyword> is the keyword from your tagcloud. It will be replaced with the exact keyword in tag cloud.
*/
private $tagcloudLink = "http://yoursite.com/tag/<keyword>";
/**
@var array banned words - common words shouldn't be considered as keyword */
/** These are english ones. You can replace them with common words of your language */
private $bannedWords = array(
'able',
'about',
'above',
'act',
'add',
'afraid',
'after',
'again',
'against',
'age',
'ago',
'agree',
'all',
'almost',
'alone',
'along',
'already',
'also',
'although',
'always',
'am',
'amount',
'an',
'and',
'anger',
'angry',
'animal',
'another',
'answer',
'any',
'appear',
'apple',
'are',
'arrive',
'arm',
'arms',
'around',
'arrive',
'as',
'ask',
'at',
'attempt',
'aunt',
'away',
'back',
'bad',
'bag',
'bay',
'be',
'became',
'because',
'become',
'been',
'before',
'began',
'begin',
'behind',
'being',
'bell',
'belong',
'below',
'beside',
'best',
'better',
'between',
'beyond',
'big',
'body',
'bone',
'born',
'borrow',
'both',
'bottom',
'box',
'boy',
'break',
'bring',
'brought',
'bug',
'built',
'busy',
'but',
'buy',
'by',
'call',
'came',
'can',
'cause',
'choose',
'close',
'close',
'consider',
'come',
'consider',
'considerable',
'contain',
'continue',
'could',
'cry',
'cut',
'dare',
'dark',
'deal',
'dear',
'decide',
'deep',
'did',
'die',
'do',
'does',
'dog',
'done',
'doubt',
'down',
'during',
'each',
'ear',
'early',
'eat',
'effort',
'either',
'else',
'end',
'enjoy',
'enough',
'enter',
'even',
'ever',
'every',
'except',
'expect',
'explain',
'fail',
'fall',
'far',
'fat',
'favor',
'fear',
'feel',
'feet',
'fell',
'felt',
'few',
'fill',
'find',
'fit',
'fly',
'follow',
'for',
'forever',
'forget',
'from',
'front',
'gave',
'get',
'gives',
'goes',
'gone',
'good',
'got',
'gray',
'great',
'green',
'grew',
'grow',
'guess',
'had',
'half',
'hang',
'happen',
'has',
'hat',
'have',
'he',
'hear',
'heard',
'held',
'hello',
'help',
'her',
'here',
'hers',
'high',
'hill',
'him',
'his',
'hit',
'hold',
'hot',
'how',
'however',
'I',
'if',
'ill',
'in',
'indeed',
'instead',
'into',
'iron',
'is',
'it',
'its',
'just',
'keep',
'kept',
'knew',
'know',
'known',
'late',
'least',
'led',
'left',
'lend',
'less',
'let',
'like',
'likely',
'likr',
'lone',
'long',
'look',
'lot',
'make',
'many',
'may',
'me',
'mean',
'met',
'might',
'mile',
'mine',
'moon',
'more',
'most',
'move',
'much',
'must',
'my',
'near',
'nearly',
'necessary',
'neither',
'never',
'next',
'no',
'none',
'nor',
'not',
'note',
'nothing',
'now',
'number',
'of',
'off',
'often',
'oh',
'on',
'once',
'only',
'or',
'other',
'ought',
'our',
'out',
'please',
'prepare',
'probable',
'pull',
'pure',
'push',
'put',
'raise',
'ran',
'rather',
'reach',
'realize',
'reply',
'require',
'rest',
'run',
'said',
'same',
'sat',
'saw',
'say',
'see',
'seem',
'seen',
'self',
'sell',
'sent',
'separate',
'set',
'shall',
'she',
'should',
'side',
'sign',
'since',
'so',
'sold',
'some',
'soon',
'sorry',
'stay',
'step',
'stick',
'still',
'stood',
'such',
'sudden',
'suppose',
'take',
'taken',
'talk',
'tall',
'tell',
'ten',
'than',
'thank',
'that',
'the',
'their',
'them',
'then',
'there',
'therefore',
'these',
'they',
'this',
'those',
'though',
'through',
'till',
'to',
'today',
'told',
'tomorrow',
'too',
'took',
'tore',
'tought',
'toward',
'tried',
'tries',
'trust',
'try',
'turn',
'two',
'under',
'until',
'up',
'upon',
'us',
'use',
'usual',
'various',
'verb',
'very',
'visit',
'want',
'was',
'we',
'well',
'went',
'were',
'what',
'when',
'where',
'whether',
'which',
'while',
'white',
'who',
'whom',
'whose',
'why',
'will',
'with',
'within',
'without',
'would',
'yes',
'yet',
'you',
'young',
'your'
);
/**
* @param string text to work with
*/
public function __construct($text = '')
{
$this->text = $text;
}
/**
* Sets the text to work with.
* @param string
*/
public function setText($text)
{
$this->text = $text;
}
/**
* Gets the text to work with.
* Note: This text isn't sanitized. It contains all the HTML and everything.
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Sets the minimal keyword lenght
* @param int
*/
public function setMinKeywordLength($len)
{
$this->minKeywordLength = $len;
}
/**
* Gets the minimal keyword length
* @return int
*/
public function getMinKeywordLength()
{
return $this->minKeywordLength;
}
/**
* Sets the banned words.
* @param array
*/
public function setBannedWords($words)
{
$this->bannedWords = $words;
}
/**
* Get banned words.
* @return array
*/
public function getBannedWords()
{
return $this->$bannedWords;
}
/**
* Set tagcloud link (for tag cloud links)
* @param string
*/
public function setTagcloudLink($tagcloudLink)
{
$this->tagcloudLink = $tagcloudLink;
}
/**
* Get tagcloud link path
* @return string
*/
public function getTagcloudLink()
{
return $this->$tagcloudLink;
}
/**
* Gegenrate tag cloud of keywords array
* @param array
* @return string
*/
public function getTagcloud($keywords)
{
$kw = "<div class="tag-cloud">";
foreach ($keywords as $key=>$value)
{
$kw .= "<a href="" . str_replace("<keyword>",$key,$this->tagcloudLink) . "" style="font-size:" . ((9)+((count($keywords)+5)*$value/count($keywords))) . "pt">". $key ."</a>";
}
$kw .= "</div>";
return $kw;
}
/**
* Returns given number of keywords as an array
* @param int
* @return array
*/
public function getKeywordsArray($count=0)
{
// We need to remove the garbage like html tags, double spaces and entities.
$text = strtolower($this->text);
$text = strip_tags($text);
$text = $this->str_replace_word($this->bannedWords, '', $text);
$text = preg_replace('/[^pLp{Zs}]/u', '', $text);
$text = preg_replace('/s+/', ' ', $text);
// Divide
$words = explode(' ', $text);
$keywords = array();
foreach ($words as $key => $word)
{
if (strlen($word) < $this->minKeywordLength)
{
unset($words[$key]);
}
if (!in_array($word, $words))
{
$keywords[$word] = 1;
}
else
{
if (isset($keywords[$word]))
{
$keywords[$word]++;
} else {
$keywords[$word] = 1;
}
}
unset($words[$key]);
}
arsort($keywords);
if($count > 0)
{
$keywords = array_slice($keywords, 0, $count);
}
return $keywords;
}
/**
* Returns given number of keywords as serialized keywords
* @param int
* @return array
*/
public function getKeywords($count = 0)
{
$keywords = $this->getKeywordsArray($count);
$keywords = implode(", ", array_keys($keywords));
return $keywords;
}
/**
* Returns description of given length
* Note: This function doesnt cut words. It returns as many words as possible.
* @param int
* @return array
*/
public function getDescription($len = 0)
{
if ($len < 1) $len = $this->defaultDescLength;
$text = strip_tags($this->text);
$text = preg_replace('/s+/', ' ', $text);
if(strlen($text) > $len)
{
return $text = substr($text, 0, strpos($text, ' ', $len));
}
else
{
return $text;
}
}
/**
* Return google pagerank for any website. [valid URL required]
* @param string website URL
* @return int google pagerank
*/
public function googlePagerank($page)
{
// Open a socket to the toolbarqueries address, used by Google Toolbar
$socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
// If a connection can be established
if($socket)
{
// Prep socket headers
$out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page)).
"&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1rn";
$out .= "Host: toolbarqueries.google.comrn";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)rn";
$out .= "Connection: Closernrn";
// Write settings to the socket
fwrite($socket, $out);
// When a response is received...
$result = "";
while(!feof($socket))
{
$data = fgets($socket, 128);
$pos = strpos($data, "Rank_");
if($pos !== false)
{
$pagerank = substr($data, $pos + 9);
$result += $pagerank;
}
}
// Close the connection
fclose($socket);
// Return the rank!
return $result;
}
}
/**
* THOSE ARE PRIVATE FUNCTIONS REQUIRED FOR THE CLASS TO WORK PROPERLY
*/
/**
* Works the same as str_replace does with one main difference. This function only replaces whole words!!
*/
private function str_replace_word($needle,$replacement,$haystack)
{
$pattern = "/b$needleb/i";
$haystack = preg_replace($pattern, $replacement, $haystack);
return $haystack;
}
/**
* Converts string to a number (required for google pagerank check)
*/
private function stringToNumber($string,$check,$magic)
{
$int32 = 4294967296; // 2^32
$length = strlen($string);
for ($i = 0; $i < $length; $i++)
{
$check *= $magic;
//If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
// the result of converting to integer is undefined
// refer to http://www.php.net/manual/en/language.types.integer.php
if($check >= $int32)
{
$check = ($check - $int32 * (int) ($check / $int32));
//if the check less than -2^31
$check = ($check < -($int32 / 2)) ? ($check + $int32) : $check;
}
$check += ord($string{$i});
}
return $check;
}
/**
* Create url hash. (required for google pagerank check)
*/
private function createHash($string)
{
$check1 = $this->stringToNumber($string, 0x1505, 0x21);
$check2 = $this->stringToNumber($string, 0, 0x1003F);
$factor = 4;
$halfFactor = $factor/2;
$check1 >>= $halfFactor;
$check1 = (($check1 >> $factor) & 0x3FFFFC0 ) | ($check1 & 0x3F);
$check1 = (($check1 >> $factor) & 0x3FFC00 ) | ($check1 & 0x3FF);
$check1 = (($check1 >> $factor) & 0x3C000 ) | ($check1 & 0x3FFF);
$calc1 = (((($check1 & 0x3C0) << $factor) | ($check1 & 0x3C)) << $halfFactor ) | ($check2 & 0xF0F );
$calc2 = (((($check1 & 0xFFFFC000) << $factor) | ($check1 & 0x3C00)) << 0xA) | ($check2 & 0xF0F0000 );
return ($calc1 | $calc2);
}
/**
* Check the hash. (required for google pagerak check)
*/
private function checkHash($hashNumber)
{
$check = 0;
$flag = 0;
$hashString = sprintf('%u', $hashNumber) ;
$length = strlen($hashString);
for ($i = $length - 1; $i >= 0; $i --)
{
$r = $hashString{$i};
if(1 === ($flag % 2))
{
$r += $r;
$r = (int)($r / 10) + ($r % 10);
}
$check += $r;
$flag ++;
}
$check %= 10;
if(0 !== $check)
{
$check = 10 - $check;
if(1 === ($flag % 2) )
{
if(1 === ($check % 2))
{
$check += 9;
}
$check >>= 1;
}
}
return '7'.$check.$hashString;
}
}