diff options
Diffstat (limited to 'textbook_companion.module')
-rwxr-xr-x | textbook_companion.module | 292 |
1 files changed, 292 insertions, 0 deletions
diff --git a/textbook_companion.module b/textbook_companion.module index 97ff309..5f91764 100755 --- a/textbook_companion.module +++ b/textbook_companion.module @@ -848,6 +848,298 @@ function _tbc_sentence_case($string) } return $string; } +/*************************** VALIDATION FUNCTIONS *****************************/ +function textbook_companion_check_valid_filename($file_name) +{ + if (!preg_match('/^[0-9a-zA-Z\_\.]+$/', $file_name)) + return FALSE; + else if (substr_count($file_name, ".") > 1) + return FALSE; + else + return TRUE; +} +function check_name($name = '') +{ + if (!preg_match('/^[0-9a-zA-Z\ ]+$/', $name)) + return FALSE; + else + return TRUE; +} +function check_chapter_number($name = '') +{ + if (!preg_match('/^([0-9])+(\.([0-9a-zA-Z])+)+$/', $name)) + return FALSE; + else + return TRUE; +} +function textbook_companion_path() +{ + return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'uploads/'; +} +function textbook_companion_samplecode_path() +{ + return $_SERVER['DOCUMENT_ROOT'] . base_path() . 'tbc_sample_code/'; +} +/****************************** DELETION FUNCTIONS ****************************/ +function delete_example($example_id) +{ + global $user; + $root_path = textbook_companion_path(); + $status = TRUE; + /*$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d", $example_id); + $example_data = db_fetch_object($example_q);*/ + $query = db_select('textbook_companion_example'); + $query->fields('textbook_companion_example'); + $query->condition('id', $example_id); + $example_q = $query->execute(); + $example_data = $example_q->fetchObject(); + if (!$example_data) + { + drupal_set_message(t('Invalid example.'), 'error'); + return FALSE; + } //!$example_data + /*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id); + $chapter_data = db_fetch_object($chapter_q);*/ + $query = db_select('textbook_companion_chapter'); + $query->fields('textbook_companion_chapter'); + $query->condition('id', $example_data->chapter_id); + $chapter_q = $query->execute(); + $chapter_data = $chapter_q->fetchObject(); + if (!$chapter_data) + { + drupal_set_message(t('Invalid example chapter.'), 'error'); + return FALSE; + } //!$chapter_data + /* deleting example files */ + /*$examples_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_id);*/ + $query = db_select('textbook_companion_example_files'); + $query->fields('textbook_companion_example_files'); + $query->condition('example_id', $example_id); + $examples_files_q = $query->execute(); + while ($examples_files_data = $examples_files_q->fetchObject()) + { + if (!file_exists($root_path . $examples_files_data->filepath)) + { + $status = FALSE; + drupal_set_message(t('Error deleting !file. File does not exists.', array( + '!file' => $examples_files_data->filepath + )), 'error'); + continue; + } //!file_exists($root_path . $examples_files_data->filepath) + /* removing example file */ + if (!drupal_unlink($root_path . $examples_files_data->filepath)) + { + $status = FALSE; + drupal_set_message(t('Error deleting !file', array( + '!file' => $examples_files_data->filepath + )), 'error'); + /* sending email to admins */ + $email_to = variable_get('textbook_companion_emails', ''); + $params['standard']['subject'] = "[ERROR] Error deleting example file"; + $params['standard']['body'] = array( + 0 => "Error deleting example files by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " : + example id : " . $example_id . " + file id : " . $examples_files_data->id . " + file path : " . $examples_files_data->filepath + ); + if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $params, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + } //!drupal_unlink($root_path . $examples_files_data->filepath) + else + { + /* deleting example files database entries */ + /*db_query("DELETE FROM {textbook_companion_example_files} WHERE id = %d", $examples_files_data->id);*/ + $query = db_delete('textbook_companion_example_files'); + $query->condition('id', $examples_files_data->id); + $num_deleted = $query->execute(); + } + } //$examples_files_data = $examples_files_q->fetchObject() + if (!$status) + return FALSE; + /* removing example folder */ + $ex_path = $chapter_data->preference_id . '/' . 'CH' . $chapter_data->number . '/' . 'EX' . $example_data->number; + $dir_path = $root_path . $ex_path; + if (is_dir($dir_path)) + { + if (!drupal_rmdir($dir_path)) + { + drupal_set_message(t('Error deleting folder !folder', array( + '!folder' => $dir_path + )), 'error'); + /* sending email to admins */ + $email_to = variable_get('textbook_companion_emails', ''); + $params['standard']['subject'] = "[ERROR] Error deleting folder"; + $params['standard']['body'] = array( + 0 => "Error deleting folder " . $dir_path . " by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] + ); + if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $params, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + return FALSE; + } //!drupal_rmdir($dir_path) + } //is_dir($dir_path) + else + { + drupal_set_message(t('Cannot delete example folder. !folder does not exists.', array( + '!folder' => $dir_path + )), 'error'); + return FALSE; + } + /* deleting example dependency and exmaple database entries */ + /*db_query("DELETE FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_id);*/ + $query = db_delete('textbook_companion_example_dependency'); + $query->condition('example_id', $example_id); + $num_deleted = $query->execute(); + /*db_query("DELETE FROM {textbook_companion_example} WHERE id = %d", $example_id);*/ + $query = db_delete('textbook_companion_example'); + $query->condition('id', $example_id); + $num_deleted = $query->execute(); + return $status; +} +function delete_chapter($chapter_id) +{ + $status = TRUE; + $root_path = textbook_companion_path(); + /*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id); + $chapter_data = db_fetch_object($chapter_q);*/ + $query = db_select('textbook_companion_chapter'); + $query->fields('textbook_companion_chapter'); + $query->condition('id', $chapter_id); + $chapter_q = $query->execute(); + $chapter_data = $chapter_q->fetchObject(); + if (!$chapter_data) + { + drupal_set_message('Invalid chapter.', 'error'); + return FALSE; + } //!$chapter_data + /* deleting examples */ + /*$example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d", $chapter_id);*/ + $query = db_select('textbook_companion_example'); + $query->fields('textbook_companion_example'); + $query->condition('chapter_id', $chapter_id); + $example_q = $query->execute(); + while ($example_data = $example_q->fetchObject()) + { + if (!delete_example($example_data->id)) + $status = FALSE; + } //$example_data = $example_q->fetchObject() + if ($status) + { + $dir_path = $root_path . $chapter_data->preference_id . '/CH' . $chapter_data->number; + if (is_dir($dir_path)) + { + $res = rmdir($dir_path); + if (!$res) + { + drupal_set_message(t('Error deleting chapter folder !folder', array( + '!folder' => $dir_path + )), 'error'); + /* sending email to admins */ + $email_to = variable_get('textbook_companion_emails', ''); + $params['standard']['subject'] = "[ERROR] Error deleting folder"; + $params['standard']['body'] = "Error deleting folder " . $dir_path; + if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $params, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + return FALSE; + } //!$res + else + { + /* deleting chapter details from database */ + /*db_query("DELETE FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id);*/ + $query = db_delete('textbook_companion_chapter'); + $query->condition('id', $chapter_id); + $num_deleted = $query->execute(); + return TRUE; + } + } //is_dir($dir_path) + else + { + drupal_set_message(t('Cannot delete chapter folder. !folder does not exists.', array( + '!folder' => $dir_path + )), 'error'); + return FALSE; + } + } //$status + return FALSE; +} +function delete_book($book_id) +{ + $status = TRUE; + $root_path = textbook_companion_path(); + /*$preference_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $book_id); + $preference_data = db_fetch_object($preference_q);*/ + $query = db_select('textbook_companion_preference'); + $query->fields('textbook_companion_preference'); + $query->condition('id', $book_id); + $preference_q = $query->execute(); + $preference_data = $preference_q->fetchObject(); + if (!$preference_data) + { + drupal_set_message('Invalid book.', 'error'); + return FALSE; + } //!$preference_data + /* delete chapters */ + /*$chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $preference_data->id);*/ + $query = db_select('textbook_companion_chapter'); + $query->fields('textbook_companion_chapter'); + $query->condition('preference_id', $preference_data->id); + $chapter_q = $query->execute(); + while ($chapter_data = $chapter_q->fetchObject()) + { + if (!delete_chapter($chapter_data->id)) + { + $status = FALSE; + } //!delete_chapter($chapter_data->id) + } //$chapter_data = $chapter_q->fetchObject() + return $status; +} +function delete_file($file_id) +{ + $root_path = textbook_companion_path(); + /*$file_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE id = %d LIMIT 1", $file_id);*/ + $query = db_select('textbook_companion_example_files'); + $query->fields('textbook_companion_example_files'); + $query->condition('id', $file_id); + $query->range(0, 1); + $file_q = $query->execute(); + $file_data = $file_q->fetchObject(); + if (!$file_data) + { + drupal_set_message('Invalid file specified.', 'error'); + return FALSE; + } //!$file_data + if (!file_exists($root_path . $file_data->filepath)) + { + drupal_set_message(t('Error deleting !file. File does not exists.', array( + '!file' => $file_data->filepath + )), 'error'); + return FALSE; + } //!file_exists($root_path . $file_data->filepath) + /* removing example file */ + if (!unlink($root_path . $file_data->filepath)) + { + drupal_set_message(t('Error deleting !file', array( + '!file' => $file_data->filepath + )), 'error'); + /* sending email to admins */ + $email_to = variable_get('textbook_companion_emails', ''); + $params['standard']['subject'] = "[ERROR] Error deleting file"; + $params['standard']['body'] = "Error deleting file by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " : + file id : " . $file_id . " + file path : " . $file_data->filepath; + if (!drupal_mail('textbook_companion', 'standard', $email_to, language_default(), $params, variable_get('textbook_companion_from_email', NULL), TRUE)) + drupal_set_message('Error sending email message.', 'error'); + return FALSE; + } //!unlink($root_path . $file_data->filepath) + else + { + /* deleting example files database entries */ + /*db_query("DELETE FROM {textbook_companion_example_files} WHERE id = %d", $file_id);*/ + $query = db_delete('textbook_companion_example_files'); + $query->condition('id', $file_id); + $num_deleted = $query->execute(); + return TRUE; + } +} function textbook_companion_init() { $path = drupal_get_path('module', 'textbook_companion'); |