prepare("INSERT INTO posts (author_id,comment_section_id,tags,title,pic_path,preview_path,comments_enabled,edit_lock) VALUES (?,?,?,?,?,?,?,?)"); $s->bind_param("ssssssss", $author_id, null, $tags, $title, $pic_path, $prev_path, $comms_enabled, $edit_lock); if ($s->execute() === false) return new ReturnT(err_code: E_DBE_INSERTFAIL, err_desc: "failed to create post record in DB"); return new ReturnT(data: $result); } // Methods /* * METHOD * Create single publication */ function Post_Create_Method (array $req, array $files): ReturnT { global $Config, $LOGGED_IN, $THIS_USER; $author_id = $THIS_USER; $tags = null; $pic_path = null; $title = null; $prev_path = null; $comms_enabled = false; // Input sanity checks // Check if user is authenticated if (!$LOGGED_IN) return new ReturnT(err_code: E_AUT_NOTAUTHED, err_desc: "you must be logged in to create posts"); // Check if there are necessary input if (!(isset($req["tags"]) && isset($files["pic"]))) return new ReturnT(err_code: E_UIN_INSUFARGS, err_desc: "tags and picture are necessary"); // TODO: add rate-limiting, instead of this // Check user role if (User_HasRole($THIS_USER, "newbie").GetData()) return new ReturnT(err_code: E_ACS_INSUFROLE, err_desc: "newbies cant create posts"); // Check image properties // If size is too large if ($files["pic"]["size"] > $Config["media"]["max_pic_size"]) return new ReturnT(err_code: E_UIN_FILE2LARGE, err_desc: "picture size is too large"); $TmpFilePath = $_FILES["pic"]["tmp_name"]; $Ext = strtolower(pathinfo($TmpFilePath, PATHINFO_EXTENSION)); // If file extension is not in list of allowed if (!in_array($Ext, $Config["media"]["allowed_exts"])) return new ReturnT(err_code: E_UIN_FILETYPE, err_desc: "picture extension is invalid"); // If file mime type is not in list of allowed if (!in_array(mime_content_type($TmpFilePath), $Config["media"]["allowed_mimetypes"])) return new ReturnT(err_code: E_UIN_FILETYPE, err_desc: "picture mime type is invalid"); // Check if resolution is bigger than allowed or have unacceptable aspect ratio list($SzX, $SzY, $Type, $Attr) = getimagesize($TmpFilePath); if (!Post_ImgResIsValid($SzX, $SzY)) return new ReturnT(err_code: E_UIN_IMGBADRES, err_desc: "image with that resolution or aspect ratio cant be accepted"); // Copy picture to storage folder $res = Post_StoreImage($TmpFilePath, $Config["media"]["previews_enabled"]); if ($res.IsError()) return $res; // TODO // Actions return Post_Create($author_id, $tags, $pic_path, $title, $prev_path, $comms_enabled, false); } if (Utils_ThisFileIsRequested(__FILE__)) { require_once("../_json.php"); // TODO: cleanup if bad result } ?>