Support

UNiTE, Remote CLI, eXtract Wizard

#25408 UNiTE 3.0 - Is UNiTE suppose to delete ALL Files and Directories First?

Posted in ‘UNiTE and Remote CLI’
This is a public ticket

Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.

Environment Information

PHP version
n/a
Tool
UNiTE
Tool version
n/a

Latest post by nicholas on Thursday, 23 June 2016 03:08 CDT

markaerrington
 Hi,

Just wanted to know if UNiTE 3.0 is suppose to delete ALL the Files and Directories first?

Thanks, Mark

nicholas
Akeeba Staff
Manager
No. It works like any other restoration: the archive is unpacked on top of the existing site, then the database is restored and the site reconfigured (e.g. the configuration.php file is replaced in Joomla!, wp-config.php and .htaccess in WordPress and so on).

I chose not to offer an option to delete "all files and folders" because that's not a well defined operation which can lead to problems. Moreover, if you think about it, you are trying to duplicate the functionality of the rm command which is pointless.

For example, given a Joomla! installation in the root of the domain and two subdomains stored in subdirectories of the main domain what would "delete all files and folders" do? Delete the subdomain directories or not? If not, how would it know? If your site has directories beyond those packed by default with the CMS / script how would it know? Basically, you'd have to tell it what to delete and what not.

This brings us to a very important realisation. UNiTE is a CLI script. There is a fantastic CLI tool for removing files and directories, in fact an internal command of every single command line interpreter: rm. You can tell it exactly what to remove. So why duplicate its functionality when you can simply call rm before running UNiTE?

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

markaerrington
The main reason why I asked is because hackers are able to upload hacking scripts through some 3rd party scripts of Joomla and WordPress. This happened to me last week with Joomla, two times. So I wouldn't know which 3rd party scripts was used until my site was hacked into. If hacking scripts was uploaded to the site, they would be left on the server after Unite restored the site. Right?

Thanks, Mark

nicholas
Akeeba Staff
Manager
Yes, they would. However, if this is the reason you're asking it makes a bit more sense figuring out how your sites get hacked and fix the root cause of the issue instead of restoring the same, vulnerable, site on the server so the hackers can hack all over again.

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

markaerrington
It worked! :-)

This works for Joomla. You will need to do some testing first before putting this on a productive site.

I put together a small script to delete all the files and folders but it will not delete the "tmp" or the "unite" folder. If there are any ".htaccess" files in any folders, it will not delete them. It also, makes an "index.php" file so nobody can see what is going on until Unite is done restoring the site.

Now, I have to see how I can make this work each time Unite is runned.
I have to run my "delete file" manually. Then start Unite by using cron. I am just in the testing stage but I hope I can put this inside Unite. Maybe Nicholas can give me some advice? If not, that's okay.

I tried to post the code but this forum would not let me. I tried to upload the file but this forum would not let me.

Thanks, Mark

markaerrington
I'm trying to make a jobsteps called "deletefiles".

I added a file in "unite/engine/step" and called it deletefiles.php

I copied some code from one of the other files in the same directory and added my own.

I ran Unite and got this error. How can I fix this? I have no idea what to look for.

Fatal error: Class 'UStepDeletefiles' not found in /home/freelocksmith/public_html/unite/engine/factory.php on line 56

Thanks, Mark

nicholas
Akeeba Staff
Manager
Regarding this:
Fatal error: Class 'UStepDeletefiles' not found in /home/freelocksmith/public_html/unite/engine/factory.php on line 56

I believe you have tried to just put some code into a straight .php file or you forgot to change the class name. This will not work. You need to create a class file like this:
<?php
// Protection against direct access
defined('UNITE') or die('Restricted access');

class UStepDeletefiles extends UAbstractPart
{
	protected function _prepare()
	{
        // This is mandatory. Do not remove.
		$this->setState('prepared');
	}

	protected function _run()
	{
        // PLACE YOUR CODE HERE -- BEGIN

        // PLACE YOUR CODE HERE -- END
        
        // After your code put this line. It's mandatory.
        $this->setState('postrun');
	}

	protected function _finalize()
	{
        // This is mandatory. Do not remove.
		$this->setState('finished');
	}
}


Put your code between the two "PLACE YOUR CODE HERE" lines and leave the rest of the file as is.

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

markaerrington
Thanks! I'm almost there! I thought I had it but I'm getting this error below. This script works great by itself in the Unite directory but not in the unite/engine/step directory.

I ran it and got this error:
PHP Fatal error: Call to undefined function RemoveDirectory() in /home/username/public_html/unite/engine/step/deletefiles.php on line 17

