/** * Create the files and folders * */ function __construct() { // HHVM doesn't support writing with PHAR // https://github.com/facebook/hhvm/issues/4899 if (defined('HHVM_VERSION')) { $this->types = array('zip'); } $this->dir = sys_get_temp_dir() . '/test-' . rand(1, 10000000); foreach ($this->files as $name => $content) { $full = $this->dir . '/' . $name; \gp\tool\Files::Save($full, $content); } }
/** * Convert a .scss or .less files to .css and include it in the page * * @param mixed $scss_files A string or array of scss files * @param string $type * @return bool */ public static function Cache($file_array, $type = 'scss') { global $dataDir; //generage the name of the css file from the modified times and content length of each imported file $file_array = (array) $file_array; $type = strtolower($type); $files_hash = \gp\tool::ArrayHash($file_array); $list_file = $dataDir . '/data/_cache/' . $type . '_' . $files_hash . '.list'; if (file_exists($list_file)) { $list = explode("\n", file_get_contents($list_file)); //pop the etag $etag = array_pop($list); // generate an etag if needed or if logged in if (\gp\tool::LoggedIn()) { $etag = \gp\tool::FilesEtag($list); } $compiled_name = $type . '_' . $files_hash . '_' . $etag . '.css'; $compiled_file = '/data/_cache/' . $compiled_name; if (file_exists($dataDir . $compiled_file)) { return $compiled_file; } } if ($type == 'less') { $compiled = self::ParseLess($file_array); } else { $compiled = self::ParseScss($file_array); } if (!$compiled) { return false; } // generate the file name $etag = \gp\tool::FilesEtag($file_array); $compiled_name = $type . '_' . $files_hash . '_' . $etag . '.css'; $compiled_file = '/data/_cache/' . $compiled_name; // save the cache // use the last line for the etag $list = $file_array; $list[] = $etag; $cache = implode("\n", $list); if (!\gp\tool\Files::Save($list_file, $cache)) { return false; } //save the css if (\gp\tool\Files::Save($dataDir . $compiled_file, $compiled)) { return $compiled_file; } return false; }
/** * Perform a page copy * */ public function CopyPage() { global $gp_index, $gp_titles, $langmessage; $this->CacheSettings(); if (!isset($_POST['from_title'])) { msg($langmessage['OOPS'] . ' (Copy from not selected)'); if (isset($_POST['insert_how'])) { $this->InsertDialog($_POST['insert_how']); } else { $this->AddHidden(); } return false; } //existing page info $from_title = $_POST['from_title']; if (!isset($gp_index[$from_title])) { msg($langmessage['OOPS_TITLE']); return false; } $from_index = $gp_index[$from_title]; $info = $gp_titles[$from_index]; //check the new title $title = $_POST['title']; $title = \gp\admin\Tools::CheckPostedNewPage($title, $message); if ($title === false) { msg($message); return false; } //get the existing content $from_file = \gp\tool\Files::PageFile($from_title); $contents = file_get_contents($from_file); //add to $gp_index first! $index = \gp\tool::NewFileIndex(); $gp_index[$title] = $index; $file = \gp\tool\Files::PageFile($title); if (!\gp\tool\Files::Save($file, $contents)) { msg($langmessage['OOPS'] . ' (File not saved)'); return false; } //add to gp_titles $new_titles = array(); $new_titles[$index]['label'] = \gp\admin\Tools::PostedLabel($_POST['title']); $new_titles[$index]['type'] = $info['type']; $gp_titles += $new_titles; //add to menu $insert = array(); $insert[$index] = array(); if (!$this->SaveNew($insert)) { $this->RestoreSettings(); return false; } $this->HiddenSaved($index); return true; }
/** * Download the source code * */ function DownloadSource() { global $langmessage, $dataDir; $this->msg('Downloading version ' . $this->core_package['version'] . ' from ' . CMS_READABLE_DOMAIN . '.'); /* for testing * $download = 'http://gpeasy.loc/x_gpEasy.zip'; */ $download = addon_browse_path . '/Special_gpEasy?cmd=download'; $contents = \gp\tool\RemoteGet::Get_Successful($download); if (!$contents || empty($contents)) { $this->msg($langmessage['download_failed'] . '(1)'); return false; } $this->msg($langmessage['package_downloaded']); $md5 = md5($contents); if ($md5 != $this->core_package['md5']) { $this->msg($langmessage['download_failed_md5'] . '<br/>Downloaded Checksum (' . $md5 . ') != Expected Checksum (' . $this->core_package['md5'] . ')'); return false; } $this->msg($langmessage['download_verified']); //save contents $temp_file = $dataDir . \gp\tool\FileSystem::TempFile('/data/_temp/update', '.zip'); if (!\gp\tool\Files::Save($temp_file, $contents)) { $this->msg($langmessage['download_failed'] . ' (2)'); return false; } $this->core_package['file'] = $temp_file; return true; }
/** * Get the contents of a revision * */ protected function GetRevision($time) { $full_path = $this->BackupFile($time); if (is_null($full_path)) { return false; } //if it's a compressed file, we need an uncompressed version if (strpos($full_path, '.gze') !== false) { ob_start(); readgzfile($full_path); $contents = ob_get_clean(); $dir = \gp\tool::DirName($full_path); $full_path = tempnam($dir, 'backup') . '.php'; \gp\tool\Files::Save($full_path, $contents); $file_sections = \gp\tool\Files::Get($full_path, 'file_sections'); unlink($full_path); } else { $file_sections = \gp\tool\Files::Get($full_path, 'file_sections'); } return $file_sections; }
/** * Get the remote package * */ public function GetRemote() { global $langmessage, $dataDir; // check values if (empty($this->type) || empty($this->id) || !is_numeric($this->id)) { $this->message($langmessage['OOPS'] . ' (Invalid Request)'); return false; } // download url $download_url = \gp\admin\Tools::RemoteUrl($this->type); // allowed to remote install? if ($download_url === false) { $this->message($langmessage['OOPS'] . ' (Can\'t remote install ' . $this->type . ')'); return false; } $download_url .= '?cmd=install&id=' . rawurlencode($this->id); // purchase order id if (is_null($this->order)) { $this->order = $this->GetOrder($this->id); } if (!is_null($this->order)) { $download_url .= '&order=' . rawurlencode($this->order); } // able to remote install? if (!\gp\admin\Tools::CanRemoteInstall()) { $this->message($langmessage['OOPS'] . ' (Can\'t remote install)'); return false; } // get package from remote $full_result = \gp\tool\RemoteGet::Get($download_url); if ((int) $full_result['response']['code'] < 200 && (int) $full_result['response']['code'] >= 300) { $this->message($langmessage['download_failed'] . ' (1)'); return false; } // download failed and a message was sent if (isset($full_result['headers']['x-error'])) { $this->message(htmlspecialchars($full_result['headers']['x-error'])); $this->message(sprintf($langmessage['download_failed_xerror'], 'href="' . self::DetailUrl($_POST['type'], $_POST['id']) . '" data-cmd="remote"')); return false; } $result = $full_result['body']; $md5 =& $full_result['headers']['x-md5']; //check md5 $package_md5 = md5($result); if ($package_md5 != $md5) { $this->message($langmessage['download_failed_md5'] . ' <br/> (Package Checksum ' . $package_md5 . ' != Expected Checksum ' . $md5 . ')'); return false; } //save contents $tempfile = $dataDir . \gp\tool\FileSystem::TempFile('/data/_temp/addon', '.zip'); if (!\gp\tool\Files::Save($tempfile, $result)) { $this->message($langmessage['download_failed'] . ' (Package not saved)'); return false; } $this->source = $this->TempFile(); $success = $this->ExtractArchive($tempfile); unlink($tempfile); return $success; }
public function put_contents($file, $contents, $type = '') { if (!\gp\tool\Files::Save($file, $contents)) { return false; } //the core does not need to be world writable @chmod($file, FS_CHMOD_FILE); return true; }
/** * Record fatal errors in /data/_site/ so we can prevent subsequent requests from having the same issue * */ static function RecordFatal($last_error) { global $dataDir, $config, $addon_current_id, $addonFolderName; $last_error['request'] = $_SERVER['REQUEST_URI']; if ($addon_current_id) { $last_error['addon_name'] = $config['addons'][$addonFolderName]['name']; $last_error['addon_id'] = $addon_current_id; } $last_error['file'] = realpath($last_error['file']); //may be redundant showError($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line'], false); //send error to logger if (empty(self::$catchable)) { return; } $last_error['time'] = time(); $last_error['request_method'] = $_SERVER['REQUEST_METHOD']; if (!empty($last_error['file'])) { $last_error['file_modified'] = filemtime($last_error['file']); $last_error['file_size'] = filesize($last_error['file']); } $content = json_encode($last_error); $temp = array_reverse(self::$catchable); foreach ($temp as $error_hash => $info) { $file = $dataDir . '/data/_site/fatal_' . $error_hash; \gp\tool\Files::Save($file, $content); if ($info['catchable_type'] == 'exec') { break; } } }
public function CopyDir($source, $dest) { global $dataDir, $langmessage; $data_files = \gp\tool\Files::ReadDir($source, false); foreach ($data_files as $file) { if ($file == '.' || $file == '..') { continue; } $source_full = $source . '/' . $file; $dest_full = $dest . '/' . $file; if (file_exists($dest_full)) { continue; } if (is_dir($source_full)) { if (!$this->CopyDir($source_full, $dest_full)) { return false; } continue; } $contents = file_get_contents($source_full); if (!\gp\tool\Files::Save($dest_full, $contents)) { message($langmessage['OOPS'] . ' Session file not copied (0)'); return false; } } return true; }
/** * Generate a file with all of the combined content * */ public static function GenerateFile($files, $type) { global $dataDir; //get etag $modified = $content_length = 0; $full_paths = array(); foreach ($files as $file) { $full_path = self::CheckFile($file); if ($full_path === false) { continue; } self::FileStat_Static($full_path, $modified, $content_length); $full_paths[$file] = $full_path; } //check css imports if ($type == 'css') { $had_imported = false; $import_data = array(); $imported_file = $dataDir . '/data/_cache/import_info.php'; if (file_exists($imported_file)) { include_once $imported_file; } foreach ($full_paths as $file => $full_path) { if (!isset($import_data[$full_path])) { continue; } $had_imported = true; foreach ($import_data[$full_path] as $imported_full) { self::FileStat_Static($imported_full, $modified, $content_length); } unset($import_data[$full_path]); } } //check to see if file exists $etag = \gp\tool::GenEtag(json_encode($files), $modified, $content_length); $cache_relative = '/data/_cache/combined_' . $etag . '.' . $type; $cache_file = $dataDir . $cache_relative; if (file_exists($cache_file)) { // change modified time to extend cache if (time() - filemtime($cache_file) > 604800) { touch($cache_file); } return $cache_relative; } //create file if ($type == 'js') { ob_start(); \gp\tool::jsStart(); foreach ($full_paths as $full_path) { readfile($full_path); echo ";\n"; } $combined_content = ob_get_clean(); } else { $imports = $combined_content = ''; $new_imported = array(); foreach ($full_paths as $file => $full_path) { $temp = new \gp\tool\Output\CombineCss($file); $combined_content .= "\n/* " . $file . " */\n"; $combined_content .= $temp->content; $imports .= $temp->imports; if (count($temp->imported)) { $new_imported[$full_path] = $temp->imported; } } $combined_content = $imports . $combined_content; //save imported data if (count($new_imported) || $had_imported) { if (count($new_imported)) { $import_data = $new_imported + $import_data; } \gp\tool\Files::SaveData($imported_file, 'import_data', $import_data); } } if (!\gp\tool\Files::Save($cache_file, $combined_content)) { return false; } \gp\admin\Tools::CleanCache(); return $cache_relative; }
/** * Save the custom.css or custom.scss file * */ public function SaveCustom($layout, $css) { global $langmessage; //delete css file if empty if (empty($css)) { return $this->RemoveCSS($layout); } //save if not empt $custom_file = $this->LayoutCSSFile($layout); if (!\gp\tool\Files::Save($custom_file, $css)) { message($langmessage['OOPS'] . ' (CSS not saved)'); return false; } return true; }
/** * Get cached data or fetch new response from server and cache it * */ public function RemoteBrowseResponse($src) { global $dataDir, $langmessage; $cache_file = $dataDir . '/data/_remote/' . sha1($src) . '.txt'; $cache_used = false; //check cache if (file_exists($cache_file) && filemtime($cache_file) + 26100 > time()) { $result = file_get_contents($cache_file); $cache_used = true; } else { $result = \gp\tool\RemoteGet::Get_Successful($src); } $data = $this->ParseResponse($result); if ($data === false) { return false; } //not unserialized? if (count($data) == 0) { echo '<p>'; echo $langmessage['search_no_results']; echo '</p>'; return false; } //save the cache if (!$cache_used) { \gp\tool\Files::Save($cache_file, $result); } return $data; }