| 1 |
<? |
|---|
| 2 |
/* |
|---|
| 3 |
--------------------------------------------------------------------------- |
|---|
| 4 |
VoicePulse Global Provisioning System (VGPS) |
|---|
| 5 |
by Ketan Patel |
|---|
| 6 |
Copyright (C) 2006 VoicePulse Inc. |
|---|
| 7 |
|
|---|
| 8 |
This program is free software, distributed under the terms of the GNU General |
|---|
| 9 |
Public License Version 2. See the LICENSE file at the top of the source tree. |
|---|
| 10 |
--------------------------------------------------------------------------- |
|---|
| 11 |
*/ |
|---|
| 12 |
|
|---|
| 13 |
require('XPath.class.php'); |
|---|
| 14 |
|
|---|
| 15 |
$link = mysql_connect( |
|---|
| 16 |
'localhost', |
|---|
| 17 |
'root', |
|---|
| 18 |
'passw0rd') |
|---|
| 19 |
or die('Could not connect: ' . mysql_error()); |
|---|
| 20 |
|
|---|
| 21 |
mysql_select_db('voicepulse_global_provisioning_system'); |
|---|
| 22 |
|
|---|
| 23 |
switch($op) { |
|---|
| 24 |
case "set_device": |
|---|
| 25 |
set_device($model, $mac, $location, $note); |
|---|
| 26 |
regen($mac); |
|---|
| 27 |
break; |
|---|
| 28 |
case "delete_device": |
|---|
| 29 |
delete_device($mac); |
|---|
| 30 |
break; |
|---|
| 31 |
case "set_setting": |
|---|
| 32 |
set_setting($mac, $key, $value); |
|---|
| 33 |
regen($mac); |
|---|
| 34 |
break; |
|---|
| 35 |
case "delete_setting": |
|---|
| 36 |
delete_setting($mac, $key); |
|---|
| 37 |
regen($mac); |
|---|
| 38 |
break; |
|---|
| 39 |
case "regen": |
|---|
| 40 |
regen($mac); |
|---|
| 41 |
break; |
|---|
| 42 |
case "list_devices": |
|---|
| 43 |
list_devices(); |
|---|
| 44 |
break; |
|---|
| 45 |
case "list_device_settings": |
|---|
| 46 |
list_device_settings($mac); |
|---|
| 47 |
break; |
|---|
| 48 |
case "list_models": |
|---|
| 49 |
list_models(); |
|---|
| 50 |
break; |
|---|
| 51 |
case "list_generic_settings": |
|---|
| 52 |
list_generic_settings(); |
|---|
| 53 |
break; |
|---|
| 54 |
default: |
|---|
| 55 |
echo "ERROR: Operation $op not defined. It should be set to set_device, delete_device, set_setting, or delete_setting.\n"; |
|---|
| 56 |
break; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
mysql_close($link); |
|---|
| 60 |
|
|---|
| 61 |
function replace_vars($string, $mac) |
|---|
| 62 |
{ |
|---|
| 63 |
$string = str_replace("[MAC]", strtoupper($mac), $string); |
|---|
| 64 |
$string = str_replace("[mac]", strtolower($mac), $string); |
|---|
| 65 |
return $string; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
function check_model($model) |
|---|
| 69 |
{ |
|---|
| 70 |
if(!$model) { |
|---|
| 71 |
echo "ERROR: \$model not defined. It should be set to the models.tag value (eg. linksys_pap2t).\n"; |
|---|
| 72 |
exit; |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
function check_mac($mac) |
|---|
| 77 |
{ |
|---|
| 78 |
if(!$mac) { |
|---|
| 79 |
echo "ERROR: \$mac not defined. It should be set to the MAC address of the device (eg. 000E01234567).\n"; |
|---|
| 80 |
exit; |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
function check_key($key) |
|---|
| 85 |
{ |
|---|
| 86 |
if(!$key) { |
|---|
| 87 |
echo "ERROR: \$key not defined. It should be set to the generic setting tag (eg. line_01_proxy).\n"; |
|---|
| 88 |
exit; |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
function check_value($value) |
|---|
| 93 |
{ |
|---|
| 94 |
if(is_null($value)) { |
|---|
| 95 |
echo "ERROR: \$value not defined. It should be set to the value to set (eg. sip.voicepulse.com).\n"; |
|---|
| 96 |
exit; |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
function set_device($model, $mac, $location, $note) |
|---|
| 101 |
{ |
|---|
| 102 |
check_model($model); |
|---|
| 103 |
check_mac($mac); |
|---|
| 104 |
|
|---|
| 105 |
$query = "SELECT model_id FROM models WHERE tag = '$model'"; |
|---|
| 106 |
$result = mysql_query($query); |
|---|
| 107 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 108 |
$model_id = $line["model_id"]; |
|---|
| 109 |
|
|---|
| 110 |
if(!$model_id) { |
|---|
| 111 |
echo "ERROR: models.tag = $model was not found in the database.\n"; |
|---|
| 112 |
exit; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
$location = mysql_escape_string($location); |
|---|
| 116 |
$note = mysql_escape_string($note); |
|---|
| 117 |
|
|---|
| 118 |
$query = "INSERT INTO devices (model_id, mac_address, location, note) VALUES ($model_id, '$mac', '$location', '$note') ON DUPLICATE KEY UPDATE location = '$location', note = '$note'"; |
|---|
| 119 |
$result = mysql_query($query); |
|---|
| 120 |
echo "SUCCESS\n"; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
function delete_device($mac) |
|---|
| 124 |
{ |
|---|
| 125 |
check_mac($mac); |
|---|
| 126 |
|
|---|
| 127 |
$query = "SELECT device_id FROM devices WHERE mac_address = '$mac'"; |
|---|
| 128 |
$result = mysql_query($query); |
|---|
| 129 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 130 |
$device_id = $line["device_id"]; |
|---|
| 131 |
|
|---|
| 132 |
if(!$device_id || $device_id <= 0) { |
|---|
| 133 |
echo "ERROR: $mac is not in the database.\n"; |
|---|
| 134 |
exit; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
$query = "DELETE FROM device_settings WHERE device_id = $device_id"; |
|---|
| 138 |
$result = mysql_query($query); |
|---|
| 139 |
$query = "DELETE FROM devices WHERE device_id = $device_id"; |
|---|
| 140 |
$result = mysql_query($query); |
|---|
| 141 |
|
|---|
| 142 |
echo "SUCCESS\n"; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
function set_setting($mac, $key, $value) |
|---|
| 146 |
{ |
|---|
| 147 |
check_mac($mac); |
|---|
| 148 |
check_key($key); |
|---|
| 149 |
check_value($value); |
|---|
| 150 |
|
|---|
| 151 |
$query = "SELECT model_id, device_id FROM devices WHERE mac_address = '$mac'"; |
|---|
| 152 |
$result = mysql_query($query); |
|---|
| 153 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 154 |
$model_id = $line["model_id"]; |
|---|
| 155 |
$device_id = $line["device_id"]; |
|---|
| 156 |
|
|---|
| 157 |
if(!$device_id || $device_id <= 0) { |
|---|
| 158 |
echo "ERROR: $mac is not in the database.\n"; |
|---|
| 159 |
exit; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
$query = "SELECT generic_settings.generic_setting_id, generic_mappings.profile_key FROM generic_settings INNER JOIN generic_mappings on generic_settings.generic_setting_id = generic_mappings.generic_setting_id WHERE generic_mappings.model_id = $model_id AND generic_settings.tag = '$key'"; |
|---|
| 163 |
$result = mysql_query($query); |
|---|
| 164 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 165 |
$generic_setting_id = $line["generic_setting_id"]; |
|---|
| 166 |
$profile_key = $line["profile_key"]; |
|---|
| 167 |
|
|---|
| 168 |
if(!$generic_setting_id || $generic_setting_id <= 0) { |
|---|
| 169 |
echo "ERROR: $key has not been mapped to a profile_key.\n"; |
|---|
| 170 |
exit; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
if($profile_key) { |
|---|
| 174 |
$key = $profile_key; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
$query = "SELECT generic_translations.profile_value FROM generic_translations WHERE model_id = $model_id AND generic_setting_id = $generic_setting_id AND generic_value = '$value'"; |
|---|
| 178 |
$result = mysql_query($query); |
|---|
| 179 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 180 |
$profile_value = $line["profile_value"]; |
|---|
| 181 |
|
|---|
| 182 |
if(!is_null($profile_value) && strlen($profile_value) > 0) { |
|---|
| 183 |
$value = $profile_value; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
$query = "INSERT INTO device_settings (device_id, profile_key, profile_value) VALUES ($device_id, '$key', '$value') ON DUPLICATE KEY UPDATE profile_value = '$value'"; |
|---|
| 187 |
$result = mysql_query($query); |
|---|
| 188 |
|
|---|
| 189 |
echo "SUCCESS\n"; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
function delete_setting($mac, $key) |
|---|
| 193 |
{ |
|---|
| 194 |
check_mac($mac); |
|---|
| 195 |
check_key($key); |
|---|
| 196 |
|
|---|
| 197 |
$query = "SELECT model_id, device_id FROM devices WHERE mac_address = '$mac'"; |
|---|
| 198 |
$result = mysql_query($query); |
|---|
| 199 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 200 |
$model_id = $line["model_id"]; |
|---|
| 201 |
$device_id = $line["device_id"]; |
|---|
| 202 |
|
|---|
| 203 |
if(!$device_id || $device_id <= 0) { |
|---|
| 204 |
echo "ERROR: $mac is not in the database.\n"; |
|---|
| 205 |
exit; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
$query = "SELECT generic_settings.generic_setting_id, generic_mappings.profile_key FROM generic_settings INNER JOIN generic_mappings on generic_settings.generic_setting_id = generic_mappings.generic_setting_id WHERE generic_mappings.model_id = $model_id AND generic_settings.tag = '$key'"; |
|---|
| 209 |
$result = mysql_query($query); |
|---|
| 210 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 211 |
$generic_setting_id = $line["generic_setting_id"]; |
|---|
| 212 |
$profile_key = $line["profile_key"]; |
|---|
| 213 |
|
|---|
| 214 |
if(!$generic_setting_id || $generic_setting_id <= 0) { |
|---|
| 215 |
echo "ERROR: $key has not been mapped to a profile_key.\n"; |
|---|
| 216 |
exit; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
if($profile_key) { |
|---|
| 220 |
$key = $profile_key; |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
$query = "DELETE FROM device_settings WHERE device_id = $device_id AND profile_key = '$key'"; |
|---|
| 224 |
$result = mysql_query($query); |
|---|
| 225 |
echo "SUCCESS\n"; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
function regen($mac) |
|---|
| 229 |
{ |
|---|
| 230 |
check_mac($mac); |
|---|
| 231 |
|
|---|
| 232 |
$query = "SELECT devices.device_id, models.model_id, models.format, models.regex, models.input_filename, models.output_filename, models.post_processing FROM devices INNER JOIN models ON devices.model_id = models.model_id WHERE devices.mac_address = '$mac'"; |
|---|
| 233 |
$result = mysql_query($query); |
|---|
| 234 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 235 |
$device_id = $line["device_id"]; |
|---|
| 236 |
$format = $line["format"]; |
|---|
| 237 |
$regex = $line["regex"]; |
|---|
| 238 |
$input_filename = $line["input_filename"]; |
|---|
| 239 |
$output_filename = $line["output_filename"]; |
|---|
| 240 |
$post_processing = $line["post_processing"]; |
|---|
| 241 |
$output_filename = replace_vars($output_filename, $mac); |
|---|
| 242 |
|
|---|
| 243 |
if(!file_exists($input_filename)) { |
|---|
| 244 |
echo "ERROR: Input filename ($input_filename) does not exist.\n"; |
|---|
| 245 |
exit; |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
$contents = file_get_contents($input_filename); |
|---|
| 249 |
$contents_array = file($input_filename); |
|---|
| 250 |
|
|---|
| 251 |
$settings = array(); |
|---|
| 252 |
$query = "SELECT profile_key, profile_value FROM device_settings WHERE device_id = $device_id"; |
|---|
| 253 |
$result = mysql_query($query); |
|---|
| 254 |
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) { |
|---|
| 255 |
$settings[$line["profile_key"]] = $line["profile_value"]; |
|---|
| 256 |
} |
|---|
| 257 |
switch($format) { |
|---|
| 258 |
case "TEXT": |
|---|
| 259 |
$output = ""; |
|---|
| 260 |
foreach($contents_array as $line) { |
|---|
| 261 |
preg_match($regex, $line, $match); |
|---|
| 262 |
if(array_key_exists($match[profile_key], $settings)) { |
|---|
| 263 |
$line = str_replace($match[profile_value], $settings[$match[profile_key]], $line); |
|---|
| 264 |
unset($settings[$match[profile_key]]); |
|---|
| 265 |
} |
|---|
| 266 |
$output .= $line; |
|---|
| 267 |
} |
|---|
| 268 |
|
|---|
| 269 |
$handle = fopen($output_filename, 'w+'); |
|---|
| 270 |
fwrite($handle, $output); |
|---|
| 271 |
fclose($handle); |
|---|
| 272 |
|
|---|
| 273 |
if(count($settings) > 0) { |
|---|
| 274 |
$errors = ""; |
|---|
| 275 |
foreach($settings as $key=>$val) { |
|---|
| 276 |
$errors .= $key.","; |
|---|
| 277 |
} |
|---|
| 278 |
$errors = rtrim($errors, ","); |
|---|
| 279 |
echo "ERROR: Profile keys ($errors) not found in template.\n"; |
|---|
| 280 |
exit; |
|---|
| 281 |
} |
|---|
| 282 |
break; |
|---|
| 283 |
case "XML": |
|---|
| 284 |
$xmlOpt = array(XML_OPTION_CASE_FOLDING => FALSE, XML_OPTION_SKIP_WHITE => TRUE); |
|---|
| 285 |
$xPath = new XPath(); |
|---|
| 286 |
$xPath->importFromString($contents); |
|---|
| 287 |
foreach($settings as $key=>$val) { |
|---|
| 288 |
if(strpos($key, "@") > 0) { |
|---|
| 289 |
list($node, $attr) = explode("@", $key); |
|---|
| 290 |
$node = rtrim($node, "/"); |
|---|
| 291 |
$result = $xPath->getAttributes($node, $attr); |
|---|
| 292 |
if(!$result) { |
|---|
| 293 |
echo "ERROR: Attribute $attr of node $node was not found.\n"; |
|---|
| 294 |
exit; |
|---|
| 295 |
} else { |
|---|
| 296 |
$return = $xPath->setAttribute($node, $attr, $val); |
|---|
| 297 |
if(!$return) { |
|---|
| 298 |
echo "ERROR: Could not set value for attribute $attr of node $node.\n"; |
|---|
| 299 |
} |
|---|
| 300 |
} |
|---|
| 301 |
} else { |
|---|
| 302 |
$result = $xPath->match($key); |
|---|
| 303 |
$xPath->replaceData($key, $val); |
|---|
| 304 |
} |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
$xPath->exportToFile($output_filename); |
|---|
| 308 |
break; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
if($post_processing && strlen($post_processing) > 0) { |
|---|
| 312 |
$post_processing = replace_vars($post_processing, $mac); |
|---|
| 313 |
exec($post_processing); |
|---|
| 314 |
} |
|---|
| 315 |
echo "SUCCESS\n"; |
|---|
| 316 |
} |
|---|
| 317 |
|
|---|
| 318 |
function list_devices() |
|---|
| 319 |
{ |
|---|
| 320 |
$query = "SELECT mac_address, location, note, models.name, models.tag FROM devices INNER JOIN models ON devices.model_id = models.model_id ORDER BY mac_address"; |
|---|
| 321 |
$result = mysql_query($query); |
|---|
| 322 |
$cols = ""; |
|---|
| 323 |
$rows = ""; |
|---|
| 324 |
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) { |
|---|
| 325 |
$cols = implode("||", array_keys($line)) . "\n"; |
|---|
| 326 |
$rows .= implode("||", $line) . "\n"; |
|---|
| 327 |
} |
|---|
| 328 |
echo $cols; |
|---|
| 329 |
echo $rows; |
|---|
| 330 |
} |
|---|
| 331 |
|
|---|
| 332 |
function list_device_settings($mac) |
|---|
| 333 |
{ |
|---|
| 334 |
check_mac($mac); |
|---|
| 335 |
|
|---|
| 336 |
$query = "SELECT model_id, device_id FROM devices WHERE mac_address = '$mac'"; |
|---|
| 337 |
$result = mysql_query($query); |
|---|
| 338 |
$line = mysql_fetch_array($result, MYSQL_ASSOC); |
|---|
| 339 |
$model_id = $line["model_id"]; |
|---|
| 340 |
$device_id = $line["device_id"]; |
|---|
| 341 |
|
|---|
| 342 |
if(!$device_id || $device_id <= 0) { |
|---|
| 343 |
echo "ERROR: $mac is not in the database.\n"; |
|---|
| 344 |
exit; |
|---|
| 345 |
} |
|---|
| 346 |
|
|---|
| 347 |
$query = "SELECT profile_key, profile_value FROM device_settings INNER JOIN devices ON device_settings.device_id = devices.device_id WHERE devices.mac_address = '$mac'"; |
|---|
| 348 |
$result = mysql_query($query); |
|---|
| 349 |
|
|---|
| 350 |
$settings = array(); |
|---|
| 351 |
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) { |
|---|
| 352 |
$settings[$line[profile_key]] = $line[profile_value]; |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
$query = "SELECT generic_settings.generic_setting_id, generic_settings.tag AS to_key, generic_mappings.profile_key as from_key FROM generic_settings INNER JOIN generic_mappings ON generic_settings.generic_setting_id = generic_mappings.generic_setting_id WHERE model_id = $model_id"; |
|---|
| 356 |
$result = mysql_query($query); |
|---|
| 357 |
|
|---|
| 358 |
$output_settings = array(); |
|---|
| 359 |
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) { |
|---|
| 360 |
if(array_key_exists($line[from_key], $settings)) { |
|---|
| 361 |
$output_settings[$line[to_key]] = $settings[$line[from_key]]; |
|---|
| 362 |
|
|---|
| 363 |
$query2 = "SELECT generic_value FROM generic_translations WHERE model_id = $model_id AND generic_setting_id = $line[generic_setting_id] AND profile_value = '".$settings[$line[from_key]]."'"; |
|---|
| 364 |
$result2 = mysql_query($query2); |
|---|
| 365 |
$line2 = mysql_fetch_array($result2, MYSQL_ASSOC); |
|---|
| 366 |
if(!is_null($line2[generic_value])) { |
|---|
| 367 |
$output_settings[$line[to_key]] = $line2[generic_value]; |
|---|
| 368 |
} |
|---|
| 369 |
|
|---|
| 370 |
unset($settings[$line[from_key]]); |
|---|
| 371 |
} |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
foreach($settings as $key=>$val) { |
|---|
| 375 |
$output_settings[$key] = $val; |
|---|
| 376 |
} |
|---|
| 377 |
|
|---|
| 378 |
foreach($output_settings as $key=>$val) { |
|---|
| 379 |
echo "$key||$val\n"; |
|---|
| 380 |
} |
|---|
| 381 |
} |
|---|
| 382 |
|
|---|
| 383 |
function list_models() |
|---|
| 384 |
{ |
|---|
| 385 |
$query = "SELECT models.name, models.tag FROM models ORDER BY name"; |
|---|
| 386 |
$result = mysql_query($query); |
|---|
| 387 |
$cols = ""; |
|---|
| 388 |
$rows = ""; |
|---|
| 389 |
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) { |
|---|
| 390 |
$cols = implode("||", array_keys($line)) . "\n"; |
|---|
| 391 |
$rows .= implode("||", $line) . "\n"; |
|---|
| 392 |
} |
|---|
| 393 |
echo $cols; |
|---|
| 394 |
echo $rows; |
|---|
| 395 |
} |
|---|
| 396 |
|
|---|
| 397 |
function list_generic_settings() |
|---|
| 398 |
{ |
|---|
| 399 |
$query = "SELECT name, tag FROM generic_settings ORDER BY name"; |
|---|
| 400 |
$result = mysql_query($query); |
|---|
| 401 |
$cols = ""; |
|---|
| 402 |
$rows = ""; |
|---|
| 403 |
while($line = mysql_fetch_array($result, MYSQL_ASSOC)) { |
|---|
| 404 |
$cols = implode("||", array_keys($line)) . "\n"; |
|---|
| 405 |
$rows .= implode("||", $line) . "\n"; |
|---|
| 406 |
} |
|---|
| 407 |
echo $cols; |
|---|
| 408 |
echo $rows; |
|---|
| 409 |
} |
|---|
| 410 |
?> |
|---|