1: <?php
2:
3: /**
4: * Profile modules
5: * @package modules
6: * @subpackage profiles
7: */
8:
9: if (!defined('DEBUG_MODE')) { die(); }
10:
11: /**
12: * @subpackage profiles/lib
13: */
14: class Hm_Profiles {
15:
16: use Hm_Repository, Searchable;
17:
18: private static $data = array();
19:
20: public static function init($hmod) {
21: self::initRepo('profiles', $hmod->user_config, $hmod->session, self::$data);
22: if (self::count() == 0) {
23: if (PHP_VERSION_ID < 70000) {
24: try {
25: self::loadLegacy($hmod);
26: } catch (Exception $e) {
27: self::$data = array();
28: }
29: }
30: if (PHP_VERSION_ID >= 70000) {
31: try {
32: self::loadLegacy($hmod);
33: } catch (Throwable $e) {
34: self::$data = array();
35: }
36: }
37: }
38: if (self::count() == 0) {
39: self::createDefault($hmod);
40: }
41: }
42:
43: public static function setDefault($id) {
44: if (! array_key_exists($id, self::$data)) {
45: return false;
46: }
47: foreach (self::$data as $p_id => $vals) {
48: if ($vals['default']) {
49: $vals['default'] = false;
50: self::edit($p_id, $vals);
51: }
52: }
53: $vals = self::get($id);
54: $vals['default'] = true;
55: self::edit($id, $vals);
56: return true;
57: }
58:
59: public static function getDefault() {
60: foreach (self::$data as $vals) {
61: if ($vals['default'] && $vals['default'] = true) {
62: return $vals;
63: }
64: }
65: return null;
66: }
67:
68:
69: /**
70: * Get the dataset for the server list
71: * @return array
72: */
73: protected static function getDataset() {
74: return self::$data;
75: }
76:
77: public static function createDefault($hmod) {
78: if (! $hmod->module_is_supported('imap') || ! $hmod->module_is_supported('smtp')) {
79: return;
80: }
81: if (! $hmod->config->get('autocreate_profile')) {
82: return;
83: }
84: $imap_servers = Hm_IMAP_List::dump();
85: $smtp_servers = Hm_SMTP_List::dump();
86: if (count($imap_servers) == 1 && count($smtp_servers) == 1) {
87: $imap_server = reset($imap_servers);
88: $smtp_server = reset($smtp_servers);
89: list($address, $reply_to) = outbound_address_check($hmod, $imap_server['user'], '');
90: self::add(array(
91: 'default' => true,
92: 'name' => 'Default',
93: 'address' => $address,
94: 'replyto' => $reply_to,
95: 'smtp_id' => $smtp_server['id'],
96: 'imap_id' => $imap_server['id'],
97: 'sig' => '',
98: 'rmk' => '',
99: 'type' => 'imap',
100: 'autocreate' => true,
101: 'user' => $imap_server['user'],
102: 'server' => $imap_server['server'],
103: ));
104: }
105: }
106:
107: public static function loadLegacy($hmod) {
108: if ($hmod->module_is_supported('imap')) {
109: foreach (Hm_IMAP_List::dump() as $id => $server) {
110: $profile = $hmod->user_config->get('profile_imap_'.$server['server'].'_'.$server['user'], array(
111: 'profile_default' => false, 'profile_name' => '', 'profile_address' => '',
112: 'profile_replyto' => '', 'profile_smtp' => '', 'profile_sig' => '', 'profile_rmk' => ''));
113: if (! $profile['profile_name']) {
114: continue;
115: }
116: self::add(array(
117: 'default' => $profile['profile_default'],
118: 'name' => $profile['profile_name'],
119: 'address' => array_key_exists('profile_address', $profile) ? $profile['profile_address'] : '',
120: 'replyto' => $profile['profile_replyto'],
121: 'smtp_id' => $profile['profile_smtp'],
122: 'imap_id' => $server['id'],
123: 'sig' => $profile['profile_sig'],
124: 'rmk' => $profile['profile_rmk'],
125: 'type' => 'imap',
126: 'user' => $server['user'],
127: 'server' => $server['server'],
128: ));
129: }
130: }
131: }
132:
133: /**
134: * @param string $field The name of the field to search within.
135: * @param mixed $value The value to search for within the specified field.
136: * @return array An array containing profiles that match the search criteria.
137: */
138: public static function search($field, $value) {
139: $res = array();
140: foreach (self::getAll() as $profile) {
141: if (!empty($profile[$field]) && $profile[$field] == $value) {
142: $res[] = $profile;
143: }
144: }
145: return $res;
146: }
147: }
148: