Passed
Push — development ( 49962e...4b352c )
by Spuds
01:07 queued 23s
created

RemoveTempAttachments::removeTempFiles()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 18
c 0
b 0
f 0
nc 15
nop 3
dl 0
loc 40
ccs 0
cts 0
cp 0
crap 56
rs 8.8333
1
<?php
2
3
/**
4
 * Check for un-posted attachments is something we can do once in a while :P
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 Beta 1
14
 *
15
 */
16
17
namespace ElkArte\ScheduledTasks\Tasks;
18
19
use ElkArte\Attachments\AttachmentsDirectory;
20
use ElkArte\Errors\Errors;
0 ignored issues
show
Bug introduced by
The type ElkArte\Errors\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use ElkArte\Helper\FileFunctions;
22
use ElkArte\Languages\Txt;
23
24
/**
25
 * Class Remove_Temp_Attachments - Check for un-posted attachments is something we can
26
 * do once in a while :P
27
 *
28
 * - This function uses \FilesystemIterator cycling through all the attachments
29
 *
30
 * @package ScheduledTasks
31
 */
32
class RemoveTempAttachments implements ScheduledTaskInterface
33
{
34
	/**
35
	 * Clean up the file system by removing up-posted or failed attachments
36
	 *
37
	 * @return bool
38
	 * @return bool
39
	 */
40
	public function run()
41
	{
42
		global $modSettings;
43
44
		$success = true;
45
46
		// We need to know where this thing is going.
47
		$attachmentsDir = new AttachmentsDirectory($modSettings, database());
48
		$attach_dirs = $attachmentsDir->getPaths();
49
50
		foreach ($attach_dirs as $attach_dir)
51
		{
52
			if (!$this->removeTempFiles($attach_dir, 'post_tmp_'))
53
			{
54
				$success = false;
55
			}
56
		}
57
58
		// Any avatar temp files?
59
		if (!empty($modSettings['custom_avatar_dir']) && is_dir($modSettings['custom_avatar_dir']))
60
		{
61
			if (!$this->removeTempFiles($modSettings['custom_avatar_dir'], 'avatar_tmp_', true))
62
			{
63
				$success = false;
64
			}
65
		}
66
67
		return $success;
68
	}
69
70
	/**
71
	 * Removes temporary files from a directory.
72
	 *
73
	 * @param string $dir The directory to clean up.
74
	 * @param string $pattern The pattern to match.
75
	 * @param bool $noExtension If true, only files without extensions will be removed.
76
	 *
77
	 * @return bool
78
	 */
79
	private function removeTempFiles($dir, $pattern, $noExtension = false)
80
	{
81
		global $context, $txt;
82
83
		try
84
		{
85
			$fileFunc = FileFunctions::instance();
86
			$files = new \FilesystemIterator($dir, \FilesystemIterator::SKIP_DOTS);
87
			foreach ($files as $file)
88
			{
89
				if (!str_contains($file->getFilename(), $pattern))
90
				{
91
					continue;
92
				}
93
94
				if ($noExtension && !empty($file->getExtension()))
95
				{
96
					continue;
97
				}
98
99
				// Temp file is more than 5 hours old!
100
				if ($file->getMTime() >= time() - 18000)
101
				{
102
					continue;
103
				}
104
105
				$fileFunc->delete($file->getPathname());
106
			}
107
		}
108
		catch (\UnexpectedValueException $e)
109
		{
110
			Txt::load('Post');
111
112
			$context['scheduled_errors']['remove_temp_attachments'][] = $txt['cant_access_upload_path'] . ' (' . $dir . ')';
113
			Errors::instance()->log_error($txt['cant_access_upload_path'] . ' (' . $e->getMessage() . ')', 'critical');
114
115
			return false;
116
		}
117
118
		return true;
119
	}
120
}
121