[44] | 1 | <?php
|
---|
| 2 | /*
|
---|
| 3 | * Position Yourself WP Plugin Classes
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | class Positioner {
|
---|
| 8 | var $start;
|
---|
| 9 | var $end;
|
---|
| 10 | var $region;
|
---|
| 11 | var $language;
|
---|
| 12 | var $meta = '';
|
---|
| 13 | var $num_results = 30;
|
---|
| 14 | var $queries = array();
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | class GoogleQuery {
|
---|
| 19 | /**
|
---|
| 20 | * @keyword string The keyword
|
---|
| 21 | * @region string The Google region (.com, .be, .nl, ...)
|
---|
| 22 | * @language string The Google language (hl=nl, hl=en, ...)
|
---|
| 23 | * @query string The Google keyword query string
|
---|
| 24 | * @url string The Google query url
|
---|
| 25 | * @position int The position of the domain for this keyword in Google
|
---|
| 26 | * @options string The Google options (meta=)
|
---|
| 27 | * @num_results int The Google number of results to keep track of
|
---|
| 28 | * @num_queries int The number of Google queries
|
---|
| 29 | * @results array The Google results
|
---|
| 30 | */
|
---|
| 31 | var $keyword;
|
---|
| 32 | var $region;
|
---|
| 33 | var $language;
|
---|
| 34 | var $query;
|
---|
| 35 | var $url = 'http://www.google.';
|
---|
| 36 | var $top_position = 0;
|
---|
| 37 | var $top_url = '';
|
---|
| 38 | var $meta = '';
|
---|
| 39 | var $num_results = 30;
|
---|
| 40 | var $num_queries = 0;
|
---|
| 41 | var $results = array();
|
---|
| 42 | var $matches = array();
|
---|
| 43 | var $competitors = array();
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * PHP 4 Compatible Constructor
|
---|
| 47 | */
|
---|
| 48 | function GoogleQuery($keyword, $region, $language, $num_results = 30, $options = '') {
|
---|
| 49 | $this->__construct($keyword, $region, $language, $num_results, $options);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * PHP 5 Constructor
|
---|
| 54 | */
|
---|
| 55 | function __construct($keyword, $region, $language, $num_results = 30, $meta = '') {
|
---|
| 56 |
|
---|
| 57 | $this->keyword = (string) trim($keyword);
|
---|
| 58 |
|
---|
| 59 | $this->region = (string) $region;
|
---|
| 60 | $this->language = (string) $language;
|
---|
| 61 |
|
---|
| 62 | $this->num_results = (int) $num_results;
|
---|
| 63 | $this->meta = (string) $meta;
|
---|
| 64 |
|
---|
| 65 | $this->query = (string) ( $this->build_query() !== false ) ? $this->build_query() : $this->keyword;
|
---|
| 66 | $this->url = (string) $this->build_url();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Get the results for a certain keyword
|
---|
| 71 | */
|
---|
| 72 | function set_results() {
|
---|
| 73 | // fetch the result for the url of this queryObject
|
---|
| 74 | $parse_result = array();
|
---|
| 75 | $parse_result = (array)$this->parse($this->url);
|
---|
| 76 |
|
---|
| 77 | if ( is_array($parse_result) && count($parse_result) > 0 ) {
|
---|
| 78 | // remove first one
|
---|
| 79 | array_splice( $parse_result, 0, 1);
|
---|
| 80 | // remove last two
|
---|
| 81 | array_splice( $parse_result, (count($parse_result)-2), 2);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if ( count($parse_result) > 0 ) $this->num_queries++;
|
---|
| 85 |
|
---|
| 86 | $this->results = (array) $parse_result;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * Parses an URL through Snoopy
|
---|
| 91 | * @return string
|
---|
| 92 | */
|
---|
| 93 | function parse($url) {
|
---|
| 94 | global $snoopy;
|
---|
| 95 |
|
---|
| 96 | $snoopy->results = '';
|
---|
| 97 | // set browser
|
---|
| 98 | $snoopy->agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8.1.13)";
|
---|
| 99 |
|
---|
| 100 | // set a raw-header
|
---|
| 101 | $snoopy->rawheaders["Pragma"] = "no-cache";
|
---|
| 102 | // fetch the links of the website
|
---|
| 103 | if ($snoopy->fetchlinks($url)) {
|
---|
| 104 | return $snoopy->results;
|
---|
| 105 | } else {
|
---|
| 106 | return $snoopy->error;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Returns a querified representation of a keyword
|
---|
| 113 | * @return string
|
---|
| 114 | */
|
---|
| 115 | function build_query() {
|
---|
| 116 |
|
---|
| 117 | $query_keyword = '';
|
---|
| 118 |
|
---|
| 119 | if ($this->keyword) {
|
---|
| 120 | $keyword = $this->keyword;
|
---|
| 121 | if (!preg_match('#\"(.*?)\"#i', $keyword) ) {
|
---|
| 122 | // split on spaces
|
---|
| 123 | $keywords = explode(' ', $keyword);
|
---|
| 124 | // url encode each part
|
---|
| 125 | $keywords = array_map("urlencode", $keywords);
|
---|
| 126 | // glue back together with +
|
---|
| 127 | $query = implode("+", $keywords);
|
---|
| 128 | } else {
|
---|
| 129 | $query = urlencode($keyword);
|
---|
| 130 | }
|
---|
| 131 | return $query;
|
---|
| 132 | } else
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /**
|
---|
| 137 | * Returns the url of the Google query
|
---|
| 138 | * @return string
|
---|
| 139 | */
|
---|
| 140 | function build_url() {
|
---|
| 141 |
|
---|
| 142 | $url = '';
|
---|
| 143 | $url = $this->url;
|
---|
| 144 | $url .= $this->region;
|
---|
| 145 | $url .= "/ie?hl=". $this->language;
|
---|
| 146 | $url .= "&q=".$this->query;
|
---|
| 147 | $url .= "&num=".$this->num_results;
|
---|
| 148 | $url .= "&sa=N";
|
---|
| 149 | $url .= "&filter=0";
|
---|
| 150 | $url .= $this->options;
|
---|
| 151 |
|
---|
| 152 | return $url;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * Returns matches in an array as an array
|
---|
| 157 | * @return array
|
---|
| 158 | */
|
---|
| 159 | function find_matches($results, $url) {
|
---|
| 160 | $matches = array();
|
---|
| 161 | $matches = preg_grep("#^$url#", $results);
|
---|
| 162 |
|
---|
| 163 | if ( count($matches) > 0 ) {
|
---|
| 164 | $this->matches = $matches;
|
---|
| 165 |
|
---|
| 166 | return $matches;
|
---|
| 167 | } else
|
---|
| 168 | return 0;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
| 172 | * Returns highest position in matches
|
---|
| 173 | * @return int
|
---|
| 174 | */
|
---|
| 175 | function find_top_position() {
|
---|
| 176 |
|
---|
| 177 | if ( count($this->matches) > 0 ) {
|
---|
| 178 | $top = 0;
|
---|
| 179 | while ($top == 0 && (list($position, $url) = each($this->matches))) {
|
---|
| 180 | $top = (int)((int)$position + 1);
|
---|
| 181 | $top_url = $url;
|
---|
| 182 | }
|
---|
| 183 | $this->top_position = $top;
|
---|
| 184 | $this->top_url = $top_url;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * Returns highest competitor position in competitormatches
|
---|
| 190 | * @return int
|
---|
| 191 | */
|
---|
| 192 | function find_top_competitor_position($results) {
|
---|
| 193 |
|
---|
| 194 | $top["position"] = 0;
|
---|
| 195 | $top["url"] = '';
|
---|
| 196 |
|
---|
| 197 | if ( is_array($results) ) {
|
---|
| 198 |
|
---|
| 199 | while ( $top["position"] == 0 && (list($position, $url) = each($results) ) ) {
|
---|
| 200 |
|
---|
| 201 | $top["position"] = (int)((int)$position + 1);
|
---|
| 202 | $top["url"] = $url;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | return $top;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /**
|
---|
| 209 | * Returns a string representation of a query
|
---|
| 210 | * @return string
|
---|
| 211 | */
|
---|
| 212 | function to_string($display = 'raw') {
|
---|
| 213 | switch($display) {
|
---|
| 214 | case "list": {
|
---|
| 215 | echo "<br />print as list";
|
---|
| 216 | }
|
---|
| 217 | break;
|
---|
| 218 |
|
---|
| 219 | case "row": {
|
---|
| 220 | echo "<br />print as table row";
|
---|
| 221 | }
|
---|
| 222 | break;
|
---|
| 223 |
|
---|
| 224 | case "raw": {
|
---|
| 225 | pyPre($this);
|
---|
| 226 | }
|
---|
| 227 | break;
|
---|
| 228 | }
|
---|
| 229 | //return $string;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | ?> |
---|