PageRenderTime 84ms CodeModel.GetById 33ms app.highlight 6ms RepoModel.GetById 43ms app.codeStats 0ms

/include/inc.php

https://github.com/aaronpk/GraphThis
PHP | 93 lines | 68 code | 22 blank | 3 comment | 19 complexity | 9c01bee8b1b0f36988e2fa91ff53e09a MD5 | raw file
 1<?php
 2ini_set('error_reporting', E_ALL);
 3date_default_timezone_set('UTC');
 4
 5if(!file_exists(dirname(__FILE__) . '/config.php'))
 6{
 7	die('Setup not complete: Copy config.template.php to config.php and modify the configuration settings to match your environment.');
 8}
 9
10require_once(dirname(__FILE__) . '/config.php');
11
12if(!array_key_exists('SHELL', $_SERVER)) {
13	session_set_cookie_params(365*24*60*60);
14	session_start();
15}
16
17$START_TIME = microtime(TRUE);
18
19require_once('functions.php');
20require_once('Model.php');
21require_once('Model/User.php');
22require_once('Model/Graph.php');
23require_once('Model/GraphSeries.php');
24require_once('Model/GraphData.php');
25require_once('Model/GraphDataNote.php');
26require_once('Model/GraphString.php');
27
28$headerTags = array();
29
30$user = FALSE;
31
32if(!array_key_exists('argv', $_SERVER)) {
33	// Don't do this stuff for cli scripts
34	
35	if(get('login_token') && session('noLoginRedirect') != 1) {
36		$user = Model_User::createFromLoginToken(get('login_token'));
37		if($user) {
38			$_SESSION['userID'] = $user->id;
39			Model_User::update(array('lastLoginAt'=>date('Y-m-d H:i:s')), $user->id);
40			
41			// Redirect to the page they were trying to get to
42			$redirect = trim(str_replace('login_token=' . get('login_token'), '', $_SERVER['REQUEST_URI']), '?');
43			if($redirect != '/login') {
44				header('Location: ' . $redirect);
45				die();
46			}
47		}
48	} else {
49		if(session('userID')) {
50			$user = new Model_User(session('userID'));
51		}
52	}
53	
54	// Redirect to the login page if they're not logged in now
55	if(($user == FALSE || $user->id == FALSE) && !in_array($_SERVER['SCRIPT_NAME'], array('/login.php', '/index.php', '/help.php'))) {
56		header('Location: /login');
57		die();
58	}
59}
60
61
62if(($user && $user->id != FALSE)) {
63	$timezone = new DateTimeZone($user->timezone);
64	date_default_timezone_set($user->timezone);
65}
66
67
68function formatText($text) {
69	$text = nl2br($text);
70
71	$expressions[] = '|(https?://[^\s]+)|';
72	$replacements[] = '<a href="$1">$1</a>';
73
74	$expressions[] = '/(?<![a-z0-9_])@([a-z0-9_]+)/i';
75	$replacements[] = '<a href="http://twitter.com/$1">@$1</a>';
76    
77	$expressions[] = '|([a-z0-9_\.\+\-]+@[a-z0-9_\.\+\-]+\.[a-z0-9\.]{2,4})|i';
78	$replacements[] = '<a href="mailto:$1">$1</a>';
79
80	$text = preg_replace($expressions, $replacements, $text);
81	
82	return $text;
83}
84
85
86function uuidSecure()
87{
88    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
89        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
90        mt_rand( 0, 0x0fff ) | 0x4000,
91        mt_rand( 0, 0x3fff ) | 0x8000,
92        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
93}