| 1 |
<?php |
<?php |
| 2 |
// $Id: ipauth.install,v 1.8 2008/10/17 17:13:59 jonfrancisskydiver Exp $ |
// $Id$ |
|
// Modified 2008/09/15 antonio.spadial |
|
|
// Modified 2008/10/17 jonfrancisskydiver |
|
|
|
|
|
/*********************************************************************************** |
|
|
* C H A N G E S |
|
|
* 1. ip2long can return a negative value, so 'ip1' and 'ip2' fields can't be unsigned |
|
|
* 'unisgned' => FALSE in both fields in function ipauth_schema(...) |
|
|
* |
|
|
***********************************************************************************/ |
|
|
|
|
| 3 |
/** |
/** |
| 4 |
* Implementation of hook_install(). |
* IP based authenticator |
| 5 |
*/ |
* @author Jonathan T. Francis |
| 6 |
function ipauth_install() { |
* |
| 7 |
|
*/ |
|
if (!db_table_exists("ip_authenticator")) |
|
|
drupal_install_schema('ipauth'); |
|
|
|
|
|
cache_clear_all(); |
|
|
menu_rebuild(); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Implementation of hook_uninstall(). |
|
|
*/ |
|
|
function ipauth_uninstall() { |
|
|
if (db_table_exists("ip_authenticator")) { |
|
|
drupal_uninstall_schema('ipauth'); |
|
|
} |
|
|
|
|
|
cache_clear_all('*', 'cache', TRUE); |
|
|
cache_clear_all('*', 'cache_filter', TRUE); |
|
|
cache_clear_all('*', 'cache_menu', TRUE); |
|
|
cache_clear_all('*', 'cache_page', TRUE); |
|
|
} |
|
| 8 |
|
|
| 9 |
|
|
| 10 |
/** |
/** |
| 12 |
*/ |
*/ |
| 13 |
function ipauth_schema() { |
function ipauth_schema() { |
| 14 |
$schema['ip_authenticator'] = array( |
$schema['ip_authenticator'] = array( |
| 15 |
'description' => t('This table stores the single IP address and the IP ranges for specific user roles.'), |
'description' => t('This table stores the single IP address and the IP ranges for specific uid.'), |
| 16 |
'fields' => array( |
'fields' => array( |
| 17 |
'id' => array( |
'id' => array( |
| 18 |
'description' => t('contains the unique ID for this IP authenticator'), |
'description' => t('contains the unique ID for this IP authenticator'), |
| 21 |
'not null' => TRUE, |
'not null' => TRUE, |
| 22 |
'size' => 'normal' |
'size' => 'normal' |
| 23 |
), |
), |
| 24 |
|
'enabled' => array( |
| 25 |
|
'description' => t('marks an ip authenticator as enabled or disabled.'), |
| 26 |
|
'type' => 'int', |
| 27 |
|
'size' => 'tiny', |
| 28 |
|
'unsigned' => TRUE, |
| 29 |
|
'not null' => TRUE, |
| 30 |
|
'default' => 1 |
| 31 |
|
), |
| 32 |
'ip1' => array( |
'ip1' => array( |
| 33 |
'description' => t('contains the first IP address'), |
'description' => t('contains the first IP address'), |
| 34 |
'type' => 'int', |
'type' => 'int', |
| 41 |
'unsigned' => TRUE, |
'unsigned' => TRUE, |
| 42 |
'not null' => TRUE |
'not null' => TRUE |
| 43 |
), |
), |
| 44 |
'roles' => array( |
'uid' => array( |
| 45 |
'description' => t('Contains the role'), |
'description' => t('Contains the uid'), |
| 46 |
'type' => 'int', |
'type' => 'int', |
| 47 |
'size' => 'normal' |
'unsigned' => TRUE, |
| 48 |
|
'not null' => TRUE |
| 49 |
), |
), |
| 50 |
'description' => array( |
'description' => array( |
| 51 |
'description' => t('contains the description of the ip authenticator'), |
'description' => t('contains the description of the ip authenticator'), |
| 53 |
'length' => '255', |
'length' => '255', |
| 54 |
'not null' => FALSE, |
'not null' => FALSE, |
| 55 |
'default' => '', |
'default' => '', |
| 56 |
) |
), |
| 57 |
|
'created' => array( |
| 58 |
|
'description' => t('Stores the timestamp when authenticator was created'), |
| 59 |
|
'type' => 'datetime', |
| 60 |
|
), |
| 61 |
), |
), |
| 62 |
'primary key' => array('id') |
'primary key' => array('id') |
| 63 |
); |
); |
| 64 |
return $schema; |
return $schema; |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
* Implementation of hook_install(). |
| 69 |
|
*/ |
| 70 |
|
function ipauth_install() { |
| 71 |
|
|
| 72 |
|
if (!db_table_exists("ip_authenticator")) |
| 73 |
|
drupal_install_schema('ipauth'); |
| 74 |
|
|
| 75 |
|
cache_clear_all(); |
| 76 |
|
|
| 77 |
|
db_query("UPDATE {system} SET weight = 5 WHERE name = 'ipauth'"); |
| 78 |
|
menu_rebuild(); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
/** |
| 83 |
|
* This update is writen to add the description field into the module's database table. |
| 84 |
|
*/ |
| 85 |
function ipauth_update_1() { |
function ipauth_update_1() { |
| 86 |
|
$ret = array(); |
| 87 |
|
|
| 88 |
$result = db_query("ALTER TABLE {ipAuthenticator} RENAME TO {ip_authenticator}", TRUE); |
// Has the description field aready been added? if so, then don't run the sql statement and scare the user with a failed message. |
| 89 |
|
$result = db_query("SHOW COLUMNS FROM {ip_authenticator}"); |
| 90 |
|
while($column = db_fetch_array($result)) { |
| 91 |
|
if ('description' == $column['Field']) { |
| 92 |
|
$ret[] = array('success' => TRUE, 'query' => t('The column, description, already exists in the database. No update required.')); |
| 93 |
|
return $ret; |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
|
|
| 97 |
$ret = array(); |
$ret[] = update_sql("ALTER TABLE {ip_authenticator} ADD COLUMN description varchar(255) default '';"); |
| 98 |
$ret[] = array('success'=>$result!==FALSE, 'query'=>'Changed the name of ipAuthenticator table to ip_authenticator.'); |
|
| 99 |
|
// write the return array. |
| 100 |
|
drupal_set_message("IP authenticator update has ran and has completed its update on the database."); |
| 101 |
|
|
| 102 |
|
return $ret; |
| 103 |
} |
} |
| 104 |
|
|
| 105 |
/** |
/** |
| 113 |
while($column = db_fetch_array($result)) { |
while($column = db_fetch_array($result)) { |
| 114 |
if ('description' == $column['Field']) { |
if ('description' == $column['Field']) { |
| 115 |
$ret[] = array('success' => TRUE, 'query' => t('The column, description, already exists in the database. No update required.')); |
$ret[] = array('success' => TRUE, 'query' => t('The column, description, already exists in the database. No update required.')); |
|
return $ret; |
|
| 116 |
} |
} |
| 117 |
} |
} |
| 118 |
|
if (array() == $ret) |
| 119 |
|
$ret[] = update_sql("ALTER TABLE {ip_authenticator} ADD COLUMN description varchar(255) default '';"); |
| 120 |
|
|
| 121 |
|
|
| 122 |
// get the defined table structure. |
|
| 123 |
$schema = ipauth_schema(); |
$column = db_result(db_query("SHOW COLUMNS FROM ip_authenticator WHERE field='auth_to' AND type='varchar(255)'")); |
| 124 |
db_add_field($ret, 'ip_authenticator', 'description', $schema['ip_authenticator']['fields']['description']); |
if (!$column) { |
| 125 |
|
|
| 126 |
|
// We need to modify the column name and type. |
| 127 |
|
$ret[] = update_sql("ALTER TABLE {ip_authenticator} CHANGE roles auth_to VARCHAR(255) NOT NULL"); |
| 128 |
|
|
| 129 |
|
// Now update the prev. role id's to role names. |
| 130 |
|
$ret[] = update_sql(" |
| 131 |
|
/* USE name descriptions and not ids. */ |
| 132 |
|
UPDATE {ip_authenticator} SET auth_to = (SELECT CONCAT('Role: ', role.name) FROM role WHERE rid=ip_authenticator.auth_to) |
| 133 |
|
WHERE EXISTS (SELECT role.name FROM role WHERE rid=ip_authenticator.auth_to)"); |
| 134 |
|
} else { |
| 135 |
|
$ret[] = array("success" => TRUE, "query"=> t("The column roles has already been altered. No update required.")); |
| 136 |
|
|
| 137 |
|
$row = db_result(db_query("SELECT auth_to FROM {ip_authenticator} WHERE 1 LIMIT 1")); |
| 138 |
|
if ( "" == $row["auth_to"] || (int)$row["auth_to"] == $row["auth_to"] ) { |
| 139 |
|
// Now update the prev. role id's to role names. |
| 140 |
|
$ret[] = update_sql(" |
| 141 |
|
/* USE name descriptions and not ids. */ |
| 142 |
|
UPDATE ip_authenticator |
| 143 |
|
SET auth_to = ( |
| 144 |
|
SELECT CONCAT('Role: ', role.name) |
| 145 |
|
FROM role |
| 146 |
|
WHERE rid=ip_authenticator.auth_to |
| 147 |
|
) |
| 148 |
|
WHERE EXISTS ( |
| 149 |
|
SELECT CONCAT('Role: ', role.name) |
| 150 |
|
FROM role |
| 151 |
|
WHERE rid=ip_authenticator.auth_to |
| 152 |
|
) |
| 153 |
|
"); |
| 154 |
|
} |
| 155 |
|
} |
| 156 |
|
|
| 157 |
// write the return array. |
// write the return array. |
| 158 |
drupal_set_message("IP authenticator update has ran and has completed its update on the database."); |
drupal_set_message("IP authenticator update has ran and has completed its update on the database."); |
| 159 |
|
|
| 160 |
return $ret; |
return $ret; |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
function ipauth_update_3() { |
| 164 |
|
$ret = array(); |
| 165 |
|
|
| 166 |
|
$ret[] = update_sql("ALTER TABLE {ip_authenticator} CHANGE auth_to uid int(10) unsigned NOT NULL"); |
| 167 |
|
$ret[] = update_sql("UPDATE {system} SET weight = 5 WHERE name = 'ipauth'"); |
| 168 |
|
|
| 169 |
|
drupal_set_message("NOTE: WITH THIS UPDATE, YOU'LL NEED TO REASSIGN ALL AUTHENTICATORS TO A USER ACCOUNT"); |
| 170 |
|
|
| 171 |
|
menu_rebuild(); |
| 172 |
|
|
| 173 |
|
return $ret; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
/** |
| 177 |
|
* Implementation of hook_uninstall(). |
| 178 |
|
*/ |
| 179 |
|
function ipauth_uninstall() { |
| 180 |
|
if (db_table_exists("ip_authenticator")) { |
| 181 |
|
drupal_uninstall_schema('ipauth'); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
cache_clear_all('*', 'cache', TRUE); |
| 185 |
|
cache_clear_all('*', 'cache_filter', TRUE); |
| 186 |
|
cache_clear_all('*', 'cache_menu', TRUE); |
| 187 |
|
cache_clear_all('*', 'cache_page', TRUE); |
| 188 |
} |
} |