Passed
Branch master (a5e4e1)
by Michael
12:01
created

class/Cloner.php (1 issue)

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Publisher;
4
5
/**
6
 * Class Cloner
7
 */
8
class Cloner
9
{
10
    // recursive cloning script
11
    /**
12
     * @param $path
13
     */
14
    public static function cloneFileFolder($path): void
15
    {
16
        global $patKeys;
17
        global $patValues;
18
19
        $newPath = \str_replace($patKeys[0], $patValues[0], $path);
20
21
        if (\is_dir($path)) {
22
            // create new dir
23
            if (!\mkdir($newPath) && !\is_dir($newPath)) {
24
                throw new \RuntimeException(\sprintf('Directory "%s" was not created', $newPath));
25
            }
26
27
            // check all files in dir, and process it
28
            $handle = \opendir($path);
29
            if ($handle) {
0 ignored issues
show
$handle is of type resource, thus it always evaluated to false.
Loading history...
30
                while (false !== ($file = \readdir($handle))) {
31
                    if (0 !== \mb_strpos($file, '.')) {
32
                        self::cloneFileFolder("{$path}/{$file}");
33
                    }
34
                }
35
                \closedir($handle);
36
            }
37
        } else {
38
            $noChangeExtensions = ['jpeg', 'jpg', 'gif', 'png', 'zip', 'ttf'];
39
            if (\in_array(\mb_strtolower(\pathinfo($path, \PATHINFO_EXTENSION)), $noChangeExtensions, true)) {
40
                // image
41
                \copy($path, $newPath);
42
            } else {
43
                // file, read it
44
                $content = file_get_contents($path);
45
                $content = \str_replace($patKeys, $patValues, $content);
46
                file_put_contents($newPath, $content);
47
            }
48
        }
49
    }
50
51
    /**
52
     * @param $dirname
53
     *
54
     * @return bool
55
     */
56
    public static function createLogo($dirname)
57
    {
58
        if (!\extension_loaded('gd')) {
59
            return false;
60
        }
61
        $requiredFunctions = [
62
            'imagecreatefrompng',
63
            'imagecolorallocate',
64
            'imagefilledrectangle',
65
            'imagepng',
66
            'imagedestroy',
67
            'imagefttext',
68
            'imagealphablending',
69
            'imagesavealpha',
70
        ];
71
        foreach ($requiredFunctions as $func) {
72
            if (!\function_exists($func)) {
73
                return false;
74
            }
75
        }
76
        //            unset($func);
77
78
        if (!\file_exists($imageBase = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'))
79
            || !\file_exists($font = $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/VeraBd.ttf'))) {
80
            return false;
81
        }
82
83
        $imageModule = \imagecreatefrompng($imageBase);
84
        // save existing alpha channel
85
        imagealphablending($imageModule, false);
86
        imagesavealpha($imageModule, true);
87
88
        //Erase old text
89
        $greyColor = \imagecolorallocate($imageModule, 237, 237, 237);
90
        \imagefilledrectangle($imageModule, 5, 35, 85, 46, $greyColor);
91
92
        // Write text
93
        $textColor     = \imagecolorallocate($imageModule, 0, 0, 0);
94
        $spaceToBorder = (int)((80 - \mb_strlen($dirname) * 6.5) / 2);
95
        \imagefttext($imageModule, 8.5, 0, $spaceToBorder, 45, $textColor, $font, \ucfirst($dirname), []);
96
97
        // Set transparency color
98
        //$white = imagecolorallocatealpha($imageModule, 255, 255, 255, 127);
99
        //imagefill($imageModule, 0, 0, $white);
100
        //imagecolortransparent($imageModule, $white);
101
102
        \imagepng($imageModule, $GLOBALS['xoops']->path('modules/' . $dirname . '/assets/images/logoModule.png'));
103
        \imagedestroy($imageModule);
104
105
        return true;
106
    }
107
}
108