Simple way to encrypt and decrypt files in php

simple way to encrypt and decrypt files in php

  1. <?php
  2. /*
  3.  * Created By Ayham Alsuleman
  4.  * CryptFile and DecryptFile
  5. */
  6.  
  7. function CryptFile($InFileName,$OutFileName,$password){
  8. //check the file if exists
  9. if (file_exists($InFileName)){
  10.  
  11. //get file content as string
  12. $InFile = file_get_contents($InFileName);
  13.  
  14. // get string length
  15. $StrLen = strlen($InFile);
  16.  
  17. // get string char by char
  18. for ($i = 0; $i < $StrLen ; $i++){
  19. //current char
  20. $chr = substr($InFile,$i,1);
  21.  
  22. //get password char by char
  23. $modulus = $i % strlen($password);
  24. $passwordchr = substr($password,$modulus, 1);
  25.  
  26. //encryption algorithm
  27. $OutFile .= chr(ord($chr)+ord($passwordchr));
  28. }
  29.  
  30. $OutFile = base64_encode($OutFile);
  31.  
  32. //write to a new file
  33. if($newfile = fopen($OutFileName, "c")){
  34. file_put_contents($OutFileName,$OutFile);
  35. fclose($newfile);
  36. return true;
  37. }else{
  38. return false;
  39. }
  40. }else{
  41. return false;
  42. }
  43. }
  44.  
  45.  
  46.  
  47.  
  48. function DecryptFile($InFileName,$OutFileName,$password){
  49. //check the file if exists
  50. if (file_exists($InFileName)){
  51.  
  52. //get file content as string
  53. $InFile = file_get_contents($InFileName);
  54. $InFile = base64_decode($InFile);
  55. // get string length
  56. $StrLen = strlen($InFile);
  57.  
  58. // get string char by char
  59. for ($i = 0; $i < $StrLen ; $i++){
  60. //current char
  61. $chr = substr($InFile,$i,1);
  62.  
  63. //get password char by char
  64. $modulus = $i % strlen($password);
  65. $passwordchr = substr($password,$modulus, 1);
  66.  
  67. //encryption algorithm
  68. $OutFile .= chr(ord($chr)-ord($passwordchr));
  69. }
  70.  
  71. //write to a new file
  72. if($newfile = fopen($OutFileName, "c")){
  73. file_put_contents($OutFileName,$OutFile);
  74. fclose($newfile);
  75. return true;
  76. }else{
  77. return false;
  78. }
  79. }else{
  80. return false;
  81. }
  82. }
  83.  
  84. //Example
  85. $orginalfile = 'orginal.jpg';
  86. $crypt = 'crypt.jpg';
  87. $decrypt = 'decrypt.jpg';
  88. $password = 'pass';
  89.  
  90. CryptFile($orginalfile,$crypt,$password);
  91. DecryptFile($crypt,$decrypt,$password);
  92. ?>

Customize login form block in drupal

I just add this code to drupal documentation page in http://drupal.org/node/478328

paste this in your template.php file in theme directory.

  1. <?php
  2. /**
  3. * This snippet will register a theme implementation of the user login form.
  4. *
  5. * Drupal 6.x only!
  6. * Replace 'mytheme' with the name of your theme.
  7. */
  8. function MYTHEME_theme(){
  9. return array(
  10. 'user_login_block' => array(
  11. 'template' => 'user-login-block',
  12. 'arguments' => array('form' => NULL),
  13. )
  14. );
  15. }
  16.  
  17. function MYTHEME_preprocess_user_login_block(&$variables) {
  18. // Add a custom placeholder to username field.
  19. $variables['form']['name']['#value'] = $variables['form']['name']['#title'];
  20. $variables['form']['name']['#attributes']['onblur'] = "if (this.value == '') {this.value = '".$variables['form']['name']['#value']."';} ;";
  21. $variables['form']['name']['#attributes']['onfocus'] = "if (this.value == '".$variables['form']['name']['#value']."') {this.value = '';} ;";
  22.  
  23. // Add a custom placeholder to password field.
  24. // this will not work until you clicked in input, to make this work see <a href="http://drupal.org/node/75225" title="http://drupal.org/node/75225">http://drupal.org/node/75225</a> and apply the patch
  25. $variables['form']['pass']['#value'] = $variables['form']['pass']['#title'];
  26. $variables['form']['pass']['#attributes']['onblur'] = "if (this.value == '') {this.value = '".$variables['form']['pass']['#value']."';} ;";
  27. $variables['form']['pass']['#attributes']['onfocus'] = "if (this.value == '".$variables['form']['pass']['#value']."') {this.value = '';} ;";
  28.  
  29. // Remove the "Username" & "Password" labels from the form.
  30. unset($variables['form']['name']['#title']);
  31. unset($variables['form']['pass']['#title']);
  32.  
  33. // Add some classes
  34. $variables['form']['name']['#attributes']['class'] = 'name-field-login-form';
  35. $variables['form']['pass']['#attributes']['class'] = 'pass-field-login-form';
  36. $variables['form']['submit']['#attributes']['class'] = 'submit-field-login-form';
  37.  
  38. // Change the text on the submit button
  39. //$variables['form']['submit']['#value'] = t('Login');
  40.  
  41. // Make the submit button (image)
  42. $variables['form']['submit']['#type'] = 'image_button';
  43. $variables['form']['submit']['#src'] = path_to_theme() . '/images/login.png';
  44.  
  45. // Remove links
  46. //unset($variables['form']['links']);
  47. $variables['form']['name']['#required'] = false;
  48. $variables['form']['pass']['#required'] = false;
  49. $variables['rendered'] = drupal_render($variables['form']);
  50. }
  51. ?>

