Thursday, April 23, 2015

Installing full LAMP stack + phpmyadmin on Ubntu 14.04 LTS AWS EC2 instance

Just a step by step tutorial:

1. sudo apt-get update
2. sudo apt-get install apache2
3. Check if apache installed correctly by visiting your servers public IP address:

http://your-servers-ip

4. sudo apt-get install mysql-server php5-mysql #installing mysql
5. sudo mysql_install_db #create database directory structure
6. sudo mysql_secure_installation #run secure installation (just follow the instructions)
7. sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt php5-curl php5-json #install php and some extensions (optionally), like mcrypt, curl and json

You can view all available php5 extensions by running this command in terminal: 

apt-cache search php5-

8. sudo nano /etc/apache2/mods-enabled/dir.conf #place index.php on the first position (instead of index.html by default)

Finally it should look like  

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl # and so on...
</IfModule>

9. sudo service apache2 restart
10. In order to install phpmyadmin you should have mcrypt php5 module installed and activated

sudo apt-get install php5-mcrypt #downloading mcrypt extension

sudo php5enmod mcrypt #activating mcrypt extension

11. sudo apt-get install phpmyadmin
12. sudo service apache2 restart

Now if you will visit http://your-public-ip/phpmyadmin, phpmyadmin page should appear BUT for me it was 404 not found error. To fix this bug you need to follow steps below:

13. sudo ln -s /usr/share/phpmyadmin /var/www/html #create symlink
14. sudo nano /etc/phpmyadmin/apache.conf #open phpmyadmin apache config file and add this line to the end of a file:

Include /etc/phpmyadmin/apache.conf

15. sudo service apache2 restart #restart apache server and try to visit http://your-public-ip/phpmyadmin

Friday, August 1, 2014

Resolving Laravel blade template engine and angularjs conflict

If you use angular.js with laravel for the first time, then you might be interested in how to solve double curly brackets conflict.

{{ variable }}

How can we solve this? Actually we have several ways:

1. Change the angular.js tags

var angularAppName = angular.module('angularAppName', [], function($interpolateProvider) {
    $interpolateProvider.startSymbol('{%'); // This might be dangerous, if you use Twig (if you know what I mean)
    $interpolateProvider.endSymbol('%}');
});

Now you can use your angular expressions like: {% 1 + 2 %}

2. Change the blade templating tags

Blade::setContentTags('<%', '%>'); // For average variables
Blade::setEscapedContentTags('<%%', '%%>'); // For escaped variables

Now you can use your blade expressions like: {% $someVariable %}

Of course you can refuse to use Blade Templates and use Twig instead (search for Laravel Twig bridge in google), or use native PHP as a template engine. This can also solve this problem.

This note was made by me, but original article you can finde here: http://scotch.io/bar-talk/quick-tip-using-laravel-blade-with-angularjs

Thursday, July 31, 2014

VK.com CURL authorization (without standart API)

Few days ago I decided to write a bot for http://olike.ru. Bot was successfully written, but one of my VK.com accounts was frozen for few minutes. So, if you'r planning to use bots in VK.com, be aware, big brother is watching you :)

How can we login into VK.com ? I'ts not that hard, but it has some tricky steps.

1. Firstly we need to send GET request to http://m.vk.com and get "ip_h" parameter (it will be needed later)

$curl = curl_init();
$options = [
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0', // You can use any other user agent
    CURLOPT_URL => 'http://m.vk.com',
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_TIMEOUT => 30
];

curl_setopt_array($curl, $options);
$response = curl_exec($curl);

preg_match('/ip\_h\=(.*?)\&/is', $response, $match);

$ip_h = $match[1];

2. Next, using ip_h parameter, we need to create next url for POST request to vk.com. This request if needed to get the link for actual authentication.

$data = [
    'email' => 'Your vk.com login or email',
    'password' => 'Your vk.com password'
];

$url = 'https://login.vk.com/?act=login&_origin=http://m.vk.com&ip_h='.$ip_h.'&role=pda&utf8=1';

$options = [
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0',
    CURLOPT_URL => $url,
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_POST => 1,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HEADER => 1,
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_COOKIEFILE => 'Path to cookie.txt file', // You can create this file wherever you want. As for me, it was in the same folder as script
    CURLOPT_COOKIEJAR => 'Path to cookie.txt file',
];

