function format_line($line, $attachments) { global $me; if ($line['is_from_me']) { $contact = $me; } else { $contact = $line['contact']; } $attachments_html = ''; if (count($attachments)) { foreach ($attachments as $at) { $imgsrc = attachment_folder($line['contact'], $line['date'], true) . $at['transfer_name']; $attachments_html .= '<img src="' . $imgsrc . '" class="u-photo">'; } } return '<div class="h-entry">' . '<time class="dt-published" datetime="' . date('c', $line['date']) . '">' . date('Y-m-d H:i:s', $line['date']) . '</time> ' . contact($contact) . ' <span class="e-content p-name">' . htmlentities(trim($line['text'])) . '</span>' . $attachments_html . '</div>'; }
} if (!file_exists($fn)) { file_put_contents($fn, html_template()); } $attachment_query = $db->query('SELECT attachment.* FROM attachment JOIN message_attachment_join ON message_attachment_join.attachment_id=attachment.ROWID WHERE message_attachment_join.message_id = ' . $line['ROWID']); $attachments = array(); while ($attachment = $attachment_query->fetch(PDO::FETCH_ASSOC)) { $attachments[] = $attachment; } if (!entry_exists($line, $attachments, $fn)) { $fp = fopen($fn, 'a'); $log = format_line($line, $attachments); fwrite($fp, $log . "\n"); fclose($fp); echo date('c', $line['date']) . "\t" . $line['contact'] . "\t" . $line['text'] . "\n"; foreach ($attachments as $at) { $imgsrc = attachment_folder($line['contact'], $line['date']) . $at['transfer_name']; if (!file_exists(dirname($imgsrc))) { mkdir(dirname($imgsrc)); } copy(str_replace('~/', $_SERVER['HOME'] . '/', $at['filename']), $imgsrc); } } if ($line['date'] > $last_timestamp) { $last_timestamp = $line['date']; } } file_put_contents('last.txt', $last_timestamp);
/** * Goes thought all the attachments and checks that they exist * * - Goes in increments of 250 * - if $fix_errors is true will remove empty files, update wrong filesizes in the DB and * - remove DB entries if the file can not be found. * * @package Attachments * @param int $start * @param boolean $fix_errors * @param string[] $to_fix */ function repairAttachmentData($start, $fix_errors, $to_fix) { global $modSettings; $db = database(); $repair_errors = array('wrong_folder' => 0, 'missing_extension' => 0, 'file_missing_on_disk' => 0, 'file_size_of_zero' => 0, 'file_wrong_size' => 0); $result = $db->query('', ' SELECT id_attach, id_folder, filename, file_hash, size, attachment_type FROM {db_prefix}attachments WHERE id_attach BETWEEN {int:substep} AND {int:substep} + 249', array('substep' => $start)); $to_remove = array(); while ($row = $db->fetch_assoc($result)) { // Get the filename. if ($row['attachment_type'] == 1) { $filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename']; } else { $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']); } // File doesn't exist? if (!file_exists($filename)) { // If we're lucky it might just be in a different folder. if (!empty($modSettings['currentAttachmentUploadDir'])) { // Get the attachment name without the folder. $attachment_name = !empty($row['file_hash']) ? $row['id_attach'] . '_' . $row['file_hash'] . '.elk' : getLegacyAttachmentFilename($row['filename'], $row['id_attach'], null, true); if (!is_array($modSettings['attachmentUploadDir'])) { $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); } // Loop through the other folders looking for this file foreach ($modSettings['attachmentUploadDir'] as $id => $dir) { if (file_exists($dir . '/' . $attachment_name)) { $repair_errors['wrong_folder']++; // Are we going to fix this now? if ($fix_errors && in_array('wrong_folder', $to_fix)) { attachment_folder($row['id_attach'], $id); } // Found it, on to the next attachment continue 2; } } if (!empty($row['file_hash'])) { // It may be without the elk extension (something wrong during upgrade/conversion) $attachment_name = $row['id_attach'] . '_' . $row['file_hash']; if (!is_array($modSettings['attachmentUploadDir'])) { $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); } // Loop through the other folders looking for this file foreach ($modSettings['attachmentUploadDir'] as $id => $dir) { if (file_exists($dir . '/' . $attachment_name)) { $repair_errors['missing_extension']++; // Are we going to fix this now? if ($fix_errors && in_array('missing_extension', $to_fix)) { rename($dir . '/' . $attachment_name, $dir . '/' . $attachment_name . '.elk'); attachment_folder($row['id_attach'], $id); } // Found it, on to the next attachment continue 2; } } } } // Could not find it anywhere $to_remove[] = $row['id_attach']; $repair_errors['file_missing_on_disk']++; } elseif (filesize($filename) == 0) { $repair_errors['file_size_of_zero']++; // Fixing? if ($fix_errors && in_array('file_size_of_zero', $to_fix)) { $to_remove[] = $row['id_attach']; @unlink($filename); } } elseif (filesize($filename) != $row['size']) { $repair_errors['file_wrong_size']++; // Fix it here? if ($fix_errors && in_array('file_wrong_size', $to_fix)) { attachment_filesize($row['id_attach'], filesize($filename)); } } } $db->free_result($result); // Do we need to delete what we have? if ($fix_errors && !empty($to_remove) && in_array('file_missing_on_disk', $to_fix)) { removeOrphanAttachments($to_remove); } return $repair_errors; }