Excel 2010 Download CSV with dynamic name from secure website - php

I am wanting to login to a site, navigate to a page, then download the .CSV file that will always end with a dynamic string due to it being 'custom'.
I have tried to access the site by recording a macro. However, as the data is not in a table the macro recorder is not able to pick up the actual address of the .csv file.
The display text is always:
Results [link]Click to Download[/link]
The html values are always:
<td class="smallText"><b>Results</b> <a href="vendor_report.php?report=custom [insert extremely long string here] ><u>Click to Download</u></a></td>
Without using a table, is there a way to get to this .csv & save it to my PC?
I am aware that the use of <td> denotes it is part of a table, but it is definitely not picking it up, I've gone through the site using the macro recorder and it's not picking up the inner contents from the page.
https://[domain].php?vf=vieworders
I had also thought to navigate to the site page, highlight the text, copy & paste to a spare sheet in my book, then use some code L42 previously wrote here (below) however I can't even get the copy & paste to work correctly.
For Each hlink In ThisWorkbook.Sheets("NameOfYourSheet").Hyperlinks
Set wb = Workbooks.Open(hlink.Address)
wb.SaveAs saveloc & hlink.Range.Offset(0,1).Value & ".xlsx"
wb.Close True
Set wb = Nothing
Next
Please advise. Thank you in advance.
UPDATE
I have found which table this is hiding in, Table 2. It is however in the midst of a lot of other text.
When I have copied & pasted the table contents to my sheet, I have problems getting the link to show as it's HTML value so I can then use that with my 2nd option (open links from spreadsheet).
This could be an issue with the original Get Data code I am using.
This is how it looks. The cells either side are filled, as well as that huge chunk of (blanked out) text in B20
Could Regex be of use here??

You could try using the XMLHTTP object with a stream:
Sub SO()
Dim objStream As Object, strURL As String
Set objStream = CreateObject("ADODB.Stream")
strURL = "vendor_report.php?report=custom [insert extremely long string here]"
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", strURL, False
.Send
If .Status = 200 Then
objStream.Open
objStream.Type = 1
objStream.Write .ResponseBody
objStream.SaveToFile "C:\users\bloggsj\output.csv", 2
objStream.Close
End If
End With
Set objStream = Nothing
End Sub
Change the save path as required.

Related

PHPExcel generated Excel Error - Links cannot be updated

My PHPExcel generated excel contains a barchart and a pie chart and is automatically fired to an email. When the email attachment is downloaded and opened (you have to click enable editing) it displays this alert box:
When the email attachment is downloaded and opened (you have to click enable editing) it displays this alert box:
and behind this you can see the expected data in their cells and a blank canvas for the charts.
I close the excel document and open it again, this time I get the expected chart displayed along with data sets overlayed by this alert box:
I close the excel document and open it again, this time I get the expected chart displayed along with data sets overlayed by this alert box:
Clicking on the update link in the second alert box resolves to launching the first alert box. Clicking continue in that resolves to the chart going blank, only showing a blank canvas with axis.
My code uses extensive variables to determine the cell location for each data field. I tried using EXIT at the end of the script as advise by one of the answers around here. That didn;t work.
Code Sample:
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$StartRow, 'Date')
->setCellValue('B'.$StartRow, $ndate)
->setCellValue('C'.$ParticipantStartLabel, 'Enrolled')
->setCellValue('D'.$ParticipantStartLabel, 'Logged In')
->setCellValue('E'.$ParticipantStartLabel, 'Yet to Log In')
->setCellValue('F'.$ParticipantStartLabel, 'Monitors (BOI, EDC and AMI)')
->setCellValue('B'.$ParticipantStartDetails, 'Participants on the program')
->setCellValue('C'.$ParticipantStartDetails, $ParticipantEnrolled)
->setCellValue('D'.$ParticipantStartDetails, $ParticipantLoggin)
->setCellValue('E'.$ParticipantStartDetails, $ParticipantYettoLog)
->setCellValue('F'.$ParticipantStartDetails, $Monitors) //$Monitors
->setCellValue('A'.$CoursesStartRow, 'S/N')
->setCellValue('B'.$CoursesStartRow, 'Course')
->setCellValue('C'.$CoursesStartRow, 'Enrolled')
->setCellValue('D'.$CoursesStartRow, 'Completed')
->setCellValue('E'.$CoursesStartRow, 'Incomplete');
//print courses for single dataset
for( $i = 0 ; $i < $NumCourses; $i++ ){
$newi = $i + 1;
$newCoursesStartRow = $CoursesStartRow + $newi;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$newCoursesStartRow, $newi)
->setCellValue('B'.$newCoursesStartRow, $Courses[$i]) // continue to Add some data on courses
->setCellValue('C'.$newCoursesStartRow, $CoursesEnrolled[$i])
->setCellValue('D'.$newCoursesStartRow, $CoursesCompleted[$i])
->setCellValue('E'.$newCoursesStartRow, $CoursesIncompleted[$i]);
}
Any help to decipher why the document would not just open with displaying this distracting alert boxes is appreciated.
It's called security, and Microsoft have applied it in all recent versions of MS Excel. The purpose is to prevent any malicious excel files from executing arbitrary code. Unfortunately, because some people have chosen to use Excel spreadsheets for malware, Microsoft have chosen to provide these warnings to indicate that they can't guarantee that this spreadsheet may contain dangerous content, and that the user opening it must make a conscious decision to accept the risks.
Generally, you would only get this error if the spreadsheet references other spreadsheet files, or if it tries to access remote data. Are you building your file froma template that may contain these?

