DMail Milestone 1.0
Drupal Mail Client
dmail.db.inc
Go to the documentation of this file.
00001 <?php
00002 // $Id: 44be90cadef7f8677fe1a5d0dfb99aea64e0ab38 $
00008 /******************************************************************************
00009  * Module database functions
00010  */
00011 
00021 function mfn_dmail_db_identities_select($user_id = NULL) {
00022   global $user;
00023   $ns = array('db', 'identities', 'full');
00024   $cmode = DCACHE_DRUPAL;
00025   $cache = dcache_get($ns, $cmode);
00026   if (isset($cache['rows']) && !dcache_expired($cache)) {
00027     return $cache['rows'];
00028   }
00029   if ($user_id === NULL) {
00030     $user_id = $user->uid;
00031   }
00032   $sql = "SELECT * FROM `{dmail_identities}` WHERE `user_id` = %d";
00033   $result = db_query($sql, $user_id);
00034   $rows = array();
00035   while ($row = db_fetch_object($result)) {
00036     $rows[] = $row;
00037   }
00038   dcache_set($ns, array('rows' => $rows), $cmode);
00039   return $rows;
00040 }
00041 
00048 function mfn_dmail_db_identities_with_counts() {
00049   global $user;
00050   $ns = array('db', 'identities', 'counts');
00051   $cmode = DCACHE_DRUPAL;
00052   $cache = dcache_get($ns, $cmode);
00053   if (isset($cache['rows']) && !dcache_expired($cache)) {
00054     return $cache['rows'];
00055   }
00056   $select = "
00057     SELECT 
00058       `dmail_identities`.`id` AS 'id', 
00059       `dmail_identities`.`name` AS 'name', 
00060       `dmail_identities`.`lastchkd` AS 'lastchkd',
00061       `dmail_identities`.`inbox` AS 'inbox',
00062       `dmail_identities`.`messages` AS 'messages',
00063       `dmail_identities`.`recent` AS 'recent'
00064     FROM
00065       `{dmail_identities}` AS `dmail_identities`
00066     WHERE
00067       `dmail_identities`.`user_id` = %d
00068     ORDER BY
00069       `dmail_identities`.`id`
00070   ";
00071   $resource = db_query($select, $user->uid);
00072   $return = array();
00073   while ($row = db_fetch_object($resource)) {
00074     $return[] = $row;
00075   }
00076   dcache_set($ns, array('rows' => $return), $cmode);
00077   return $return;
00078 }
00079 
00096 function mfn_dmail_db_identity_update($id, $values) {
00097   global $user;
00098   $identity = mfn_dmail_db_identity_select($id);
00099   $sql = "UPDATE {dmail_identities} SET `name` = '%s', `host` = '%s', `service` = '%s', `port` = %d, `inbox` = '%s', `delimiter` = '%s', `encryption` = '%s', `vld8cert` = %d, `expunge` = %d, `readonly` = %d, `movedel` = %d, `user` = '%s', `pass` = '%s', `smtpport` = %d, `sanitizedeleted` = %d, `deletefolder` = '%s', `draftfolder` = '%s', `sentfolder` = '%s', `junkfolder` = '%s', `toplevel` = %d WHERE id = %d";
00100   $ret[] = db_query(
00101     $sql,
00102     $values['name'],
00103     $values['host'],
00104     $values['service'],
00105     $values['port'],
00106     $values['inbox'],
00107     $values['delimiter'],
00108     $values['encrypt'],
00109     $values['validate_cert'] == 'yes',
00110     $values['expunge'] == 'yes',
00111     $values['readonly'] == 'yes',
00112     $values['move_deleted'] == 'yes',
00113     $values['user'],
00114     mfn_dmail_encrypt($values['pass']),
00115     $values['smtp_port'],
00116     $values['sanitize_deleted'] == 'yes',
00117     $values['delete_folder'],
00118     $values['draft_folder'],
00119     $values['sent_folder'],
00120     $values['junk_folder'],
00121     $values['toplevel'],
00122     $id
00123   );
00124   if ($values['inbox'] !== $identity->inbox) {
00125     $inbox = mfn_dmail_db_mbox_select_by_name($id, $identity->inbox);
00126     $sql = "UPDATE {dmail_mboxes} SET `name` = '%s' WHERE `id` = %d";
00127     $ret[] = db_query($sql, $values['inbox'], $inbox->id);
00128     dcache_del(array('db', 'mboxes'), DCACHE_ALL);
00129   }
00130   dcache_del(array('db', 'identities'), DCACHE_ALL);
00131   return $ret;
00132 }
00133 
00149 function mfn_dmail_db_identity_update_counts($id, $messages, $recent) {
00150   $sql = "UPDATE {dmail_identities} SET `messages` = %d, `recent` = %d WHERE id = %d";
00151   $identity = mfn_dmail_db_identity_select($id);
00152   $messages = ($messages < 0) ? 0 : $messages;
00153   $recent = ($recent < 0) ? 0 : $recent;
00154   if ($identity->messages != $messages ||
00155       $identity->recent != $recent) {
00156     dcache_del(array('db', 'identities'), DCACHE_ALL);
00157     return db_query($sql, $messages, $recent, $id);
00158   }
00159 }
00160 
00173 function mfn_dmail_db_identity_add($values) {
00174   global $user;
00175   $sql = "INSERT INTO {dmail_identities} (`user_id`, `name`, `created`, `lastchkd`, `host`, `service`, `port`, `inbox`, `delimiter`, `encryption`, `vld8cert`, `expunge`, `readonly`, `movedel`, `user`, `pass`, `smtpport`, `sanitizedeleted`, `deletefolder`, `draftfolder`, `sentfolder`, `junkfolder`, `toplevel`, `messages`, `recent`) VALUES (%d, '%s', NOW(), '0000-00-00 00:00:00', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', %d, %d, %d )";
00176   $ret[] = db_query(
00177     $sql,
00178     $user->uid,
00179     $values['name'],
00180     $values['host'],
00181     $values['service'],
00182     $values['port'],
00183     $values['inbox'],
00184     $values['delimiter'],
00185     $values['encrypt'],
00186     $values['validate_cert'] == 'yes',
00187     $values['expunge'] == 'yes',
00188     $values['readonly'] == 'yes',
00189     $values['move_deleted'] == 'yes',
00190     $values['user'],
00191     mfn_dmail_encrypt($values['pass']),
00192     $values['smtp_port'],
00193     $values['sanitize_deleted'] == 'yes',
00194     $values['delete_folder'],
00195     $values['draft_folder'],
00196     $values['sent_folder'],
00197     $values['junk_folder'],
00198     $values['toplevel'],
00199     0,
00200     0
00201   );
00202   $identity_id = db_last_insert_id('dmail_identities', 'id');
00203   $ret[] = $identity_id;
00204   if ($values['inbox']) {
00205     $sql = "INSERT INTO {dmail_mboxes} (`identity_id`, `name`, `created`) VALUES (%d, '%s', NOW())";
00206     $ret[] = db_query($sql, $identity_id, $values['inbox']);
00207   }
00208   dcache_del(array('db', 'identities'), DCACHE_ALL);
00209   dcache_del(array('db', 'mboxes'), DCACHE_ALL);
00210   return $ret;
00211 }
00212 
00225 function mfn_dmail_db_identity_delete($id) {
00226   $sql = "DELETE FROM `{dmail_identities}` WHERE `id` = %d";
00227   dcache_del(array('db', 'identities'), DCACHE_ALL);
00228   return db_query($sql, $id);
00229 }
00230 
00240 function mfn_dmail_db_identity_select($id) {
00241   global $user;
00242   $ns = array('db', 'identities', 'single');
00243   $cache = dcache_get($ns);
00244   if (isset($cache['row']) &&
00245       isset($cache['id']) &&
00246       $cache['id'] == $id &&
00247       !dcache_expired($cache)
00248      ) {
00249     return $cache['row'];
00250   }
00251   $sql = "SELECT * FROM `{dmail_identities}` WHERE `id` = %d and `user_id` = %d";
00252   $return = db_fetch_object(db_query($sql, $id, $user->uid));
00253   dcache_set($ns, array('id' => $id, 'row' => $return));
00254   return $return;
00255 }
00256 
00269 function mfn_dmail_db_identity_update_lastchkd($id, $lastchkd) {
00270   $sql = "UPDATE `{dmail_identities}` SET `lastchkd` = '%s' WHERE `id` = %d";
00271   dcache_del(array('db', 'identities'), DCACHE_ALL);
00272   return db_query($sql, $lastchkd, $id);
00273 }
00274 
00287 function mfn_dmail_db_mbox_select_by_name($identity_id, $mbox) {
00288   $ns = array('db', 'mboxes', 'by_name');
00289   $cache = dcache_get($ns);
00290   if (isset($cache['row']) && 
00291       $cache['identity_id'] == $identity_id &&
00292       $cache['mbox_name'] == $mbox &&
00293       !dcache_expired($cache)
00294      ) {
00295     return $cache['row'];
00296   }
00297   $sql = "SELECT * FROM `{dmail_mboxes}` WHERE `identity_id` = %d and `name` = '%s'";
00298   $row = db_fetch_object(db_query($sql, $identity_id, $mbox));
00299   dcache_set($ns, array('identity_id' => $row->identity_id, 'mbox_name' => $row->name, 'row' => $row));
00300   return $row;
00301 }
00302 
00312 function mfn_dmail_db_mbox_select($id) {
00313   $ns = array('db', 'mboxes', 'single');
00314   $cache = dcache_get($ns);
00315   if (isset($cache['row']) &&
00316       $cache['id'] == $id &&
00317       !dcache_expired($cache)
00318      ) {
00319     return $cache['row'];
00320   }
00321   $sql = "SELECT * FROM `{dmail_mboxes}` WHERE `id` = %d";
00322   $row = db_fetch_object(db_query($sql, $id));
00323   dcache_set($ns, array('id' => $id, 'row' => $row));
00324   return $row;
00325 }
00326 
00336 function mfn_dmail_db_mbox_delete($id) {
00337   $sql = "DELETE FROM `{dmail_mboxes}` WHERE `id` = %d";
00338   $mbox = mfn_dmail_db_mbox_select($id);
00339   $identity = mfn_dmail_db_identity_select($mbox->identity_id);
00340   $identity->messages -= $mbox->messages;
00341   $identity->recent -= $mbox->recent;
00342   mfn_dmail_db_identity_update_counts($identity->id, $identity->messages, $identity->recent);
00343   dcache_del(array('db', 'mboxes'), DCACHE_ALL);
00344   return db_query($sql, $id);
00345 }
00346 
00356 function mfn_dmail_db_mboxes_select($identity_id) {
00357   $ns = array('db', 'mboxes', 'full');
00358   $cmode = DCACHE_DRUPAL;
00359   $cache = dcache_get($ns, $cmode);
00360   if (isset($cache['rows']) &&
00361       $cache['identity_id'] == $identity_id &&
00362       !dcache_expired($cache)
00363      ) {
00364     return $cache['rows'];
00365   }
00366   $sql = "SELECT * FROM `{dmail_mboxes}` WHERE `identity_id` = %d ORDER BY `name`";
00367   $result = db_query($sql, $identity_id);
00368   $rows = array();
00369   while ($row = db_fetch_object($result)) {
00370     $rows[] = $row;
00371   }
00372   dcache_set($ns, array('identity_id' => $identity_id, 'rows' => $rows), $cmode);
00373   return $rows;
00374 }
00375 
00388 function mfn_dmail_db_mboxes_add($identity, $mbox_data) {
00389   if ($mbox_data->no_server_in_name) {
00390     $name = $mbox_data->name;
00391   }
00392   else {
00393     list(, $name) = explode('}', $mbox_data->name);
00394   }
00395   $sql = "INSERT INTO `{dmail_mboxes}` (`identity_id`, `name`, `created`, `lastchkd`, `attributes`, `flags`, `messages`, `recent`, `unseen`) VALUES (%d, '%s', NOW(), NOW(), %d, %d, %d, %d, %d)";
00396   $result = db_query($sql, $identity->id, $name, $mbox_data->attributes, $mbox_data->flags, $mbox_data->messages, $mbox_data->recent, $mbox_data->unseen);
00397   if ($result) {
00398     $id = db_last_insert_id('dmail_mboxes', 'id');
00399   }
00400   else {
00401     $id = FALSE;
00402   }
00403   if ($id !== FALSE) {
00404     $identity->messages += $mbox_data->messages;
00405     $identity->recent += $mbox_data->recent;
00406     mfn_dmail_db_identity_update_counts($identity->id, $identity->messages, $identity->recent);
00407   }
00408   dcache_del(array('db', 'mboxes'), DCACHE_ALL);
00409   return $id;
00410 }
00411 
00424 function mfn_dmail_db_mboxes_update($id, $mbox_data) {
00425   $sql = "UPDATE `{dmail_mboxes}` SET `attributes` = %d, `flags` = %d, `messages` = %d, `recent` = %d, `unseen` = %d, `lastchkd` = '%s' WHERE `id` = %d";
00426   $mbox = mfn_dmail_db_mbox_select($id);
00427   if ($mbox->messages != $mbox_data->messages &&
00428       $mbox->recent != $mbox_data->recent &&
00429       $mbox->unseen != $mbox_data->unseen &&
00430       $mbox->attributes != $mbox_data->attributes &&
00431       $mbox->flags != $mbox_data->flags) {
00432     $identity = mfn_dmail_db_identity_select($mbox->identity_id);
00433     if ($mbox->messages != $mbox_data->messages) {
00434       $identity->messages = ((($identity->messages -= $mbox->messages) < 0) ? 0 : $identity->messages) + $mbox_data->messages;
00435     }
00436     if ($mbox->recent != $mbox_data->recent) {
00437       $identity->recent = ((($identity->recent -= $mbox->recent) < 0) ? 0 : $identity->recent) + $mbox_data->recent;
00438     }
00439     $ret = db_query($sql, $mbox_data->attributes, $mbox_data->flags, $mbox_data->messages, $mbox_data->recent, $mbox_data->unseen, $mbox_data->lastchkd, $id);
00440     mfn_dmail_db_identity_update_counts($identity->id, $identity->messages, $identity->recent);
00441     dcache_del(array('db', 'mboxes'), DCACHE_ALL);
00442   }
00443   return $ret;
00444 }
00445 
00456 function mfn_dmail_db_message_header_select($msgid) {
00457   $ns = array('db', 'message_headers', 'single');
00458   $cmode = DCACHE_DRUPAL;
00459   $cache = dcache_get($ns, $cmode);
00460   if ($cache['row'] && $cache['row']->msgid == $msgid && !dcache_expired($cache)) {
00461     return $cache['row'];
00462   }
00463   $sql = "SELECT * from `{dmail_message_headers}` WHERE `msgid` = '%s'";
00464   $cache['row'] = db_fetch_object(db_query($sql, $msgid));
00465   $cache['row'] = $cache['row'] === NULL ? FALSE : $cache['row'];
00466   $row =& $cache['row'];
00467   if ($row->re) {
00468     $row->subject = "Re: {$row->subject}";
00469   }
00470   dcache_set($ns, $cache, $cmode);
00471   return $cache['row'];
00472 }
00473 
00483 function mfn_dmail_db_message_header_add($message_header) {
00484   $mmh =& $message_header;
00485   $mh = mfn_dmail_db_message_header_select($mmh->msgid);
00486   if ($mh !== FALSE) {
00487     return FALSE;
00488   }
00489   $mmh->date = mfn_dmail_datetime($mmh->date);
00490   $sql = "INSERT INTO `{dmail_message_headers}` (`msgid`, `subject`, `re`, `from`, `to`, `date`, `message_id`, `size`, `created`, `updated`) VALUES ('%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
00491   $s = mfn_dmail_subject_re($mmh->subject);
00492   $mmh->subject = $s->subject;
00493   $mmh->re = $s->re;
00494   $result = db_query($sql, $mmh->msgid, $mmh->subject, $mmh->re, $mmh->from, $mmh->to, $mmh->date, $mmh->message_id, $mmh->size);
00495   dcache_del(array('db', 'message_header'), DCACHE_ALL);
00496   return $result;
00497 }
00498 
00509 function mfn_dmail_db_message_header_update($message_header) {
00510   $mmh =& $message_header;
00511   $sql = "UPDATE `{dmail_message_headers}` SET `subject` = '%s', `re` = %d, `from` = '%s', `to` = '%s', `date` = '%s', `message_id` = '%s', `size` = %d, `updated` = NOW() WHERE `msgid` = '%s'";
00512   $mh = mfn_dmail_db_message_header_select($message_header->msgid);
00513   if ($mh === FALSE) {
00514     return FALSE;
00515   }
00516   $result = NULL;
00517   $mmh->date = mfn_dmail_datetime($mmh->date);
00518   if ($mh->subject != $mmh->subject ||
00519       $mh->re != $mmh->re ||
00520       $mh->from != $mmh->from ||
00521       $mh->to != $mmh->to ||
00522       $mh->date != $mmh->date ||
00523       $mh->message_id != $mmh->message_id ||
00524       $mh->size != $mmh->size) {
00525     $s = mfn_dmail_subject_re($mmh->subject);
00526     $mmh->subject = $s->subject;
00527     $mmh->re = $s->re;
00528     $result = db_query($sql, $mmh->subject, $mmh->re, $mmh->from, $mmh->to, $mmh->date, $mmh->message->id, $mmh->size, $mmh->msgid);
00529     dcache_del(array('db', 'message_header'), DCACHE_ALL);
00530   }
00531   return $result;
00532 }
00533 
00549 function mfn_dmail_db_mbox_message_header_select($mbox_id, $msgid = NULL) {
00550   $ns = array('db', 'mbox_message_headers', 'single');
00551   $cmode = DCACHE_DRUPAL;
00552   $cache = dcache_get($ns, $cmode);
00553   if ($msgid === NULL) {
00554     if ($cache['row'] && $cache['row']->id == $mbox_id && !dcache_expired($cache)) {
00555       return $cache['row'];
00556     }
00557     $sql = "SELECT * from `{dmail_mbox_message_headers}` WHERE `id` = %d";
00558     $mbox_header = db_fetch_object(db_query($sql, $mbox_id));
00559     if ($mbox_header !== FALSE) {
00560       $header = mfn_dmail_db_message_header_select($mbox_header->msgid);
00561       foreach((array)$header as $key => $param) {
00562         if (!isset($mbox_header->$key)) {
00563           $mbox_header->$key = $header->$key;
00564         }
00565       }
00566       $cache['row'] = $mbox_header;
00567     }
00568     else {
00569       $cache['row'] = FALSE;
00570     }
00571   }
00572   else {
00573     if ($cache['row'] && $cache['row']->mbox_id == $mbox_id && $cache['row']->msgid == $msgid && !dcache_expired($cache)) {
00574       return $cache['row'];
00575     }
00576     $sql = "SELECT * from `{dmail_mbox_message_headers}` WHERE `mbox_id` = %d AND `msgid` = '%s'";
00577     $cache['row'] = db_fetch_object(db_query($sql, $mbox_id, $msgid));
00578   }
00579   $row =& $cache['row'];
00580   $row = $row === NULL ? FALSE : $row;
00581   dcache_set($ns, $cache, $cmode);
00582   return $row;
00583 }
00584 
00591 function mfn_dmail_db_mbox_message_headers_select_ordered($mbox_id) {
00592   global $pager_page_array, $pager_total, $pager_total_items;
00593   $sortdir =& $_GET['sort'];
00594   $sortcol =& $_GET['order'];
00595   $ns = array('db', 'mbox_message_headers', 'ordered');
00596   $cmode = DCACHE_DRUPAL;
00597   $cache = dcache_get($ns, $cmode);
00598   if (isset($cache['rows']) &&
00599       $cache['mbox_id'] == $mbox_id &&
00600       $cache['sortcol'] == $sortcol &&
00601       $cache['sortdir'] == $sortdir &&
00602       !dcache_expired($cache)
00603      ) {
00604     $pager_page_array = $cache['pager_page_array'];
00605     $pager_total = $cache['pager_total'];
00606     $pager_total_items = $cache['pager_total_items'];
00607     return $cache['rows'];
00608   }
00609   $sql = "SELECT mmh.`id` AS \"id\",
00610                  mmh.`mbox_id` AS \"mbox_id\",
00611                  mmh.`msgid` AS \"msgid\",
00612                  mh.`from` AS \"from\",
00613                  mh.`to` AS \"to\",
00614                  mh.`subject` AS \"subject\",
00615                  mh.`re` AS \"re\",
00616                  mh.`date` AS \"date\",
00617                  mh.`size` AS \"size\",
00618                  mh.`message_id` AS \"message_id\",
00619                  mh.`created` AS \"header_created\",
00620                  mh.`updated` AS \"header_updated\",
00621                  mmh.`uid` AS \"uid\",
00622                  mmh.`msgno` AS \"msgno\",
00623                  mmh.`recent` AS \"recent\",
00624                  mmh.`flagged` AS \"flagged\",
00625                  mmh.`answered` AS \"answered\",
00626                  mmh.`deleted` AS \"deleted\",
00627                  mmh.`seen` AS \"seen\",
00628                  mmh.`draft` AS \"draft\",
00629                  mmh.`display` AS \"display\",
00630                  mmh.`created` AS \"mbox_header_created\",
00631                  mmh.`updated` as \"mbox_header_updated\"
00632           FROM `{dmail_mbox_message_headers}` AS mmh
00633           JOIN `{dmail_message_headers}` AS mh ON mh.`msgid` = mmh.`msgid`
00634           WHERE mmh.`mbox_id` = %d
00635          ";
00636   $sortby = $sortcol;
00637   foreach (mfn_dmail_items_list_header() as $data) {
00638     if ($data['data'] == $sortcol) {
00639       $sortby = $data['field'];
00640       break;
00641     }
00642   }
00643   $sql .= " ORDER BY `{$sortby}` {$sortdir}, `date` ASC ";
00644   $result = db_query($sql, $mbox_id);
00645   $rows = array();
00646   while ($row = db_fetch_object($result)) {
00647     $row->current_id = count($rows);
00648     $row->prev_id = $row->current_id ? $row->current_id - 1 : NULL;
00649     $row->next_id = $row->current_id + 1;
00650     $row->subject = $row->re ? "Re: {$row->subject}" : $row->subject;
00651     $rows[] = $row;
00652   }
00653   if (count($rows)) {
00654     $rows[count($rows) - 1]->next_id = NULL;
00655   }
00656   dcache_set($ns, array(
00657     'mbox_id' => $mbox_id,
00658     'sortdir' => $sortdir,
00659     'sortcol' => $sortcol,
00660     'rows' => $rows,
00661     'pager_page_array' => $pager_page_array,
00662     'pager_total' => $pager_total,
00663     'pager_total_items' => $pager_total_items,
00664   ), $cmode);
00665   return $rows;
00666 }
00667 
00677 function mfn_dmail_db_mbox_message_header_add($mbox_message_header) {
00678   $mmh =& $mbox_message_header;
00679   $mh = mfn_dmail_db_mbox_message_header_select($mmh->mbox_id, $mmh->msgid);
00680   if ($mh !== FALSE) {
00681     return FALSE;
00682   }
00683   $sql = "INSERT INTO `{dmail_mbox_message_headers}` (`mbox_id`, `msgid`, `uid`, `msgno`, `recent`, `flagged`, `answered`, `deleted`, `seen`, `draft`, `display`, `created`, `updated`) VALUES (%d, '%s', %d, %d, %d, %d, %d, %d, %d, %d, %d, NOW(), NOW())";
00684   $result = db_query($sql, $mmh->mbox_id, $mmh->msgid, $mmh->uid, $mmh->msgno, $mmh->recent, $mmh->flagged, $mmh->answered, $mmh->deleted, $mmh->seen, $mmh->draft, $mmh->display);
00685   dcache_del(array('db', 'mbox_message_headers'), DCACHE_ALL);
00686   return $result;
00687 }
00688 
00698 function mfn_dmail_db_mbox_message_header_update($mbox_message_header) {
00699   $mmh =& $mbox_message_header;
00700   $mh = mfn_dmail_db_mbox_message_header_select($mmh->mbox_id, $mmh->msgid);
00701   if ($mh === FALSE) {
00702     return FALSE;
00703   }
00704   $result = NULL;
00705   if ($mh->uid != $mmh->uid ||
00706       $mh->msgno != $mmh->msgno ||
00707       $mh->recent != $mmh->recent ||
00708       $mh->flagged != $mmh->flagged ||
00709       $mh->answered != $mmh->answered ||
00710       $mh->deleted != $mmh->deleted ||
00711       $mh->seen != $mmh->seen ||
00712       $mh->draft != $mmh->draft ||
00713       $mh->display != $mmh->display) {
00714     $sql = "UPDATE `{dmail_mbox_message_headers}` SET `uid` = '%s', `msgno` = '%s', `recent` = %d, `flagged` = %d, `answered` = %d, `deleted` = %d, `seen` = %d, `draft` = %d, `display` = %d, `updated` = NOW() WHERE `mbox_id` = %d and `msgid` = '%s'";
00715     $result = db_query($sql, $mmh->uid, $mmh->msgno, $mmh->recent, $mmh->flagged, $mmh->answered, $mmh->deleted, $mmh->seen, $mmh->draft, $mmh->display, $mmh->mbox_id, $mmh->mbox_id);
00716   }
00717   return $result;
00718 }
00719 
00729 function mfn_dmail_db_mbox_message_headers_select($mbox_id) {
00730   $ns = array('db', 'mbox_message_headers', 'full');
00731   $cmode = DCACHE_DRUPAL;
00732   $cache = dcache_get($ns, $cmode);
00733   if ($cache['rows'] && $cache['mbox_id'] == $mbox_id && !dcache_expired($cache)) {
00734     return $cache['rows'];
00735   }
00736   $sql = "SELECT * from `{dmail_mbox_message_headers}` WHERE `mbox_id` = %d";
00737   $result = db_query($sql, $mbox_id);
00738   $rows = array();
00739   while ($row = db_fetch_object($result)) {
00740     $rows = $row;
00741   }
00742   $cache['rows'] = $rows;
00743   $cache['mbox_id'] = $mbox_id;
00744   dcache_set($ns, $cache, $cmode);
00745   return $cache['rows'];
00746 }
00747 
00751 function mfn_dmail_db_mbox_message_headers_count($mbox_id) {
00752   $sql = "SELECT COUNT(1) as \"count\" FROM `{dmail_mbox_message_headers}` WHERE `mbox_id` = %d";
00753   return db_result(db_query($sql, $mbox_id));
00754 }
00755 
00765 function mfn_dmail_db_junk_message_select($msgid) {
00766   global $user;
00767   $ns = array('db', 'junk_messages', 'single');
00768   $cache = dcache_get($ns);
00769   if (isset($cache['row']) &&
00770       $cache['uid'] == $user->uid &&
00771       $cache['msgid'] == $msgid &&
00772       !dcache_expired($cache)
00773      ) {
00774     return $cache['row'];
00775   }
00776   $sql = "SELECT * FROM `{dmail_junk_messages}` WHERE `user_id` = %d AND `msgid` = '%s'";
00777   $row = db_fetch_object(db_query($sql, $user->uid, $msgid));
00778   dcache_set($ns, array('uid' => $user->uid, 'msgid' => $msgid, 'row' => $row));
00779   return $row;
00780 }
00781 
00791 function mfn_dmail_db_junk_message_add($msgid) {
00792   global $user;
00793   $sql = "INSERT INTO `{dmail_junk_messages}` (`user_id`, `msgid`) VALUES (%d, '%s')";
00794   dcache_del(array('db', 'junk_messages'), DCACHE_ALL);
00795   return db_query($sql, $user->uid, $msgid);
00796 }
00797 
00807 function mfn_dmali_db_junk_message_del($msgid) {
00808   global $user;
00809   $sql = "DELETE FROM `{dmail_junk_messages}` WHERE `user_id` = %d AND `msgid` = '%s'";
00810   dcache_del(array('db', 'junk_messages'), DCACHE_ALL);
00811   return db_query($sql, $user->uid, $msgid);
00812 }
00813 
00823 function mfn_dmail_db_headerinfo_select($msgid) {
00824   $ns = array('db', 'headerinfo', 'single');
00825   $cache = dcache_get($ns);
00826   if (isset($cache['row']) &&
00827       $cache['msgid'] == $msgid &&
00828       !dcache_expired($cache)
00829      ) {
00830     return $cache['row'];
00831   }
00832   $sql = "SELECT * FROM `{dmail_headerinfo}` WHERE `msgid` = '%s'";
00833   $ret = db_fetch_object(db_query($sql, $msgid));
00834   if ($ret !== FALSE) {
00835     $ret->headers = unserialize(gzuncompress(base64_decode($ret->headers)));
00836   }
00837   dcache_set($ns, array('msgid' => $msgid, 'row' => $ret));
00838   return $ret;
00839 }
00840 
00850 function mfn_dmail_db_headerinfo_add($headerinfo) {
00851   $sql = "INSERT INTO `{dmail_headerinfo}` (`msgid`, `headers`) VALUES ('%s', '%s') ON DUPLICATE KEY UPDATE `headers` = VALUES(`headers`)";
00852   $msgid = $headerinfo->msgid;
00853   unset($headerinfo->msgid);
00854   dcache_del(array('db', 'headerinfo'), DCACHE_ALL);
00855   return db_query($sql, $msgid, base64_encode(gzcompress(serialize($headerinfo))));
00856 }
00857 
00867 function mfn_dmail_db_headerinfo_delete($msgid) {
00868   $sql = "DELETE FROM `{dmail_headerinfo}` WHERE `msgid` = '%s'";
00869   dcache_del(array('db', 'headerinfo'), DCACHE_ALL);
00870   return db_query($sql, $msgid);
00871 }
00872 
00882 function mfn_dmail_db_addrmap_select($address) {
00883   global $user;
00884   $ns = array('db', 'addrmap', 'single');
00885   $cache = dcache_get($ns);
00886   if (isset($cache['row']) &&
00887       $cache['uid'] == $user->uid &&
00888       $cache['address'] == $address &&
00889       !dcache_expired($cache)
00890      ) {
00891     return $cache['row'];
00892   }
00893   $sql = "SELECT * FROM `{dmail_addrmap}` WHERE `user_id` = %d AND `address` = '%s'";
00894   $ret = db_fetch_object(db_query($sql, $user->uid, $address));
00895   dcache_set($ns, array('uid' => $user->uid, 'address' => $address, 'row' => $ret));
00896   return $ret;
00897 }
00898 
00905 function mfn_dmail_db_signatures_select() {
00906   global $user;
00907   $ns = array('db', 'signatures', 'full');
00908   $cmode = DCACHE_DRUPAL;
00909   $cache = dcache_get($ns, $cmode);
00910   if (isset($cache['rows']) &&
00911       $cache['uid'] == $user->uid &&
00912       !dcache_expired($cache)
00913      ) {
00914     return $cache['rows'];
00915   }
00916   $sql = "SELECT * FROM `{dmail_signatures}` WHERE `user_id` = %d";
00917   $result = db_query($sql, $user->uid);
00918   $rows = array();
00919   while ($row = db_fetch_object($result)) {
00920     $rows[$row->id] = $row;
00921   }
00922   dcache_set($ns, array('uid' => $user->uid, 'rows' => $rows), $cmode);
00923   return $rows;
00924 }
00925 
00935 function mfn_dmail_db_signature_select($name_or_id) {
00936   global $user;
00937   $ns = array('db', 'signatures', 'single');
00938   $cache = dcache_get($ns);
00939   if (isset($cache['row']) &&
00940       $cache['name'] == $name_or_id &&
00941       !dcache_expired($cache) 
00942      ) {
00943     return $cache['row'];
00944   }
00945   if (is_numeric($name_or_id)) {
00946     $sql = "SELECT * FROM `{dmail_signatures}` WHERE `user_id` = %d AND `id` = %d";
00947   }
00948   else {
00949     $sql = "SELECT * FROM `{dmail_signatures}` WHERE `user_id` = %d AND `name` = '%s'";
00950   }
00951   $row = db_fetch_object(db_query($sql, $user->uid, $name_or_id));
00952   dcache_set($ns, array('name' => $name_or_id, 'row' => $row));
00953   return $row;
00954 }
00955 
00965 function mfn_dmail_db_signatures_add($values) {
00966   global $user;
00967   $sql = "INSERT INTO `{dmail_signatures}` (`user_id`, `name`, `signature`, `created`, `updated`) VALUES (%d, '%s', '%s', NOW(), NOW())";
00968   $result = db_query($sql, $user->uid, $values['name'], $values['signature']);
00969   if ($result !== FALSE) {
00970     $result = db_last_insert_id('dmail_signatures', 'id');
00971   }
00972   dcache_del(array('db', 'signatures'), DCACHE_ALL);
00973   return $result;
00974 }
All Data Structures Files Functions Variables Enumerations