Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.23 KB | None | 0 0
  1. <?php  
  2.     ob_start();
  3.    
  4.     require_once('header.php');
  5.    
  6.     if (!isset($_SESSION['project'])) {
  7.         header("Location: index.php");
  8.     }
  9.    
  10.     function ajaxErrorDetect($error_message) {
  11.         if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  12.             ob_clean();
  13.             header('HTTP/1.1 500 Internal Server Error');
  14.             header('Content-type: text/plain');
  15.             exit($error_message);
  16.         }
  17.         else {
  18.         }
  19.     }
  20.  
  21.     function createDirectory($dirPath) {
  22.         $error_check = true;
  23.        
  24.         for ($i=0; $i <= 2; $i++) {
  25.             if ($error_check == true) {
  26.                 if (!file_exists($dirPath[$i])) {
  27.                     mkdir($dirPath[$i], 0777, true);
  28.                 }
  29.                 else {
  30.                 }
  31.             }
  32.         }
  33.        
  34.         return $error_check;
  35.     }
  36.    
  37.     //Change this to only delete the directory the failed file is trying to upload to!!!
  38.     function deleteDirectory($targetPath) {
  39.         if (is_dir($targetPath)) {
  40.             $objects = scandir($targetPath);
  41.             foreach ($objects as $object) {
  42.                 if ($object != "." && $object !="..") {
  43.                     if (filetype($targetPath . DIRECTORY_SEPARATOR . $object) == "dir") {
  44.                         deleteDirectory($targetPath . DIRECTORY_SEPARATOR . $object);
  45.                     } else {
  46.                         unlink($targetPath . DIRECTORY_SEPARATOR . $object);
  47.                     }
  48.                 }
  49.             }
  50.         reset($objects);
  51.         rmdir($targetPath);
  52.         }
  53.     }
  54.    
  55.     $ds = DIRECTORY_SEPARATOR;
  56.     $storeFolder = 'uploads';
  57.     $error_message = '';
  58.    
  59.     //Check if delete passed in header
  60.     if (isset($_GET['delete_pg'])) { //---------------------------------------------------------------------------------UPDATE FOR THIS SHOULD BE DONE VIA JQUERY!!!!
  61.         $page = mysqli_real_escape_string($db, $_GET['delete_pg']);
  62.        
  63.         //Delete associated files
  64.         if (file_exists(__DIR__ . "/submissions/2017/{$_SESSION['project']}/pdf/{$_SESSION['project']}_page_". $page .".pdf")) {
  65.             unlink(__DIR__ . "/submissions/2017/{$_SESSION['project']}/pdf/{$_SESSION['project']}_page_". $page .".pdf");
  66.         }
  67.         if (file_exists(__DIR__ . "/submissions/2017/{$_SESSION['project']}/jpeg/{$_SESSION['project']}_page_". $page .".jpeg")) {
  68.             unlink(__DIR__ . "/submissions/2017/{$_SESSION['project']}/jpeg/{$_SESSION['project']}_page_". $page .".jpeg");
  69.         }
  70.         if (file_exists(__DIR__ . "/submissions/2017/{$_SESSION['project']}/thumbnails/{$_SESSION['project']}_page_". $page ."_thumbnail.jpeg")) {
  71.             unlink(__DIR__ . "/submissions/2017/{$_SESSION['project']}/thumbnails/{$_SESSION['project']}_page_". $page ."_thumbnail.jpeg");
  72.         }
  73.        
  74.         //Prepare sql query
  75.         $sql = "
  76.             UPDATE `submissions`
  77.             SET `proposal_pg". $page ."` = NULL
  78.             WHERE `project` = '{$_SESSION['project']}'
  79.         ";
  80.        
  81.         $db->query($sql);
  82.        
  83.         header("Location: submit.php"); //---------------------------------------------------------NO, UPDATE THROUGH JQUERY!
  84.     }
  85.    
  86.     //If filesize overwhelms php.ini
  87.     if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) && empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0 ) {
  88.             $error_message = 'Error: File size can not exceed 7mb';
  89.             ajaxErrorDetect($error_message);
  90.     }
  91.  
  92.     //If php detects there are filed being transmitted
  93.     if (!empty($_FILES)) {
  94.        
  95.         $project = $_SESSION['project'];
  96.        
  97.        
  98.         //UPDATE DB ENTRY
  99.         /* while ($row = $result->fetch_assoc()) {
  100.             $id = $row['id'];
  101.         } */
  102.        
  103.         //Initialize variable scope
  104.         $fileNo = '';
  105.        
  106.         //Check which file has been uploaded
  107.         if (isset($_FILES['file_01']['name'])) {
  108.             $fileNo = '1';
  109.         }
  110.         else if (isset($_FILES['file_02']['name'])) {
  111.             $fileNo = '2';
  112.         }
  113.         else if (isset($_FILES['file_03']['name'])) {
  114.             $fileNo = '3';
  115.         }
  116.        
  117.         //Bind the files to the appropriate variables
  118.         $fileBase= basename(strtolower($_FILES['file_0'. $fileNo]['name']));
  119.         $fileSize = $_FILES['file_0' . $fileNo]['size'];
  120.         $fileType = $_FILES['file_0' . $fileNo]['type'];
  121.         $tempFile = $_FILES['file_0' . $fileNo]['tmp_name'];
  122.        
  123.         //Confirm that the file sent over is under 7mb (1024 conversion)
  124.         if ($fileSize >= 7340032) {
  125.             $error_message = 'Error: File size can not exceed 7mb';
  126.             ajaxErrorDetect($error_message);
  127.         }
  128.         else {
  129.             if ($fileType != 'application/pdf') {
  130.                 $error_message = 'Error: You may only upload files in PDF form';
  131.                 ajaxErrorDetect($error_message);
  132.             }
  133.             else {
  134.                
  135.                 //Prepare variables with the appropriate path names
  136.                 $targetPath = 'submissions/2017/' . $project;
  137.                 $newFileName = $project . '_page_' . $fileNo;
  138.                 $targetFile =  $targetPath . '/pdf/' . $newFileName . '.pdf';
  139.                
  140.                 //Prepare an array that will store the appropriate paths to the different image and pdf folders
  141.                 $dirPath = array();
  142.                 array_push($dirPath, $targetPath . '/pdf', $targetPath . '/jpeg', $targetPath . '/thumbnails');
  143.                
  144.                 //If the directory was successfully created
  145.                 if (!createDirectory($dirPath)) {
  146.                     $error_message = 'Error: Could not create directory. Please report this error to info@winterstations.com';
  147.                     ajaxErrorDetect($error_message);
  148.                 }
  149.                 else {
  150.                    
  151.                     //Upload the transferred files
  152.                     if (!move_uploaded_file($tempFile, $targetFile)) {
  153.                         $error_message = 'Error: Could not transfer files. Please report this error to info@winterstations.com';
  154.                         ajaxErrorDetect($error_message);
  155.                     }
  156.                     else {
  157.                        
  158.                         //Prepare variables with the appropriate path names
  159.                         $pdfPath = __DIR__ . $ds . $targetFile;
  160.                         $jpgPath = __DIR__ . $ds . $targetPath . '/jpeg/' . $newFileName . '.jpeg';
  161.                         $thumbPath = __DIR__ . $ds . $targetPath . '/thumbnails/' . $newFileName . '_thumbnail.jpeg';
  162.                        
  163.                         //Get page count
  164.                         $pagecount = exec('identify -format %n '. $pdfPath);
  165.                        
  166.                         //If page 1 or 2 do not have more than 1 page then:
  167.                         if ($pagecount == 1 || $fileNo == '3') {
  168.                            
  169.                             //Create JPG from PDF
  170.                             exec('convert -density 300 ' . $pdfPath . '[0] ' . $jpgPath);
  171.                            
  172.                             //Get page dimensions from newly created JPG
  173.                             $size = getimagesize($jpgPath);
  174.                             $width = $size[0];
  175.                             $height = $size[1];
  176.                            
  177.                             //Determine the orientation of the submitted files
  178.                             if ($fileNo == 1 || $fileNo == 2) {
  179.                                 if ($height > $width) {
  180.                                     $orientation = 'portrait';
  181.                                     //Rotate and produce thumbnail from pdf
  182.                                     exec('convert -colorspace sRGB -rotate 90 -resize 306x198 -background white -extent 306x198 -quality 100 ' . $pdfPath . '[0] ' . $thumbPath);
  183.                                     //Reduce the size of the produced full jpeg
  184.                                     exec('convert -colorspace sRGB -resize 792x1224 -background white -extent 792x1224 -quality 100 ' . $jpgPath . ' ' . $jpgPath);
  185.                                 }
  186.                                 else {
  187.                                     $orientation = 'landscape';
  188.                                     //Produce landscape thumbnail from pdf
  189.                                     exec('convert -colorspace sRGB -resize 306x198 -background white -extent 306x198 -quality 100 ' . $pdfPath . '[0] ' . $thumbPath);
  190.                                     //Reduce the size of the produced full jpeg
  191.                                     exec('convert -colorspace sRGB -resize 1224x792 -background white -extent 1224x792 -quality 100 ' . $jpgPath . ' ' . $jpgPath);
  192.                                 }
  193.                             }
  194.                             else if ($fileNo == 3) {
  195.                                 if ($height > $width) {
  196.                                     $orientation = 'portrait';
  197.                                     //Produce portrait thumbnail from pdf
  198.                                     exec('convert -colorspace sRGB -resize 155x198 -background white -extent 155x198 -quality 100 ' . $pdfPath . '[0] ' . $thumbPath);
  199.                                     //Reduce the size of the produced full jpeg
  200.                                     exec('convert -colorspace sRGB -resize 613x792 -background white -extent 613x792 -quality 100 ' . $jpgPath . ' ' . $jpgPath);
  201.                                 }
  202.                                 else {
  203.                                     $orientation = 'landscape';
  204.                                     //Rotate and produce thumbnail from pdf
  205.                                     exec('convert -colorspace sRGB -rotate 90 -resize 155x198 -background white -extent 155x198 -quality 100 ' . $pdfPath . '[0] ' . $thumbPath);
  206.                                     //Reduce the size of the produced full jpeg
  207.                                     exec('convert -colorspace sRGB -resize 792x613 -background white -extent 792x613 -quality 100 ' . $jpgPath . ' ' . $jpgPath);
  208.                                 }
  209.                             }
  210.                             else {
  211.                                 $error_message = 'Error: Unknown file. Please report this error to info@winterstations.com';
  212.                                 ajaxErrorDetect($error_message);
  213.                             }
  214.  
  215.                             //Prepare the database query to get the ID of the proper project to update with the newly-uploaded files
  216.                             $sql = "SELECT * FROM `submissions` WHERE `project` = '$project'";
  217.                             $result = $db->query($sql);
  218.                             if ($result->num_rows() == 0) {
  219.                                 //CREATE DB ENTRY
  220.                                 $sql = "
  221.                                     INSERT INTO `submissions` (
  222.                                         `project`,
  223.                                         `proposal_pg". $fileNo ."`)
  224.                                         VALUES (
  225.                                             ?,
  226.                                             ?,
  227.                                             )";
  228.                            
  229.                                     $statement = $db->prepare($sql);
  230.                                     $statement->bind_param('ss', $project, $newFileName);
  231.                                     $statement->execute();
  232.                                     $statement->close();
  233.                             }
  234.                             else {
  235.                                 //Prepare query for new file link
  236.                                 $sql = "
  237.                                     UPDATE `submissions`
  238.                                     SET `proposal_pg". $fileNo ."` = '$newFileName'
  239.                                     WHERE `project` = '$project'
  240.                                 ";
  241.                                 $db->query($sql);
  242.                             }
  243.                            
  244.                             //Prompt saying the files were successfully uploaded
  245.                             $error_message = 'File successfully uploaded';
  246.                             ajaxErrorDetect($error_message);
  247.                         }
  248.                         else {
  249.                             if (isset($targetFile)) {
  250.                                
  251.                                 if (file_exists($pdfPath)) {
  252.                                     unlink($pdfPath);
  253.                                 }
  254.                                 if (file_exists($jpgPath)) {
  255.                                     unlink($jpgPath);
  256.                                 }
  257.                                 if (file_exists($thumbPath)) {
  258.                                     unlink($thumbPath);
  259.                                 }
  260.                                
  261.                                
  262.                                 $sql = "
  263.                                     UPDATE `submissions`
  264.                                     SET `proposal_pg". $fileNo ."` = NULL
  265.                                     WHERE `project` = '$project'
  266.                                 ";
  267.                                
  268.                                 $db->query($sql);
  269.                                
  270.                                 $error_message = 'Error: Pages 1 and 2 cannot contain multi-page pdfs.';
  271.                                 ajaxErrorDetect($error_message);
  272.                             }
  273.                         }
  274.                     }
  275.                 }
  276.             }
  277.         }
  278.     }
  279.     ?>
  280.     <div id="panel" class="submit-panel">
  281.         <script>
  282.             var errorMessage = <?php print json_encode($error_message); ?>,
  283.                 project = <?php print json_encode($_SESSION['project']); ?>;
  284.         </script>
  285.         <h2>Submit</h2>
  286.         <?php if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {?>
  287.             <span class="close-panel">X</span>
  288.         <?php } ?>
  289.         <div class="error-rep"></div>
  290.         <div class="form-divider"></div>
  291.         <div class="form-subheader">PAGE ONE</div>
  292.         <div class="upload-container ledger" id="file_01">
  293.             <div class="ledger-preview" <?php
  294.                
  295.                 //Default page set status to false
  296.                 $page_one_set = false;
  297.                
  298.                 //Prepare the query
  299.                 $sql = "
  300.                     SELECT *
  301.                     FROM `submissions`
  302.                     WHERE `project` = '{$_SESSION['project']}'
  303.                 ";
  304.                
  305.                 if (!$result = $db->query($sql)) {
  306.                     die();
  307.                     $error_message = 'Error: There was an error running the query. Please report this error to info@winterstations.com';
  308.                     ajaxErrorDetect($error_message);
  309.                 }
  310.                 while($row = $result->fetch_assoc()) {
  311.                     if ($row['proposal_pg1'] != NULL) {                                
  312.                         $page_one_set = true;
  313.                         print 'style="background-image: url(\'submissions/2017/'. $_SESSION['project'] . '/thumbnails/' . $row['proposal_pg1'] . '_thumbnail.jpeg?' . time() . '\')"';
  314.                     }
  315.                 }
  316.            
  317.             ?>>
  318.                 <form class="dropzone" id="file-one" action="submit.php" method="post" enctype="multipart/form-data">
  319.                     <div class="progress-container">
  320.                         <div class="progress-bar">
  321.                             <div class="progress-fill">
  322.                             </div>
  323.                         </div>
  324.                         <div class="progress-text">
  325.                         </div>
  326.                     </div>
  327.                     <span class="preview-add">
  328.                         <?php
  329.                             if ($page_one_set == false) {
  330.                                 print "+";
  331.                             }
  332.                         ?>
  333.                     </span>
  334.                     <div class="file-upload-offset">
  335.                         <div class="preview-options"><?php
  336.                             if ($page_one_set == true) {
  337.                         ?>
  338.                             <span class="submission-view"><a href="<?php print "submissions/2017/{$_SESSION['project']}/pdf/{$_SESSION['project']}_page_1.pdf"; ?>">View</a></span> | <span class="submission_remove"><a href="<?php print "submit.php?delete_pg=1"; ?>">Delete</a></span>
  339.                         <?php } ?>
  340.                         </div>
  341.                         <div class="fallback">
  342.                             <input name="file_01" type="file" accept="application/pdf" /><br /><br />
  343.                             <input type="submit" value="Upload" />
  344.                         </div>
  345.                     </div>
  346.                 </form>
  347.             </div>
  348.         </div>
  349.         <div class="preview-description">
  350.         <span>Requirements Checklist:</span>
  351.             <ul class="submissions-checklist">
  352.                 <div>&#9633; &nbsp; 11 x 17" (279mm x 432mm).</div>
  353.                 <div>&#9633; &nbsp; Single-page PDF file under 7mb.</div>
  354.                 <div>&#9633; &nbsp; A maximum of 150 word text describing the project and presenting the
  355.                     experience offered by the visitors.</div>
  356.                 <div>&#9633; &nbsp; Layout of selected images and drawings describing the project.</div>
  357.             </ul>
  358.         </div>
  359.         <div class="form-divider"></div>
  360.  
  361.         <div class="form-subheader">PAGE TWO</div>
  362.         <div class="upload-container ledger" id="file_02">
  363.             <div class="ledger-preview" <?php
  364.                
  365.                 //Default page set status to false
  366.                 $page_two_set = false;
  367.                
  368.                 //Prepare the query
  369.                 $sql = "
  370.                     SELECT *
  371.                     FROM `submissions`
  372.                     WHERE `project` = '{$_SESSION['project']}'
  373.                 ";
  374.                
  375.                 if (!$result = $db->query($sql)) {
  376.                     die();
  377.                     $error_message = 'Error: There was an error running the query. Please report this error to info@winterstations.com';
  378.                     ajaxErrorDetect($error_message);
  379.                 }
  380.                 while($row = $result->fetch_assoc()) {
  381.                     if ($row['proposal_pg2'] != NULL) {                                
  382.                         $page_two_set = true;
  383.                         print 'style="background-image: url(\'submissions/2017/'. $_SESSION['project'] . '/thumbnails/' . $row['proposal_pg2'] . '_thumbnail.jpeg?' . time() . '\')"';
  384.                     }
  385.                 }
  386.            
  387.             ?>>
  388.                 <form class="dropzone" id="file-two" action="submit.php" method="post" enctype="multipart/form-data">
  389.                     <div class="progress-container">
  390.                         <div class="progress-bar">
  391.                             <div class="progress-fill">
  392.                             </div>
  393.                         </div>
  394.                         <div class="progress-text">
  395.                         </div>
  396.                     </div>
  397.                     <span class="preview-add">
  398.                         <?php
  399.                             if ($page_two_set == false) {
  400.                                 print "+";
  401.                             }
  402.                         ?>
  403.                     </span>
  404.                     <div class="file-upload-offset">
  405.                         <div class="preview-options"><?php
  406.                             if ($page_two_set == true) {
  407.                         ?>
  408.                             <span class="submission-view"><a href="<?php print "submissions/2017/{$_SESSION['project']}/pdf/{$_SESSION['project']}_page_2.pdf"; ?>">View</a></span> | <span class="submission_remove"><a href="<?php print "submit.php?delete_pg=2"; ?>">Delete</a></span>
  409.                         <?php } ?>
  410.                         </div>
  411.                         <div class="fallback">
  412.                             <input name="file_02" type="file" accept="application/pdf" /><br /><br />
  413.                             <input type="submit" value="Upload" />
  414.                         </div>
  415.                     </div>
  416.                 </form>
  417.             </div>
  418.         </div>
  419.         <div class="preview-description">
  420.         <span>Requirements Checklist:</span>
  421.             <ul class="submissions-checklist">
  422.                 <div>&#9633; &nbsp; 11 x 17" (279mm x 432mm).</div>
  423.                 <div>&#9633; &nbsp; Single-page PDF file under 7mb.</div>
  424.                 <div>&#9633; &nbsp; 4 main views or elevations of the installation indicating
  425.                     size and specification of materials. Entrant is free to
  426.                     choose the scale.</div>
  427.                 <div>&#9633; &nbsp; A perspective showing the design in its entirety.</div>
  428.             </ul>
  429.         </div>
  430.         <div class="form-divider"></div>
  431.        
  432.         <div class="form-subheader">PAGE THREE</div>
  433.         <div class="upload-container ledger" id="file_03">
  434.             <div class="letter-preview" <?php
  435.                
  436.                 //Default page set status to false
  437.                 $page_three_set = false;
  438.                
  439.                 //Prepare the query
  440.                 $sql = "
  441.                     SELECT *
  442.                     FROM `submissions`
  443.                     WHERE `project` = '{$_SESSION['project']}'
  444.                 ";
  445.                
  446.                 if (!$result = $db->query($sql)) {
  447.                     die();
  448.                     $error_message = 'Error: There was an error running the query. Please report this error to info@winterstations.com';
  449.                     ajaxErrorDetect($error_message);
  450.                 }
  451.                 while($row = $result->fetch_assoc()) {
  452.                     if ($row['proposal_pg3'] != NULL) {                                
  453.                         $page_three_set = true;
  454.                         print 'style="background-image: url(\'submissions/2017/'. $_SESSION['project'] . '/thumbnails/' . $row['proposal_pg3'] . '_thumbnail.jpeg?' . time() . '\')"';
  455.                     }
  456.                 }
  457.            
  458.             ?>>
  459.                 <form class="dropzone" id="file-three" action="submit.php" method="post" enctype="multipart/form-data">
  460.                     <div class="progress-container">
  461.                         <div class="progress-bar">
  462.                             <div class="progress-fill">
  463.                             </div>
  464.                         </div>
  465.                         <div class="progress-text">
  466.                         </div>
  467.                     </div>
  468.                     <span class="preview-add">
  469.                         <?php
  470.                             if ($page_three_set == false) {
  471.                                 print "+";
  472.                             }
  473.                         ?>
  474.                     </span>
  475.                     <div class="file-upload-offset">
  476.                         <div class="preview-options"><?php
  477.                             if ($page_three_set == true) {
  478.                         ?>
  479.                             <span class="submission-view"><a href="<?php print "submissions/2017/{$_SESSION['project']}/pdf/{$_SESSION['project']}_page_3.pdf"; ?>">View</a></span> | <span class="submission_remove"><a href="<?php print "submit.php?delete_pg=3"; ?>">Delete</a></span>
  480.                         <?php } ?>
  481.                         </div>
  482.                         <div class="fallback">
  483.                             <input name="file_03" type="file" accept="application/pdf" /><br /><br />
  484.                             <input type="submit" value="Upload" />
  485.                         </div>
  486.                     </div>
  487.                 </form>
  488.             </div>
  489.         </div>
  490.         <div class="preview-description">
  491.         <span>Requirements Checklist:</span>
  492.             <ul class="submissions-checklist">
  493.                 <div>&#9633; &nbsp; 8.5 x 11" (216mm x 279mm)</div>
  494.                 <div>&#9633; &nbsp; PDF file under 7mb</div>
  495.                 <div>&#9633; &nbsp; Design team member names, with brief CV for each.</div>
  496.                 <div>&#9633; &nbsp; A 100 word description of the team, highlighting its strength.</div>
  497.                 <div>&#9633; &nbsp; Name, location and date of recent publications, projects, or exhibitions.</div>
  498.                 <div>&#9633; &nbsp; Images of selected projects for which team members have been responsible, followed with a brief description.</div>
  499.             </ul>
  500.         </div>
  501.         <div class="form-divider"></div>
  502.         <div>Click or drag and drop your files into the dashed boxes above. Please allow a few minutes for processing.
  503.         If you see your submission in the thumbnails above (click 'View' if you are unsure), this means we have received your entry.  
  504.         You may edit your submissions here until November 4, 2016.
  505.         Should you experience any difficulty uploading your files, please contact <a class="email-link-tomek"></a></div>
  506.     </div>
  507. </ul>
  508. <?php include_once('footer.php'); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement