示例#1
0
 public function exec_restore($params = false)
 {
     ignore_user_abort(true);
     ini_set('memory_limit', '512M');
     if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'set_time_limit')) {
         set_time_limit(0);
     }
     $loc = $this->backup_file;
     // Get the provided arg
     if (isset($params['id'])) {
         $id = $params['id'];
     } elseif (isset($_GET['filename'])) {
         $id = $params['filename'];
     } elseif (isset($_GET['file'])) {
         $id = $params['file'];
     } elseif ($loc != false) {
         $id = $loc;
     }
     if ($id == null) {
         return array('error' => 'You have not provided a backup to restore.');
     }
     $here = $this->get_bakup_location();
     $filename = $here . $id;
     $ext = get_file_extension($filename);
     $ext_error = false;
     $sql_file = false;
     if (!is_file($filename)) {
         return array('error' => 'You have not provided a existing backup to restore.');
         die;
     }
     $temp_dir_restore = false;
     switch ($ext) {
         case 'zip':
             $back_log_action = 'Unzipping userfiles';
             $this->log_action($back_log_action);
             $exract_folder = md5(basename($filename));
             $unzip = new \Microweber\Utils\Unzip();
             $target_dir = mw_cache_path() . 'backup_restore' . DS . $exract_folder . DS;
             if (!is_dir($target_dir)) {
                 mkdir_recursive($target_dir);
             }
             $result = $unzip->extract($filename, $target_dir, $preserve_filepath = true);
             $temp_dir_restore = $target_dir;
             $sql_restore = $target_dir . 'mw_sql_restore.sql';
             if (is_file($sql_restore)) {
                 $sql_file = $sql_restore;
             }
             break;
         case 'sql':
             $sql_file = $filename;
             break;
         default:
             $ext_error = true;
             break;
     }
     if ($ext_error == true) {
         return array('error' => 'Invalid file extension. The restore file must be .sql or .zip');
         die;
     }
     if ($sql_file != false) {
         $back_log_action = 'Restoring database';
         $this->log_action($back_log_action);
         $filename = $sql_file;
         $sqlErrorText = '';
         $sqlErrorCode = 0;
         $sqlStmt = '';
         $sqlFile = file_get_contents($filename);
         $sqlArray = explode($this->file_q_sep, $sqlFile);
         if (!isset($sqlArray[1])) {
             $sqlArray = explode("\n", $sqlFile);
         }
         // Process the sql file by statements
         $engine = mw()->database_manager->get_sql_engine();
         foreach ($sqlArray as $stmt) {
             $stmt = str_replace('/* MW_TABLE_SEP */', ' ', $stmt);
             $stmt = str_ireplace($this->prefix_placeholder, get_table_prefix(), $stmt);
             $stmt = str_replace("", '', $stmt);
             //                $stmt = str_replace("\x0D", '', $stmt);
             //                $stmt = str_replace("\x09", '', $stmt);
             ////
             //                $stmt = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', '', $stmt);
             if ($engine == 'sqlite') {
                 $stmt = str_replace("\\'", "''", $stmt);
             }
             if ($this->debug) {
                 d($stmt);
             }
             if (strlen(trim($stmt)) > 3) {
                 try {
                     @mw()->database_manager->q($stmt, true);
                     // mw()->database_manager->q($stmt);
                 } catch (QueryException $e) {
                     echo 'Caught exception: ' . $e->getMessage() . "\n";
                     $sqlErrorCode = 1;
                 } catch (Exception $e) {
                     echo 'Caught exception: ' . $e->getMessage() . "\n";
                     $sqlErrorCode = 1;
                 }
             }
         }
         // Print message (error or success)
         if ($sqlErrorCode == 0) {
             $back_log_action = 'Database restored!';
             $this->log_action($back_log_action);
             echo "Database restored!\n";
             echo 'Backup used: ' . $filename . "\n";
         } else {
             echo "An error occurred while restoring backup!<br><br>\n";
             echo "Error code: {$sqlErrorCode}<br>\n";
             echo "Error text: {$sqlErrorText}<br>\n";
             echo "Statement:<br/> {$sqlStmt}<br>";
         }
         $back_log_action = 'Database restored!';
         $this->log_action($back_log_action);
         echo "Files restored successfully!<br>\n";
         echo 'Backup used: ' . $filename . "<br>\n";
         if ($temp_dir_restore != false) {
             @unlink($filename);
         }
     }
     if (userfiles_path()) {
         if (!is_dir(userfiles_path())) {
             mkdir_recursive(userfiles_path());
         }
     }
     if (media_base_path()) {
         if (!is_dir(media_base_path())) {
             mkdir_recursive(media_base_path());
         }
     }
     if ($temp_dir_restore != false and is_dir($temp_dir_restore)) {
         echo "Media restored!<br>\n";
         $srcDir = $temp_dir_restore;
         $destDir = userfiles_path();
         $copy = $this->copyr($srcDir, $destDir);
     }
     if (function_exists('mw_post_update')) {
         mw_post_update();
     }
     $back_log_action = 'Cleaning up cache';
     $this->log_action($back_log_action);
     mw()->cache_manager->clear();
     $this->log_action(false);
 }
 public function sitemapxml()
 {
     $sm_file = mw_cache_path() . 'sitemap.xml';
     $skip = false;
     if (is_file($sm_file)) {
         $filelastmodified = filemtime($sm_file);
         if ($filelastmodified - time() > 3 * 3600) {
             $skip = 1;
         }
     }
     if ($skip == false) {
         $map = new \Microweber\Utils\Sitemap($sm_file);
         $map->file = mw_cache_path() . 'sitemap.xml';
         $cont = get_content('is_active=1&is_deleted=0&limit=2500&fields=id,updated_at&orderby=updated_at desc');
         if (!empty($cont)) {
             foreach ($cont as $item) {
                 $map->addPage($this->app->content_manager->link($item['id']), 'daily', 1, $item['updated_at']);
             }
         }
         $map = $map->create();
     }
     $map = $sm_file;
     $fp = fopen($map, 'r');
     // send the right headers
     header('Content-Type: text/xml');
     header('Content-Length: ' . filesize($map));
     // dump the file and stop the script
     fpassthru($fp);
     event_trigger('mw_robot_url_hit');
     exit;
 }
