PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_joomlapack/includes/CDBDump.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 743 lines | 394 code | 107 blank | 242 comment | 56 complexity | f3458572b38ca979dc2efc9b80cca318 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package JoomlaPack
  4. * @subpackage CUBE
  5. * @copyright Copyright (C) 2006-2008 JoomlaPack Developers. All rights reserved.
  6. * @version $Id$
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. *
  9. * JoomlaPack is free software. This version may have been modified pursuant
  10. * to the GNU General Public License, and as distributed it includes or
  11. * is derivative of works licensed under the GNU General Public License or
  12. * other free or open source software licenses.
  13. **/
  14. // ensure this file is being included by a parent file - Joomla! 1.0.x and 1.5 compatible
  15. (defined( '_VALID_MOS' ) || defined('_JEXEC')) or die( 'Direct Access to this location is not allowed.' );
  16. $config = CConfiguration::getInstance();
  17. define('JPROWSPERSTEP', $config->get('mnRowsPerStep') ); // Default is dumping 100 rows per step
  18. /**
  19. * A generic MySQL database dump class, using Joomla!'s JDatabase class for handling the connection.
  20. * Configuration parameters:
  21. * isJoomla <boolean> True to use the existing Joomla! DB connection, false to create connection to another db
  22. * useFilters <string> Should I use db table exclusion filters? Default equals the isJoomla setting above
  23. * host <string> MySQL database server host name or IP address
  24. * port <string> MySQL database server port (optional)
  25. * username <string> MySQL user name, for authentication
  26. * password <string> MySQL password, for authentication
  27. * database <string> MySQL database
  28. * dumpFile <string> Absolute path to dump file; must be writable (optional; if left blank it is automatically calculated)
  29. */
  30. class CDBDump extends CJPEngineParts
  31. {
  32. // **********************************************************************
  33. // Configuration parameters
  34. // **********************************************************************
  35. /**
  36. * True to use the existing Joomla! DB connection, false to create connection to another db
  37. *
  38. * @var boolean
  39. */
  40. var $_isJoomla = true;
  41. /**
  42. * Should I use db table exclusion filters? Default equals the isJoomla setting above
  43. *
  44. * @var string
  45. */
  46. var $_useFilters = true;
  47. /**
  48. * MySQL database server host name or IP address
  49. *
  50. * @var string
  51. */
  52. var $_host = '';
  53. /**
  54. * MySQL database server port (optional)
  55. *
  56. * @var string
  57. */
  58. var $_port = '';
  59. /**
  60. * MySQL user name, for authentication
  61. *
  62. * @var string
  63. */
  64. var $_username = '';
  65. /**
  66. * MySQL password, for authentication
  67. *
  68. * @var string
  69. */
  70. var $_password = '';
  71. /**
  72. * MySQL database
  73. *
  74. * @var string
  75. */
  76. var $_database = '';
  77. /**
  78. * Absolute path to dump file; must be writable (optional; if left blank it is automatically calculated)
  79. *
  80. * @var string
  81. */
  82. var $_dumpFile = '';
  83. // **********************************************************************
  84. // Private fields
  85. // **********************************************************************
  86. /**
  87. * Is this a database only backup? Assigned from CCUBE settings.
  88. *
  89. * @var boolean
  90. */
  91. var $_DBOnly = false;
  92. /**
  93. * The database exclusion filters, as a simple array
  94. *
  95. * @var array
  96. */
  97. var $_exclusionFilters = array();
  98. /**
  99. * A simple array of table names to be included in the backup set
  100. *
  101. * @var array
  102. */
  103. var $_tables = array();
  104. /**
  105. * Is JoomFish installed? If it is, we have to cope for this and modify our
  106. * database calls
  107. *
  108. * @var boolean
  109. */
  110. var $_hasJoomFish = false;
  111. /**
  112. * Absolute path to core tables backup file
  113. *
  114. * @var string
  115. */
  116. var $_filenameCore = '';
  117. /**
  118. * Absolute path to sample content backup file
  119. *
  120. * @var string
  121. * @deprecated
  122. */
  123. var $_filenameSample = '';
  124. /**
  125. * The next table to backup
  126. *
  127. * @var string
  128. */
  129. var $_nextTable;
  130. /**
  131. * The next row of the table to start backing up from
  132. *
  133. * @var integer
  134. */
  135. var $_nextRange;
  136. /**
  137. * Current table's row count
  138. *
  139. * @var integer
  140. */
  141. var $_maxRange;
  142. /**
  143. * Implements the constructor of the class
  144. *
  145. * @return CDBDump
  146. */
  147. function CDBDump()
  148. {
  149. $this->_DomainName = "PackDB";
  150. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: New instance");
  151. }
  152. /**
  153. * Implements the _prepare abstract method
  154. *
  155. */
  156. function _prepare()
  157. {
  158. // Process parameters, passed to us using the setup() public method
  159. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Processing parameters");
  160. if( is_array($this->_parametersArray) ) {
  161. $this->_isJoomla = array_key_exists('isJoomla', $this->_parametersArray) ? $this->_parametersArray['isJoomla'] : $this->_isJoomla;
  162. $this->_useFilters = array_key_exists('isJoomla', $this->_parametersArray) ? $this->_parametersArray['useFilters'] : $this->_isJoomla;
  163. $this->_host = array_key_exists('host', $this->_parametersArray) ? $this->_parametersArray['host'] : $this->_host;
  164. $this->_port = array_key_exists('port', $this->_parametersArray) ? $this->_parametersArray['port'] : $this->_port;
  165. $this->_username = array_key_exists('username', $this->_parametersArray) ? $this->_parametersArray['username'] : $this->_username;
  166. $this->_password = array_key_exists('password', $this->_parametersArray) ? $this->_parametersArray['password'] : $this->_password;
  167. $this->_dumpFile = array_key_exists('dumpFile', $this->_parametersArray) ? $this->_parametersArray['dumpFile'] : $this->_dumpFile;
  168. $this->_database = array_key_exists('database', $this->_parametersArray) ? $this->_parametersArray['database'] : $this->_dumpFile;
  169. }
  170. // Get DB backup only mode
  171. $cube =& CCUBE::getInstance();
  172. $this->_DBOnly = $cube->_OnlyDBMode;
  173. unset( $cube );
  174. // Detect JoomFish
  175. $this->_hasJoomFish = file_exists(JPSiteRoot . '/administrator/components/com_joomfish/config.joomfish.php');
  176. // Fetch the database exlusion filters
  177. $this->_getExclusionFilters();
  178. // Find tables to be included and put them in the $_tables variable
  179. $this->_getTablesToBackup();
  180. // Find where to store the database backup files
  181. $this->_getBackupFilePaths();
  182. // Remove any leftovers
  183. $this->_removeOldFiles();
  184. // Initialize the algorithm
  185. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Initializing algorithm for first run");
  186. $this->_nextTable = array_shift( $this->_tables );
  187. $this->_nextRange = 0;
  188. $this->_isPrepared = true;
  189. }
  190. /**
  191. * Implements the _run() abstract method
  192. */
  193. function _run()
  194. {
  195. // Check if we are already done
  196. if ($this->_hasRan) {
  197. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Already finished");
  198. $this->_isRunning = false;
  199. $this->_Step = "";
  200. $this->_Substep = "";
  201. return;
  202. }
  203. // Mark ourselves as still running (we will test if we actually do towards the end ;) )
  204. $this->_isRunning = true;
  205. // Initialize local variables
  206. $db = $this->_getDB();
  207. if( $db === false ) return;
  208. $outCreate = ''; // Used for outputting CREATE TABLE commands
  209. $outData = ''; // Used for outputting INSERT INTO commands
  210. $this->_enforceSQLCompatibility(); // Apply MySQL compatibility option
  211. // Get this table's canonical and abstract name
  212. $tableName = $this->_nextTable;
  213. $tableAbstract = $this->_getAbstract( $tableName );
  214. // If it is the first run, find number of rows and get the CREATE TABLE command
  215. if( $this->_nextRange == 0 )
  216. {
  217. $this->_getRowCount( $tableAbstract );
  218. $outCreate = $this->_getCreateTable( $tableAbstract, $tableName );
  219. }
  220. // Ugly hack to make JoomlaPack skip over #__jp_packvars
  221. if( $tableAbstract == '#__jp_packvars' )
  222. {
  223. CJPLogger::WriteLog(_JP_LOG_INFO, "Skipping table " . $this->_nextTable);
  224. $this->_nextRange = $this->_maxRange + 1;
  225. $numRows = 0;// joostina pach
  226. }
  227. // Check if we have more work to do on this table
  228. if( $this->_nextRange < $this->_maxRange )
  229. {
  230. // Get the number of rows left to dump from the current table
  231. $sql = "select * from `$tableAbstract`";
  232. $db->setQuery( $sql, $this->_nextRange, JPROWSPERSTEP );
  233. $db->query();
  234. $numRows = $db->getNumRows();
  235. CJPLogger::WriteLog(_JP_LOG_DEBUG, "Dumping $numRows rows of " . $this->_nextTable);
  236. // Only dump if we have more than 0 rows to dump
  237. if ($numRows > 0)
  238. {
  239. $startRange = $this->_nextRange; // This is the range we start from, minus 1
  240. $sql = "select * from `$tableAbstract`";
  241. $db->setQuery( $sql, $startRange, JPROWSPERSTEP ); // 1.2a - Optimized database dump to use fewer db queries
  242. $dataDump = $db->loadAssocList();
  243. $numRows = 0;
  244. foreach( $dataDump as $myRow ) {
  245. $numRows++;
  246. $sql = "select * from `$tableAbstract`";
  247. $db->setQuery( $sql, $startRange, 1 );
  248. $numOfFields = count( $myRow );
  249. $outData .= "INSERT INTO `" . ($this->_DBOnly ? $tableName : $tableAbstract) . "` VALUES (";
  250. // Step through each of the row's values
  251. $fieldID = 0;
  252. // Fix 1.2a - NULL values were being skipped
  253. foreach( $myRow as $value )
  254. {
  255. // The ID of the field, used to determine placement of commas
  256. $fieldID++;
  257. // Post-process the value
  258. if( is_null($value) )
  259. {
  260. $value = 'null'; // Cope for null values
  261. } else {
  262. // Accomodate if runtime magic quotes are present
  263. $value = get_magic_quotes_runtime() ? stripslashes( $value ) : $value;
  264. // Escape the value
  265. $value = $db->getEscaped( $value );
  266. // Construct the output and append it
  267. }
  268. $outData .= "'$value'";
  269. if( $fieldID < $numOfFields ) $outData .= ', ';
  270. } // foreach
  271. $outData .=");\n";
  272. } // for (all rows left)
  273. } // if numRows > 0...
  274. // Advance the _nextRange pointer
  275. $this->_nextRange += ($numRows != 0) ? $numRows : 1;
  276. $this->_Step = $tableName;
  277. $this->_Substep = $this->_nextRange . ' / ' . $this->_maxRange;
  278. } // if more work on the table
  279. else
  280. {
  281. // Tell the user we are done with the table
  282. CJPLogger::WriteLog(_JP_LOG_DEBUG, "Done with " . $this->_nextTable);
  283. if( count($this->_tables) == 0 )
  284. {
  285. // We have finished dumping the database!
  286. CJPLogger::WriteLog(_JP_LOG_INFO, "Database has been successfully dumped to SQL file(s)");
  287. $this->_isRunning = false;
  288. $this->_hasRan = true;
  289. $this->_Step = '';
  290. $this->_Substep = '';
  291. $this->_nextTable = '';
  292. $this->_nextRange = 0;
  293. } else {
  294. // Switch tables
  295. $this->_nextTable = array_shift( $this->_tables );
  296. $this->_nextRange = 0;
  297. $this->_Step = $this->_nextTable;
  298. $this->_Substep = '';
  299. }
  300. }
  301. $this->_writeDump( $outCreate, $outData, $tableAbstract );
  302. }
  303. /**
  304. * Implements the _finalize() abstract method
  305. *
  306. */
  307. function _finalize()
  308. {
  309. // If we are not just doing a db only backup, add a fragment for the SQL file(s)
  310. if( !$this->_DBOnly )
  311. {
  312. $db = CJVAbstract::getDatabase();
  313. $fileList = array();
  314. $fragmentSize = 0;
  315. $filename = $this->_filenameCore;
  316. $filesize = filesize( $filename );
  317. $fragmentSize += $filesize;
  318. $fileList[] = $filename;
  319. // This code is obsolete, needed for the modified J! 1.0.11 installer
  320. if ($this->_filenameCore != $this->_filenameSample) {
  321. $filename = $this->_filenameSample;
  322. $filesize = filesize( $filename );
  323. $fragmentSize += $filesize;
  324. $fileList[] = $filename;
  325. }
  326. $fragmentDescriptor = array();
  327. $fragmentDescriptor['type'] = "sql";
  328. $fragmentDescriptor['size'] = $fragmentSize;
  329. $fragmentDescriptor['files'] = $fileList;
  330. $serializedDescriptor = serialize($fragmentDescriptor);
  331. $sql = 'SELECT COUNT(*) FROM #__jp_packvars WHERE `key` LIKE "fragment%"';
  332. $db->setQuery( $sql );
  333. $currentNode = $db->loadResult();
  334. $currentNode++;
  335. CJPTables::WriteVar("fragment$currentNode", $serializedDescriptor);
  336. CJPLogger::WriteLog(_JP_LOG_DEBUG, "Added new fragment #$currentNode with SQL file '$filename'");
  337. }
  338. $this->_isFinished = true;
  339. }
  340. /**
  341. * Gets the database exclusion filters through the Filter Manager class
  342. */
  343. function _getExclusionFilters()
  344. {
  345. if( $this->_useFilters )
  346. {
  347. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Retrieving db exclusion filters");
  348. CJVAbstract::import('CFilterManager');
  349. $filterManager = new CFilterManager();
  350. $filterManager->init();
  351. $this->_exclusionFilters = $filterManager->getFilters('database');
  352. unset( $filterManager );
  353. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Retrieved db exclusion filters, OK.");
  354. } else {
  355. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Skipping filters");
  356. $this->_exclusionFilters = array();
  357. }
  358. }
  359. /**
  360. * Finds the table names to be included in the backup set and puts them in the
  361. * $this->_tables array.
  362. */
  363. function _getTablesToBackup()
  364. {
  365. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Finding tables to include in the backup set");
  366. $db = $this->_getDB();
  367. $sql = "show tables";
  368. $db->setQuery( $sql );
  369. $db->query();
  370. $allTables = $db->loadResultArray();
  371. if( count($this->_exclusionFilters) > 0 )
  372. {
  373. // If we have filters, make sure the tables pass the filtering
  374. $this->_tables = array();
  375. foreach( $allTables as $myTable )
  376. {
  377. if( !in_array( $myTable, $this->_exclusionFilters ) ) $this->_tables[] = $myTable;
  378. }
  379. } else {
  380. // If no filters are set, just include every single table
  381. $this->_tables = $allTables;
  382. }
  383. }
  384. /**
  385. * Find where to store the backup files
  386. */
  387. function _getBackupFilePaths()
  388. {
  389. $configuration = CConfiguration::getInstance();
  390. $folderPath = $configuration->TempDirectory;
  391. if ($this->_DBOnly ) {
  392. // On DB Only backups we use different naming, no matter what's the setting
  393. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Only dump database mode detected");
  394. $this->_filenameCore = CJVAbstract::getExpandedTarName( '.sql' );
  395. $this->_filenameSample = $this->_filenameCore;
  396. } else {
  397. if( $this->_dumpFile != '' )
  398. {
  399. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Forced filename using dumpFile found.");
  400. // If the dumpFile was set, forcibly use this value
  401. $this->_filenameCore = $folderPath . "/" . $this->_dumpFile;
  402. $this->_filenameSample = $folderPath . "/" . $this->_dumpFile;
  403. } else {
  404. if( $this->_isJoomla )
  405. {
  406. // Joomla! Core Database, use the JoomlaPack way of figuring out the filenames
  407. if ($configuration->AltInstaller->SQLDumpMode == "split") {
  408. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Split files detected");
  409. $this->_filenameCore = $folderPath . "/joomla.sql";
  410. $this->_filenameSample = $folderPath . "/sample_data.sql";
  411. } else {
  412. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Single file detected");
  413. $this->_filenameCore = $folderPath . "/" .$configuration->AltInstaller->BaseDump;
  414. $this->_filenameSample = $folderPath . "/" . $configuration->AltInstaller->BaseDump;
  415. }
  416. } else {
  417. // External databases, we use the database's name
  418. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: External database detected");
  419. $this->_filenameCore = $folderPath . "/" . $this->_database . '.sql';
  420. $this->_filenameSample = $this->_filenameCore;
  421. }
  422. }
  423. }
  424. $this->_filenameCore = CJVAbstract::TranslateWinPath( $this->_filenameCore );
  425. $this->_filenameSample = CJVAbstract::TranslateWinPath( $this->_filenameSample );
  426. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBBackupEngine :: Core SQL file is " . $this->_filenameCore);
  427. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBBackupEngine :: Sample SQL file is " . $this->_filenameSample);
  428. }
  429. /**
  430. * Deletes any leftover files from previous backup attempts
  431. *
  432. */
  433. function _removeOldFiles()
  434. {
  435. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBBackupEngine :: Deleting leftover files, if any");
  436. if( file_exists( $this->_filenameCore ) ) @unlink( $this->_filenameCore );
  437. if( file_exists( $this->_filenameSample ) ) @unlink( $this->_filenameSample );
  438. }
  439. /**
  440. * Applies the SQL compatibility setting
  441. */
  442. function _enforceSQLCompatibility()
  443. {
  444. $configuration = CConfiguration::getInstance();
  445. $db = $this->_getDB();
  446. switch( $configuration->MySQLCompat )
  447. {
  448. case 'compat':
  449. $sql = "SET SESSION sql_mode='HIGH_NOT_PRECEDENCE,NO_TABLE_OPTIONS'";
  450. break;
  451. default:
  452. $sql = "SET SESSION sql_mode=''";
  453. break;
  454. }
  455. $db->setQuery( $sql );
  456. $db->query();
  457. }
  458. /**
  459. * Returns a table's abstract name (replacing the prefix with the magic #__ string)
  460. *
  461. * @param string $tableName The canonical name, e.g. 'jos_content'
  462. * @return string The abstract name, e.g. '#__content'
  463. */
  464. function _getAbstract( $tableName )
  465. {
  466. // FIX 1.2 Stable - Handle (very rare) cases with an empty db prefix
  467. $prefix = CJVAbstract::getDBPrefix();
  468. switch( $prefix )
  469. {
  470. case '':
  471. // This is more of a hack; it assumes all tables are Joomla! tables if the prefix is empty.
  472. return '#__' . $tableName;
  473. break;
  474. default:
  475. // Normal behaviour for 99% of sites
  476. return str_replace( $prefix, "#__", $tableName );
  477. break;
  478. }
  479. }
  480. /**
  481. * Gets the row count for table $tableAbstract. Also updates the $this->_maxRange variable.
  482. *
  483. * @param string $tableAbstract The abstract name of the table (works with canonical names too, though)
  484. * @return integer Row count of the table
  485. */
  486. function _getRowCount( $tableAbstract )
  487. {
  488. $db = $this->_getDB();
  489. $sql = "SELECT COUNT(*) FROM `$tableAbstract`";
  490. $db->setQuery( $sql );
  491. $this->_maxRange = $this->_hasJoomFish ? $db->loadResult(false) : $db->loadResult();
  492. CJPLogger::WriteLog(_JP_LOG_DEBUG, "Rows on " . $this->_nextTable . " : " . $this->_maxRange);
  493. return $this->_maxRange;
  494. }
  495. /**
  496. * Gets the CREATE TABLE command for a given table
  497. *
  498. * @param string $tableAbstract The abstract name of the table (works with canonical names too, though)
  499. * @param string $tableName The canonical name of the table
  500. * @return string The CREATE TABLE command, w/out newlines
  501. */
  502. function _getCreateTable( $tableAbstract, $tableName )
  503. {
  504. $db = $this->_getDB();
  505. $sql = "SHOW CREATE TABLE `$tableAbstract`";
  506. $db->setQuery( $sql );
  507. $db->query();
  508. $temp = $db->loadAssocList();
  509. $tablesql = $temp[0]['Create Table'];
  510. unset( $temp );
  511. // Replace table prefix with the #__ placeholder, but do not replace it if this
  512. // is a database only backup
  513. if(!$this->_DBOnly)
  514. {
  515. // $tablesql = str_replace( CJVAbstract::getDBPrefix() , "#__", $tablesql );
  516. // This replacing algorithm takes into account empty prefixes.
  517. $tablesql = str_replace( $tableName , $tableAbstract, $tablesql );
  518. }
  519. // Replace newlines with spaces
  520. $tablesql = str_replace( "\n", " ", $tablesql ) . ";\n";
  521. if( $this->_DBOnly )
  522. {
  523. $drop = "DROP TABLE IF EXISTS `$tableName`;\n";
  524. $tablesql = $drop . $tablesql;
  525. }
  526. return $tablesql;
  527. }
  528. /**
  529. * Writes the SQL dump into the output files. If it fails, it sets the $_isError and
  530. * $_errorMessage variables of the class to signal an error condition
  531. *
  532. * @param string $outCreate Any CREATE TABLE / DROP TABLE commands
  533. * @param string $outData Any INSERT INTO commands
  534. * @param string $tableAbstract The current table's abstract name
  535. * @return boolean TRUE on successful write, FALSE otherwise
  536. */
  537. function _writeDump( &$outCreate, &$outData, $tableAbstract )
  538. {
  539. // FIXME: Support for Core Tables has been dropped due to adoption of JPI
  540. // $isCore = $this->_isJoomla ? in_array( $tableAbstract, $this->_CORETABLES ) : true;
  541. $isCore = false;
  542. $result = ($outCreate != '') ? $this->_writeline( $this->_filenameCore, $outCreate ) : true;
  543. if( !$result )
  544. {
  545. CJPLogger::WriteLog( _JP_LOG_ERROR, 'Writing to ' . $this->_filenameCore . ' has failed. Check permissions!' );
  546. $this->_isError= true;
  547. $this->_errorMessage = 'Writing to ' . $this->_filenameCore . ' has failed. Check permissions!';
  548. return false;
  549. }
  550. $fname = $isCore ? $this->_filenameCore : $this->_filenameSample;
  551. $result = ($outData != '') ? $this->_writeline( $fname, $outData ) : true;
  552. if( !$result )
  553. {
  554. CJPLogger::WriteLog( _JP_LOG_ERROR, 'Writing to ' . $this->_filenameCore . ' has failed. Check permissions!' );
  555. $this->_isError= true;
  556. $this->_errorMessage = 'Writing to ' . $this->_filenameCore . ' has failed. Check permissions!';
  557. return false;
  558. }
  559. return true;
  560. }
  561. /**
  562. * Saves the string in $fileData to the file $backupfile. Returns TRUE. If saving
  563. * failed, return value is FALSE.
  564. * @param string $backupfile Name of backup file
  565. * @param string $fileData Data to write
  566. * @return boolean TRUE is saving to the file succeeded
  567. */
  568. function _writeline($backupfile, $fileData) {
  569. if(empty($fileData)) return true; // Catch null data case
  570. if ($zp = fopen($backupfile, 'a')) {
  571. fwrite($zp, $fileData);
  572. fclose($zp);
  573. return true;
  574. } else {
  575. return false;
  576. }
  577. }
  578. /**
  579. * Return an instance of JDatabase
  580. *
  581. * @return JDatabase
  582. */
  583. function _getDB()
  584. {
  585. if( $this->_isJoomla )
  586. {
  587. CJPLogger::WriteLog(_JP_LOG_DEBUG, "CDBDump :: Core joomla database");
  588. // Core Joomla! database, get the existing instance
  589. $db = CJVAbstract::getDatabase();
  590. return $db;
  591. } else {
  592. // We have to connect ourselves
  593. if( defined('_JEXEC') )
  594. {
  595. // Joomla! 1.5.x
  596. jimport('joomla.database.database');
  597. jimport( 'joomla.database.table' );
  598. $conf =& JFactory::getConfig();
  599. $host = $this->_host . ($this->_port != '' ? ':' . $this->_port : '');
  600. $user = $this->_username;
  601. $password = $this->_password;
  602. $database = $this->_database;
  603. $prefix = '';
  604. $driver = $conf->getValue('config.dbtype');
  605. $debug = $conf->getValue('config.debug');
  606. $options = array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );
  607. $db =& JDatabase::getInstance( $options );
  608. if ( JError::isError($db) ) {
  609. CJPLogger::WriteLog(_JP_LOG_ERROR, "CDBDump :: Database Error:" . $db->toString());
  610. $this->_errorMessage = "CDBDump :: Database Error:" . $db->toString();
  611. $this->_isError = true;
  612. return false;
  613. }
  614. if ($db->getErrorNum() > 0) {
  615. CJPLogger::WriteLog(_JP_LOG_ERROR, 'JDatabase::getInstance: Could not connect to database <br/>' . 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg() );
  616. $this->_errorMessage = 'JDatabase::getInstance: Could not connect to database <br/>' . 'joomla.library:'.$db->getErrorNum().' - '.$db->getErrorMsg();
  617. $this->_isError = true;
  618. return false;
  619. }
  620. $db->debug( $debug );
  621. return $db;
  622. } else {
  623. // Joomla! 1.0.x
  624. $host = $this->_host . ($this->_port != '' ? ':' . $this->_port : '');
  625. $user = $this->_username;
  626. $password = $this->_password;
  627. $database = $this->_database;
  628. $prefix = '';
  629. $db &= new database($host, $user, $password, $database, $prefix, false);
  630. // TODO: I should really check if we conencted to the database...
  631. return $db;
  632. }
  633. }
  634. }
  635. }
  636. ?>