Mercury mail and php email creation script - php

I need help creating a script or program that can add users to my mercury mail server when they sign up on a form. I'm using a basic php post form, it does create all the necessary files to run the account but when I open mercury mail the new user account has not been added to the accounts list. And the new account cannot sign in.
Please assist me in creating a client email signup script or program so that my clients can easily create an email on my server for free.
Link to the form code: http://pastebin.com/bEtv4eck
Link to the php post code: http://pastebin.com/rwBJatap
P.S. I have the script working to where it can create the new user and everything, but it won't allow a login unless the email server is restarted. Any way to fix this?

Try using the RELOAD USERS after making the changes to the pmail.usr and mail, directories.

You need to also add user in the PMAIL.USR file
Try
const MERCURY_PATH = 'C:\Apache\xampp\MercuryMail';
$userFile = MERCURY_PATH . DIRECTORY_SEPARATOR . "PMAIL.USR";
$mailDir = MERCURY_PATH . DIRECTORY_SEPARATOR . "MAIL";
$newName = "Baba Konko";
$newUsername = "baba";
$newPassword = "pass";
$host = "localhost";
if (! is_writeable ( $userFile )) {
die ( "You don't have permission to Create new User" );
}
if (! is_writeable ( $mailDir )) {
die ( "You don't have permission to add mail folder" );
}
// Check if user exist
if (is_file ( $userFile )) {
$users = file ( $userFile );
foreach ( $users as $user ) {
list ( $status, $username, $name ) = explode ( ";", strtolower ( $user ) );
if (strtolower ( $newUsername ) == $username) {
die ( "User Already Exist" );
}
}
}
$userData = "U;$newUsername;$newName";
$fp = fopen ( $userFile, "a" );
if ($fp) {
fwrite ( $fp, $userData . chr ( 10 ) );
fclose ( $fp );
}
$folder = $mailDir . DIRECTORY_SEPARATOR . $newUsername;
if (! mkdir ( $folder )) {
die ( "Error Creating Folder" );
}
$pm = '# Mercury/32 User Information File' . chr ( 10 );
$pm .= 'POP3_access: ' . $newPassword . chr ( 10 );
$pm .= 'APOP_secret: ' . $newPassword . chr ( 10 );
$pmFile = $folder . DIRECTORY_SEPARATOR . 'PASSWD.PM';
file_put_contents ( $pmFile, $pm );

Related

Downloading file to server from url

I have a laravel application where I download files to my server from given URLs. I am using the following code to do this.
$file_name = $files_directory . str_replace( " ", "-", $_POST['file_name'] ) . $_POST['file_extension'];
if ( file_put_contents( $file_name, fopen( $file_url, 'r' ) ) !== false ) {
$success = true;
$msg = "File Downloaded Successfully";
}
I am using user input to create a filename and extension. Is there a way to get the filename and extension from the URL response? Or is there a better way to approach this problem?
I think, you will have problems with the solution . Because you havn't put try/catch cases and you hasn't validated file extensions. And these can bring security issuses in future. You have to change your script like this:
$file_name = $files_directory . str_replace( " ", "-", $_POST['file_name'] ) . $_POST['file_extension'];
try {
if(in_array(mb_strtolower($_POST['file_extension']), ['jpg','png','...permitted_extenions.....'])){
if ( file_put_contents( $file_name, fopen( $file_url, 'r' ) ) !== false ) {
$success = true;
$msg = "File Downloaded Successfully";
}
}else throw new Exception('Errors with extention');
}catch(\Exception $e){
echo $e->getMessage();
}

How to format data when writng to txt file with php?

