From 29363a185dea07dbb035ccdc027a48881d8e96fd Mon Sep 17 00:00:00 2001 From: redmatrix Date: Wed, 6 Apr 2016 18:07:29 -0700 Subject: file sync work --- include/import.php | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) (limited to 'include/import.php') 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 ); + } + } + } + } + } +} -- cgit v1.2.3