Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace ITS;
- use \Psr\Http\Message\ServerRequestInterface as Request;
- use \Psr\Http\Message\ResponseInterface as Response;
- class pictures {
- private $pdo;
- private $directory = ""; // relative path
- private $rootPath = ""; // servers root
- private $picture = null;
- private $tableId;
- private $filename;
- private $fileExtension;
- private $mimeType ="image/png"; // hack incase the file doesnt have a mime type
- private $picSize;
- public function __construct($pdo){
- $this->pdo = $pdo;
- }
- /*
- public function preparePic
- Get picture ready to be inserted into the database
- RETURN true on complete, other an eror code specified by https://www.php.net/manual/en/features.file-upload.errors.php
- */
- public function preparePic(\Slim\Http\UploadedFile $uploadedFile, $picPath,$rootPath,$tableId){
- $this->rootPath = $rootPath;
- $this->tableId = $tableId;
- $this->picPath = $picPath; // this is the relative path like uploads/pictures/vehicles..
- if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
- $this->fileExtension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
- $this->picture = $uploadedFile;
- $this->filename = "ITS-".time();
- $this->mimeType = $uploadedFile->getClientMediaType(); // get the mime type
- $this->picSize = $uploadedFile->getSize( ); // get the size of the picture.. not needed right now
- return 0;
- } else {
- return $uploadedFile->getError();
- }
- }
- /*
- public function getPic()
- Gets pictures needed to serve in the app
- */
- public function getPic($rowId,$tableId){
- /*
- `picture_id` int(11) NOT NULL AUTO_INCREMENT,
- `file_name` varchar(200) NOT NULL,
- `description` text,
- `upload_date` varchar(45) NOT NULL,
- `uuid` varchar(100) NOT NULL,
- `mime_type` varchar(45) NOT NULL,
- */
- $Sql = "SELECT * FROM tblPictures WHERE intTableID=:TableId AND intRowId=:RowId";
- $stmt = $this->pdo->prepare($Sql);
- //$stmt->execute(array(':TableId' => $tableId, 'RowId' => $tableId ));
- $stmt->execute(array(':TableId' => $tableId, 'RowId' => $rowId ));
- return $stmt->fetch();
- }
- /*
- public function getPicByUuid()
- Gets pictures needed to serve in the app
- */
- public function getPicByUuid($uuid){
- /*
- `picture_id` int(11) NOT NULL AUTO_INCREMENT,
- `file_name` varchar(200) NOT NULL,
- `description` text,
- `upload_date` varchar(45) NOT NULL,
- `uuid` varchar(100) NOT NULL,
- `mime_type` varchar(45) NOT NULL,
- */
- $Sql = "SELECT * FROM pictures WHERE uuid = :uuid";
- $stmt = $this->pdo->prepare($Sql);
- $stmt->execute(array(':uuid' => $uuid));
- return $stmt->fetch();
- }
- public function removePic(){}
- public function commitPic($rowID){
- /*
- * `picture_id` int(11) NOT NULL AUTO_INCREMENT,
- `file_name` varchar(200) NOT NULL,
- `description` text,
- `upload_date` varchar(45) NOT NULL,
- `uuid` varchar(100) NOT NULL,
- `mime_type` varchar(45) NOT NULL,
- */
- $fullFileName = $this->picPath. "/" . $this->filename.".".$this->fileExtension;
- $uuid = $this->guid(). "." .$this->fileExtension;
- $query = "INSERT INTO pictures (`picture_id`,`file_name`, `description`,`upload_date`,`uuid`,`mime_type`) values(
- null,
- :file_name,
- :description,
- NOW(),
- :uuid,
- :mime_type
- )";
- try {
- $stmt = $this->pdo->prepare($query);
- $stmt->execute(array(
- ':description' => "Added by System",
- ':file_name' => $fullFileName,
- ':uuid' => $uuid,
- ':mime_type' => $this->mimeType
- ));
- } catch (PDOException $e) {
- // tell someone who cares
- }
- // write to Picture database
- $newpath =$this->rootPath . $fullFileName;
- $this->picture->moveTo($newpath );
- }
- public function getEmoji(){}
- function guid()
- {
- $randomString = openssl_random_pseudo_bytes(16);
- $time_low = bin2hex(substr($randomString, 0, 4));
- $time_mid = bin2hex(substr($randomString, 4, 2));
- $time_hi_and_version = bin2hex(substr($randomString, 6, 2));
- $clock_seq_hi_and_reserved = bin2hex(substr($randomString, 8, 2));
- $node = bin2hex(substr($randomString, 10, 6));
- /**
- * Set the four most significant bits (bits 12 through 15) of the
- * time_hi_and_version field to the 4-bit version number from
- * Section 4.1.3.
- * @see http://tools.ietf.org/html/rfc4122#section-4.1.3
- */
- $time_hi_and_version = hexdec($time_hi_and_version);
- $time_hi_and_version = $time_hi_and_version >> 4;
- $time_hi_and_version = $time_hi_and_version | 0x4000;
- /**
- * Set the two most significant bits (bits 6 and 7) of the
- * clock_seq_hi_and_reserved to zero and one, respectively.
- */
- $clock_seq_hi_and_reserved = hexdec($clock_seq_hi_and_reserved);
- $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2;
- $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000;
- return sprintf('%08s-%04s-%04x-%04x-%012s', $time_low, $time_mid, $time_hi_and_version, $clock_seq_hi_and_reserved, $node);
- } // guid
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement