Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 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"));
         }
     }
 }