In the code is:
chdir('../');
RemoveDirectory(getcwd());
MakeFile(getcwd());
chdir('unite');

function RemoveDirectory($directory){

Thanks, Mark

nicholas
Akeeba Staff
Manager
Since this is a class now your functions have now become class methods. This means that instead of
RemoveDirectory(getcwd());

you should do
$this->RemoveDirectory(getcwd());


Further reference:
Object-Oriented PHP for beginners from tuts+
PHP: Classes and Objects from the PHP manual

Happy coding :)

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

markaerrington
Hi and thanks again for your help Nicholas! I really don't know what I am doing. One big problem with me is that I have a short term memory problem on stuff like this and sometimes I don't remember what I just did. I have all these notes I save so I can go back and see what I did. I spend lots of time looking on the web for examples, sometimes days. Of course, not 8 hrs a day...just about 3 to 6 hrs.

So now I'll have to see why I got this error below. One site you showed me might help me. I'll check it out. Thanks!

Fatal error: Call to undefined method UStepDeletefiles::RemoveDirectory() in /home/username/public_html/unite/engine/step/deletefiles.php on line 18

===============================

LINE 18: $this->RemoveDirectory(getcwd());
function RemoveDirectory($directory){

    foreach (glob("{$directory}/*") as $file)
    {
		if ($file!=$directory.'/unite' && $file!=$directory.'/tmp' ){
			if(is_dir($file)) { 
				RemoveDirectory($file);
			} else {
				unlink($file);
			}
		}
    }
             if($directory!=getcwd()){
             rmdir($directory);
	}
} 

Thanks, Mark

nicholas
Akeeba Staff
Manager
This applies to all calls to methods. See your code, you are calling RemoveDirectory. Replace it with $this->RemoveDirectory as I told you above.

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

markaerrington
I'm sure you can see the mistakes but for me, and looking on the web, I can't see what's wrong. Same Error.

echo $_SERVER['DOCUMENT_ROOT'];
$this->RemoveDirectory(getcwd());
$this->MakeFile(getcwd());
$this->chdir('unite');

 function RemoveDirectory($directory){

    foreach (glob("{$directory}/*") as $file)
    {
		if ($file!=$directory.'/unite' && $file!=$directory.'/tmp' ){
			if(is_dir($file)) { 
				$this->RemoveDirectory($file);
			} else {
				$this->unlink($file);
			}
		}
    }
             if($directory!=getcwd()){
             $this->rmdir($directory);
	}
} 

function MakeFile($fp){
$content = "<!DOCTYPE html><title></title></html>";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/index.php","wb");
fwrite($fp,$content);
fclose($fp);
} 

Thanks, Mark

nicholas
Akeeba Staff
Manager
I believe you need something like the attached.

Remember that you basically must prepend $this-> ONLY to user-defined functions. chdir and unlink are part of PHP itself. The are not defined in your class, therefore you don't use $this-> in front of them. Also, function definitions go outside the _run method itself, they have to be part of the class.

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

nicholas
Akeeba Staff
Manager
The attachment didn't make it. Use this direct Dropbox link instead: https://dl.dropboxusercontent.com/u/5168399/deletefiles.zip

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

markaerrington
YES! This works GREAT!

Now that I saw how this is set up. I know what I did wrong and would have NEVER figured it out if you didn't help me! Thank you very much, Nicholas! (Muchas Gracias , Nicholas!)

In my code, I just had to change this line to what I had it before:
$this->rmdir($directory);
to
rmdir($directory);

Fill free to add, modify, this to Unite if you want. I got the code from many examples on the web. As you can see I am not a programmer, but a programmer wanna be. :-)

When using my deletefile code, I always use .jpa Akeeba Backup package because Unite will restore from this package with no problems at all!

I am so happy to get this done and working just the way I wanted it too.

Now, I can move on to my other websites and add this to Unite.

Thanks, Mark

nicholas
Akeeba Staff
Manager
I'm glad we could help :)

Nicholas K. Dionysopoulos

Lead Developer and Director

πŸ‡¬πŸ‡·Greek: native πŸ‡¬πŸ‡§English: excellent πŸ‡«πŸ‡·French: basic β€’ πŸ• My time zone is Europe / Athens
Please keep in mind my timezone and cultural differences when reading my replies. Thank you!

Support Information

Working hours: We are open Monday to Friday, 9am to 7pm Cyprus timezone (EET / EEST). Support is provided by the same developers writing the software, all of which live in Europe. You can still file tickets outside of our working hours, but we cannot respond to them until we're back at the office.

Support policy: We would like to kindly inform you that when using our support you have already agreed to the Support Policy which is part of our Terms of Service. Thank you for your understanding and for helping us help you!