Wordpress wp_insert_post fire multiple times on mobile devices - php

I have built a custom front-end multipage donation form on Wordpress, saving the data via session variables across the pages. I then use save_post hook to run a function to redirect the user, after submitting the form, to an online payment portal. The problem is, when users access the form via mobile, the function wp_insert_post fires multiple time.
This is the code that I have on the php page used for processing the data from the form.
<?php
header('Cache-Control: no cache'); //no cache
session_cache_limiter('private_no_expire'); // works
//session_cache_limiter('public'); // works too
//let's start the session
session_start();
require_once 'library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
//LISTING ALL VARIABLES AVAILABLE FROM FRONTEND FORM
$negara = $_SESSION['negara'];
$binatang = $_SESSION['binatang'];
if($_SESSION['ekor']) {
$ekorBahagian = $_SESSION['ekor'];
} else {
$ekorBahagian = $_SESSION['bahagian'];
}
$nama1 = $_SESSION['nama'];
$kp1 = $_SESSION['kp'];
$telefon1 = $_SESSION['telefon'];
$emel1 = $_SESSION['emel'];
$alamat11 = $_SESSION['alamat1'];
$alamat21 = $_SESSION['alamat2'];
$poskod1 = $_SESSION['poskod'];
$bandar1 = $_SESSION['bandar'];
$negeri1 = $_SESSION['negeri'];
$peserta1 = $_SESSION['peserta'];
$kempen = $_POST['kempen'];
$bank = $_POST['bank'];
if($telefon1) {
$mobile = preg_replace("/[^0-9]/", "", $telefon1);
$custTel = $mobile;
$custTel2 = substr($mobile, 0, 1);
if ($custTel2 == '+') {
$custTel3 = substr($mobile, 1, 1);
if ($custTel3 != '6') {
$custTel = "+6" . $mobile;
}
} elseif ($custTel2 == '6') {
} else {
if ($custTel != '') {
$custTel = "+6" . $mobile;
}
}
}
//purifying the texts
$nama = $purifier->purify($nama1);
$kp = $purifier->purify($kp1);
$telefon = $purifier->purify($custTel);
$emel = $purifier->purify($emel1);
$alamat1 = $purifier->purify($alamat11);
$alamat2 = $purifier->purify($alamat21);
$poskod = $purifier->purify($poskod1);
$bandar = $purifier->purify($bandar1);
$negeri = $purifier->purify($negeri1);
$peserta = $purifier->purify($peserta1);
if($_SESSION['ekor']) {
$bil = $_SESSION['ekor']; //capturing bilangan ekor into a var
switch ($_SESSION['negara']){
case 'Malaysia':
$jumlahHarga = $bil*(650*7);
break;
case 'ASEAN':
$jumlahHarga = $bil*(450*7);
break;
case 'Timur Tengah':
$jumlahHarga = $bil*(1300*7);
break;
case 'Afrika':
$jumlahHarga = $bil*(350*7);
break;
default:
}
} else {
$bil = $_SESSION['bahagian']; //capturing bilangan bahagian into a var
switch ($_SESSION['negara']){
case 'Malaysia':
$jumlahHarga = $bil*650;
break;
case 'ASEAN':
$jumlahHarga = $bil*450;
break;
case 'Timur Tengah':
$jumlahHarga = $bil*1300;
break;
case 'Afrika':
$jumlahHarga = $bil*350;
break;
default:
}
}
$post = array(
'post_title' => wp_strip_all_tags( $nama ),
'post_status' => 'publish',
'post_type' => 'qurban',
'meta_input' => array(
'pilihan_negara' => $negara,
'pilihan_lembu' => $binatang,
'bilangan_ekorbahagian' => $ekorBahagian,
'jumlah_bayaran' => $jumlahHarga,
'nama_penuh' => $nama,
'nombor_kad_pengenalan' => $kp,
'nombor_telefon' => $telefon,
'emel' => $emel,
'alamat_rumah_1' => $alamat1,
'alamat_rumah_2' => $alamat2,
'poskod' => $poskod,
'bandar' => $bandar,
'negeri' => $negeri,
'senarai_nama_peserta' => $peserta,
'bank' => $bank,
'kempen' => $kempen
)
);
$post_id = wp_insert_post($post);
get_header();
?>
<?php
get_footer();
?>
Below is the code that uses save_post to redirect the user to external payment site:
?php
require_once "Mobile_Detect.php";
////////////////////////////
//// PAYMENT REDIRECTION FUNCTION
////////////////////////////
function my_save_post_iq( $post_id ) {
debug_to_console( "save post function fired" );
$billplzApi = get_field('iq_secret_key', 'option');
$billplzId = get_field('iq_collection_id', 'option');
// bail early if not a donation post
if( get_post_type($post_id) !== 'qurban' ) {
return;
}
// bail early if editing in admin
if( is_admin() ) {
return;
}
$post = get_post( $post_id);
$jumlah_bayaran_iq = get_field('jumlah_bayaran', $post_id);
//check & update user device type
$detect = new Mobile_Detect;
if($detect->isMobile()){
update_field('devices', 'mobile' , $post);
} else {
update_field('devices', 'desktop', $post);
}
$name = get_field('nama_penuh', $post);
$email = get_field('emel', $post);
$mobile = get_field('nombor_telefon', $post);
$bank = get_field('bank', $post);
$billplz_data = array(
'amount' => $jumlah_bayaran_iq * 100,
'name' => $name,
'mobile' => $mobile,
'email' => $email,
'collection_id' => $billplzId,
'deliver' => false,
'reference_1_label' => 'Bank Code',
'reference_1' => $bank,
'reference_2_label' => 'Post ID',
'reference_2' => $post_id,
'description' => 'xxx',
'redirect_url' => home_url('qurbanv2/paymentredirectv2'),
'callback_url' => home_url('paymentcallback')
);
$process = curl_init('https://www.billplz.com/api/v3/bills/');
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $billplzApi . ":");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($billplz_data));
$return = curl_exec($process);
curl_close($process);
$arr = json_decode($return, true);
$billplz_url = $arr['url'];
$billplz_id = $arr['id'];
//update payment status
update_field('billplz_iq', $billplz_id , $post);
//$hzpost_name = array(
// 'ID' = $post,
// 'post_name' = $billplz_url
//);
//
//wp_update_post($hzpost_name);
header('Location: '. $billplz_url . '?auto_submit=true');
exit();
}
add_action('save_post', 'my_save_post_iq', 10, 1);
I am new to Wordpress development, so please help.

