mysqlimagereplacer/index.php

337 lines
16 KiB
PHP
Raw Normal View History

2016-12-15 21:24:41 +00:00
<?php
/**
* mySQL image replacer - https://github.com/zlaxy/mysqlimagereplacer
* v.0.1 alpha - uder DWTWL license https://soundragon.su/license/license.html
* interface script
*
* !MAKE BACKUP OF YOUR SQL-TABLE BEFORE YOU TRY THIS SCRIPT!
*
* requirement: php-xml module
*
* todo: curl and $wgetline="wget --timeout=2 http://somedomain/image.jpg";
* todo: rawurlencode for non-latin domains
*/
session_start();
function PageView() {
// First wizard-page
if ($_SESSION["step"]==0) {
echo "STEP 1: choose database:<br>\n";
$_SESSION["subdir"]="images";
echo "<form action=index.php method=post>\n";
echo " database host: <input type=text name=\"dbhost\" value=\"localhost\" maxlength=255><br>\n";
echo " database user: <input type=text name=\"dbuser\" value=\"",$_SESSION["dbuser"],"\" maxlength=255><br>\n";
echo " database pass: <input type=password name=\"dbpass\" maxlength=255><br>\n";
echo " database name: <input type=text name=\"dbname\" value=\"",$_SESSION["dbname"],"\" maxlength=255><br>\n";
echo " type template: <select name=\"dbtype\">\n";
echo " <option value=\"Custom\"";
if (($_SESSION["template"]=="Custom")||(!$_SESSION["template"])) echo " selected";
echo ">Custom</option>\n";
echo " <option value=\"Wordpress\"";
if ($_SESSION["template"]=="Wordpress") echo " selected";
echo ">Wordpress</option>\n";
echo " <option value=\"Joomla\"";
if ($_SESSION["template"]=="Joomla") echo " selected";
echo ">Joomla</option>\n";
echo " <option value=\"Drupal\"";
if ($_SESSION["template"]=="Drupal") echo " selected";
echo ">Drupal</option>\n";
echo " </select><br>\n";
echo " <input type=submit name=\"next0\" value=\"Next\">\n";
echo "</form>\n";
if ($_SESSION["error"]=="dbconnection") echo " Error: ", mysqli_connect_error(),"<br>\n";
if ($_SESSION["error"]=="dbconnectionname") echo " Error: enter database name.<br>\n";
}
// Second wizard-page
if ($_SESSION["step"]==1) {
echo "STEP 2: entries and directories:<br>\n";
echo "<form action=index.php method=post>\n";
echo " entries located in ",$_SESSION["dbname"],"@",$_SESSION["dbhost"],"<br>\n";
echo " Table name: <input type=text name=\"dbtable\" value=\"",$_SESSION["dbtable"],"\" maxlength=255><br>\n";
echo " Column name: <input type=text name=\"dbcolumn\" value=\"",$_SESSION["dbcolumn"],"\" maxlength=255><br>\n";
echo " Content directory: ~/<input type=text name=\"dirname\" value=\"",$_SESSION["dirname"],"\" maxlength=255><br>\n";
echo " <input type=submit name=\"prev1\" value=\"Previous\"> <input type=submit name=\"next1\" value=\"Next\">\n";
echo "</form>\n";
if ($_SESSION["error"]) echo $_SESSION["error"],"<br>\n";
}
// Third wizard-page
if ($_SESSION["step"]==2) {
echo "STEP 3: check data:<br>\n";
$postnumber=0;
$totalimagenumber=0;
$_SESSION["correctimagenumber"]=0;
$_SESSION["alreadyimagenumber"]=0;
$_SESSION["brokenimagenumber"]=0;
$dbconnection=mysqli_connect($_SESSION["dbhost"],$_SESSION["dbuser"],$_SESSION["dbpass"],$_SESSION["dbname"]);
mysqli_set_charset($dbconnection,'utf8');
$resulttable=mysqli_query($dbconnection,"SELECT * FROM ".$_SESSION["dbtable"]);
$resultrows=mysqli_num_rows($resulttable);
$_SESSION["imgsrcs"]=array();
// Checking table
while($row=mysqli_fetch_assoc($resulttable)){
$contentpost=new DOMDocument();
$contentpost->loadHTML($row[$_SESSION["dbcolumn"]]);
$imageTags=$contentpost->getElementsByTagName('img');
$imagenumberinpost=0;
foreach($imageTags as $tag) {
$imagenumberinpost++;
$totalimagenumber++;
$_SESSION["imgsrcs"][$postnumber][$imagenumberinpost]=$tag->getAttribute('src');
$testurl=parse_url($_SESSION["imgsrcs"][$postnumber][$imagenumberinpost]);
if ((!empty($testurl['host'])&&!empty($testurl['path']))&&(($testurl['scheme']=="http")||($testurl ['scheme']=="https"))&&(!($testurl['host']==$_SERVER['SERVER_NAME']))) $_SESSION["correctimagenumber"]++;
else if ($testurl['host']==$_SERVER['SERVER_NAME']) $_SESSION["alreadyimagenumber"]++;
else $_SESSION["brokenimagenumber"]++;
}
$postnumber++;
}
echo "<form action=index.php method=post>\n";
echo " directory to upload new images:<br>\n";
echo " ",getcwd(),"/<input type=text name=\"subdir\" value=\"",$_SESSION["subdir"],"\" maxlength=63> (leave blank if you don't need subdir)<br>\n";
echo " type of html-prefix:<br>\n";
echo " <input type=\"radio\" name=\"htmlprefix\" value=\"http\"";
if ($_SESSION["htmlprefix"]=="http") echo " checked";
echo ">http://<br>\n";
echo " <input type=\"radio\" name=\"htmlprefix\" value=\"https\"";
if ($_SESSION["htmlprefix"]=="https") echo " checked";
echo ">https://<br>\n";
echo " <input type=\"radio\" name=\"htmlprefix\" value=\"wo\"";
if ($_SESSION["htmlprefix"]=="wo") echo " checked";
echo ">without prefix and domain name<br>\n";
echo " type of download:<br>\n";
echo " <input type=\"radio\" name=\"dwnldtype\" value=\"put\"";
if (!($_SESSION["downloadtype"]=="curl")) echo " checked";
echo ">file_put_contents<br>\n";
echo " <input type=\"radio\" name=\"dwnldtype\" value=\"curl\"";
if ($_SESSION["downloadtype"]=="curl") echo " checked";
echo " disabled"; // usopported in alpha version
echo ">curl<br>\n";
echo " <input type=\"radio\" name=\"dwnldtype\" value=\"wget\"";
if ($_SESSION["downloadtype"]=="wget") echo " checked";
echo " disabled"; // usopported in alpha version
echo ">wget<br>\n";
echo " number of rows in table: $resultrows<br>\n";
echo " number of rows with imgsrc tags: ",count($_SESSION["imgsrcs"]),"<br>\n";
echo " total number of img src tags: $totalimagenumber<br>\n";
echo " broken image links: ",$_SESSION["brokenimagenumber"],"<br>\n";
echo " already downloaded image links: ",$_SESSION["alreadyimagenumber"],"<br>\n";
echo " correct external image links for download: ",$_SESSION["correctimagenumber"],"<br>\n";
echo " <input type=checkbox name=fixbroken value=true";
if ($_SESSION["fixbroken"]) echo " checked";
echo ">delete broken tags?<br>\n";
echo " <input type=checkbox name=fixundownload value=true";
if ($_SESSION["fixundownload"]) echo " checked";
echo ">remove undownloadble tags?<br>\n";
echo " <input type=checkbox name=fixalready value=true";
if ($_SESSION["fixalready"]) echo " checked";
echo " disabled"; // usopported in alpha version
echo ">fix prefix to already downloaded tags?<br>\n";
echo " <input type=submit name=\"prev2\" value=\"Previous\"> <input type=submit name=\"deta2\" value=\"Details\"> <input type=submit name=\"next2\" value=\"Next\">\n";
echo "</form>\n";
if ($_SESSION["error"]) echo $_SESSION["error"],"<br>\n";
if ($_SESSION["correctimagenumber"]<1) echo "you dont have any external images to download or fix<br>\n";
if ($_SESSION["details"]=="step2") {
echo "green links will be try to downloaded, yellow (already downloaded) and red (broken) - not:<hr>\n";
foreach ($_SESSION["imgsrcs"] as $postnum=>$postimgs) {
echo ($postnum+1),":<br>\n";
foreach ($postimgs as $imgs) {
$testurl=parse_url($imgs);
if ((!empty($testurl['host'])&&!empty($testurl['path']))&&(($testurl['scheme']=="http")||($testurl ['scheme']=="https"))&&(!($testurl['host']==$_SERVER['SERVER_NAME']))) echo " <font color=green>";
else if ($testurl['host']==$_SERVER['SERVER_NAME']) echo " <font color=yellow>";
else echo " <font color=red>";
echo $imgs,"</font><br>\n";
}
echo "<hr>\n";
}
}
}
// Approve page
if ($_SESSION["step"]==3) {
echo "STEP 4: approve processing:<br>\n";
echo "<form action=index.php method=post>\n";
echo " new img src tag will be like that:<br>\n";
echo " <i>\"http://",$_SERVER['SERVER_NAME'],"/",$_SESSION["dirname"];
if ($_SESSION["subdir"]) echo "/",rawurlencode($_SESSION["subdir"]);
echo "/newimage.jpg\"</i><br>\n";
echo " will be:<br>\n";
if ($_SESSION["correctimagenumber"]>0) {
echo $_SESSION["correctimagenumber"]," images uploaded, images placed to ",getcwd();
if ($_SESSION["subdir"]) echo "/",$_SESSION["subdir"];
echo "/<br>\n";
}
if ($_SESSION["fixbroken"]) echo $_SESSION["brokenimagenumber"]," tags will be deleted<br>\n";
echo " are you sure to proceed?<br>\n";
echo " <input type=submit name=\"prev3\" value=\"Previous\"> <input type=submit name=\"next3\" value=\"Next\">\n";
echo "</form>\n";
}
// Start page
if ($_SESSION["step"]==4) {
$_SESSION["error"]="";
echo "RESULT:<br>\n";
if ($_SESSION["subdir"]) {
mkdir($_SESSION["subdir"]);
if (!chdir($_SESSION["subdir"])) $_SESSION["error"]="error to apperate with new directory";
} else {
if (mkdir ("testimuploaddir")) rmdir("testimuploaddir");
else $_SESSION["error"]="error to apperate with directory, check privilegies";
}
if ($_SESSION["error"]) echo $_SESSION["error"]."<br>\n";
else {
chdir(__DIR__);
$csvsessiondata[]=$_SESSION["dbhost"];
$csvsessiondata[]=$_SESSION["dbuser"];
$csvsessiondata[]=$_SESSION["dbpass"];
$csvsessiondata[]=$_SESSION["dbname"];
$csvsessiondata[]=$_SESSION["dbtable"];
$csvsessiondata[]=$_SESSION["dbcolumn"];
$csvsessiondata[]=$_SERVER['SERVER_NAME'];
$csvsessiondata[]=$_SESSION["fixbroken"];
$csvsessiondata[]=$_SESSION["dirname"];
$csvsessiondata[]=$_SESSION["subdir"];
$csvsessiondata[]=$_SESSION["downloadtype"];
$csvsessiondata[]=$_SESSION["htmlprefix"];
$csvsessiondata[]=$_SESSION["fixalready"];
$csvsessiondata[]=$_SESSION["fixundownload"];
$csvsessiondata[]=$_SESSION["correctimagenumber"];
$fhandle=fopen("indata.csv","w");
fputcsv($fhandle,$csvsessiondata);
fclose($fhandle);
exec("php -q exec.php < /dev/null > exec.log &");
echo "Process started, it's take some time. Please press Details for check status<br>\n";
}
echo "<form action=index.php method=post>\n";
echo " <input type=submit name=\"deta4\" value=\"Details\">\n";
echo "</form>\n";
}
// Result page
if ($_SESSION["step"]==5) {
echo "RESULT:<br>\n";
if (file_exists("outdata.csv"))
{
$fhandle=fopen("outdata.csv","r");
$outdatacsv=fgetcsv($fhandle, 1024, ",");
fclose($fhandle);
$rawsedit=$outdatacsv[0];
$updatedurls=$outdatacsv[1];
$failedtoupdate=$outdatacsv[2];
$deletedurls=$outdatacsv[3];
echo "raws edited: ",$rawsedit,", urldecode downloaded: ",$updatedurls,", failed to download: ",$failedtoupdate,", deleted tags: ",$deletedurls,"<br>\n";
} else {
$logexec=file_get_contents("exec.log");
echo $logexec;
}
echo "<form action=index.php method=post>\n";
echo " <input type=submit name=\"deta5\" value=\"Details\">\n";
echo "</form>\n";
}
}
$_SESSION["step"]=0;
$_SESSION["error"]="";
$_SESSION["details"]="";
if ($_POST["next0"]) {
$_SESSION["dbhost"]=$_POST["dbhost"];
$_SESSION["dbuser"]=$_POST["dbuser"];
$_SESSION["dbpass"]=$_POST["dbpass"];
$_SESSION["dbname"]=$_POST["dbname"];
$_SESSION["template"]=$_POST["dbtype"];
$dbconnection=mysqli_connect($_SESSION["dbhost"],$_SESSION["dbuser"],$_SESSION["dbpass"],$_SESSION["dbname"]);
$_SESSION["step"]=1;
if (!$dbconnection) {
$_SESSION["step"]=0;
$_SESSION["error"]="dbconnection";
} if ($_SESSION["template"]=="Wordpress") {
$_SESSION["dbtable"]="wp_posts";
$_SESSION["dbcolumn"]="post_content";
$_SESSION["dirname"]="wp-content/uploads";
} if ($_SESSION["template"]=="Joomla") {
$_SESSION["dbtable"]="prefix_content";
$_SESSION["dbcolumn"]="fulltext";
$_SESSION["dirname"]="images/stories";
} if ($_SESSION["template"]=="Drupal") {
$_SESSION["dbtable"]="field_data_body";
$_SESSION["dbcolumn"]="body_value";
$_SESSION["dirname"]="sites/default/files";
} if (!$_POST["dbname"]) {
$_SESSION["step"]=0;
$_SESSION["error"]="dbconnectionname";
}
}
if ($_POST["prev1"]) {
$_SESSION["dbtable"]=$_POST["dbtable"];
$_SESSION["dbcolumn"]=$_POST["dbcolumn"];
$_SESSION["dirname"]=$_POST["dirname"];
$_SESSION["step"]=0;
} if ($_POST["next1"]) {
$_SESSION["dbtable"]=$_POST["dbtable"];
$_SESSION["dbcolumn"]=$_POST["dbcolumn"];
$_SESSION["dirname"]=$_POST["dirname"];
$dbconnection=mysqli_connect($_SESSION["dbhost"],$_SESSION["dbuser"],$_SESSION["dbpass"],$_SESSION["dbname"]);
$columns=mysqli_query($dbconnection, "SHOW COLUMNS FROM ".$_SESSION["dbtable"]);
if (!$columns) {
$_SESSION["error"]=mysqli_error();
$_SESSION["step"]=1;
}
if (mysqli_num_rows($columns)>0) {
$_SESSION["error"]="Column not found";
while ($row = mysqli_fetch_assoc($columns)) if ($row[Field]==$_SESSION["dbcolumn"]) $_SESSION["error"]="";
} else $_SESSION["error"]="Table is empty or not exists";
$uploadsdir=chdir("../".$_SESSION["dirname"]);
if ($uploadsdir) { if (!is_writeable(getcwd())) $_SESSION["error"]="Cant write to directory, check previlegies"; }
else $_SESSION["error"]="Cant open directory";
if (!$_SESSION["error"]) $_SESSION["step"]=2;
else $_SESSION["step"]=1;
}
if ($_POST["prev2"]) {
$_SESSION["step"]=1;
$_SESSION["subdir"]=$_POST["subdir"];
} if ($_POST["deta2"]) {
$_SESSION["details"]="step2";
$_SESSION["step"]=2;
$_SESSION["subdir"]=$_POST["subdir"];
$_SESSION["htmlprefix"]=$_POST["htmlprefix"];
$_SESSION["downloadtype"]=$_POST["dwnldtype"];
$_SESSION["fixbroken"]=$_POST["fixbroken"];
$_SESSION["fixundownload"]=$_POST["fixundownload"];
$_SESSION["fixalready"]=$_POST["fixalready"];
$uploadsdir=chdir("../".$_SESSION["dirname"]);
} if ($_POST["next2"]) {
$_SESSION["step"]=3;
$_SESSION["subdir"]=$_POST["subdir"];
$_SESSION["htmlprefix"]=$_POST["htmlprefix"];
$_SESSION["downloadtype"]=$_POST["dwnldtype"];
$_SESSION["fixbroken"]=$_POST["fixbroken"];
$_SESSION["fixundownload"]=$_POST["fixundownload"];
$_SESSION["fixalready"]=$_POST["fixalready"];
$uploadsdir=chdir("../".$_SESSION["dirname"]);
if ($_POST["fixbroken"]) { if (($_SESSION["correctimagenumber"]<1)&($_SESSION["brokenimagenumber"]<1)) $_SESSION["step"]=2; }
else if ($_SESSION["correctimagenumber"]<1) $_SESSION["step"]=2;
}
if ($_POST["prev3"]) {
$_SESSION["step"]=2;
$uploadsdir=chdir("../".$_SESSION["dirname"]);
} if ($_POST["next3"]) {
$_SESSION["step"]=4;
$uploadsdir=chdir("../".$_SESSION["dirname"]);
}
if ($_POST["deta4"]) {
$_SESSION["step"]=5;
}
if ($_POST["deta5"]) {
$_SESSION["step"]=5;
}
?><!DOCTYPE html>
<html>
<head>
<title>image replacer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php PageView();?>
</body>
</html>