Sunday, November 24, 2019

php 7.3 compact() undefined variable

If you have used compact and upgraded php to 7.3 you maybe noticed warnings when variable is not defined.
PHP 7.3 made compact function strict and it will throw warnings.

Quick dirty solution:
Replace
compact
with

@compact

Normal solution:

Replace compact with array. It will take time, so if you are in production use quick dirty solution to supress notice, and when you have time make changes.
Here gist https://gist.github.com/AucT/55340f911259fd7a6e9a76b766985c6c  you can use it on any php online website and decompact your function calls.

Sunday, August 11, 2019

Prevent / Defend Against XSS Attack in PHP

Include funtion e. This will escape malicious js stuff.

function e($value)
{
    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
}

Call function e to the input you need to display. For example

<input type="text" value="<?=e($_GET['q']??'')?>">

Tuesday, August 6, 2019

Create symlink folder

For example we have our folder in /etc/nginx/sites-enabled/example.com
And to make virtual symlink folder here /etc/nginx/sites-available we run this

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/


Thursday, July 25, 2019

Windows cmd for each file in folder do smthing

for %f in (*.*) do echo %f
If there is space in files and you need to call some tool

for %f in (*.*) do call MyTool "%f"

Thursday, February 21, 2019

docker mysql

docker run --name my-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 -d mysql

Tuesday, November 27, 2018

nginx reload

sudo nginx -t
sudo systemctl restart nginx

Saturday, November 24, 2018

Import 2G sql file to mysql database

edit my.cnf
my.cnf
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
innodb_buffer_pool_size=2048M (50% of your ram)
max_connections=100
key_buffer_size=256M
max_allowed_packet=256M

innodb_buffer_pool_size = 2G
innodb_log_buffer_size = 256M
innodb_log_file_size = 1G
innodb_write_io_threads = 16
innodb_flush_log_at_trx_commit = 0
then
mysql -uroot -p dbname < /var/www/mydb.sql


source: https://dba.stackexchange.com/a/83385/86553

Sunday, July 22, 2018

Post to facebook page from php

Facebook has very weird documentation. If you have facebook page and want to post on page with php script here what you need:

1. Facebook page
2. Your user never ending access token. Here normal guide how to get it https://medium.com/@Jenananthan/how-to-create-non-expiry-facebook-page-token-6505c642d0b1
3. Get your pageId. Go to your page Information (or About)->In the very bottom your pageId. At the moment of the article it is here https://www.facebook.com/pg/<pageUrl>/about/. Where <pageUrl> is your facebook page slug.
4. Get facebook php sdk  https://github.com/facebook/php-graph-sdk with composer

composer require facebook/graph-sdk
Note: this command is good at the moment of this article. But you better go to github and copy, because they can change it.


Now here the script


$appId = ''; //your App Id
$appSecret = ''; //your app secret
$pageId = '0000000000'; //id of the page you want to publish on
$userAccessToken = ''; //Your never expire user access token

$fb = new \Facebook\Facebook([
    'app_id' => $appId,
    'app_secret' => $appSecret,
    'default_graph_version' => 'v3.0',
]);
$linkData = [
    'link' => 'http://google.com', //TODO  change
    'message' => "My message", //TODO  change
];


$longLivedToken = $fb->getOAuth2Client()->getLongLivedAccessToken($userAccessToken);
$fb->setDefaultAccessToken($longLivedToken);
$response = $fb->sendRequest('GET', $pageId, ['fields' => 'access_token'])
    ->getDecodedBody();

$foreverPageAccessToken = $response['access_token'];
$fb->setDefaultAccessToken($foreverPageAccessToken);
$response = $fb->post("/$pageId/feed", $linkData);
Note: If you're using github then values for first 4 variables should be outside of git. For example in env file or php file which is in gitignore.

Sunday, July 15, 2018

php get substring after character or word


Here we will get substring after word '/'
$item = 'posts/p10504';

substr(strrchr($item, '/'),1);
substr($item , strpos($item , '/') + 1);

//both lines has value of p10504

Tuesday, June 19, 2018

[Solved] LombokProcessor could not be initialized

switch jdk to 1.8
If you use intellij got to file->project structure->project and choose 1.8 jdk

Friday, March 30, 2018

redis commands

start cli
redis-cli

show keys
KEYS

showo keys starting with blog
KEYS blog*

clear all keys (delete all from redis)
flushall

Wednesday, November 29, 2017

Elastic search start stop

Start
sudo service elasticsearch start
Stop
sudo service elasticsearch stop

Thursday, October 5, 2017

Tuesday, September 12, 2017

copy content of folder to another folder

see what owner of files is (for sake).

sudo cp -a /source/. /dest/
this should copy all files and in my case assign right owner.
If not cd to the dest folder and use

sudo chown www-data:www-data *

Nginx test && reload

sudo nginx -t && sudo nginx -s reload

Thursday, August 17, 2017

Friday, June 23, 2017

How to remove permanently last commit from git remote

To only remove commit but save current code use:

git reset --soft HEAD~1
...make new commit and then
git push origin master --force


To remove commit and remove commit's code use:

git reset --hard HEAD~1
git push origin master --force

Saturday, April 8, 2017

how to create helpers file laravel5

Simple tip for simple stuff.

create folder Helpers in app directory
create file StringHelper.php in app\Helpers

<?php
namespace App\Helpers;
class StringHelper
{
public static function getCleanString($string)
{
//remove unnecessary chars
$string = str_replace(['-', ' '], '', $string);
return str_slug($string);
}
}

add any simple function in this class as static.
when you need a different helper, create new class for example YoutubeHelper.php and add there stuff you need.

usage

\App\Helpers\StringHelper::getCleanString($name)