Note that password input will not work (will not get a default value),
See http://drupal.org/node/75225 and apply the patch

Make a new file "user-login-block.tpl.php" in your theme, and add the following:

  1. <div class="login-form-custom">
  2. <?php print $rendered; ?>
  3. </div>

Now clear your theme registry by visiting and saving /admin/build/themes, and your new tpl.php should work.

Customize search form in Drupal

paste this in your template.php file in theme directory, please read following code and customize with your needs

  1. <?php
  2. function MYTHEME_preprocess_search_theme_form(&$variables, $hook) {
  3. // Add a custom placeholder text to Search box field.
  4. $variables['form']['search_theme_form']['#value'] = $variables['form']['search_theme_form']['#title'];
  5. $variables['form']['search_theme_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = '".$variables['form']['search_theme_form']['#value']."';} ;";
  6. $variables['form']['search_theme_form']['#attributes']['onfocus'] = "if (this.value == '".$variables['form']['search_theme_form']['#value']."') {this.value = '';} ;";
  7.  
  8. // Remove the "Search this site" label from the form.
  9. unset($variables['form']['search_theme_form']['#title']);
  10.  
  11. // Add a custom class to the search box.
  12. $variables['form']['search_theme_form']['#attributes']['class'] = 'search-box';
  13.  
  14. //change the text field size
  15. $variables['form']['search_theme_form']['#size'] = 25;
  16.  
  17. // Change the text on the submit button
  18. //$variables['form']['submit']['#value'] = t('Go');
  19.  
  20. // Rebuild the rendered version (search form only, rest remains unchanged)
  21. unset($variables['form']['search_theme_form']['#printed']);
  22. $variables['search']['search_theme_form'] = drupal_render($variables['form']['search_theme_form']);
  23.  
  24. $variables['form']['submit']['#type'] = 'image_button';
  25. $variables['form']['submit']['#src'] = path_to_theme().'/images/search.png';
  26.  
  27. // Rebuild the rendered version (submit button, rest remains unchanged)
  28. unset($variables['form']['submit']['#printed']);
  29. $variables['search']['submit'] = drupal_render($variables['form']['submit']);
  30.  
  31. // Collect all form elements to make it easier to print the whole form.
  32. $variables['search_form'] = implode($variables['search']);
  33. }
  34. }
  35. ?>

7 x 7 improvements in Drupal 7

7 x 7 improvements in Drupal 7:
why to use Drupal?
as a user, developer, themer ,sysadmin ..etc

Martix Multiply function

This function multiply any two arrays (Matrix) with matrix multiply conditions ,get entry from files

attach:
file:array1.txt
content:
3;5;4
6;2;3

file:array2.txt
content:
2;1
6;7
8;9

