wordpress php code not running (wpforms) - php

I'm hosting a local wordpress site through xampp, and am testing some code. Basically I wanted to take the form data from a wpforms, and when the user clicks submit it then sends that data to an external mysql database. This is the code
global $wpdb;
function be_db_connector($fields) {
$username = 'example';
$password = 'example';
$database = 'example';
$localhost = 'example';
$wpdb = new wpdb('username','password','database','localhost');
$wpdb->show_errors();
$wpdb->insert('ProspectsDevOnly', array(
'username' => $fields['0']['value'],
'password' => $fields['2']['value'],
'email' => $fields['1']['value'],
'create_time' => 20180526),
array(
'%s',
'%s',
'%s',
'%d',
) );
}
add_action( 'wpforms_process_complete_7', 'be_db_connector', 10, 1 );
I put the code in my themes function.php file, but when I submit the form nothing happens - nothing is sent to my database.
Any idea why this may be? Thanks for the help.

First you can enable debug mode in wp-config.php in root folder.
Assign value for username,password,database,password.
function be_db_connector() {
$username = 'root';
$password = 'root123';
$database = 'account';
$localhost = '127.0.0.1';
//$wpdb = new wpdb('username','password','database','localhost');
$wpdb = new wpdb($username,$password,$database,'localhost');
$wpdb->show_errors();
$insert = $wpdb->insert('account', array(
'firstname' => '',
'lastname' => '',
'age' => '',
'gender' => 20180526,
'emailad' => '',
'username' => '',
'password' =>''
));
}
add_action( 'init', 'be_db_connector', 10, 1 );

Related

Is there a way to handle large arrays of colums from MySQL when importing via $POST