Found the bug. It's got to do with how the payment gateway treats mobile number. I've added a line to add '+' in front of the numbers, and the duplication stops.
if($telefon1) {
$mobile = preg_replace("/[^0-9]/", "", $telefon1);
$custTel = $mobile;
$custTel2 = substr($mobile, 0, 1);
if ($custTel2 == '+') {
$custTel3 = substr($mobile, 1, 1);
if ($custTel3 != '6') {
$custTel = "+6" . $mobile;
}
} elseif ($custTel2 == '6') {
$custTel = "+" . $mobile; //added this line.
} else {
if ($custTel != '') {
$custTel = "+6" . $mobile;
}
}
}

Related

database is not updating the values after successful transaction through payment gateway

My website's framework is in CodeIgniter. I have integrated instamojo payment gateway in it. The query inserting the payment id but after successful payment not updating the column value of status to 1 from 0,
This is the block code of paymentcontroller.php
public static function userDataUpdate($trx)
{
$general = getGeneral();
$data = Deposit::where('trx', $trx)->first();
if ($data->status == 0) {
$data->status = 1;
$data->save();
$user = User::find($data->user_id);
$wallet = $user->wallet;
$wallet->balance += $data->amount;
$wallet->save();
$transaction = new Transaction();
$transaction->user_id = $data->user_id;
$transaction->amount = $data->amount;
$transaction->post_balance = $wallet->balance;
$transaction->charge = $data->charge;
$transaction->trx_type = '+';
$transaction->details = 'Deposited via ' . $data->gatewayCurrency()->name;
$transaction->trx = $data->trx;
$transaction->save();
$adminNotification = new AdminNotification();
$adminNotification->user_id = $user->id;
$adminNotification->title = 'Deposit succeeded via '.$data->gatewayCurrency()->name;
$adminNotification->click_url = urlPath('admin.deposit.successful');
$adminNotification->save();
notify($user, 'DEPOSIT_COMPLETE', [
'method_name' => $data->gatewayCurrency()->name,
'method_currency' => $data->method_currency,
'method_amount' => showAmount($data->final_amo),
'amount' => showAmount($data->amount),
'charge' => showAmount($data->charge),
'currency' => $general->cur_text,
'rate' => showAmount($data->rate),
'trx' => $data->trx,
'post_balance' => showAmount($wallet->balance)
]);
}
}
This is the code of processcontroller.php
<?php
namespace App\Http\Controllers\Gateway\Instamojo;
use App\Models\Deposit;
use App\Http\Controllers\Gateway\PaymentController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ProcessController extends Controller
{
/*
* Instamojo Gateway
*/
public static function process($deposit)
{
$basic = getGeneral();
$instaMojoAcc = json_decode($deposit->gatewayCurrency()->gateway_parameter);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payment-requests/');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
"X-Api-Key:$instaMojoAcc->api_key",
"X-Auth-Token:$instaMojoAcc->auth_token"
)
);
$payload = array(
'purpose' => 'Payment to ' . $basic->sitename,
'amount' => round($deposit->final_amo,2),
'buyer_name' => $deposit->user->username,
'redirect_url' => route('user.deposit.history'),
'webhook' => route('ipn.'.$deposit->gateway->alias),
'email' => $deposit->user->email,
'btc_wallet' => $deposit->trx,
'send_email' => true,
'allow_repeated_payments' => false
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
curl_close($ch);
$res = json_decode($response);
if (#$res->success) {
if(!#$res->payment_request->id){
$send['error'] = true;
$send['message'] = "Response not given from API. Please re-check the API credentials.";
}else{
$deposit->btc_wallet = $res->payment_request->id;
$deposit->save();
$send['redirect'] = true;
$send['redirect_url'] = $res->payment_request->longurl;
$send['btc_wallet'] = $deposit->trx;
}
} else {
$send['error'] = true;
$send['message'] = "Credentials mismatch. Please contact with admin";
}
return json_encode($send);
}
public function ipn(Request $request)
{
//$this->customLog($request->all(),'Instamojo',true);
$deposit = Deposit::where('btc_wallet', $_POST['payment_request_id'])->orderBy('id', 'DESC')->first();
$instaMojoAcc = json_decode($deposit->gatewayCurrency()->gateway_parameter);
$deposit->detail = $request->all();
$deposit->save();
$imData = $_POST;
$macSent = $imData['mac'];
unset($imData['mac']);
ksort($imData, SORT_STRING | SORT_FLAG_CASE);
$mac = hash_hmac("sha1", implode("|", $imData), $instaMojoAcc->salt);
if ($macSent == $mac && $imData['status'] == "Credit" && $deposit->status == '0') {
PaymentController::userDataUpdate($deposit->trx);
}
}
}
I am unable to find out why the query is not updating the status of the transaction in mysqli.

