diff --git a/include/attach.php b/include/attach.php index 69adbc546..b6b56db34 100644 --- a/include/attach.php +++ b/include/attach.php @@ -1843,16 +1843,27 @@ function attach_export_data($channel,$resource_id) { $paths[] = $r[0]; } while($hash_ptr); + + $ret['fetch_url'] = z_root() . '/getfile'; + $ret['original_channel'] = $channel['channel_address']; + + $paths = array_reverse($paths); $ret['attach'] = $paths; + if($attach_ptr['is_photo']) { $r = q("select * from photo where resource_id = '%s' and uid = %d order by scale asc", dbesc($resource_id), intval($channel['channel_id']) ); - $ret['photo'] = $r; + if($r) { + foreach($r as $rr) { + $rr['data'] = base64_encode($rr['data']); + } + $ret['photo'] = $r; + } $r = q("select * from item where resource_id = '%s' and resource_type = 'photo' and uid = %d ", dbesc($resource_id), @@ -1874,4 +1885,16 @@ function attach_export_data($channel,$resource_id) { return $ret; +} + + +/* strip off 'store/nickname/' from the provided path */ + +function get_attach_binname($s) { + $p = $s; + if(strpos($s,'store/') === 0) { + $p = substr($s,6); + $p = substr($p,strpos($p,'/')+1); + } + return $p; } \ No newline at end of file diff --git a/include/import.php b/include/import.php index 9a57012b2..83e0a0aa8 100644 --- a/include/import.php +++ b/include/import.php @@ -870,6 +870,132 @@ function import_mail($channel,$mails) { - +function sync_files($channel,$files) { + + require_once('include/attach.php'); + + if($channel && $files) { + foreach($files as $f) { + if(! $f) + continue; + + $fetch_url = $f['fetch_url']; + $oldbase = dirname($fetch_url); + $original_channel = $f['original_channel']; + + if(! ($fetch_url && $original_channel)) + continue; + + if($f['attach']) { + $attachment_stored = false; + foreach($f['attach'] as $att) { + $x = attach_by_hash($att['hash']); + if($x && $x['uid'] == $channel['channel_id']) + continue; + + $newfname = 'store/' . $channel['channel_address'] . '/' . get_attach_binname($att['data']); + + if($att['filetype'] === 'multipart/mixed' && $att['is_dir']) { + os_mkdir($newfname, STORAGE_DEFAULT_PERMISSIONS,true); + $att['data'] = $newfname; + dbesc_array($att); + $r = dbq("INSERT INTO attach (`" + . implode("`, `", array_keys($att)) + . "`) VALUES ('" + . implode("', '", array_values($att)) + . "')" ); + + continue; + } + else { + $time = datetime_convert(); + + $parr = array('hash' => $channel['channel_hash'], + 'time' => $time, + 'resource' => $att['hash'], + 'revision' => 0, + 'sig' => rsa_sign($channel['channel_hash'] . '.' . $time, $channel['channel_prvkey']) + ); + + $store_path = $newfname; + + $fp = fopen($newfname,'w'); + if(! $fp) { + logger('failed to open file.'); + continue; + } + $redirects = 0; + $x = z_post_url($fetch_url,$parr,$redirects,array('filep' => $fp)); + fclose($fp); + + if($x['success']) { + $attachment_stored = true; + + dbesc_array($att); + $r = dbq("INSERT INTO attach (`" + . implode("`, `", array_keys($att)) + . "`) VALUES ('" + . implode("', '", array_values($att)) + . "')" ); + } + continue; + } + + } + } + if(! $attachment_stored) { + // should we queue this and retry or what? + logger('attachment store failed'); + } + if($f['photo']) { + foreach($f['photo'] as $p) { + unset($p['id']); + $p['aid'] = $channel['channel_account_id']; + $p['uid'] = $channel['channel_id']; + + if($p['scale'] === 0 && $p['os_storage']) + $p['data'] = $store_path; + else + $p['data'] = base64_decode($p['data']); + + dbesc_array($p); + $r = dbq("INSERT INTO photo (`" + . implode("`, `", array_keys($p)) + . "`) VALUES ('" + . implode("', '", array_values($p)) + . "')" ); + + } + } + if($f['item']) { + sync_items($channel,$f['item']); + foreach($f['item'] as $i) { + if($i['message_id'] !== $i['message_parent']) + continue; + $r = q("select * from item where mid = '%s' and uid = %d limit 1", + dbesc($i['message_id']), + intval($channel['channel_id']) + ); + if($r) { + $item = $r[0]; + item_url_replace($channel,$item,$oldbase,z_root(),$original_channel); + + dbesc_array($item); + $item_id = $item['id']; + unset($item['id']); + $str = ''; + foreach($item as $k => $v) { + if($str) + $str .= ","; + $str .= " `" . $k . "` = '" . $v . "' "; + } + + $r = dbq("update `item` set " . $str . " where id = " . $item_id ); + } + } + } + } + } +} diff --git a/include/photos.php b/include/photos.php index 5838c013f..312ea18d3 100644 --- a/include/photos.php +++ b/include/photos.php @@ -312,7 +312,7 @@ function photo_upload($channel, $observer, $args) { 'title' => $title, 'created' => $p['created'], 'edited' => $p['edited'], - 'id' => rawurlencode(z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash), + 'id' => z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash, 'link' => $link, 'body' => $obj_body ); @@ -320,7 +320,7 @@ function photo_upload($channel, $observer, $args) { $target = array( 'type' => ACTIVITY_OBJ_ALBUM, 'title' => (($album) ? $album : '/'), - 'id' => rawurlencode(z_root() . '/photos/' . $channel['channel_address'] . '/album/' . bin2hex($album)) + 'id' => z_root() . '/photos/' . $channel['channel_address'] . '/album/' . bin2hex($album) ); // Create item container diff --git a/include/text.php b/include/text.php index a1a1cfb1c..d38f4f7c4 100644 --- a/include/text.php +++ b/include/text.php @@ -2730,14 +2730,23 @@ function json_url_replace($old,$new,&$s) { } -function item_url_replace($channel,&$item,$old,$new) { +function item_url_replace($channel,&$item,$old,$new,$oldnick = '') { - if($item['attach']) + if($item['attach']) { json_url_replace($old,$new,$item['attach']); - if($item['object']) + if($oldnick) + json_url_replace('/' . $oldnick . '/' ,'/' . $channel['channel_address'] . '/' ,$item['attach']); + } + if($item['object']) { json_url_replace($old,$new,$item['object']); - if($item['target']) + if($oldnick) + json_url_replace('/' . $oldnick . '/' ,'/' . $channel['channel_address'] . '/' ,$item['object']); + } + if($item['target']) { json_url_replace($old,$new,$item['target']); + if($oldnick) + json_url_replace('/' . $oldnick . '/' ,'/' . $channel['channel_address'] . '/' ,$item['target']); + } if(string_replace($old,$new,$item['body'])) { $item['sig'] = base64url_encode(rsa_sign($item['body'],$channel['channel_prvkey'])); @@ -2746,6 +2755,14 @@ function item_url_replace($channel,&$item,$old,$new) { // @fixme item['plink'] and item['llink'] + str_replace($old,$new,$item['plink']); + if($oldnick) + str_replace('/' . $oldnick . '/' ,'/' . $channel['channel_address'] . '/' ,$item['plink']); + + str_replace($old,$new,$item['llink']); + if($oldnick) + str_replace('/' . $oldnick . '/' ,'/' . $channel['channel_address'] . '/' ,$item['llink']); + }