+ // relocate
+ if (x($_POST,'relocate') && x($_POST,'relocate_url') && $_POST['relocate_url']!=""){
+ $new_url = $_POST['relocate_url'];
+ $new_url = rtrim($new_url,"/");
+
+ $parsed = @parse_url($new_url);
+ if (!$parsed || (!x($parsed,'host') || !x($parsed,'scheme'))) {
+ notice(t("Can not parse base url. Must have at least <scheme>://<domain>"));
+ goaway($a->get_baseurl(true) . '/admin/site' );
+ }
+
+ /* steps:
+ * replace all "baseurl" to "new_url" in config, profile, term, items and contacts
+ * send relocate for every local user
+ * */
+
+ $old_url = $a->get_baseurl(true);
+
+ function update_table($table_name, $fields, $old_url, $new_url) {
+ global $db, $a;
+
+ $dbold = dbesc($old_url);
+ $dbnew = dbesc($new_url);
+
+ $upd = array();
+ foreach ($fields as $f) {
+ $upd[] = "`$f` = REPLACE(`$f`, '$dbold', '$dbnew')";
+ }
+
+ $upds = implode(", ", $upd);
+
+
+
+ $q = sprintf("UPDATE %s SET %s;", $table_name, $upds);
+ $r = q($q);
+ if (!$r) {
+ notice( "Falied updating '$table_name': " . $db->error );
+ goaway($a->get_baseurl(true) . '/admin/site' );
+ }
+ }
+
+ // update tables
+ update_table("profile", array('photo', 'thumb'), $old_url, $new_url);
+ update_table("term", array('url'), $old_url, $new_url);
+ update_table("contact", array('photo','thumb','micro','url','nurl','request','notify','poll','confirm','poco'), $old_url, $new_url);
+ update_table("unique_contacts", array('url'), $old_url, $new_url);
+ update_table("item", array('owner-link','owner-avatar','author-name','author-link','author-avatar','body','plink','tag'), $old_url, $new_url);
+
+ // update config
+ $a->set_baseurl($new_url);
+ set_config('system','url',$new_url);
+
+ // send relocate
+ $users = q("SELECT uid FROM user WHERE account_removed = 0 AND account_expired = 0");
+
+ foreach ($users as $user) {
+ proc_run('php', 'include/notifier.php', 'relocate', $user['uid']);
+ }
+
+ info("Relocation started. Could take a while to complete.");
+
+ goaway($a->get_baseurl(true) . '/admin/site' );
+ }
+ // end relocate
+