示例#3
0
 function composer_run()
 {
     $composer_cache = mw_cache_path() . 'composer' . DS;
     $vendor_cache = normalize_path($composer_cache . 'vendor', true);
     $composer_path = normalize_path(base_path() . '/', false);
     $composer_json = normalize_path(base_path() . DS . 'composer.json', false);
     $composer_json_cache = normalize_path($composer_cache . DS . 'composer.json', false);
     if (!is_dir($vendor_cache)) {
         mkdir_recursive($vendor_cache);
     }
     if (is_file($composer_json)) {
         copy($composer_json, $composer_json_cache);
     }
     putenv("COMPOSER_VENDOR_DIR=" . $vendor_cache);
     putenv("COMPOSER_NO_INTERACTION=1");
     $this->_log_msg('Composer update...');
     $runner = new \Microweber\Utils\ComposerUpdate($composer_cache);
     $config = array('prepend-autoloader' => false, 'no-install' => true, 'no-scripts' => true, 'no-plugins' => true, 'no-progress' => true, 'preferred-install' => 'dist', 'no-dev' => true, 'no-custom-installers' => true, 'no-autoloader' => true);
     $out = $runner->run($config);
     if ($out == 2) {
         return array('error' => 'Error resolving Composer dependencies');
     } elseif ($out == 1) {
         return array('error' => 'Composer has an unknown error');
     } elseif ($out === 0) {
         return array('move_vendor' => 1, 'working' => 1, 'message' => 'Composer has completed');
     } else {
         return array('success' => 1, 'message' => $out);
     }
 }
示例#4
0
 public function thumbnail_img($params)
 {
     extract($params);
     if (!isset($width)) {
         $width = 200;
     }
     if (!isset($height)) {
         $width = 200;
     }
     if (!isset($src) or $src == false) {
         return $this->pixum($width, $height);
     }
     $src = strtok($src, '?');
     $surl = $this->app->url_manager->site();
     $local = false;
     $media_url = media_base_url();
     $media_url = trim($media_url);
     $src = str_replace('{SITE_URL}', $surl, $src);
     $src = str_replace('%7BSITE_URL%7D', $surl, $src);
     $src = str_replace('..', '', $src);
     if (strstr($src, $surl) or strpos($src, $surl)) {
         $src = str_replace($surl . '/', $surl, $src);
         //$src = str_replace($media_url, '', $src);
         $src = str_replace($surl, '', $src);
         $src = ltrim($src, DS);
         $src = ltrim($src, '/');
         $src = rtrim($src, DS);
         $src = rtrim($src, '/');
         //$src = media_base_path() . $src;
         $src = MW_ROOTPATH . $src;
         $src = normalize_path($src, false);
     } else {
         $src1 = media_base_path() . $src;
         $src1 = normalize_path($src1, false);
         $src2 = MW_ROOTPATH . $src;
         $src2 = normalize_path($src2, false);
         if (is_file($src1)) {
             $src = $src1;
         } elseif (is_file($src2)) {
             $src = $src2;
         } else {
             $no_img = true;
             //                if (function_exists('getimagesize')) {
             //                    $im = @getimagesize($src);
             //                    if ($im) {
             //                        if (isset($im['mime'])) {
             //                            $save_ext = false;
             //                            switch ($im['mime']) {
             //                                case "image/jpeg":
             //                                case "image/jpg":
             //                                    $save_ext = 'jpg';
             //                                    break;
             //                                case "image/gif":
             //                                    $save_ext = 'gif';
             //                                    break;
             //                                case "image/png":
             //                                    $save_ext = 'png';
             //                                    break;
             //                                case "image/bmp":
             //                                    $save_ext = 'bmp';
             //                                    break;
             //                            }
             //                            if ($save_ext != false) {
             //                                $ext = $save_ext;
             //                                $no_img = false;
             //                            }
             //                        }
             //
             //                    }
             //                }
             if ($no_img) {
                 return $this->pixum_img();
             }
         }
     }
     $media_root = media_base_path();
     if (!is_writable($media_root)) {
         $media_root = mw_cache_path();
     }
     $cd = $this->thumbnails_path() . $width . DS;
     if (!is_dir($cd)) {
         mkdir_recursive($cd);
     }
     $index_file = $cd . 'index.html';
     if (!is_file($index_file)) {
         file_put_contents($index_file, 'Thumbnail directory is not allowed');
     }
     if (!isset($ext)) {
         $ext = strtolower(get_file_extension($src));
     }
     $cache = md5(serialize($params)) . '.' . $ext;
     $cache = str_replace(' ', '_', $cache);
     if (isset($cache_id)) {
         $cache = str_replace(' ', '_', $cache_id);
         $cache = str_replace('..', '', $cache);
     }
     $cache_path = $cd . $cache;
     if (file_exists($cache_path)) {
         if (!headers_sent()) {
             if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
                 $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
             } else {
                 $if_modified_since = '';
             }
             $mtime = filemtime($src);
             $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
             if ($if_modified_since == $gmdate_mod) {
                 header("HTTP/1.0 304 Not Modified");
             }
         }
     } else {
         if (file_exists($src)) {
             if ($ext == 'svg') {
                 $res1 = file_get_contents($src);
                 $res1 = $this->svgScaleHack($res1, $width, $height);
                 file_put_contents($cache_path, $res1);
             } else {
                 if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png' || $ext == 'bmp') {
                     $tn = new \Microweber\Utils\Thumbnailer($src);
                     $thumbOptions = array('maxLength' => $height, 'width' => $width);
                     $tn->createThumb($thumbOptions, $cache_path);
                     unset($tn);
                 } else {
                     return $this->pixum_img();
                 }
             }
         }
     }
     $ext = get_file_extension($cache_path);
     if ($ext == 'jpg') {
         $ext = 'jpeg';
     }
     header("Content-Type: image/" . $ext);
     header("Content-Length: " . filesize($cache_path));
     readfile($cache_path);
     exit;
 }
