Drupal

Warning message

JQuery Cycle must be installed in order to run the slideshow. Please go to Status Report for instructions.

Contains all the posts related to drupal .

The password is failed to login because special character symbol like “$”(dollar sign) is considered to be special character in Linux.
drush upwd USERNAME --password="PASSWORD"

This command is actually running for Linux in which some special characters always needs to be escaped with “\”. So if you try the password:

 drush upwd root --password="S$1gma"

The above password will never work, to make the above password work you need to escape it in Linux like:

drush upwd root --password="S\$1gma"

Of course the new password now is S$1gma but this is the way how bash deals with some characters.

So either you escape all the special characters in your string or use some SAFE SET of characters like [,._+:@%/-] which don't need to be escaped.

Settings file protection by password encryption

Settings.php was criticized for storing the plain text MySQL username and password. This procedure will let us remove the username and password from the settings.php and let us put that credentials outside of the website directory and store into another folder which can be taken care by security team or server managing people.

To do this, include a line at the top of settings.php file:

require_once '../secure_mysite/secure_credentials.php';

This line specify that include a script called “secure_credentials.php” and if it’s not found, stop the script from further running. For now, we have created a secure folder called “dbpw” which we have stored outside of website directory. We stored our php script into this folder and call that script using above line inside settings.php.
This secure_credentials.php contain two function i.e. one for encryption and one for decryption.

secure_credentials.php

