powershell - check if pdf is encrypted - pdf

Using powershell I need to loop a series of pdf file and make some operation on them using pdftk. I'd like to know if exists some method to detect if pdf is encrypted or not. In this way, if the pdf is encrypted I don't work on it and my loop skips to the next file. Thanks for the attention.
edit. While I wait for some answer I've found that itextsharp has an isencrypted method.
After I load the assembly
[System.Reflection.Assembly]::LoadFrom("c:\my_path\itextsharp.dll")
what do I have to do to use the above method?

[System.Reflection.Assembly]::LoadFrom("c:\itext\itextsharp.dll")
$itext = new-object itextsharp.text.pdf.PdfReader("c:\itext\1.pdf")
$itext.isEncrypted()
You should get either true or false as a result.

For the people that reach this page searching for a way to check if files are NTFS encrypted, this is the way to go:
[System.IO.File]::GetAttributes($RootFolder).ToString().Contains("Encrypted")

Related

PHPSpreadsheet to Read Passworded XLSX

I've spent all day searching both the documentation and the web but there doesn't seem to be a way to programmatically open a passworded spreadsheet with phpspreadsheet; and spout library for that matter.
In Phpspreadsheet, I'm interested in the xlsx reader and have followed the inheritance chain to the root class but none have a "setpassword" type method (I notice writer class has though.)
Is this a true oversight in both libraries or have I missed something somehow? Is there any other way to do this in PHP? Or next best, strip the password off a set of xlsx files programmatically and then read it with either library?
Seeing no other solution, I solved this by writing VBA to open each file and save without the password. Then I could process with PHPSpreadsheet.

Form a Excel file (PHPExcel) and mail it as an attachment (PHPMailer) without server storing, possible?

I want to form a file (PHPExcel) and mail it as an attachment (PHPMailer) without server storing, is it possible?
I'm aware of the possibility of forming/creating file in PHPExcel and sending it as an attachment thru PHPMailer here. But it works thru/by writing a file somewhere in a server. Poor as far as server resources consumption.
PHPExcel allows to output this way directly without saving on a server:
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
...
$objWriter->save('php://output');
Is it possible (and how) to attach it to email on the fly similar as save('php://output')?
To send an attachment without using local files, use the addStringAttachment() method. For example:
$string = $mything->getExcelData();
$mail->addStringAttachment($string, 'myfile.xls');
Internally this will call PHPMailer::filenameToType(), which will set the appropriate application/vnd.ms-excel MIME type for the .xls extension.
In general you probably don't need to worry too much about memory consumption for doing this - PHPExcel itself is far more memory-intensive than simply storing this string temporarily, so if you are running into trouble, you will likely do so before you ever get this far

Stopping invalid file type or file name submissions in coldfusion

So, I'm having this lovely issue where people like to submit invalid file types or funky named files... (like.. hey_i_like_"quotes".docx) Sometimes they will even try to upload a .html link...
How should I check for something like this? It seems to create an error every time someone submits a poorly named item.
Should I create a cfscript that checks it before submission? Or is there an easier way?
If it was before submission it would be javascript not cfscript. Javascript can always be got round, so I'd say you'd be better doing it server-side with ColdFusion. Personally I'd just wrap the whole thing in a try/catch (you should do this anyway as a matter of course with all file upload type things), and throw an error back at them if their filename is no good.
When you say submit are you using cffile to allow your users to upload file.
If so, use the attribute "accept" with a try and catch around. for example....
<cftry>
<cffile action = "upload"
fileField = "FileContents"
destination = "c:\files\upload\"
accept="image/jpg, application/msword"
>
<cfcatch type="Any" >
<p>sorry we could not upload your file!</p>
</cfcatch>
</cftry>
I personally would not use "just" JavaScript as this could be disabled and you are back in the same boat.
Hope this helps.
On the server, as part of validation, use reFindNoCase() along with an appropriate regex to check for a properly formatted file path. You can find lots of example regex expressions for a file path on the internet, such at this one. Hope that helps.
As #Duncan pointed out, a client-side validation would most likely be in JavaScript. Personally, if I had time/resources, I would do this as a convenience for the end user. If they upload an enormous PDF when a DOCX is required by the system, it would be annoying for them not to receive a message until the upload is complete.
As far as filenames go, it seems to me that the simplest solution (and one I've used in the past) is to assume all filenames are bad, and rename them. There are several ways to do this. If you need to preserve the original filename, I would just use urlEncodedFormat() ot clean the filename into something that is web-friendly. If you need to preserve all versions, you can append a date/time stamp, so bob.xocx becomes bob_201104051129.docx or somesuch. If you must keep the original filename without any changes, I would recommend seting up a DB table as a pinter system, keeping the original name, timestamp, and other metadata there and referring to the file by renaming it to the ID.
But urlEncodedFormat() is probably enough for what you've outlined.
For user experience it's best to do it client-side but it's not bad at all to double check server side too.
For the client side part, I recommend using the jQuery validation plugin, easy to use.

VB check inside a folder to see if there are files

I am trying to get my vb.net application to look inside a folder on the web server and then let me know whether there are files in there or if the folder is empty...Would anybody know where i would begin? Thanks
Use the DirectoryInfo.EnumerateFiles() method.
Dim myDir as DirectoryInfo = new DirectoryInfo(pathToDir)
If (myDir.EnumerateFiles().Any())) Then
' Got files in direcotry!
End If
If you are also interested in finding out if there are directories within this one, there is also DirectoryInfo.EnumerateDirectories().
I would suggest you have a look at Directory.GetFiles
If your program is running on the web server, you can simply check whether Directory.GetFileSystemEntries(path) returns anything.
If your program is running on a client, you'll need to make a server-side script that calls Directory.GetFileSystemEntries and returns a value to the client.

VB.NET - Read lines from command line to Windows form possible?

Hey Overflow, I have an application which serves as a user interface for a spartan/command line program.
I have the program running on a separate process, and my application monitors it to see if it is responding and how mush CPU it is utilising.
Now I have a list of files in my program (listbox) which are to be sent to the application, which happens fine. But I want to be able to read text from the com-line so as to determine when the first file has been processed.
Com-line says one of "selecting settings", "unsupported format" and "cannot be fixed".
What I want to be able to do is when it says one of these three things, remove item(0) in listbox1.
Is this possible?
I thought of programming an event which handles com_exe.print or something or other, if possible.
Read the standard output from the process.
MSDN Article
Theres an example of synchronous reading from the process in that article.
You might be able to do what you want using the AttachConsole API function as described here. However, maybe an easier alternative would be if you could pipe the output of the command line app to a text file and then your app could just parse the text file (assuming that the command line file wouldn't lock the file completely, I'm not sure about that).
If you don't know how to pipe the output, this page has quite a bit of information.