Tag Archives: PHP

Yii2 MySQL remote SSL connection

'db' => [
	'class' => 'yii\db\Connection',
	'dsn' => 'mysql:host=REMOTE.HOST.MYSQL;dbname=remote_db_name',
	'username' => 'remote_db_user',
	'password' => 'remote_db_password',
	'charset' => 'utf8',
	'attributes' => [
		PDO::MYSQL_ATTR_SSL_KEY => dirname(dirname(__DIR__)) . '/common/config/ssl/client-key.pem',
		PDO::MYSQL_ATTR_SSL_CERT => dirname(dirname(__DIR__)) . '/common/config/ssl/client-cert.pem',                
		PDO::MYSQL_ATTR_SSL_CA => dirname(dirname(__DIR__)) . '/common/config/ssl/server-ca.pem',                
		],
],

This is assuming that the certs are within the common folder of an Yii2 advanced template project.

It’s probably also a good idea to chmod the pem files to 600 and the ssl directory to 700 so that only the user

Where are the pem files?

/var/lib/mysql/*.pem

Limesurvey Admin 500 Internal Server Error

Recently I moved a LimeSurvey instance from one server to another, unfortunately, upon doing this I was suddenly unable to login to the admin panel.
The login screen would appear, but upon trying to login, the browser just sat there with a spinning “thinking” wheel.

After sometime the following would appear on the screen:

Request Timeout
This request takes too long to process, it is timed out by the server. If it should not be timed out, please contact administrator of this web site to increase ‘Connection Timeout’.

I tried enabling Debugging mode, but nothing was logged.
After some googling, I found this thread followed by this thread

The solution
It turns out that Limesurvey is trying to ‘call’ home in the background to see if there is an update. For whatever reason, this request is failing.
To stop it from doing this, it’s possible to edit the config.php file (/application/config/config.php)
And under the section:

'config'=>array(
// debug: Set this to 1 if you are looking for errors. If you still get no errors after enabling this
// then please check your error-logs - either in your hosting provider admin panel or in some /logs directory
// on your webspace.
// LimeSurvey developers: Set this to 2 to additionally display STRICT PHP error messages and get full access to standard templates
'debug'=>0,
'debugsql'=>0, // Set this to 1 to enanble sql logging, only active when debug = 2
// Update default LimeSurvey config here
)

And add a line to the bottom there ‘updatable’ => false,:


'config'=>array(
// debug: Set this to 1 if you are looking for errors. If you still get no errors after enabling this
// then please check your error-logs - either in your hosting provider admin panel or in some /logs directory
// on your webspace.
// LimeSurvey developers: Set this to 2 to additionally display STRICT PHP error messages and get full access to standard templates
'debug'=>0,
'debugsql'=>0, // Set this to 1 to enanble sql logging, only active when debug = 2
// Update default LimeSurvey config here
'updatable' => false,
)

Success!
I use Installatron inside of Cpanel to manage updates, so I’m ok with disabling updates here.

Environment Info:
LimeSurvey: 2.72.3+171020
Cpanel with
PHP 7
Litespeed

Yii CGridView Default Order / Yii ActiveRecord Default Sort

Disclaimer, I know next to nothing about Yii….

Today I had an issue with a Yii App and it’s default admin CRUD tables, and sorting fields, well everything that listed a record from the database was in the order it was inserted into the database and not in a human friendly alphabetical order.

It turns out it’s really easy to set a default so that everything that uses the Model, comes out in a sorted way. (Note for larger things, you probably don’t want every single query that hits the DB getting sorted, so you’d probably only apply the sorting as required, but for small things, this shouldn’t be an issue).

For this example I have a Model called ‘Contacts’.

Open up your model (mine is: /models/Contacts.php)

Above the function ‘ public static function model($className=__CLASS__)’

Add the following function (if it’s not present)


/*
* Let's Setup some defaults!
*/
public function defaultScope()
{
return array(
'order' => 'FirstName'
);
}

In the array of ‘order’ change the value to the name of the column that you want to sort by, for my example it was ‘FirstName’.

Save, upload, test, enjoy.

Like I said at the start, I know nothing about Yii, had I, I probably wouldn’t have spent 20 minutes googling it.

Resources: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#default-scope

htaccess max upload size in wordpress

I had a host today that wouldn’t allow wordpress to upload any files (images/themes/plugins).

It turns out some .htaccess magic saved the day:

SecFilterScanPOST Off
php_value upload_max_filesize 32M
php_value post_max_size 32M
php_value max_execution_time 300
php_value max_input_time 300

Given that it’s in the .htaccess file, it will only work on some hosts that allow you to configure php via the .htaccess, but hey if it works great!

Thanks to this site for help

osTicket 1.9.4 – Show Due Date Column

Andrew (another one) got in contact with me asking if I could help him to how to show an extra column in osTicket 1.9.4, he tired to follow my previous post without much luck (after rereading it, I’m surprised if that’s ever helped anyone!).

He wanted the ability to see and sort by a “Due Date” column. Well it turns out to be a really easy feature to add.

Here is a GitHub Gist of the modified tickets.inc.php

Instructions for use:

  1. Backup /[osticket-install]/include/staff/tickets.inc.php
  2. Download the following file and copy it to /[osticket-install]/include/staff/tickets.inc.php
  3. Test! (If it break badly, I’m sorry, please restore your backup file).

As a side note, it also got me to finally setup a github account!

Here is the step by step instructions:

  1. Update the sort options ~Line 170.Effectively what we are doing here is adding the ‘duedate’=>’duedate’ to the array. So replace $sortOptions with below:
    [gist id = “c66ef9d90bdcbf874685” file = “step-1.php” width = “90%”]
  2. Add the heading to the table ~Line 374
    Just add the following line in after the closing </th> tag for the line <a <?php echo $date_sort; ?>………
    [gist id = “c66ef9d90bdcbf874685” file = “step-2.php” width = “90%”]
  3. Add the data row to the table ~ Line 468
    After the <td align=”center” nowrap><?php echo Format::db_datetime($row[‘effective_date’]); ?></td> add:
    [gist id = “c66ef9d90bdcbf874685” file = “step-3.php” width = “90%”]
  4. Finally fix up the footer ~ Line 506
    Change <td colspan=”7″> to:
    [gist id = “c66ef9d90bdcbf874685” file = “step-4.php” width = “90%]
  5. Thats it!

Don’t break it!

 

Compiling PHP into an EXE along with additional command line arguments

Recently I was building a simple PHP script that took the input of a CSV and extracted the data from it and feed it into a JSON-RPC based API.

Originally I had planned to automate the FTP of the CSV to the server, but then I thought if I could take the PHP file to the CSV it might be easier. The other issue I had was that I didn’t want to setup a complete server just to run a single PHP file.

Enter PHC-WIN: http://wiki.swiftlytilting.com/Phc-win

phc-win appears to be one of the simplest options for converting a simple script into a simple exe file (although a handful of DLL files are required to make the exe file run).

After testing and experimenting with the compiler, I thought “wouldn’t it be great to be able to pass some additional command line arguments to the complied file (think the filename of the CSV file).”

Well it turns out that you can. The variable that sorts the additional arguments is the “$_SERVER[‘argv’] array.

A simple print_r($_SERVER[‘argv’]); command allows you to see all the arguments and the order that they are passed to the program. It appears that the first one (position 0) is always the name of the application.

Quick, simple and easy :).