adodb setConnectionParameter CharacterSet - utf-8

How do you use the following code in adodb?
Where to put "'CharacterSet ',' UTF-8'"?
protected $connectionParameters = array();
/**
* Adds a parameter to the connection string.
*
* These parameters are added to the connection string when connecting,
* if the driver is coded to use it.
*
* #param string $parameter The name of the parameter to set
* #param string $value The value of the parameter
*
* #return null
*
* #example, for mssqlnative driver ('CharacterSet','UTF-8')
*/
final public function setConnectionParameter($parameter,$value)
{
$this->connectionParameters[$parameter] = $value;
}

set it after you create the ADOdb object, but before you connection to the database.
$db = newADOConnection('mssqlnative');
$db->setConnectionParameter('CharacterSet', 'UTF-8');
$db->connect($host, $user, $password, $database);

'CharacterSet' will not work for mysqli.
Correct one:
$db->setConnectionParameter(MYSQLI_SET_CHARSET_NAME, 'utf8');

Related

how to save and retrieve base64 encoded data using Laravel model

I am doing a web app using Laravel 7 api.
I receive data with a json request that I must store in the db with a base64 encoding.
I store the data in this way:
public function create_request(Request $request)
{
$req_store = new Req;
$req_store->text = base64_encode($request->input('requestInformations.text'));
$req_store->save();
}
Then obviously to retrieve the data of the text column I must use everytime base64_decode().
My question is that: is there a way to say to Laravel that everytime that I store a new request the column text must be authomatically encoded to base64 and everytime that I retrieve that data from the database the field text must be authomatically base64 decoded? I suppose I must write something in the Req.php model...
Can help?
You should use Accessors & Mutators for this. Please follow the link https://laravel.com/docs/7.x/eloquent-mutators#accessors-and-mutators
public function getTextAttribute($value)
{
return base64_decode($value);
}
public function setTextAttribute($value)
{
$this->attributes['text'] = base64_encode($value);
}
You may use a custom caster
https://laravel.com/docs/7.x/eloquent-mutators#custom-casts
<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class Base64 implements CastsAttributes
{
/**
* Cast the given value.
*
* #param \Illuminate\Database\Eloquent\Model $model
* #param string $key
* #param mixed $value
* #param array $attributes
* #return mixed
*/
public function get($model, $key, $value, $attributes)
{
return base64_decode($value);
}
/**
* Prepare the given value for storage.
*
* #param \Illuminate\Database\Eloquent\Model $model
* #param string $key
* #param array $value
* #param array $attributes
* #return string
*/
public function set($model, $key, $value, $attributes)
{
return base64_encode($value);
}
}
And in your model
use App\Casts\Base64;
/**
* The attributes that should be cast.
*
* #var array
*/
protected $casts = [
'text' => Base64::class,
];

How to parse / validate / handle http headers in PHP

Currently i am building my own php framework and i am now creating an implementation of the PHP-FIG PSR-7 MessageInterface. In specific the withHeader method. It states that the method could trow an exeption: \InvalidArgumentException for invalid header names or values.
So i am wondering, when is a header valid or invalid? Same for the values.
Or should i accept any header, and any header value? That could be dangerous right?
I now you can generaly say that if a header has multiple values, they are comma seperated. But that does not allways apply. If i look at a user-agent header for example, the value itself sometimes contains a comma. But you should treat it as a single value.
Indeed, it's "dangerous" to pass a header name - as argument of withHeader(), which
is NULL
is not a string
is an empty string
The same applies to the header value argument. It must be an array or a string (representing only one value, not a comma-separated list of values!).
As for the implementation of the withHeader method:
/**
* Return an instance with the provided value replacing the specified header.
*
* ...
*
* #param string $name Case-insensitive header field name.
* #param string|string[] $value Header value(s).
* #return static
* #throws \InvalidArgumentException for invalid header names or values.
*/
public function withHeader($name, $value) {
$this
->validateHeaderName($name)
->validateHeaderValue($value)
;
$clone = clone $this;
$clone->replaceHeader($name, $value);
return $clone;
}
/**
* =================
* Not part of PSR-7
* =================
*
* Validate header name.
*
* #param string $name Case-insensitive header field name.
* #return $this
* #throws \InvalidArgumentException
*/
protected function validateHeaderName($name) {
if (!isset($name)) {
throw new \InvalidArgumentException('No header name provided!');
}
if (!is_string($name)) {
throw new \InvalidArgumentException('The header name must be a string!');
}
if (empty($name)) {
throw new \InvalidArgumentException('Empty header name provided!');
}
return $this;
}
/**
* =================
* Not part of PSR-7
* =================
*
* Validate header value.
*
* #param string|string[] $value Header value(s).
* #return $this
* #throws \InvalidArgumentException
*/
protected function validateHeaderValue($value) {
if (isset($value) && !is_array($value) && !is_string($value)) {
throw new \InvalidArgumentException('The header value must be a string or an array!');
}
return $this;
}
/**
* =================
* Not part of PSR-7
* =================
*
* Replace a header item with a new one.
*
* #param string $name Case-insensitive header field name.
* #param string|string[] $value Header value(s).
* #return $this
* #done
*/
protected function replaceHeader($name, $value) {
$this
->removeHeader($name)
->addHeader($name, $value)
;
return $this;
}
You can find that in RFC 7230. Check Zend Diactoro's HeaderSecurity class for an implementation.

