ThreemaGateway_Option_DownloadPath::verifyOption()   B
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 10
nop 3
1
<?php
2
/**
3
 * Download path option.
4
 *
5
 * @package ThreemaGateway
6
 * @author rugk
7
 * @copyright Copyright (c) 2016 rugk
8
 * @license MIT
9
 */
10
11
class ThreemaGateway_Option_DownloadPath
12
{
13
    /**
14
     * Verifies whether the dir is valid (can be created) and is writable.
15
     *
16
     * @param string             $dirpath    Input
17
     * @param XenForo_DataWriter $dataWriter
18
     * @param string             $fieldName  Name of field/option
19
     *
20
     * @return bool
21
     */
22
    public static function verifyOption(&$dirpath, XenForo_DataWriter $dataWriter, $fieldName)
23
    {
24
        // correct path
25
        if (substr($dirpath, 0, 1) == '/') {
26
            $dirpath = substr($dirpath, 1);
27
        }
28
29
        // if disabled (path = empty), we accept this
30
        if ($dirpath == '') {
31
            return true;
32
        }
33
34
        $absoluteDir = XenForo_Application::getInstance()->getRootDir() . '/' . $dirpath;
35
36
        // try to move the old dir to the new place
37
        try {
38
            self::moveDir($absoluteDir);
39
        } catch (Exception $e) {
40
            // ignore me, moving the dir is not critical
41
        }
42
43
        // check path
44
        if (!ThreemaGateway_Handler_Validation::checkDir($absoluteDir)) {
45
            $dataWriter->error(new XenForo_Phrase('threemagw_invalid_downloadpath'), $fieldName);
46
            return false;
47
        }
48
49
        return true;
50
    }
51
52
53
    /**
54
     * Moves old dir to new path if useful/possible.
55
     *
56
     * When the dir was not moved for some reason, it returns false. If a move
57
     * was done, it returns true. This means it silently fails, if the dir could
58
     * not be moved.
59
     *
60
     * @param  string    $newDir Please pass the absolute path here!
61
     * @throws Exception (from rename())
62
     * @return bool
63
     */
64
    protected static function moveDir($newDir)
65
    {
66
        /** @var XenForo_Options $xenOptions */
67
        $xenOptions = XenForo_Application::getOptions();
68
        /** @var string $orgDir original old dir */
69
        $orgDir = XenForo_Application::getInstance()->getRootDir() . '/' . $xenOptions->threema_gateway_downloadpath;
70
71
        // if no option set or previous option does not apply anymore
72
        // there is nothing to do
73
        if (empty($xenOptions->threema_gateway_downloadpath) ||
74
            !file_exists($orgDir)) {
75
            return false;
76
        }
77
78
        // if old dir check fails, do not go on
79
        if (!ThreemaGateway_Handler_Validation::checkDir($orgDir)) {
80
            return false;
81
        }
82
83
        return rename($orgDir, $newDir);
84
    }
85
}
86