query( "INSERT INTO course (parent_id, label) VALUES (:parent, :label)", [ ':parent' => Registry::get('REQUEST')->POST['parent_id'], ':label' => Registry::get('REQUEST')->POST['label'] ] )->lastInsertID(); $cache = self::update_courses_cache(); // update category caches return $cache['breadcrumbs']; // return } /** update course * --- * @param void (get data from POST) */ public static function update_course() { Registry::use('database')->query( "UPDATE course SET parent_id = :parent, label = :label WHERE id = :id", [ ':id' => Registry::get('REQUEST')->POST['id'], ':parent' => Registry::get('REQUEST')->POST['parent_id'], ':label' => Registry::get('REQUEST')->POST['label'] ] ); $cache = self::update_courses_cache(); return $cache['breadcrumbs']; } /** update courses cache * --- -- -- - - - * Shall run if anything changes to courses * * @param void * @return (array): the two course caches */ public static function update_courses_cache() { $tree = proxy( // cache-get categories [\app\models\cms\Course_model::class, 'category_tree'], [], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE ); $breadcrumbs = proxy( // cache-get breadcrumbs [\app\models\cms\Course_model::class, 'breadcrumbs'], [$tree], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE ); return [ 'tree' => $tree, 'breadcrumbs' => $breadcrumbs ]; } ## ------------------------------------------------------------------------- ## ## FILE METHODS ## ## ------------------------------------------------------------------------- /** upload_file * upload the file to the file system * * the method reads the POST and FILES array * to retrieve all needed parametres * * + $_FILES[file] * + $_POST[folder] : lesson's ID * + $_POST[reference] : reference type */ public static function upload_file() { $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 = array( 'application/pdf', 'image/png', 'image/jpeg', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ); // 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' => $store_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 * @return id (int): id of created media record */ public static function define_media($data) { $sql = "INSERT INTO wiki_media (`title`, `type`, `path`) VALUES (:title, :mimetype, :filepath)"; $stmt = $this->pdo->prepare($sql); $stmt->execute([ 'title' => $data['title'], 'mimetype' => $data['type'], 'filepath' => $data['path'] ]); $inserted_id = $this->pdo->lastInsertId(); return $inserted_id; } }