function encrypt_credentials($username, $password, $encrypted_key = NULL, $iv = NULL) {
  $method = "AES-256-CBC";
  $password_file = "/www/htdocs/secure_mysite/pass.txt";
  $key_file = "/www/htdocs/secure_mysite/key.txt";
  $wasItSecure = true;
  $ivlen = openssl_cipher_iv_length($method);
  $iv = openssl_random_pseudo_bytes($ivlen, $wasItSecure);

  if (!file_exists($key_file)) {
  	if(!$fh = fopen($key_file, "w")) { die("

Could Not Open Key File"); } $encrypted_key = openssl_random_pseudo_bytes(32); $result = fwrite($fh, $encrypted_key); fclose($fh); } $encrypted_data = openssl_encrypt($password, $method, $encrypted_key, 0, $iv); //create a file handler by opening the file if(!$fh = fopen($password_file, "w")) { die("

Could Not Open Password File"); } $txt = $username . ":" . $encrypted_data . ":" . $iv; $result = fwrite($fh, $txt); if($result) echo "Username and Password successfully encrypted and stored\n"; fclose($fh); } function decrypt_credentials($param) { $password_file = "/www/htdocs/secure_mysite/pass.txt"; $key_file = "/www/htdocs/secure_mysite/key.txt"; $method = "AES-256-CBC"; $filecontents = file_get_contents($key_file); $all_words = preg_split('/[\s]+/', $filecontents, -1, PREG_SPLIT_NO_EMPTY); if(!$fh = fopen($password_file, "r")) { die("

Could Not Open Password File"); } while(!feof($fh)) { $line = fgets($fh, 4096); $from_file = explode(":", $line); } $decrypted_pass = openssl_decrypt($from_file[1], $method, $all_words[0], 0, $from_file[2]); fclose($fh); return ($param == 'username') ? $from_file[0] : $decrypted_pass; }

Encryption Function:

function encrypt_credentials($username, $password, $rand_pass = NULL, $iv = NULL) {}

This function takes 4 parameters in which 2 are optional, but username and password is required. This function used AES-256-CBC encryption method to encrypt the password. To use the encryption properly I have used:

string openssl_encrypt ( string $data , string $method , string $password [, int $options = 0 [,string $iv = "" ]] )

For making this script more automatic and independent I have generated 3rd argument $encrypted_key in above function by using an inbuilt function openssl_random_pseudo_bytes(). This key will be stored in a separate key file called “key.txt”. The key will be generated only once when the program runs first time. If you want to generate a new or different key you have to delete key.txt file. The openssl_encrypt() function require one more parameter which is “iv” (initialization vector) as the 5h parameter. The “iv” is generated automatically in this script by PHP inbuilt function openssl_random_pseudo_bytes(). Then all these parameters passed into openssl_encrypt() function to generate the encrypted data. The generated data will be stored inside “pass.txt” file. This text file contain 3 parameters as well which are separated by “:” parameter.
Username: Encrypted Data: IV

To run the above function we need to run this command by CD into the folder and then run the command from the command line:

 php -r 'require "secure_credentials.php";encrypt_credentials("superman", "makeitsecure!#2");' 

Where superman is example of username and makeitsecure!#2 is the password.

You will only need to run this command with username and password. With this command it will call the encrypt_credentials() function inside the secure_credentials.php file and store the results in pass.txt file. File pass.txt will look something like this:

 superman:cmWK+ymPjL7vB5v4+FM39A==:°ß™™ÊÏ s߈ÜÄVë 

When the settings file will run it will call the decrypt function.
The two statements inside the settings file will call the decrypt function in the secure_credentials.php file.
decrypt_credentials('username')
decrypt_credentials('pass')

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'yourdb',
      'username' => decrypt_credentials('username'), //Note this statement
      'password' => decrypt_credentials('password'), //Note this statement
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

When this function is called, it looks for the pass.txt, opens it and decrypt the password and send the password in the decrypted format to the Drupal back again.
By above process we are not storing the plain password anywhere and the password will be only known by the Person who will run the command line function. If he forgots the password, this command line function needs to be run once again.

For Drupal 7 :

drush vset maintenance_mode 1  // To take website into maintenance mode
drush vset maintenance_mode 0  // To take website into live mode

For Drupal 6 :

drush vset site_offline 1  // To take website into maintenance mode
drush vset site_offline 0  // To take website into live mode

Get the value of UTC time and convert it into unix timestamp and then convert that timestamp to date will give you exact local date and time.

$time = strtotime($dateInUTC.' UTC');
$LocalDate = date("Y-m-d H:i:s", $time);

First Create a function hook_form_alter.Then inside hook_form_alter depending upon your choice :
a) If you want to disable field (it will be visible, but user can't change it) :

$form['field_myfield']['#disabled'] = TRUE;

b) If you want it to be hidden, but to keep value it has before editing the way to do that is:

$form['field_myfield']['#access'] = FALSE;

c) if you want to hide it:

hide($form['field_myfield']);

If you want to have a taxonomy for countries inside your website, you can download the XML file attached with this post and then download the Taxonomy Import module from here https://www.drupal.org/project/taxonomy_xml.
After downloading, you can see two new tabs for import/export on taxonomies page.
Go to import -> Target Vocabulary (Select "Your Desktop Direct File Upload") -> Browse file to upload -> From dropdown select format of file to XML -> then click on import.

You can see that a new vocabulary has been created, then later you can change the name of vocabulary .

Download File: 

To increase your PHP Memory Limit (memory_limit) in your php.ini file. If you dont know where is your php.ini file, you can look at this post
http://techrappers.com/post/28/creating-custom-phpini-file-shared-server
Then look for the word memory_limit, default memory limit is 64 MB. Now you can increase it to either 128 MB or bigger number like 512 MB.

memory_limit = 128M  ;

You can also use :

ini_set('memory_limit','64M'); 

in your PHP script for example in index.php to increase the memory limit.

This error occurs when PHP requires more memory to execute a process than it is currently allocated.The above error tells that it requires more memory but currently having only around 64 MB of memory allocated.

To solve this issue you have to increase your PHP Memory Limit (memory_limit) in your php.ini file. If you dont know where is your php.ini file, you can look at this post
http://techrappers.com/post/28/creating-custom-phpini-file-shared-server
Then look for the word memory_limit, default memory limit is 64 MB. Now you can increase it to either 128 MB or bigger number like 512 MB.

memory_limit = 128M  ;

You can also use :

ini_set('memory_limit','64M'); 

in your PHP script for example in index.php to increase the memory limit.

Suppose you are using drupal entity form rather than a webform, because you would like to group the fields and take advantage of add more button. Using entity form is also useful when you have to interact with drupal core node fields. Below code is useful if you want to get the values of fields inside a particular entity form.

  $entityform_id = 1; // entity form id
  $entityform = entityform_load($entityform_id);
  $entityform_wrapper = entity_metadata_wrapper('entityform', $entityform);
  // Access fields with $entityform_wrapper->field_name->value() or ->field_name->raw()
  $field_my_field = $entityform_wrapper->field_name->value(); // for example

  // If you have single field 
  drupal_set_message($entityform_wrapper->field_my_field->value()); // for example

  // if you have mutilple fields using field collection and field_my_field is a group of fields from field collection
  foreach ($field_my_field as $field_my_field_key => $field_my_field_value) {
    drupal_set_message($field_my_field_value->you_field_name[LANGUAGE_NONE][0]['value']);
  }

First reach to the destination folder on which you want to perform this operation.
Then login to your linux terminal with username and password and run the following command :

tar -czvf bob_backup.tar.gz *

tar is format of the file, tar is used for compression of files.
c (--create)
z (tar has an option built into it to use 'gzip' to zip the file at the same time you make the tarball. If you add z to the options, and change the name of the file to create to a .gz extension)
v (--verbose-show what files they are)
f (--file -make a file - should always be the last option)

Pages

Subscribe to RSS - Drupal