/[drupal]/contributions/modules/serapi/serapi.module
ViewVC logotype

Contents of /contributions/modules/serapi/serapi.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Wed Nov 4 22:32:30 2009 UTC (3 weeks, 2 days ago) by yaph
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA1, HEAD
Changes since 1.9: +4 -1 lines
File MIME type: text/x-php
Check whether HTTP_REFERER key of $_SERVER array is set before further processing. Added README.txt.
1 <?php
2 // $Id: serapi.module,v 1.9 2009/08/15 20:31:38 yaph Exp $
3
4 /**
5 * Returns an array with information on the performed search
6 * if the user comes from one of the supported search engines.
7 */
8 function serapi_get_search($referer = '') {
9 if (!$referer) {
10 if (!isset($_SERVER['HTTP_REFERER'])) {
11 return false;
12 }
13 $referer = $_SERVER['HTTP_REFERER'];
14 }
15
16 if (!valid_url($referer)) {
17 watchdog('serapi', 'The referring URL @url could not be validated', array('@url' => $referer));
18 return false;
19 }
20
21 $url_parts = parse_url($referer);
22 // only check urls that contain a query string, hell awaits when
23 // search engines start to use url rewriting for serps
24 if (is_array($url_parts) && isset($url_parts['host']) && isset($url_parts['query'])) {
25 $engines = serapi_get_engine_list();
26 // always check arrays before using them in foreach
27 if(!is_array($engines)) {
28 return false;
29 }
30 $host = $url_parts['host'];
31 $query = $url_parts['query'];
32 foreach($engines as $name => $prop) {
33 if (false !== stripos($host, $prop['host_pattern'])) {
34 $param = $prop['query_param'];
35 $query_parts = array();
36 parse_str($query, $query_parts);
37 // check that the query contains the search parameter
38 if (isset($query_parts[$param])) {
39 $search_string = $query_parts[$param];
40 // check whether there is more than one search keyword
41 if (false !== strpos($search_string, ' ')) {
42 $search['keywords'] = explode(' ', $search_string);
43 }
44 else {
45 $search['keywords'] = array($search_string);
46 }
47 $search['string'] = $search_string;
48 $search['url'] = $referer;
49 $search['engine'] = $name;
50 $search['host'] = $host;
51 return $search;
52 }
53 }
54 }
55 }
56 return false;
57 }
58
59 /**
60 * Returns an array of search engines supported
61 * by this module
62 *
63 * @return Array
64 * Array of search engines
65 */
66 function serapi_get_engine_list() {
67 return array(
68 'google' => array(
69 'host_pattern' => 'www.google.',
70 'query_param' => 'q'
71 ),
72 'yahoo' => array(
73 'host_pattern' => 'search.yahoo.com',
74 'query_param' => 'p'
75 ),
76 'bing' => array(
77 'host_pattern' => 'bing.com',
78 'query_param' => 'q'
79 ),
80 'ask' => array(
81 'host_pattern' => 'ask.com',
82 'query_param' => 'q'
83 ),
84 'aol.de' => array(
85 'host_pattern' => '.aol.de',
86 'query_param' => 'q'
87 ),
88 'aol' => array(
89 'host_pattern' => '.aol.',
90 'query_param' => 'query'
91 ),
92 'altavista' => array(
93 'host_pattern' => 'altavista.com',
94 'query_param' => 'q'
95 ),
96 'alltheweb' => array(
97 'host_pattern' => 'alltheweb.com',
98 'query_param' => 'q'
99 ),
100 );
101 }
102
103 /**
104 * Implementation of hook_block
105 */
106 function serapi_block($op = 'list', $delta = 0, $edit = array()) {
107 if ('list' == $op) {
108 $blocks = array();
109 $blocks[0] = array(
110 'info' => t('Search Engine Referer Information'),
111 'weight' => 0
112 );
113 return $blocks;
114 }
115 elseif ('view' == $op) {
116 switch($delta) {
117 case 0:
118 $block['content'] = serapi_block_search_engine_information();
119 $block['subject'] = t('Search Engine Referer Information');
120 break;
121 }
122 return $block;
123 }
124 }
125
126 function serapi_block_search_engine_information() {
127 $search_info = serapi_get_search();
128 if (false !== $search_info) {
129 return t('You came from @host and searched for !search_link.',
130 array('@host' => $search_info['host'],
131 '!search_link' => l(check_plain($search_info['string']),
132 check_url($search_info['url']),
133 array('attributes' => array('rel' => 'nofollow')))
134 )
135 );
136 }
137 return false;
138 }

  ViewVC Help
Powered by ViewVC 1.1.2