Merge lp:~vierbergenlars/remotecp-panel/offline-db-access into lp:~vierbergenlars/remotecp-panel/trunk

Proposed by Lars Vierbergen
Status: Merged
Approved by: Lars Vierbergen
Approved revision: 207
Merged at revision: 208
Proposed branch: lp:~vierbergenlars/remotecp-panel/offline-db-access
Merge into: lp:~vierbergenlars/remotecp-panel/trunk
Diff against target: 287 lines (+179/-7)
10 files modified
inc/controllers.php (+3/-1)
inc/exceptions/database.php (+143/-0)
inc/exceptions/exceptions.php (+6/-0)
inc/exceptions/handler.php (+11/-0)
inc/rdb/auth.php (+0/-3)
inc/rdb/db_info.php (+1/-0)
inc/rdb/rdb.php (+2/-1)
remote_panel.php (+11/-0)
system_remote/database.php (+1/-1)
system_remote/desktop.php (+1/-1)
To merge this branch: bzr merge lp:~vierbergenlars/remotecp-panel/offline-db-access
Reviewer Review Type Date Requested Status
Lars Vierbergen Approve
Review via email: mp+79880@code.launchpad.net

Description of the change

Add remote database offline access to admin area (offline-db-access blueprint)

To post a comment you must log in.
207. By Lars Vierbergen

Started implementation of blueprint offline-db-access.
Add exceptions

