/** * lists all files * * @param {files} $data * @return {array} */ public static function listAll($siteId) { $dir = app()->basePath() . '/public/sites/' . $siteId . '/data/menus/'; $exts = array('json'); $files = Utilities::listFiles($dir, $siteId, $exts); $arr = array(); foreach ($files as $file) { $path = app()->basePath() . '/public/sites/' . $siteId . '/' . $file; if (file_exists($path)) { $json = json_decode(file_get_contents($path), true); $id = str_replace('.json', '', $file); $id = str_replace('data/menus/', '', $id); $id = str_replace('/', '', $id); array_push($arr, array('id' => $id, 'name' => $json['name'])); } } return $arr; }
/** * Returns all HTML files for a given path * * @param {string} $path the recipient's email address * @return {Array} list of HTML fields */ public static function listFiles($dir, $id, $exts, $restrict = NULL) { $root = scandir($dir); if (!isset($result)) { $result = array(); } foreach ($root as $value) { if ($value === '.' || $value === '..' || $value === '.htaccess') { continue; } if (is_file("{$dir}/{$value}")) { $file = "{$dir}/{$value}"; $ext = pathinfo($file, PATHINFO_EXTENSION); if (in_array($ext, $exts) === TRUE) { $paths = explode('sites/' . $id . '/', "{$dir}/{$value}"); $is_restricted = FALSE; if ($restrict != NULL) { foreach ($restrict as $item) { // TODO: MAKE SURE THE FILE DOES NOT START WITH A RESTRICTED PATH if (substr($paths[1], 0, strlen($item)) === $item) { $is_restricted = TRUE; } } } if ($is_restricted === FALSE) { $result[] = $paths[1]; } } else { continue; } continue; } foreach (Utilities::listFiles("{$dir}/{$value}", $id, $exts, $restrict) as $value) { $result[] = $value; } } return $result; }
/** * 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++; } } }