I am trying to work on a API to import data to MySQL Database.
I have it working on a small scale.
I have now altered it so its more in line with the current database. Its a large table and been poorly structured, But for simplicity and maintaining am trying to keep MySQL database and the Applications database inline.
MSQL = Windows based application:
MySQL = The Server database, Am extracting Data out of MSQL hosted locally and importing it to a MySQL Database hosted externally.
As i have expanded the code, its no longer working, this could be a number of factors like the table needs structuring / indexing / primary, or i am passing too much data via a query. The application is starting to look very messy.
The question is, is it necessary and better practice me listing out each column and matching up the data sets apposed to trying to utilize the MySQL INFORMATION_SCHEMA to list the columns instead.
Here is the code so far, I have already removed a number of columns.
We have a Server and Host:
Server Code
Class file: Api.php
class API
{
private $connect = '';
function __construct()
{
$this->database_connection();
}
function database_connection()
{
$this->connect = new PDO("mysql:host=$Host;dbname=$DatabaseName", $DatabaseUsername, $DatabasePassword);
}
function insert() // Insert Function
{
if(isset($_POST["Branch"]))
{
$form_data = array(
':Branch'=>$_POST['Branch'],
':Date'=>$_POST['Date'],
':Week'=>$_POST['Week'],
':Period'=>$_POST['Period'],
':Invoice_No'=>$_POST['Invoice_No'],
':Invoice_Reference'=>$_POST['Invoice_Reference'],
':Line_No'=>$_POST['Line_No'],
':Till_No'=>$_POST['Till_No'],
':Operator'=>$_POST['Operator'],
':Stock_Code'=>$_POST['Stock_Code'],
':Barcode'=>$_POST['Barcode'],
':Line_Quantity'=>$_POST['Line_Quantity'],
':Weight'=>$_POST['Weight'],
':Weight_Unit'=>$_POST['Weight_Unit'],
':Man_Weighed'=>$_POST['Man_Weighed'],
':Unit_ID'=>$_POST['Unit_ID'],
':Line_Price_Band'=>$_POST['Line_Price_Band'],
':Original_Sell'=>$_POST['Original_Sell'],
':Actual_Sell'=>$_POST['Actual_Sell'],
':Cost'=>$_POST['Cost'],
':Vat_Rate'=>$_POST['Vat_Rate'],
':Discount_Rate'=>$_POST['Discount_Rate'],
':Value_Goods'=>$_POST['Value_Goods'],
':Value_VAT'=>$_POST['Value_VAT'],
':Value_Sale'=>$_POST['Value_Sale'],
':Value_Cost'=>$_POST['Value_Cost'],
':Price_Override_Amount'=>$_POST['Price_Override_Amount'],
':Price_Overrided'=>$_POST['Price_Overrided'],
':Discounted'=>$_POST['Discounted'],
':Account_No'=>$_POST['Account_No'],
':Sub_Account_No'=>$_POST['Sub_Account_No'],
':Customer_Account'=>$_POST['Customer_Account'],
':Sub_Customer_Account'=>$_POST['Sub_Customer_Account'],
':Cust_Type_ID'=>$_POST['Cust_Type_ID'],
':Super_Department_ID'=>$_POST['Super_Department_ID'],
':Department_ID'=>$_POST['Department_ID'],
':Group_ID'=>$_POST['Group_ID'],
':Sub_Group_ID'=>$_POST['Sub_Group_ID'],
':Retail_Location_ID'=>$_POST['Retail_Location_ID'],
':Retail_Sub_Location_ID'=>$_POST['Retail_Sub_Location_ID'],
':Entry_Method'=>$_POST['Entry_Method'],
':End_Sale_Discount_Rate'=>$_POST['End_Sale_Discount_Rate'],
':Loyalty_Discount_Rate'=>$_POST['Loyalty_Discount_Rate'],
':Date_Time_Stamp'=>$_POST['Date_Time_Stamp']
);
$query = "
INSERT INTO Lines
(Branch, Date, Week, Period, Invoice_No, Invoice_Reference, Line_No, Till_No, Operator, Stock_Code, Barcode, Line_Quantity, Weight, Weight_Unit, Man_Weighed, Unit_ID, Line_Price_Band, Original_Sell, Actual_Sell, Cost, Vat_Rate, Discount_Rate, Value_Goods, Value_VAT, Value_Sale, Value_Cost, Price_Override_Amount, Price_Overrided, Discounted, Account_No, Sub_Account_No, Customer_Account, Sub_Customer_Account, Cust_Type_ID, Super_Department_ID, Department_ID, Group_ID, Sub_Group_ID, Retail_Location_ID, Retail_Sub_Location_ID, Entry_Method, End_Sale_Discount_Rate, Loyalty_Discount_Rate, Date_Time_Stamp) VALUES
(:Branch, :Date, :Week, :Period, :Invoice_No, :Invoice_Reference, :Line_No, :Till_No, :Operator, :Stock_Code, :Barcode, :Line_Quantity, :Weight, :Weight_Unit, :Man_Weighed, :Unit_ID, :Line_Price_Band, :Original_Sell, :Actual_Sell, :Cost, :Vat_Rate, :Discount_Rate, :Value_Goods, :Value_VAT, :Value_Sale, :Value_Cost, :Price_Override_Amount, :Price_Overrided, :Discounted, :Account_No, :Sub_Account_No, :Customer_Account, :Sub_Customer_Account, :Cust_Type_ID, :Super_Department_ID, :Department_ID, :Group_ID, :Sub_Group_ID, :Retail_Location_ID, :Retail_Sub_Location_ID, :Entry_Method, :End_Sale_Discount_Rate, :Loyalty_Discount_Rate, :Date_Time_Stamp)
";
$statement = $this->connect->prepare($query);
if($statement->execute($form_data))
{
echo $statement ;
$data[] = array(
'success' => '1'
);
}
else
{
var_dump($statement) ;
$data[] = array(
'success' => '0'
);
}
}
else
{
$data[] = array(
'success' => '0'
);
}
return $data;
}
}
Calling the Function: Handler test_api.php
// This does have an include for Api.php
if($_GET["action"] == 'insert')
{
$data = $api_object->insert();
}
Host code
This wont be the final thing, as ill be extracting from MSQL building the array and posting this way, This is just laid out like so for testing
<?php
$form_data = array(
'Branch' => '1',
'Date' => '8/11/2018 13:42:00',
'Week' => '1',
'Period' => '1',
'Invoice_No' => '9999998',
'Invoice_Reference' => '99999998',
'Line_No' => '1',
'Till_No' => '1',
'Operator' => '99',
'Stock_Code' => '123456',
'Barcode' => '654321',
'Line_Quantity' => '99',
'Weight' => '',
'Weight_Unit' => '',
'Man_Weighed' => '',
'Unit_ID' => '',
'Line_Price_Band' => '1',
'Original_Sell' => '99.99',
'Actual_Sell' => '99.99',
'Cost' => '9.99',
'Vat_Rate' => '1',
'Discount_Rate' => '',
'Value_Goods' => '',
'Value_VAT' => '',
'Value_Sale' => '',
'Value_Cost' => '',
'Price_Override_Amount' => '',
'Price_Overrided' => '',
'Discounted' => '',
'Account_No' => '',
'Sub_Account_No' => '',
'Customer_Account' => '',
'Sub_Customer_Account' => '',
'Cust_Type_ID' => '',
'Super_Department_ID' => '',
'Department_ID' => '',
'Group_ID' => '',
'Sub_Group_ID' => '',
'Retail_Location_ID' => '',
'Retail_Sub_Location_ID' => '',
'Entry_Method' => '',
'End_Sale_Discount_Rate' => '',
'Loyalty_Discount_Rate' => '',
'Date_Time_Stamp' => ''
);
$api_url = "localhost/Modules/API/Server/test_api.php?action=insert"; //change this url as per your folder path for api folder
$client = curl_init($api_url);
curl_setopt($client, CURLOPT_POST, true);
curl_setopt($client, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($client);
// var_dump($response);
curl_close($client);
$result = json_decode($response, true);
echo $response ;
foreach($result as $keys => $values)
{
if($result[$keys]['success'] == '1')
{
echo 'update';
}
else
{
echo 'error';
echo $response ;
}
}
At the moment its not working, I am not sure why, As it was working fine with a much smaller dataset.
And my IDE hasnt reported of any errors or typos, But thats something else, if you do spot a error, please do say.
The question is, is there a better way to work with large number of columns.
In previous API's i have basically just dumped the data to txt and imported from a deliminator. So this is all very new to me.

Typo 3: Create user on FE login

The situation:
I build an authentication service that uses Basic Authentication to check if the user exists on an external database and fetches some data. The users in question only exist on the external database.
The problem:
Typo3 needs to have an user entry in the fe_user table to login the user.
So whenever this entry does not exist, the user cannot login.
What I want to do:
Create the user in the authentication service to avoid using a sql dump from the external database and ensure that synchronisation is possible.
The relevant code:
public function authUser(array $user) {
$a_user = $this->login['uname'];
$a_pwd = $this->login['uident_text'];
$url = 'https://soliday.fluchtpunkt.at/api/queryMediaItems';
$data = json_decode('{"language":"de-at"}');
$basicAuth = base64_encode("$a_user:$a_pwd");
// use key 'http' even if you send the request to https://...
$options = array (
'http' => array (
'header' => array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic {$basicAuth}"
),
'method' => 'POST',
'content' => '{"language":"de-at"}'
)
);
$context = stream_context_create ( $options );
$result = file_get_contents ($url, false, $context);
$response = gzdecode($result);
$checkUser = $this->fetchUserRecord ( $this->login ['uname'] );
if (!is_array($checkUser)&& $result!== FALSE) {
$this->createUser();
}
// failure
if ($result === FALSE) {
return static::STATUS_AUTHENTICATION_FAILURE_BREAK;
}
$this->processData($response);
// success
return static::STATUS_AUTHENTICATION_SUCCESS_BREAK;
}
public function createUser() {
$username = $this->login ['uname'];
$password = $this->login ['uident_text'];
$record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', "username = '" . $username . "' AND disable = 0 AND deleted = 0" );
if (! $record) {
// user has no DB record (yet), create one using defaults registered in extension config
// password is not important, username is set to the user's input
$record = array (
'username' => $username,
'password' => $password,
'name' => '',
'email' => '',
'disable' => '0',
'deleted' => '0',
'pid' => $this->config ['storagePid'],
'usergroup' => $this->config ['addUsersToGroups'],
'tstamp' => time ()
);
if (t3lib_extMgm::isLoaded ( 'extbase' )) {
$record ['tx_extbase_type'] = $this->config ['recordType'];
}
$GLOBALS ['TYPO3_DB']->exec_INSERTquery ( 'fe_users', $record );
$uid = $GLOBALS ['TYPO3_DB']->sql_insert_id ();
$record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', 'uid = ' . intval ( $uid ) );
}
$_SESSION [$this->sessionKey] ['user'] ['fe'] = $record;
}
the ext_localconf.php file:
<?php
if (!defined('TYPO3_MODE')) {
die ('Access denied.');
}
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
$_EXTKEY,
'auth' /* sv type */,
'AuthService' /* sv key */,
array(
'title' => 'GET Authentication service',
'description' => 'Authenticates users with GET request.',
'subtype' => 'getUserFE, authUserFE',
'available' => true,
'priority' => 90,
'quality' => 90,
'os' => '',
'exec' => '',
'className' => Plaspack\professionalZoneLogin\Service\AuthService::class,
)
);
You should extend AuthenticationService with your own code, way of doing that is described here https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Xclasses/Index.html
Not sure if it's related, but t3lib_extMgm should be \TYPO3\CMS\Core\Utility\ExtensionManagementUtility unless you're using TYPO3 6.
You can also see if you get any SQL errors by calling $GLOBALS['TYPO3_DB']->sql_error().

