GET['type']; // get media-type $real_path = MEDIA_STORAGE_ROOT . $file_path; // construct real path if (!file_exists($real_path)) { Render::view('error/404'); } else { Render::file($real_path, $media_type); } } else { Render::view('error/404', [ 'error_code' => 403, 'moto' => 'Forbidden', 'message' => '' ]); } } /** get_file_attributes * * returns attributes of a file * (medias are proxied for speed optimization) * * @param $path (string) : file path * @return $file attributes --or-- false */ private static function get_file_attributes($path) { $medias = Cache_service::files_attributes(); foreach($medias as $key => $medi) { if ($medi['path'] == $path) { return $medi; } } return false; } ## PETITIONS ## ------------------------------------------------------------------------- ## secretarial support / teachers' requests and applications /** (any) petition * * check if user is authorized to view the content; * if so, prepare and render the petition view * * @param $petition_type (string): [common|penalty] */ public static function request_petition($petition_tag) { // get current user $manager = new App_manager(); if (!$manager->hasUserToken()) { // if user is not connected Render::view('error/404', [ 'error_code' => 403, // serve forbidden 'moto' => 'Forbidden', 'message' => 'Για να έχετε πρόσβαση, θα πρέπει πρώτα να συνδεθείτε' ]); die(); // then end; } $token = $manager->getUserToken(); // from token $user = $token->getUser(); // create user $id = $user->getID(); // keep id user switch ($petition_tag) { // route to specific type of petition case 'common': self::render_common_petition(['user' => $user]); break; case 'penalty': self::render_penalty_form(['user' => $user]); break; default: // if not a known petition type Render::view('error/404', [ // then serve not-found 'message' => 'Δεν βρέθηκε το είδος της αίτησης ή του εγγράφου που ζητήσατε.' ]); } } private static function render_penalty_form($opts) { $user_id = $opts['user']->getID(); $form_setup = json_decode(json_encode(PENALTY_FORM, JSON_UNESCAPED_UNICODE)); // get all teachers $teachers_array = Registry::use('database')->runQuery( "SELECT concat(last_name, ' ', first_name) as TeacherName FROM user ORDER BY TeacherName", [] ); $teachers = []; // constuct teacher names as a simple array foreach($teachers_array as $key => $person) { $teachers[] = $person['TeacherName']; } // print_r( $teachers); die(); // get $key of rapporteur, president and members inside the form_setup->form array for($i=0; $i < sizeof($form_setup->form) ; $i++) { if (isset($form_setup->form[$i]->name)) { if ($form_setup->form[$i]->name == 'rapporteur') { $keyRapporteur = $i; } if ($form_setup->form[$i]->name == 'president') { $keyPresident = $i; } if ($form_setup->form[$i]->name == 'members') { $keyMembers = $i; } } } // set option sources for rapporteur, president and members $form_setup->form[$keyRapporteur]->options = $teachers; $form_setup->form[$keyPresident]->options = $teachers; $form_setup->form[$keyMembers]->options = $teachers; // get Form's HTML and Jsvascript $form = JsonToForm::json_form($form_setup, [ ['name' => 'id', 'value' => $user_id] // pass user identity ]); Render::view('templates/penalty', ['form' => $form]); } ## ------------------------------------------------------------------------- ## ## ADMIN METHODS (insert, updated etc.) ## ## ------------------------------------------------------------------------- /** files * echo all files * * @return (array) */ public static function files() { return Cache_service::files_attributes(); } /** upload_file * upload the file to the file system * * the method reads the POST and FILES array * to retrieve all needed parametres * * FILES @param file * POST @param folder : petition's ID or somthing random * SESSION @param user_id */ public static function upload_file() { $request = Registry::get('REQUEST'); $uploaded = self::upload_to_fs(); // upload file to file-system if ($uploaded['success']) { $media_id = self::define_media([ // define media in database; get id 'title' => $request->POST['title'], 'type' => $uploaded['type'], 'path' => $uploaded['path'] ]); Render::json([ // render results as json 'success' => true, 'id' => $media_id, 'title' => $request->POST['title'], 'path' => $uploaded['path'], 'type' => $uploaded['type'] ]); } else { Render::json(['success' => false ]); } } /** upload to fs * upload file to File-System * * POST @param folder * FILES @param file */ private static function upload_to_fs() { $post = Registry::get('REQUEST')->POST; $files = Registry::get('REQUEST')->FILES; // Checks before uploading the file //////////////////////////////////////////////////////////////////////// // ** 1: file is upladed to temporary folder --------------------------- if (! is_uploaded_file($files['file']['tmp_name'])) { return ['success' => false]; // bye! } // ** 2: File belongs to the allowed MIME types ------------------------ $allowed_file_types = [ // pdf 'application/pdf', // images 'image/png', 'image/jpeg', // word 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // excel 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // rar 'application/vnd.rar', 'application/x-rar-compressed', 'application/octet-stream', // zip 'application/zip', 'application/x-zip-compressed', 'multipart/x-zip' // 'application/octet-stream' refers to zip; also to rar (no-need to re-include) ]; // Recomended MIME type checking via mime_content_type(): $mime_type = mime_content_type($files['file']['tmp_name']); if (! in_array($mime_type, $allowed_file_types)) { // File type NOT allowed ... return ['success' => false]; // bye! } $file_name = $files['file']['name']; $file_type = $files['file']['type']; // do not take it for granted $file_size = $files['file']['size']; $file_tmp = $files['file']['tmp_name']; $bare_name = pathinfo($file_name, PATHINFO_FILENAME); $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); // ** 3: filename or size checks may be added -------------------------- if ($file_name == "") { return ['success' => false]; // bye! } // READY to finaly save/upload the file to CDN ///////////////////////// $folder = MEDIA_STORAGE_ROOT . $post['folder']; if (!file_exists($folder)) { // create folder if not exists mkdir($folder, 0757, true); } // print_r([ // 'dir' => $folder, // 'file' => $bare_name, // 'ext' => $file_ext, // 'type' => $file_type // ]); die(); $relative_filename = $post['folder'] .'/' . strtolower(self::clear_file_name($bare_name) .'.'. $file_ext); $store_filename = MEDIA_STORAGE_ROOT . $relative_filename; if (move_uploaded_file($files["file"]["tmp_name"], $store_filename)) { return [ 'success' => true, 'path' => $relative_filename, 'type' => $mime_type ]; } else { return ['success' => false]; } } /** clear_file_name * replace greek characters and strip symbols */ private static function clear_file_name($str) { $el = mb_split( "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωάέήίόύώϊϋς ", ""); $en = str_split("ABGDEZHUIKLMNJOPRSTYFXCVabgdezhuiklmnjoprstyfxcvaehioyviys-"); $strip = str_split("!@#$%^&*()+~`[]{};'/<>?=\""); return str_replace($strip, '', str_replace($el, $en, $str)); } /** define_media * * create a record in media table * * @param $data (array): [title => , path => , type => mime-type] * @return id (int): id of created media record */ private static function define_media($data) { $request = Registry::get('REQUEST'); $media_id = Registry::use('database')->query( "INSERT INTO media (label, `type`, `path`) VALUES (:label, :mimetype, :filepath)", [ 'label' => $data['title'], 'mimetype' => $data['type'], 'filepath' => $data['path'] ] )->lastInsertID(); return $media_id; } /** create media for petition * * links petition to each media-file of the media `id`s array * * @param $media (array): a list of media-file `id`s * @param $petition_id (int) */ private static function create_medias_for_petition($medias, $petition_id) { foreach($medias as $key => $medi) { self::link_media_to_petition($medi, $petition_id); // link to petition } return true; } /** link one media-file to a specific post * * NOTE: * the method does not check if media is linked already * so be sure that the pair of (media_id,post_id) not exist * * @param $media_id (int) * @param $petition_id (int) */ private static function link_media_to_petition( $media_id, $petition_id ) { Registry::use('database')->runQuery( "INSERT INTO petition_media (petition_id, media_id) VALUES (:petition, :media)", [ 'petition' => $petition_id, 'media' => $media_id, ] ); return true; } }