aboutsummaryrefslogtreecommitdiff
path: root/app/class/Modelfont.php
blob: 48885bede6e35bf5f8527e27dbd44568b4d8e0ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Wcms;

class Modelfont extends Model
{

    protected const FONT_TYPES = ['woff2', 'woff', 'otf', 'ttf', 'eot', 'svg'];
    


    public function getfontlist()
    {
        return $this->fontlist($this->list());
    }

    public function getfonttypes()
    {
        $fonttypes = array_map(function ($ext) {
            return '.' . $ext;
        }, $this::FONT_TYPES);
        return implode(', ', $fonttypes);
    }

    public function renderfontface()
    {
        $list = $this->list();
        $fontlist = $this->fontlist($list);
        $fontface = $this->fontface($fontlist);
        $this->write($fontface);
    }


    public function list()
    {
        $list = [];
        if ($handle = opendir(Model::FONT_DIR)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != "..") {
                    $list[] = $entry;
                }
            }
        }

        return $list;
    }

    public function fontlist(array $list)
    {
        $fontlist = [];
        $fonttypes = implode('|', $this::FONT_TYPES);
        $regex = '#(.+)\.(' . $fonttypes . ')#';
        foreach ($list as $font) {
            if (preg_match($regex, $font, $out)) {
                $fontlist[] = ['id' => $out[1], 'ext' => $out[2], 'size' => filesize(Model::FONT_DIR . $font)];
            }
        }
        return $fontlist;
    }

    public function fontface(array $fontlist)
    {
        $fontface = '';
        foreach ($fontlist as $font) {
            $fontface .= '@font-face {\n
                font-family: ' . $font['id'] . ';\n
                src: url( ' . Model::fontpath() . $font['id'] . '.' . $font['ext'] . ');
                }\n\n';
        }
        return $fontface;
    }


    public function write(string $fontface)
    {
        $write = file_put_contents(Model::ASSETS_CSS_DIR . 'fonts.css', $fontface);
        if ($write !== false) {
        }
    }

    public function upload(array $file, $maxsize = 2 ** 24, $id = null)
    {
        $message = 'runing';
        if ($file['font']['error'] == 0 && $file['font']['size'] < $maxsize) {
            $infosfichier = pathinfo($file['font']['name']);
            $extension_upload = $infosfichier['extension'];
            $extensions_autorisees = $this::FONT_TYPES;
            if (in_array($extension_upload, $extensions_autorisees)) {
                if (!empty($id)) {
                    $id = strtolower(strip_tags($id));
                    $id = str_replace(' ', '_', $id);
                } else {
                    $id = $infosfichier['filename'];
                }
                if (!file_exists($this::FONT_DIR . $id . '.' . $extension_upload)) {
                    $extension_upload = strtolower($extension_upload);
                    $uploadok = move_uploaded_file(
                        $file['font']['tmp_name'],
                        $this::FONT_DIR . $id . '.' . $extension_upload
                    );
                    if ($uploadok) {
                        $message = true;
                    } else {
                        $message = 'uploaderror';
                    }
                } else {
                    $message = 'filealreadyexist';
                }
            }
        } else {
            $message = 'filetoobig';
        }
        return $message;
    }
}