function process_photos($post_id, $photos) { global $hexon_nr; $index = 0; $time = date('Y/m'); foreach ($photos as $photo_nr) { $file = 'wp-content/uploads/' . $time . '/' . $hexon_nr . '-' . $index . '.jpg'; $imgdata = file_get_contents($photo_nr['@value']); file_put_contents($file, $imgdata); attach_image($file, $post_id); $index++; } }
function fix_images($filedirectory) { global $vbulletin; $contenttypeid = vB_Types::instance()->getContentTypeId("vBCms_Article"); $set = $vbulletin->db->query_read(" SELECT cms_node.nodeid, cms_article.* FROM " . TABLE_PREFIX . "cms_article AS cms_article JOIN " . TABLE_PREFIX . "cms_node AS cms_node ON (cms_article.contentid = cms_node.contentid AND cms_node.contenttypeid = $contenttypeid) "); while ($row = $vbulletin->db->fetch_array($set)) { $attachment_map = array(); $pagetext = $row['pagetext']; //get attachments and replace with new ids $matches = array(); if (preg_match_all("#\\[ATTACH=CONFIG\\](\\d+)\\[/ATTACH\\]#i", $row['pagetext'], $matches)) { foreach($matches[1] AS $attachmentid) { if (!array_key_exists($attachmentid, $attachment_map)) { $file_name = get_image_filename($filedirectory, $attachmentid); $attachment_map[$attachmentid] = attach_image($file_name, $filedirectory, $row['nodeid']); } } if (count($attachment_map)) { $orig = array(); $replacement = array(); foreach($attachment_map AS $oldid => $newid) { $orig[] = "[ATTACH=CONFIG]" . $oldid . "[/ATTACH]"; $replacement[] = "[ATTACH=CONFIG]" . $newid . "[/ATTACH]"; } if (count($orig)) { $pagetext = str_replace($orig, $replacement, $pagetext); } $vbulletin->db->query_write(" UPDATE " . TABLE_PREFIX . "cms_article SET pagetext = '" . $vbulletin->db->escape_string($pagetext) . "' WHERE contentid = $row[contentid] "); } } //find and replace attachments added as IMG tags. Otherwise they'll look for the live site, which is bad. $matches = array(); if (preg_match_all("#\\[IMG\\][^]]*attachmentid=(\\d+)[^]]*\\[/IMG\\]#i", $row['pagetext'], $matches)) { $orig = array(); $replacement = array(); foreach($matches[1] AS $key => $attachmentid) { if (!array_key_exists($attachmentid, $attachment_map)) { $file_name = get_image_filename($filedirectory, $attachmentid); $attachment_map[$attachmentid] = attach_image($file_name, $filedirectory, $row['nodeid']); } if ($attachment_map[$attachmentid]) { $orig[] = $matches[0][$key]; $replacement[] = "[ATTACH]" . $attachment_map[$attachmentid] . "[/ATTACH]"; } } if (count($orig)) { $pagetext = str_replace($orig, $replacement, $pagetext); $vbulletin->db->query_write(" UPDATE " . TABLE_PREFIX . "cms_article SET pagetext = '" . $vbulletin->db->escape_string($pagetext) . "' WHERE contentid = $row[contentid] "); } } //handle preview images $matches = array(); if (preg_match("#attachmentid=(\\d+)&#", $row['previewimage'], $matches)) { $attachmentid = $matches[1]; if ($attachmentid) { if (!array_key_exists($attachmentid, $attachment_map)) { $file_name = get_image_filename($filedirectory, $attachmentid); $attachment_map[$attachmentid] = attach_image($file_name, $filedirectory, $row['nodeid']); } $newid = $attachment_map[$attachmentid]; if ($newid) { $record = $vbulletin->db->query_first($q = " SELECT thumbnail_width, thumbnail_height FROM " . TABLE_PREFIX . "attachment AS attach INNER JOIN " . TABLE_PREFIX . "filedata AS data ON data.filedataid = attach.filedataid WHERE attachmentid = $newid" ); $vbulletin->db->query_write($q = " UPDATE " . TABLE_PREFIX . "cms_article SET previewimage = 'attachment.php?attachmentid=$newid&cid=$contenttypeid', imagewidth = $record[thumbnail_width], imageheight = $record[thumbnail_height] WHERE contentid = $row[contentid] "); } else { echo "<p>Could not find attachmentid $attachmentid</p>"; } } else { var_dump($row['contentid'], $row['previewimage']); } } } }