public function create($filename = false) { if (is_array($filename)) { $filename = false; } ignore_user_abort(true); $start = microtime_float(); if (defined('MW_CRON_EXEC')) { } else { only_admin_access(); } $table = '*'; if ($table == '*') { $extname = 'all'; } else { $extname = str_replace(',', '_', $table); $extname = str_replace(' ', '_', $extname); } $here = $this->get_bakup_location(); if (!is_dir($here)) { if (!mkdir_recursive($here)) { $back_log_action = 'Error the dir is not writable: ' . $here; $this->log_action($back_log_action); } } ini_set('memory_limit', '512M'); set_time_limit(0); $index1 = $here . 'index.php'; if ($filename == false) { $engine = mw()->database_manager->get_sql_engine(); $mwv = MW_VERSION; $mwv = str_replace('.', '', $mwv); $filename_to_return = 'database_' . date('YMdHis') . '_' . uniqid() . '_' . $mwv . '_' . $engine . '.sql'; } else { $filename_to_return = $filename; } $filess = $here . $filename_to_return; if (is_file($filess)) { return false; } touch($filess); touch($index1); $sql_bak_file = $filess; $hta = $here . '.htaccess'; if (!is_file($hta)) { touch($hta); file_put_contents($hta, 'Deny from all'); } $head = '/* Microweber database backup exported on: ' . date('l jS \\of F Y h:i:s A') . " */ \n"; $head .= '/* get_table_prefix(): ' . get_table_prefix() . " */ \n\n\n"; file_put_contents($sql_bak_file, $head); $return = ''; $tables = '*'; // Get all of the tables if ($tables == '*') { $tables = array(); $result = mw()->database_manager->get_tables_list(); if (!empty($result)) { foreach ($result as $item) { $tables[] = $item; } } } else { if (is_array($tables)) { $tables = explode(',', $tables); } } $back_log_action = 'Starting database backup'; $this->log_action($back_log_action); // Cycle through each provided table foreach ($tables as $table) { $is_cms_table = false; if (get_table_prefix() == '') { $is_cms_table = 1; } elseif (stristr($table, get_table_prefix())) { $is_cms_table = 1; } if (stristr($table, 'sessions')) { $is_cms_table = false; } if ($table != false and $is_cms_table) { $back_log_action = "Backing up database table {$table}"; $this->log_action($back_log_action); $qs = 'SELECT * FROM ' . $table; $result = mw()->database_manager->query($qs, $cache_id = false, $cache_group = false, $only_query = false); $num_fields = count($result[0]); $table_without_prefix = $this->prefix_placeholder . str_ireplace(get_table_prefix(), '', $table); // $return = 'DROP TABLE IF EXISTS ' . $table_without_prefix . $this->file_q_sep . "\n\n\n"; // $this->append_string_to_file($sql_bak_file, $return); $ddl = mw()->database_manager->get_table_ddl($table); $ddl = str_ireplace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ', $ddl); $create_table_without_prefix = str_ireplace(get_table_prefix(), $this->prefix_placeholder, $ddl); $return = "\n\n" . $create_table_without_prefix . $this->file_q_sep . "\n\n\n"; $this->append_string_to_file($sql_bak_file, $return); $this->log_action(false); if (!empty($result)) { $table_accos = str_replace(get_table_prefix(), '', $table); $columns = $this->app->database_manager->get_fields($table_accos); foreach ($result as $row) { $row = array_values($row); $columns = array_values($columns); $columns_q = false; $columns_temp = array(); foreach ($columns as $column) { $columns_temp[] = $column; } if (!empty($columns_temp)) { $columns_q = implode(',', $columns_temp); $columns_q = '(' . $columns_q . ')'; } $return = 'REPLACE INTO ' . $table_without_prefix . ' ' . $columns_q . ' VALUES('; for ($j = 0; $j < $num_fields; ++$j) { $row[$j] = str_replace("'", '’', $row[$j]); if (isset($row[$j])) { $return .= "'" . $row[$j] . "'"; } else { $return .= "''"; } if ($j < $num_fields - 1) { $return .= ','; } } $return .= ')' . $this->file_q_sep . "\n\n\n"; $this->append_string_to_file($sql_bak_file, $return); } } $return = "\n\n\n"; $this->append_string_to_file($sql_bak_file, $return); } } $this->log_action(false); $back_log_action = 'Saving to file ' . basename($filess); $this->log_action($back_log_action); $end = microtime_float(); $end = round($end - $start, 3); $this->log_action(false); return array('success' => "Backup was created for {$end} sec! {$filename_to_return}", 'filename' => $filename_to_return, 'runtime' => $end, 'url' => dir2url($filess)); }
public function save_content($data, $delete_the_cache = true) { if (is_string($data)) { $data = parse_params($data); } $mw_global_content_memory = array(); $adm = $this->app->user_manager->is_admin(); $table = $this->tables['content']; $table_data = $this->tables['content_data']; $checks = mw_var('FORCE_SAVE_CONTENT'); $orig_data = $data; $stop = false; if ($stop == true) { return array('error' => 'You are not logged in as admin to save content!'); } $cats_modified = false; if (!empty($data)) { if (!isset($data['id'])) { $data['id'] = 0; } if ($data['id'] == 0 and !isset($data['is_active'])) { $data['is_active'] = 1; } $this->app->event_manager->trigger('content.before.save', $data); if (intval($data['id']) == 0) { if (isset($data['subtype']) and $data['subtype'] == 'post' and !isset($data['content_type'])) { $data['subtype'] = 'post'; $data['content_type'] = 'post'; } if (!isset($data['subtype'])) { $data['subtype'] = 'post'; } if (!isset($data['content_type'])) { $data['content_type'] = 'post'; } } } if (isset($data['content_url']) and !isset($data['url'])) { $data['url'] = $data['content_url']; } if (!isset($data['parent']) and isset($data['content_parent'])) { $data['parent'] = $data['content_parent']; } if (isset($data['is_active'])) { if ($data['is_active'] === 'y') { $data['is_active'] = 1; } elseif ($data['is_active'] === 'n') { $data['is_active'] = 0; } } $data_to_save = $data; if (!isset($data['title']) and isset($data['content_title'])) { $data['title'] = $data['content_title']; } if (isset($data['title'])) { if ($data['title'] == '<br>' or $data['title'] == '<br />' or $data['title'] == '<br/>') { $data['title'] = ''; } $data['title'] = htmlspecialchars($data['title'], ENT_QUOTES, "UTF-8"); $data_to_save['title'] = $data['title']; } if (!isset($data['url']) and intval($data['id']) != 0) { $q = $this->get_by_id($data_to_save['id']); $thetitle = $q['title']; $q = $q['url']; $theurl = $q; } else { if (isset($data['url'])) { $theurl = $data['url']; } else { $theurl = $data['title']; } $thetitle = $data['title']; } if (isset($data['id']) and intval($data['id']) == 0) { if (!isset($data['is_deleted']) or $data['is_deleted'] == '') { $data_to_save['is_deleted'] = 0; } else { $data_to_save['is_deleted'] = $data['is_deleted']; } if (!isset($data['title']) or $data['title'] == '') { $data['title'] = "New page"; if (isset($data['content_type']) and $data['content_type'] != 'page') { $data['title'] = "New " . $data['content_type']; if (isset($data['subtype']) and $data['subtype'] != 'page' and $data['subtype'] != 'post' and $data['subtype'] != 'static' and $data['subtype'] != 'dynamic') { $data['title'] = "New " . $data['subtype']; } } $data_to_save['title'] = $data['title']; } } if (isset($data['url']) == false or $data['url'] == '') { if (isset($data['title']) != false and intval($data['id']) == 0) { $data['url'] = $this->app->url_manager->slug($data['title']); if ($data['url'] == '') { $data['url'] = date("Y-M-d-His"); } } } $url_changed = false; if (isset($data['url']) != false and is_string($data['url'])) { $search_weird_chars = array('%E2%80%99', '%E2%80%99', '%E2%80%98', '%E2%80%9C', '%E2%80%9D'); $str = $data['url']; $good[] = 9; #tab $good[] = 10; #nl $good[] = 13; #cr for ($a = 32; $a < 127; $a++) { $good[] = $a; } $newstr = ''; $len = strlen($str); for ($b = 0; $b < $len + 1; $b++) { if (isset($str[$b]) and in_array(ord($str[$b]), $good)) { $newstr .= $str[$b]; } } $newstr = str_replace('--', '-', $newstr); $newstr = str_replace('--', '-', $newstr); if ($newstr == '-' or $newstr == '--') { $newstr = 'post-' . date('YmdHis'); } $data['url'] = $newstr; $url_changed = true; $data_to_save['url'] = $data['url']; } if (isset($data['category']) or isset($data['categories'])) { $cats_modified = true; } $table_cats = $this->tables['categories']; if (isset($data_to_save['title']) and $data_to_save['title'] != '' and (!isset($data['url']) or trim($data['url']) == '')) { $data['url'] = $this->app->url_manager->slug($data_to_save['title']); } if (isset($data['url']) and $data['url'] != false) { if (trim($data['url']) == '') { $data['url'] = $this->app->url_manager->slug($data['title']); } $data['url'] = $this->app->database_manager->escape_string($data['url']); $date123 = date("YmdHis"); $get = array(); $get['url'] = $data['url']; $get['single'] = true; $q = $this->get($get); if (!empty($q)) { if ($data['id'] != $q['id']) { $data['url'] = $data['url'] . '-' . $date123; $data_to_save['url'] = $data['url']; } } if (isset($data_to_save['url']) and strval($data_to_save['url']) == '' and isset($data_to_save['quick_save']) == false) { $data_to_save['url'] = $data_to_save['url'] . '-' . $date123; } if (isset($data_to_save['title']) and strval($data_to_save['title']) == '' and isset($data_to_save['quick_save']) == false) { $data_to_save['title'] = 'post-' . $date123; } if (isset($data_to_save['url']) and strval($data_to_save['url']) == '' and isset($data_to_save['quick_save']) == false) { $data_to_save['url'] = strtolower(reduce_double_slashes($data['url'])); } } if (isset($data_to_save['url']) and is_string($data_to_save['url'])) { if ($data_to_save['url'] == '') { $data_to_save['url'] = date("Y-M-d-His"); } $data_to_save['url'] = str_replace(site_url(), '', $data_to_save['url']); } if (isset($data['created_at'])) { $data_to_save['created_at'] = $data['created_at']; } if (isset($data['updated_at'])) { $data_to_save['updated_at'] = $data['updated_at']; } $data_to_save_options = array(); if (isset($data_to_save['is_home']) and $data_to_save['is_home'] == 'y') { $data_to_save['is_home'] = 1; } elseif (isset($data_to_save['is_home']) and $data_to_save['is_home'] == 'n') { $data_to_save['is_home'] = 0; } if (isset($data_to_save['is_shop']) and $data_to_save['is_shop'] === 'y') { $data_to_save['is_shop'] = 1; } elseif (isset($data_to_save['is_shop']) and $data_to_save['is_shop'] === 'n') { $data_to_save['is_shop'] = 0; } if (isset($data_to_save['require_login']) and $data_to_save['require_login'] === 'y') { $data_to_save['require_login'] = 1; } elseif (isset($data_to_save['require_login']) and $data_to_save['require_login'] === 'n') { $data_to_save['require_login'] = 0; } if (isset($data_to_save['is_home']) and $data_to_save['is_home'] == 1) { if ($adm == true) { $q = Content::where('is_home', 1)->update(array('is_home' => 0)); } else { $data_to_save['is_home'] = 0; } } if (isset($data_to_save['content_type']) and strval($data_to_save['content_type']) == 'post') { if (isset($data_to_save['subtype']) and strval($data_to_save['subtype']) == 'static') { $data_to_save['subtype'] = 'post'; } else { if (isset($data_to_save['subtype']) and strval($data_to_save['subtype']) == 'dynamic') { $data_to_save['subtype'] = 'post'; } } } if (isset($data_to_save['subtype']) and strval($data_to_save['subtype']) == 'dynamic') { $check_ex = false; if (isset($data_to_save['subtype_value']) and trim($data_to_save['subtype_value']) != '' and intval($data_to_save['subtype_value']) > 0) { $check_ex = $this->app->category_manager->get_by_id(intval($data_to_save['subtype_value'])); } if ($check_ex == false) { if (isset($data_to_save['id']) and intval(trim($data_to_save['id'])) > 0) { $test2 = $this->app->category_manager->get('data_type=category&rel_type=content&rel_id=' . intval($data_to_save['id'])); if (isset($test2[0])) { $check_ex = $test2[0]; $data_to_save['subtype_value'] = $test2[0]['id']; } } unset($data_to_save['subtype_value']); } } $par_page = false; if (isset($data_to_save['content_type']) and strval($data_to_save['content_type']) == 'post') { if (isset($data_to_save['parent']) and intval($data_to_save['parent']) > 0) { $par_page = $this->get_by_id($data_to_save['parent']); } if (is_array($par_page)) { $change_to_dynamic = true; if (isset($data_to_save['is_home']) and $data_to_save['is_home'] == 1) { $change_to_dynamic = false; } if ($change_to_dynamic == true and $par_page['subtype'] == 'static') { $par_page_new = array(); $par_page_new['id'] = $par_page['id']; $par_page_new['subtype'] = 'dynamic'; $par_page_new = $this->app->database_manager->save($table, $par_page_new); $cats_modified = true; } if (!isset($data_to_save['categories'])) { $data_to_save['categories'] = ''; } if (is_string($data_to_save['categories']) and isset($par_page['subtype_value']) and intval($par_page['subtype_value']) != 0) { $data_to_save['categories'] = $data_to_save['categories'] . ', ' . intval($par_page['subtype_value']); } } $c1 = false; if (isset($data_to_save['category']) and !isset($data_to_save['categories'])) { $data_to_save['categories'] = $data_to_save['category']; } if (isset($data_to_save['categories']) and $par_page == false) { if (is_string($data_to_save['categories'])) { $c1 = explode(',', $data_to_save['categories']); if (is_array($c1)) { foreach ($c1 as $item) { $item = intval($item); if ($item > 0) { $cont_cat = $this->get('limit=1&content_type=page&subtype_value=' . $item); if (isset($cont_cat[0]) and is_array($cont_cat[0])) { $cont_cat = $cont_cat[0]; if (isset($cont_cat["subtype_value"]) and intval($cont_cat["subtype_value"]) > 0) { $data_to_save['parent'] = $cont_cat["id"]; break; } } } } } } } } if (isset($data_to_save['content'])) { if (trim($data_to_save['content']) == '' or $data_to_save['content'] == false) { $data_to_save['content'] = null; } else { if (isset($data['download_remote_images']) and $data['download_remote_images'] != false and $adm == true) { $site_url = $this->app->url_manager->site(); $images = mw()->parser->query($data_to_save['content'], 'img'); $to_download = array(); $to_replace = array(); $possible_sources = array(); if (isset($data['insert_content_image']) and $data['insert_content_image'] != false and isset($data['content'])) { $data['content'] = "<img src='{$data['insert_content_image']}' /> " . $data['content']; } if (!empty($images)) { foreach ($images as $image) { $srcs = array(); preg_match('/src="([^"]*)"/i', $image, $srcs); if (!empty($srcs) and isset($srcs[1]) and $srcs[1] != false) { $possible_sources[] = $srcs[1]; } } } if (!empty($possible_sources)) { foreach ($possible_sources as $image_src) { if (!stristr($image_src, $site_url)) { $to_replace[] = $image_src; $image_src = strtok($image_src, '?'); $ext = get_file_extension($image_src); switch (strtolower($ext)) { case 'jpg': case 'jpeg': case 'png': case 'gif': case 'svg': $to_download[] = $image_src; break; default: break; } } } } if (!empty($to_download)) { $to_download = array_unique($to_download); if (!empty($to_download)) { foreach ($to_download as $src) { $dl_dir = media_base_path() . 'downloaded' . DS; if (!is_dir($dl_dir)) { mkdir_recursive($dl_dir); } $dl_file = $dl_dir . md5($src) . basename($src); if (!is_file($dl_file)) { $is_dl = $this->app->url_manager->download($src, false, $dl_file); } if (is_file($dl_file)) { $url_local = dir2url($dl_file); $data_to_save['content'] = str_ireplace($src, $url_local, $data_to_save['content']); } } } } } $data_to_save['content'] = mw()->parser->make_tags($data_to_save['content']); } } if (!isset($data_to_save['updated_at'])) { $data_to_save['updated_at'] = date("Y-m-d H:i:s"); } if (isset($data_to_save['id']) and intval($data_to_save['id']) == 0 or !isset($data_to_save['id'])) { if (!isset($data_to_save['position']) or intval($data_to_save['position']) == 0) { $pos_params = array(); $pos_params['table'] = 'content'; if (isset($data_to_save['content_type']) and strval($data_to_save['content_type']) == 'page') { $pos_params['content_type'] = $data_to_save['content_type']; $pos_params['min'] = 'position'; } else { $pos_params['max'] = 'position'; } $get_max_pos = mw()->database_manager->get($pos_params); if (is_null($get_max_pos)) { $data_to_save['position'] = 1; } else { if (is_int($get_max_pos) or is_string($get_max_pos)) { if (isset($data_to_save['content_type']) and strval($data_to_save['content_type']) == 'page') { $data_to_save['position'] = intval($get_max_pos) - 1; } else { $data_to_save['position'] = intval($get_max_pos) + 1; } } } } $data_to_save['posted_at'] = $data_to_save['updated_at']; } $cats_modified = true; if (isset($data_to_save['url']) and $data_to_save['url'] == $this->app->url_manager->site()) { unset($data_to_save['url']); } $data_to_save['allow_html'] = true; $this->no_cache = true; //clean some fields if (isset($data_to_save['custom_field_type']) and isset($data_to_save['value'])) { unset($data_to_save['custom_field_type']); unset($data_to_save['value']); } if (isset($data_to_save['custom_field_help_text'])) { unset($data_to_save['custom_field_help_text']); unset($data_to_save['custom_field_help_text']); } if (isset($data_to_save['custom_field_is_active'])) { unset($data_to_save['custom_field_is_active']); } if (isset($data_to_save['name'])) { unset($data_to_save['name']); } if (isset($data_to_save['values'])) { unset($data_to_save['values']); } if (isset($data_to_save['value'])) { unset($data_to_save['value']); } if (isset($data_to_save['title'])) { $url_changed = true; } $data_to_save['table'] = $table; $data_fields = array(); if (!empty($orig_data)) { $data_str = 'data_'; $data_str_l = strlen($data_str); foreach ($orig_data as $k => $v) { if (is_string($k)) { if (strlen($k) > $data_str_l) { $rest = substr($k, 0, $data_str_l); $left = substr($k, $data_str_l, strlen($k)); if ($rest == $data_str) { if (!isset($data_to_save['data_fields'])) { $data_to_save['data_fields'] = array(); } $data_to_save['data_fields'][$left] = $v; } } } } } if (isset($data_to_save['parent']) and $data_to_save['parent'] != 0) { if (isset($data_to_save['id']) and $data_to_save['id'] != 0) { if ($data_to_save['parent'] == $data_to_save['id']) { $data_to_save['parent'] = 0; } } } $save = $this->app->database->extended_save($table, $data_to_save); $id = $save; if (isset($data_to_save['parent']) and $data_to_save['parent'] != 0) { $upd_posted = array(); $upd_posted['posted_at'] = $data_to_save['updated_at']; $upd_posted['id'] = $data_to_save['parent']; $save_posted = $this->app->database_manager->save($table, $upd_posted); } $after_save = $data_to_save; $after_save['id'] = $id; $this->app->event_manager->trigger('content.after.save', $after_save); $this->app->cache_manager->delete('content/' . $save); $this->app->cache_manager->delete('content_fields/global'); if ($url_changed != false) { $this->app->cache_manager->delete('menus'); $this->app->cache_manager->delete('categories'); } if (!isset($data_to_save['images']) and isset($data_to_save['pictures'])) { $data_to_save['images'] = $data_to_save['pictures']; } if (isset($data_to_save['images']) and is_string($data_to_save['images'])) { $data_to_save['images'] = explode(',', $data_to_save['images']); } if (isset($data_to_save['images']) and is_array($data_to_save['images']) and !empty($data_to_save['images'])) { $images_to_save = $data_to_save['images']; foreach ($images_to_save as $image_to_save) { if (is_string($image_to_save)) { $image_to_save = trim($image_to_save); if ($image_to_save != '') { $save_media = array(); $save_media['content_id'] = $id; $save_media['filename'] = $image_to_save; $check = $this->app->media_manager->get($save_media); $save_media['media_type'] = 'picture'; if ($check == false) { $this->app->media_manager->save($save_media); } } } elseif (is_array($image_to_save) and !empty($image_to_save)) { $save_media = $image_to_save; $save_media['content_id'] = $id; $this->app->media_manager->save($save_media); } } } if (isset($data_to_save['add_content_to_menu']) and is_array($data_to_save['add_content_to_menu'])) { foreach ($data_to_save['add_content_to_menu'] as $menu_id) { $ids_to_save = $save; $this->add_content_to_menu($ids_to_save, $menu_id); } } $custom_field_table = $this->tables['custom_fields']; $custom_field_table = mw()->database_manager->real_table_name($custom_field_table); $sid = mw()->user_manager->session_id(); $media_table = $this->tables['media']; $media_table = mw()->database_manager->real_table_name($media_table); if ($sid != false and $sid != '' and $id != false) { $clean = " UPDATE {$custom_field_table} SET\n rel_type =\"content\" ,\n rel_id =\"{$id}\"\n WHERE\n\n (rel_id=0 OR rel_id IS NULL OR rel_id =\"0\")\n AND rel_type =\"content\"\n\t "; $this->app->database_manager->q($clean); $clean = " UPDATE {$media_table} SET\n rel_id =\"{$id}\"\n WHERE\n session_id =\"{$sid}\"\n AND rel_type =\"content\" AND (rel_id=0 OR rel_id IS NULL)\n "; $this->app->database_manager->q($clean); } $this->app->cache_manager->delete('custom_fields'); $this->app->cache_manager->delete('custom_fields_values'); $this->app->cache_manager->delete('media/global'); if (isset($data_to_save['parent']) and intval($data_to_save['parent']) != 0) { $this->app->cache_manager->delete('content' . DIRECTORY_SEPARATOR . intval($data_to_save['parent'])); } if (isset($data_to_save['id']) and intval($data_to_save['id']) != 0) { $this->app->cache_manager->delete('content' . DIRECTORY_SEPARATOR . intval($data_to_save['id'])); } $this->app->cache_manager->delete('content' . DIRECTORY_SEPARATOR . 'global'); $this->app->cache_manager->delete('content' . DIRECTORY_SEPARATOR . '0'); $this->app->cache_manager->delete('content_fields/global'); $this->app->cache_manager->delete('content'); $this->app->cache_manager->delete('categories/global'); $this->app->cache_manager->delete('categories_items/global'); if ($cats_modified != false) { if (isset($c1) and is_array($c1)) { foreach ($c1 as $item) { $item = intval($item); if ($item > 0) { $this->app->cache_manager->delete('categories/' . $item); } } } } event_trigger('mw_save_content', $save); return $id; }
public function download_remote_images_from_text($text) { $site_url = $this->app->url_manager->site(); $images = $this->app->parser->query($text, 'img'); $to_download = array(); $to_replace = array(); $possible_sources = array(); if (!empty($images)) { foreach ($images as $image) { $srcs = array(); preg_match('/src="([^"]*)"/i', $image, $srcs); if (!empty($srcs) and isset($srcs[1]) and $srcs[1] != false) { $possible_sources[] = $srcs[1]; } } } if (!empty($possible_sources)) { foreach ($possible_sources as $image_src) { if (!stristr($image_src, $site_url)) { $to_replace[] = $image_src; $image_src = strtok($image_src, '?'); $ext = get_file_extension($image_src); switch (strtolower($ext)) { case 'jpg': case 'jpeg': case 'png': case 'gif': case 'svg': $to_download[] = $image_src; break; default: break; } } } } if (!empty($to_download)) { $to_download = array_unique($to_download); if (!empty($to_download)) { foreach ($to_download as $src) { $dl_dir = media_base_path() . 'downloaded' . DS; if (!is_dir($dl_dir)) { mkdir_recursive($dl_dir); } $dl_file = $dl_dir . md5($src) . basename($src); if (!is_file($dl_file)) { $is_dl = $this->app->url_manager->download($src, false, $dl_file); } if (is_file($dl_file)) { $url_local = dir2url($dl_file); $text = str_ireplace($src, $url_local, $text); } } } } return $text; }