PDO::ERRMODE_EXCEPTION)); } catch(PDOException $e) { Error("Could not connect to database.", $e->getMessage(), 501); } if(!isset($_POST["apiKey"])) // Throw this message as an error so that if this is encountered via ES3Cloud it is put into an error state. Error("ES3Cloud is functioning correctly.", "ES3Cloud is functioning correctly.", 403); if($_POST["apiKey"] != $api_key) Error("Incorrect API Key", "Incorrect API Key", 403); // ----- GET FILE ----- if(isset($_POST["getFile"])) { $stmt = $db->prepare("SELECT $fileDataField FROM $tableName WHERE $filenameField = :filename AND $userField = :user AND $lastUpdatedField > :timestamp LIMIT 1"); $stmt->bindParam(":filename", $_POST["getFile"]); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $postTimestamp = GetPOSTTimestamp(); $stmt->bindParam(":timestamp", $postTimestamp); $stmt->execute(); if($stmt->rowCount() > 0) { $data = $stmt->fetchColumn(); // Manually set the content length so WWW.progress works. header($_SERVER["SERVER_PROTOCOL"] . " 200 OK"); header("Cache-Control: public"); header("Content-Type: application/octet-stream"); header("Content-Transfer-Encoding: Binary"); header("Content-Length:".strlen($data)); echo $data; } } // ----- PUT FILE ----- else if(isset($_POST["putFile"])) { // Get uploaded data. $filePath = $_FILES["data"]["tmp_name"]; // If file doesn't exist or it contains no data, throw an error. if(!file_exists($filePath)) Error("Uploaded file does not exist.", "Uploaded file does not exist.", 400); // If file doesn't exist or it contains no data, throw an error. if(filesize($filePath) == 0) Error("Uploaded file is empty.", "Uploaded file is empty.", 400); $fp = fopen($filePath, 'rb'); $stmt = $db->prepare("INSERT INTO $tableName ($filenameField, $fileDataField, $userField, $lastUpdatedField) VALUES (:filename, :data, :user, :timestamp) ON DUPLICATE KEY UPDATE $fileDataField = VALUES($fileDataField), $lastUpdatedField = VALUES($lastUpdatedField)"); $stmt->bindParam(":filename", $_POST["putFile"]); $stmt->bindParam(":data", $fp, PDO::PARAM_LOB); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $postTimestamp = GetPOSTTimestamp(); $stmt->bindParam(":timestamp", $postTimestamp); $stmt->execute(); } // ----- RENAME FILE ----- else if(isset($_POST["renameFile"])) { $stmt = $db->prepare("UPDATE $tableName SET $filenameField = :newFilename WHERE $filenameField = :filename AND $userField = :user"); $stmt->bindParam(":filename", $_POST["renameFile"]); $stmt->bindParam(":newFilename", $_POST["newFilename"]); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $stmt->execute(); } // ----- DELETE FILE ----- else if(isset($_POST["deleteFile"])) { $stmt = $db->prepare("DELETE FROM $tableName WHERE $filenameField = :filename AND $userField = :user"); $stmt->bindParam(":filename", $_POST["deleteFile"]); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $stmt->execute(); } // ----- GET FILENAMES WITH PATTERN ----- else if(isset($_POST["getFilenames"]) && isset($_POST["pattern"])) { echo "Here"; $stmt = $db->prepare("SELECT $filenameField FROM $tableName WHERE $userField = :user AND $filenameField LIKE :pattern"); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $stmt->bindParam(":pattern", $_POST["pattern"]); $stmt->execute(); $rows = $stmt->fetchAll(); foreach($rows as $row) echo $row[$filenameField] . ";"; } // ----- GET FILENAMES ----- else if(isset($_POST["getFilenames"])) { $stmt = $db->prepare("SELECT $filenameField FROM $tableName WHERE $userField = :user"); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $stmt->execute(); $rows = $stmt->fetchAll(); foreach($rows as $row) echo $row[$filenameField] . ";"; } // ----- GET TIMESTAMP ----- else if(isset($_POST["getTimestamp"])) { $stmt = $db->prepare("SELECT $lastUpdatedField FROM $tableName WHERE $filenameField = :filename AND $userField = :user LIMIT 1"); $stmt->bindParam(":filename", $_POST["getTimestamp"]); $postUser = GetPOSTUser(); $stmt->bindParam(":user", $postUser); $stmt->execute(); if($stmt->rowCount() > 0) echo $stmt->fetchColumn(); else Error("Could not get timestamp as file does not exist.", "Could not get timestamp as file does not exist.", 400); } else Error("No valid operation was specified", "No valid operation was specified", 400); // Close the connection to the database by nullifying the variable. $db = null; function GetPOSTUser() { return isset($_POST["user"]) ? $_POST["user"] : ""; } function GetPOSTTimestamp() { return isset($_POST["timestamp"]) ? $_POST["timestamp"] : 0; } function Error($headerMsg, $msg, $code) { header($headerMsg, true, $code); print_r($msg); if(isset($GLOBALS['db'])) $GLOBALS['db'] = null; exit(); } // ------- INSTALL METHODS ------- function PreInstall() { echo '

ES3 Cloud Installation

This will install the ES3 Cloud tables on your MySQL database, and add the required ES3Variables.php file to your server.

Please enter your database details below:

Database Host:


Database User:


Database Password:


Database Name:


'; } function Install($dbHost, $dbUser, $dbPassword, $dbName, $tableName, $filenameField, $fileDataField, $userField, $lastUpdatedField) { try { $db = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_TIMEOUT => 5)); $tableExists = $db->query("SELECT * FROM information_schema.tables WHERE table_schema = '$dbName' AND table_name = '$tableName' LIMIT 1;"); if($tableExists->rowCount() == 0) { // Create the table if it doesn't already exist. try { $createTableQuery = "CREATE TABLE IF NOT EXISTS `$tableName` ( `$filenameField` varchar(200) NOT NULL, `$fileDataField` longblob NOT NULL, `$userField` varchar(64) NOT NULL, `$lastUpdatedField` int(11) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`$filenameField`,`$userField`) ) ENGINE=InnoDB COLLATE=utf8_unicode_ci CHARSET=utf8;"; $db->query($createTableQuery); } catch (PDOException $e) { echo "

Could not create tables on database. Database threw error:

".$e->getMessage()."

To manually install the MySQL tables, please run the following SQL code on your database:

$createTableQuery
"; exit(); } } try { $apiKey = substr(md5(microtime()),rand(0,26),12); $phpScript = ""; // Check that path is writable or file_put_contents is supported. if(!function_exists("file_put_contents")) { ManuallyInstall($phpScript); exit(); } else { file_put_contents("ES3Variables.php", $phpScript); } } catch(Exception $e) { ManuallyInstall($phpScript); exit(); } if(!file_exists("ES3Variables.php")) { ManuallyInstall($phpScript); exit(); } echo "

Successfully installed ES3Cloud

IMPORTANT:
Please take note of your API key below. You will need to use it whenever using the API.

Your API key can also be found in the ES3Variables.php file which has just been installed.

API Key: $apiKey

"; } catch(PDOException $e) { echo "

Database could not be accessed with these details. The database returned the following error:

" . $e->getMessage() . "

"; PreInstall(); exit(); } } function ManuallyInstall($phpScript) { echo "

Couldn't create PHP file on your server. This could be because file_put_contents is not supported on your server, or you do not have permission to write files to this folder on your server.

To manually install the PHP file, please create a file named ES3Variables.php in the same directory as your ES3.php file with the following contents:

$phpScript

After creating this file, installation will be complete.

"; } ?>