Opencart API : Issue with session app_id (how to get api_id)

I am working on Opencart API (opencart v2.3) and I follow this link for documentation (Opencart ) . But there is no data on opencart APIs and how to use it, So I follow steps from other websites and using that code I receive this message when call login api, Success: API session successfully started!
But whenever I use another API for add product in cart or view cart or add order, I receive permission issue. I debug code and found that it required session app_id and when I check, it store only token, not app_id
I use following code which I found by googling.
common.php
<?php
function do_curl_request($url, $params=array()) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'E:\practice\oc2.3\tmp\apicookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'E:\practice\oc2.3\tmp\apicookie.txt');
$params_string = '';
if (is_array($params) && count($params)) {
foreach($params as $key=>$value) {
$params_string .= $key.'='.$value.'&';
}
rtrim($params_string, '&');
curl_setopt($ch,CURLOPT_POST, count($params));
curl_setopt($ch,CURLOPT_POSTFIELDS, $params_string);
}
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
login.php
<?php
require "common.php";
// set up params
$url = 'http://opencart2_3.local/index.php?route=api/restopencart/login';
$fields = array(
'key' => 'FpafURRNAHgVcaUXZozahVdEOV7mtp1Q0ejvAMAIAfiZyVqIptqZ2uV9eQvT3PytlzELULH1vQwLKikFGBOm3yky1rTuFO6sEi0eBkH1y6WgpaNWIsB0ZMiRCCbGCBZZak2uR1CBg0TpOzcbevXWGStvoUsaKgl0B3OKRoHk6mRj7e6S63HJQzQksbbz0JfCuZsY9cvhY4ArQPzNf3XfrdgE3nTG5hYQCXaKPVqtS3R2Vqr4sazwjgXYajy7h6Dv',
);
$json = do_curl_request($url, $fields);
$data = json_decode($json);
if (isset($data->token)) {
$token = $data->token;
}
var_dump($data);
add_product.php
<?php
require "common.php";
// set up params
$url = 'http://opencart2_3.local/index.php?route=api/restopencart/addproduct';
$fields = array(
'product_id' => '32',
'quantity' => '1',
'option[226]' => '15'
);
$json = do_curl_request($url, $fields);
$data = json_decode($json);
var_dump($data);
customer api
public function index() {
$this->load->language('api/customer');
// Delete past customer in case there is an error
unset($this->session->data['customer']);
$json = array();
if (!isset($this->session->data['api_id'])) {
$json['error']['warning'] = $this->language->get('error_permission');
} else {
// Add keys for missing post vars
$keys = array(
'customer_id',
'customer_group_id',
'firstname',
'lastname',
'email',
'telephone',
'fax'
);
foreach ($keys as $key) {
if (!isset($this->request->post[$key])) {
$this->request->post[$key] = '';
}
}
// Customer
if ($this->request->post['customer_id']) {
$this->load->model('account/customer');
$customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']);
if (!$customer_info || !$this->customer->login($customer_info['email'], '', true)) {
$json['error']['warning'] = $this->language->get('error_customer');
}
}
if ((utf8_strlen(trim($this->request->post['firstname'])) < 1) || (utf8_strlen(trim($this->request->post['firstname'])) > 32)) {
$json['error']['firstname'] = $this->language->get('error_firstname');
}
if ((utf8_strlen(trim($this->request->post['lastname'])) < 1) || (utf8_strlen(trim($this->request->post['lastname'])) > 32)) {
$json['error']['lastname'] = $this->language->get('error_lastname');
}
if ((utf8_strlen($this->request->post['email']) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) {
$json['error']['email'] = $this->language->get('error_email');
}
if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
$json['error']['telephone'] = $this->language->get('error_telephone');
}
// Customer Group
if (is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
$customer_group_id = $this->request->post['customer_group_id'];
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}
// Custom field validation
$this->load->model('account/custom_field');
$custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);
foreach ($custom_fields as $custom_field) {
if (($custom_field['location'] == 'account') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
} elseif (($custom_field['location'] == 'account') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
$json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
}
}
if (!$json) {
$this->session->data['customer'] = array(
'customer_id' => $this->request->post['customer_id'],
'customer_group_id' => $customer_group_id,
'firstname' => $this->request->post['firstname'],
'lastname' => $this->request->post['lastname'],
'email' => $this->request->post['email'],
'telephone' => $this->request->post['telephone'],
'fax' => $this->request->post['fax'],
'custom_field' => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array()
);
$json['success'] = $this->language->get('text_success');
}
}
if (isset($this->request->server['HTTP_ORIGIN'])) {
$this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
$this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
$this->response->addHeader('Access-Control-Max-Age: 1000');
$this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Put the token right after your request URL will make it works.
Assume token returned by api/login is KYMmXA4Bcj8nL9WD3nl0oalaJOL1KSKo.
add_product.php
<?php
require "common.php";
// set up params
$url = 'http://opencart2_3.local/index.php?route=api/restopencart/addproduct&token=KYMmXA4Bcj8nL9WD3nl0oalaJOL1KSKo';
$fields = array(
'product_id' => '32',
'quantity' => '1',
'option[226]' => '15'
);
$json = do_curl_request($url, $fields);
$data = json_decode($json);
var_dump($data);
Make sure server's IP address is added to the allowed IP addresses.
To check that, go to System → Users → API then edit the Default one.
Once there, click on IP Address tab and insert the server IP address.
To get the server IP address, you can use the following command line:
$ curl ipinfo.io/ip

PHP Loop Function with different parameters

I have a function which queries an api and outputs the response as an array. I can run this function once and then I can echo the array output.
But problem for me is, I can call this function once and have output. But I'd like to loop through these function parameters and call it for multiple usernames. Example:
<?php
require("./include/function.php");
$Player=fetchCharacterDescriptions("Senaxx", "2");
echo "<tr>";
echo "<th class=\"col-md-3\">" . $Player[0]['username'] . "</th>";
foreach ( $Player as $var )
{
echo "<th class=\"col-md-3\">",$var['class']," ",$var['light'],"</th>";
}
echo "</tr>";
echo "</thead>";
echo "</table>";
?>
And this call's the function fetchCharacterDescriptions in function.php which is:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$hash = array(
'3159615086' => 'Glimmer',
'1415355184' => 'Crucible Marks',
'1415355173' => 'Vanguard Marks',
'898834093' => 'Exo',
'3887404748' => 'Human',
'2803282938' => 'Awoken',
'3111576190' => 'Male',
'2204441813' => 'Female',
'671679327' => 'Hunter',
'3655393761' => 'Titan',
'2271682572' => 'Warlock',
'3871980777' => 'New Monarchy',
'529303302' => 'Cryptarch',
'2161005788' => 'Iron Banner',
'452808717' => 'Queen',
'3233510749' => 'Vanguard',
'1357277120' => 'Crucible',
'2778795080' => 'Dead Orbit',
'1424722124' => 'Future War Cult',
'2033897742' => 'Weekly Vanguard Marks',
'2033897755' => 'Weekly Crucible Marks',
);
function translate($x)
{
global $hash;
return array_key_exists($x, $hash) ? $hash[$x] : null;
}
//BungieURL
function callBungie($uri)
{
$apiKey = '145c4aff30864167ac4548c02c050679';
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-API-Key: ' . $apiKey
));
if (!$result = json_decode(curl_exec($ch) , true))
{
$result = false;
}
curl_close($ch);
return $result;
}
//Request Player
function fetchPlayer($username, $platform)
{
$result = false;
$uri = 'http://www.bungie.net/Platform/Destiny/SearchDestinyPlayer/' . $platform . '/' . $username;
$json = callBungie($uri);
if (isset($json['Response'][0]['membershipId']))
{
$result = array(
'membershipId' => $json['Response'][0]['membershipId'],
'membershipType' => $platform
);
}
return $result;
}
//Request characters
function fetchCharacters($username, $platform)
{
$result = array();
if($player = fetchPlayer($username, $platform)) {
$uri = 'http://bungie.net/Platform/Destiny/'.$player['membershipType'].'/Account/'.$player['membershipId'].'?ignorecase=true';
if( $json = callBungie($uri) ) {
foreach ($json['Response']['data']['characters'] as $character) {
$result[] = $character;
}
}
}
return $result;
}
//Request character descriptions
function fetchCharacterDescriptions($username, $platform)
{
$character_descriptions = array();
if($characters = fetchCharacters($username, $platform)) {
foreach ($characters as $character) {
$class = translate($character['characterBase']['classHash']);
$emblem = $character['emblemPath'];
$backgroundpath = $character['emblemPath'];
$level = $character['characterLevel'];
$character_id = $character['characterBase']['characterId'];
$light = $character['characterBase']['stats']['STAT_LIGHT']['value'];
$username = $username;
$character_descriptions[] = array(
'class'=> $class,
'emblem'=> $emblem,
'backgroundpath'=>$backgroundpath,
'character_id' => $character_id,
'characterlevel' => $level,
'light' => $light,
'username' => $username
);
}
return $character_descriptions;
}
return false;
}
?>
So my function call is: fetchCharacterDescriptions("Senaxx", "2"); and i'd like to add more players to this (from an array or something) So i can request the stats for multiple usernames.
You just have to loop over the players an perform fetchCharacterDescriptions for each of them.
$players = array(
"Senaxx" => "2",
"SomeoneElse" => "2",
);
foreach ($players as $playerName => $platformId) {
$Player = fetchCharacterDescriptions($playerName, $platformId);
// do your other stuff
}
Keep in mind that your webpage will load veeery slow because every call to fetchCharacterDescriptions() executes 2 curl requests. Also - if the API is down, your site effectivly is as well (or blank at least).
You are probably better off fetching the data beforehand (in certain intervalls) and storing it into a database/csv file or something.