Unable to log in or create users in MediaWiki

I am unable to log in / create a New user in a MediaWiki wiki.
It throws the error:
Account creation error: Incorrect password entered. Please try again.
Or when I try to log in:
Log-in error :Incorrect password entered. Please try again.
Based on the help documents in MediaWiki added the below code in the localsettings.php file.
I have also added the php-ldap service though command prompt in my local ubuntu machine.
LDAP Settings in Local Settings.php
require_once 'extensions/LdapAuthentication/LdapAuthentication.php';
require_once 'includes/AuthPlugin.php';
$wgAuth = new LdapAuthenticationPlugin();
$wgLDAPDomainNames = array(
'MDomain',
);
$wgLDAPServerNames = array(
'MDomain.local' => '10.10.1.6','MDomain-C02.MDomain','MDomain01.MDomain.local',
);
$wgLDAPUseLocal = false;
$wgLDAPEncryptionType = array(
'MDomain.LOCAL' => 'tls',
);
$wgLDAPPort = array(
'openldap_example_com' => 389,
);
$wgLDAPProxyAgent = array(
'MDomain.local' => 'cn=wikimedia-service,dc=MDomain,dc=LOCAL',
);
$wgLDAPProxyAgentPassword = array(
'MDomain.local' => 'XXXX',
);
$wgLDAPSearchAttributes = array(
'MDomain.local' => 'sAMAccountName'
);
$wgLDAPBaseDNs = array(
'MDomain.local' => 'ou=MDomain,dc=MDomain,dc=local',
);
# To pull e-mail address from LDAP
$wgLDAPPreferences = array(
'MDomain.local' => array( 'email' => 'mail')
);
# Group based restriction
$wgLDAPGroupUseFullDN = array( "MDomain.local"=>false );
$wgLDAPGroupObjectclass = array( "MDomain.local"=>"Group" );
$wgLDAPGroupAttribute = array( "MDomain.local"=>"memberuid" );
$wgLDAPGroupSearchNestedGroups = array( "MDomain.local"=>true );
$wgLDAPGroupNameAttribute = array( "MDomain.local"=>"cn" );
$wgLDAPRequiredGroups = array( "MDomain.local"=>array("cn=wikimedia-service,dc=MDomain,dc=local"));
$wgLDAPLowerCaseUsername = array(
'MDomain.local' => true,
);
Can you please let me know if i am missing anything.