Code:

  1. <?php
  2. /*
  3.  * Created By Ayham Alsuleman in 25\10\2010 9:22am
  4.  * MartixMultiply
  5. */
  6.  
  7. //check if the gives file is a matrix
  8. function IsMatrix($file){
  9. if (file_exists($file)){
  10. //read the file
  11. $array = file($file);
  12. $rows = count($array);
  13. $cols = count(split(';',$array[0]));
  14. $error = false;
  15. //check if all the matrix with same cols
  16. foreach ($array as $k =>$line){
  17. $linecols = count(split(';',$line));
  18. if($linecols !== $cols){
  19. $error = true;
  20. }
  21. }
  22. //return false if not matrix
  23. if ($error == true){
  24. return false;
  25. //return dim of matrix
  26. }else{
  27. $dim = array();
  28. $dim['cols'] = $cols;
  29. $dim['rows'] = $rows;
  30. return $dim;
  31. }
  32. }
  33. }
  34.  
  35. //return Matrix Cell
  36. function MatrixCell($array){
  37. if(is_array($array)){
  38. $mat = array();
  39. $k = 0;
  40. foreach ($array as $ln => $line){
  41. $linearray = split(';',$line);
  42. foreach ($linearray as $cn => $cell){
  43. $mat[$k][$cn] = $cell;
  44. }
  45. $k++;
  46. }
  47. return $mat;
  48. }else{
  49. return false;
  50. }
  51. }
  52.  
  53.  
  54. function MartixMultiply ($file1,$file2){
  55. if (IsMatrix($file1) && IsMatrix($file2)){
  56. $dim1 = IsMatrix($file1);
  57. $dim2 = IsMatrix($file2);
  58. $mat1 = MatrixCell(file($file1));
  59. $mat2 = MatrixCell(file($file2));
  60. //to multiply two arrays the close dimensions must be equal
  61. //and the result array will be dimensions of the sides ..
  62. //e.g array1[5,2]*array2[2,3] we can multiply because the close dimensions equals 2 = 2
  63. //and the result array will be array[5,3]
  64. if($dim1['rows'] == $dim2['cols']){
  65. $mat = array();
  66. //walk on rows in first mat
  67. for ($i=0; $i<$dim1['rows']; $i++) {
  68. //work on cols in second mat
  69. for ($j=0; $j<$dim2['cols']; $j++) {
  70. $x = 0;
  71. //walk on each cell in cols in second mat and get the result
  72. for ($k=0; $k<$dim2['rows']; $k++) {
  73. $x += $mat1[$i][$k] * $mat2[$k][$j];
  74. }
  75. $mat[$i][$j] = $x;
  76. }
  77. }
  78. return $mat;
  79. }else{
  80. return false;
  81. }
  82. }
  83. }
  84.  
  85. //Example
  86. $file1 = 'array1.txt';
  87. $file2 = 'array2.txt';
  88. print '<pre>';
  89. print_r (MartixMultiply ($file1,$file2));
  90. print '</pre>';

Redirect user to edit his profile after register in drupal

you need :
Token + Token actions + Trigger
1- Go to admin/settings/actions/manage
2- add Redirect to a tokenized URL... and enter
user/[UID]/edit then save
3- Go to admin/build/trigger/user
4- after register select from dropdown list Redirect to a tokenized URL then click Assign

you can customize this page with
profile category weight
one page profile

How to control Block visibility

This code allow block to show in
training
training/*
content type 'product'
and all terms with taxonomy id 3,6,7

  1. <?php
  2. if ( ($_GET['q'] == 'training') or (substr($_GET['q'],0,9 ) == 'training/')){
  3. return true;
  4. }else{
  5. // valid node id in view mode
  6. if ( arg(0) == 'node' AND is_numeric(arg(1)) AND arg(2) == FALSE ) {
  7. $node = node_load(arg(1)); // cached
  8. if ( ($node->type == 'product') ) {
  9. return true;
  10. }
  11. }else{
  12. $match = FALSE;
  13.  
  14.  
  15. // put here the vocabulary ID you're interested in
  16. $vids = array(3,6,7);
  17.  
  18. if (arg(0) == 'taxonomy' && arg(1) == 'term') {
  19. foreach( $vids as $vk => $vid){
  20. $str_tids = arg(2);
  21. $terms = taxonomy_get_tree($vid);
  22. foreach ( $terms as $term ) {
  23. $items[] = $term->tid;
  24. }
  25.  
  26. $terms = taxonomy_terms_parse_string($str_tids);
  27.  
  28. //browse each value
  29. foreach ($terms['tids'] as $k => $v) {
  30. if (in_array($v, $items)) {
  31. $match = TRUE;
  32. }
  33. }
  34. }
  35. }
  36. }
  37. return $match;
  38.  
  39. }
  40. return FALSE;
  41. ?>

Breadcrumb in taxonomy term page

make custom Breadcrumb in taxonomy term page

  1. <?php
  2. function YOURTHEME_breadcrumb($breadcrumb) {
  3.  
  4. if (arg(0) == 'taxonomy' && arg(1) == 'term') {
  5. $tid = arg(2);
  6. $term = taxonomy_get_term($tid);
  7. $vocabulary = taxonomy_vocabulary_load($term->vid);
  8. $vterms = taxonomy_get_tree($term->vid);
  9. $vitems = '';
  10. foreach ( $vterms as $vterm ) {
  11. $vitems .= "&#43;".$vterm->tid;
  12. }
  13.  
  14. $breadcrumb[] = l($vocabulary->name, 'taxonomy/term/'.$vitems);
  15. $breadcrumb[] = l($term->name, 'taxonomy/term/'.$term->tid);
  16. }
  17. if (count($breadcrumb) > 1) {
  18. // $breadcrumb[] = drupal_get_title();
  19. if ($breadcrumb) {
  20. return '<div class="breadcrumb">'. implode(' &rsaquo; ', $breadcrumb) ."</div>\n";
  21. }
  22. }
  23. }
  24. ?>


Ayham Alsuleman
Syndicate content