This is my code:
<?php
//check if the form allows user input to be extracted
if(isset($_POST['email']))
//if so loop begins
{
//creates a variable called $data that contains the user input for a particular input name from the form
//writes to txt file
$myfile = fopen("form.txt", "w") or die("Unable to open file!");
$email = "Email: ";
fwrite($myfile, $email);
fclose($myfile);
//appends to txt file
$data=$_POST['email'];
//creates a variable called $fp that contains the function fopen which opens a file called form.txt
$fp = fopen('form.txt', 'a');
//initiates the function fwrite which displays the user input in the txt file
fwrite($fp, $data); //when echo in html use div center tag
//closes txt file with the function fclose
fclose($fp);
}
if(isset($_POST['title']))
{
$data=$_POST['title'];
$fp = fopen('form.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
if(isset($_POST['date']))
{
$data=$_POST['date'];
$fp = fopen('form.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
if(isset($_POST['link']))
{
$data=$_POST['link'];
$fp = fopen('form.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>
I want to know how to write the user input to the txt file in php with a space between the header and the input and a linebreak after every one. Whenever I try to use the 'w' more than once the text from the first time it was used is not displayed.
Whenever I try to use the 'w' more than once the text from the first time it was used is not displayed.
Referring to the manual fopen
'w' - write mode will create a new file or reset the pointer to the beginning of the file (overwrite the existing content)
'a' - append mode will set the pointer to the end of the file (add content to the end)
So, that's why you're seeing that behaviour.
With regards to
I want to know how to write the user input to the txt file in php with a space between the header and the input and a linebreak after every one.
I'm unclear from your code and comments what exactly you consider to be the Header, Input and "each one". You code refers to a non-existent loop.
So, with the assumption that this actually all just executes in one go and "header is $email and that "input" and "each one" is every instance of $data. Your code should look something more like the following.
(Caveat: I only had a couple of minutes so it could be improved upon with string formatting and such, and I am assuming some php versioning)
Use PHP_EOL for cross-platform end of line. Refer When do I use the PHP constant "PHP_EOL"? for more information.
Again an assumption that this is all one post action and that you want a clean file for each email. If so, from the code you've provided, there appears no need to open the file multiple times.
if( isset( $_POST['email'] ) ) {
$myfile = fopen("form.txt", "w") or die("Unable to open file!");
fwrite($myfile,
"Email: " . $_POST['email'] . PHP_EOL .
( isset( $_POST['title'] ) ? ( $_POST['title'] . PHP_EOL ) : '' ) .
( isset( $_POST['date'] ) ? ( $_POST['date'] . PHP_EOL ) : '' ) .
( isset( $_POST['link'] ) ? ( $_POST['link'] . PHP_EOL ) : '' ) .
);
fclose($myfile);
}
However, if all fields are not captured at the same time, and there is no need for ordering of the entries to be relevant in the file, nor for it to be "clean" then just use append mode instead of write mode.
$myfile = fopen("form.txt", "a") or die("Unable to open file!");
fwrite($myfile,
( isset( $_POST['email'] ) ? ( "Email: " . $_POST['email'] . PHP_EOL ) : '') .
( isset( $_POST['title'] ) ? ( $_POST['title'] . PHP_EOL ) : '' ) .
( isset( $_POST['date'] ) ? ( $_POST['date'] . PHP_EOL ) : '' ) .
( isset( $_POST['link'] ) ? ($_POST['link'] . PHP_EOL ) : '' )
);
fclose($myfile);

Generate HTML file from Gravity Form Submission

How do I generate an html file (with specific styling) from a Gravity Form submission using PHP? Right now, I'm using Gravity Form with Gravity PDF to generate PDFs, but I also need these pdfs to be generated as html files. I was told that I could use a gf hook called gform_notification. Here is what I have so far:
add_filter( 'gform_notification_30', 'add_attachment_html', 10, 3 ); //target form id 2, change to your form id
function write_html($filename, $entry){
$filename = fopen( 'file_'.'rand(0, 999999)'.'.html', "w");
$text = $entry;
$path = '/public_html/wp-content/uploads/HTML/';
fwrite($path.$filename, $text);
fclose($filename); }
function add_attachment_html( $notification, $form, $entry ) {
//There is no concept of user notifications anymore, so we will need to target notifications based on other criteria,
//such as name or subject
if( $notification['name'] == 'HTML' ) {
//get upload root for WordPress
$upload = wp_upload_dir();
$upload_path = $upload['basedir'];
//add file, use full path , example -- $attachment = "C:\\xampp\\htdocs\\wpdev\\wp-content\\uploads\\test.txt"
$attachment = $upload_path . $filename;
GFCommon::log_debug( __METHOD__ . '(): file to be attached: ' . $attachment );
if ( file_exists( $attachment ) ) {
$notification['attachments'] = rgar( $notification, 'attachments', array() );
$notification['attachments'][] = $attachment;
GFCommon::log_debug( __METHOD__ . '(): file added to attachments list: ' . print_r( $notification['attachments'], 1 ) );
} else {
GFCommon::log_debug( __METHOD__ . '(): not attaching; file does not exist.' );
}
}
//return altered notification object
return $notification;
}
I'm brand new to coding so please bear with me. My biggest issue is generating the new HTML file. I think I can figure out how to attach it to the notification email (function add_attachment_html) once I get that part done.

Passing a folder name through a url as a parameter to invoke a php function

I am trying to list the contents of a directory by passing a folder name as a URL parameter to invoke a php function. I followed some other examples provided on stackoverflow and have been able to get the php function invoked and am certain I am reaching the code inside because of the echo statements that are output.
The url seems to be encoded correctly because when I display the path info inside the php function all the paths check out.
www.mysite.com/php/genListing.php?function=genListing&folder=wp-content/uploads/myfiles
The check on is_readable() appears to be failing. I have checked the file permissions for that directory and all users have read access. Anybody have any idea of what the problem might be?
genListing.php
if ( ! empty( $_GET['function'] ) && function_exists( $_GET['function'] ) ) {
if ( $_GET['function'] == 'genListing')
{
$atts = $_POST;
genListing( $atts );
}
}
function genListing( $atts ) {
$folder = $_GET[ 'folder' ];
if ( ! empty( $_GET['title'] ) ) {
$title = $_GET['title'];
}
else
{
$title = 'Directory Listing';
}
echo "<p>Made it inside genListing(): " . $folder . "</p>";
$fullFolderPath = trailingslashit( WP_INSTANCE_HOME ) . $folder;
echo "<p> Trying: " . $fullFolderPath . "</p>";
// bad folder check
if ( empty( $folder ) || ! is_readable( $fullFolderPath ) ) {
echo "<p>The folder selected was not valid.</p>";
return 'The folder selected was not valid.';
}

Backup Entire Website Using PHP

Using PHP, I am developing a CMS. This needs to support website backups.
Musts:
Compressed ZIP Folders
Must work on at least Linux and Windows
Must work on PHP 5.0, PHP 4 would be nice
I just need a function/class, don't link me open-source software as I need to do this my self
CMS does not need MySQL backups as it is XML powered
I've already checked into ZipArchive in PHP. Here is all I got so far. However when I try to go to the ZIP file on the server that it says it created, I get a 404? It isn't working and I don't know why.
<?php
$filename = CONTENT_DIR . 'backups/' . date( 'm-d-Y_H-i-s' ) . '.zip';
if ( $handle = opendir( ABS_PATH ) ) {
$zip = new ZipArchive();
if ( $zip->open( $filename, ZIPARCHIVE::CREATE ) !== true ) {
exit( "cannot open <$filename>\n" );
}
$string = '';
while ( ( $file = readdir( $handle ) ) !== false ) {
$zip->addFile( $file );
$string .= "$file\n<br>";
}
closedir( $handle );
$string .= "Status of the Zip Archive: " . $zip->status;
$string .= "<br>System status of the Zip Archive: " . $zip->statusSys;
$string .= "<br>Number of files in archive: " . $zip->numFiles;
$string .= "<br>File name in the file system: " . $zip->filename;
$string .= "<br>Comment for the archive: " . $zip->comment;
$zip->close();
echo $string;
}
?>

Categories