Repository navigation
Expand file tree
/
Copy pathController.php
More file actions
130 lines (115 loc) · 2.95 KB
/
Copy pathController.php
File metadata and controls
130 lines (115 loc) · 2.95 KB
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
<?php
namespace Core;
use App\Config;
use Exception;
/**
* Base controller
*
* PHP version 7.0
*/
abstract class Controller
{
/**
* Parameters from the matched route
*
* @var array
*/
protected $route_params = [];
/**
* Class constructor
*
* @param array $route_params Parameters from the route
*
* @return void
*/
public function __construct($route_params)
{
$this->route_params = $route_params;
}
/**
* Magic method called when a non-existent or inaccessible method is
* called on an object of this class. Used to execute before and after
* filter methods on action methods. Action methods need to be named
* with an "Action" suffix, e.g. indexAction, showAction etc.
*
* @param string $name Method name
* @param array $args Arguments passed to the method
*
* @return void
* @throws Exception
*/
public function __call($name, $args)
{
$method = $name . 'Action';
if (method_exists($this, $method)) {
if ($this->before() !== false) {
call_user_func_array([$this, $method], $args);
$this->after();
}
} else {
throw new Exception("Method $method not found in controller " . get_class($this));
}
}
/**
* Before filter - called before an action method.
*
* @return void
*/
protected function before()
{
if (!isset($_SESSION)) {
session_start();
}
}
/**
* After filter - called after an action method.
*
* @return void
*/
protected function after()
{
}
/**
* Redirect to a different page
*
* @param string $url The relative URL
*
* @return void
*/
public function redirect($url)
{
header('Location: http://' . $_SERVER['HTTP_HOST'] . $url, true, 303);
exit;
}
/**
* Get the originally-requested page to return to after requiring login, or default to the homepage
*
* @return string
*/
protected function get_return_to_page()
{
if (!isset($_SESSION)) {
session_start();
}
if (isset($_SESSION['return_to'])) {
return $_SESSION['return_to'];
} else {
return '/';
}
}
/**
* @param $captchaResponse
* @return bool TRUE on success FALSE otherwise
*/
public function verify_captcha($captchaResponse)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, ['secret' => Config::CAPTCHA_SECRET, 'response' => $captchaResponse]);
curl_setopt($curl, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = json_decode(curl_exec($curl));
curl_close($curl);
return $result->success;
}
}