registers with the verification email , but when submitted insert double data

still continue the project which I've posted . a bit of trouble on registering with a verification email . currently running normally: registers the verification email, sents to the email destination. but I see there are two inserts with the same data (as submitted) in the database. Is there anything wrong with my script ?
this my controllers
function submit() {
$_POST['dob'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');
$password = $this->input->post('password');
$email = $this->input->post('email');
$dob = $this->input->post('dob');
$jkl = $this->input->post('jkl');
$lastlogin = $this->input->post('lastlogin');
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'password' => $password,
'email' => $email,
'dob' => $dob,
'jkl' => $jkl,
'lastlogin' => $lastlogin,
'active' => 0
);
$this->m_register->add_account($data);
$id = $this->m_register->add_account($data);
$encrypted_id = md5($id);
$this->load->library('email');
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.gmail.com',
'smtp_port' => 465,
'smtp_user' => '*******#*****.com ',
'smtp_pass' => '**********',
'mailtype' => 'html',
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$email_setting = array('mailtype'=>'html');
$this->email->initialize($email_setting);
$this->email->from('*****#******.COM', 'RRR');
$this->email->to($email);
$this->email->subject('Confirmation Email');
$this->email->message("WELCOME TO RRRR <br><p></p>Hallo $firstname $lastname <br><br><br><p>Terimakasih telah melakuan registrasi dengan:<br><br><p>
Username = $email<p>
Password = $password
<br><br>
<p>
untuk memverifikasi akun silahkan klik tautan dibawah ini</p><br><br>"
.site_url("login/register/verification/$encrypted_id")."
<br><br><br>
<p></p><br>
<p>Thanks</p>Admin ");
if($this->email->send())
{
$data = array ( 'isi' => 'login/vsuccess');
$this->load->view('layout/wrapper',$data);
}else
{
$data = array ( 'isi' => 'login/vgagal');
$this->load->view('layout/wrapper',$data);
}
}
this my models
function add_account($data){
$this->load->database();
$this->db->insert('user',$data);
return mysql_insert_id();
}
$this->m_register->add_account($data);
$id = $this->m_register->add_account($data);
Check these two lines in your code, remove:
$this->m_register->add_account($data);
And it will work.
I don't fully understand the question, but have you noticed that you seem to insert data into your db twice?
$this->m_register->add_account($data);
$id = $this->m_register->add_account($data);
Because you are calling ad_account() twice.
$this->m_register->add_account($data);
$id = $this->m_register->add_account($data);
$this->m_register->add_account($data);
$id = $this->m_register->add_account($data);
Obviously, you insert $data to your database 2 times :)
$this->m_register->add_account($data); calls the add_account() function in your m_register model. With $id = $this->m_register->add_account($data); you call it the second time, but you also catch the returned data. This should be the reason why you see the two inserts inside your database.
Deleting the first calling, and leaving just $id = $this->m_register->add_account($data);, should solve the problem.
I suggest adding form validation in your controller, where you could use a callback function for email in order to check if the email already exists in your table of users. This way a user cannot register twice with the same email.

mysqli functions and external classes

Im trying to write a function that uses an external db class but its not going so well. The class works fine when its not in a function. Heres my code:
require_once('MysqliDb.php');
function add_post($userid,$body,$cat_id,$user_link){
$db = new MysqliDb('localhost', 'root', 'root', 'my_database');
$date = new DateTime();
$now = $date->getTimestamp();
$insertData = array(
'user_id' => $userid,
'body' => $body,
'stamp' => $now,
'cat_id' => $cat_id,
'link' => $user_link
);
if ( $Db->insert('posts', $insertData) ) echo 'success!';
}
Heres the call:
$userid = 1;
$body = "hey whats up this is from a db class";
$cat_id = 3;
$user_link = "http://www.aol.com";
add_post($userid,$body,$cat_id,$user_link);

Categories