/** * Lists files for the site * * @param {string} $id */ public static function ListFiles($id) { $dir = app()->basePath() . '/public/sites/' . $id . '/files'; // list allowed types $exts = explode(',', env('ALLOWED_FILETYPES')); // list files $arr = Utilities::ListFiles($dir, $id, $exts, array('files/thumb/', 'files/thumbs/')); return $arr; }
/** * Refreshes the page JSON * * @param {User} $user * @param {string} $id friendly id of site (e.g. site-name) * @return Response */ public static function refreshJSON($user, $site) { // get base path for the site $json_file = app()->basePath() . '/public/sites/' . $site->id . '/data/pages.json'; // set dir $dir = app()->basePath() . '/public/sites/' . $site->id; // list files $files = Utilities::ListFiles($dir, $site->id, array('html'), array('plugins/', 'components/', 'css/', 'data/', 'files/', 'js/', 'locales/', 'fragments/', 'themes/')); // setup arrays to hold data $arr = array(); foreach ($files as $file) { // defaults $title = ''; $description = ''; $keywords = ''; $callout = ''; $url = $file; $text = ''; $html = ''; $language = 'en'; $direction = 'ltr'; $photo = ''; $thumb = ''; $lastModifiedDate = date('Y-m-d\\TH:i:s.Z\\Z', time()); // set full file path $file = app()->basePath() . '/public/sites/' . $site->id . '/' . $file; $file_modified_time = filemtime($file); // setup timestamp as JS date $timestamp = date('Y-m-d\\TH:i:s.Z\\Z', $file_modified_time); // set parser $dom = HtmlDomParser::str_get_html(file_get_contents($file), $lowercase = true, $forceTagsClosed = false, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN = false, $defaultBRText = DEFAULT_BR_TEXT, $defaultSpanText = DEFAULT_SPAN_TEXT); // get title $els = $dom->find('title'); if (isset($els[0])) { $title = $els[0]->innertext; } // get els $els = $dom->find('body'); // set timestamp in head if (isset($els[0])) { $lastModifiedDate = $els[0]->getAttribute('data-lastmodified'); } // get description $els = $dom->find('meta[name=description]'); if (isset($els[0])) { $description = $els[0]->content; } // get keywords $els = $dom->find('meta[name=keywords]'); if (isset($els[0])) { $keywords = $els[0]->content; } // get text $els = $dom->find('[role=main]'); if (isset($els[0])) { $main_content = $els[0]->innertext; } // get the text from the content $text = strip_tags($main_content); $text = preg_replace("/\\s+/", " ", $text); $text = trim($text); $text = preg_replace('/[[:^print:]]/', '', $text); // get photo $photos = $dom->find('[role=main] img'); if (isset($photos[0])) { $photo = $photos[0]->src; } $thumb = ''; if ($photo === NULL || $photo === '') { $photo = ''; } else { if (substr($photo, 0, 4) === "http") { $thumb = $photo; } else { $thumb = str_replace('files/', 'files/thumbs/', $photo); $thumb = str_replace('thumbs/thumbs', 'thumbs/', $thumb); } } // get language and direction $els = $dom->find('html'); if (isset($els[0])) { $language = $els[0]->lang; $direction = $els[0]->dir; } // cleanup url $url = ltrim($url, '/'); // strip any trailing .html from url $url = preg_replace('/\\.[^.\\s]{3,4}$/', '', $url); // setup data $data = array('title' => $title, 'description' => $description, 'text' => $text, 'keywords' => $keywords, 'callout' => $callout, 'url' => $url, 'photo' => $photo, 'thumb' => $thumb, 'language' => $language, 'direction' => $direction, 'firstName' => $user->firstName, 'lastName' => $user->lastName, 'lastModifiedBy' => $user->email, 'lastModifiedDate' => $timestamp); // push to array if (substr($url, 0, strlen('.default')) !== '.default') { array_push($arr, $data); } } // encode arr $content = json_encode($arr, JSON_PRETTY_PRINT); // update content file_put_contents($json_file, $content); return $arr; }