public static function minimize_html($input) { if (trim($input) === "") { return $input; } // Remove extra white-space(s) between HTML attribute(s) $input = preg_replace_callback('#<([^\\/\\s<>!]+)(?:\\s+([^<>]*?)\\s*|\\s*)(\\/?)>#s', function ($matches) { return '<' . $matches[1] . preg_replace('#([^\\s=]+)(\\=([\'"]?)(.*?)\\3)?(\\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>'; }, str_replace("\r", "", $input)); // Minify inline CSS declaration(s) if (strpos($input, ' style=') !== false) { $input = preg_replace_callback('#<([^<]+?)\\s+style=([\'"])(.*?)\\2(?=[\\/\\s>])#s', function ($matches) { return '<' . $matches[1] . ' style=' . $matches[2] . MinimizeTools::minimize_css($matches[3]) . $matches[2]; }, $input); } return preg_replace(array('#<(img|input)(>| .*?>)#s', '#(<!--.*?-->)|(>)(?:\\n*|\\s{2,})(<)|^\\s*|\\s*$#s', '#(<!--.*?-->)|(?<!\\>)\\s+(<\\/.*?>)|(<[^\\/]*?>)\\s+(?!\\<)#s', '#(<!--.*?-->)|(<[^\\/]*?>)\\s+(<[^\\/]*?>)|(<\\/.*?>)\\s+(<\\/.*?>)#s', '#(<!--.*?-->)|(<\\/.*?>)\\s+(\\s)(?!\\<)|(?<!\\>)\\s+(\\s)(<[^\\/]*?\\/?>)|(<[^\\/]*?\\/?>)\\s+(\\s)(?!\\<)#s', '#(<!--.*?-->)|(<[^\\/]*?>)\\s+(<\\/.*?>)#s', '#<(img|input)(>| .*?>)<\\/\\1>#s', '#( ) (?![<\\s])#', '#(?<=\\>)( )(?=\\<)#', '#\\s*<!--(?!\\[if\\s).*?-->\\s*|(?<!\\>)\\n+(?=\\<[^!])#s'), array('<$1$2</$1>', '$1$2$3', '$1$2$3', '$1$2$3$4$5', '$1$2$3$4$5$6$7', '$1$2$3', '<$1$2', '$1 ', '$1', ""), $input); }
function post($action = null) { $users = new UsersData(); if ($action == 'process' && $users->checkPass() && !$users->authNeeded()) { DashboardUtils::backupFiles($_POST['files']); MinimizeTools::minimizeHTMLFiles($_POST['files']); new AnalyticsData(); new MediaData(); new SettingsData(); $rss = new RSSData(); $rss->buildDataFile($_POST['files']); $nav = new NavigationData(); $nav->buildDataFile($_POST['files']); $blog = new BlogData(); $blog->buildDataFile($_POST['files']); $footer = new FooterData(); $footer->buildDataFile($_POST['files']); $pages = new PagesData(); $pages->buildDataFile($_POST['files']); DashboardUtils::renameFiles($_POST['files']); DashboardUtils::copyApacheConfig(); DashboardUtils::createXMLSiteMap(); $logsData = new LogsData(); $logsData->addToLog('has initiated the CMS on the following files:', implode(" ", $_POST['files'])); header('Location: /admin/'); } else { include_once '401.html'; } }
public function buildDataFile($files) { foreach ($files as $file) { $this->addPage(str_replace(array('.html', '.htm'), '', $file)); // create datafile to store stuff $dataFile = 'page-' . str_replace(array('.html', '.htm'), '.json', $file); if (file_exists($this->dataLoc . $dataFile)) { $data = json_decode(file_get_contents($this->dataLoc . $dataFile), true); } else { $data = array(); } // start collecting fields to add to data $fileData = file_get_contents('../' . $file, true); $html = str_get_html($fileData); foreach ($html->find('html') as $htmlTag) { if (isset($htmlTag->lang)) { $settingsData = new SettingsData(); $settingsData->setLang($htmlTag->lang); } $htmlTag->lang = "<?=get('autocms-settings.json', 'site-lang')?>"; } foreach ($html->find('head link') as $pageLinks) { $cssFile = strtolower($pageLinks->href); if (!DashboardUtils::startsWith($cssFile, '//') && !DashboardUtils::startsWith($cssFile, 'http://') && !DashboardUtils::startsWith($cssFile, 'https://')) { if (!DashboardUtils::startsWith($cssFile, '/')) { $cssFile = '/' . $cssFile; } if (file_exists($_SERVER['DOCUMENT_ROOT'] . $cssFile) && strpos($cssFile, 'min') === false && strpos($cssFile, 'css') !== false) { if (!is_dir($_SERVER['DOCUMENT_ROOT'] . '/assets/')) { mkdir($_SERVER['DOCUMENT_ROOT'] . '/assets/'); } if (!is_dir($_SERVER['DOCUMENT_ROOT'] . '/assets/auto-css/')) { mkdir($_SERVER['DOCUMENT_ROOT'] . '/assets/auto-css/'); } $cssFileData = file_get_contents('../' . $cssFile, true); $cssFileData = MinimizeTools::minimize_css($cssFileData); $fileName = pathinfo($cssFile, PATHINFO_FILENAME); $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/assets/auto-css/' . $fileName . '.min.css', 'w'); fwrite($fp, $cssFileData); fclose($fp); $pageLinks->href = '/assets/auto-css/' . $fileName . '.min.css'; } } } foreach ($html->find('.auto-head title') as $pageTitle) { $data['title'] = array('text' => $pageTitle->innertext, 'description' => 'title', 'type' => 'text'); $pageTitle->innertext = "<?=get('{$dataFile}', 'title')?>"; } foreach ($html->find('.auto-head meta') as $pageMeta) { if ($pageMeta->name == 'keywords' || $pageMeta->name == 'description' || $pageMeta->name == 'author') { $data[$pageMeta->name] = array('text' => $pageMeta->content, 'description' => $pageMeta->name, 'type' => 'text'); $pageMeta->content = "<?=get('{$dataFile}', '{$pageMeta->name}')?>"; } if (isset($pageMeta->property) && isset($pageMeta->content)) { $property = preg_replace("/[^a-z^A-Z^0-9_-]/", "", $pageMeta->property); if ($pageMeta->property == "og:image") { //todo: fix this (copy og:image and put in library if exist) /*$imgFileName = ''; $source = $pageMeta->content; $source = parse_url($source, PHP_URL_PATH); $fileExt = pathinfo(parse_url($source, PHP_URL_PATH), PATHINFO_EXTENSION); $fileExt = MediaData::getImageType($fileExt, $source); if ($fileExt != 'error') { $media = new MediaData(); if (!$media->checkMediaLibrary('images', $source)) { $imgFileName = $media->makeDateFolders() . uniqid() . '.' . $fileExt; copy($source, $_SERVER['DOCUMENT_ROOT'] . $imgFileName); $media->addToMediaLibrary('images', $imgFileName, $source); } else { $imgFileName = $media->getFromMediaLibrary('images', $source); } } */ $data[$property] = array('image' => $pageMeta->content, 'description' => $pageMeta->property, 'type' => 'image'); $pageMeta->content = "<?=get('{$dataFile}', '{$property}')?>"; } else { if ($pageMeta->property == "og:url") { $pageMeta->content = '<?="http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]?>'; } else { if ($pageMeta->property == "og:title") { $pageMeta->content = "<?=get('{$dataFile}', 'title')?>"; } else { if ($pageMeta->property == "og:description") { $pageMeta->content = "<?=get('{$dataFile}', 'description')?>"; } else { if ($pageMeta->property == "og:site_name") { $pageMeta->content = "<?=get('autocms-settings.json', 'site-name')?>"; } else { $data[$property] = array('text' => $pageMeta->content, 'description' => $pageMeta->property, 'type' => 'text'); $pageMeta->content = "<?=get('{$dataFile}', '{$property}')?>"; } } } } } } } foreach ($html->find('.auto-head') as $pageHead) { $rssData = new RSSData(); $rssFeed = ''; if ($rssData->hasFeed()) { $rssFeed = '<link rel="alternate" type="application/rss+xml" href="' . $rssData->getFeedLocation() . '" title="RSS feed for My Page">'; } $pageHead->innertext .= $rssFeed . "<?=get('autocms-analytics.json', 'analytics')?>"; $pageHead->class = str_replace('auto-head', '', $pageHead->class); if (trim($pageHead->class) === '') { $pageHead->class = null; } } foreach ($html->find('.auto-color, .auto-edit, .auto-edit-text, .auto-link, .auto-edit-img, .auto-edit-bg-img, .auto-repeat') as $edit) { $fieldID = uniqid(); $desc = ''; if (strpos($edit->class, 'auto-repeat') !== false) { if (isset($edit->autocms)) { $desc = $edit->autocms; } $data[$fieldID] = array('repeat' => array(), 'description' => $desc, 'type' => 'repeat'); $count = 0; $data[$fieldID]['repeat'][$count] = array(); foreach ($html->find('.auto-repeat .auto-color, .auto-repeat .auto-edit, .auto-repeat .auto-edit-text, .auto-repeat .auto-link, .auto-repeat .auto-edit-img, .auto-repeat .auto-edit-bg-img') as $repeat) { $desc = ''; $repeatFieldID = uniqid(); if (strpos($repeat->class, 'auto-edit-img') !== false) { $this->makeImageBGImage($repeat, $data, $dataFile, $fieldID, $desc, false, $count, $repeatFieldID); } else { if (strpos($repeat->class, 'auto-edit-bg-img') !== false) { $this->makeImageBGImage($repeat, $data, $dataFile, $fieldID, $desc, true, $count, $repeatFieldID); } else { if (strpos($repeat->class, 'auto-link') !== false) { $this->makeLink($repeat, $data, $dataFile, $fieldID, $desc, $count, $repeatFieldID); } else { if (strpos($repeat->class, 'auto-edit-text') !== false) { $this->makeHTMLText($repeat, $data, $dataFile, $fieldID, $desc, 'text', $count, $repeatFieldID); } else { if (strpos($repeat->class, 'auto-edit') !== false) { $this->makeHTMLText($repeat, $data, $dataFile, $fieldID, $desc, 'html', $count, $repeatFieldID); } else { if (strpos($repeat->class, 'auto-color') !== false) { $this->makeColor($repeat, $data, $dataFile, $fieldID, $desc, $count, $repeatFieldID); } } } } } } } $edit->class = str_replace('auto-repeat', '', $edit->class); if (trim($edit->class) === '') { $edit->class = null; } $edit->autocms = null; $edit->outertext = '<?php for ($x = 0; $x ' . "< repeatCount('{$dataFile}', '{$fieldID}');" . ' $x++) { ?>' . $edit->outertext . "<?php } ?>"; } else { if (strpos($edit->class, 'auto-edit-img') !== false) { $this->makeImageBGImage($edit, $data, $dataFile, $fieldID, $desc); } else { if (strpos($edit->class, 'auto-edit-bg-img') !== false) { $this->makeImageBGImage($edit, $data, $dataFile, $fieldID, $desc, true); } else { if (strpos($edit->class, 'auto-link') !== false) { $this->makeLink($edit, $data, $dataFile, $fieldID, $desc); } else { if (strpos($edit->class, 'auto-edit-text') !== false) { $this->makeHTMLText($edit, $data, $dataFile, $fieldID, $desc, 'text'); } else { if (strpos($edit->class, 'auto-edit') !== false) { $this->makeHTMLText($edit, $data, $dataFile, $fieldID, $desc); } else { if (strpos($edit->class, 'auto-color') !== false) { $this->makeColor($edit, $data, $dataFile, $fieldID, $desc); } } } } } } } } // write data file $fp = fopen($this->dataLoc . $dataFile, 'w'); fwrite($fp, json_encode($data)); fclose($fp); $fileTopper = '<?php require_once("admin/other/get.php") ?>'; // write html file $fp = fopen('../' . $file, 'w'); fwrite($fp, $fileTopper . $html); fclose($fp); } }