This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinit.php
252 lines (216 loc) · 6.65 KB
/
init.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/*
This file is part of yourTinyTodo by the yourTinyTodo community.
Copyrights for portions of this file are retained by their owners.
Based on myTinyTodo by Max Pozdeev
(C) Copyright 2009-2010 Max Pozdeev <maxpozdeev@gmail.com>
Licensed under the GNU GPL v3 license. See file COPYRIGHT for details.
*/
if(!defined('YTTPATH')) define('YTTPATH', dirname(__FILE__) .'/');
if(!defined('YTTCOREPATH')) define('YTTCOREPATH', YTTPATH.'core/');
require_once(YTTPATH. 'common.php');
if(!file_exists(YTTPATH.'db/config.php')) {
die("Not installed. Run <a href=setup.php>setup.php</a> first.");
} else {
require_once(YTTPATH. 'db/config.php');
}
require_once(YTTPATH. 'core/Database.class.php');
require_once(YTTPATH. 'core/Lang.class.php');
require_once(YTTPATH. 'core/Notification.class.php');
require_once(YTTPATH. 'core/TimeTracker.class.php');
define('YTT_VERSION', '1.0a');
if(!isset($config)) global $config;
Config::loadConfig($config);
unset($config);
date_default_timezone_set(Config::get('timezone'));
if(Config::get('debugmode')) {
ini_set('display_errors', 1);
error_reporting(E_ALL);
} else {
ini_set('display_errors', 0);
error_reporting(0);
}
# MySQL Database Connection
if(Config::get('db') == 'mysql')
{
try
{
require_once(YTTCOREPATH . 'db/class.db.mysql.php');
$db = DBConnection::init(new Database_Mysql());
$db->connect(Config::get('mysql.host'), Config::get('mysql.user'), Config::get('mysql.password'), Config::get('mysql.db'));
$db->dq("SET NAMES utf8");
}
catch(Exception $e)
{
die('Database connection error - check config file');
}
}
# PostgreSQL Database Connection
elseif(Config::get('db') == 'postgres')
{
require_once(YTTCOREPATH . 'db/class.db.postgres.php');
$db = DBConnection::init(new Database_Postgres());
$db->connect(Config::get('postgres.host'), Config::get('postgres.user'), Config::get('postgres.password'), Config::get('postgres.db'));
$db->dq("SET NAMES 'utf8'");
}
# SQLite3 (pdo_sqlite)
elseif(Config::get('db') == 'sqlite')
{
try
{
require_once(YTTCOREPATH . 'db/class.db.sqlite3.php');
$db = DBConnection::init(new Database_Sqlite3());
$db->connect(YTTPATH. 'db/todolist.db');
}
catch(Exception $e)
{
die('Database connection error - check config file');
}
}
else {
# It seems not installed
die("Not installed. Run <a href=setup.php>setup.php</a> first.");
}
$db->prefix = Config::get('prefix');
require_once(YTTPATH. 'core/Lang.class.php');
require_once(YTTPATH. 'lang/'.Config::get('lang').'.php');
$_yttinfo = array();
$needAuth = (Config::get('password') != '' || Config::get('multiuser') == 1) ? 1 : 0;
$multiUser = (Config::get('multiuser') == 1) ? 1 : 0;
if($needAuth && !isset($dontStartSession))
{
if(Config::get('session') == 'files')
{
session_save_path(YTTPATH. 'tmp/sessions');
ini_set('session.gc_maxlifetime', '1209600'); # 14 days session file minimum lifetime
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 10);
}
ini_set('session.use_cookies', true);
ini_set('session.use_only_cookies', true);
session_set_cookie_params(1209600, url_dir(Config::get('url')=='' ? $_SERVER['REQUEST_URI'] : Config::get('url'))); # 14 days session cookie lifetime
session_name('ytt-session');
session_start();
}
$notifications_count = (Config::get('multiuser') == 1)?Notification::getUnreadCount():false;
function is_logged()
{
if(isset($_SESSION['logged'])) {
return true;
}
if(Config::get('auth_bypass') != 'none') {
$classname = Config::get('auth_bypass').'_Bypass';
if(file_exists(YTTCOREPATH.'authentication/'.$classname.'.php')) {
require_once(YTTCOREPATH.'authentication/'.$classname.'.php');
$bypass = new $classname;
$bypass->setSession();
}
}
return false;
}
function is_readonly()
{
$needAuth = (Config::get('password') != '' || Config::get('multiuser') == 1) ? 1 : 0;
if(($needAuth && !is_logged()) || (Config::get('multiuser') == 1 && is_logged() && $_SESSION['role'] == 3)) return true;
return false;
}
function is_admin()
{
$needAuth = (Config::get('password') != '' || Config::get('multiuser') == 1) ? 1 : 0;
if(!$needAuth) {
return true;
}
if($needAuth && Config::get('multiuser') != 1 && is_logged()) {
return true;
}
if($needAuth && Config::get('multiuser') == 1 && isset($_SESSION['role']) && $_SESSION['role'] == 1) {
return true;
}
return false;
}
function timestampToDatetime($timestamp)
{
$format = Config::get('dateformat') .' '. (Config::get('clock') == 12 ? 'g:i A' : 'H:i');
return formatTime($format, $timestamp);
}
function formatTime($format, $timestamp=0)
{
$lang = Lang::instance();
if($timestamp == 0) $timestamp = time();
$newformat = strtr($format, array('F'=>'%1', 'M'=>'%2'));
$adate = explode(',', date('n,'.$newformat, $timestamp), 2);
$s = $adate[1];
if($newformat != $format)
{
$am = (int)$adate[0];
$ml = $lang->get('months_long');
$ms = $lang->get('months_short');
$F = $ml[$am-1];
$M = $ms[$am-1];
$s = strtr($s, array('%1'=>$F, '%2'=>$M));
}
return $s;
}
function getUserName($userid) {
$db = DBConnection::instance();
$username = '';
if($userid > 0) {
$username = $db->sq("SELECT username FROM {$db->prefix}users WHERE id=$userid");
}
return $username;
}
function _e($s)
{
echo Lang::instance()->get($s);
}
function __($s)
{
return Lang::instance()->get($s);
}
function _r($s, $params) {
if(is_array($params)) {
return vsprintf(Lang::instance()->get($s), $params);
} else {
return vsprintf(Lang::instance()->get($s), array($params));
}
}
function yttinfo($v)
{
global $_yttinfo;
if(!isset($_yttinfo[$v])) {
echo get_yttinfo($v);
} else {
echo $_yttinfo[$v];
}
}
function get_yttinfo($v)
{
global $_yttinfo;
if(isset($_yttinfo[$v])) return $_yttinfo[$v];
switch($v)
{
case 'template_url':
$_yttinfo['template_url'] = get_yttinfo('ytt_url'). 'themes/'. Config::get('template') . '/';
return $_yttinfo['template_url'];
case 'url':
$_yttinfo['url'] = Config::get('url');
if($_yttinfo['url'] == '')
$_yttinfo['url'] = 'http://'.$_SERVER['HTTP_HOST'] .($_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '').
url_dir(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME']);
return $_yttinfo['url'];
case 'ytt_url':
$_yttinfo['ytt_url'] = Config::get('ytt_url');
if($_yttinfo['ytt_url'] == '') $_yttinfo['ytt_url'] = url_dir(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME']);
return $_yttinfo['ytt_url'];
case 'title':
$_yttinfo['title'] = (Config::get('title') != '') ? htmlarray(Config::get('title')) : __('Your Tiny Todolist');
return $_yttinfo['title'];
}
return false;
}
function jsonExit($data)
{
header('Content-type: application/json; charset=utf-8');
echo json_encode($data);
exit;
}