PluginProbe
RSFirewall! / trunk
RSFirewall! vtrunk
rsfirewall / libraries / controller.php

controller.php in RSFirewall! trunk, at libraries/controller.php

189 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package RSFirewall!
4 * @copyright (c) 2018 RSJoomla!
5 * @link https://www.rsjoomla.com
6 * @license GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html
7 */
8
9 if ( ! defined( 'WPINC' ) ) {
10 die;
11 }
12
13 class RSFirewall_Controller
14 {
15 public $model;
16
17 /**
18 * Holds extended class filename (eg. RSFirewall_Controller_Configuration => configuration)
19 */
20 protected $filename;
21
22 /**
23 * Holds the rsfirewall version
24 */
25 protected $version;
26
27
28 /**
29 * RSFirewall_Controller constructor.
30 * Loads model.
31 */
32 public function __construct()
33 {
34 // Get filename
35 $this->filename = str_ireplace('RSFirewall_Controller_', '', get_class($this));
36
37 // Load the version number
38 $this->version = RSFirewall::get_instance()->get_version();
39
40 // Get proper model name
41 $model_name = RSFirewall_Helper::removeProPart($this->filename);
42
43 // Sanitize filename
44 $this->filename = strtolower($this->filename);
45 $this->filename = preg_replace('/[^a-z0-9_]/', '', $this->filename);
46
47 // Load model
48 $this->model = RSFirewall_Helper::call_user_func_pro(array('RSFirewall_Model_'.ucfirst($model_name), 'get_instance'));
49 }
50
51 /**
52 * Gets an instance to the requested controller
53 *
54 * @return RSFirewall_Controller
55 */
56 public static function get_instance()
57 {
58 static $instances = array();
59
60 $class = get_called_class();
61
62 if ( empty($instances[$class]) )
63 {
64 $instances[$class] = new $class;
65 }
66
67 return $instances[$class];
68 }
69
70 /**
71 * Loads the necessary css/scripts for the specific controller/view
72 */
73
74 public function load_scripts(){
75 add_action( 'admin_enqueue_scripts', array($this, 'enqueue_scripts') );
76 add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles') );
77 }
78
79 /**
80 * Must be declared in case we do not use them in a specific controller so that warnings would not show up
81 */
82 public function enqueue_scripts($pagenow){}
83 public function enqueue_styles($pagenow){}
84
85 /**
86 * Loads default view.
87 */
88 public function display($tmpl = '')
89 {
90 $template_name = $this->filename;
91 // if the Pro part is present then it must be removed
92 $template_name = RSFirewall_Helper::removeProPart($template_name);
93
94 if (is_string($tmpl) && !empty($tmpl)) {
95 $template_name .= '_'.$tmpl;
96 }
97
98 $path = RSFIREWALL_BASE . 'views/' . $template_name . '.php';
99 $pro_path = RSFIREWALL_BASE . 'proversion/views/' . $template_name . '.php';
100
101
102 if (!file_exists($pro_path) && !file_exists($path))
103 {
104 wp_die("View {$template_name} does not exist.");
105 }
106
107 // include first the pro then the normal
108 include (file_exists($pro_path) ? $pro_path : $path);
109 }
110
111 /**
112 * @param $val
113 *
114 * @return mixed|string
115 */
116 protected function json_encode( $val ) {
117 if ( is_string( $val ) ) {
118 $val = str_replace( array( "\n", "\r", "\t", "\v", "\f" ), array( '\n', '\r', '\t', '\v', '\f' ), $val );
119
120 return '"' . addcslashes( $val, '"\\' ) . '"';
121 }
122
123 if ( is_numeric( $val ) ) {
124 return $val;
125 }
126
127 if ( $val === null ) {
128 return 'null';
129 }
130
131 if ( $val === true ) {
132 return 'true';
133 }
134
135 if ( $val === false ) {
136 return 'false';
137 }
138
139 $assoc = is_array( $val ) ? array_keys( $val ) !== range( 0, count( $val ) - 1 ) : true;
140
141 $res = array();
142 foreach ( $val as $k => $v ) {
143 $v = $this->json_encode( $v );
144 if ( $assoc ) {
145 $k = '"' . addcslashes( $k, '"\\' ) . '"';
146 $v = $k . ':' . $v;
147 }
148 $res[] = $v;
149 }
150 $res = implode( ',', $res );
151
152 return ( $assoc ) ? '{' . $res . '}' : '[' . $res . ']';
153 }
154
155 /**
156 * @param $response
157 *
158 * @return mixed|string|void
159 */
160 protected function encode( $response ) {
161 $result = json_encode( $response );
162
163 // Added a failsafe in case the response isn't encoded - at least we'll have something to work with.
164 if ( $result === false ) {
165 $result = $this->json_encode( $response );
166 }
167
168 return $result;
169 }
170
171 /**
172 * @param $success
173 * @param null $data
174 */
175 protected function show_response( $success, $data = null ) {
176 // compute the response
177 $response = new stdClass();
178 $response->success = $success;
179 if ( $data ) {
180 $response->data = $data;
181 }
182
183 // show the response
184 echo $this->encode( $response );
185
186 // close
187 exit();
188 }
189 }