Writing to a text file on a web-server with Visual Basic

I am working on a project where I have a simple conveyor program done in Visual Basic. I have a button on a website which when pressed writes a "1" into a text file via a simple PHP server program. The VB conveyor system monitors the text file and if it has the value of "1" the conveyor stops. I then have a reset button on the VB program that when pressed should write a "0" into the text file and allow the conveyor to restart. I was able to get the VB program to monitor the text file, and it works accordingly, but I can't get the reset button to write the "0" into the text file.
The code that reads the file is:
Dim remoteStatus As New WebClient()
Dim data = remoteStatus.DownloadString("http://www.users.wfleitz.com/fleitzwc/example.txt")
Console.WriteLine(data)
Console.Read()
Dim remoteEnable As Boolean
If data.Trim = "0" Then
remoteEnable = True
ElseIf data.Trim = "1" Then
remoteEnable = False
Else
remoteEnable = False
End If
The code I have been trying to use to write to the text file is:
Private Sub PushButton1_Load(sender As Object, e As EventArgs) Handles PushButton1.Click
Dim resetEvent As New WebClient()
Dim textAddress As String = "http://www.users.miamioh.edu/fleitzwc/example.txt"
Dim resetCommand As String = "0"
Dim resetArray As Byte() = Encoding.ASCII.GetBytes(resetCommand)
Dim resetStream As Stream = resetEvent.OpenWrite(textAddress, resetCommand)
resetStream.Write(resetArray, 0, resetArray.Length)
resetStream.Close()
End Sub
One other thing that i was thinking about, was rather than have the VB program write to the text file, have it talk to the PHP file that contains the code for the website, and have it send the "0" value. I didn't know if that would be cleaner or not.
If anyone could point me in the right direction it would be greatly appreciated.
Thank you in advance,
Wfleitz
What I would do in your case is use PHP as you said.
<?php
/*
Upload this to http://www.users.miamioh.edu/fleitzwc/script.php or something like that.
Do either
http://www.users.miamioh.edu/fleitzwc/script.php?bar=get or
http://www.users.miamioh.edu/fleitzwc/script.php?bar=reset
*/
$foo = $_GET['bar'];
if ($foo=="get"){
echo file_get_contents("example.txt");
}
if ($foo=="reset"){
file_put_contents("example.txt","0");
}
This should work finely for what you are doing. It is somewhat incomplete though because I didn't fully pay attention to the details of the question but I think you are able to modify it to work with your needs. If not, I can update it.

Temporary storage on a server

