PHP Code:
<?php
#Creates the entries for a folder full of documents in the sugar database
#Copy over the contents of the folder defined by $moved, to /cache/upload when you're done
#Variables start at line 6, line 23, then again at line 28
$host="localhost"; # MySQL data here
$user="root";
$password="SECRETPASSWORD!";
$database="sugar";
#Don't touch!
echo "logging in";
$con = mysql_connect("$host","$user","$password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
echo "failed to connect";
}
mysql_select_db("$database", $con);
#Put the folder of documents here... don't use ./ or weird stuff happens
ListFolder("./CGI");
function ListFolder($path)
{
$userID='da3308a4-9be7-ee40-7116-44ce699546c3'; #UID of the user you want to be uploading the documents
$category="Knowledge Base"; #Document Category
$subCategory="Documentation"; #Subcategory
$moved="c:/server/apache2/htdocs/TEST"; #the folder you want your files copyed over to.
#Copy the contents of this folder to /cache/upload when you're done
#No more variables below this line
#Leave dates alone unless you ... whatever
$date=date("Y-m-d H:i:s");
$roughDate=date("Y-m-d");
#using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
#display the target folder.
while (false !== ($file = readdir($dir_handle)))
{
if($file!="." && $file!="..")
{
if (is_dir($path."/".$file))
{
#Display a list of sub folders.
ListFolder($path."/".$file);
}
else
{
#Sets up the file to be uploaded then moved
$pathfile="$path/$file";
$ext = explode(".",$file);
$ext = $ext[1];
$guid=guid();
$DOC=guid();
$test="$moved/$guid";
echo "$file </br>";
echo "$moved </br> $guid </br> $test </br>";
#SQL commands created
$sql="INSERT INTO `$database`.`documents` (`id`, `document_name`, `active_date`, `exp_date`, `description`, `category_id`, `subcategory_id`, `status_id`, `date_entered`, `date_modified`, `deleted`, `modified_user_id`, `created_by`, `document_revision_id`, `related_doc_id`, `related_doc_rev_id`, `is_template`, `template_type`)
VALUES ('$DOC', '$file', '$roughDate', '$roughDate', NULL, '$category', '$subCategory', 'Live', '$date', '$date', '0', '$userID', '$userID', '$guid', '', '', '0', NULL);";
mysql_query($sql);
echo $sql;
$sql="INSERT INTO `$database`.`document_revisions` (`id`, `change_log`, `document_id`, `date_entered`, `created_by`, `filename`, `file_ext`, `file_mime_type`, `revision`, `deleted`, `date_modified`)
VALUES ('$guid', 'first entry', '$DOC', '$date', '$userID', '$file', '$ext', 'documentation', '1', '0', '$date');";
#SQL commands executed
mysql_query($sql);
echo "$sql </br>";
#Give the file a UID as its name, so it can be copied to /cache/upload
copy("$pathfile","$test");
}
}
}
#closing the directory
closedir($dir_handle);
}
#weeee! Stolen sugar functions!
function guid (){
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = sprintf("%x", $a_dec* 1000000);
$sec_hex = sprintf("%x", $a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}
function create_guid_section($characters)
{
$return = "";
for($i=0; $i<$characters; $i++)
{
$return .= sprintf("%x", mt_rand(0,15));
}
return $return;
}
function ensure_length(&$string, $length)
{
$strlen = strlen($string);
if($strlen < $length)
{
$string = str_pad($string,$length,"0");
}
else if($strlen > $length)
{
$string = substr($string, 0, $length);
}
}
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}
?>
Bookmarks