Skip to main content

๐Ÿ”จ System Calls and Common Functions

Applications can directly use the built-in classes and methods of the emlog system, which can improve development efficiency. Common methods and functions can be directly used for template and plugin development.

Input and Outputโ€‹

Input Class: Get GET, POST Variablesโ€‹

Please use the core Input class to get variables submitted by GET and POST. Do not use $a = $_POST['xxxx'] directly to get them, which may cause security problems such as SQL injection.

// Read string submitted via POST, default value is empty
$var_name = Input::postStrVar('var_name', '');
// Read number type submitted via POST, default value is 0
$var_name = Input::postIntVar('var_name', 0);

// Read string submitted via GET, default value is empty
$var_name = Input::getStrVar('var_name', '');
// Read number type submitted via GET, default value is 0
$var_name = Input::getIntVar('var_name', 0);

// Read number type array submitted via POST, e.g.: name="ids[]", default value is: []
$logs = Input::postIntArray('blog');
// Read string type array submitted via POST, e.g.: name="someting[]", default value is: []
$logs = Input::postStrArray('blog');

// Read string submitted via GET, POST, and COOKIE, default value is empty
$var_name = Input::requestStrVar('var_name', '');
// Read number type submitted via GET, POST, and COOKIE, default value is 0
$var_name = Input::requestNumVar('var_name', 0);

Output Class: JSON Output Classโ€‹

Can quickly output successful and error data structures in JSON format.

Output::ok();

// Output success
Output::ok();

// Output success, with returned data
$data = ['id'=>1];
Output::ok($data);

Output::error();

// Output error
Output::error('id error');

// Output error, return http status 200
Output::error('id error', 200);

Output JSON format:

{
"code": 0,
"msg": "",
"data": ""
}

Get System Settingsโ€‹

Use the static method get of the Option class to get some system setting options, as follows:

Option::get('att_maxsize') // File upload max size limit
Option::get('att_type') // Allowed file upload types
Option::get('att_imgmaxw') // Upload image thumbnail generation, max width
Option::get('att_imgmaxh') // Upload image thumbnail generation, max height

Get URL Routeโ€‹

Get the URL route information currently visited by the user, which is convenient for themes to output personalized content for different URLs.

$D = Dispatcher::getInstance();
echo $D->_path; // Currently visited URL path
echo $D->_model; // Module routed by the system
echo $D->_params; // Parameters captured by the system route

Payment Integrationโ€‹

Order Systemโ€‹

emlog core has built-in order table and order data model to manage orders, see: include/model/order_model.php

Create Orderโ€‹

// app_name: Application name, such as the English alias of the theme or plugin, used to distinguish orders of different applications
// pay_type: Payment type, e.g.: alipay, wechat, credit (points payment)
// sku_name: Product type, e.g. resource, article, credits, vip, etc.: resource, article, credits, vip
// sku_id: Product ID, e.g.: article ID, resource id, and other custom product IDs
// price: Price
// UID: Current User ID
$pay_type = 'wechat';
$sku_name = 'app';
$sku_id = 1;
$price = 100;
$Order_Model = new Order_Model('app_name');
$order_id = $Order_Model->createOrder(UID, $pay_type, $sku_name, $sku_id, $price);

Query Order by Order IDโ€‹

$order_id = Input::postStrVar('order_id');
$Order_Model = new Order_Model('app_name');
$order = $Order_Model->getOrderById($order_id);

Update Order Statusโ€‹

$Order_Model = new Order_Model('app_name');
$data = [
'order_id' => $out_trade_no, // Order number called back by payment platform
'pay_price' => $result['total_amount'], // Payment amount called back by payment platform
'out_trade_no' => $result['trade_no'], // Transaction number called back by payment platform
'update_time' => time(),
];
$r = $Order_Model->updateOrder($out_trade_no, $data);

Order Data Structureโ€‹

The following table shows the fields of the order data structure and their corresponding type descriptions.

Field NameTypeDescription
idIntegerOrder ID
order_uidIntegerUser ID
sku_nameStringProduct Name, e.g.: resource, app, vip, article, etc.
sku_idIntegerProduct ID, e.g. Article ID
priceFloatOrder Price
pay_priceFloatPayment Amount
refund_amountFloatRefund Amount
update_timestampTimestampUpdate Timestamp
create_timestampTimestampCreate Timestamp
update_timeStringUpdate Date Time
create_timeStringCreate Date Time
out_trade_noStringExternal Transaction Number, e.g. serial number returned by Alipay
pay_typeStringPayment Method, e.g.: alipay, wechat

AI Integrationโ€‹

Get Text Chat Model Configurationโ€‹

// Returns: api_url, api_key, model
AI::getCurrentModelInfo();

AI Chatโ€‹

$prompt = 'hello';
$response = AI::chat($prompt);
echo $response;

Get Image Generation Model Configurationโ€‹

// Returns: api_url, api_key, model
AI::getCurrentImageModelInfo();

Generate Image and Save to Resource Libraryโ€‹

$prompt = 'Draw a kitten';
$response = AI::generateImageAndSave($prompt);
echo $response;

Get User Informationโ€‹


$userModel = new User_Model();

// Get user info
$user = $userModel->getOneUser($uid);

$avatar = $user['photo']; // Avatar
$nickname = $user['nickname']; // Nickname
$credits = $user['credits']; // Credits

Credits Add/Deductโ€‹


$userModel = new User_Model();

// Add credits for user
$userModel->addCredits($uid, 100);

// Deduct user credits
$userModel->reduceCredits($uid, 50);

Applications can use the core database model to query and process articles, resources, users, and other data. Related models and methods can be found in the Model class in the include/model directory.

Global Functionsโ€‹

Send Email Notificationโ€‹

$mail = 'xxx@qq.com';
$title = 'Email Title';
$content = 'Email Content';
Notice::sendMail($mail, $title, $content);

Truncate Content to Specified Lengthโ€‹

// 1st parameter: content to be truncated
// 2nd parameter: truncated length
// 3rd parameter: whether to filter html tags in content, 1 filter, 0 do not filter
// As follows: Truncate the first 180 characters of the log content and filter html tags

echo subContent($value['log_description'], 180, 1);

Get Article URLโ€‹

// Parameter 1: $article_id Article ID
<?= Url::log($article_id)?>

Get Article Category Page URLโ€‹

// Parameter 1: $sort_id Category ID
<?= Url::sort($sort_id)?>

Get User IPโ€‹

echo getIp();

Get First Image in Contentโ€‹

// Parameter $content: article content, can be markdown or html format content
// Returns: Image URL
$imageUrl = getFirstImage($content);

Get User Gravatar Avatarโ€‹

// Parameter: email
// Returns: Avatar URL
$avatar = getGravatar($email);

Friendly Time Descriptionโ€‹

// Parameter: $timestamp unix timestamp
// Returns: Friendly time description, e.g.: 1 minute ago
echo smartDate($timestamp);

Output Friendly Prompt Messageโ€‹

Message content supports html

emMsg('Please install <a href="/admin/store.php?keyword=ๆ”ฏไป˜sdk">Payment SDK Plugin</a> first');