-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path_app.php
200 lines (164 loc) · 4.97 KB
/
_app.php
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
<?php
/* Web App Main Class */
// Singleton
class App
{
// Database connection
private static $db = null;
// Current page
private static $page = 'dashboard';
// Page Titles <title></title>
private static $pageTitles = [
'theme' => 'Choose campaign theme',
'builder' => 'Create new campaign',
'login' => 'Authorization'
];
/**
* @var Singleton The reference to *Singleton* instance of this class
*/
private static $instance;
/**
* Returns the *Singleton* instance of this class.
*
* @return Singleton The *Singleton* instance.
*/
public static function getInstance()
{
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
protected function __construct()
{
}
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone()
{
}
/**
* Private unserialize method to prevent unserializing of the *Singleton*
* instance.
*
* @return void
*/
private function __wakeup()
{
}
/** VIEW FUNCTIONS **/
// Set current page
public static function setPage( $page ){
self::$page = $page;
}
// Get current page
public static function getPage(){
return self::$page;
}
// Display page title according to current page
public static function displayPageTitle(){
echo isset( self::$pageTitles[self::$page] ) ? self::$pageTitles[self::$page] : self::$pageTitles['builder'];
}
/** USER FUNCTIONS **/
// Check if the user is logged in
public static function isUserLoggedIn(){
if ( isset( $_SESSION['user_id'] ) ) {
return true;
}
return false;
}
// Get current user information
// if $attr is passed - return specific attribute
// otherwise - return whole array
public static function getUserInfo($attr = null){
if ( !self::isUserLoggedIn() ) {
return false;
}
$result = self::DBQuery('SELECT * FROM `login` where `id`= "' . $_SESSION['user_id'] . '" LIMIT 1')->fetch();
if(is_array($result)){
if(!$attr) {
return $result;
}
if(array_key_exists($attr, $result)){
return $result[$attr];
}
}
return false;
}
// Get user ID
public static function getUserID(){
return self::getUserInfo('id');
}
// Get user App ID
public static function getUserApp(){
return self::getUserInfo('app');
}
// Get user name
public static function getUserName(){
return self::getUserInfo('name');
}
// Get user company
public static function getUserCompany(){
return self::getUserInfo('company');
}
// Get user email
public static function getUserEmail(){
return self::getUserInfo('username');
}
// Do user logout
public static function doUserLogout(){
$_SESSION['user_id'] = '';
unset( $_SESSION['user_id'] );
}
/** THEME/TEMPLATE FUNCTIONS **/
// Get list of available email templates
// return array with template slug and name
public static function getTemplates(){
$result = array();
$templates = array_filter(glob('templates/*'), 'is_dir');
foreach ($templates as $template){
$template_slug = basename($template);
$template_name = str_replace('_', ' ', $template_slug);
$template_name = str_replace('-', ' ', $template_name);
$template_name = ucwords($template_name);
$tmp = array(
'slug'=> $template_slug,
'name'=> $template_name
);
array_push($result, $tmp);
}
return $result;
}
/** DATABASE FUNCTIONS **/
// Connect to database
public static function DBConnect($db_name, $db_user, $db_password, $db_host = '127.0.0.1'){
$db_host = $db_host ? $db_host : '127.0.0.1';
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=' . $db_name . ';host=' . $db_host . ';charset=utf8';
try {
self::$db = new PDO($dsn, $db_user, $db_password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
}
// SQL Query
public static function DBQuery( $q ){
$q = self::$db->query($q );
return $q;
}
// Insert Records, Inserted ID will be returned
public static function DBExecute( $q ){
self::$db->exec($q );
$inserted_id = self::$db->lastInsertId();
return $inserted_id;
}
}