/** * Lists the pages for given site * * @return Response */ public function listAll(Request $request) { // get request data $email = $request->input('auth-email'); $id = $request->input('auth-id'); // get site and user $site = Site::getById($id); $user = User::getByEmail($email, $id); // list pages in the site $arr = Page::listAll($user, $site); // sort by last modified date usort($arr, function ($a, $b) { $ts1 = strtotime($a['lastModifiedDate']); $ts2 = strtotime($b['lastModifiedDate']); return $ts2 - $ts1; }); return response()->json($arr); }
/** * Publishes plugins for the site * * @param {Site} $site */ public static function publishPlugins($user, $site) { // get plugins for the site $dir = app()->basePath() . '/public/sites/' . $site->id . '/plugins/'; $exts = array('html'); $files = Utilities::listFiles($dir, $site->id, $exts); $plugins = array(); foreach ($files as $file) { $path = app()->basePath() . '/public/sites/' . $site->id . '/' . $file; if (file_exists($path)) { $html = file_get_contents($path); $id = basename($path); $id = str_replace('.html', '', $id); // push plugin to array array_push($plugins, $id); } } // location where twig should look for templates (local to site, then global) $template_dirs = array(app()->basePath() . '/public/sites/' . $site->id . '/plugins'); $global_plugin_dir = app()->basePath() . '/resources/plugins'; if (file_exists($global_plugin_dir)) { array_push($template_dirs, $global_plugin_dir); } // setup twig $loader = new \Twig_Loader_Filesystem($template_dirs); $twig = new \Twig_Environment($loader); $twig->addExtension(new BetterSortTwigExtension()); // get all pages $pages = Page::listAll($user, $site); // list all forms, menus, galleries $forms = Form::listExtended($site->id); $menus = Menu::listExtended($site->id); $galleries = Gallery::listExtended($site->id); $i = 0; // get html of pages foreach ($pages as $page) { // stript html $url = $page['url']; $url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url); // get html of page $file = app()->basePath() . '/public/sites/' . $site->id . '/' . $url . '.html'; if (file_exists($file)) { $html = file_get_contents($file); // set parser $dom = HtmlDomParser::str_get_html($html, $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // find main content $el = $dom->find('[role=main]'); $main_content = ''; // get the fragment content if (isset($el[0])) { $main_content = $el[0]->innertext; } // set html $pages[$i]['html'] = $main_content; } $i++; } $i = 0; // public plugin for pages foreach ($pages as $item) { // get page $page = new Page($item); // setup current page $current_page = array('url' => $page->url, 'title' => $page->title, 'description' => $page->description, 'keywords' => $page->keywords, 'callout' => $page->callout, 'photo' => $page->photo, 'thumb' => $page->thumb, 'language' => $page->language, 'direction' => $page->direction, 'firstName' => $page->firstName, 'lastName' => $page->lastName, 'lastModifiedBy' => $page->lastModifiedBy, 'lastModifiedDate' => $page->lastModifiedDate); // setup whether the site is using friendly urls $useFriendlyURLs = false; if (env('FRIENDLY_URLS') === true || env('FRIENDLY_URLS') === 'true') { $useFriendlyURLs = true; } // setup current site $current_site = array('id' => $site->id, 'name' => $site->name, 'email' => $site->email, 'api' => Utilities::retrieveAppUrl() . '/api', 'useFriendlyURLs' => $useFriendlyURLs); // set url $url = $page->url; $url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url); $location = app()->basePath() . '/public/sites/' . $site->id . '/' . $url . '.html'; // check for valid location if (file_exists($location)) { // get html from page $html = file_get_contents($location); // walk through plugins foreach ($plugins as $plugin) { // insert into respond-plugin comments $start = '<!-- respond-plugin:' . $plugin . ' -->'; $end = '<!-- /respond-plugin:' . $plugin . ' -->'; // check for start and end if (strpos($html, $start) !== FALSE && strpos($html, $end) !== FALSE) { // load the template $template = $twig->loadTemplate($plugin . '.html'); // render the template $plugin_html = $template->render(array('pages' => $pages)); // replace content $html = Utilities::replaceBetween($html, $start, $end, $plugin_html); } } // make sure the html is not empty if (!empty($html)) { // load the parser $dom = HtmlDomParser::str_get_html($html, $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // insert into [respond-plugin] elements foreach ($dom->find('[respond-plugin]') as $el) { if (isset($el->type)) { if (array_search($el->type, $plugins) !== FALSE) { // load the template $template = $twig->loadTemplate($el->type . '.html'); $render_arr = array('page' => $current_page, 'site' => $current_site, 'pages' => $pages, 'forms' => $forms, 'galleries' => $galleries, 'menus' => $menus, 'attributes' => $el->attr); // render the template $plugin_html = $template->render($render_arr); // set the inner text $el->innertext = $plugin_html; } } } } // find main content $el = $dom->find('[role=main]'); $main_content = ''; // get the fragment content if (isset($el[0])) { $main_content = $el[0]->innertext; } // put html back file_put_contents($location, $dom); // update html in the array $pages[$i]['html'] = $main_content; // increment $i++; } } }
/** * Saves all settings * * @param {string} $name * @param {string} $siteId site id * @return Response */ public static function saveAll($settings, $user, $site) { // get file $file = app()->basePath() . '/resources/sites/' . $site->id . '/settings.json'; // get settings if (file_exists($file)) { file_put_contents($file, json_encode($settings, JSON_PRETTY_PRINT)); // update settings in the pages $arr = Page::listAll($user, $site); foreach ($arr as $item) { // get page $page = new Page($item); $path = app()->basePath() . '/public/sites/' . $site->id . '/' . $page->url . '.html'; // fix double html $path = str_replace('.html.html', '.html', $path); // init css $set_css = false; $css = ''; if (file_exists($path)) { // get contents of the page $html = file_get_contents($path); // parse HTML $dom = HtmlDomParser::str_get_html($html, $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // walk through settings foreach ($settings as $setting) { // handle sets if (isset($setting['sets'])) { // set attribute if (isset($setting['attribute'])) { // find setting $els = $dom->find('[' . $setting['id'] . ']'); // set attribute foreach ($els as $el) { $el->setAttribute($setting['attribute'], $setting['value']); } } // set css if (isset($setting['css'])) { // build css string $set_css = true; $css .= str_replace('config(--' . $setting['id'] . ')', $setting['value'], $setting['css']); } } } // remove existing inline styles $styles = $dom->find('[respond-settings]'); foreach ($styles as $style) { $style->outertext = ''; } // append style to the dom $head = $dom->find('head', 0); if ($head != NULL) { $head->innertext = $head->innertext() . '<style respond-settings>' . $css . '</style>'; } // update contents file_put_contents($path, $dom); } } return TRUE; } return FALSE; }