First round of changes for 2.2.0 headless update for testing

# Conflicts:
#	src/com/Base.js
#	src/com/controllers/NavIndex.js
#	src/com/controllers/PageEditor.js
#	src/com/controllers/SettingsIndex.js
#	src/libraries/FipamoAPI.js
#	src/libraries/FipamoAdminAPI.js
#	src/package-lock.json
#	src/package.json
This commit is contained in:
Ro 2021-06-29 15:18:34 -07:00
commit 02563a4c87
8 changed files with 710 additions and 84 deletions

7
.gitignore vendored
View file

@ -44,3 +44,10 @@ config.codekit3
/src/com
/src/style
/src/node_modules
/src/com/Base.js
/src/com/controllers/NavIndex.js
/src/com/controllers/PageEditor.js
/src/com/controllers/SettingsIndex.js
/src/libraries/FipamoAPI.js
/src/package-lock.json
/src/package.json

View file

@ -8,6 +8,13 @@ class InitAPI
public static function handleInitTasks($task, $request)
{
//check if a site config already exists. if it does, deny set up request
//restore to previous version of site while a config exists is only accessible
//through settings.
if (Setup::status()) {
$result = ["type" => "blogInitFail", "message" => "Site already set up"];
} else {
switch ($task) {
case "init":
$result = Setup::init($request);
@ -16,6 +23,7 @@ class InitAPI
$result = Setup::restore($request);
break;
}
}
return $result;
}

View file

@ -20,7 +20,15 @@ class APIControl
switch (isset($args["third"]) ? $args["third"] : "none") {
case "status":
if (Member::verifyKey($_GET["key"])) {
$result = AuthAPI::status();
} else {
$result = [
"message" => "Valid key required. API access denied, homie",
"type" => "API_ERROR",
];
}
break;
case "page":
//echo
@ -117,7 +125,21 @@ class APIControl
}
break;
case "login": //move to 'api/auth'
//check if request is remote and if so, verify token
if ($body["remote"] || $body["remote"] == "true") {
if (Member::verifyKey($body["key"])) {
$result = AuthAPI::login($body);
} else {
$result = [
"message" => "API access denied, homie",
"type" => "API_ERROR",
];
}
} else {
//request is local, so it's cool
$result = AuthAPI::login($body);
}
break;
case "logout": //move to 'api/auth'
$result = AuthAPI::logout($body);

View file

