Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ITS;
  4. use \Psr\Http\Message\ServerRequestInterface as Request;
  5. use \Psr\Http\Message\ResponseInterface as Response;
  6.  
  7. class pictures {
  8.     private $pdo;
  9.     private $directory = ""; // relative path
  10.     private $rootPath = ""; // servers root
  11.     private $picture = null;
  12.     private $tableId;
  13.     private $filename;
  14.     private $fileExtension;
  15.     private $mimeType ="image/png"; // hack incase the file doesnt have a mime type
  16.     private $picSize;
  17.  
  18.  
  19.     public function __construct($pdo){
  20.  
  21.         $this->pdo = $pdo;
  22.  
  23.     }
  24.  
  25.  
  26.  
  27.  
  28.     /*
  29.  
  30.     public function preparePic
  31.         Get picture ready to be inserted into the database
  32.         RETURN true on complete, other an eror code specified by https://www.php.net/manual/en/features.file-upload.errors.php
  33.     */
  34.     public function preparePic(\Slim\Http\UploadedFile $uploadedFile, $picPath,$rootPath,$tableId){
  35.         $this->rootPath = $rootPath;
  36.         $this->tableId = $tableId;
  37.         $this->picPath = $picPath; // this is the relative path like uploads/pictures/vehicles..
  38.  
  39.  
  40.  
  41.         if ($uploadedFile->getError() === UPLOAD_ERR_OK) {
  42.             $this->fileExtension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
  43.             $this->picture = $uploadedFile;
  44.             $this->filename = "ITS-".time();
  45.             $this->mimeType =  $uploadedFile->getClientMediaType(); // get the mime type
  46.             $this->picSize = $uploadedFile->getSize( ); // get the size of the picture.. not needed right now
  47.             return 0;
  48.         } else {
  49.             return $uploadedFile->getError();
  50.         }
  51.     }
  52.  
  53.  
  54.     /*
  55.     public function getPic()
  56.         Gets pictures needed to serve in the app
  57.     */
  58.     public function getPic($rowId,$tableId){
  59.         /*
  60.                `picture_id` int(11) NOT NULL AUTO_INCREMENT,
  61.               `file_name` varchar(200) NOT NULL,
  62.               `description` text,
  63.               `upload_date` varchar(45) NOT NULL,
  64.               `uuid` varchar(100) NOT NULL,
  65.               `mime_type` varchar(45) NOT NULL,
  66.         */
  67.  
  68.         $Sql = "SELECT * FROM tblPictures WHERE intTableID=:TableId AND intRowId=:RowId";
  69.         $stmt = $this->pdo->prepare($Sql);
  70.         //$stmt->execute(array(':TableId' => $tableId, 'RowId' => $tableId ));
  71.         $stmt->execute(array(':TableId' => $tableId, 'RowId' => $rowId ));
  72.  
  73.         return $stmt->fetch();
  74.  
  75.     }
  76.  
  77.  
  78.     /*
  79.     public function getPicByUuid()
  80.         Gets pictures needed to serve in the app
  81.     */
  82.     public function getPicByUuid($uuid){
  83.         /*
  84.          `picture_id` int(11) NOT NULL AUTO_INCREMENT,
  85.           `file_name` varchar(200) NOT NULL,
  86.           `description` text,
  87.           `upload_date` varchar(45) NOT NULL,
  88.           `uuid` varchar(100) NOT NULL,
  89.           `mime_type` varchar(45) NOT NULL,
  90.         */
  91.  
  92.         $Sql = "SELECT * FROM pictures WHERE uuid = :uuid";
  93.  
  94.         $stmt = $this->pdo->prepare($Sql);
  95.  
  96.         $stmt->execute(array(':uuid' => $uuid));
  97.  
  98.         return $stmt->fetch();
  99.  
  100.     }
  101.  
  102.     public function removePic(){}
  103.  
  104.  
  105.  
  106.  
  107.     public function commitPic($rowID){
  108.  
  109.         /*
  110.          *   `picture_id` int(11) NOT NULL AUTO_INCREMENT,
  111.               `file_name` varchar(200) NOT NULL,
  112.               `description` text,
  113.               `upload_date` varchar(45) NOT NULL,
  114.               `uuid` varchar(100) NOT NULL,
  115.               `mime_type` varchar(45) NOT NULL,
  116.          */
  117.  
  118.         $fullFileName =  $this->picPath. "/" . $this->filename.".".$this->fileExtension;
  119.         $uuid = $this->guid(). "." .$this->fileExtension;
  120.  
  121.         $query = "INSERT INTO pictures (`picture_id`,`file_name`, `description`,`upload_date`,`uuid`,`mime_type`) values(
  122.                         null,
  123.                         :file_name,
  124.                         :description,
  125.                         NOW(),
  126.                         :uuid,
  127.                         :mime_type
  128.                     )";
  129.  
  130.         try {
  131.             $stmt = $this->pdo->prepare($query);
  132.             $stmt->execute(array(
  133.                 ':description' => "Added by System",
  134.                 ':file_name' => $fullFileName,
  135.                 ':uuid' => $uuid,
  136.                 ':mime_type' => $this->mimeType
  137.             ));
  138.  
  139.         } catch (PDOException $e) {
  140.             // tell someone who cares
  141.         }
  142.  
  143.         // write to Picture database
  144.         $newpath =$this->rootPath . $fullFileName;
  145.         $this->picture->moveTo($newpath );
  146.     }
  147.  
  148.  
  149.     public function getEmoji(){}
  150.  
  151.  
  152.     function guid()
  153.     {
  154.         $randomString = openssl_random_pseudo_bytes(16);
  155.         $time_low = bin2hex(substr($randomString, 0, 4));
  156.         $time_mid = bin2hex(substr($randomString, 4, 2));
  157.         $time_hi_and_version = bin2hex(substr($randomString, 6, 2));
  158.         $clock_seq_hi_and_reserved = bin2hex(substr($randomString, 8, 2));
  159.         $node = bin2hex(substr($randomString, 10, 6));
  160.  
  161.         /**
  162.          * Set the four most significant bits (bits 12 through 15) of the
  163.          * time_hi_and_version field to the 4-bit version number from
  164.          * Section 4.1.3.
  165.          * @see http://tools.ietf.org/html/rfc4122#section-4.1.3
  166.          */
  167.         $time_hi_and_version = hexdec($time_hi_and_version);
  168.         $time_hi_and_version = $time_hi_and_version >> 4;
  169.         $time_hi_and_version = $time_hi_and_version | 0x4000;
  170.  
  171.         /**
  172.          * Set the two most significant bits (bits 6 and 7) of the
  173.          * clock_seq_hi_and_reserved to zero and one, respectively.
  174.          */
  175.         $clock_seq_hi_and_reserved = hexdec($clock_seq_hi_and_reserved);
  176.         $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2;
  177.         $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000;
  178.  
  179.         return sprintf('%08s-%04s-%04x-%04x-%012s', $time_low, $time_mid, $time_hi_and_version, $clock_seq_hi_and_reserved, $node);
  180.     } // guid
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement