cfftp : Copy and move file remotely? - coldfusion

I need to copy files from one folder to the another on the same sftp server. My code currently copies the files locally and reuploads them.
<cfftp
action = "open"
username = "#APPLICATION.intxml.SFTPUSERNAME#"
password = "#APPLICATION.intxml.SFTPPASSWORD#"
connection = "sftpcon"
server = "#APPLICATION.intxml.SFTPADDRESS#"
port = "#APPLICATION.intxml.SFTPPORT#"
timeout = "#APPLICATION.pageTimeout#"
secure = "#sftp#"/>
<cfif cfftp.succeeded>
<cfftp action = "LISTDIR" stopOnError = "No" name = "ListFiles" directory = "/#sfolder#" connection = "sftpcon"/>
<cfloop query=getFiles>
<cfftp action = "GETFILE"
stopOnError = "Yes"
name = "theFile"
transferMode = "binary"
timeout = 3600
retrycount = 10
remoteFile = "#sfolder##name#"
localFile = "#dfolder#/#name#"
failIfExists = "no"
connection = "sftpcon">
<cfftp action = "PUTFILE"
stopOnError = "Yes"
name = "theFile"
transferMode = "binary"
timeout = 3600
retrycount = 10
localfile = "#sfolder##name#"
remoteFile = "#dfolder#/#name#"
failIfExists = "no"
connection = "sftpcon">
</cfloop>
</cfif>
<cfftp action = "close"
connection = "sftpcon"
stopOnError = "Yes">
Is there a better way to do this with coldfusion?

With CFFTP you can do a RENAME (check the docs) which should solve your problem

Found out that the ftp protocol does not provide a way to remotely copy a file into another folder and keep the original. That precludes coldfusion from providing a solution. Moving files on the other hand can be done with a rename. See the other answer and comments on the original question.

Code to show directories of files:
<cfftp
username= "username"
password= "password"
port= "22"
server= "hostofyousystem"
secure= "yes"
name= "ftpconnection"
action= "listdir"
directory= "/" />
<cfdump var="#ftpconnection#" />
<cfoutput query="ftpconnection">
#path#<br/>
</cfoutput>
Code to get file and save it in our system if file exist then replace:
<cfftp
username= "username"
password= "password"
port= "22"
server= "hostofyousystem"
secure= "yes"
action= "getFile"
remotefile= "/myfile.csv"
localfile= "D:/web/files/Data/thisfile.csv"
failIfExists="no" />

Related

Migrated to CF2018 and having problems with sessions

I recently migrated an app from CF2010 to CF2018 and we're having problems on sessions when a user logs in.
Adding some dumps and aborts I see that the session is successfully set on a valid login, but when using cflocation or cfheader it loses the session (application.cfc reruns onSessionStart). My application.cfc looks like:
this.applicationTimeout = createTimeSpan(0,8,0,0);
this.sessionmanagement = true;
this.clientmanagement = false;
this.sessiontimeout = createTimeSpan(0,0,20,0);
this.scriptProtect = "all";
this.setClientCookies = true;
this.showDebugOutput = false;
this.enablecfoutputonly = false;
onSessionStart is pretty simple:
public void function onSessionStart() {
lock scope="session" type="exclusive" timeout="10" {
session.started = now();
session.loggedIn = false;
};
lock scope="application" type="exclusive" timeout="5" {
application.sessions = application.sessions + 1;
};
writeLog(file = "g-session-log", type = "information", application = "no", text = "session started:");
};
I can see the log file gain an entry when the login page is processed. In the server admin I have "Use J2EE session variables" and "Enable Session Variables" both checked. Cookie timeout is 1440, HTTPOnly is checked and "Disable updating ColdFusion internal cookies using ColdFusion tags/functions" is checked.
Found the issue - answering in case anyone else has this problem. In my onApplicationStart() I set an http and https siteroot. On <cflocation> called application.secureSiteRoot but because I was moving the SSL cert over for testing I had it set to http, not https. That prevented the cookies from being set.

Get subject new mails from a office365 mail account

I am trying to download the subject of all new mails. The mails are stored in an office365 mail account. So far i have the following:
<cfimap
action ="OPEN"
connection = "Test"
password = "xxxx"
port = "993"
secure = "yes"
server = "outlook.office365.com"
stoponerror = "true"
timeout = "10"
username = "xxxx">
<cfimap
action="getHeaderOnly"
folder="Inbox"
connection="Test"
name="getHeaders"
>
<Cfdump var=#getHeaders#>
<cfimap action="close" connection = "Test">
This is ridiculously slow (several minutes). In my situation I only need to download the subject line of all new mails. I do not need anything else. Any thoughts on how to speed up things.
Update
Came up with an alternative solution. See Convert java code to coldfusion code for an alternative to the cfimap tag.

cflocation and session variables