@ -48,7 +48,7 @@ class Auth
];
$token = Token::create(
$found["id"],
$found["key"],
$found["secret"],
time() + 3600,
"localhost"

File diff suppressed because one or more lines are too long

View file

@ -10,52 +10,47 @@ export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
export const API_STATUS = "/api/v1/status";
export const API_INIT = "/api/v1/init";
export const API_RESTORE = "/api/v1/restore";
export const API_LOGIN = "/api/v1/login";
export const API_GET_PAGES = "/api/v1/page/published";
export const API_GET_PAGE = "/api/v1/page/single";
export const API_GET_SECRET = "/api/v1/get-secret";
export const API_RESET_PASS = "/api/v1/reset-password";
export const API_CREATE_BACKUP = "/api/v1/backup";
export const API_DOWNLOAD_BACKUP = "/api/v1/backup/download";
export const API_RESTORE_BACKUP = "/api/v1/backup/restore";
export const API_UPLOAD_AVATAR = "/api/v1/settings/add-avatar";
export const API_UPLOAD_BACKGROUND = "/api/v1/settings/add-feature-background";
export const API_IMAGE_UPLOAD = "/api/v1/page/add-entry-image";
//** API TASKS **//
export const AUTH_STATUS = "getAuthStatus";
export const TASK_SITE_INIT = "blogInit";
export const TASK_BACKUP_RESTORE = "downloadBackup";
export const TASK_BACKUP_RESTORE = "restoreBackup";
export const TASK_BACKUP_CREATE = "createBackup";
export const TASK_GET_SECRET = "retrieveSecret";
export const TASK_RESET_PASS = "resetPassword";
export const TASK_GET_CONTENT = "retrieveContent";
export const TASK_UPLOAD_FILES = "uploadFiles";
export default class FipamoAPI {
//--------------------------
// constructor
//--------------------------
constructor(baseURL = null) {
/**
* A tub of methods for creating/restoring installs, resetting passwords and uploading images.
*/
class FipamoUtilityAPI {
/**
* @constructor
* @param {string} baseURL - url of site; uses local when empty
* @param {string} key - user api key
*/
constructor(baseURL = null, key = null) {
this.baseURL = null;
if (baseURL) {
this.baseURL = baseURL;
this.key = null;
if (key) this.key = key;
if (baseURL) this.baseURL = baseURL;
}
}
//--------------------------
// methods
//--------------------------
//** MEMBER AUTH METHODS **//
login(data) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + API_LOGIN : API_LOGIN,
AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
init(data) {
/**
* Promise method used create new site from scratch. For local use only.
* @param {object} data - json object that contains data for set up
* @property {string} new_member_handle - handle for new user
* @property {string} new_member_email - email for new user
* @property {string} new_member_pass - password for new user
* @property {string} new_member_title - title for new user
*/
create(data) {
return new Promise((resolve, reject) => {
this._request(
API_INIT,
@ -72,8 +67,14 @@ export default class FipamoAPI {
});
});
}
handleInitRestore(form) {
/**
* Promise method for restoring site from a previous back up. For local use only.
* @param {object} form - form object that contains restore data and files
* @property {string} restore_member_handle - handle for site user
* @property {string} restore_member_pass - password for site user
* @property {file} backup-upload - backup zip file
*/
restore(form) {
return new Promise((resolve, reject) => {
var url, event, method, type, data;
@ -91,8 +92,37 @@ export default class FipamoAPI {
});
});
}
/**
* Promise method for creating a zip back up of current site. For local use only.
*/
getSecret(data) {
backup() {
return new Promise((resolve, reject) => {
var url, event, method, type, data;
url = API_CREATE_BACKUP;
event = TASK_BACKUP_CREATE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_JSON;
data = { task: "create_backup" };
this._request(url, event, method, type, data)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Promise method for retrieving user secret key. For local use only.
* @param {object} data - json object that contains data for set up
* @property {string} email - email for site user
*/
secret(data) {
return new Promise((resolve, reject) => {
this._request(
API_GET_SECRET,
@ -109,8 +139,15 @@ export default class FipamoAPI {
});
});
}
/**
* Promise method for resetting password for user. For local use only.
* @param {object} data - json object that contains data for set up
* @property {string} new_password - password for user
* @property {string} new_password2 - confirm password for user
* @property {string} secret - secret key for user
*/
setNewPass(data) {
newPass(data) {
return new Promise((resolve, reject) => {
this._request(
API_RESET_PASS,
@ -128,21 +165,57 @@ export default class FipamoAPI {
});
}
//** CONTENT API METHODS **//
getPages(num, key) {
let pageNum = num;
if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1;
/**
* Promise method for uploading images [todo: change to uploading files]
* @param {string} type - type of upload
* @param {input} files - form input containing files
*/
imageUpload(type, files) {
return new Promise((resolve, reject) => {
let url = "";
switch (type) {
case "avatar-upload":
url = API_UPLOAD_AVATAR;
break;
case "background-upload":
url = API_UPLOAD_BACKGROUND;
break;
default:
url = API_IMAGE_UPLOAD;
break;
}
var imageData = new FormData();
if (this.baseURL) {
imageData.append("key", this.key);
imageData.append("remote", true);
} else {
imageData.append("remote", false);
}
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match("image.*")) {
continue;
}
if (type === "avatar-upload") {
imageData.append("avatar_upload", file, file.name);
} else if (type === "background-upload") {
imageData.append("background_upload", file, file.name);
} else {
imageData.append("post_image", file, file.name);
}
}
this._request(
this.baseURL
? this.baseURL + API_GET_PAGES + "?key=" + key
: API_GET_PAGES,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
url,
TASK_UPLOAD_FILES,
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
imageData
)
.then((result) => {
resolve(result);
.then((r) => {
resolve(r);
})
.catch((err) => {
reject(err);
@ -150,24 +223,6 @@ export default class FipamoAPI {
});
}
getPage(id, key) {
return new Promise((resolve, reject) => {
this._request(
//API_GET_PAGE + "/" + id + "?key=" + key,
this.baseURL
? this.baseURL + API_GET_PAGE + "/" + id + "?key=" + key
: API_GET_PAGE,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
//--------------------------
// private
//--------------------------
@ -225,3 +280,5 @@ export default class FipamoAPI {
//pass element to display request progress
}
}
export { FipamoUtilityAPI as default };

View file

@ -0,0 +1,349 @@
//** REQUEST TYPES **//
export const REQUEST_TYPE_POST = "POST";
export const REQUEST_TYPE_GET = "GET";
export const REQUEST_TYPE_PUT = "PUT";
export const REQUEST_TYPE_DELETE = "DELETE";
//** POST CONTENT TYPES **//
export const CONTENT_TYPE_JSON = "json";
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
//** API URLS **//
export const API_STATUS = "/api/v1/status";
export const API_GET_NAV = "/api/settings/nav";
export const API_NEW_PAGE = "/api/v1/page/create";
export const API_EDIT_PAGE = "/api/v1/page/write";
export const API_DELETE_PAGE = "/api/v1/page/delete";
export const API_SETTINGS_SYNC = "/api/v1/settings/sync";
export const API_PUBLISH_PAGES = "/api/v1/settings/publish";
export const API_NAV_SYNC = "/api/v1/settings/nav-sync";
export const API_REINDEX_PAGES = "/api/v1/settings/reindex";
export const API_SEND_MAIL = "/api/v1/mailer";
export const API_LOGIN = "/api/v1/login";
//** API TASKS **//
export const AUTH_STATUS = "getAuthStatus";
export const TASK_SETTINGS_WRITE = "writeSettings";
export const TASK_PUBLISH_SITE = "publishSite";
export const TASK_PAGE_CREATE = "createNewPage";
export const TASK_PAGE_EDIT = "editPage";
export const TASK_PAGE_DELETE = "deletePage";
export const TASK_SEND_MAIL = "sendMail";
export const TASK_REINDEX_PAGE = "reIndexPages";
//** API STATUS **//
export const API_ACCESS_GOOD = "apiUseAuthorized";
export const API_ACCESS_BAD = "apiUseNotAuthorized";
/**
* A can of methods used to edit install settings, navigation pages.
*/
class FipamoAdminAPI {
/**
* @constructor
* @param {string} baseURL - url of site; uses local when empty
* @param {string} key - user api key
*/
constructor(baseURL = null, key = null) {
this.percentComplete = 0; //for later
this.token = null;
this.baseURL = null;
this.key = null;
if (key) this.key = key;
if (baseURL) this.baseURL = baseURL;
//if key is valid, checks to see if a session is active and returns
this._request(
this.baseURL
? this.baseURL + API_STATUS + "?key=" + this.key
: API_STATUS + "?key=" + this.key
).then((response) => {
if (response.type === API_ACCESS_GOOD) {
this.token = response.token;
} else {
//don't set token
//console.log("NO TOKEN");
}
});
}
/**
* Promise method for checking credentials. Must login to use Admin API.
* @param {object} data - json object that contains data for set up
* @property {string} handle - handle for site user
* @property {string} password - password for site user
*/
login(data) {
return new Promise((resolve, reject) => {
this.baseURL ? (data.remote = true) : (data.remote = false);
this.key ? (data.key = this.key) : (data.key = null);
this._request(
this.baseURL ? this.baseURL + API_LOGIN : API_LOGIN,
AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Method for saving settings
* @param {object} data - json object that contains settings data set on the front-end
* @property {string} global.base_url - base url for site
* @property {string} global.title - site title
* @property {string} global.descriptions - brief site summary
* @property {string} global.background - url for site feature image for header
* @property {boolean} global.private - privacy state for site [disabled]
* @property {boolean} global.renderOnSave - property for publishing site when page saved [disabled]
* @property {string} global.theme - current theme for site
* @property {boolean} global.externalAPI - toggle for external API access
* @property {string} member.handle - current member handle
* @property {string} member.email - current member email
* @property {string} email.active - current email protocol being used
* @property {string} email.smtp.domain - url of smtp service being
* @property {string} email.smtp.email - email account of smtp service
* @property {string} email.smtp.password - password for email of smtp service
* @property {string} email.mailgun.domain - mailgun domain url
* @property {string} email.mailgun.key - mailgun key
*/
syncSettings(data) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL
? this.baseURL + API_SETTINGS_SYNC + "?key=" + this.key
: API_SETTINGS_SYNC + "?key=" + this.key,
TASK_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Method for retrieving user authorizing user login
* @param {object} data - json object that contains task
* @property {string} task - publishing task [deprecated]
*/
publishSite(data) {
return new Promise((resolve, reject) => {
this._request(
//API_PUBLISH_PAGES,
this.baseURL ? this.baseURL + API_PUBLISH_PAGES : API_PUBLISH_PAGES,
TASK_PUBLISH_SITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Method for handling page creating and editing
* @param {string} task - current page action
* @param {object} data - form object that contains info for current page being edited/created
* @property {string} id - sequence id for page
* @property {string} uuid - unique identifier for page
* @property {string} layout - current page layout
* @property {string} current_title - saved url save title for persistence when changing title
* @property {string} content - markdown body of page
* @property {string} title - current title of page
* @property {string} created - date page was created
* @property {string} slug - url safe string of page title
* @property {string} tags - comma separated list of tags
* @property {boolean} menu - property that indicates page is included in site menu
* @property {boolean} featured - property that indicates page is featured
* @property {boolean} published - property that indicates page is public
* @property {input} feature_image - main image for page
*/
pageActions(task, data) {
let url, event, content;
switch (task) {
case TASK_PAGE_CREATE:
url = API_NEW_PAGE;
event = TASK_PAGE_CREATE;
content = CONTENT_TYPE_FORM;
break;
case TASK_PAGE_EDIT:
url = API_EDIT_PAGE;
event = TASK_PAGE_EDIT;
content = CONTENT_TYPE_FORM;
break;
case TASK_PAGE_DELETE:
url = API_DELETE_PAGE;
event = TASK_PAGE_DELETE;
content = CONTENT_TYPE_JSON;
break;
default:
break;
}
if (this.baseURL) {
data.key = this.key;
data.remote = true;
} else {
data.remote = false;
}
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + url : url,
event,
REQUEST_TYPE_POST,
content,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Method for saving pages to be included in main site navigation
* @param {object} data - json object that contains items to be included in main site navigation
* @property {string} item.title - page title
* @property {string} item.slug - url safe title
* @property {string} item.uuid - unique identifier
* @property {string} item.path - directory path to associated markdown file
*/
syncNav(data) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + API_NAV_SYNC : API_NAV_SYNC,
TASK_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Method for sending mail
* @param {object} message - json object that contains items to be included in main site navigation
* @property {string} content - message to send
*/
sendMail(message) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + API_SEND_MAIL : API_SEND_MAIL,
TASK_SEND_MAIL,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
message
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Maintenance method to clean up page sequencing [disabled]
* @param {object} data - json object that contains items to be included in main site navigation
*/
reindexPages(data) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + API_REINDEX_PAGES : API_REINDEX_PAGES,
TASK_REINDEX_PAGE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
//--------------------------
// private
//--------------------------
_request(
requestURL,
eventType,
requestType = REQUEST_TYPE_GET,
contentType = CONTENT_TYPE_JSON,
requestData = null
) {
var self = this;
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.upload.onprogress = self.handleLoadProgress;
request.open(requestType, requestURL, true);
request.onload = () => {
if (request.status == 200) {
let response = JSON.parse(request["response"]);
resolve(response);
} else {
let error = JSON.parse(request["response"]);
reject(error);
}
};
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
if (
eventType === TASK_SETTINGS_WRITE ||
eventType === TASK_PAGE_EDIT ||
eventType === TASK_PAGE_DELETE ||
eventType === TASK_PUBLISH_SITE ||
eventType === TASK_REINDEX_PAGE
)
request.setRequestHeader("fipamo-access-token", self.token);
switch (contentType) {
case CONTENT_TYPE_JSON:
request.setRequestHeader(
"Content-type",
"application/" + contentType
);
request.send(JSON.stringify(requestData));
break;
case CONTENT_TYPE_FORM:
request.send(requestData);
break;
}
} else {
request.send();
}
});
}
//--------------------------
// event handlers
//--------------------------
handleLoadProgress(e) {
this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
//pass element to display request progress
}
}
export { FipamoAdminAPI as default };

View file

@ -0,0 +1,183 @@
//** REQUEST TYPES **//
export const REQUEST_TYPE_POST = "POST";
export const REQUEST_TYPE_GET = "GET";
export const REQUEST_TYPE_PUT = "PUT";
export const REQUEST_TYPE_DELETE = "DELETE";
//** POST CONTENT TYPES **//
export const CONTENT_TYPE_JSON = "json";
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
//** API URLS **//
export const API_GET_PAGES = "/api/v1/page/published";
export const API_GET_FEATURED = "/api/v1/page/featured";
export const API_GET_PAGE = "/api/v1/page/single";
export const API_GET_MENU = "/api/v1/page/menu";
export const API_GET_TAGS = "/api/v1/page/tags";
//** API TASKS **//
export const TASK_GET_CONTENT = "retrieveContent";
/**
* Fipamo Content API
* A bag of methods for getting page info from an install.
* To use remotely, include url of install and user key found in settings in the Dashboard.
*/
class FipamoContentAPI {
/**
* @constructor
* @param {string} baseURL - url of site; uses local when empty
* @param {string} key - user api key
* @author Ro
*/
constructor(baseURL = null, key = null) {
this.baseURL = null;
this.key = null;
if (key) this.key = key;
if (baseURL) this.baseURL = baseURL;
}
/**
* Promise method for retrieving page data
* @param {string} type - type of pages being retrieved; null value defaults to published
* @example
* api.pages('published').then(pages=>{
* console.log("Pages Object", pages);
* })
* @returns {object} json object that contains pages of requested type
*
*/
pages(type = null) {
//set url based on request type
let requestURL = "";
switch (type) {
default:
case "published":
requestURL = API_GET_PAGES + "?key=" + this.key;
break;
case "featured":
requestURL = API_GET_FEATURED + "?key=" + this.key;
break;
case "menu":
requestURL = API_GET_MENU + "?key=" + this.key;
break;
}
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + requestURL : requestURL,
TASK_GET_CONTENT
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Promise method for retrieving single page
* @param {string} id - uuid of desired page
* @example
* api.page("a-uuid-for-a-page").then(page=>{
console.log("Page Object", page);
* })
* @returns {object} json object that contains data for requested page
*/
page(id) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL
? this.baseURL + API_GET_PAGE + "/" + id + "?key=" + this.key
: API_GET_PAGE + "/" + id + "?key=" + this.key,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
/**
* Promise method for retrieving all tags used by pages
* @example
* api.tags().then(tags=>{
console.log("Tags Object", tags);
* })
* @returns {object} json object that contains tags used by pages
*/
tags() {
return new Promise((resolve, reject) => {
this._request(
this.baseURL
? this.baseURL + API_GET_TAGS + "?key=" + this.key
: API_GET_TAGS + "?key=" + this.key,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
//--------------------------
// private
//--------------------------
_request(
requestURL,
eventType,
requestType = REQUEST_TYPE_GET,
contentType = CONTENT_TYPE_JSON,
requestData = null
) {
var self = this;
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.upload.onprogress = self.handleLoadProgress;
request.open(requestType, requestURL, true);
request.onload = () => {
if (request.status == 200) {
let response = JSON.parse(request["response"]);
resolve(response);
} else {
let error = JSON.parse(request["response"]);
reject(error);
}
};
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
switch (contentType) {
case CONTENT_TYPE_JSON:
request.setRequestHeader(
"Content-type",
"application/" + contentType
);
request.send(JSON.stringify(requestData));
break;
case CONTENT_TYPE_FORM:
request.send(requestData);
break;
}
} else {
request.send();
}
});
}
//--------------------------
// event handlers
//--------------------------
handleLoadProgress(e) {
this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
//pass element to display request progress
}
}
export { FipamoContentAPI as default };