aboutsummaryrefslogtreecommitdiff
path: root/app/class/Model.php
blob: e26a00621d118ef437538f3d92a92c8e5f9a347f (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php

namespace Wcms;

abstract class Model
{

	const CONFIG_FILE = 'config.json';
	const MAN_FILE = 'MANUAL.md';
	const CSS_DIR = 'assets' . DIRECTORY_SEPARATOR .'css' . DIRECTORY_SEPARATOR;
	const JS_DIR = 'assets' . DIRECTORY_SEPARATOR .'js' . DIRECTORY_SEPARATOR;
	const ICONS_DIR = 'assets' . DIRECTORY_SEPARATOR .'icons' . DIRECTORY_SEPARATOR;
	const FONT_DIR = 'fonts' . DIRECTORY_SEPARATOR;
	const MEDIA_DIR = 'media' . DIRECTORY_SEPARATOR;
	const FAVICON_DIR = self::MEDIA_DIR . 'favicon' . DIRECTORY_SEPARATOR;
	const THUMBNAIL_DIR = self::MEDIA_DIR . 'thumbnail' . DIRECTORY_SEPARATOR;
	const TEMPLATES_DIR = '.'. DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
	const RENDER_DIR = 'assets'. DIRECTORY_SEPARATOR . 'render' . DIRECTORY_SEPARATOR;
	const HTML_RENDER_DIR = 'render' . DIRECTORY_SEPARATOR;
	const GLOBAL_DIR = 'assets'. DIRECTORY_SEPARATOR . 'global' . DIRECTORY_SEPARATOR;
	const DATABASE_DIR = '.' . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR;
	const PAGES_DIR = self::DATABASE_DIR . 'pages' . DIRECTORY_SEPARATOR;

	const MEDIA_SORTBY = [
		'id' => 'id',
		'size' => 'size',
		'type' => 'type',
		'date' => 'date',
		'extension' => 'extension'
	];

	const MAP_LAYOUTS = [
        'cose' => 'cose',
		'cose-bilkent' => 'cose-bilkent',
        'circle' => 'circle',
		'breadthfirst' => 'breadthfirst',
		'concentric' => 'concentric',
		'grid' => 'grid',
		'random' => 'random',
    ];
 	
	
	const MEDIA_EXT = [
		'jpg' => 'image',
		'jpeg' => 'image',
		'png' => 'image',
		'gif' => 'image',
		'ico' => 'image',
		'tiff' => 'image',
		'bmp' => 'image',
		'mp3' => 'sound',
		'opus' => 'sound',
		'wav' => 'sound',
		'ogg' => 'sound',
		'flac' => 'sound',
		'aiff' => 'sound',
		'm4a' => 'sound',
		'mp4' => 'video',
		'mkv' => 'video',
		'avi' => 'video',
		'mov' => 'video',
		'wmv' => 'video',
		'm4v' => 'video',
		'zip' => 'archive',
		'7zip' => 'archive',
		'pdf' => 'document',
		'odt' => 'document',
		'doc' => 'document',
		'docx' => 'document',
		'woff' => 'font',
		'woff2' => 'font',
		'otf' => 'font',
		'ttf' => 'font',
		'js' => 'code',
		'html' => 'code',
		'css' => 'code',
		'php' => 'code',
		'' => 'other'
	];

	const LIST_STYLES = [
		'list' => 'list',
		'card' => 'card'
	];

	const FLASH_MESSAGE_TYPES = [
		'info' =>'info',
		'warning' => 'warning',
		'success' => 'success',
		'error' => 'error'
	];

	const COLUMNS = ['id', 'favicon', 'title', 'description', 'tag', 'date', 'datemodif', 'datecreation', 'secure', 'authors', 'linkto', 'visitcount', 'affcount', 'editcount'];

	const TEXT_ELEMENTS = ['header', 'nav', 'main', 'aside', 'footer'];

	const MAX_ID_LENGTH = 64;
	const PASSWORD_MIN_LENGTH = 4;
	const PASSWORD_MAX_LENGTH = 32;

	/** RENDER OPTIONS	 */
	// add class in html element indicating from witch page the content come.
	const RENDER_CLASS_ORIGIN = false;
	// render empty CONTENT element as empty html element, if set to false, render html comment
	const RENDER_EMPTY_ELEMENT = false; 


	/** CONFIG OPTIONS */
	const HOMEPAGE = ['default', 'search', 'redirect'];

	public static function dirtopath($dir)
	{
		$basepath = '';
		if(!empty(Config::basepath())) {
			$basepath = Config::basepath() . '/'  ;
		}
		$dir = str_replace('\\', '/', $dir);
		return '/' . $basepath . $dir;
	}

	public static function renderpath()
	{
		return self::dirtopath(Model::RENDER_DIR);
	}

	public static function globalpath()
	{
		return self::dirtopath(Model::GLOBAL_DIR);
	}

	public static function csspath() 
	{
		return self::dirtopath(Model::CSS_DIR);
	}

	public static function jspath() 
	{
		return self::dirtopath(Model::JS_DIR);
	}

	public static function mediapath()
	{
		return self::dirtopath(Model::MEDIA_DIR);
	}

	public static function faviconpath()
	{
		return self::dirtopath(Model::FAVICON_DIR);
	}

	public static function thumbnailpath()
	{
		return self::dirtopath(Model::THUMBNAIL_DIR);
	}

	public static function fontpath()
	{
		return self::dirtopath(Model::FONT_DIR);
	}

	public static function iconpath()
	{
		return self::dirtopath(Model::ICONS_DIR);
	}

	/**
	 * Check if dir exist. If not, create it
	 * 
	 * @param string $dir Directory to check
	 * 
	 * @return bool return true if the dir already exist or was created succesfullt. Otherwise return false
	 */
	public static function dircheck(string $dir) : bool
	{
		if (!is_dir($dir)) {
			return mkdir($dir);
		} else {
			return true;
		}
	}

	/**
	 * 
	 */
	public static function mediatypes()
	{
		return array_unique(array_values(self::MEDIA_EXT));
	}

	public static function getflashmessages()
	{
		if (!empty($_SESSION['user' . Config::basepath()]['flashmessages'])) {
			$flashmessage = $_SESSION['user' . Config::basepath()]['flashmessages'];
			$_SESSION['user' . Config::basepath()]['flashmessages'] = [];
			if (is_array($flashmessage)) {
				return $flashmessage;
			} else {
				return [];
			}
			return $flashmessage;
		}
	}

	/**
	 * Add a message to flash message list
	 * 
	 * @param string $content The message content
	 * @param string $type Message Type, can be `info|warning|success`
	 */
	public static function sendflashmessage(string $content, string $type = 'info')
	{
		if (!key_exists($type, self::FLASH_MESSAGE_TYPES)) {
			$type = 'info';
		}
		$_SESSION['user' . Config::basepath()]['flashmessages'][] = ['content' => $content, 'type' => $type];
	}
}