As i said before in another thread am working in a mirgation from joomla/fireboard to vBulletin CMS, and now an working in upload the old image attachments into the vBulletin database;
to import correctly sections, I previously added two fields to the cms_node tab,called oldjoomlaid and oldjoomlasec and use it as reference here.
I already revised twice my script but am having problems in display the image in the article; instead of the image it appears the link "ATTACHMENT XXXX"; do i forget to set any parameter anywhere?
PHP Code:
<?php
/**
* Associate Pics to Articles
* this script should capture the pictures from the cms_article table
* (if contained as images in the pagetext field), pass it through GD,
* upload it as BLOB and combine it again to the related vBulletin articles
* hope this works ^^;
*
* modification: since the original img tag has been filtered in the imported articles,
* now the data has to be taken from the original joomla database.
*
* @Package: Migration
* @Date: 01-04-2010 16:54:00 -0800
*
*/
//new: put a GET 'mode' value: if 0 or none = test, if 1 = real
$mode = $_GET['mode'];
if(!$mode || $mode == ''){
$mode = 0;
}
//=============================================================================
//initialize variables
ini_set('max_execution_time', 3000);
require('includes/db_connection.php');
define('BASE_ATT_PATH','http://localhost/vbcms/');
//define all arrays and paths
$jarticles = array();
$vbarticles = array();
$vbattachedpics = array();
$vbattach = 'temp/';
$vbattachthumb = 'temp/';
$now = time();
print 'actual time is: '.$now.'<br/>';
//configure the attachment type
$attachtypes = array(
array('ext'=>'gif','mimetype'=>'Content-type: image/gif', 'maxW'=>620,'maxH'=>280,'maxsize'=>20000,'th'=>1),
array('ext'=>'jpeg','mimetype'=>'Content-type: image/jpeg', 'maxW'=>620,'maxH'=>280,'maxsize'=>20000, 'th'=>1),
array('ext'=>'jpg','mimetype'=>'Content-type: image/jpeg', 'maxW'=>620,'maxH'=>280,'maxsize'=>100000, 'th'=>1),
array('ext'=>'jpe','mimetype'=>'Content-type: image/jpeg', 'maxW'=>620,'maxH'=>280,'maxsize'=>20000, 'th'=>1),
array('ext'=>'png','mimetype'=>'Content-type: image/png', 'maxW'=>620,'maxH'=>280,'maxsize'=>20000, 'th'=>1),
array('ext'=>'bmp','mimetype'=>'Content-type: image/bitmap', 'maxW'=>620,'maxH'=>280,'maxsize'=>20000, 'th'=>1),
);
//==================================================================
// So in old site articles the images are not stored as attachments in the database
// but as linked files and inserted in the article body as HTML links;
// this script will retrieve the link, upload the picture in the DB and update the
// tables in the vbulletin database.
//
// new modifications: parse the old old site articles to retrieve the enclosed image from the text
// and parse the inserted 'oldjoomlaid' number in the vbulletin cms_node table, since the content
// have been filtered.
//===================================================================
connect_oldsite();
//tags to get the image
$filtermosimagetag = "#<div class=\"mosimage\"( .*?)>(.*?)<\/div><\/div>#";
$filterimgtag="/src=\"(.*?)\"/";
$sql0 = "SELECT id, introtext FROM jos_content";
$res0 = mysql_query($sql0);
while($row0 = mysql_fetch_object($res0)){
//i extract introtext here since i have to parse it for the image file
$itext = $row0->introtext;
if(preg_match($filtermosimagetag,$itext)){
print '<b>this one match!</b><br/>';
preg_match($filtermosimagetag,$itext,$retmosarr);
print 'retmosarr:<br/>';
print_r($retmosarr);
print '<br/><br/>';
//the usual return pattern of preg_match of this regexp is the following:
//array(3) {
// [0]=> string(281) "<div class="mosimage" style=" float: left;" align="center"><img src="images/rubriche/mechtrade.jpg" width="150" height="150" hspace="6"
// alt="Sample image" title="Sample image" border="0" /></div>"
// [1]=> string(38) " style=" float: left;" align="center""
// [2]=> string(209) "<img src="images/rubriche/mechtrade.jpg" width="150" height="150" hspace="6"
// alt="Sample image" title="Sample image" border="0" />"
//}
// corresponding to the capturing groups; will take the second element to regexp the image name - this might change however
$imgfiltered = $retmosarr[2];
print 'imgfiltered:'.$imgfiltered.'<br/>';
preg_match($filterimgtag,$imgfiltered,$retimgarr);
//the return pattern for this regexp is the following:
//array(2) {
// [0]=> string(35) "src="images/rubriche/posaobama.jpg""
// [1]=> string(29) "images/rubriche/posaobama.jpg"
//}
//will need the second capturing group and split it to retrieve image filename and extension
$imgnamefarray = explode('/',$retimgarr[1]);
//the last element of imgnamefilteredarray is the image name
$imagename = $imgnamefarray[(count($imgnamefarray))-1];
print 'imgname is:'.$imagename.'<br/>';
array_push($jarticles,array( 'id'=>$row0->id,
'introtext' =>$itext,
'imgurl' => $retimgarr[1],
'imgname' => $imagename,
));
} //end if
}// end while
print 'jarticles contains '.count($jarticles).' elements<br/>';
//==================================================================
// part two - connect vbulletin DB and populate vbarticles
//==================================================================
//connect the database
connect_vbcms();
//query for articles and user id and salt
$sql = "SELECT cms_node.nodeid, cms_node.userid, cms_node.contentid, cms_node.oldjoomlaid, cms_article.pagetext, user.salt
FROM cms_node, cms_article, user
WHERE cms_node.contentid = cms_article.contentid
AND cms_node.contenttypeid =18 AND cms_node.oldjoomlaid != 0 AND user.userid = cms_node.userid ORDER BY cms_node.nodeid"; //remove order by... limit 50 - is for tests !!!IMPORTANT
$res = mysql_query($sql);
while($row = mysql_fetch_object($res)){
array_push($vbarticles, array( 'nodeid'=>$row->nodeid,
'userid'=>$row->userid,
'salt'=> $row->salt,
'contentid'=>$row->contentid,
'pagetext'=>$row->pagetext,
'time'=>$now,
'imageurl'=>'',
'oldjoomlaid'=>$row->oldjoomlaid,
));
} //end while
print 'vbarticles contains '.count($vbarticles).' filtered elements<br/>';
//==================================================================
//This is the cycle to align the two arrays in one
foreach($vbarticles as $i=>$value){
foreach ($jarticles as $j=>$value){
if($vbarticles[$i]['oldjoomlaid'] == $jarticles[$j]['id']){
$vbarticles[$i]['imageurl'] = $jarticles[$j]['imgurl'];
} //end if vbarticles
} //end foreach jarticles
} //end foreach vbarticles
//===================================================================
// foreach to process the images and update databases
foreach($vbarticles as $i=>$value){
if($vbarticles[$i]['imageurl']!=''){
print 'i index = '.$i .'<br/>';
print 'currentindex: '.$vbarticles[$i]['nodeid'].'<br/>';
$attachmentpathArray = explode('/',$vbarticles[$i]['imageurl']);
$filename = urlencode($attachmentpathArray[(count($attachmentpathArray)-1)]);
$loaderurl = str_replace(' ',"%20",$vbarticles[$i]['imageurl']);
$fnnoext = substr($filename,0,-4);
$fnext = substr($filename,-3);
$retrievefile = BASE_ATT_PATH . $loaderurl;
//debug string
print'retrievefile: '.$retrievefile.'<br/>';
switch(strtolower($fnext)){
case 'gif':
$file1 = imagecreatefromgif($retrievefile);
$imgPar = retrieveImgParams('gif',$attachtypes);
break;
case 'jpeg':
$file1 = imagecreatefromjpeg($retrievefile);
$imgPar = retrieveImgParams('jpeg',$attachtypes);
break;
case 'jpg':
$file1 = imagecreatefromjpeg($retrievefile);
$imgPar = retrieveImgParams('jpg',$attachtypes);
break;
case 'jpe':
$file1 = imagecreatefromjpeg($retrievefile);
$imgPar = retrieveImgParams('jpe',$attachtypes);
break;
case 'png':
$file1 = imagecreatefrompng($retrievefile);
$imgPar = retrieveImgParams('png',$attachtypes);
break;
case 'bmp':
$file1 = imagecreatefrombmp($retrievefile);
$imgPar = retrieveImgParams('bmp',$attachtypes);
break;
} //end switch
print 'the file '.$retrievefile.' <u>is an image</u> (or so i think)<br/>';
$maxsizeW = $imgPar['maxW'];
$maxsizeH = $imgPar['maxH'];
$mimeType = $imgPar['mimetype'];
$imgext = $imgPar['ext'];
//determine sizes
$imgsize = getimagesize($retrievefile);
$img1w = $imgsize[0];
$img1h = $imgsize[1];
$ratio = round($img1w/$img1h,2);
//determine size for pic
$img1w = ($img1w >$maxsizeW ) ? $maxsizeW : $img1w;
$img1h = ($img1h >$maxsizeH ) ? $maxsizeH : $img1h;
//determine size for thumbnail
$img2w = ($img1w >100 ) ? 100 : $img1w;
$img2h = round($img2w/$ratio); //rounded width/ratio
//create the pic
$attach = imagecreatetruecolor($img1w,$img1h);
imagecopyresampled($attach,$file1,0,0,0,0,$img1w,$img1h,$img1w,$img1h);
//create the thumbnail
$attachthumb = imagecreatetruecolor($img2w,$img2h);
imagecopyresampled($attachthumb,$file1,0,0,0,0,$img2w,$img2h,$img1w,$img1h);
ob_start();
switch(strtolower($imgext)){
case 'gif':
$imgrendered = $vbattach.$fnnoext.'.'.$imgext;
imagegif($attach, $imgrendered);
break;
case 'jpeg':
$imgrendered = $vbattach.$fnnoext.'.'.$imgext;
imagejpeg($attach,$imgrendered,80);
break;
case 'jpg':
$imgrendered = $vbattach.$fnnoext.'.'.$imgext;
imagejpeg($attach,$imgrendered,80);
break;
case 'jpe':
$imgrendered = $vbattach.$fnnoext.'.'.$imgext;
imagejpeg($attach,$imgrendered,80);
break;
case 'png':
$imgrendered = $vbattach.$fnnoext.'.'.$imgext;
imagepng($attach,$imgrendered);
break;
case 'bmp':
$imgrendered = $vbattach.$fnnoext.'.png';
//store it as png since bmp support in GD is non existent
imagepng($attach,$imgrendered);
break;
} //end switch $ext
ob_end_clean();
ob_start();
//all thumbnails are stored as png
$thumbrendered = $vbattachthumb.$fnnoext."_thu.png";
imagepng($attachthumb,$thumbrendered);
ob_end_clean();
//=============================================================================
// process the file
// start with storing the pic in the filedata table
$myattach = addslashes(file_get_contents($imgrendered));
$myattachfilesize = filesize($imgrendered);
$myattachhash = md5_file($imgrendered);
$myattachthumb = addslashes(file_get_contents($thumbrendered));
$myattachthumbfilesize = filesize($thumbrendered);
//prepare the mysql query for filedata
//new: since have the problem that same attachment can be added to the post more times, will
//check that the same file is not already present
$sql11 = "INSERT INTO filedata ( filedataid,
userid,
dateline,
thumbnail_dateline,
filedata,
filesize,
filehash,
thumbnail,
thumbnail_filesize,
extension,
refcount,
width,
height,
thumbnail_width,
thumbnail_height
) VALUES (
NULL,
".$vbarticles[$i]['userid'].",
".$now.",
".$now.",
'".$myattach."',
".$myattachfilesize.",
'".$myattachhash."',
'".$myattachthumb."',
".$myattachthumbfilesize.",
'".$imgext."',
1,
".$img1w.",
".$img1h.",
".$img2w.",
".$img2h."
)";
//dummy query to display what is going on
$sql11b = "INSERT INTO filedata ( filedataid,
userid,
dateline,
thumbnail_dateline,
filedata,
filesize,
filehash,
thumbnail,
thumbnail_filesize,
extension,
refcount,
width,
height,
thumbnail_width,
thumbnail_height
) VALUES (
NULL,
".$vbarticles[$i]['userid'].",
".$now.",
".$now.",
'[attachbinary]',
".$myattachfilesize.",
'".$myattachhash."',
'[thumbbinary]',
".$myattachthumbfilesize.",
'".$imgext."',
1,
".$img1w.",
".$img1h.",
".$img2w.",
".$img2h."
)";
@fclose($imgrendered);
@fclose($thumbrendered);
print 'sql11 ='.$sql11b.'<br/>';
if($mode == '1'){
print 'real insert<br/>';
$res11 = mysql_query($sql11) or die('Invalid query: ' . mysql_error());
}
$filedataindex = mysql_insert_id();
printf("Records inserted: %d\n", mysql_affected_rows());
print 'filedataindex = '.$filedataindex.'<br/>';
sleep(1);
//sql query for table attachment
//retrieve if there are already records linked to that post
//build the settings field
//========================================================================================
// the settings field is made like this:
// a:7:{
// s:9:"alignment";s:4:"left";
// s:4:"size"; s:8:"fullsize";
// s:7:"caption"; s:0:"";
// s:7:"linkurl"; s:0:"";
// s:6:"styles"; s:0:"";
// s:11:"description"; s:0:"";
// s:5:"title"; s:14:"currencypuzzle";
// }
// is a serialized array
//========================================================================================
$attachsetting = array('alignment' => 'left',
'size' => 'fullsize',
'caption' => '',
'linkurl' => '',
'styles' => '',
'description' => '',
'title' => '',
);
//define posthash
// in the vbulletin code, the posthash is calcolated as following:
// $posthash = md5(TIMENOW . $vbulletin->userinfo['userid'] . $vbulletin->userinfo['salt']);
$myposthash = md5($now.$vbarticles[$i]['userid'].$vbarticles[$i]['salt']);
$sql12 = "INSERT INTO attachment ( attachmentid,
contenttypeid,
contentid,
userid,
dateline,
filedataid,
state,
counter,
posthash,
filename,
caption,
reportthreadid,
settings,
displayorder,
importattachmentid
) VALUES (
NULL,
18,
".$vbarticles[$i]['contentid'].",
".$vbarticles[$i]['userid'].",
".$now.",
".$filedataindex.",
'visible',
0,
'".$myposthash."',
'".$fnnoext.'.'.$imgext."',
NULL,
0,
'". serialize($attachsetting) ."',
0,
0
)";
print 'sql12 ='.$sql12.'<br/>';
if($mode == '1'){
print 'real insert<br/>';
$res12 = mysql_query($sql12) or die('Invalid query: ' . mysql_error());
}
$attachmentid = mysql_insert_id();
printf("Records inserted: %d\n", mysql_affected_rows());
print'attachmentid is: '.$attachmentid.'<br/>';
// sql query for attachmentcategoryuser
$sql13 = "INSERT INTO attachmentcategoryuser ( filedataid,
userid,
categoryid,
filename,
dateline
) VALUES (
".$filedataindex.",
".$vbarticles[$i]['userid'].",
0,
'".$fnnoext.'.'.$imgext."',
".$now."
)";
print 'sql13 ='.$sql13.'<br/>';
if($mode == '1'){
print 'real insert<br/>';
$res13 = mysql_query($sql13) or die('Invalid query: ' . mysql_error());
}
printf("Records inserted: %d\n", mysql_affected_rows());
//and at last fix the article table adding there is an attach
//first get the content of the pagetext
//need to insert the BBCODE [ATTACH]attachnumber[/ATTACH]
$removeimgtag = "#\[ATTACH=CONFIG\](.*?)\[\/ATTACH\]#"; //regEx for remove img
$newcontent = preg_replace($removeimgtag,' ',$vbarticles[$i]['pagetext']);
$newcontent = str_replace(chr(39),'’',$newcontent);
$newcontentpage = '[ATTACH=CONFIG]'.$attachmentid.'[/ATTACH]'.chr(13).chr(10).$newcontent;
print 'newcontent = '.$newcontentpage.'<br/>';
// print 'pagetext = '.$pagetext.'<br/>';
//$pw = 'http://192.168.1.112/vbcms/';
$pw = '';
$attachlink = $pw.'attachment.php?attachmentid='.$attachmentid.'&cid=18';
//TODO: make the query that modifies article
$sql14 = "UPDATE cms_article SET previewimage = '".$attachlink."',imagewidth = ".$img2w.", imageheight= ".$img2h.", pagetext = '".$newcontentpage."', previewtext = '".$newcontentpage."' WHERE contentid = ".$vbarticles[$i]['contentid']." ";
$sql14b = "UPDATE cms_article SET previewimage = '".$attachlink."',imagewidth = ".$img2w.", imageheight= ".$img2h.", pagetext = '[text]', previewtext = '[text]' WHERE contentid = ".$vbarticles[$i]['contentid']." ";
print 'sql14 ='.$sql14b.'<br/>';
if($mode == '1'){
print 'real insert<br/>';
$res14 = mysql_query($sql14) or die('Invalid query: ' . mysql_error());
printf("Records inserted: %d\n", mysql_affected_rows());
}
if($querycnt<50){
$querycnt++;
} else {
print 'sleep<br/>';
// sleep(5);
$querycnt = 0;
}
} //end if vbarticles
} //end foreach
//=============================================================================
//imagecreatefrombmp function from php.net since GD does not support bmp directly
function imagecreatefrombmp($p_sFile){
// Load the image into a string
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
// Process the header
// Structure: http://www.fastgraph.com/help/bmp_header_format.html
if (substr($header,0,4)=="424d")
{
// Cut it in parts of 2 bytes
$header_parts = str_split($header,2);
// Get the width 4 bytes
$width = hexdec($header_parts[19].$header_parts[18]);
// Get the height 4 bytes
$height = hexdec($header_parts[23].$header_parts[22]);
// Unset the header params
unset($header_parts);
}
// Define starting X and Y
$x = 0;
$y = 1;
// Create newimage
$image = imagecreatetruecolor($width,$height);
// Grab the body from the image
$body = substr($hex,108);
// Calculate if padding at the end-line is needed
// Divided by two to keep overview.
// 1 byte = 2 HEX-chars
$body_size = (strlen($body)/2);
$header_size = ($width*$height);
// Use end-line padding? Only when needed
$usePadding = ($body_size>($header_size*3)+4);
// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
// Calculate the next DWORD-position in the body
for ($i=0;$i<$body_size;$i+=3)
{
// Calculate line-ending and padding
if ($x>=$width)
{
// If padding needed, ignore image-padding
// Shift i to the ending of the current 32-bit-block
if ($usePadding)
$i += $width%4;
// Reset horizontal position
$x = 0;
// Raise the height-position (bottom-up)
$y++;
// Reached the image-height? Break the for-loop
if ($y>$height)
break;
}
// Calculation of the RGB-pixel (defined as BGR in image-data)
// Define $i_pos as absolute position in the body
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);
// Calculate and draw the pixel
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
// Raise the horizontal position
$x++;
}
// Unset the body / free the memory
unset($body);
// Return image-object
return $image;
} //end function
//=============================================================================
//function that parse the attachtypes array and return the values for the file format
function retrieveImgParams($type,$attachtypes){
// global $attacthypes;
// print_r($attachtypes);
print 'type = '.$type.'<br/>';
foreach ($attachtypes as $i=>$value){
if($attachtypes[$i]['ext'] == $type){
return array( 'ext'=> $attachtypes[$i]['ext'],
'mimetype'=> $attachtypes[$i]['mimetype'],
'maxW'=> $attachtypes[$i]['maxW'],
'maxH'=> $attachtypes[$i]['maxH'],
'maxsize'=> $attachtypes[$i]['maxsize'],
'th'=> $attachtypes[$i]['th'],
);
} //end if
}//end foreach
} //end function
?>