//the two functions most likely to be used by outside pages are
//cloudfiledocument_href - create a link to download a document, launch on the users desktop and then save the result back to $serverdirectory
//cloudfiledocument_convertto_documentids($serverdirectory) -- what ever serverdirectory you saved the files to, now save these files as a documentid and return array of the documentids
function cloudfiledocument_link($text,$documentid,$serverdirectory,$callbackfunction='',$onclickshowwaiting=0)
{ //this function downloads a document to the users system and monitors it, when it changes on their system, the updated PDF is posted back to the serverdirectory.
//this must be visible, so we create a div with overflow hidden only 1x1px
//serverdirectory should be unique to this user, you can come up with keywords and IDs which will make it so that a directory will be unique to a certain action, however becareful not to clober directories in use elsewhere
$useragent = get_useragent();
if(!$text)
{
developer_error("cloudfiledocument was called without a valid documentid");
ddie("invalid documentid ");
return;
}
if(!db_number($documentid))
{
developer_error("cloudfiledocument was called without a valid documentid");
ddie("invalid documentid ");
return;
}
if(!trim($serverdirectory))
{
developer_error("cloudfiledocument was called without a valid parameter serverdirectory");
ddie("invalid documentid ");
return;
}
$rnd = cloudfiledocument_getrnd($documentid, $serverdirectory, $callbackfunction);
if($onclickshowwaiting)
$addclicktext = " if (typeof(display_splash) == 'function'){display_splash('" . $useragent ."'," . $documentid . ");} "; // to use this you have to call splashboxinit('articleid') first.
// $(this).find('a').html('Downloading') ";
echo "$text";
}
function cloudfiledocument_convertto_documentids($serverdirectory, $documentidarr,$type,$id)
{
//@type you must pass in the shortname of the columnid on tbldocument // ie for messageid = 455 pass in $type = 'message' $id = 455
//this function takes an array of document ids, matches those up to what was 'captured' and converts each of those saved files to a document id, then returns an array of the converted documents.
if(!$documentidarr)
return array();
if(!is_array($documentidarr))
{
developer_error("Invalid content passed in to cloudfiledocument_convertto_documentids($serverdirectory,$documentidarr)", "The second parameter should be an array of documentids that we are supposed to copy to new documents");
return array();
}
$arr = cloudfiledocument_getcapturedfiles($serverdirectory );
d(arr,$arr);
$docs = array();
foreach($documentidarr as $k=>$documentid)
{
if(!$arr[$documentid])
{
//set a message if for somereason someone is saying the would like to copy the 'uncaptured' file to a new document
developer_error("cloudfiledocument_convertto_documentids error","one of the documentid s passed into cloudfiledocument_convertto_documentids: $documentid does is not one of the documents that was captured: ".print_r($arr,true));
ddie("ERROR");
}
$tocopy = $arr[$documentid];
d("Copy this file to a new document", $tocopy);
$extraarray[newfilename] = $tocopy[filename];
$extraarray[newfilename] = $tocopy[filename];
$docs[] = save_file_to_document($id, $type , $tocopy[filepath], $extraarray);
}
return $docs;
}
function cloudfiledocument_capturefile($rnd ,$frompath)
{
if(!($arr = cloudfiledocument_rndextract($rnd)) )
{
d("invalid rnd value: ($rnd) not foud in \$_SESSION[login][cloudfiledocument] ", $_SESSION[login][cloudfiledocument]);
return false;
}
extract($arr);//SESSION GET
if(!file_exists($frompath))
{
developer_error("Failed: cloudfiledocument_upload($documentid,$serverdirectory,$hash,$frompath) ", "$frompath: is not a valid file. Seems we got into this function withough validating some data, cannot proceed");
return false;
}
$savedir = $_SERVER[DOCUMENT_ROOT]."/tmp_pdf/$serverdirectory/";
$document = db_first("select * from tbldocument where documentid = ".db_number($documentid));
if(!$document)
{
developer_error("Failed: cloudfiledocument_upload($documentid,$serverdirectory,$hash,$frompath) ", "Document id: $documentid is not a valid document");
return false;
}
$documentname = $document[documentname];
$documentname = str_ireplace(".pdf",'.pdf',$documentname);
$documentname = str_replace("-S.pdf",'.pdf',$documentname);
$documentname = str_replace("-signed.pdf",'.pdf',$documentname);
$documentname = str_replace("-.pdf",'.pdf',$documentname);
$documentname = str_replace(".pdf",'-S.pdf',$documentname);
dlog("cloudfiledocument","save $_SERVER[REMOTE_ADDR] - ".$_SESSION[login][loginusername]." - $documentname($documentid)");
//$documentname = $documentid."-".$documentname; //i was gonna keep the documentid at one point, but no longer (just the -S)
$savepath = $savedir.$documentname;
rename_wrapper($frompath,$savepath,1);
d("rename_wrapper($frompath,$savepath);");
}
function cloudfiledocument_getcapturedfiles($serverdirectory )
{
$hashdir = $_SERVER[DOCUMENT_ROOT]."/tmp_pdf/$serverdirectory/";
$d = dir($hashdir);
$files = array();
$outfiles = array();
while($f = $d->read())
{
if(substr($f,0,1)!= '.')
$files[] = $f;
}
foreach($files as $f)
{
$documentid = list_first($f,"-");
$outfiles[$documentid][documentid] = $documentid;
$outfiles[$documentid][filename] = list_allbutfirst($f,"-");
$outfiles[$documentid][filepath] = $hashdir.$f;
}
return $outfiles;
}
function cloudfiledocument_getrnd($documentid, $serverdirectory, $callbackfunction)
{ //get the random $link to be used and passed in urls
$lnkhash[documentid] = $documentid;
$lnkhash[serverdirectory] = $serverdirectory;
$lnkhash[callbackfunction] = $callbackfunction;
if(!$_SESSION[login][cloudfiledocument])
$_SESSION[login][cloudfiledocument] = array();
$rnd = array_search($_SESSION[login][cloudfiledocument], $lnkhash); //find out if we already have the link
if(!$rnd)
$rnd = $documentid."-".randomchars(6);
$_SESSION[login][cloudfiledocument][$rnd] = $lnkhash;
return $rnd;
}
function cloudfiledocument_rndextract($rnd)
{ //get the random $link to be used and passed in urls
if($_SESSION[login][cloudfiledocument][$rnd])
return $_SESSION[login][cloudfiledocument][$rnd];
return array();
}
function cloudfiledocument_getcloudfile($rnd)
{
if(!($arr = cloudfiledocument_rndextract($rnd)) )
{
d("invalid rnd value: ($rnd) not foud in \$_SESSION[login][cloudfiledocument] ", $_SESSION[login][cloudfiledocument]);
return false;
}
extract($arr);//SESSION GET
cloudfiledocument_html($documentid,$serverdirectory,$callbackfunction);
}
function cloudfiledocument_html($documentid,$serverdirectory,$callbackfunction='')
{
$siteurl = get_site_rooturl();
$rnd=cloudfiledocument_getrnd($documentid, $serverdirectory, $callbackfunction) ;
$postbackurl=$siteurl."/_cloudfiledocument.php?postback=$rnd";
$doc = get_relativedocpath($documentid);
dlog("cloudfiledocument","download $_SERVER[REMOTE_ADDR] - ".$_SESSION[login][loginusername]." - $doc");
?>
}