I'm looking for an efficient and secure way to store small amount of user data like a line of text, for an indefinite period of time.
The scenario: One client 'A' sends some data to another client 'B' , asynchronously via a server 'S'. For simplicity, consider the data to be a single line of text. Now, this data would be delivered to client 'B' by server 'S' , only when B asks for it. So until that time Server 'S' has to store this data.
For a simple demo, I implemented it very crudely as follows: Client 'A' does a POST request with XMLHttpRequest, sending the text to 'S' as follows
xhr.open("POST",url+"?sentText=text"); //url points to a php file on S
xhr.send();
The PHP code then saves the received text to a file as follows:
<?php
$selected = $_GET["sentText"];
$writefile = "text.txt";
$fh = fopen($writefile,'w');
fwrite($fh,$selected);
fclose($fh);
?>
Later, after any amount of time, B asks for the text in this file,using BufferedInputStream as follows:
URL fileurl = new URL("url/text.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(fileurl.openStream()));
while((tmpReceived = in.readLine())!=null){
received = tmpReceived;
}
This demo works properly, but this is just a demo. However, if this were to be a full scale real world application with millions of users, how would I implement the following:The part where server S stores the text. Obviously, I can't save the text as plain text in a file, as it would breach my users' privacy. And it would also be ineffecient when there are millions of users. How do commercial services like Dropbox achieve this?
To keep things simple, I just need to know how to do this for text, then I can extrapolate to other types possibly.
Thanks a lot!

How can I make this PHP code more efficient? (Editing a line in a CSV file)

This code works, but I just hacked it together with my limited knowledge of PHP and I'm sure there's a more elegant and efficient way to go about it. If you'd be so kind as to point out how I can improve, that would be great!
So I have a CSV file, structured like so:
Code Class Value Status Date Created Date Redeemed
========================================================================
a51f3g45 gold 50 valid 2012-08-20
4f6a2984 silver 200 redeemed 2012-08-23 2012-08-27
gf3eb54b gold 150 valid 2012-08-30
etc...
The user fills out a form to change the Class, Value, and Status fields of a given line. I cobbled together the following code to replace the old values with the new ones:
$file = 'codes.csv';
$old_csv_string = file_get_contents($file);
preg_match('/('.$_POST['code'].',.*,.*,.*,.*,.*)\n/',$old_csv_string,$matches);
$old_row = $matches[1];
preg_match('/'.$_POST['code'].',(.*,.*,.*),.*,.*\n/',$old_csv_string,$matches_part);
$old_row_part = $matches_part[1];
$new_row_part = $_POST['class'].",".$_POST['value'].",".$_POST['status'];
$new_row = str_replace($old_row_part,$new_row_part,$old_row);
$new_csv_string = str_replace($old_row,$new_row,$old_csv_string);
file_put_contents($file,$new_csv_string);
So can I do better than 10 lines of code? Any advice would be greatly appreciated :)
Note: I tried using fgetcsv, but I couldn't figure out how to find the unique code within a 2D array, then replace its siblings.
Why are you doing this ?
I think you should store the data in a SQL table.
Each time user update data, do it in the table.
If you want the CSV to be downloadable at any moment. Use a .htaccess to redirect your.csv to csv_generator.php only if your.csv does not exist.
csv_generator.php will regenerate the whole csv if it does not exist, save it on hard drive for later use, and send it with correct mime/type in header (so it's transparent for user). User don't see he is requesting a php page.
Then you need to delete the csv on hard drive each time someone update the data (so it will be regenerated on next request)
I think this is the way to have an always ready to download csv online.
Do you know google doc does this ? Users can change data in a spreadsheet wich is available to download as a csv from a url (you need to publish this spreadsheet as a csv file).
Try using split like that for each line:
list($code, $class, $value, $status, $created, $redeemed) = split(",", $line, 6) ;
Thus you will have each field in separate variable.
Of course you need to take care of the first row in case you don't want to copy header.

Open txt files from a directory, compare the values, and output the top 15 in PHP

I recently designed a referral game website for the fun of it.
There's a simple MySQL user system with a email verification. It's using the UserCake user management system.
On top of this i added a php page that the user could give to "victims" that when they visit it they get "infected" and can infect other users or "victims". This page uses GET to get the username from the url. I have a folder that when a user registers it creates a file with 4 digits and then the username. (ex; 0000Username.txt) All the numbers are the same, it's just so that if a user discovers the folder they won't be able to find the files. There is also a txt file in the same format with IPS in the name. (ex; 0000IPSUsername.txt) The file when visited gets the username from the url, then checks if the text file for that username exists. If the username is present in the url, and a valid user it opens the IPS file and adds the IP of the visitor, then opens the user text file, takes the value and adds one to it, and saves. At the end it makes the difference between saying "You are infected, Username has infected (amount) people." or just you have been infected.
Now to what i need!
I need to add a hi-scores to the website so people can compete to be the one with the most "infections".
I thought i could use readdir to get a list of the files and open them with the value in an array, but i need it to also strip the username from the file name. It would be best if it just saves to a text file like "Username | value" because then i can add echo's of the html tags and have it include the file in the page i want it to be one.
Many thanks in advance.
Try using an array and count($array) to count the files.
Stripping the values out is as simple as using PHP's str_replace function. More here: http://php.net/manual/en/function.str-replace.php
With the 0000Username.txt file try using json.
// Get
$contents = file_get_contents('0000Username.txt');
$data = json_decode($contents); // PHP > 5.2.0
// Put
$data['username'] = 'Username';
$data['infectious'] = $data['infectious'] + 1;
$contents = json_encode($data); // PHP > 5.2.0
file_put_contents($contents);

Categories