Skip to content

Commit bc60053

Browse files
committed
End of Website Weekend
1 parent 07a8785 commit bc60053

File tree

5,295 files changed

+1307520
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

5,295 files changed

+1307520
-0
lines changed

.ftpquota

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8193 290823511

apple-touch-icon-precomposed.png

Loading

apple-touch-icon.png

Loading

database_runner.php

+396
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,396 @@
1+
<?php
2+
/*
3+
4+
Author: Stephen Carnam
5+
Version: 1.0
6+
License: GPLv2
7+
Author URI: http://serverpress.com
8+
9+
This program is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU General Public License
11+
as published by the Free Software Foundation; either version 2
12+
of the License, or (at your option) any later version.
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22+
23+
*/
24+
25+
// Our database_runner app will move files into their correct positions followed by
26+
// processing the database in sequential chunks...
27+
28+
// Attempt to bump up meager memory hosts
29+
if((int) @ini_get('memory_limit') < 64){
30+
if(strpos(ini_get('disable_functions'), 'ini_set') === false){
31+
@ini_set('memory_limit', '64M');
32+
}
33+
}
34+
@set_time_limit( 600 );
35+
36+
if ( ! isset($_GET['session_id']) || ! isset($_GET['operation']) ){
37+
echo 'Invalid session or operation.';
38+
exit();
39+
}else{
40+
41+
// Recover session from DesktopServer for WordPress plugin
42+
session_id($_GET['session_id']);
43+
session_start();
44+
if ( ! isset($_SESSION['server_details']) ){
45+
echo 'Missing server_details.';
46+
exit();
47+
}
48+
}
49+
global $server_details;
50+
$server_details = $_SESSION['server_details'];
51+
52+
// Move files into position
53+
if ( $_GET['operation'] == 'move_files' ){
54+
$src = $server_details['DOCUMENT_ROOT'] . '/ds-deploy';
55+
$dst = rtrim( $server_details['DOCUMENT_ROOT'], '/' );
56+
$files = scandir ( $src );
57+
foreach ( $files as $file ){
58+
if ($file != "." && $file != ".." &&
59+
strpos( $file, '/ds-deploy/.htaccess' ) === false &&
60+
strpos( $file, 'database_runner.php' ) === false &&
61+
substr( $file, -4 ) != '.sql' ){
62+
rmove ( "$src/$file", "$dst/$file" );
63+
}
64+
}
65+
echo 'ok';
66+
exit();
67+
}
68+
69+
// Execute sequential database file
70+
if ( $_GET['operation'] == 'database' ){
71+
$src = $server_details['DOCUMENT_ROOT'] . '/ds-deploy';
72+
$files = scandir ( $src );
73+
74+
// Find first file iteration
75+
for ($n = 1; $n < 999; $n++){
76+
foreach ( $files as $file ){
77+
if ( substr( $file, 0, 8) == 'database' && substr( $file, -4) == '.sql' ){
78+
79+
// Process the file and remove it
80+
import_sql( $src . '/' . $file );
81+
rrmdir( $src . '/' . $file );
82+
echo 'ok';
83+
exit();
84+
}
85+
}
86+
}
87+
}
88+
89+
// Cleanup ds-deploy folder
90+
if ( $_GET['operation'] == 'cleanup' ){
91+
92+
// Move .htaccess last
93+
if ( is_file( $server_details['DOCUMENT_ROOT'] . '/ds-deploy/.htaccess' ) ) {
94+
rmove( $server_details['DOCUMENT_ROOT'] . '/ds-deploy/.htaccess', $server_details['DOCUMENT_ROOT'] . '/.htaccess' );
95+
}
96+
97+
// Remove ds-deploy
98+
$src = $server_details['DOCUMENT_ROOT'] . '/ds-deploy';
99+
rrmdir( $src );
100+
echo 'ok';
101+
exit();
102+
}
103+
104+
// Function to remove folders and files
105+
function rrmdir($dir) {
106+
if (is_dir($dir)) {
107+
$files = scandir($dir);
108+
foreach ($files as $file)
109+
if ($file != "." && $file != "..") rrmdir("$dir/$file");
110+
rmdir($dir);
111+
}
112+
else if (file_exists($dir)) unlink($dir);
113+
}
114+
115+
// Function to move folders and files
116+
function rmove($src, $dst) {
117+
if (file_exists ( $dst ))
118+
rrmdir ( $dst );
119+
if (is_dir ( $src )) {
120+
121+
// Try rename first
122+
if ( ! @rename($src, $dst) ){
123+
124+
// Otherwise make destination folder and xfer file by file
125+
mkdir ( $dst );
126+
$files = scandir ( $src );
127+
foreach ( $files as $file )
128+
if ($file != "." && $file != "..")
129+
rmove ( "$src/$file", "$dst/$file" );
130+
}
131+
} elseif (file_exists ( $src )){
132+
copy ( $src, $dst );
133+
@unlink( $src );
134+
}
135+
}
136+
137+
// Import the givn database sql file
138+
function import_sql( $file ){
139+
global $server_details;
140+
$buffer = @file_get_contents( $file );
141+
142+
// Accelerate imports on newer MySQL stacks
143+
$buffer = "SET NAMES utf8;\n SET autocommit=0;\n SET foreign_key_checks=0;\n SET unique_checks=0;\n SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n " . $buffer;
144+
$buffer .= "\n COMMIT;\n SET unique_checks=1;\n; SET foreign_key_checks=1;\n";
145+
if ( false === $buffer ){
146+
echo 'Error - Unable to get database.sql contents.';
147+
exit();
148+
}
149+
150+
// Connect to database and import our script with adapted phpMyAdmin parser v3.5.7
151+
$dsn = 'mysql:host=' . $server_details['DB_HOST'];
152+
$dsn .= ';dbname=' . $server_details['DB_NAME'];
153+
try {
154+
$dbc = new PDO( $dsn, $server_details['DB_USER'], $server_details['DB_PASSWORD'] );
155+
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
156+
} catch (PDOException $e) {
157+
echo 'Error - ' . $e->getMessage();
158+
exit();
159+
}
160+
161+
//
162+
// *** Set up prerequisite to use phpMyAdmin's SQL Import plugin
163+
//
164+
$GLOBALS['finished'] = true;
165+
166+
// Defaults for parser
167+
$sql = '';
168+
$start_pos = 0;
169+
$i = 0;
170+
$len= 0;
171+
$big_value = 2147483647;
172+
$delimiter_keyword = 'DELIMITER '; // include the space because it's mandatory
173+
$length_of_delimiter_keyword = strlen($delimiter_keyword);
174+
$sql_delimiter = ';';
175+
176+
// Current length of our buffer
177+
$len = strlen($buffer);
178+
179+
// Grab some SQL queries out of it
180+
while ($i < $len) {
181+
$found_delimiter = false;
182+
183+
// Find first interesting character
184+
$old_i = $i;
185+
// this is about 7 times faster than looking for each sequence one by one with strpos()
186+
if (preg_match('/(\'|"|#|-- |\/\*|`|(?i)(?<![A-Z0-9_])' . $delimiter_keyword . ')/', $buffer, $matches, PREG_OFFSET_CAPTURE, $i)) {
187+
// in $matches, index 0 contains the match for the complete
188+
// expression but we don't use it
189+
$first_position = $matches[1][1];
190+
} else {
191+
$first_position = $big_value;
192+
}
193+
/**
194+
* @todo we should not look for a delimiter that might be
195+
* inside quotes (or even double-quotes)
196+
*/
197+
// the cost of doing this one with preg_match() would be too high
198+
$first_sql_delimiter = strpos($buffer, $sql_delimiter, $i);
199+
if ($first_sql_delimiter === false) {
200+
$first_sql_delimiter = $big_value;
201+
} else {
202+
$found_delimiter = true;
203+
}
204+
205+
// set $i to the position of the first quote, comment.start or delimiter found
206+
$i = min($first_position, $first_sql_delimiter);
207+
208+
if ($i == $big_value) {
209+
// none of the above was found in the string
210+
211+
$i = $old_i;
212+
if (!$GLOBALS['finished']) {
213+
break;
214+
}
215+
// at the end there might be some whitespace...
216+
if (trim($buffer) == '') {
217+
$buffer = '';
218+
$len = 0;
219+
break;
220+
}
221+
// We hit end of query, go there!
222+
$i = strlen($buffer) - 1;
223+
}
224+
225+
// Grab current character
226+
$ch = $buffer[$i];
227+
228+
// Quotes
229+
if (strpos('\'"`', $ch) !== false) {
230+
$quote = $ch;
231+
$endq = false;
232+
while (!$endq) {
233+
// Find next quote
234+
$pos = strpos($buffer, $quote, $i + 1);
235+
/*
236+
* Behave same as MySQL and accept end of query as end of backtick.
237+
* I know this is sick, but MySQL behaves like this:
238+
*
239+
* SELECT * FROM `table
240+
*
241+
* is treated like
242+
*
243+
* SELECT * FROM `table`
244+
*/
245+
if ($pos === false && $quote == '`' && $found_delimiter) {
246+
$pos = $first_sql_delimiter - 1;
247+
// No quote? Too short string
248+
} elseif ($pos === false) {
249+
// We hit end of string => unclosed quote, but we handle it as end of query
250+
if ($GLOBALS['finished']) {
251+
$endq = true;
252+
$i = $len - 1;
253+
}
254+
$found_delimiter = false;
255+
break;
256+
}
257+
// Was not the quote escaped?
258+
$j = $pos - 1;
259+
while ($buffer[$j] == '\\') $j--;
260+
// Even count means it was not escaped
261+
$endq = (((($pos - 1) - $j) % 2) == 0);
262+
// Skip the string
263+
$i = $pos;
264+
265+
if ($first_sql_delimiter < $pos) {
266+
$found_delimiter = false;
267+
}
268+
}
269+
if (!$endq) {
270+
break;
271+
}
272+
$i++;
273+
// Aren't we at the end?
274+
if ($GLOBALS['finished'] && $i == $len) {
275+
$i--;
276+
} else {
277+
continue;
278+
}
279+
}
280+
281+
// Not enough data to decide
282+
if ((($i == ($len - 1) && ($ch == '-' || $ch == '/'))
283+
|| ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-')
284+
|| ($ch == '/' && $buffer[$i + 1] == '*')))) && !$GLOBALS['finished']) {
285+
break;
286+
}
287+
288+
// Comments
289+
if ($ch == '#'
290+
|| ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-'
291+
&& (($i < ($len - 2) && $buffer[$i + 2] <= ' ')
292+
|| ($i == ($len - 1) && $GLOBALS['finished'])))
293+
|| ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*')
294+
) {
295+
// Copy current string to SQL
296+
if ($start_pos != $i) {
297+
$sql .= substr($buffer, $start_pos, $i - $start_pos);
298+
}
299+
// Skip the rest
300+
$start_of_comment = $i;
301+
// do not use PHP_EOL here instead of "\n", because the export
302+
// file might have been produced on a different system
303+
$i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i);
304+
// didn't we hit end of string?
305+
if ($i === false) {
306+
if ($GLOBALS['finished']) {
307+
$i = $len - 1;
308+
} else {
309+
break;
310+
}
311+
}
312+
// Skip *
313+
if ($ch == '/') {
314+
$i++;
315+
}
316+
// Skip last char
317+
$i++;
318+
// We need to send the comment part in case we are defining
319+
// a procedure or function and comments in it are valuable
320+
$sql .= substr($buffer, $start_of_comment, $i - $start_of_comment);
321+
// Next query part will start here
322+
$start_pos = $i;
323+
// Aren't we at the end?
324+
if ($i == $len) {
325+
$i--;
326+
} else {
327+
continue;
328+
}
329+
}
330+
// Change delimiter, if redefined, and skip it (don't send to server!)
331+
if (strtoupper(substr($buffer, $i, $length_of_delimiter_keyword)) == $delimiter_keyword
332+
&& ($i + $length_of_delimiter_keyword < $len)) {
333+
// look for EOL on the character immediately after 'DELIMITER '
334+
// (see previous comment about PHP_EOL)
335+
$new_line_pos = strpos($buffer, "\n", $i + $length_of_delimiter_keyword);
336+
// it might happen that there is no EOL
337+
if (false === $new_line_pos) {
338+
$new_line_pos = $len;
339+
}
340+
$sql_delimiter = substr($buffer, $i + $length_of_delimiter_keyword, $new_line_pos - $i - $length_of_delimiter_keyword);
341+
$i = $new_line_pos + 1;
342+
// Next query part will start here
343+
$start_pos = $i;
344+
continue;
345+
}
346+
347+
// End of SQL
348+
if ($found_delimiter || ($GLOBALS['finished'] && ($i == $len - 1))) {
349+
$tmp_sql = $sql;
350+
if ($start_pos < $len) {
351+
$length_to_grab = $i - $start_pos;
352+
353+
if (! $found_delimiter) {
354+
$length_to_grab++;
355+
}
356+
$tmp_sql .= substr($buffer, $start_pos, $length_to_grab);
357+
unset($length_to_grab);
358+
}
359+
// Do not try to execute empty SQL
360+
if (! preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
361+
$sql = $tmp_sql;
362+
363+
//
364+
// Execute on our connection
365+
//
366+
try {
367+
if ( $dbc->exec( $sql ) === FALSE ){
368+
$error = $dbc->errorInfo();
369+
$dbc = null;
370+
echo 'Error - ' . $error[2];
371+
exit();
372+
}
373+
} catch (PDOException $e) {
374+
echo 'Error - ' . $e->getMessage();
375+
exit();
376+
}
377+
$buffer = substr($buffer, $i + strlen($sql_delimiter));
378+
379+
// Reset parser:
380+
$len = strlen($buffer);
381+
$sql = '';
382+
$i = 0;
383+
$start_pos = 0;
384+
// Any chance we will get a complete query?
385+
//if ((strpos($buffer, ';') === false) && !$GLOBALS['finished']) {
386+
if ((strpos($buffer, $sql_delimiter) === false) && !$GLOBALS['finished']) {
387+
break;
388+
}
389+
} else {
390+
$i++;
391+
$start_pos = $i;
392+
}
393+
}
394+
} // End of parser loop
395+
$dbc = null;
396+
}

favicon.ico

Whitespace-only changes.

0 commit comments

Comments
 (0)