New Page in Safe Mode=ON Permissions Problem (FTP UPLOAD)

saeidcdc

I had some problems with adding pages in safe_mode=enable server (plesk control panel)
so new pages had apache owner and not available!
I have changed this functions.php for work with folders & permissions, i know that is very ugly! but it works
in main pages and 2nd level pages and often in 3th level!

CHANGES: in framework/functions.php file:
take following function:
// Function to create directories
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) {
   if(!file_exists($dir_name)) {
       $umask = umask(0);
       mkdir($dir_name, $dir_mode);
       umask($umask);
       return true;
   } else {
       return false;    
   }
}

and replace with this:
// Function to create directories
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) {
   if(!file_exists($dir_name)) {
       $parent_folders = str_replace(WB_PATH.PAGES_DIRECTORY.'/', '', $dir_name);
       $ftp_server = "xxxxxxxx";
       $ftp_user = "xxxxxxxx";
       $ftp_pass = "xxxxxxxx";
       // set up a connection or die
       $conn = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server <br/>");
       // try to login
       if (@ftp_login($conn, $ftp_user, $ftp_pass)) {
//            echo "Connected as $ftp_user@$ftp_server\n<br/>";
       } else {
//            echo "Couldn't connect as $ftp_user\n<br/>";
       }

// YOU MAY NEED TO CHANGE THIS LINE
       @ftp_chdir($conn, "httpdocs/fa/pages");

       if (@ftp_mkdir($conn, $parent_folders)) {
//         echo "successfully created $parent_folders\n<br/>";
       } else {
//         echo "There was a problem while creating $parent_folders\n<br/>";
       }
       if (@ftp_chmod($conn, 0777, $parent_folders) !== false) {
//         echo "$parent_folders chmoded successfully to 0777\n<br/>";
       } else {
//         echo " could not chmod $parent_folders\n<br/>";
       }

       @ftp_close($conn);

//        umask($umask);
       return true;
   } else {
       return false;    
   }
}


and take following part:
       $handle = fopen($filename, 'w');
       fwrite($handle, $content);
       fclose($handle);
       // Chmod the file
       change_mode($filename);


and replace with this:

       $ftp_server = "xxxxxxxxx";
       $ftp_user = "xxxxxxxxx";
       $ftp_pass = "xxxxxxxxx";
       $file_ext = basename($filename);
       $filehandy = str_replace(WB_PATH.PAGES_DIRECTORY.'/', '', $filename);
       $destfolder = substr($filehandy , 0, strlen($filehandy)-strlen($file_ext)) ;
       // set up a connection or die
       $conn1 = @ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
       // try to login
       if (@ftp_login($conn1, $ftp_user, $ftp_pass)) {
//            echo "Connected as $ftp_user@$ftp_server\n";
       } else {
//            echo "Couldn't connect as $ftp_user\n";
       }

// YOU MAY NEED TO CHANGE THIS LINE
       @ftp_chdir($conn1, "httpdocs/fa/pages/");
       if (@ftp_chmod($conn1, 0777, substr($destfolder, 0, -1)) !== false) {
//         echo "$destfolder chmoded successfully to 0777\n<br/>";
       } else {
//         echo "could not chmod $destfolder\n<br/>";
       }

       $handle = fopen($filename.'.tmp', 'w');
       fwrite($handle, $content);
       fclose($handle);
//        Chmod the file
//        chmod($filename, 0755);
       change_mode($filename);

       if(@ftp_chdir($conn1, substr($destfolder, 0, -1))) {
//            echo "Current directory is now: " . $destfolder . "\n";
       } else {
//            echo "Couldn't change directory\n";
       };
       @ftp_chdir($conn1, WB_PATH.PAGES_DIRECTORY);
       if (@ftp_put($conn1, $file_ext, $filename.'.tmp', FTP_ASCII)) {
       echo "Successfully Created Page File\n";
       } else {
       echo "There was a problem while creating $filename\n";
       }
       unlink($filename.'.tmp');
   
       @ftp_chdir($conn1, "../");
       if (@ftp_chmod($conn1, 0755, substr($destfolder, 0, -1)) !== false) {
//         echo "$destfolder chmoded successfully to 0755\n<br/>";
       } else {
//         echo "could not chmod $destfolder\n<br/>";
       }
       @ftp_close($conn1);


ITS DONE! :-D