curl_setopt_array($curl, $options);
$response = curl_exec($curl);

3. Now we need to parse a little response from the last request, and get from there URL, so we can finally log into the system and make some dirty things

// Getting our login URL
preg_match('/Location: (.*?)\n/is', $response, $match);

// Removing all whitespaces
$url = trim($match[1]);

// Sending request and now we must be logged in
$options = [
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0',
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HEADER => 1,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_COOKIEFILE => 'Path to cookie.txt file',
    CURLOPT_COOKIEJAR => 'Path to cookie.txt file'
];

curl_setopt_array($curl, $options);
curl_exec($curl);

// Closing CURL
curl_close($curl);

This is it :) Now you can send some request, that requires authorization (e.g. add likes to photos, join groups and so on)

Tuesday, July 29, 2014

Laravel package review: Sentry - authentication & authorization system

Brief view

Cartalyst/Sentry is one of the best packages for Laravel 4 (not only), that provides authentication and authorization features. Besides authentication and authorization, Sentry provides group management, permission control, registration, custom hashing and additional security features. You can read full documentation on official Cartalyst page: https://cartalyst.com/manual/sentry

Requirements

  • PHP 5.3+

Supports

  • Laravel 4+
  • Code Igniter 3.0-dev
  • Fuel PHP 1.x
  • Native PHP

Review

First of all, Sentry is really easy to install package. Using Composer your authentication/authorization system will be ready to use in minutes, you just need to add "cartalyst/sentry": "2.1.*" string to your composer.json require array and run composer update.
After installation you will need to configure app.php laravel config, to register provider and its alias, run migrations and publish Sentry config. More detailed documentation you can view on the official Cartalyst page.
Its really easy to work with Sentry. All errors are thrown as an exceptions, and you can easily handle them. As an example, i will show you registration code:

try
{
    // Let's register a user.
    $user = Sentry::register(array(
        'email'    => 'john.doe@example.com',
        'password' => 'test',
    ));

    // Let's get the activation code
    $activationCode = $user->getActivationCode();

    // Send activation code to the user so he can activate the account

}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
    echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
    echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserExistsException $e)
{
    echo 'User with this login already exists.';
}

As you can see, this code is really straight forward. All other functions of Sentry works the same. Code is clear and follows FIG standarts. As for me, I was able to integrate this package with my own social authentication module, because of high customization level of Sentry.
Despite all advantages, I actually had one problem. Sentry has its own social authentication package (Sentry-social), which can work together with Cartalyst. Sad, but I was unable to configure latest version of Sentry with Sentry-social because of some errors. Moreover, i was even unable to find on github source codes of Sentry-social.

Conclusion

As for me, this is one of the best authentication/authorization packages I have ever worked with, not only on Laravel, but on Kohana and Code Igniter too. I strongly recommend you to try it out despite working on your own authorization system.

Friday, July 25, 2014

"Why do I fail?" story


Useful tips on how to create successful business for less than 100$

Based on an article by Tim Ferris: http://fourhourworkweek.com/2012/05/24/six-figure-businesses-built-for-less-than-100-17-lessons-learned/. I just wrote down few interesting moments to sum up the article.

1. Create something, that people don't know how to do (or its hard to understand), but they are interested in this. Provide your product with results (photos, videos, experience etc.)
2. Make things simple. Provide articles with detailed explanations, images, how-to videos.
3. Start quick and start cheap.
4. You always can find money, just be smart. Use kickstarter like websites, try to find the solution. "No" is not always the end answer.
5. Add payment functions to your website. Make them simple.
6. Take a look on something, that people love and hate. This can be a good business model. Make things for those, who love it.
7. Don't work hard, work smart.
8. Try to make things better. You can base on your previous experience and do better experience for your potential customers.
9. Be open to change, test new ideas and new ways.
10. "Make your offer so compelling that buyers have no reason to say no. Give them an offer they can’t refuse."
11. Give people what they really want. Dig deeper into the problem, try to figure out what people really want, not what they say they want.
12. Sell happiness
13. Focus on your actual customers
14. Offer a strong guarantee. Make people trust you, be open.
15. If you have no money on marketing, try to self-promote your product, "hustle" around.
16. Plan your product launch long before actual start. Make people excited, make people want your product like "movie trailers and so on"
17. Learn from mistakes. Learn from your mistakes and other people mistakes too.