public static function delete($id) { global $lC_Database; $lC_CategoryTree = new lC_CategoryTree_Admin(); if (is_numeric($id)) { $lC_CategoryTree->setBreadcrumbUsage(false); $categories = array_merge(array(array('id' => $id, 'text' => '')), $lC_CategoryTree->getArray($id)); $products = array(); $products_delete = array(); foreach ($categories as $category) { $Qproducts = $lC_Database->query('select products_id from :table_products_to_categories where categories_id = :categories_id'); $Qproducts->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES); $Qproducts->bindInt(':categories_id', $category['id']); $Qproducts->execute(); while ($Qproducts->next()) { $products[$Qproducts->valueInt('products_id')]['categories'][] = $category['id']; } } foreach ($products as $key => $value) { $Qcheck = $lC_Database->query('select categories_id from :table_products_to_categories where products_id = :products_id and categories_id not in :categories_id limit 1'); $Qcheck->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES); $Qcheck->bindInt(':products_id', $key); $Qcheck->bindRaw(':categories_id', '("' . implode('", "', $value['categories']) . '")'); $Qcheck->execute(); if ($Qcheck->numberOfRows() === 0) { $products_delete[$key] = $key; } } lc_set_time_limit(0); foreach ($categories as $category) { $lC_Database->startTransaction(); $Qimage = $lC_Database->query('select categories_image from :table_categories where categories_id = :categories_id'); $Qimage->bindTable(':table_categories', TABLE_CATEGORIES); $Qimage->bindInt(':categories_id', $category['id']); $Qimage->execute(); $Qc = $lC_Database->query('delete from :table_categories where categories_id = :categories_id'); $Qc->bindTable(':table_categories', TABLE_CATEGORIES); $Qc->bindInt(':categories_id', $category['id']); $Qc->setLogging($_SESSION['module'], $id); $Qc->execute(); if (!$lC_Database->isError()) { $Qcd = $lC_Database->query('delete from :table_categories_description where categories_id = :categories_id'); $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION); $Qcd->bindInt(':categories_id', $category['id']); $Qcd->setLogging($_SESSION['module'], $id); $Qcd->execute(); if (!$lC_Database->isError()) { $Qp2c = $lC_Database->query('delete from :table_products_to_categories where categories_id = :categories_id'); $Qp2c->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES); $Qp2c->bindInt(':categories_id', $category['id']); $Qp2c->setLogging($_SESSION['module'], $id); $Qp2c->execute(); if (!$lC_Database->isError()) { // permalink $Qpb = $lC_Database->query('delete from :table_permalinks where item_id = :item_id'); $Qpb->bindTable(':table_permalinks', TABLE_PERMALINKS); $Qpb->bindInt(':item_id', $category['id']); $Qpb->execute(); if (!$lC_Database->isError()) { $lC_Database->commitTransaction(); lC_Cache::clear('categories'); lC_Cache::clear('category_tree'); lC_Cache::clear('also_purchased'); if (!lc_empty($Qimage->value('categories_image'))) { $Qcheck = $lC_Database->query('select count(*) as total from :table_categories where categories_image = :categories_image'); $Qcheck->bindTable(':table_categories', TABLE_CATEGORIES); $Qcheck->bindValue(':categories_image', $Qimage->value('categories_image')); $Qcheck->execute(); if ($Qcheck->numberOfRows() === 0) { if (file_exists(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')))) { @unlink(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image'))); } } } } else { $lC_Database->rollbackTransaction(); } } else { $lC_Database->rollbackTransaction(); } } else { $lC_Database->rollbackTransaction(); } } else { $lC_Database->rollbackTransaction(); } } foreach ($products_delete as $id) { lC_Products_Admin::remove($id); } lC_Cache::clear('categories'); lC_Cache::clear('category_tree'); lC_Cache::clear('also_purchased'); return true; } return false; }
public static function restore($filename = false) { global $lC_Database, $lC_Session; lc_set_time_limit(0); if ($filename !== false) { if (file_exists(DIR_FS_BACKUP . $filename)) { $restore_file = DIR_FS_BACKUP . $filename; $extension = substr($filename, -3); if ($extension == 'sql' || $extension == '.gz' || $extension == 'zip') { switch ($extension) { case 'sql': $restore_from = $restore_file; $remove_raw = false; break; case '.gz': $restore_from = substr($restore_file, 0, -3); exec(CFG_APP_GUNZIP . ' ' . $restore_file . ' -c > ' . $restore_from); $remove_raw = true; break; case 'zip': $restore_from = substr($restore_file, 0, -4); exec(CFG_APP_UNZIP . ' ' . $restore_file . ' -d ' . DIR_FS_BACKUP); $remove_raw = true; break; } if (isset($restore_from) && file_exists($restore_from)) { $fd = fopen($restore_from, 'rb'); $restore_query = fread($fd, filesize($restore_from)); fclose($fd); } } } } else { $sql_file = new upload('sql_file'); if ($sql_file->parse()) { $extension = substr($sql_file->filename, -3); switch ($extension) { case 'sql': $restore_from = $sql_file->tmp_filename; $remove_raw = false; break; case '.gz': $restore_from = substr($sql_file->tmp_filename, 0, -3); exec(CFG_APP_GUNZIP . ' ' . $sql_file->tmp_filename . ' -c > ' . $restore_from); $remove_raw = true; break; case 'zip': $restore_from = DIR_FS_WORK . substr($sql_file->filename, 0, -4); exec(CFG_APP_UNZIP . ' ' . $sql_file->tmp_filename . ' -d ' . DIR_FS_WORK); $remove_raw = true; break; } $restore_query = fread(fopen($restore_from, 'rb'), filesize($restore_from)); $filename = $restore_from; } } if (isset($restore_query) && !empty($restore_query)) { $sql_array = array(); $sql_length = strlen($restore_query); $pos = strpos($restore_query, ';'); // loop and remove comments for ($i = $pos; $i < $sql_length; $i++) { if ($restore_query[0] == '#') { $restore_query = ltrim(substr($restore_query, strpos($restore_query, "\n"))); $sql_length = strlen($restore_query); $i = strpos($restore_query, ';') - 1; continue; } if ($restore_query[$i + 1] == "\n") { for ($j = $i + 2; $j < $sql_length; $j++) { if (trim($restore_query[$j]) != '') { $next = substr($restore_query, $j, 6); if ($next[0] == '#') { // find out where the break position is so we can remove this line (#comment line) for ($k = $j; $k < $sql_length; $k++) { if ($restore_query[$k] == "\n") { break; } } $query = substr($restore_query, 0, $i + 1); $restore_query = substr($restore_query, $k); // join the query before the comment appeared, with the rest of the dump $restore_query = $query . $restore_query; $sql_length = strlen($restore_query); $i = strpos($restore_query, ';') - 1; continue 2; } break; } } if ($next == '') { // get the last insert query $next = 'insert'; } if (stristr($next, 'create') || stristr($next, 'insert') || stristr($next, 'drop t')) { $next = ''; $sql_array[] = substr($restore_query, 0, $i); $restore_query = ltrim(substr($restore_query, $i + 1)); $sql_length = strlen($restore_query); $i = strpos($restore_query, ';') - 1; } } } for ($i = 0, $n = sizeof($sql_array); $i < $n; $i++) { $lC_Database->simpleQuery($sql_array[$i]); } // $lC_Session->close(); // empty the sessions table // $Qsessions = $lC_Database->query('delete from :table_sessions'); // $Qsessions->bindTable(':table_sessions', TABLE_SESSIONS); // $Qsessions->execute(); // empty the who's online table $Qwho = $lC_Database->query('delete from :table_whos_online'); $Qwho->bindTable(':table_whos_online', TABLE_WHOS_ONLINE); $Qwho->execute(); $Qcfg = $lC_Database->query('delete from :table_configuration where configuration_key = :configuration_key'); $Qcfg->bindTable(':table_configuration', TABLE_CONFIGURATION); $Qcfg->bindValue(':configuration_key', 'DB_LAST_RESTORE'); $Qcfg->execute(); $Qcfg = $lC_Database->query('insert into :table_configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ("Last Database Restore", "DB_LAST_RESTORE", :filename, "Last database restore file", 6, 0, now())'); $Qcfg->bindTable(':table_configuration', TABLE_CONFIGURATION); $Qcfg->bindValue(':filename', $filename); $Qcfg->execute(); lC_Cache::clear('configuration'); if (isset($remove_raw) && $remove_raw === true) { unlink($restore_from); } //lc_redirect_admin(lc_href_link_admin(FILENAME_DEFAULT, 'login')); return true; } return false; }