Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.68 KB | None | 0 0
  1. <?php
  2.  
  3. // Kickstart the framework
  4. $f3=require('lib/base.php');
  5.  
  6. $f3->set('DEBUG',0);
  7. if ((float)PCRE_VERSION<7.9)
  8.     trigger_error('PCRE version is out of date');
  9.  
  10. // Load configuration
  11. $f3->config('config.ini');
  12.  
  13. //bye bye ie 8 and lower
  14. if(preg_match('/(?i)msie [5-8]/',$_SERVER['HTTP_USER_AGENT']))
  15. {
  16.     echo View::instance()->render('ie.htm');
  17.     exit;
  18. }
  19.  
  20. /* Controller routes */
  21.  
  22. /*** index ***/
  23. $f3->route('GET /',
  24.     function($f3) {
  25.         $f3->set('name','yello');
  26.         echo View::instance()->render('index.htm');
  27.     }
  28. );
  29.  
  30. /*** gallery ***/
  31. $f3->route('GET /gallery',
  32.     function($f3) {
  33.         $f3->set('tags', array());
  34.         $f3->set('images', array(
  35.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  36.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  37.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  38.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  39.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  40.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  41.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  42.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  43.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  44.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  45.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  46.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  47.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  48.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  49.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg')
  50.         ));
  51.  
  52.         echo View::instance()->render('gallery.htm');
  53.     }
  54. );
  55.  
  56. $f3->route('GET /gallery/*',
  57.     function($f3,$params) {
  58.         $f3->set('name', 'Gallery');
  59.  
  60.         $tags = explode('/', $params[1]);
  61.         $f3->set('tags', $tags);
  62.  
  63.         $f3->set('images', array(
  64.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  65.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  66.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  67.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  68.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  69.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  70.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  71.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  72.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  73.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  74.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  75.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  76.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  77.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  78.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg')
  79.         ));
  80.  
  81.         echo View::instance()->render('gallery.htm');
  82.     }
  83. );
  84.  
  85.  
  86. /*** admin ***/
  87. $f3->route('GET /admin/login',
  88.     function($f3,$params) {
  89.         if(checkToken($f3))
  90.             $f3->reroute('/admin/img/list');
  91.  
  92.         $f3->set('name', 'Login');
  93.         echo View::instance()->render('admin-login.htm');
  94.     }
  95. );
  96.  
  97. $f3->route('GET /admin/img/list',
  98.     function($f3,$params) {
  99.         if(!checkToken($f3))
  100.             $f3->reroute('/admin/login?returnUrl=' . urlencode('/admin/img/list'));
  101.  
  102.         $f3->set('name', 'Admin');
  103.         echo View::instance()->render('admin-img-list.htm');
  104.     }
  105. );
  106.  
  107. $f3->route('GET /admin/img/add',
  108.     function($f3,$params) {
  109.         if(!checkToken($f3))
  110.             $f3->reroute('/admin/login?returnUrl=' . urlencode('/admin/img/add'));
  111.  
  112.         $f3->set('name', 'Admin');
  113.         echo View::instance()->render('admin-img-edit.htm');
  114.     }
  115. );
  116.  
  117. /*** img ***/
  118. $f3->route('GET /img/@id',
  119.     function($f3, $params) {
  120.         $db = db($f3);
  121.  
  122.         $imgList = $db->exec(
  123.             array('SELECT * FROM pp_img where img_id = :imgId'),
  124.             array(
  125.                 array(':imgId'=>$params['id'])
  126.             )
  127.         );
  128.  
  129.         if(sizeof($imgList) == 0) {
  130.             $f3->error(404);
  131.             return;
  132.         }
  133.  
  134.         $img = $imgList[0];
  135.         $f3->set('img_file', $img['img_file']);
  136.         echo View::instance()->render('img.htm');
  137.     }
  138. );
  139.  
  140.  
  141. /* API - for ajax calls, returns JSON */
  142.  
  143. /*** helper methods ***/
  144.  
  145. function apiResponse($data) {
  146.     header('Content-Type: application/json');
  147.     return json_encode(array( 'status' => 'OK', 'data' => $data));
  148. }
  149.  
  150. function apiErrorResponse($msg) {
  151.     header('Content-Type: application/json');
  152.     http_response_code(500);
  153.     return json_encode(array( 'status' => 'ERROR', 'errorMessage' => $msg));
  154. }
  155.  
  156. function db($f3) {
  157.     return new DB\SQL(
  158.         'mysql:host=localhost;port=3306;dbname=mysql',
  159.         'root',
  160.         $f3->get('DBPWD')
  161.     );
  162. }
  163.  
  164. function checkToken($f3) {
  165.     $db = db($f3);
  166.     $usrList = $db->exec(
  167.         array('SELECT * FROM pp_user where token = :token and last_login < date_add(now(), interval 1 hour)'),
  168.         array(
  169.             array(
  170.                 ':token'=>$_COOKIE['PP_AUTH']
  171.             )
  172.         )
  173.     );
  174.  
  175.     return empty($usrList) ? null : $usrList[0];
  176. }
  177.  
  178. function createThumbnail($source, $target, $thumb_width, $thumb_height) {
  179.     //usage: $this->createThumbnail('img/gallery/img_0000001.jpg', 'img/gallery/thumbs/img_thumb_0000001.jpg', 768, 512);
  180.     $image = imagecreatefromjpeg($source);
  181.  
  182.     $width = imagesx($image);
  183.     $height = imagesy($image);
  184.  
  185.     $original_aspect = $width / $height;
  186.     $thumb_aspect = $thumb_width / $thumb_height;
  187.  
  188.     if ( $original_aspect >= $thumb_aspect )
  189.     {
  190.        // If image is wider than thumbnail (in aspect ratio sense)
  191.        $new_height = $thumb_height;
  192.        $new_width = $width / ($height / $thumb_height);
  193.     }
  194.     else
  195.     {
  196.        // If the thumbnail is wider than the image
  197.        $new_width = $thumb_width;
  198.        $new_height = $height / ($width / $thumb_width);
  199.     }
  200.  
  201.     $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
  202.  
  203.     // Resize and crop
  204.     imagecopyresampled($thumb,
  205.        $image,
  206.        0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
  207.        0 - ($new_height - $thumb_height) / 2, // Center the image vertically
  208.        0, 0,
  209.        $new_width, $new_height,
  210.        $width, $height);
  211.     imagejpeg($thumb, $target, 80);
  212. }
  213.  
  214. /*** auth ***/
  215. $f3->route('GET /api/login/@username/@password',
  216.     function($f3,$params) {
  217.         $db = db($f3);
  218.  
  219.         $usrList = $db->exec(
  220.             array('SELECT * FROM pp_user where username = :username and password = md5(concat(:password,salt))'),
  221.             array(
  222.                 array(
  223.                     ':username'=>$params['username'],
  224.                     ':password'=>$params['password']
  225.                 )
  226.             )
  227.         );
  228.  
  229.         if(empty($usrList)) {
  230.             echo apiErrorResponse("Login failed");
  231.         } else {
  232.             $user = $usrList[0];
  233.             $token = bin2hex(openssl_random_pseudo_bytes(16));
  234.             $db->exec(
  235.                 array('update pp_user set token = :token, last_login = now() where user_id = :userId'),
  236.                 array(
  237.                     array(
  238.                         ':token'=>$token,
  239.                         ':userId'=>$user['user_id']
  240.                     )
  241.                 )
  242.             );
  243.  
  244.             setcookie("PP_AUTH", $token, time() + 3600, '/');
  245.             echo apiResponse("Success");
  246.         }
  247.     }
  248. );
  249.  
  250. $f3->route('GET /api/logout',
  251.     function($f3,$params) {
  252.         if(!$_COOKIE['PP_AUTH']) {
  253.             echo apiResponse('OK');
  254.             return;
  255.         }
  256.  
  257.         $db = db($f3);
  258.         $usrList = $db->exec(
  259.             array('update pp_user set token = null where token = :token'),
  260.             array(
  261.                 array(
  262.                     ':token'=>$_COOKIE['PP_AUTH']
  263.                 )
  264.             )
  265.         );
  266.  
  267.         echo apiResponse('OK');
  268.     }
  269. );
  270.  
  271. /*** tags ***/
  272. $f3->route('GET /api/tags/get/@tag',
  273.     function($f3,$params) {
  274.         $result = array();
  275.  
  276.         $db = db($f3);
  277.  
  278.         $tagList = $db->exec(
  279.             array('SELECT * FROM pp_tag where tag_name like :tag'),
  280.             array(
  281.                 array(':tag'=>$params['tag'] . '%')
  282.             )
  283.         );
  284.  
  285.         echo apiResponse($tagList);
  286.     }
  287. );
  288.  
  289. /*** image list ***/
  290. $f3->route('GET /api/gallery/getimages',
  291.     function($f3,$params) {
  292.         echo apiResponse(array(
  293.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  294.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  295.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  296.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  297.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  298.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  299.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  300.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  301.             array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
  302.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  303.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  304.             array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
  305.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  306.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
  307.             array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg')
  308.         ));
  309.     }
  310. );
  311.  
  312. /*** upload image ***/
  313. $f3->route('POST /api/img/upload',
  314.     function($f3) {
  315.         if(!($user = checkToken($f3))) {
  316.             $f3->error(401);
  317.         }
  318.  
  319.         //file must be present
  320.         if(!$_FILES['file']) {
  321.             echo apiErrorResponse('no file');
  322.             return;
  323.         }
  324.  
  325.         //1mb upload limit
  326.         if($_FILES['file']['error'] == 1 || $_FILES['file']['size'] > 1000000) {
  327.             echo apiErrorResponse('maximum upload size is 1MB');
  328.             return;
  329.         }
  330.  
  331.         if($_FILES['file']['name']) {
  332.             $fileExtension = end((explode(".", $_FILES['file']['name'])));
  333.             $hash = uniqid('img_');
  334.             $imgPath = "ui/img/upload/" . $hash . "." . $fileExtension;
  335.             $imgThumbPath = "ui/img/upload/" . $hash . "_thumb." . $fileExtension;
  336.             move_uploaded_file($_FILES['file']['tmp_name'], $imgPath);
  337.             createThumbnail($imgPath, $imgThumbPath, 768, 512);
  338.             echo apiResponse(array( 'original_file' => $_FILES['file']['name'], 'img_file' => $imgPath, 'img_thumb_file' => $imgThumbPath ));
  339.         } else {
  340.             echo apiErrorResponse("upload failed");
  341.         }
  342.     }
  343. );
  344.  
  345. /*** save image ***/
  346. $f3->route('POST /api/img/save',
  347.     function($f3) {
  348.         if(!($user = checkToken($f3))) {
  349.             $f3->error(401);
  350.         }
  351.  
  352.         $db = db($f3);
  353.         if($f3->get('POST.img_id')) {
  354.             //TODO
  355.         } else {
  356.             $db->begin();
  357.  
  358.             $imgFile = end((explode("/", $f3->get('POST.img_file'))));
  359.             $imgThumbFile = end((explode("/", $f3->get('POST.img_thumb_file'))));
  360.  
  361.             rename($f3->get('POST.img_file'), 'ui/img/gallery/' . $imgFile);
  362.             rename($f3->get('POST.img_thumb_file'), 'ui/img/gallery/' . $imgThumbFile);
  363.  
  364.             $db->exec(
  365.                 array('insert into pp_img (img_file, img_thumb_file, name, description, inserted, inserted_by) values(:imgFile, :imgThumbFile, :name, :description, now(), :insertedBy)'),
  366.                 array(
  367.                     array(
  368.                         ':imgFile'=>$imgFile,
  369.                         ':imgThumbFile'=>$imgThumbFile,
  370.                         ':name'=>$f3->get('POST.name'),
  371.                         ':description'=>$f3->get('POST.description'),
  372.                         ':insertedBy'=>$user['user_id']
  373.                     )
  374.                 )
  375.             );
  376.  
  377.             $imgId = $db->lastInsertId();
  378.             foreach (explode(',', $f3->get('POST.tag_ids')) as $tagId) {
  379.                 $db->exec(
  380.                     array('insert into pp_img_tag (img_id, tag_id) values(:imgId, :tagId)'),
  381.                     array(
  382.                         array(
  383.                             ':imgId'=>$imgId,
  384.                             ':tagId'=>$tagId
  385.                         )
  386.                     )
  387.                 );
  388.             }
  389.            
  390.             $db->commit();
  391.             echo apiResponse($imgId);
  392.         }
  393.     }
  394. );
  395.  
  396. //TEST METHODS
  397.  
  398. $f3->route('GET /api/add-dummy-user',
  399.     function($f3) {
  400.         $salt = uniqid(mt_rand(), true);
  401.         $db = db($f3);
  402.         $db->exec(
  403.             array('insert into pp_user (username, password, salt) values(:username, :password, :salt)'),
  404.             array(
  405.                 array(
  406.                     ':username'=>'admin',
  407.                     ':password'=>md5('admin'.$salt),
  408.                     ':salt'=>$salt
  409.                 )
  410.             )
  411.         );
  412.  
  413.         echo apiResponse("Success");
  414.     }
  415. );
  416.  
  417. $f3->route('GET /api/test-email',
  418.     function($f3) {
  419.         $smtp = new SMTP ('smtp.gmail.com', '465', 'ssl', 'pavlinio2a@gmail.com', 'xxxx');
  420.         $smtp->set('From', '"Some website"');
  421.         $smtp->set('To', '"Pavle Gartner" <pavlinio2a@gmail.com>');
  422.         $smtp->set('Subject', 'Test');
  423.         echo apiResponse($smtp->send('My content'));
  424.     }
  425. );
  426.  
  427. $f3->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement