| 191 | | |
|---|
| 192 | | //check to see if were using the directory in ivr's, ensureing that we have ivr's |
|---|
| 193 | | $ivr_stat = $db->getOne('SELECT count(*) FROM modules WHERE modulename = "ivr" AND enabled = 1'); |
|---|
| 194 | | if(DB::IsError($q)) { |
|---|
| 195 | | die_freepbx(_('Error migrating to new directory! ERROR: Could not get Ivr status ' . $q->getDebugInfo())); |
|---|
| 196 | | } |
|---|
| 197 | | if ($ivr_stat == 1) { |
|---|
| 198 | | $count = $db->getOne('SELECT count(*) FROM ivr WHERE displayname != "__install_done"'); |
|---|
| 199 | | if(DB::IsError($count)) { |
|---|
| 200 | | die_freepbx(_('Error migrating to new directory! ERROR: Could not get count of Ivr\'s using the legacy Directory ' . $q->getDebugInfo())); |
|---|
| 201 | | } |
|---|
| 202 | | } else { |
|---|
| 203 | | $count = 0; |
|---|
| 204 | | } |
|---|
| 205 | | |
|---|
| 206 | | //Migrate ivr's if we have ivr's using directory |
|---|
| 207 | | if ($count > 0) { |
|---|
| 208 | | out(_("Migrating Ivr's to new Directory")); |
|---|
| 209 | | //if we were able to set a new directory |
|---|
| 210 | | if (isset($newdir) && $newdir){ |
|---|
| 211 | | //get al ivr's |
|---|
| 212 | | $ivrs = $db->getAll('SELECT * FROM ivr WHERE displayname != "__install_done"', DB_FETCHMODE_ASSOC); |
|---|
| 213 | | if(DB::IsError($ivrs)) { |
|---|
| 214 | | die_freepbx(_('Error migrating to new directory! ERROR: Could not get all Ivr\'s')); |
|---|
| 215 | | } |
|---|
| 216 | | |
|---|
| 217 | | //get misc destinations that might be referencing legacy directory |
|---|
| 218 | | $miscdests = $db->getAll('SELECT id FROM miscdests WHERE destdial = "{infoservices:directory}"'); |
|---|
| 219 | | foreach ($miscdests as $m) { |
|---|
| 220 | | $miscdest[] = 'ext-miscdests,' . $m[0] . ',1'; |
|---|
| 221 | | } |
|---|
| 222 | | //iterate over ivr's and their entries, looking for a misc apps that might be legacy directory or a # dest |
|---|
| 223 | | foreach ($ivrs as $ivr) { |
|---|
| 224 | | out(_("Migrating Ivr " . $ivr['displayname'] . "'s entires to new Directory")); |
|---|
| 225 | | |
|---|
| 226 | | //is this ivr set to use directory? |
|---|
| 227 | | $true_opts = array('1', 'on', 'ON', 'CHECKED', 'checked', 'TRUE', 'true'); |
|---|
| 228 | | if (in_array($ivr['enable_directory'], $true_opts)) { |
|---|
| 229 | | out(_("Removing legacy Directory from Ivr " . $ivr['displayname'] . "'s destinations")); |
|---|
| 230 | | |
|---|
| 231 | | //update ivr dests |
|---|
| 232 | | $sql = 'UPDATE ivr_dests SET dest = ? WHERE ivr_id = ? AND '; |
|---|
| 233 | | //add misc diests if we have any |
|---|
| 234 | | if (isset($miscdest) && $miscdest) { |
|---|
| 235 | | $sql .= ' (selection = "#" '; |
|---|
| 236 | | $m = array(); |
|---|
| 237 | | foreach ($miscdest as $md) { |
|---|
| 238 | | $m[] = '"' . $md . '"'; |
|---|
| 239 | | } |
|---|
| 240 | | $sql .= ' OR dest IN(' . implode(',', $m) . ') )'; |
|---|
| 241 | | } else { |
|---|
| 242 | | $sql .= ' selection = "#" '; |
|---|
| 243 | | } |
|---|
| 244 | | $q = $db->query($sql, array($dirdest, $ivr['ivr_id'])); |
|---|
| 245 | | if(DB::IsError($q)) { |
|---|
| 246 | | die_freepbx(_('Error migrating to new directory! ERROR: Failed to update ivr destinations ' . $q->getDebugInfo())); |
|---|
| 247 | | } |
|---|
| 248 | | |
|---|
| 249 | | |
|---|
| 250 | | //check to ensure that we have a # destination |
|---|
| 251 | | $sql = 'SELECT COUNT(*) FROM ivr_dests WHERE ivr_id = ? AND selection = "#"'; |
|---|
| 252 | | $pound_count = $db->getOne($sql, array($ivr['ivr_id'])); |
|---|
| 253 | | |
|---|
| 254 | | if ($pound_count < 1) { |
|---|
| 255 | | $sql = 'INSERT INTO ivr_dests (ivr_id, selection, dest) VALUES (?, ?, ?)'; |
|---|
| 256 | | $q = $db->query($sql, array($ivr['ivr_id'], '#', $dirdest)); |
|---|
| 257 | | if(DB::IsError($q)) { |
|---|
| 258 | | die_freepbx(_('Error migrating to new directory! ERROR: Failed to add ivr destination # ' . $q->getDebugInfo())); |
|---|
| 259 | | } |
|---|
| 260 | | } |
|---|
| 261 | | |
|---|
| 262 | | //remove the legacy directroy option from this ivr |
|---|
| 263 | | $sql = 'UPDATE ivr SET enable_directory = "" WHERE ivr_id = ?'; |
|---|
| 264 | | $q = $db->query($sql, array($ivr['ivr_id'])); |
|---|
| 265 | | if(DB::IsError($q)) { |
|---|
| 266 | | die_freepbx(_('Error migrating to new directory! ERROR: Failed to unset enable_directory from ivr ' . $q->getDebugInfo())); |
|---|
| 267 | | } |
|---|
| 268 | | } else {//even if it wasnt set to use the legace directory, ensure that we dont have mics dests pointing at it |
|---|
| 269 | | if (isset($miscdest) && $miscdest) { |
|---|
| 270 | | $sql = 'UPDATE ivr_dests SET dest = ? WHERE ivr_id = ? AND dest'; |
|---|
| 271 | | $m = array(); |
|---|
| 272 | | foreach ($miscdest as $md) { |
|---|
| 273 | | $m[] = '"' . $md . '"'; |
|---|
| 274 | | } |
|---|
| 275 | | $sql .= ' IN(' . implode(',', $m) . ')'; |
|---|
| 276 | | $q = $db->query($sql, array($dirdest, $ivr['ivr_id'])); |
|---|
| 277 | | if(DB::IsError($q)) { |
|---|
| 278 | | die_freepbx(_('Error migrating to new directory! ERROR: Failed to update ivr destinations (2) ' . $q->getDebugInfo())); |
|---|
| 279 | | } |
|---|
| 280 | | } |
|---|
| 281 | | } |
|---|
| 282 | | }//<--- end ivr foreach loop |
|---|
| 283 | | } else { //if we dont have/couldnt create a new directory, assume there are no vm users and disable the option from ivr's |
|---|
| 284 | | out(_("Removing legacy Directory from Ivr " . $ivr['displayname'])); |
|---|
| 285 | | $q = $db->query('UPDATE ivr SET enable_directory = ""'); |
|---|
| 286 | | if(DB::IsError($q)) { |
|---|
| 287 | | die_freepbx(_('Error migrating to new directory! ERROR: Failed to unset enable_directory from ivr (2) ' . $q->getDebugInfo())); |
|---|
| 288 | | } |
|---|
| 289 | | } |
|---|
| 290 | | }//<--- end ivr if case |
|---|
| 291 | | |
|---|
| 292 | | //TODO: migrate misc dests if any are using legacy directory |
|---|
| 293 | | |
|---|
| 302 | | |
|---|
| 303 | | /** Recursively read voicemail.conf (and any included files) |
|---|
| 304 | | * This function coppied from functions.inc.php |
|---|
| 305 | | */ |
|---|
| 306 | | function parse_voicemailconf_directory_migration($filename, &$vmconf, &$section) { |
|---|
| 307 | | if (is_null($vmconf)) { |
|---|
| 308 | | $vmconf = array(); |
|---|
| 309 | | } |
|---|
| 310 | | if (is_null($section)) { |
|---|
| 311 | | $section = "general"; |
|---|
| 312 | | } |
|---|
| 313 | | |
|---|
| 314 | | if (file_exists($filename)) { |
|---|
| 315 | | $fd = fopen($filename, "r"); |
|---|
| 316 | | while ($line = fgets($fd, 1024)) { |
|---|
| 317 | | if (preg_match("/^\s*(\d+)\s*=>\s*(\d*),(.*),(.*),(.*),(.*)\s*([;#].*)?/",$line,$matches)) { |
|---|
| 318 | | // "mailbox=>password,name,email,pager,options" |
|---|
| 319 | | // this is a voicemail line |
|---|
| 320 | | $vmconf[$section][ $matches[1] ] = array("mailbox"=>$matches[1], |
|---|
| 321 | | "pwd"=>$matches[2], |
|---|
| 322 | | "name"=>$matches[3], |
|---|
| 323 | | "email"=>$matches[4], |
|---|
| 324 | | "pager"=>$matches[5], |
|---|
| 325 | | "options"=>array(), |
|---|
| 326 | | ); |
|---|
| 327 | | |
|---|
| 328 | | // parse options |
|---|
| 329 | | //output($matches); |
|---|
| 330 | | foreach (explode("|",$matches[6]) as $opt) { |
|---|
| 331 | | $temp = explode("=",$opt); |
|---|
| 332 | | //output($temp); |
|---|
| 333 | | if (isset($temp[1])) { |
|---|
| 334 | | list($key,$value) = $temp; |
|---|
| 335 | | $vmconf[$section][ $matches[1] ]["options"][$key] = $value; |
|---|
| 336 | | } |
|---|
| 337 | | } |
|---|
| 338 | | } else if (preg_match('/^(?:\s*)#include(?:\s+)["\']{0,1}([^"\']*)["\']{0,1}(\s*[;#].*)?$/',$line,$matches)) { |
|---|
| 339 | | // include another file |
|---|
| 340 | | |
|---|
| 341 | | if ($matches[1][0] == "/") { |
|---|
| 342 | | // absolute path |
|---|
| 343 | | $filename = trim($matches[1]); |
|---|
| 344 | | } else { |
|---|
| 345 | | // relative path |
|---|
| 346 | | $filename = dirname($filename)."/".trim($matches[1]); |
|---|
| 347 | | } |
|---|
| 348 | | |
|---|
| 349 | | parse_voicemailconf_directory_migration($filename, $vmconf, $section); |
|---|
| 350 | | |
|---|
| 351 | | } else if (preg_match("/^\s*\[(.+)\]/",$line,$matches)) { |
|---|
| 352 | | // section name |
|---|
| 353 | | $section = strtolower($matches[1]); |
|---|
| 354 | | } else if (preg_match("/^\s*([a-zA-Z0-9-_]+)\s*=\s*(.*?)\s*([;#].*)?$/",$line,$matches)) { |
|---|
| 355 | | // name = value |
|---|
| 356 | | // option line |
|---|
| 357 | | $vmconf[$section][ $matches[1] ] = $matches[2]; |
|---|
| 358 | | } |
|---|
| 359 | | } |
|---|
| 360 | | fclose($fd); |
|---|
| 361 | | } |
|---|
| 362 | | } |
|---|