+ return $saved_array;
+}
+
+/**
+ * Save a recent search to the prefs
+ *
+ * Function arguments are different in 1.5.0
+ * @param integer $recent_index
+ * @since 1.5.0
+ * @private
+ */
+function asearch_save_recent($recent_index)
+{
+ global $recent_prefkeys, $saved_prefkeys;
+
+ $saved_array = asearch_read_saved();
+ if (isset($saved_array[$saved_prefkeys[0]])) {
+ $saved_index = count($saved_array[$saved_prefkeys[0]]);
+ } else {
+ $saved_index = 0;
+ }
+ $recent_array = asearch_read_recent();
+ $n = 0;
+ foreach ($recent_prefkeys as $key) {
+ $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
+ if (!empty($recent_slice[0]))
+ asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]);
+ else
+ asearch_removePref($saved_prefkeys[$n], $saved_index);
+ $n++;
+ }
+}
+
+/**
+ * Write a recent search to prefs
+ *
+ * Function arguments are different in 1.5.0
+ * @param array $recent_array
+ * @since 1.5.0
+ * @private
+ */
+function asearch_write_recent(&$recent_array)
+{
+ global $recent_prefkeys, $search_memory;
+
+ $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]]));
+ for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
+ foreach ($recent_prefkeys as $key) {
+ asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]);
+ }
+ }
+ for (; $recent_num < $search_memory; $recent_num++) {
+ foreach ($recent_prefkeys as $key) {
+ asearch_removePref($key, $recent_num);
+ }
+ }
+}
+
+/**
+ * Remove a recent search from prefs
+ *
+ * Function arguments are different in 1.5.0
+ * @param integer $forget_index removed search number
+ * @since 1.5.0
+ * @private
+ */
+function asearch_forget_recent($forget_index)
+{
+ global $recent_prefkeys;
+
+ $recent_array = asearch_read_recent();
+ foreach ($recent_prefkeys as $key) {
+ array_splice($recent_array[$key], $forget_index, 1);
+ }
+ asearch_write_recent($recent_array);
+}
+
+/**
+ * Find a recent search in the prefs (used to avoid saving duplicates)
+ * @param array $recent_array
+ * @param array $mailbox_array
+ * @param array $biop_array
+ * @param array $unop_array
+ * @param array $where_array
+ * @param array $what_array
+ * @param array $exclude_array
+ * @param array $sub_array
+ * @return integer
+ * @since 1.5.0
+ * @private
+ */
+function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
+{
+ global $recent_prefkeys, $search_advanced;
+
+ $where_string = asearch_serialize($where_array);
+ $mailbox_string = asearch_serialize($mailbox_array);
+ $what_string = asearch_serialize($what_array);
+ $unop_string = asearch_serialize($unop_array);
+ if ($search_advanced) {
+ $biop_string = asearch_serialize($biop_array);
+ $exclude_string = asearch_serialize($exclude_array);
+ $sub_string = asearch_serialize($sub_array);
+ }
+ $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE]]);
+ for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
+ if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num])) {
+ if (
+ $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num] &&
+ $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX]][$recent_num] &&
+ $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT]][$recent_num] &&
+ $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP]][$recent_num] &&
+ ((!$search_advanced) ||
+ ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP]][$recent_num] &&
+ $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE]][$recent_num] &&
+ $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB]][$recent_num]))
+ )
+ return $recent_num;
+ }
+ }
+ return -1;
+}
+
+/**
+ * Push a recent search into the prefs
+ * @param array $recent_array
+ * @param array $mailbox_array
+ * @param array $biop_array
+ * @param array $unop_array
+ * @param array $where_array
+ * @param array $what_array
+ * @param array $exclude_array
+ * @param array $sub_array
+ * @since 1.5.0
+ * @private
+ */
+function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
+{
+ global $recent_prefkeys, $search_memory;
+ //global $what; // Hack to access issued search from read_body.php
+ $what = 1;
+ /**
+ * Update search history and store it in the session so we can retrieve the
+ * issued search when returning from an external page like read_body.php
+ */
+ $criteria[$what] = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ sqsession_register($criteria, ASEARCH_CRITERIA);
+ if ($search_memory > 0) {
+ $recent_array = asearch_read_recent();
+ $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
+ if ($recent_found >= 0) { // Remove identical recent
+ foreach ($recent_prefkeys as $key) {
+ array_splice($recent_array[$key], $recent_found, 1);
+ }
+ }
+ $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array);