Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Kickstart the framework
- $f3=require('lib/base.php');
- $f3->set('DEBUG',0);
- if ((float)PCRE_VERSION<7.9)
- trigger_error('PCRE version is out of date');
- // Load configuration
- $f3->config('config.ini');
- //bye bye ie 8 and lower
- if(preg_match('/(?i)msie [5-8]/',$_SERVER['HTTP_USER_AGENT']))
- {
- echo View::instance()->render('ie.htm');
- exit;
- }
- /* Controller routes */
- /*** index ***/
- $f3->route('GET /',
- function($f3) {
- $f3->set('name','yello');
- echo View::instance()->render('index.htm');
- }
- );
- /*** gallery ***/
- $f3->route('GET /gallery',
- function($f3) {
- $f3->set('tags', array());
- $f3->set('images', array(
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg')
- ));
- echo View::instance()->render('gallery.htm');
- }
- );
- $f3->route('GET /gallery/*',
- function($f3,$params) {
- $f3->set('name', 'Gallery');
- $tags = explode('/', $params[1]);
- $f3->set('tags', $tags);
- $f3->set('images', array(
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg')
- ));
- echo View::instance()->render('gallery.htm');
- }
- );
- /*** admin ***/
- $f3->route('GET /admin/login',
- function($f3,$params) {
- if(checkToken($f3))
- $f3->reroute('/admin/img/list');
- $f3->set('name', 'Login');
- echo View::instance()->render('admin-login.htm');
- }
- );
- $f3->route('GET /admin/img/list',
- function($f3,$params) {
- if(!checkToken($f3))
- $f3->reroute('/admin/login?returnUrl=' . urlencode('/admin/img/list'));
- $f3->set('name', 'Admin');
- echo View::instance()->render('admin-img-list.htm');
- }
- );
- $f3->route('GET /admin/img/add',
- function($f3,$params) {
- if(!checkToken($f3))
- $f3->reroute('/admin/login?returnUrl=' . urlencode('/admin/img/add'));
- $f3->set('name', 'Admin');
- echo View::instance()->render('admin-img-edit.htm');
- }
- );
- /*** img ***/
- $f3->route('GET /img/@id',
- function($f3, $params) {
- $db = db($f3);
- $imgList = $db->exec(
- array('SELECT * FROM pp_img where img_id = :imgId'),
- array(
- array(':imgId'=>$params['id'])
- )
- );
- if(sizeof($imgList) == 0) {
- $f3->error(404);
- return;
- }
- $img = $imgList[0];
- $f3->set('img_file', $img['img_file']);
- echo View::instance()->render('img.htm');
- }
- );
- /* API - for ajax calls, returns JSON */
- /*** helper methods ***/
- function apiResponse($data) {
- header('Content-Type: application/json');
- return json_encode(array( 'status' => 'OK', 'data' => $data));
- }
- function apiErrorResponse($msg) {
- header('Content-Type: application/json');
- http_response_code(500);
- return json_encode(array( 'status' => 'ERROR', 'errorMessage' => $msg));
- }
- function db($f3) {
- return new DB\SQL(
- 'mysql:host=localhost;port=3306;dbname=mysql',
- 'root',
- $f3->get('DBPWD')
- );
- }
- function checkToken($f3) {
- $db = db($f3);
- $usrList = $db->exec(
- array('SELECT * FROM pp_user where token = :token and last_login < date_add(now(), interval 1 hour)'),
- array(
- array(
- ':token'=>$_COOKIE['PP_AUTH']
- )
- )
- );
- return empty($usrList) ? null : $usrList[0];
- }
- function createThumbnail($source, $target, $thumb_width, $thumb_height) {
- //usage: $this->createThumbnail('img/gallery/img_0000001.jpg', 'img/gallery/thumbs/img_thumb_0000001.jpg', 768, 512);
- $image = imagecreatefromjpeg($source);
- $width = imagesx($image);
- $height = imagesy($image);
- $original_aspect = $width / $height;
- $thumb_aspect = $thumb_width / $thumb_height;
- if ( $original_aspect >= $thumb_aspect )
- {
- // If image is wider than thumbnail (in aspect ratio sense)
- $new_height = $thumb_height;
- $new_width = $width / ($height / $thumb_height);
- }
- else
- {
- // If the thumbnail is wider than the image
- $new_width = $thumb_width;
- $new_height = $height / ($width / $thumb_width);
- }
- $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
- // Resize and crop
- imagecopyresampled($thumb,
- $image,
- 0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
- 0 - ($new_height - $thumb_height) / 2, // Center the image vertically
- 0, 0,
- $new_width, $new_height,
- $width, $height);
- imagejpeg($thumb, $target, 80);
- }
- /*** auth ***/
- $f3->route('GET /api/login/@username/@password',
- function($f3,$params) {
- $db = db($f3);
- $usrList = $db->exec(
- array('SELECT * FROM pp_user where username = :username and password = md5(concat(:password,salt))'),
- array(
- array(
- ':username'=>$params['username'],
- ':password'=>$params['password']
- )
- )
- );
- if(empty($usrList)) {
- echo apiErrorResponse("Login failed");
- } else {
- $user = $usrList[0];
- $token = bin2hex(openssl_random_pseudo_bytes(16));
- $db->exec(
- array('update pp_user set token = :token, last_login = now() where user_id = :userId'),
- array(
- array(
- ':token'=>$token,
- ':userId'=>$user['user_id']
- )
- )
- );
- setcookie("PP_AUTH", $token, time() + 3600, '/');
- echo apiResponse("Success");
- }
- }
- );
- $f3->route('GET /api/logout',
- function($f3,$params) {
- if(!$_COOKIE['PP_AUTH']) {
- echo apiResponse('OK');
- return;
- }
- $db = db($f3);
- $usrList = $db->exec(
- array('update pp_user set token = null where token = :token'),
- array(
- array(
- ':token'=>$_COOKIE['PP_AUTH']
- )
- )
- );
- echo apiResponse('OK');
- }
- );
- /*** tags ***/
- $f3->route('GET /api/tags/get/@tag',
- function($f3,$params) {
- $result = array();
- $db = db($f3);
- $tagList = $db->exec(
- array('SELECT * FROM pp_tag where tag_name like :tag'),
- array(
- array(':tag'=>$params['tag'] . '%')
- )
- );
- echo apiResponse($tagList);
- }
- );
- /*** image list ***/
- $f3->route('GET /api/gallery/getimages',
- function($f3,$params) {
- echo apiResponse(array(
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'deepsky.jpg', 'img_thumb_file' => 'deepsky.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'img_0000001.jpg', 'img_thumb_file' => 'img_thumb_0000001.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg'),
- array('img_file' => 'nightscape.jpg', 'img_thumb_file' => 'nightscape.jpg')
- ));
- }
- );
- /*** upload image ***/
- $f3->route('POST /api/img/upload',
- function($f3) {
- if(!($user = checkToken($f3))) {
- $f3->error(401);
- }
- //file must be present
- if(!$_FILES['file']) {
- echo apiErrorResponse('no file');
- return;
- }
- //1mb upload limit
- if($_FILES['file']['error'] == 1 || $_FILES['file']['size'] > 1000000) {
- echo apiErrorResponse('maximum upload size is 1MB');
- return;
- }
- if($_FILES['file']['name']) {
- $fileExtension = end((explode(".", $_FILES['file']['name'])));
- $hash = uniqid('img_');
- $imgPath = "ui/img/upload/" . $hash . "." . $fileExtension;
- $imgThumbPath = "ui/img/upload/" . $hash . "_thumb." . $fileExtension;
- move_uploaded_file($_FILES['file']['tmp_name'], $imgPath);
- createThumbnail($imgPath, $imgThumbPath, 768, 512);
- echo apiResponse(array( 'original_file' => $_FILES['file']['name'], 'img_file' => $imgPath, 'img_thumb_file' => $imgThumbPath ));
- } else {
- echo apiErrorResponse("upload failed");
- }
- }
- );
- /*** save image ***/
- $f3->route('POST /api/img/save',
- function($f3) {
- if(!($user = checkToken($f3))) {
- $f3->error(401);
- }
- $db = db($f3);
- if($f3->get('POST.img_id')) {
- //TODO
- } else {
- $db->begin();
- $imgFile = end((explode("/", $f3->get('POST.img_file'))));
- $imgThumbFile = end((explode("/", $f3->get('POST.img_thumb_file'))));
- rename($f3->get('POST.img_file'), 'ui/img/gallery/' . $imgFile);
- rename($f3->get('POST.img_thumb_file'), 'ui/img/gallery/' . $imgThumbFile);
- $db->exec(
- array('insert into pp_img (img_file, img_thumb_file, name, description, inserted, inserted_by) values(:imgFile, :imgThumbFile, :name, :description, now(), :insertedBy)'),
- array(
- array(
- ':imgFile'=>$imgFile,
- ':imgThumbFile'=>$imgThumbFile,
- ':name'=>$f3->get('POST.name'),
- ':description'=>$f3->get('POST.description'),
- ':insertedBy'=>$user['user_id']
- )
- )
- );
- $imgId = $db->lastInsertId();
- foreach (explode(',', $f3->get('POST.tag_ids')) as $tagId) {
- $db->exec(
- array('insert into pp_img_tag (img_id, tag_id) values(:imgId, :tagId)'),
- array(
- array(
- ':imgId'=>$imgId,
- ':tagId'=>$tagId
- )
- )
- );
- }
- $db->commit();
- echo apiResponse($imgId);
- }
- }
- );
- //TEST METHODS
- $f3->route('GET /api/add-dummy-user',
- function($f3) {
- $salt = uniqid(mt_rand(), true);
- $db = db($f3);
- $db->exec(
- array('insert into pp_user (username, password, salt) values(:username, :password, :salt)'),
- array(
- array(
- ':username'=>'admin',
- ':password'=>md5('admin'.$salt),
- ':salt'=>$salt
- )
- )
- );
- echo apiResponse("Success");
- }
- );
- $f3->route('GET /api/test-email',
- function($f3) {
- $smtp = new SMTP ('smtp.gmail.com', '465', 'ssl', 'pavlinio2a@gmail.com', 'xxxx');
- $smtp->set('From', '"Some website"');
- $smtp->set('To', '"Pavle Gartner" <pavlinio2a@gmail.com>');
- $smtp->set('Subject', 'Test');
- echo apiResponse($smtp->send('My content'));
- }
- );
- $f3->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement