/**
  * create a /cache/appStart-unminified.js file. Grunt will handle the renaming of the file and the generation
  * of the /cache/minifiedResourcePaths.php file.
  */
 public static function createAppStartFile()
 {
     $exportTypes = Core::$exportTypePlugins;
     $exportTypeJSModules = ExportTypePluginHelper::getExportTypeJSResources($exportTypes, "string");
     $dataTypes = DataTypePluginHelper::getDataTypeList(Core::$dataTypePlugins);
     $dataTypeJSModules = DataTypePluginHelper::getDataTypeJSResources($dataTypes, "string");
     $js = 'require(["manager","generator","accountManager",' . $exportTypeJSModules . "," . $dataTypeJSModules . ',"pageInit"], function(manager) {manager.start(); });';
     $file = realpath(__DIR__ . "/../../cache/") . "/appStartGenerated.js";
     if (is_file($file)) {
         unlink($file);
     }
     if (file_put_contents($file, $js)) {
         return true;
     } else {
         return false;
     }
 }
Beispiel #2
0
 /**
  * Called by Core::init(), this initializes Core::$exportTypePlugins.
  * @access private
  */
 private static function initExportTypes($runtimeContext)
 {
     if (!Core::$settingsFileExists) {
         return;
     }
     self::$exportTypePlugins = ExportTypePluginHelper::getExportTypePlugins($runtimeContext);
 }
 private function setExportTypes()
 {
     $index = $this->post["index"];
     $exportTypes = ExportTypePluginHelper::getExportTypePlugins("installationDatabaseReady", false);
     if ($index >= count($exportTypes)) {
         $this->response["success"] = 1;
         $this->response["content"] = "";
         $this->response["isComplete"] = true;
     } else {
         // attempt to install this export type
         $currExportType = $exportTypes[$index];
         $this->response["exportTypeName"] = $currExportType->getName();
         $this->response["exportTypeFolder"] = $currExportType->folder;
         $this->response["isComplete"] = false;
         try {
             list($success, $content) = $currExportType->install();
             $this->response["success"] = $success;
             $this->response["content"] = $content;
         } catch (Exception $e) {
             $this->response["success"] = false;
             $this->response["content"] = "Unknown error.";
         }
     }
 }
