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

Contents of /contributions/modules/whois/whois.module

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


Revision 1.3 - (show annotations) (download) (as text)
Tue Aug 12 11:15:57 2008 UTC (15 months, 2 weeks ago) by Gurpartap
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, DRUPAL-6--1-1, HEAD
Changes since 1.2: +153 -53 lines
File MIME type: text/x-php
Whois lookup 6.x-1.0
1 <?php
2 // $Id: whois.module,v 1.1.2.1 2007/03/21 15:33:04 Gurpartap Exp $
3
4 /**
5 * Implementation of hook_requirements()
6 */
7 function whois_requirements($phase) {
8 $requirements = array();
9 $t = get_t();
10 switch ($phase) {
11 case 'install' :
12 case 'runtime' :
13 $path = drupal_get_path('module', 'whois') . '/phpwhois/whois.main.php';
14 if (!file_exists($path)) {
15 $requirements['whois'] = array(
16 'title' => $t('Whois lookup'),
17 'description' => $t("Whois module requires !phpwhois to do whois queries. !download and put it's contents in <em>modules/whois</em> directory (so that it looks something like this: <em>modules/whois/phpwhois/example.php</em>).", array('!phpwhois' => l('phpWhois library', 'http://phpwhois.sf.net/'), '!download' => l('Download phpWhois', 'http://sourceforge.net/project/showfiles.php?group_id=31207&package_id=23260'))),
18 'severity' => $phase == 'install' ? REQUIREMENT_WARNING : REQUIREMENT_ERROR,
19 'value' => $t('!phpwhois library missing', array('!phpwhois' => l('phpWhois', 'http://phpwhois.sf.net/'))),
20 );
21 }
22 }
23 return $requirements;
24 }
25
26 /**
27 * Implementation of hook_perm()
28 */
29 function whois_perm() {
30 return array('access whois');
31 }
32
33 /**
34 * Implementation of hook_menu()
35 */
36 function whois_menu() {
37 $items = array();
38
39 $items['whois'] = array(
40 'title' => t('Whois lookup'),
41 'page callback' => 'whois_whois_page',
42 'access arguments' => array('access whois'),
43 'type' => MENU_NORMAL_ITEM,
44 );
45 $items['admin/settings/whois'] = array(
46 'title' => t('Whois lookup'),
47 'description' => t('Configure Whois lookup output style, dynamic(AJAX) results, hourly threshold, and more.'),
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('whois_settings'),
50 'access arguments' => array('administer site configuration'),
51 'type' => MENU_NORMAL_ITEM,
52 );
53
54 return $items;
55 }
56
57 function whois_settings() {
58 $form = array();
59
60 $form['whois_settings'] = array(
61 '#type' => 'fieldset',
62 '#title' => t('Basic configuration'),
63 '#collapsed' => TRUE,
64 );
65 $form['whois_settings']['whois_output_method'] = array(
66 '#type' => 'radios',
67 '#title' => t('Output method'),
68 '#default_value' => variable_get('whois_output_method', 'html'),
69 '#description' => t(''),
70 '#options' => array(
71 'basic' => 'Basic',
72 'html' => 'HTMLized',
73 'object' => 'PHP object',
74 ),
75 );
76 $form['whois_settings']['whois_enable_ajax'] = array(
77 '#type' => 'checkbox',
78 '#title' => t('Dynamic lookup (AJAX)'),
79 '#default_value' => variable_get('whois_enable_ajax', 1),
80 '#description' => t('Lookup and view the whois results dynamically (i.e. without reloading page) using AJAX request.'),
81 );
82 $form['whois_settings']['whois_hourly_threshold'] = array('#type' => 'select',
83 '#title' => t('Hourly threshold'),
84 '#options' => drupal_map_assoc(array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59)),
85 '#default_value' => variable_get('whois_hourly_threshold', 13),
86 '#description' => t('The maximum number of whois lookups a user can perform per hour.'),
87 );
88 $form['whois_log_watchdog'] = array(
89 '#type' => 'checkbox',
90 '#title' => t('Log watchdog entry'),
91 '#default_value' => variable_get('whois_log_watchdog', 1),
92 '#description' => t('Log a watchdog entry for each whois lookup performed.'),
93 );
94 return system_settings_form($form);
95 }
96
97 function whois_whois_page() {
98 $output = '';
99 $address = $_POST['address'] ? whois_parse_url($_POST['address']) : arg(1);
100
101 if (isset($address)) {
102 // Check for hourly threshold.
103 if (flood_is_allowed('whois', variable_get('whois_hourly_threshold', 13))) {
104 $output .= '<h3>' . t('Whois lookup for %address:', array('%address' => $address)) . '</h3>';
105 $output .= whois_get_whois($address);
106 }
107 else {
108 $output .= t("You cannot do more than %number whois lookups per hour. Please try again later.", array('%number' => variable_get('whois_hourly_threshold', 13)));
109 }
110 if (isset($_POST['address'])) {
111 // Avoid debug information(devel.module) from being added to the preview.
112 $GLOBALS['devel_shutdown'] = FALSE;
113
114 if (variable_get('whois_log_watchdog', 1)) {
115 // Watchdog entry for lookup request.
116 watchdog('whois',
117 t('Whois lookup for: %address', array('%address' => $address)),
118 array($address), WATCHDOG_NOTICE, l('View', "whois/$address") . ' · ' . l('Address', "http://$address/"));
119 }
120
121 // Stop further processing and return requested data.
122 exit(drupal_json(array('html' => $output)));
123 }
124 drupal_set_breadcrumb(array(l(t('Home'), '<front>'), l(t('Whois lookup'), 'whois')));
125 }
126 // Load JS and CSS for dynamic lookups using AJAX.
127 if (variable_get('whois_enable_ajax', 1)) {
128 $path = drupal_get_path('module', 'whois');
129 drupal_add_js($path . '/whois.js');
130 drupal_add_css($path . '/whois.css');
131 }
132
133 return drupal_get_form('whois_whois_form') . '<div id="live-preview-container"><div id="live-whois-preview">' . $output . '</div><div id="live-whois-preview-background"></div></div>';
134 }
135
136 function whois_whois_form() {
137 $form = array();
138
139 $form['whois_lookup'] = array(
140 '#type' => 'fieldset',
141 '#collapsed' => TRUE,
142 );
143 $form['whois_lookup']['whois_address'] = array(
144 '#type' => 'textfield',
145 '#title' => t('Lookup address'),
146 '#default_value' => arg(1),
147 '#required' => TRUE,
148 '#prefix' => '<div class="container-inline">',
149 );
150 $form['whois_lookup']['whois_submit'] = array(
151 '#type' => 'submit',
152 '#value' => t('Lookup'),
153 '#suffix' => '</div>',
154 );
155 $form['whois_lookup']['whois_description'] = array(
156 '#value' => '<div class="description" style="margin: 0;">' . t('Enter a domain name or IP address for <em>whois</em> information.') . '</div>',
157 );
158
159 return $form;
160 }
161
162 function whois_whois_form_submit($form, &$form_state) {
163 global $user;
164 $address = whois_parse_url($form_state['values']['whois_address']);
165
166 if (variable_get('whois_log_watchdog', 1)) {
167 // Watchdog entry for lookup request.
168 watchdog('whois',
169 t('Whois lookup for: %address', array('%address' => $address)),
170 array($address), WATCHDOG_NOTICE, l('View', "whois/$address") . ' · ' . l('Address', "http://$address/"));
171 }
172
173 $form_state['redirect'] = 'whois/' . $address;
174 return;
175 }
176
177 function whois_get_whois($address) {
178 $data = '';
179 $path = drupal_get_path('module', 'whois') . '/phpwhois/whois.main.php';
180
181 if (!file_exists($path)) {
182 drupal_set_message(t('There are problems with <em>Whois lookup</em> setup. Report to the website administrators promptly!', array('@status' => url('admin/logs/status'))), 'error');
183 }
184 elseif ($address != '') {
185 include_once('phpwhois/whois.main.php');
186 include_once('phpwhois/whois.utils.php');
187 $option = variable_get('whois_output_method', 'html');
188 $whois = new Whois();
189 $result = $whois->Lookup($address);
190
191 switch($option) {
192 case 'html':
193 if (!empty($result['rawdata'])) {
194 $utils = new utils;
195 $data .= $utils->showHTML($result);
196 }
197 else {
198 $data .= implode($whois->Query['errstr'],"\n<br></br>");
199 }
200 break;
201
202 case 'object':
203 if ($whois->Query['status'] < 0) {
204 $data .= implode($whois->Query['errstr'],"\n<br></br>");
205 }
206 else {
207 $utils = new utils;
208 $data .= $utils->showObject($result);
209 }
210 break;
211
212 default:
213 if(!empty($result['rawdata'])) {
214 $data .= '<pre>' . implode($result['rawdata'],"\n") . '</pre>';
215 }
216 else {
217 $data .= implode($whois->Query['errstr'],"\n<br></br>");
218 }
219 break;
220 }
221 }
222
223 return $data;
224 }
225
226 function whois_parse_url($url) {
227 $r = "^(?:(?P<scheme>\w+)://)?";
228 $r .= "(?:(?P<login>\w+):(?P<pass>\w+)@)?";
229 $r .= "(?P<host>(?:(?P<subdomain>[\w\.]+)\.)?" . "(?P<domain>\w+\.(?P<extension>\w+)))";
230 $r .= "(?::(?P<port>\d+))?";
231 $r .= "(?P<path>[\w/]*/(?P<file>\w+(?:\.\w+)?)?)?";
232 $r .= "(?:\?(?P<arg>[\w=&]+))?";
233 $r .= "(?:#(?P<anchor>\w+))?";
234 $r = "!$r!"; // Delimiters
235
236 preg_match($r, $url, $result);
237
238 return $result[6];
239 }

  ViewVC Help
Powered by ViewVC 1.1.2