aboutsummaryrefslogtreecommitdiff
path: root/app/class/Modelfont.php
blob: 3f25e21f0a8feb01cc13bf597f833591cd1dab26 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

namespace Wcms;

class Modelfont extends Model
{

    const FONT_TYPES = ['woff2', 'woff', 'otf', 'ttf', 'eot', 'svg'];
    
	public function fontdircheck()
	{
		if(!is_dir(Model::FONT_DIR)) {
			return mkdir(Model::FONT_DIR);
		} else {
			return true;
		}
	}

    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()
    {
        if ($handle = opendir(Model::FONT_DIR)) {
            $list = [];
            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 {' . PHP_EOL . 'font-family: ' . $font['id'] . ';' . PHP_EOL . ' src: url( ' . Model::fontpath() . $font['id'] . '.' . $font['ext'] . ');' . PHP_EOL . '}' . PHP_EOL . PHP_EOL;
        }
        return $fontface;
    }


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

        }
    }

    public function upload(array $file, $maxsize = 2 ** 24, $id = null)
    {
        $message = 'runing';
        if (isset($file) and $file['font']['error'] == 0 and $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;
    }




}




?>