login to local and server

how to log on localhost mark must be changed at the server login below to log in the server , and ENVIRONMENT == "development " is not called server and ENVIRONMENT == " test " called Server , without change of ENVIRONMENT == " test "?
if($email == NULL || $pass == NULL) return array("success"=>0,"msg"=>"Email dan password harus diisi");
else
{
if (ENVIRONMENT == "development")
{
$data['result'] =Array
(
'message' => 'Success',
'code' => 00,
'obj' => Array ('loginTried' => 0,
'email' => 'maheswara#gmail.com',
'phoneNumber' => 089615378878,
'name' => 'Dika',
'point' => 10, ));
return array("success"=>1,"msg"=>"Logged in");
}
else
{
$pass = md5($pass);
$url = "http://tooz.co.";
$post = json_encode(array('email' => $email, 'userPassword' => $pass));
$fields_string = "";
$fields = array(
'logintikiid' => $post
);
//url-ify the data for the POST
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$data = json_decode($result,TRUE);
if($data['code']=="00")
{
$this->CI->session->user_email = $data['obj']['email'];
$this->CI->session->user_phone = $data['obj']['phoneNumber'];
$this->CI->session->user_name = $data['obj']['name'];
$this->CI->session->tooz_point = $data['obj']['point'];
return array("success"=>1,"msg"=>"Logged in");
}
else
{
return array("success"=>0,"msg"=>"Email dan password salah");
}
}
}
}
this is controler
public function index($from=NULL)
{
$data = array();
switch ($from) {
case 'pickup':
$redirect = base_url("pickup");
set_top_msg("Untuk mengakses fitur pickup, silahkan login terlebih dahulu");
break;
case 'book':
$redirect = base_url("book");
set_top_msg("Untuk mengakses fitur booking, silahkan login terlebih dahulu");
break;
default:
$redirect = base_url();
break;
}
if ($this->session->user_email)
{
header("location:". base_url("tooz"));
die();
}
else if($this->input->post())
{
$this->load->library("login_library");
$email = $this->input->post("email");
$pass = $this->input->post("pass");
$return = $this->login_library->login($email,$pass);
if($return['success']==1)header('Location:'. $redirect);
else set_top_msg($return["msg"],"warning");
}
load_page($data);
}