示例#5
0
 function get_import_location()
 {
     if (defined('MW_CRON_EXEC')) {
     } else {
         if (!is_admin()) {
             return false;
         }
     }
     $loc = $this->imports_folder;
     if ($loc != false) {
         return $loc;
     }
     $folder_root = false;
     if (function_exists('userfiles_path')) {
         $folder_root = userfiles_path();
     } elseif (mw_cache_path()) {
         $folder_root = normalize_path(mw_cache_path());
     }
     $here = $folder_root . "import" . DS;
     if (!is_dir($here)) {
         mkdir_recursive($here);
         $hta = $here . '.htaccess';
         if (!is_file($hta)) {
             touch($hta);
             file_put_contents($hta, 'Deny from all');
         }
     }
     $here = $folder_root . "import" . DS . get_table_prefix() . DS;
     $here2 = $this->app->option_manager->get('import_location', 'admin/import');
     if ($here2 != false and is_string($here2) and trim($here2) != 'default' and trim($here2) != '') {
         $here2 = normalize_path($here2, true);
         if (!is_dir($here2)) {
             mkdir_recursive($here2);
         }
         if (is_dir($here2)) {
             $here = $here2;
         }
     }
     if (!is_dir($here)) {
         mkdir_recursive($here);
     }
     $loc = $here;
     $this->imports_folder = $loc;
     return $here;
 }
示例#6
0
 public function download($requestUrl, $post_params = false, $save_to_file = false)
 {
     if ($post_params != false and is_array($post_params)) {
         $postdata = http_build_query($post_params);
     } else {
         $postdata = false;
     }
     $ref = site_url();
     $opts = array('http' => array('method' => 'POST', 'header' => "User-Agent: Microweber/" . MW_VERSION . "\r\n" . 'Content-type: application/x-www-form-urlencoded' . "\r\n" . 'Referer: ' . $ref . "\r\n", 'content' => $postdata));
     $requestUrl = str_replace(' ', '%20', $requestUrl);
     if (function_exists('curl_init')) {
         $ch = curl_init($requestUrl);
         curl_setopt($ch, CURLOPT_COOKIEJAR, mw_cache_path() . "global/cookie.txt");
         curl_setopt($ch, CURLOPT_COOKIEFILE, mw_cache_path() . "global/cookie.txt");
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Microweber " . MW_VERSION . ";)");
         if ($post_params != false) {
             curl_setopt($ch, CURLOPT_POST, count($post_params));
             curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
         }
         //	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
         //curl_setopt($ch, CURLOPT_TIMEOUT, 400);
         $result = curl_exec($ch);
         curl_close($ch);
     } else {
         $context = stream_context_create($opts);
         $result = file_get_contents($requestUrl, false, $context);
     }
     if ($save_to_file == true) {
         file_put_contents($save_to_file, $result);
     } else {
         return $result;
     }
     return false;
 }