Guest User

Untitled

a guest
Jan 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.17 KB | None | 0 0
  1. <?php
  2. require("../function/connection.php");
  3.  
  4.  
  5. // define defaulto timezone:
  6. date_default_timezone_set('Europe/Riga');
  7.  
  8.  
  9. error_reporting(E_ALL | E_STRICT);
  10.  
  11. if (isset($_POST['name'])) { $name = $_POST['name']; }
  12. if (isset($_POST['category'])) { $category = $_POST['category']; }
  13. if (isset($_POST['date_start'])) { $date_start = $_POST['date_start']; }
  14. if (isset($_POST['time_start'])) { $time_start = $_POST['time_start']; }
  15. if (isset($_POST['place'])) { $place = $_POST['place']; }
  16. if (isset($_POST['city_id'])) { $city_id = $_POST['city_id']; }
  17. if (isset($_POST['description'])) { $description = $_POST['description']; }
  18. if (isset($_POST['reference'])) { $reference = $_POST['reference']; }
  19.  
  20.  
  21.  
  22.  
  23. $xxx = new UploadHandler();
  24. $image_small = $xxx->get_file_object();
  25.  
  26.  
  27.  
  28. class UploadHandler
  29. {
  30. private $options;
  31.  
  32. function __construct($options=null) {
  33. $this->options = array(
  34. 'script_url' => $_SERVER['PHP_SELF'],
  35. 'upload_dir' => dirname(__FILE__).'/../img/temp/',
  36. 'upload_url' => dirname($_SERVER['PHP_SELF']).'/../img/temp/',
  37. 'param_name' => 'files',
  38. 'max_file_size' => null,
  39. 'min_file_size' => 1,
  40. 'accept_file_types' => '/.+$/i',
  41. 'max_number_of_files' => null,
  42. 'discard_aborted_uploads' => true,
  43. 'image_versions' => array(
  44.  
  45. 'large' => array(
  46. 'upload_dir' => dirname(__FILE__).'/../img/events/top/',
  47. 'upload_url' => dirname($_SERVER['PHP_SELF']).'/../img/events/top/',
  48. 'max_width' => 160,
  49. 'max_height' => 160
  50. ),
  51.  
  52. 'thumbnail' => array(
  53. 'upload_dir' => dirname(__FILE__).'/../img/events/small/',
  54. 'upload_url' => dirname($_SERVER['PHP_SELF']).'/../img/events/small/',
  55. 'max_width' => 90,
  56. 'max_height' => 65
  57. )
  58. )
  59. );
  60. if ($options) {
  61. $this->options = array_replace_recursive($this->options, $options);
  62. }
  63. }
  64.  
  65. public function get_file_object($file_name) {
  66. $file_path = $this->options['upload_dir'].$file_name;
  67. if (is_file($file_path) && $file_name[0] !== '.') {
  68. $file = new stdClass();
  69. $file->name = $file_name;
  70. $file->size = filesize($file_path);
  71. $file->url = $this->options['upload_url'].rawurlencode($file->name);
  72. foreach($this->options['image_versions'] as $version => $options) {
  73. if (is_file($options['upload_dir'].$file_name)) {
  74. $file->{$version.'_url'} = $options['upload_url']
  75. .rawurlencode($file->name);
  76. }
  77. }
  78. $file->delete_url = $this->options['script_url']
  79. .'?file='.rawurlencode($file->name);
  80. $file->delete_type = 'DELETE';
  81. return $file;
  82. }
  83. return null;
  84. }
  85.  
  86. private function get_file_objects() {
  87. return array_values(array_filter(array_map(
  88. array($this, 'get_file_object'),
  89. scandir($this->options['upload_dir'])
  90. )));
  91. }
  92.  
  93. private function create_scaled_image($file_name, $options) {
  94. $file_path = $this->options['upload_dir'].$file_name;
  95. $new_file_path = $options['upload_dir'].$file_name;
  96. list($img_width, $img_height) = @getimagesize($file_path);
  97. if (!$img_width || !$img_height) {
  98. return false;
  99. }
  100. $scale = min(
  101. $options['max_width'] / $img_width,
  102. $options['max_height'] / $img_height
  103. );
  104. if ($scale > 1) {
  105. $scale = 1;
  106. }
  107. $new_width = $img_width * $scale;
  108. $new_height = $img_height * $scale;
  109. $new_img = @imagecreatetruecolor($new_width, $new_height);
  110. switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
  111. case 'jpg':
  112. case 'jpeg':
  113. $src_img = @imagecreatefromjpeg($file_path);
  114. $write_image = 'imagejpeg';
  115. break;
  116. case 'gif':
  117. @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
  118. $src_img = @imagecreatefromgif($file_path);
  119. $write_image = 'imagegif';
  120. break;
  121. case 'png':
  122. @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
  123. @imagealphablending($new_img, false);
  124. @imagesavealpha($new_img, true);
  125. $src_img = @imagecreatefrompng($file_path);
  126. $write_image = 'imagepng';
  127. break;
  128. default:
  129. $src_img = $image_method = null;
  130. }
  131. $success = $src_img && @imagecopyresampled(
  132. $new_img,
  133. $src_img,
  134. 0, 0, 0, 0,
  135. $new_width,
  136. $new_height,
  137. $img_width,
  138. $img_height
  139. ) && $write_image($new_img, $new_file_path);
  140. // Free up memory (imagedestroy does not delete files):
  141. @imagedestroy($src_img);
  142. @imagedestroy($new_img);
  143. return $success;
  144. }
  145.  
  146. private function has_error($uploaded_file, $file, $error) {
  147. if ($error) {
  148. return $error;
  149. }
  150. if (!preg_match($this->options['accept_file_types'], $file->name)) {
  151. return 'acceptFileTypes';
  152. }
  153. if ($uploaded_file && is_uploaded_file($uploaded_file)) {
  154. $file_size = filesize($uploaded_file);
  155. } else {
  156. $file_size = $_SERVER['CONTENT_LENGTH'];
  157. }
  158. if ($this->options['max_file_size'] && (
  159. $file_size > $this->options['max_file_size'] ||
  160. $file->size > $this->options['max_file_size'])
  161. ) {
  162. return 'maxFileSize';
  163. }
  164. if ($this->options['min_file_size'] &&
  165. $file_size < $this->options['min_file_size']) {
  166. return 'minFileSize';
  167. }
  168. if (is_int($this->options['max_number_of_files']) && (
  169. count($this->get_file_objects()) >= $this->options['max_number_of_files'])
  170. ) {
  171. return 'maxNumberOfFiles';
  172. }
  173. return $error;
  174. }
  175.  
  176. private function handle_file_upload($uploaded_file, $name, $size, $type, $error) {
  177. $file = new stdClass();
  178.  
  179. /* Attela nosaukuma modifikacija */
  180. $file->name = basename(stripslashes($name));
  181.  
  182. $extension = strrchr($file->name, '.'); // strrchr - no labas puses nems nogriezis lidz pirmajam punktam faila paplasinajumu asdoakodsk.jpg jeb "jpg"
  183. $file_name_only = explode(" ", $file->name);
  184. $name = 'uid-xxx-time-'.md5(time().$file_name_only[0]).$extension;
  185.  
  186. $file->name = trim(basename(stripslashes($name)), ".\x00..\x20");
  187. $file->size = intval($size);
  188. $file->type = $type;
  189. $error = $this->has_error($uploaded_file, $file, $error);
  190.  
  191.  
  192.  
  193.  
  194. if (!$error && $file->name) {
  195. $file_path = $this->options['upload_dir'].$file->name;
  196. $append_file = !$this->options['discard_aborted_uploads'] &&
  197. is_file($file_path) && $file->size > filesize($file_path);
  198. clearstatcache();
  199. if ($uploaded_file && is_uploaded_file($uploaded_file)) {
  200. // multipart/formdata uploads (POST method uploads)
  201. if ($append_file) {
  202. file_put_contents(
  203. $file_path,
  204. fopen($uploaded_file, 'r'),
  205. FILE_APPEND
  206. );
  207. } else {
  208. move_uploaded_file($uploaded_file, $file_path);
  209. }
  210. } else {
  211. // Non-multipart uploads (PUT method support)
  212. file_put_contents(
  213. $file_path,
  214. fopen('php://input', 'r'),
  215. $append_file ? FILE_APPEND : 0
  216. );
  217. }
  218. $file_size = filesize($file_path);
  219. if ($file_size === $file->size) {
  220. $file->url = $this->options['upload_url'].rawurlencode($file->name);
  221. foreach($this->options['image_versions'] as $version => $options) {
  222. if ($this->create_scaled_image($file->name, $options)) {
  223. $file->{$version.'_url'} = $options['upload_url']
  224. .rawurlencode($file->name);
  225. }
  226. }
  227. } else if ($this->options['discard_aborted_uploads']) {
  228. unlink($file_path);
  229. $file->error = 'abort';
  230. }
  231. $file->size = $file_size;
  232. $file->delete_url = $this->options['script_url']
  233. .'?file='.rawurlencode($file->name);
  234. $file->delete_type = 'DELETE';
  235. } else {
  236. $file->error = $error;
  237. }
  238. return $file;
  239. }
  240.  
  241. public function get() {
  242. $file_name = isset($_REQUEST['file']) ?
  243. basename(stripslashes($_REQUEST['file'])) : null;
  244. if ($file_name) {
  245. $info = $this->get_file_object($file_name);
  246. } else {
  247. $info = $this->get_file_objects();
  248. }
  249. header('Content-type: application/json');
  250. echo json_encode($info);
  251. }
  252.  
  253. public function post() {
  254. $upload = isset($_FILES[$this->options['param_name']]) ?
  255. $_FILES[$this->options['param_name']] : array(
  256. 'tmp_name' => null,
  257. 'name' => null,
  258. 'size' => null,
  259. 'type' => null,
  260. 'error' => null
  261. );
  262. $info = array();
  263. if (is_array($upload['tmp_name'])) {
  264. foreach ($upload['tmp_name'] as $index => $value) {
  265. $info[] = $this->handle_file_upload(
  266. $upload['tmp_name'][$index],
  267. isset($_SERVER['HTTP_X_FILE_NAME']) ?
  268. $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
  269. isset($_SERVER['HTTP_X_FILE_SIZE']) ?
  270. $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
  271. isset($_SERVER['HTTP_X_FILE_TYPE']) ?
  272. $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
  273. $upload['error'][$index]
  274. );
  275. }
  276. } else {
  277. $info[] = $this->handle_file_upload(
  278. $upload['tmp_name'],
  279. isset($_SERVER['HTTP_X_FILE_NAME']) ?
  280. $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'],
  281. isset($_SERVER['HTTP_X_FILE_SIZE']) ?
  282. $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'],
  283. isset($_SERVER['HTTP_X_FILE_TYPE']) ?
  284. $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'],
  285. $upload['error']
  286. );
  287. }
  288. header('Vary: Accept');
  289. if (isset($_SERVER['HTTP_ACCEPT']) &&
  290. (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
  291. header('Content-type: application/json');
  292. } else {
  293. header('Content-type: text/plain');
  294. }
  295. echo json_encode($info);
  296. }
  297.  
  298. public function delete() {
  299. $file_name = isset($_REQUEST['file']) ?
  300. basename(stripslashes($_REQUEST['file'])) : null;
  301. $file_path = $this->options['upload_dir'].$file_name;
  302. $success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
  303. if ($success) {
  304. foreach($this->options['image_versions'] as $version => $options) {
  305. $file = $options['upload_dir'].$file_name;
  306. if (is_file($file)) {
  307. unlink($file);
  308. }
  309. }
  310. }
  311. header('Content-type: application/json');
  312. echo json_encode($success);
  313. }
  314. }
  315.  
  316.  
  317. $upload_handler = new UploadHandler();
  318.  
  319.  
  320. header('Pragma: no-cache');
  321. header('Cache-Control: private, no-cache');
  322. header('Content-Disposition: inline; filename="files.json"');
  323. header('X-Content-Type-Options: nosniff');
  324.  
  325.  
  326. switch ($_SERVER['REQUEST_METHOD']) {
  327. case 'HEAD':
  328. case 'GET':
  329. $upload_handler->get();
  330. break;
  331. case 'POST':
  332. $upload_handler->post();
  333. break;
  334. case 'DELETE':
  335. $upload_handler->delete();
  336. break;
  337. case 'OPTIONS':
  338. break;
  339. default:
  340. header('HTTP/1.0 405 Method Not Allowed');
  341. }
  342.  
  343.  
  344.  
  345.  
  346. if (isset($_POST['add_event']))
  347. {
  348. $sql = mysql_query("
  349. INSERT INTO event
  350. (
  351. id,
  352. name,
  353. category_id,
  354. date_start,
  355. time_start,
  356. place,
  357. description,
  358. reference,
  359. city_id,
  360. image_small,
  361. image_medium,
  362. image_large,
  363. image_top,
  364. active,
  365. top,
  366. user_id,
  367. date_created
  368. )
  369. VALUES
  370. (
  371. NULL,
  372. '$name',
  373. '$category',
  374. '$date_start',
  375. '$time_start',
  376. '$place',
  377. '$description',
  378. '$reference',
  379. '$city_id',
  380. '$image_small',
  381. '',
  382. '',
  383. '',
  384. '1',
  385. '0',
  386. '1',
  387. now()
  388. )
  389. ")
  390. or die(mysql_error());
  391. }
  392. ?>
Add Comment
Please, Sign In to add comment