Completed
Pull Request — master (#126)
by Gino
03:40
created

TDMCreateBuilding::copyDir()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 10
nc 4
nop 2
dl 0
loc 17
rs 8.8571
c 2
b 1
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * TDMCreateBuilding class.
15
 *
16
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
17
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
18
 *
19
 * @since           2.5.x
20
 *
21
 * @author          TDM TEAM DEV MODULE
22
 *
23
 * @version         $Id: building.php 12425 2014-02-23 22:40:09Z timgno $
24
 */
25
26
/**
27
 * Class TDMCreateBuilding.
28
 */
29
class TDMCreateBuilding
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
30
{
31
    /**
32
     *  @static function getInstance
33
     *
34
     *  @param null
35
     *
36
     * @return TDMCreateBuilding
37
     */
38
    public static function getInstance()
39
    {
40
        static $instance = false;
41
        if (!$instance) {
42
            $instance = new self();
43
        }
44
45
        return $instance;
46
    }
47
48
    /**
49
     * @param bool $action
50
     *
51
     * @return XoopsThemeForm
52
     */
53
    public function getForm($action = false)
54
    {
55
        $tc = TDMCreateHelper::getInstance();
56
        if ($action === false) {
57
            $action = $_SERVER['REQUEST_URI'];
58
        }
59
        xoops_load('XoopsFormLoader');
60
        $form = new XoopsThemeForm(_AM_TDMCREATE_ADMIN_CONST, 'buildform', $action, 'post', true);
61
        $form->setExtra('enctype="multipart/form-data"');
62
        $moduleObj = $tc->getHandler('modules')->getObjects(null);
0 ignored issues
show
Bug introduced by
The method getObjects cannot be called on $tc->getHandler('modules') (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
63
        $mod_select = new XoopsFormSelect(_AM_TDMCREATE_CONST_MODULES, 'mod_id', 'mod_id');
64
        $mod_select->addOption('', _AM_TDMCREATE_BUILD_MODSELOPT);
65
        foreach ($moduleObj as $mod) {
66
            $mod_select->addOption($mod->getVar('mod_id'), $mod->getVar('mod_name'));
67
        }
68
        $form->addElement($mod_select, true);
69
70
        $form->addElement(new XoopsFormHidden('op', 'build'));
71
        $form->addElement(new XoopsFormButton(_REQUIRED.' <sup class="red bold">*</sup>', 'submit', _SUBMIT, 'submit'));
72
73
        return $form;
74
    }
75
76
    /**
77
     * @param string $dir
78
     * @param string $pattern
79
     *
80
     * @return clearDir
81
     */
82
    public function clearDir($dir, $pattern = '*')
83
    {
84
        // Find all files and folders matching pattern
85
        $files = glob($dir."/$pattern");
86
        // Interate thorugh the files and folders
87
        foreach ($files as $file) {
88
            // if it's a directory then re-call clearDir function to delete files inside this directory     
89
            if (is_dir($file) && !in_array($file, array('..', '.'))) {
90
                // Remove the directory itself
91
                self::clearDir($file, $pattern);
92
            } elseif (is_file($file) && ($file != __FILE__)) {
93
                // Make sure you don't delete the current script
94
                unlink($file);
95
            }
96
        }
97
        rmdir($dir);
98
    }
99
100
    /**
101
     * @param string $src
102
     * @param string $dst
103
     *
104
     * @return copyDir
105
     */
106
    public function copyDir($src, $dst)
107
    {
108
        $dir = opendir($src);
109
        @mkdir($dst);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
110
        while (false !== ($file = readdir($dir))) {
111
            if (($file != '.') && ($file != '..')) {
112
                if (is_dir($src.'/'.$file)) {
113
                    // Copy the directory itself
114
                    self::copyDir($src.'/'.$file, $dst.'/'.$file);
115
                } else {
116
                    // Make sure you copy the current script
117
                    copy($src.'/'.$file, $dst.'/'.$file);
118
                }
119
            }
120
        }
121
        closedir($dir);
122
    }
123
}
124