My user comes from a 3rd party site via http post with login credentials encrypted in the URL.
Once the index.cfm recognizes these variables, it sends the request to:
<cflocation url="login.cfm?vals=#URLEncodedFormat(url.vals)#" addtoken="yes">
The login.cfm builds a session struct if the login credentials are valid.
Session.user.userID = 1;
Session.user.firstName = "jo";
Session.user.lastName = "boo";
Then, it does:
<cflocation url="somepage.cfm" addtoken="yes">
When I dump the session variable in somepage.cfm, I do not see the session.user struct. Also, I keep seeing different cfid, cftoken on somepage.cfm every single I refresh. I am on ColdFusion 10.
Any ideas? How can I keep the session.user?
Edit: application.cfc has
this.name = "My Application";
this.applicationTimeout = createTimeSpan(0,2,0,0);
this.clientManagement = true;
this.loginStorage = "session";
this.sessionManagement = true;
this.sessionTimeout = createTimeSpan(0,0,30,0);
this.setClientCookies = true;
this.setDomainCookies = false;
this.scriptProtect = "all";
this.javaSettings = {LoadPaths = ["#GetDirectoryFromPath(GetCurrentTemplatePath())#java/"], reloadOnChange=true, watchInterval=180};
EDIT: here are the files
http://1drv.ms/1kjnQO2
Unzip them to your C:\ColdFusion10\cfusion\wwwroot\
then go to :
http://localhost:8500/test/call.cfm
EDIT: 19:00 - 10Jun:
Wow, this really sucked! Came home, downloaded the zip, opened it up to localhost. When I run, I can see the session.user variables from call.cfm.
<cfdump var="#server#">
gives me:
coldfusion
struct
InstallKit Native Windows
appserver J2EE
expiration {ts '2012-10-30 10:35:35'}
productlevel Developer
productname ColdFusion Server
productversion 10,0,0,283111
rootdir C:\ColdFusion10\cfusion
I am not sure if this has something to do with the CF server version.
UPDATE: 09:00 11-Jun.
Once I disable "Use J2EE session variables " in CFAdmin, the session variables are visible to call.cfm. Now, I have to make it work with J2EE session variables when they are enabled.

Login with gmail account in Coldfusion

I want to login with gmail/google account and I found this tutorial Gmail Login in Coldfusion. I done All the steps and After login my page redirect then I want to display user Profile information so I dump this
<cfdump var="#session.profilesArray#">
but it gives me an empty array.why I am not getting my profile data after successfully lo-gin.
If I am getting wrong way for fetching my profile then what is correct way. Thanks.
You just add this line into your scope
Open your Application.cfc and then add this code
change scope = "https://www.googleapis.com/auth/analytics.readonly" with scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile
you can just add scope = "https://www.googleapis.com/auth/userinfo.profile but if u want to access email then add second one as I Post in my answer.
<cfset request.oauthSettings =
{scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile",
client_id = "Your-id",
client_secret = "your-secret",
redirect_uri = "redirect-page",
state = "optional"} />
Now you can get User Information from function that you can call like this
<cfscript>
public function getProfile(accesstoken) {
var h = new com.adobe.coldfusion.http();
h.setURL("https://www.googleapis.com/oauth2/v1/userinfo");
h.setMethod("get");
h.addParam(type="header",name="Authorization",value="OAuth #accesstoken#");
h.addParam(type="header",name="GData-Version",value="3");
h.setResolveURL(true);
var result = h.send().getPrefix();
return deserializeJSON(result.filecontent.toString());
}
</cfscript>
<cfoutput>
<cfset show = getProfile(session.ga_accessToken)>
<cfdump var="#show#">
</cfoutput>
Hope this will help you.

elmah - cannot email exceptions

Setting up elmah has been relatively painless for me. Until I tried to use the email facility. I have read through all the previous questions on the subject but just can't get it going. Here are the relevant entries in my web.config file . This just represents one of the many attempts that I have made, this one using gmail and . What am I doing wrong?
<elmah>
<errorMail
from="myusername#gmail.com"
to="me#myemail.com"
subject="elmah exception"
async="true"
smtpPort="0"
useSsl="true" />
</elmah>
<system.net>
<mailSettings>
<smtp deliveryMethod ="Network">
<network
host="smtp.gmail.com"
port="587"
userName="myusername#gmail.com"
password="..." />
</smtp>
</mailSettings>
</system.net>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
</httpModules>
EDIT
Just to prove the settings, I have managed to successfully send an email from my app using gmail. The settings used were equivalent and yet I still cannot get elmah to send an email. Hers is the code snippet.
MailMessage mailObj = new MailMessage();
mailObj.Subject = "gmail test";
mailObj.From = new System.Net.Mail.MailAddress("myusername#gmail.com");
mailObj.To.Add("test#myemail.com.au");
mailObj.Body = "Test Email";
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
NetworkCredential basicCredential = new NetworkCredential("myusername#gmail.com", "mypassword");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Send(mailObj);
Even some confirmation that it all appears correct would be useful.
the port is 465 and the protocol is SSL.
Did you consider any ill effects from GMail's 2-factor authentication? I was just having this problem and created an application-specific password for my development work.