save to ftp

  1. <?php
  2. // $Id: savetoftp.module,v 1.0 2008/08/05 11:00:00 kevin Exp $
  3.  
  4. /**
  5.  * Lets users save nodes to an FTP server.
  6.  * Adds text boxes for logging into FTP server in order to save.
  7.  */
  8.  
  9. /**
  10.  * Implementation of hook_nodeapi().
  11.  */
  12. function savetoftp_nodeapi(&$node, $op, $teaser, $page) {
  13.  switch ($op) {
  14.    case 'view':
  15.         global $user;
  16.         if ($user->uid == 0) {
  17.                         break;
  18.                 }
  19.         $node->content['savetoftp'] = array(
  20.                 '#value' => drupal_get_form('savetoftp', $node),
  21.                 '#weight' => 10
  22.                 );
  23.         }
  24. }
  25. /**
  26. * Implementation of hook_form_alter. Define the form for saving to ftp.
  27. */     
  28.  
  29. function savetoftp_form_alter($form_id, &$form) {
  30.  if ($form_id == $form['#node']->type .'_node_form') { 
  31.         $form['savetoftp'] = array(
  32.       '#type' => 'fieldset',
  33.       '#title' => t('FTP Server settings'),
  34.       '#collapsible' => TRUE,
  35.       '#collapsed' => FALSE,
  36.       '#access' => user_access('administer nodes'),
  37.       '#weight' => 30,
  38.     );
  39.     $form['savetoftp']['ftp_server'] = array(
  40.       '#type' => 'textfield',
  41.       '#default_value' => variable_get('ftp_server', 'www.sfsu.edu'),
  42.       '#maxlength' => 50,
  43.       '#collapsible' => TRUE,
  44.       '#collapsed' => TRUE,
  45.       '#description' => t('Enter your FTP Server'),
  46.     );
  47.         $form['savetoftp']['ftp_username'] = array(
  48.       '#type' => 'textfield',
  49.       '#default_value' => variable_get('ftp_username', ''),
  50.       '#maxlength' => 50,
  51.       '#collapsible' => TRUE,
  52.       '#collapsed' => TRUE,
  53.       '#description' => t('Enter your FTP Username'),
  54.     );
  55.         $form['savetoftp']['ftp_password'] = array(
  56.       '#type' => 'password',
  57.       '#default_value' => variable_get('ftp_password', 0),
  58.       '#maxlength' => 50,
  59.           '#size' => 15,
  60.       '#collapsible' => TRUE,
  61.       '#collapsed' => TRUE,
  62.       '#description' => t('Enter your FTP Password'),
  63.     );
  64.         $form['savetoftp']['ftp_directory'] = array(
  65.       '#type' => 'textfield',
  66.       '#default_value' => variable_get('ftp_directory', 'public_html'),
  67.       '#maxlength' => 50,
  68.       '#collapsible' => TRUE,
  69.       '#collapsed' => TRUE,
  70.       '#description' => t('Enter your default directory, this will most likely be public_html or blank'),
  71.     );
  72.         $form['#submit']['savetoftp'] = array(
  73.         'save_to_ftp' => array()
  74.         );
  75. }       
  76. }
  77.        
  78. function save_to_ftp($form, &$form_values) {
  79. /** Code added to save node to a file and then send it to ftp server */
  80.         $html_title = $form_values['title'];
  81.         $html_title = str_replace(" ", "", $html_title);
  82.         $html_header = $form_values['field_header'][0][value];
  83.         $html_leftsidebar = $form_values['field_leftsidebar'][0][value];
  84.         $html_rightsidebar = $form_values['field_rightsidebar'][0][value];
  85.         $html_body = $form_values['body'];
  86.         $html_footer = $form_values['field_footer'][0][value];
  87.         $file_name = $html_title.'.html';
  88.         $file = fopen($file_name, "w");
  89.         fwrite($file, $html_header);
  90.         fwrite($file, $html_leftsidebar);
  91.         fwrite($file, $html_rightsidebar);
  92.         fwrite($file, $html_body);
  93.         fwrite($file, $html_footer);
  94.         fclose($file);
  95.         set_time_limit(0);
  96.         $ftpserver = $form_values['ftp_server'];
  97.         $ftp_uname = $form_values['ftp_username'];
  98.         $ftp_pass = $form_values['ftp_password'];
  99.         $targetDir = 'public_html/noindex/';
  100. if (!$fp = ftp_connect($ftpserver, 21, 30))
  101. {
  102.   die('Connection failed');
  103. }
  104. if (!ftp_login($fp, $ftp_uname, $ftp_pass))
  105. {
  106.   die('Login failed');
  107. }
  108. if (!ftp_chdir($fp, $targetDir))
  109. {
  110.   die ('Unable to change directory to: ' . $targetDir);
  111. }
  112.  
  113. $fileholder = 'C:/web/xampp/htdocs/drupal-5.7/'.$file_name;
  114. $remote_file = $file_name;
  115. $filetobemoved = fopen($fileholder,"r");
  116.                                                 if(!ftp_fput($fp, $remote_file, $filetobemoved, FTP_BINARY))
  117.                                                         {
  118.                                                         $errorMessage .= "&error-unable-ftp-fput;";
  119.                                                         }                                                      
  120. /** end of custom code add */
  121. }