/** * @expectedException malkusch\bav\FieldException */ public function testInvalidFirstLineContent() { $backend = new FileDataBackend(); $file = $backend->getFile(); $invalidFile = __DIR__ . "/../data/invalidFirstLineContent.txt"; copy($file, $invalidFile); $fp = fopen($invalidFile, "c"); fputs($fp, "XXX"); $validator = new FileValidator(); $validator->validate($invalidFile); }
public function validate() { parent::validate(); if (!$this->type) { $this->setError(100); } if (!is_null($this->minWidth) && $this->width < $this->minWidth) { $this->setError(101, ['width' => $this->width, 'min-width' => $this->minWidth]); } if (!is_null($this->minHeight) && $this->height < $this->minHeight) { $this->setError(102, ['height' => $this->height, 'min-height' => $this->minHeight]); } if (!is_null($this->maxWidth) && $this->width > $this->maxWidth) { $this->setError(103, ['width' => $this->width, 'max-width' => $this->maxWidth]); } if (!is_null($this->maxHeight) && $this->height > $this->maxHeight) { $this->setError(104, ['height' => $this->height, 'max-height' => $this->maxHeight]); } return empty($this->getErrors()); }
/** * @test * @expectedException RuntimeException */ public function throwRuntimeExceptionOnWriteIfPathIsNotWritable() { $this->touchUnwritableFile(); FileValidator::canWrite($this->unwritablePath, $this->throwException); }
public function ajax_importnext() { $time_start = microtime(true); $response["error"] = "none"; $response["file"] = "none"; if (file_exists($_POST["path"])) { $response["file"] = $_POST["path"]; if (USE_DB) { $f = $_POST["path"]; global $g_creationDate; $g_creationDate = intval($_POST["timestamp"]); // bad style, but so much easier $hash_md5 = md5_file($f); $hash_alpha = base_convert($hash_md5, 16, 36); // shorten hash to shorten urls (better looking, less bandwidth) while (strlen($hash_alpha) < 25) { $hash_alpha = '0' . $hash_alpha; } $history = new History(); $themeInfo = $history->loadThemeFromHash($hash_alpha); if (empty($themeInfo)) { $path_parts = pathinfo($f); $path_item = $path_parts['dirname']; $filename = $path_parts['filename'] . '.' . $path_parts['extension']; $themeInfo = FileValidator::prepareThemeInfo($path_item . '/' . $filename, $filename, 'application/zip', false); if (!empty($themeInfo)) { $this->fileValidator = new FileValidator($themeInfo); $this->fileValidator->validate(); if (UserMessage::getCount(ERRORLEVEL_FATAL) > 0) { $response["error"] = "fatal error:\n"; foreach (UserMessage::getMessages(ERRORLEVEL_FATAL) as $m) { $response["error"] .= "\n" . $m; } } else { if ($this->fileValidator->serialize()) { if (UserMessage::getCount(ERRORLEVEL_FATAL) > 0) { // at least one error occured while serializing (no thumbnail...) $response["error"] = "fatal error, could not serialize validation results:\n"; foreach (UserMessage::getMessages(ERRORLEVEL_FATAL) as $m) { $response["error"] .= "\n" . $m; } foreach (UserMessage::getMessages(ERRORLEVEL_CRITICAL) as $m) { $response["error"] .= "\n" . $m; } } else { $this->validationResults = $this->fileValidator->getValidationResults(I18N::getCurLang()); $themeInfo = $this->fileValidator->themeInfo; $response["themeinfo"] = $themeInfo; } } else { // at least one error occured while serializing (no thumbnail...) if (UserMessage::getCount(ERRORLEVEL_CRITICAL) > 0) { $response["error"] = "could not serialize validation results"; } foreach (UserMessage::getMessages(ERRORLEVEL_CRITICAL) as $m) { $response["error"] .= "\n" . $m; } } } } else { if (UserMessage::getCount(ERRORLEVEL_FATAL) > 0) { // at least one error occured while serializing (no thumbnail...) $response["error"] = "could not execute validation:\n"; foreach (UserMessage::getMessages(ERRORLEVEL_FATAL) as $m) { $response["error"] .= "\n" . $m; } foreach (UserMessage::getMessages(ERRORLEVEL_CRITICAL) as $m) { $response["error"] .= "\n" . $m; } } else { $response["error"] = "could not execute validation (unknown error)."; } } } } } $time_end = microtime(true); $time = $time_end - $time_start; $response["duration"] = $time; //ob_clean(); header('Content-Type: application/json'); echo json_encode($response); }
/** * Restore check results from a JSON file. **/ public static function unserialize($hash) { if (!USE_DB) { return null; } $directory = ThemeInfo::getReportDirectory($hash); if (!file_exists($directory)) { return null; } $history = new History(); $themeInfo = $history->loadThemeFromHash($hash); if (empty($themeInfo)) { return null; } $fileValidator = new FileValidator($themeInfo); global $ExistingLangs; foreach ($ExistingLangs as $l) { $_validationResults = ValidationResults::unserialize($hash, $l); if (empty($_validationResults)) { continue; } $fileValidator->validationResults[$l] = $_validationResults; } if (!empty($themeInfo->parentId)) { $fewInfo = $history->getFewInfo($themeInfo->parentId); if (!empty($fewInfo["id"])) { $themeInfo->parentNameSanitized = $fewInfo["namesanitized"]; } $themeInfo->parentThemeType = $fewInfo["themetype"]; } if ($themeInfo->isThemeForest) { $fileValidator->generateThemeForestReport(); } return $fileValidator; }
public function ajax_sample() { $time_start = microtime(true); $response["error"] = "none"; $response["html"] = ""; $themeid = 1; if (isset($_POST["themeid"])) { $themeid = intval($_POST["themeid"]); } if ($themeid < 1) { $themeid = 1; } $checkid = $_POST["checkid"]; if (USE_DB) { $history = new History(); $themInfo = $history->getFewInfo($themeid); $hash = $themInfo["hash"]; $fileValidator = FileValidator::unserialize($hash); $fileValidator->validate($checkid); //if (UserMessage::getCount(ERRORLEVEL_FATAL) == 0) // serialize only if no fatal errors $validationResults = $fileValidator->getValidationResults(I18N::getCurLang()); if (count($validationResults->check_critical) > 0 || count($validationResults->check_warnings) > 0 || count($validationResults->check_info) > 0) { $url = TC_HTTPDOMAIN . '/' . Route::getInstance()->assemble(array("lang" => "en", "phpfile" => "results", "hash" => $hash)); $html = '<h2 style="color:#D00;">' . $themInfo["name"] . '<a href="' . $url . '" target="_blank" style="font-size:14px;margin-left:6px"><span class="glyphicon glyphicon-new-window"></span></a>' . '</h2>'; } if (count($validationResults->check_critical) > 0) { //$html .= '<h2 style="line-height:100px;color:#D00;">'.__("Critical alerts").'</h2>'; $html .= '<ol>'; foreach ($validationResults->check_critical as $check) { $html .= '<h4 style="color:#666;margin-top:40px;"><li>' . $check->title . ' : ' . $check->hint . '</li></h4>'; if (!empty($check->messages)) { $html .= '<p style="color:#c94b4b;">' . implode('<br/>', $check->messages) . '</p>'; } } $html .= '</ol>'; } if (count($validationResults->check_warnings) > 0) { //$html .= '<h2 style="line-height:100px;color:#eea43a;">'.__("Warnings").'</h2>'; $html .= '<ol>'; foreach ($validationResults->check_warnings as $check) { $html .= '<h4 style="color:#666;margin-top:40px;"><li>' . $check->title . ' : ' . $check->hint . '</li></h4>'; if (!empty($check->messages)) { $html .= '<p style="color:#eea43a;">' . implode('<br/>', $check->messages) . '</p>'; } } $html .= '</ol>'; } if (count($validationResults->check_info) > 0) { //$html .= '<h2 style="line-height:100px;color:#eea43a;">'.__("Warnings").'</h2>'; $html .= '<ol>'; foreach ($validationResults->check_info as $check) { $html .= '<h4 style="color:#666;margin-top:40px;"><li>' . $check->title . ' : ' . $check->hint . '</li></h4>'; if (!empty($check->messages)) { $html .= '<p style="color:#00b6e3;">' . implode('<br/>', $check->messages) . '</p>'; } } $html .= '</ol>'; } $response["html"] = $html; $prevId = $history->getPrevId($themeid); if (!empty($prevId)) { $themInfoNext = $history->getFewInfo($prevId); $response["next_id"] = $prevId; $response["next_name"] = $themInfoNext["name"]; } else { $response["next_id"] = null; $response["next_name"] = null; } } $time_end = microtime(true); $time = $time_end - $time_start; $response["duration"] = $time; //ob_clean(); header('Content-Type: application/json'); echo json_encode($response); }
public function prepare() { $routeParts = Route::getInstance()->match(); // There are 2 types of results to display // 1 - Display an already evaluated file which results were stored on the server. Just need the id. e.g : results?id=162804c3c358267d3a16855686ab1887 // 2 - Unknown file. Need $_FILES and $_POST["filetype"] if (isset($routeParts["ut"])) { $path_item = TC_ROOTDIR . '/include/unittests/'; $filename = urldecode($routeParts["ut"]); if (!(substr($filename, -4) == ".zip" && file_exists($path_item . $filename))) { echo $path_item . $filename . ' does not exist. Cannot continue'; die; } $themeInfo = FileValidator::prepareThemeInfo($path_item . $filename, $filename, 'application/zip', false); $this->fileValidator = new FileValidator($themeInfo); $this->fileValidator->validate(); $this->validationResults = $this->fileValidator->getValidationResults(I18N::getCurLang()); } else { if (isset($routeParts["hash"])) { $hash = $routeParts["hash"]; $this->fileValidator = FileValidator::unserialize($hash); $themeInfo = $this->fileValidator->themeInfo; $checkfiles = scandir(TC_INCDIR . '/Checks'); $youngestCheckTimestamp = 0; foreach ($checkfiles as $f) { if ($f == '.' || $f == '..') { continue; } $m = filemtime(TC_INCDIR . '/Checks/' . $f); if ($youngestCheckTimestamp < $m) { $youngestCheckTimestamp = $m; } } if ($this->fileValidator->themeInfo->validationDate < $youngestCheckTimestamp) { $this->fileValidator->validate(); if (UserMessage::getCount(ERRORLEVEL_FATAL) == 0) { // serialize only if no fatal errors $this->fileValidator->serialize(true); } } $this->validationResults = $this->fileValidator->getValidationResults(I18N::getCurLang()); } else { if (count($_FILES) > 0 && isset($_FILES["file"]) && !empty($_FILES["file"]["name"])) { if (TC_ENVIRONMENT == "dev" || isset($_SESSION['token_' . $_POST['token']])) { unset($_SESSION['token_' . $_POST['token']]); $themeInfo = FileValidator::upload(); if ($themeInfo) { $this->fileValidator = new FileValidator($themeInfo); $this->fileValidator->validate(); if (isset($_POST["donotstore"]) || UserMessage::getCount(ERRORLEVEL_FATAL) > 0) { $this->fileValidator->clean(); } else { $this->fileValidator->serialize(true); } $this->validationResults = $this->fileValidator->getValidationResults(I18N::getCurLang()); if (isset($_POST["donotstore"])) { $this->inlinescripts[] = "ga('send', 'event', 'theme', 'submit', 'not stored');"; } else { $this->inlinescripts[] = "ga('send', 'event', 'theme', 'submit', 'stored');"; } } } else { UserMessage::enqueue(__("Unvalid form"), ERRORLEVEL_FATAL); } } else { UserMessage::enqueue(__("No file uploaded."), ERRORLEVEL_FATAL); $this->meta["title"] = __("No file uploaded"); $this->meta["description"] = __("No file uploaded"); return; } } } if (!empty($themeInfo)) { if ($themeInfo->themetype == TT_JOOMLA) { $this->meta["title"] = sprintf(__("%s%% : Joomla template %s"), htmlspecialchars($themeInfo->score), htmlspecialchars($themeInfo->name)); $this->meta["description"] = sprintf(__("Security and code quality score of Joomla template %s."), htmlspecialchars($themeInfo->name)); } else { $this->meta["title"] = sprintf(__("%s%% : Wordpress theme %s"), htmlspecialchars($themeInfo->score), htmlspecialchars($themeInfo->name)); $this->meta["description"] = sprintf(__("Security and code quality score of Wordpress theme %s."), htmlspecialchars($themeInfo->name)); } if ($themeInfo->score < 100.0) { if ($themeInfo->score > 95) { $this->meta["favicon"] = "favicon100"; } else { if ($themeInfo->score > 80) { $this->meta["favicon"] = "favicon95"; } else { $this->meta["favicon"] = "favicon80"; } } } } else { $this->meta["title"] = __("Check results"); $this->meta["description"] = __("Security and code quality score"); } global $ExistingLangs; foreach ($ExistingLangs as $l) { if ($this->fileValidator) { $themeInfo = $this->fileValidator->themeInfo; if (!empty($themeInfo) && $themeInfo->serializable && USE_DB) { $this->samepage_i18n[$l] = TC_HTTPDOMAIN . '/' . Route::getInstance()->assemble(array("lang" => $l, "phpfile" => "results", "hash" => $themeInfo->hash)); } else { $this->samepage_i18n[$l] = null; } } else { $this->samepage_i18n[$l] = TC_HTTPDOMAIN . '/' . Route::getInstance()->assemble(array("lang" => $l, "phpfile" => "results")); } } }
/** * Return whether the file is writable. * * @return boolean true if the file is writable. * @throws \RuntimeException Throw if the file is not writable and $throwException is set to true. */ public function isWritable() { return FileValidator::canWrite($this->path, $this->throwException); }