Example #1
0
 public function createRequest()
 {
     $uri = $_SERVER['REQUEST_URI'];
     $basePath = $this->configuration->getBasePath();
     if ($basePath && strncmp($uri, $basePath, strlen($basePath)) !== 0) {
         throw new ApiException("Invalid endpoint");
     }
     $uri = substr($uri, strlen($basePath) - 1);
     if ($this->configuration->getPublicKey() !== trim($_SERVER['HTTP_X_API_KEY'])) {
         throw new AuthorizationException("Invalid API key");
     }
     $hasBody = $this->hasBody();
     $input = $hasBody ? file_get_contents('php://input') : '';
     $signature = hash_hmac('sha256', $uri . $input, $this->configuration->getPrivateKey());
     if ($signature !== trim($_SERVER['HTTP_X_API_SIGNATURE'])) {
         throw new AuthorizationException("Invalid signature");
     }
     if ($hasBody) {
         $parameters = json_decode($input, JSON_OBJECT_AS_ARRAY);
         if ($parameters === NULL && $input !== '' && strcasecmp(trim($input, " \t\n\r"), 'null') !== 0) {
             $error = json_last_error();
             throw new ApiException('JSON parsing error: ' . $error);
         }
     } else {
         $parameters = filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW);
     }
     $name = ($a = strpos($uri, '?')) !== FALSE ? substr($uri, 0, $a) : $uri;
     return new Request(ltrim($name, '/'), $_SERVER['REQUEST_METHOD'], $parameters);
 }
 /**
  * Deletes old versions that were never active
  */
 public static function deleteOldVersions()
 {
     $config = Configuration::getAppConfig();
     $dbObj = DBPool::getInstance();
     $maxVersionCount = $config['max version count'];
     $minVersionCount = $config['min version count'];
     if ($maxVersionCount <= $dbObj->get_var("SELECT COUNT(1) FROM version WHERE active=false AND was_active=false")) {
         $oldVersionsArray = array();
         $oldVersions = $dbObj->get_results("SELECT id FROM version\n                            WHERE active=false AND was_active=false\n                            ORDER by id\n                            LIMIT " . ($maxVersionCount - $minVersionCount));
         foreach ($oldVersions as $o) {
             $oldVersionsArray[] = $o->id;
         }
         if (count($oldVersionsArray) > 0) {
             $query = "DELETE FROM version WHERE id IN (" . implode(",", $oldVersionsArray) . ")";
             $dbObj->query($query);
             //Delete data from the disk
             foreach ($oldVersionsArray as $version) {
                 $dataDirPath = Configuration::getBasePath() . "www/data/" . Util::XML_FILE . "/{$version}";
                 $imageDirPath = Configuration::getBasePath() . "www/data/" . Util::IMAGE_FILE . "/{$version}";
                 $changeDirPath = Configuration::getBasePath() . "www/data/" . Util::CHANGE_FILE . "/{$version}";
                 Util::deleteAll($dataDirPath);
                 Util::deleteAll($imageDirPath);
                 Util::deleteAll($changeDirPath);
             }
             $logger = Logger::getInstance();
             $logStr = "Deleted versions " . implode(",", $oldVersionsArray);
             $logger->log($logStr, Logger::INFO, Version::PACKAGE);
         }
     }
 }
 public static function getBaseDirectoryPath($type)
 {
     $basePath = Configuration::getBasePath();
     $baseDirPath = $basePath . "www/data/" . $type . "/" . TableUpdate::getVersion() . "/";
     Util::createDir($baseDirPath);
     return $baseDirPath;
 }
 public static function updateContents($fileType, $xmlStr)
 {
     try {
         $xml = new SimpleXMLElement($xmlStr);
     } catch (Exception $ex) {
         throw new Exception("Invalid XML!");
     }
     $filePath = Configuration::getBasePath() . "files/" . $fileType . ".xml";
     return file_put_contents($filePath, $xmlStr);
 }