accessing 'blog' functionality / template in FUEL CMS

I'm having trouble getting the "stock" blog functionality / template working within FUEL CMS.
I have read that it is already there, stock with the download configuration of the CMS; I have also tried creating one from scratch and uploading a 'blog' theme from a project found in GitHub. None have worked so far.
I found the blog variable at:
_variables/global.php
I have created a 'blog' controller via interpretation of (gappy) docs.
By adding the below code within it; then making a corresponding 'blog.php' view. I get nothing but a 404 error.
<?php
class Blog extends CI_Controller {
public function view($page = 'home')
{
//you can acesse this http://example.com/blog/view/
}
public function new($page = 'home')
{
//you can acesse this http://example.com/blog/new/
}
}
Within the modules folder. I found this 'stock' blog controller file. But don't know how to use it? found at: /fuel/modules/blog/controller/blog.php
<?php
require_once(MODULES_PATH.'/blog/libraries/Blog_base_controller.php');
class Blog extends Blog_base_controller {
function __construct()
{
parent::__construct();
}
function _remap()
{
$year = ($this->uri->rsegment(2) != 'index') ? (int) $this->uri->rsegment(2) : NULL;
$month = (int) $this->uri->rsegment(3);
$day = (int) $this->uri->rsegment(4);
$slug = $this->uri->rsegment(5);
$limit = (int) $this->fuel->blog->config('per_page');
$view_by = 'page';
// we empty out year variable if it is page because we won't be querying on year'
if (preg_match('#\d{4}#', $year) && !empty($year) && empty($slug))
{
$view_by = 'date';
}
// if the first segment is id then treat the second segment as the id
else if ($this->uri->rsegment(2) === 'id' && $this->uri->rsegment(3))
{
$view_by = 'slug';
$slug = (int) $this->uri->rsegment(3);
$post = $this->fuel->blog->get_post($slug);
if (isset($post->id))
{
redirect($post->url);
}
}
else if (!empty($slug))
{
$view_by = 'slug';
}
// set this to false so that we can use segments for the limit
$cache_id = fuel_cache_id();
$cache = $this->fuel->blog->get_cache($cache_id);
if (!empty($cache))
{
$output =& $cache;
}
else
{
$vars = $this->_common_vars();
if ($view_by == 'slug')
{
return $this->post($slug);
}
else if ($view_by == 'date')
{
$page_title_arr = array();
$posts_date = mktime(0, 0, 0, $month, $day, $year);
if (!empty($day)) $page_title_arr[] = $day;
if (!empty($month)) $page_title_arr[] = date('M', strtotime($posts_date));
if (!empty($year)) $page_title_arr[] = $year;
// run before_posts_by_date hook
$hook_params = array('year' => $year, 'month' => $month, 'day' => $day, 'slug' => $slug, 'limit' => $limit);
$this->fuel->blog->run_hook('before_posts_by_date', $hook_params);
$vars = array_merge($vars, $hook_params);
$vars['page_title'] = $page_title_arr;
$vars['posts'] = $this->fuel->blog->get_posts_by_date($year, (int) $month, $day, $slug);
$vars['pagination'] = '';
}
else
{
$limit = $this->fuel->blog->config('per_page');
$this->load->library('pagination');
$config['uri_segment'] = 3;
$offset = $this->uri->segment($config['uri_segment']);
$this->config->set_item('enable_query_strings', FALSE);
$config = $this->fuel->blog->config('pagination');
$config['base_url'] = $this->fuel->blog->url('page/');
//$config['total_rows'] = $this->fuel->blog->get_posts_count();
$config['page_query_string'] = FALSE;
$config['per_page'] = $limit;
$config['num_links'] = 2;
//$this->pagination->initialize($config);
if (!empty($offset))
{
$vars['page_title'] = lang('blog_page_num_title', $offset, $offset + $limit);
}
else
{
$vars['page_title'] = '';
}
// run before_posts_by_date hook
$hook_params = array('offset' => $offset, 'limit' => $limit, 'type' => 'posts');
$this->fuel->blog->run_hook('before_posts_by_page', $hook_params);
$vars['offset'] = $offset;
$vars['limit'] = $limit;
$vars['posts'] = $this->fuel->blog->get_posts_by_page($limit, $offset);
// run hook again to get the proper count
$hook_params['type'] = 'count';
$this->fuel->blog->run_hook('before_posts_by_page', $hook_params);
//$config['total_rows'] = count($this->fuel->blog->get_posts_by_page());
$config['total_rows'] = $this->fuel->blog->get_posts_count();
// create pagination
$this->pagination->initialize($config);
$vars['pagination'] = $this->pagination->create_links();
}
// show the index page if the page doesn't have any uri_segment(3)'
$view = ($this->uri->rsegment(2) == 'index' OR ($this->uri->rsegment(2) == 'page' AND !$this->uri->segment(3))) ? 'index' : 'posts';
$output = $this->_render($view, $vars, TRUE);
$this->fuel->blog->save_cache($cache_id, $output);
}
$this->output->set_output($output);
}
function post($slug = null)
{
if (empty($slug))
{
redirect_404();
}
$this->load->library('session');
$blog_config = $this->fuel->blog->config();
// run before_posts_by_date hook
$hook_params = array('slug' => $slug);
$this->fuel->blog->run_hook('before_post', $hook_params);
$post = $this->fuel->blog->get_post($slug);
if (isset($post->id))
{
$vars = $this->_common_vars();
$vars['post'] = $post;
$vars['user'] = $this->fuel->blog->logged_in_user();
$vars['page_title'] = $post->title;
$vars['next'] = $this->fuel->blog->get_next_post($post);
$vars['prev'] = $this->fuel->blog->get_prev_post($post);
$vars['slug'] = $slug;
$vars['is_home'] = $this->fuel->blog->is_home();
$antispam = md5(random_string('unique'));
$field_values = array();
// post comment
if (!empty($_POST))
{
$field_values = $_POST;
// the id of "content" is a likely ID on the front end, so we use comment_content and need to remap
$field_values['content'] = $field_values['new_comment'];
unset($field_values['antispam']);
if (!empty($_POST['new_comment']))
{
$vars['processed'] = $this->_process_comment($post);
}
else
{
add_error(lang('blog_error_blank_comment'));
}
}
$cache_id = fuel_cache_id();
$cache = $this->fuel->blog->get_cache($cache_id);
if (!empty($cache) AND empty($_POST))
{
$output =& $cache;
}
else
{
$this->load->library('form');
if (is_true_val($this->fuel->blog->config('use_captchas')))
{
$captcha = $this->_render_captcha();
$vars['captcha'] = $captcha;
}
$vars['thanks'] = ($this->session->flashdata('thanks')) ? blog_block('comment_thanks', $vars, TRUE) : '';
$vars['comment_form'] = '';
$this->session->set_userdata('antispam', $antispam);
if ($post->allow_comments)
{
$this->load->module_model(BLOG_FOLDER, 'blog_comments_model');
$this->load->library('form_builder', $blog_config['comment_form']);
$fields['author_name'] = array('label' => 'Name', 'required' => TRUE);
$fields['author_email'] = array('label' => 'Email', 'required' => TRUE);
$fields['author_website'] = array('label' => 'Website');
$fields['new_comment'] = array('label' => 'Comment', 'type' => 'textarea', 'required' => TRUE);
$fields['post_id'] = array('type' => 'hidden', 'value' => $post->id);
$fields['antispam'] = array('type' => 'hidden', 'value' => $antispam);
if (!empty($vars['captcha']))
{
$fields['captcha'] = array('required' => TRUE, 'label' => 'Security Text', 'value' => '', 'after_html' => ' <span class="captcha">'.$vars['captcha']['image'].'</span><br /><span class="captcha_text">'.lang('blog_captcha_text').'</span>');
}
// now merge with config... can't do array_merge_recursive'
foreach($blog_config['comment_form']['fields'] as $key => $field)
{
if (isset($fields[$key])) $fields[$key] = array_merge($fields[$key], $field);
}
if (!isset($blog_config['comment_form']['label_layout'])) $this->form_builder->label_layout = 'left';
if (!isset($blog_config['comment_form']['submit_value'])) $this->form_builder->submit_value = 'Submit Comment';
if (!isset($blog_config['comment_form']['use_form_tag'])) $this->form_builder->use_form_tag = TRUE;
if (!isset($blog_config['comment_form']['display_errors'])) $this->form_builder->display_errors = TRUE;
$this->form_builder->form_attrs = 'method="post" action="'.site_url($this->uri->uri_string()).'#comments_form"';
$this->form_builder->set_fields($fields);
$this->form_builder->set_field_values($field_values);
$this->form_builder->set_validator($this->blog_comments_model->get_validation());
$vars['comment_form'] = $this->form_builder->render();
$vars['fields'] = $fields;
}
$output = $this->_render('post', $vars, TRUE);
// save cache only if we are not posting data
if (!empty($_POST))
{
$this->fuel->blog->save_cache($cache_id, $output);
}
}
if (!empty($output))
{
$this->output->set_output($output);
return;
}
}
else
{
show_404();
}
}
function _process_comment($post)
{
if (!is_true_val($this->fuel->blog->config('allow_comments'))) return;
$notified = FALSE;
// check captcha
if (!$this->_is_valid_captcha())
{
add_error(lang('blog_error_captcha_mismatch'));
}
// check that the site is submitted via the websit
if (!$this->_is_site_submitted())
{
add_error(lang('blog_error_comment_site_submit'));
}
// check consecutive posts
if (!$this->_is_not_consecutive_post())
{
add_error(lang('blog_error_consecutive_comments'));
}
$this->load->module_model(BLOG_FOLDER, 'blog_users_model');
$user = $this->blog_users_model->find_one(array('fuel_users.email' => $this->input->post('author_email', TRUE)));
// create comment
$this->load->module_model(BLOG_FOLDER, 'blog_comments_model');
$comment = $this->blog_comments_model->create();
$comment->post_id = $post->id;
$comment->author_id = (!empty($user->id)) ? $user->id : NULL;
$comment->author_name = $this->input->post('author_name', TRUE);
$comment->author_email = $this->input->post('author_email', TRUE);
$comment->author_website = $this->input->post('author_website', TRUE);
$comment->author_ip = $_SERVER['REMOTE_ADDR'];
$comment->content = trim($this->input->post('new_comment', TRUE));
$comment->date_added = NULL; // will automatically be added
//http://googleblog.blogspot.com/2005/01/preventing-comment-spam.html
//http://en.wikipedia.org/wiki/Spam_in_blogs
// check double posts by IP address
if ($comment->is_duplicate())
{
add_error(lang('blog_error_comment_already_submitted'));
}
// if no errors from above then proceed to submit
if (!has_errors())
{
// submit to akisment for validity
$comment = $this->_process_akismet($comment);
// process links and add no follow attribute
$comment = $this->_filter_comment($comment);
// set published status
if (is_true_val($comment->is_spam) OR $this->fuel->blog->config('monitor_comments'))
{
$comment->published = 'no';
}
// save comment if saveable and redirect
if (!is_true_val($comment->is_spam) OR (is_true_val($comment->is_spam) AND $this->fuel->blog->config('save_spam')))
{
if ($comment->save())
{
$notified = $this->_notify($comment, $post);
$this->load->library('session');
$vars['post'] = $post;
$vars['comment'] = $comment;
$this->session->set_flashdata('thanks', TRUE);
$this->session->set_userdata('last_comment_ip', $_SERVER['REMOTE_ADDR']);
$this->session->set_userdata('last_comment_time', time());
redirect($post->url);
}
else
{
add_errors($comment->errors());
}
}
else
{
add_error(lang('blog_comment_is_spam'));
}
}
return $notified;
}
// check captcha validity
function _is_valid_captcha()
{
$valid = TRUE;
// check captcha
if (is_true_val($this->fuel->blog->config('use_captchas')))
{
if (!$this->input->post('captcha'))
{
$valid = FALSE;
}
else if (!is_string($this->input->post('captcha')))
{
$valid = FALSE;
}
else
{
$post_captcha_md5 = $this->_get_encryption($this->input->post('captcha'));
$session_captcha_md5 = $this->session->userdata('comment_captcha');
if ($post_captcha_md5 != $session_captcha_md5)
{
$valid = FALSE;
}
}
}
return $valid;
}
// check to make sure the site issued a session variable to check against
function _is_site_submitted()
{
return ($this->session->userdata('antispam') AND $this->input->post('antispam') == $this->session->userdata('antispam'));
}
// disallow multiple successive submissions
function _is_not_consecutive_post()
{
$valid = TRUE;
$time_exp_secs = $this->fuel->blog->config('multiple_comment_submission_time_limit');
$last_comment_time = ($this->session->userdata('last_comment_time')) ? $this->session->userdata('last_comment_time') : 0;
$last_comment_ip = ($this->session->userdata('last_comment_ip')) ? $this->session->userdata('last_comment_ip') : 0;
if ($_SERVER['REMOTE_ADDR'] == $last_comment_ip AND !empty($time_exp_secs))
{
if (time() - $last_comment_time < $time_exp_secs)
{
$valid = FALSE;
}
}
return $valid;
}
// process through akisment
function _process_akismet($comment)
{
if ($this->fuel->blog->config('akismet_api_key'))
{
$this->load->module_library(BLOG_FOLDER, 'akismet');
$akisment_comment = array(
'author' => $comment->author_name,
'email' => $comment->author_email,
'body' => $comment->content
);
$config = array(
'blog_url' => $this->fuel->blog->url(),
'api_key' => $this->fuel->blog->config('akismet_api_key'),
'comment' => $akisment_comment
);
$this->akismet->init($config);
if ( $this->akismet->errors_exist() )
{
if ( $this->akismet->is_error('AKISMET_INVALID_KEY') )
{
log_message('error', 'AKISMET :: Theres a problem with the api key');
}
elseif ( $this->akismet->is_error('AKISMET_RESPONSE_FAILED') )
{
log_message('error', 'AKISMET :: Looks like the servers not responding');
}
elseif ( $this->akismet->is_error('AKISMET_SERVER_NOT_FOUND') )
{
log_message('error', 'AKISMET :: Wheres the server gone?');
}
}
else
{
$comment->is_spam = ($this->akismet->is_spam()) ? 'yes' : 'no';
}
}
return $comment;
}
// strip out
function _filter_comment($comment)
{
$this->load->helper('security');
$comment_attrs = array('content', 'author_name', 'author_email', 'author_website');
foreach($comment_attrs as $filter)
{
$text = $comment->$filter;
// first remove any nofollow attributes to clean up... not perfect but good enough
$text = preg_replace('/<a(.+)rel=["\'](.+)["\'](.+)>/Umi', '<a$1rel="nofollow"$3>', $text);
// $text = str_replace('<a ', '<a rel="nofollow"', $text);
$text = strip_image_tags($text);
$comment->$filter = $text;
}
return $comment;
}
function _notify($comment, $post)
{
// send email to post author
if (!empty($post->author))
{
$config['wordwrap'] = TRUE;
$this->load->library('email', $config);
$this->email->from($this->fuel->config('from_email'), $this->fuel->config('site_name'));
$this->email->to($post->author->email);
$this->email->subject(lang('blog_comment_monitor_subject', $this->fuel->blog->config('title')));
$msg = lang('blog_comment_monitor_msg');
$msg .= "\n".fuel_url('blog/comments/edit/'.$comment->id)."\n\n";
$msg .= (is_true_val($comment->is_spam)) ? lang('blog_email_flagged_as_spam')."\n" : '';
$msg .= lang('blog_email_published').": ".$comment->published."\n";
$msg .= lang('blog_email_author_name').": ".$comment->author_name."\n";
$msg .= lang('blog_email_author_email').": ".$comment->author_email."\n";
$msg .= lang('blog_email_author_website').": ".$comment->author_website."\n";
$msg .= lang('blog_email_author_ip').": ".gethostbyaddr($comment->author_ip)." (".$comment->author_ip.")\n";
$msg .= lang('blog_email_content').": ".$comment->content."\n";
$this->email->message($msg);
return $this->email->send();
}
else
{
return FALSE;
}
}
function _render_captcha()
{
$this->load->library('captcha');
$blog_config = $this->config->item('blog');
$assets_folders = $this->config->item('assets_folders');
$blog_folder = MODULES_PATH.BLOG_FOLDER.'/';
$captcha_path = $blog_folder.'assets/captchas/';
$word = strtoupper(random_string('alnum', 5));
$captcha_options = array(
'word' => $word,
'img_path' => $captcha_path, // system path to the image
'img_url' => captcha_path('', BLOG_FOLDER), // web path to the image
'font_path' => $blog_folder.'fonts/',
);
$captcha_options = array_merge($captcha_options, $blog_config['captcha']);
if (!empty($_POST['captcha']) AND $this->session->userdata('comment_captcha') == $this->input->post('captcha'))
{
$captcha_options['word'] = $this->input->post('captcha');
}
$captcha = $this->captcha->get_captcha_image($captcha_options);
$captcha_md5 = $this->_get_encryption($captcha['word']);
$this->session->set_userdata('comment_captcha', $captcha_md5);
return $captcha;
}
function _get_encryption($word)
{
$captcha_md5 = md5(strtoupper($word).$this->config->item('encryption_key'));
return $captcha_md5;
}
}
My goal is:
1.) Enable 'Blog' Module / template / functionality and understand how I did it. I find the docs lacking, I'm also new at code igniter so that could be why. I just want the most basic way to do this for now.
And 2.) I want to create a page 'from scratch' that resolves on the dashboard side as well. I have created pages in /views/ but they resolve with that whole string /fuel/application/views/page/ I want to create a normal page without all that in the URL. I have tried creating corresponding controllers even variables and haven't had much luck!!!!!!!
As of FUEL CMS 1.0 the blog module is no longer bundled with the CMS by default. You would need to do the following:
Download & setup FUEL CMS per the install instructions here: https://github.com/daylightstudio/FUEL-CMS
Next, once you've got that up and running you can download & setup the blog module per the instructions here: https://github.com/daylightstudio/FUEL-CMS-Blog-Module
Once the blog is setup, you should be able to access it at "yourdomain.com/blog". As far as creating themes, there is a views/themes folder in the blog module which contains a default theme and also where you can setup your custom theme. Additional information about the blog module & theming can be found here http://docs.getfuelcms.com/modules/blog

Categories