An alternative to deprecated addKey() method in Magento?

I am trying to add an index for a column over Magento data setup script.
/** #var Mage_Eav_Model_Entity_Setup $installer */
$installer = $this;
$installer->startSetup();
$installer
->getConnection()
->addKey(
$installer->getTable('enterprise_rma/rma'),
'IDX_EXPORT_DATE',
'export_date'
);
However our inspection tool complains:
The method Varien_Db_Adapter_Pdo_Mysql::addKey() has been deprecated with message: since 1.5.0.0
What I can use instead of addKey() in this case?
Look at the addKey function in the class Varien_Db_Adapter_Pdo_Mysql:
public function addKey($tableName, $indexName, $fields, $indexType = 'index', $schemaName = null)
{
return $this->addIndex($tableName, $indexName, $fields, $indexType, $schemaName);
}
It is just making a call to the addIndex function of the same class, this function is not deprecated, so you should use this one.
/**
* Add new index to table name
*
* #param string $tableName
* #param string $indexName
* #param string|array $fields the table column name or array of ones
* #param string $indexType the index type
* #param string $schemaName
* #return Zend_Db_Statement_Interface
* #throws Zend_Db_Exception|Exception
*/
public function addIndex($tableName, $indexName, $fields,
$indexType = Varien_Db_Adapter_Interface::INDEX_TYPE_INDEX, $schemaName = null)
(My code come from Magento Enterprise 1.12)

Retrieve js cookie using mage magento model php

Created a cookie using javascript
function (c_name,value,exdays) {
value = source ;
c_name = "Cookie" ;
var exdate=new Date();
exdays = exdate.setTime(exdate.getTime() + (30*24*60*60*1000));
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
});
Can i retrieve using Mage::getModel(‘core/cookie’)->get(); ??
If you would look inside the class Mage_Core_Model_Cookie the definition for the method get is:
/**
* Retrieve cookie or false if not exists
*
* #param string $neme The cookie name
* #return mixed
*/
public function get($name = null)
{
return $this->_getRequest()->getCookie($name, false);
}
The _getRequest() retreives an instance of Mage_Core_Controller_Request_Http a class that extends Zend_Controller_Request_Http inside which the method getCookie is defined as:
/**
* Retrieve a member of the $_COOKIE superglobal
*
* If no $key is passed, returns the entire $_COOKIE array.
*
* #todo How to retrieve from nested arrays
* #param string $key
* #param mixed $default Default value to use if key not found
* #return mixed Returns null if key does not exist
*/
public function getCookie($key = null, $default = null)
{
if (null === $key) {
return $_COOKIE;
}
return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
}
So yes, you can retrieve a cookie using Magento classes and methods, Zend or the $_COOKIE superglobal.
Yes, you can access javascript cookies, in your case by using:
$cookieValue = Mage::getModel('core/cookie')->get('Cookie');
Also, If it doesn't require that you set the cookie via JavaScript you can set it by:
/*
* ->set('name', 'value', 'expDate', 'path', 'domain', 'secure', 'httpsOnly')
* Only Name and Value are required. expDate set for 24 hours below.
*/
Mage::getModel('core/cookie')->set('Cookie', source, time()+86400);

PHPDoc return type for MySQL Resource

What is convenient name return type for MySQL resource in PHPDoc?
example i have this code
/**
* Get table data
* #param type $table
* #param type $select
* #param type $condition
* #return mysql_resource
*/
function getResource($table, $field, $condition) {
$resource = mysql_query("SELECT $field FROM $table WHERE $condition ");
return $resource;
}
as you can see #return i write mysql_resource,
is there any convenient name for return mysql resource?
This is typically handled by just "#return resource"... I believe just "resource" matches how var_dump() would show as the object's datatype.

Resources