Beispiel #4
0
$cssIncludes = $exportTypeCssIncludes . "\n" . $dataTypeCssIncludes;
// used in the settings page
$pageParams["allCountryPlugins"] = Core::$countryPlugins;
$pageParams["allExportTypes"] = $exportTypes;
$pageParams["groupedDataTypes"] = Core::$dataTypePlugins;
$pageParams["allDataTypes"] = $dataTypes;
$pageParams["allTranslations"] = Core::$translations->getList();
$useMinifiedResources = Core::isUsingMinifiedResources();
$pageParams["useMinifiedResources"] = $useMinifiedResources;
if ($useMinifiedResources) {
    $pageParams["minifiedResourcePaths"] = Minification::getMinifiedResourcePaths();
}
$pageParams["dataTypeJSModules"] = $dataTypeJSModules;
$pageParams["exportTypeJSModules"] = $exportTypeJSModules;
$pageParams["exportTypeAdditionalSettings"] = $exportTypeAdditionalSettings;
$pageParams["settings"] = $settings;
$pageParams["cssIncludes"] = $cssIncludes;
$pageParams["codeMirrorIncludes"] = ExportTypePluginHelper::getExportTypeCodeMirrorModes($exportTypes);
$pageParams["defaultExportType"] = Core::$user->getDefaultExportType();
$pageParams["defaultNumRows"] = Core::getDefaultNumRows();
if (Core::checkIsLoggedIn()) {
    $pageParams["isLoggedIn"] = true;
    $pageParams["accountType"] = Core::$user->getAccountType();
    $pageParams["selectedDataTypes"] = Core::$user->getSelectedDataTypes();
    $pageParams["selectedExportTypes"] = Core::$user->getSelectedExportTypes();
    $pageParams["selectedCountries"] = Core::$user->getSelectedCountries();
} else {
    $pageParams["isLoggedIn"] = false;
    $pageParams["accountType"] = "";
}
Templates::displayPage("resources/templates/index.tpl", $pageParams);
 /**
  * AjaxRequest objects are automatically processed when they are created, based on the unique $action
  * value. The result of the call is stored in $response to be handled however you need (e.g. output
  * as JSON, XML etc) - or an Exception is thrown if something went wrong. Exceptions are used SOLELY for
  * program errors: not for user-entry errors.
  */
 public function __construct($action, $post = array())
 {
     if (empty($action)) {
         throw new Exception("no_action_specified");
         return;
     }
     $this->action = $action;
     $post = Utils::sanitize($post);
     switch ($this->action) {
         // ------------------------------------------------------------------------------------
         // INSTALLATION
         // ------------------------------------------------------------------------------------
         // a fresh install assumes it's a blank slate: no database tables, no settings file
         case "installationTestDbSettings":
             Core::init("installation");
             list($success, $content) = Database::testDbSettings($post["dbHostname"], $post["dbName"], $post["dbUsername"], $post["dbPassword"]);
             $this->response["success"] = $success;
             $this->response["content"] = $content;
             break;
         case "installationCreateSettingsFile":
             Core::init("installation");
             if (Core::checkSettingsFileExists()) {
                 $this->response["success"] = 0;
                 $this->response["content"] = "Your settings.php file already exists.";
                 return;
             } else {
                 list($success, $content) = Installation::createSettingsFile($post["dbHostname"], $post["dbName"], $post["dbUsername"], $post["dbPassword"], $post["dbTablePrefix"]);
                 $this->response["success"] = $success;
                 $this->response["content"] = $content;
             }
             break;
         case "installationCreateDatabase":
             Core::init("installation_db_ready");
             list($success, $content) = Installation::createDatabase();
             if (!$success) {
                 $this->response["success"] = 0;
                 $this->response["content"] = $content;
                 return;
             }
             // always create the administrator account. If the user chose the anonymous setup, all values
             // will be blank and all configurations will be associated with this (anonymous) user
             $adminAccount = array("accountType" => "admin", "firstName" => $post["firstName"], "lastName" => $post["lastName"], "email" => $post["email"], "password" => $post["password"]);
             Account::createAccount($adminAccount);
             // make note of the fact that we've passed this installation step
             Settings::setSetting("userAccountSetup", $post["userAccountSetup"]);
             Settings::setSetting("installationStepComplete_Core", "yes");
             $this->response["success"] = 1;
             $this->response["content"] = "";
             break;
         case "installationDataTypes":
             Core::init("installation_db_ready");
             $index = $post["index"];
             $groupedDataTypes = DataTypePluginHelper::getDataTypePlugins("installion_db_ready", false);
             $dataTypes = DataTypePluginHelper::getDataTypeList($groupedDataTypes);
             if ($index >= count($dataTypes)) {
                 $this->response["success"] = 1;
                 $this->response["content"] = "";
                 $this->response["isComplete"] = true;
             } else {
                 // attempt to install this data type
                 $currDataType = $dataTypes[$index];
                 $this->response["dataTypeName"] = $currDataType->getName();
                 $this->response["dataTypeFolder"] = $currDataType->folder;
                 $this->response["isComplete"] = false;
                 try {
                     list($success, $content) = $currDataType->install();
                     $this->response["success"] = $success;
                     $this->response["content"] = $content;
                 } catch (Exception $e) {
                     $this->response["success"] = false;
                     $this->response["content"] = "Unknown error.";
                 }
             }
             break;
         case "installationSaveDataTypes":
             Core::init("installation_db_ready");
             $folders = $post["folders"];
             $response = Settings::setSetting("installedDataTypes", $folders);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["errorMessage"];
             break;
         case "installationExportTypes":
             Core::init("installation_db_ready");
             $index = $post["index"];
             $exportTypes = ExportTypePluginHelper::getExportTypePlugins("installation_db_ready", false);
             if ($index >= count($exportTypes)) {
                 $this->response["success"] = 1;
                 $this->response["content"] = "";
                 $this->response["isComplete"] = true;
             } else {
                 // attempt to install this data type
                 $currExportType = $exportTypes[$index];
                 $this->response["exportTypeName"] = $currExportType->getName();
                 $this->response["exportTypeFolder"] = $currExportType->folder;
                 $this->response["isComplete"] = false;
                 try {
                     list($success, $content) = $currExportType->install();
                     $this->response["success"] = $success;
                     $this->response["content"] = $content;
                 } catch (Exception $e) {
                     $this->response["success"] = false;
                     $this->response["content"] = "Unknown error.";
                 }
             }
             break;
         case "installationSaveExportTypes":
             Core::init("installation_db_ready");
             $folders = $post["folders"];
             $response = Settings::setSetting("installedExportTypes", $folders);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["errorMessage"];
             break;
         case "installationCountries":
             Core::init("installation_db_ready");
             $index = $post["index"];
             $countryPlugins = CountryPluginHelper::getCountryPlugins(false);
             if ($index >= count($countryPlugins)) {
                 $this->response["success"] = 1;
                 $this->response["content"] = "";
                 $this->response["isComplete"] = true;
             } else {
                 // attempt to install this data type
                 $currCountryPlugin = $countryPlugins[$index];
                 $this->response["countryName"] = $currCountryPlugin->getName();
                 $this->response["countryFolder"] = $currCountryPlugin->folder;
                 $this->response["isComplete"] = false;
                 try {
                     // always run the uninstallation function first to ensure any old data is all cleared out
                     $currCountryPlugin->uninstall();
                     list($success, $content) = $currCountryPlugin->install();
                     $this->response["success"] = $success;
                     $this->response["content"] = $content;
                 } catch (Exception $e) {
                     $this->response["success"] = false;
                     $this->response["content"] = "Unknown error.";
                 }
             }
             break;
         case "installationSaveCountries":
             Core::init("installation_db_ready");
             $folders = $post["folders"];
             $response = Settings::setSetting("installedCountries", $folders);
             $response = Settings::setSetting("installationComplete", "yes");
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["errorMessage"];
             break;
         case "generateInPage":
             Core::init("generation");
             $gen = new Generator($_POST);
             $response = $gen->generate();
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["content"];
             $this->response["isComplete"] = $response["isComplete"];
             break;
             // ------------------------------------------------------------------------------------
             // USER ACCOUNTS
             // ------------------------------------------------------------------------------------
         // ------------------------------------------------------------------------------------
         // USER ACCOUNTS
         // ------------------------------------------------------------------------------------
         case "getAccount":
             Core::init();
             $response = Core::$user->getAccount();
             $this->response["success"] = true;
             $this->response["content"] = $response;
             break;
         case "getUsers":
             Core::init();
             $response = Core::$user->getUsers();
             $this->response["success"] = $response["success"];
             if (isset($response["accounts"])) {
                 $this->response["content"] = $response["accounts"];
             }
             break;
         case "createAccount":
             Core::init();
             if (!Core::checkIsLoggedIn()) {
                 $this->response["success"] = false;
                 $this->response["errorCode"] = ErrorCodes::NOT_LOGGED_IN;
             } else {
                 if (Core::$user->getAccountType() != "admin") {
                     $this->response["success"] = false;
                     $this->response["errorCode"] = ErrorCodes::NON_ADMIN;
                 } else {
                     $accountInfo = $post;
                     $accountInfo["accountType"] = "user";
                     $response = Account::createAccount($accountInfo);
                     $this->response["success"] = true;
                 }
             }
             break;
         case "deleteAccount":
             Core::init();
             if (!Core::checkIsLoggedIn()) {
                 $this->response["success"] = false;
                 $this->response["errorCode"] = ErrorCodes::NOT_LOGGED_IN;
             } else {
                 if (Core::$user->getAccountType() != "admin") {
                     $this->response["success"] = false;
                     $this->response["errorCode"] = ErrorCodes::NON_ADMIN;
                 } else {
                     $accountID = $post["accountID"];
                     $response = Core::$user->deleteAccount($accountID);
                     $this->response["success"] = true;
                 }
             }
             break;
             // updates the current logged in user's info
         // updates the current logged in user's info
         case "updateAccount":
             Core::init();
             if (!Core::checkIsLoggedIn()) {
                 $this->response["success"] = false;
                 $this->response["errorCode"] = ErrorCodes::NOT_LOGGED_IN;
             } else {
                 if (Core::$user->isAnonymous()) {
                     $this->response["success"] = false;
                     $this->response["errorCode"] = ErrorCodes::INVALID_REQUEST;
                 } else {
                     $accountID = $post["accountID"];
                     $this->response = Core::$user->updateAccount($accountID, $post);
                 }
             }
             break;
         case "saveConfiguration":
             Core::init();
             $response = Core::$user->saveConfiguration($post);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["message"];
             if (isset($response["lastUpdated"])) {
                 $this->response["lastUpdated"] = $response["lastUpdated"];
             }
             break;
         case "deleteDataSets":
             Core::init();
             $configurationIDs = $post["configurationIDs"];
             $response = Core::$user->deleteConfigurations($configurationIDs);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["message"];
             break;
         case "saveDataSetVisibilityStatus":
             Core::init();
             $configurationID = $post["configurationID"];
             $status = $post["status"];
             $time = $post["time"];
             $response = Core::$user->saveDataSetVisibilityStatus($configurationID, $status, $time);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["message"];
             if (isset($response["newStatus"])) {
                 $this->response["newStatus"] = $response["newStatus"];
             }
             break;
         case "getPublicDataSet":
             Core::init();
             $configurationID = $post["dataSetID"];
             $response = Core::$user->getPublicDataSet($configurationID);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["message"];
             break;
         case "login":
             Core::init();
             $email = $post["email"];
             $password = $post["password"];
             $response = Account::login($email, $password);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["message"];
             break;
             // for single
         // for single
         case "logout":
             Core::init();
             if (!Core::checkIsLoggedIn()) {
                 $this->response["success"] = true;
             } else {
                 if (!Core::$user->isAnonymous()) {
                     Core::$user->logout();
                     $this->response["success"] = true;
                 }
             }
             break;
         case "resetPassword":
             Core::init();
             $email = $post["email"];
             $response = Account::resetPassword($email);
             $this->response["success"] = $response["success"];
             $this->response["content"] = $response["message"];
             break;
     }
 }
 private function installExportTypes()
 {
     $exportTypes = ExportTypePluginHelper::getExportTypePlugins("installationDatabaseReady", false);
     $hasError = false;
     $results = array();
     $count = 0;
     $folders = array();
     foreach ($exportTypes as $currExportType) {
         try {
             list($success, $content) = $currExportType->install();
             if (!$success) {
                 $hasError = true;
                 break;
             }
             $folder = $currExportType->getFolder();
             $results[] = array("name" => $currExportType->getName(), "folder" => $folder);
             $folders[] = $folder;
             $count++;
         } catch (Exception $e) {
             $hasError = true;
             break;
         }
     }
     // need error handling here
     Settings::setSetting("installedExportTypes", implode(",", $folders));
     $this->response["success"] = !$hasError;
     $this->response["content"] = array("total" => $count, "results" => $results);
 }
 /**
  * Called during an API request for data generation. This does the same thing as initUIGenerator: it pulls the
  * core values out and stores them on the current DataGenerator object. The only difference is the source: this
  * pulls them from the JSON content POSTed, rather than relies on the browser form POST used by the UI.
  * @param $json
  */
 private function initAPIGenerator($json)
 {
     $this->exportTarget = "newTab";
     // should be a constant
     $this->batchSize = $json->numRows;
     $this->batchNum = 1;
     $this->numResults = $json->numRows;
     $this->applyRowsGeneratedLimit();
     $this->countries = property_exists($json, "countries") ? $json->countries : array();
     $this->apiData = $json;
     if (isset($postData["configurationID"])) {
         $this->configurationID = $postData["configurationID"];
     }
     // make a note of whether this batch is the first / last. This is useful information for the
     // Export Types to know whether to output special content at the top or bottom
     $this->isFirstBatch = true;
     $this->isLastBatch = true;
     $this->currentBatchFirstRow = 1;
     $this->currentBatchLastRow = $this->numResults;
     // figure out what we're going to need to generate
     $this->createDataSetTemplateAPI($json);
     $this->exportType = ExportTypePluginHelper::getExportTypeByFolder($json->export->type);
     $this->isCompressionRequired = false;
 }