Revision history for this message
Lars Vierbergen (vierbergenlars) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'inc/controllers.php'
2--- inc/controllers.php 2011-09-02 16:38:47 +0000
3+++ inc/controllers.php 2011-10-19 19:44:25 +0000
4@@ -1,7 +1,9 @@
5 <?php //@./inc/controllers.php ?><?php
6+require_once 'inc/exceptions/exceptions.php';
7 require_once 'inc/errorhandler.php';
8 require_once 'inc/utils/magic_quotes.php';
9-ob_start('ob_gzhandler');
10+//ob_start('ob_gzhandler');
11+ob_start();
12 header('Cache-Control: private');
13 require_once 'inc/sitemgmt/constants.php';
14 require_once 'inc/JavaScript_errorhandler.php';
15
16=== added directory 'inc/exceptions'
17=== added file 'inc/exceptions/database.php'
18--- inc/exceptions/database.php 1970-01-01 00:00:00 +0000
19+++ inc/exceptions/database.php 2011-10-19 19:44:25 +0000
20@@ -0,0 +1,143 @@
21+<?php
22+/**
23+ * Handles exceptions related to the database
24+ * @author Lars Vierbergen
25+ * @package exceptions
26+ */
27+class DatabaseException extends RuntimeException {
28+ /**
29+ * Unknown error
30+ * @var int
31+ */
32+ const unknown=0;
33+ /**
34+ * Connection error
35+ * @var int
36+ */
37+ const connect=1;
38+ /**
39+ * Query error
40+ * @var int
41+ */
42+ const query=2;
43+ /**
44+ * Error on a row operation
45+ * @var int
46+ */
47+ const row=4;
48+ /**
49+ * Error on a SELECT statement
50+ * @var int
51+ */
52+ const select=8;
53+ /**
54+ * Error on an UPDATE statement
55+ * @var int
56+ */
57+ const update=16;
58+ /**
59+ * Error on an INSERT statement
60+ * @var int
61+ */
62+ const insert=32;
63+ /**
64+ * Error on a DELETE statement
65+ * @var int
66+ */
67+ const delete=64;
68+ /**
69+ * Error on a table operation
70+ * @var int
71+ */
72+ const table=128;
73+ /**
74+ * Error on a SHOW statement
75+ * @var int
76+ */
77+ const show=156;
78+ /**
79+ * Error on an ALTER statement
80+ * @var int
81+ */
82+ const alter=512;
83+ /**
84+ * Error on a CREATE statement
85+ * @var int
86+ */
87+ const create=1024;
88+ /**
89+ * Error on a DROP statement
90+ * @var int
91+ */
92+ const drop=2048;
93+ /**
94+ * The table on which the error occurs
95+ * @var string
96+ */
97+ protected $table='';
98+ /**
99+ * Throw a database-related error
100+ * @param string $table The table on which the error occured
101+ * @param int $code Explanation of the error by using class constants
102+ * @param Exception $previous The previous exception
103+ */
104+ function __construct($table='' ,$code=0, Exception $previous=NULL) {
105+ $this->table=$table;
106+ $this->code=$code;
107+ parent::__construct('Database failed on '.$this->table,$code,$previous);
108+ }
109+ /**
110+ * Get the table on which the exception occured
111+ * @return string Table name
112+ */
113+ final function getTable() {
114+ return $this->table;
115+ }
116+ /**
117+ * Get the type of the failure, the error codes are converted to a single string
118+ * @return string String representation of the exact exception that occured
119+ */
120+ final function getFailureType() {
121+ $errors='';
122+ if($this->code==self::unknown) {
123+ return 'UNKNOWN';
124+ }
125+ if($this->code&self::connect) {
126+ $errors.='connect>';
127+ }
128+ if($this->code&self::query) {
129+ $errors.='query>';
130+ }
131+ if($this->code&self::row) {
132+ $errors.='row:';
133+ }
134+ if($this->code&self::select) {
135+ $errors.='select,';
136+ }
137+ if($this->code&self::update) {
138+ $errors.='update,';
139+ }
140+ if($this->code&self::insert) {
141+ $errors.='insert,';
142+ }
143+ if($this->code&self::delete) {
144+ $errors.='delete,';
145+ }
146+ if($this->code&self::table) {
147+ $errors.='table:';
148+ }
149+ if($this->code&self::show) {
150+ $errors.='show,';
151+ }
152+ if($this->code&self::alter) {
153+ $errors.='alter,';
154+ }
155+ if($this->code&self::create) {
156+ $errors.='create,';
157+ }
158+ if($this->code&self::drop) {
159+ $errors.='drop,';
160+ }
161+ return substr($errors,0,-1);
162+ }
163+}
164\ No newline at end of file
165
166=== added file 'inc/exceptions/exceptions.php'
167--- inc/exceptions/exceptions.php 1970-01-01 00:00:00 +0000
168+++ inc/exceptions/exceptions.php 2011-10-19 19:44:25 +0000
169@@ -0,0 +1,6 @@
170+<?php
171+/**
172+ * A list of exceptions used by RemoteCP
173+ * @package exceptions
174+ */
175+require 'database.php';
176
177=== added file 'inc/exceptions/handler.php'
178--- inc/exceptions/handler.php 1970-01-01 00:00:00 +0000
179+++ inc/exceptions/handler.php 2011-10-19 19:44:25 +0000
180@@ -0,0 +1,11 @@
181+<?php
182+function default_exception_handler($e,$type) {
183+ header('HTTP/1.1 500 Internal Server Error');
184+ $es=$type.': <i>'.get_class($e).'</i>: <b>'.$e->getMessage().'</b> in <b>'.$e->getFile().'</b> on </b>'.$e->getLine().'</b>';
185+ if(defined('__DEBUG__')&&__DEBUG__) {
186+ $es.='<hr>Trace:<br><pre>';
187+ $es.=$e->getTraceAsString();
188+ $es.='</pre>';
189+ }
190+ echo '<h1>Internal Server Error</h1>'.$es;
191+}
192\ No newline at end of file
193
194=== modified file 'inc/rdb/auth.php'
195--- inc/rdb/auth.php 2011-09-02 16:38:47 +0000
196+++ inc/rdb/auth.php 2011-10-19 19:44:25 +0000
197@@ -39,9 +39,6 @@
198 else {
199 $remote_connection=@new MySQLi($rdbauth['host'],$rdbauth['user'],$rdbauth['password'],$rdbauth['database']);
200 }
201- if(!@$remote_connection->ping()) {
202- trigger_error('Could not connect to the remote database',E_USER_ERROR);
203- }
204 return $remote_connection;
205 }
206 }
207
208=== modified file 'inc/rdb/db_info.php'
209--- inc/rdb/db_info.php 2011-09-26 17:38:10 +0000
210+++ inc/rdb/db_info.php 2011-10-19 19:44:25 +0000
211@@ -3,6 +3,7 @@
212 static private $info=array();
213 static function init() {
214 global $remote_connection;
215+ if(!@$remote_connection->ping()) throw new DatabaseException('Remote database is down',DatabaseException::connect);
216 $q=$remote_connection->query('SELECT * FROM `_system_`');
217 while($r=$q->fetch_array()) {
218 self::$info[$r[0]]=$r[1];
219
220=== modified file 'inc/rdb/rdb.php'
221--- inc/rdb/rdb.php 2011-09-26 16:13:19 +0000
222+++ inc/rdb/rdb.php 2011-10-19 19:44:25 +0000
223@@ -5,10 +5,11 @@
224 */
225 require_once('inc/rdb/auth.php');
226 if(!rdb_auth::$exists) {
227- trigger_error('Remote database does not exist',E_USER_ERROR);
228+ throw new DatabaseException('Remote database does not exist',DatabaseException::connect);
229 }
230 else {
231 $remote_connection=rdb_auth::connect();
232+ if(!@$remote_connection->ping()) throw new DatabaseException('Remote database is not accessible',DatabaseException::connect);
233 }
234 require_once 'inc/rdb/db_info.php';
235 ?>
236\ No newline at end of file
237
238=== modified file 'remote_panel.php'
239--- remote_panel.php 2011-10-18 18:14:57 +0000
240+++ remote_panel.php 2011-10-19 19:44:25 +0000
241@@ -6,9 +6,20 @@
242 * This file is called from the webbrowser when using RemoteCP to connect to a remote database.
243 */
244 require('inc/controllers.php');
245+try {
246 require_once('inc/rdb/rdb.php');
247 require_once('inc/groups/groups.php');
248 require_once('inc/accountlevels.php');
249+}
250+catch(DatabaseException $e) {
251+ if($e->getCode()==DatabaseException::connect) {
252+ define('REMOTEDB_DOWN',true);
253+ }
254+ else {
255+ default_exception_handler($e, 'DatabaseException');
256+ exit;
257+ }
258+}
259 $t->addFile(__FILE__);
260 //Basic HTML start
261 ?>
262
263=== modified file 'system_remote/database.php'
264--- system_remote/database.php 2011-09-26 16:13:19 +0000
265+++ system_remote/database.php 2011-10-19 19:44:25 +0000
266@@ -54,7 +54,7 @@
267 </fieldset>
268 <fieldset>
269 <legend><?php echo $t->_('Information')?></legend>
270- <?php echo $t->_('Filesystem'); ?>: <?php echo rdb_info::getInfo('fs'); ?>
271+ <?php echo $t->_('Filesystem'); ?>: <?php if(defined('REMOTEDB_DOWN')&&REMOTEDB_DOWN) echo $t->_('Remote database is down'); else echo rdb_info::getInfo('fs'); ?>
272 </fieldset>
273 <fieldset>
274 <legend><?php echo $t->_('Delete database'); ?></legend>
275
276=== modified file 'system_remote/desktop.php'
277--- system_remote/desktop.php 2011-06-28 16:13:01 +0000
278+++ system_remote/desktop.php 2011-10-19 19:44:25 +0000
279@@ -4,7 +4,7 @@
280 <AJAX_SCRIPTEVAL>NULL</AJAX_SCRIPTEVAL>
281 < HEADERS -->
282 <?php if(rdb_auth::$level==9) {
283- ?>| <a onclick="openLoc('<?php echo $t->_('Administration'); ?>','?control=admin','admin');"><?php echo $t->_('Administration'); ?></a> |<?php } ?>| <a onclick="openLoc('<?php echo $t->_('Groups'); ?>','?control=groups','groups');"><?php echo $t->_('Groups'); ?></a> |
284+ ?>| <a onclick="openLoc('<?php echo $t->_('Administration'); ?>','?control=admin','admin');"><?php echo $t->_('Administration'); ?></a> |<?php } if(defined('REMOTEDB_DOWN')&&REMOTEDB_DOWN) {die('<hr>'.$t->_('Remote database is not available'));} ?>| <a onclick="openLoc('<?php echo $t->_('Groups'); ?>','?control=groups','groups');"><?php echo $t->_('Groups'); ?></a> |
285 <hr><?php
286 $q=$remote_connection->query('SELECT * FROM `sites` WHERE `user`="'.session::$guid.'"');
287 while($sites=$q->fetch_assoc()) { ?>| <a onclick="openLoc('<?php echo getSiteInfo($sites['ref'],'SITE_TITLE'); ?>','?control=addons&site=<?php echo $sites['ref']; ?>','<?php echo $sites['ref']; ?>_ctrls_main');"><?php echo getSiteInfo($sites['ref'],'SITE_TITLE'); ?></a> |<?php }

Subscribers

People subscribed via source and target branches

to all changes: