diff --git a/site_verify.admin.inc b/site_verify.admin.inc index 732f999..c994bcf 100644 --- a/site_verify.admin.inc +++ b/site_verify.admin.inc @@ -59,8 +59,8 @@ function site_verify_edit_form($form, &$form_state, $record = array(), $engine = $record += array( 'svid' => NULL, 'file' => '', - 'file_contents' => t('This is a verification page.'), 'meta' => '', + 'fid' => 0, 'engine' => $engine, ); $form_state['storage']['record'] = $record; @@ -121,7 +121,7 @@ function site_verify_edit_form($form, &$form_state, $record = array(), $engine = $form['file_upload'] = array( '#type' => 'file', '#title' => t('Upload an existing verification file'), - '#description' => t('If you have been provided with an actual file, you can simply upload the file.'), + '#description' => t('If you have been provided with an actual file, you can simply upload the file. Allowed files types are (html).'), '#access' => $record['engine']['file'], ); $form['file'] = array( @@ -132,18 +132,14 @@ function site_verify_edit_form($form, &$form_state, $record = array(), $engine = '#element_validate' => $record['engine']['file_validate'], '#access' => $record['engine']['file'], ); - $form['file_contents'] = array( - '#type' => 'textarea', - '#title' => t('Verification file contents'), - '#default_value' => $record['file_contents'], - '#element_validate' => $record['engine']['file_contents_validate'], - '#wysiwyg' => FALSE, - '#access' => $record['engine']['file_contents'], + $form['fid'] = array( + '#type' => 'value', + '#value' => $record['fid'], ); + if (!variable_get('clean_url', 0)) { drupal_set_message(t('Using verification files will not work if clean URLs are disabled.', array('@clean-urls' => url('admin/settings/clean-url'))), 'error', FALSE); $form['file']['#disabled'] = TRUE; - $form['file_contents']['#disabled'] = TRUE; $form['file_upload']['#disabled'] = TRUE; } @@ -174,27 +170,32 @@ function site_verify_edit_form($form, &$form_state, $record = array(), $engine = * Validation callback; save the uploaded file and check file name uniqueness. */ function site_verify_validate_file($form, &$form_state) { - $values = &$form_state['values']; - // Import the uploaded verification file. $validators = array('file_validate_extensions' => array()); - if ($file = file_save_upload('file_upload', $validators, FALSE, FILE_EXISTS_REPLACE)) { + + // Pre pair saving this file. + $destination = 'private://site_verify/'; + file_prepare_directory($destination, FILE_CREATE_DIRECTORY); + + if ($file = file_save_upload('file_upload', $validators, $destination, FILE_EXISTS_ERROR)) { $contents = @file_get_contents($file->uri); - file_delete($file); if ($contents === FALSE) { + file_delete($file); drupal_set_message(t('The verification file import failed, because the file %filename could not be read.', array('%filename' => $file->filename)), 'error'); } else { - $values['file'] = $file->filename; - $values['file_contents'] = $contents; - //drupal_set_message(t('The verification file @filename was successfully imported.', array('@filename' => $file->filename))); - } - } - - if ($values['file']) { - $existing_file = db_query("SELECT svid FROM {site_verify} WHERE LOWER(file) = LOWER(:file) AND svid <> :svid", array(':file' => $values['file'], ':svid' => $values['svid']))->fetchField(); - if ($existing_file) { - form_set_error('file', t('The file %filename is already being used in another verification.', array('%filename' => $values['file']))); + // Remove the old file... + if (isset($values['fid']) && $values['fid'] != 0) { + $old_file = file_load($values['fid']); + file_delete($old_file); + } + $form_state['values']['file'] = $file->filename; + // set the fid so it can be saved. + $form_state['values']['fid'] = $file->fid; + // Change the status + $file->status = FILE_STATUS_PERMANENT; + // Update the file status into the database + file_save($file); } } } @@ -212,10 +213,25 @@ function site_verify_edit_form_submit($form, &$form_state) { else { // Save the verification to the database. if ($form_state['values']['svid']) { - drupal_write_record('site_verify', $form_state['values'], array('svid')); + db_update('site_verify') + ->fields(array( + 'engine' => $form_state['values']['engine'], + 'fid' => $form_state['values']['fid'], + 'meta' => $form_state['values']['meta'], + 'file' => $form_state['values']['file'], + )) + ->condition('svid', $form_state['values']['svid']) + ->execute(); } else { - drupal_write_record('site_verify', $form_state['values']); + db_insert('site_verify') + ->fields(array( + 'engine' => $form_state['values']['engine'], + 'fid' => $form_state['values']['fid'], + 'meta' => $form_state['values']['meta'], + 'file' => $form_state['values']['file'], + )) + ->execute(); } drupal_set_message(t('Verification saved.')); @@ -247,6 +263,12 @@ function site_verify_delete_form($form, $form_state, $record) { function site_verify_delete_form_submit($form, &$form_state) { $record = $form_state['values']['record']; + // Remove the old file... + if (isset($record['fid']) && $record['fid'] != 0) { + $old_file = file_load($record['fid']); + file_delete($old_file); + } + db_delete('site_verify')->condition('svid', $record['svid'])->execute(); drupal_set_message(t('Verification for %engine has been deleted.', array('%engine' => $record['engine']['name']))); watchdog('site_verify', 'Verification for %engine deleted.', array('%engine' => $record['engine']['name']), WATCHDOG_NOTICE); diff --git a/site_verify.install b/site_verify.install index a1177d5..fbf6304 100644 --- a/site_verify.install +++ b/site_verify.install @@ -25,17 +25,12 @@ function site_verify_schema() { 'default' => '', 'description' => '', ), - 'file' => array( - 'type' => 'varchar', - 'length' => 255, - 'default' => '', - 'description' => '', - ), - 'file_contents' => array( - 'type' => 'text', + 'fid' => array( + 'type' => 'int', 'not null' => TRUE, - 'size' => 'big', - 'description' => '', + 'unsigned' => TRUE, + 'default' => 0, + 'description' => 'File ID', ), 'meta' => array( 'type' => 'text', @@ -128,3 +123,19 @@ function site_verify_import_ghs() { variable_del('ghs_string_verify'); module_disable(array('ghs')); } + +/** + * Add new column for file ID and drop old file_contents file. + */ +function site_verify_update_7001() { + db_drop_field('site_verify', 'file_contents'); + $field = array( + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + 'default' => 0, + 'description' => 'File ID', + ); + db_add_field('site_verify', 'fid', $field); +} + diff --git a/site_verify.module b/site_verify.module index c3669dd..9ba7ffc 100755 --- a/site_verify.module +++ b/site_verify.module @@ -83,7 +83,7 @@ function site_verify_init() { * An array of the site verification record, or FALSE if not found. */ function site_verify_load($svid) { - $record = db_query("SELECT svid, engine, file, file_contents, meta FROM {site_verify} WHERE svid = :svid", array(':svid' => $svid))->fetchAssoc(); + $record = db_query("SELECT svid, engine, file, fid, meta FROM {site_verify} WHERE svid = :svid", array(':svid' => $svid))->fetchAssoc(); if ($record) { $record['engine'] = site_verify_engine_load($record['engine']); } @@ -110,7 +110,7 @@ function site_verify_site_verify_engine_info() { $engines['google'] = array( 'name' => t('Google'), 'file' => TRUE, - 'file_contents' => TRUE, + 'fid' => 0, 'file_example' => 'google1234567890abcdef.html', 'meta' => TRUE, 'meta_example' => '', @@ -118,18 +118,19 @@ function site_verify_site_verify_engine_info() { $engines['yahoo'] = array( 'name' => t('Yahoo!'), 'file' => TRUE, - 'file_contents' => TRUE, + 'fid' => 0, 'meta' => TRUE, ); $engines['bing'] = array( 'name' => t('Bing'), 'file' => TRUE, - 'file_contents' => TRUE, + 'fid' => 0, 'meta' => TRUE, ); $engines['yandex'] = array( 'name' => t('Yandex'), 'file' => TRUE, + 'fid' => 0, 'file_example' => 'yandex_b5741169901f6c20.txt', 'meta' => TRUE, 'meta_example' => '', @@ -137,7 +138,7 @@ function site_verify_site_verify_engine_info() { $engines['custom'] = array( 'name' => t('Custom verification'), 'file' => TRUE, - 'file_contents' => TRUE, + 'fid' => 0, 'meta' => TRUE, ); return $engines; @@ -162,9 +163,9 @@ function site_verify_get_engines() { 'file' => FALSE, 'file_example' => FALSE, 'file_validate' => array(), - 'file_contents' => FALSE, 'file_contents_example' => FALSE, 'file_contents_validate' => array(), + 'fid' => 0, 'meta' => FALSE, 'meta_example' => FALSE, 'meta_validate' => array(), @@ -177,11 +178,24 @@ function site_verify_get_engines() { function site_verify_output($svid) { $verification = site_verify_load($svid); - if ($verification['file_contents'] && $verification['engine']['file_contents']) { - echo $verification['file_contents']; + if (!empty($verification['fid'])) { + if ($file = file_load($verification['fid'])) { + $contents = @file_get_contents($file->uri); + echo $contents; + } + else { + drupal_set_message(t("The verification file @file does not exitis.", array('@file' => $verification['file'])), 'error'); + watchdog('site_verify', "The verification file @file does not exitis.", array('@file' => $verification['file']), WATCHDOG_ERROR); + drupal_set_title(t('Verification page')); + return t('This is a verification page for the @title search engine.', array('@title' => $verification['file'])); + } + } + else if (!empty($verification['meta'])) { + $contents = $verification['meta']; + echo $contents; } else { drupal_set_title(t('Verification page')); - return t('This is a verification page for the @title search engine.', array('!title' => $verification['engine']['name'])); + return t('This is a verification page for the @title search engine.', array('@title' => $verification['file'])); } }