Beispiel #8
0
 /**
  * Examines the contents of the `export` property, which is an object of the following form:
  * { type: "N", settings: {} }. "N" is the folder name of the desired Export Type (see
  * /plugins/axportTypes); `settings` is an object containing whatever settings have been specified by that
  * Export Type's schema.json file.
  * @param $json
  * @return array
  */
 private function validateExportTypeSettings($json)
 {
     // first, find the Export Type and check it's valid
     if (!property_exists($json->export, "type")) {
         return array("error" => ErrorCodes::API_INVALID_JSON_CONTENT, "error_details" => "Missing `type` property in the `export` object");
     }
     $exportTypeFolders = ExportTypePluginHelper::getExportTypeFolders();
     $exportType = $json->export->type;
     if (!in_array($exportType, $exportTypeFolders)) {
         return array("error" => ErrorCodes::API_UNKNOWN_EXPORT_TYPE, "error_details" => "invalid `type` attribute of the `export` object: `{$exportType}`");
     }
     $exportType = ExportTypePluginHelper::getExportTypeByFolder($exportType);
     $schema = json_decode($exportType->getSchema());
     // only validate if there's actually a schema for this Export Type
     if ($schema !== null) {
         $json = property_exists($json->export, "settings") ? $json->export->settings : json_decode("{}");
         // verify the settings for this data type
         $result = Jsv4::validate($json, $schema);
         if (!$result->valid) {
             return array("error" => ErrorCodes::API_INVALID_EXPORT_TYPE_JSON, "error_details" => "Invalid Export Type JSON `settings` content passed", "path" => $result->errors[0]->dataPath, "validation_error" => $result->errors[0]->message);
         }
     }
 }