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

  ViewVC Help
Powered by ViewVC 1.1.2