| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | if (!defined('DEBUG_MODE')) { die(); } |
| 10: | |
| 11: | require APP_PATH.'modules/carddav_contacts/hm-carddav.php'; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class Hm_Handler_process_add_carddav_contact_from_msg extends Hm_Handler_Module { |
| 17: | public function process() { |
| 18: | list($success, $form) = $this->process_form(array('contact_source', 'contact_value')); |
| 19: | if (!$success) { |
| 20: | return; |
| 21: | } |
| 22: | $details = $this->config->dump(); |
| 23: | list($type, $source) = explode(':', $form['contact_source']); |
| 24: | if ($type != 'carddav' || !array_key_exists($source, $details)) { |
| 25: | return; |
| 26: | } |
| 27: | $detail = $details[$source]; |
| 28: | $auths = $this->user_config->get('carddav_contacts_auth_setting', array()); |
| 29: | if (!array_key_exists($source, $auths)) { |
| 30: | return; |
| 31: | } |
| 32: | $auth = $auths[$source]; |
| 33: | $pass = false; |
| 34: | if (array_key_exists('pass', $auth)) { |
| 35: | $pass = $auth['pass']; |
| 36: | } |
| 37: | $parts = process_address_fld($form['contact_value']); |
| 38: | if (!is_array($parts) || count($parts) == 0 || !is_array($parts[0]) || |
| 39: | !array_key_exists('email', $parts[0]) || !trim($parts[0]['email'])) { |
| 40: | Hm_Msgs::add('Unable to add contact', 'warning'); |
| 41: | return; |
| 42: | } |
| 43: | $email = $parts[0]['email']; |
| 44: | if (array_key_exists('label', $parts[0]) && trim($parts[0]['label'])) { |
| 45: | $fn = $parts[0]['label']; |
| 46: | } |
| 47: | else { |
| 48: | $fn = $email; |
| 49: | } |
| 50: | $carddav = new Hm_Carddav($source, $detail['server'], $auth['user'], $pass); |
| 51: | if ($carddav->add_contact(array('carddav_fn' => $fn, 'carddav_email' => $email))) { |
| 52: | Hm_Msgs::add('Contact Added'); |
| 53: | } |
| 54: | } |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | class Hm_Handler_process_delete_carddav_contact extends Hm_Handler_Module { |
| 61: | public function process() { |
| 62: | $details = $this->config->dump(); |
| 63: | list($success, $form) = $this->process_form(array('contact_type', 'contact_source', 'contact_id')); |
| 64: | if (!$success || $form['contact_type'] != 'carddav' || !array_key_exists($form['contact_source'], $details)) { |
| 65: | return; |
| 66: | } |
| 67: | $auths = $this->user_config->get('carddav_contacts_auth_setting', array()); |
| 68: | $detail = $details[$form['contact_source']]; |
| 69: | if (!array_key_exists($form['contact_source'], $auths)) { |
| 70: | return; |
| 71: | } |
| 72: | $auth = $auths[$form['contact_source']]; |
| 73: | $pass = false; |
| 74: | if (array_key_exists('pass', $auth)) { |
| 75: | $pass = $auth['pass']; |
| 76: | } |
| 77: | $carddav = new Hm_Carddav($form['contact_source'], $detail['server'], $auth['user'], $pass); |
| 78: | $contacts = $this->get('contact_store'); |
| 79: | $contact = $contacts->get($form['contact_id']); |
| 80: | if ($carddav->delete_contact($contact)) { |
| 81: | Hm_Msgs::add('Contact Deleted'); |
| 82: | $this->out('contact_deleted', 1); |
| 83: | } |
| 84: | else { |
| 85: | Hm_Msgs::add('Could not delete contact', 'warning'); |
| 86: | } |
| 87: | } |
| 88: | } |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | class Hm_Handler_process_add_carddav_contact extends Hm_Handler_Module { |
| 94: | public function process() { |
| 95: | $details = $this->config->dump(); |
| 96: | list($success, $form) = $this->process_form(array('contact_source', 'carddav_email', 'carddav_fn', 'add_contact')); |
| 97: | if (!$success || !array_key_exists($form['contact_source'], $details)) { |
| 98: | return; |
| 99: | } |
| 100: | $auths = $this->user_config->get('carddav_contacts_auth_setting', array()); |
| 101: | $detail = $details[$form['contact_source']]; |
| 102: | if (!array_key_exists($form['contact_source'], $auths)) { |
| 103: | return; |
| 104: | } |
| 105: | $auth = $auths[$form['contact_source']]; |
| 106: | $pass = false; |
| 107: | if (array_key_exists('pass', $auth)) { |
| 108: | $pass = $auth['pass']; |
| 109: | } |
| 110: | $carddav = new Hm_Carddav($form['contact_source'], $detail['server'], $auth['user'], $pass); |
| 111: | if ($carddav->add_contact($this->request->post)) { |
| 112: | Hm_Msgs::add('Contact Added'); |
| 113: | } |
| 114: | } |
| 115: | } |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | class Hm_Handler_process_edit_carddav_contact extends Hm_Handler_Module { |
| 121: | public function process() { |
| 122: | $contacts = $this->get('contact_store'); |
| 123: | $details = $this->config->dump(); |
| 124: | list($success, $form) = $this->process_form(array('contact_source', 'contact_id', 'carddav_email', |
| 125: | 'carddav_fn', 'edit_contact')); |
| 126: | if (!$success || !array_key_exists($form['contact_source'], $details)) { |
| 127: | return; |
| 128: | } |
| 129: | $contact = $contacts->get($form['contact_id']); |
| 130: | $auths = $this->user_config->get('carddav_contacts_auth_setting', array()); |
| 131: | $detail = $details[$form['contact_source']]; |
| 132: | if (!array_key_exists($form['contact_source'], $auths)) { |
| 133: | return; |
| 134: | } |
| 135: | $auth = $auths[$form['contact_source']]; |
| 136: | $pass = false; |
| 137: | if (array_key_exists('pass', $auth)) { |
| 138: | $pass = $auth['pass']; |
| 139: | } |
| 140: | $carddav = new Hm_Carddav($form['contact_source'], $detail['server'], $auth['user'], $pass); |
| 141: | if ($carddav->update_contact($contact, $this->request->post)) { |
| 142: | Hm_Msgs::add('Contact Updated'); |
| 143: | } |
| 144: | } |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | class Hm_Handler_load_edit_carddav_contact extends Hm_Handler_Module { |
| 151: | public function process() { |
| 152: | $details = $this->config->dump(); |
| 153: | if (array_key_exists('contact_source', $this->request->get) && |
| 154: | array_key_exists('contact_type', $this->request->get) && |
| 155: | $this->request->get['contact_type'] == 'carddav' && |
| 156: | array_key_exists($this->request->get['contact_source'], $details) && |
| 157: | array_key_exists('contact_id', $this->request->get)) { |
| 158: | |
| 159: | $contacts = $this->get('contact_store'); |
| 160: | $contact = $contacts->get($this->request->get['contact_id']); |
| 161: | if (is_object($contact)) { |
| 162: | $current = $contact->export(); |
| 163: | $current['id'] = $this->request->get['contact_id']; |
| 164: | $this->out('current_carddav_contact', $current); |
| 165: | } |
| 166: | } |
| 167: | } |
| 168: | } |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | class Hm_Handler_load_carddav_contacts extends Hm_Handler_Module { |
| 174: | public function process() { |
| 175: | |
| 176: | $contacts = $this->get('contact_store'); |
| 177: | $auths = $this->user_config->get('carddav_contacts_auth_setting', array()); |
| 178: | $details = $this->config->dump(); |
| 179: | |
| 180: | foreach ($details as $name => $vals) { |
| 181: | |
| 182: | $this->append('contact_edit', sprintf('carddav:%s', $name)); |
| 183: | if (!array_key_exists($name, $auths)) { |
| 184: | continue; |
| 185: | } |
| 186: | $pass = ''; |
| 187: | if (array_key_exists('pass', $auths[$name])) { |
| 188: | $pass = $auths[$name]['pass']; |
| 189: | } |
| 190: | $carddav = new Hm_Carddav($name, $vals['server'], $auths[$name]['user'], $pass); |
| 191: | $carddav->get_vcards(); |
| 192: | $contacts->import($carddav->addresses); |
| 193: | $this->append('contact_sources', 'carddav'); |
| 194: | } |
| 195: | $this->out('contact_store', $contacts, false); |
| 196: | $this->out('carddav_sources', $details); |
| 197: | } |
| 198: | } |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | class Hm_Handler_load_carddav_settings extends Hm_Handler_Module { |
| 204: | public function process() { |
| 205: | $this->out('carddav_settings', config('carddav')); |
| 206: | $this->out('carddav_auth', $this->user_config->get('carddav_contacts_auth_setting', array())); |
| 207: | } |
| 208: | } |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | class Hm_Handler_process_carddav_auth_settings extends Hm_Handler_Module { |
| 214: | public function process() { |
| 215: | if (!array_key_exists('save_settings', $this->request->post)) { |
| 216: | return; |
| 217: | } |
| 218: | $settings = $this->user_config->get('carddav_contacts_auth_setting', array()); |
| 219: | $users = array(); |
| 220: | $passwords = array(); |
| 221: | $results = $settings; |
| 222: | if (array_key_exists('carddav_usernames', $this->request->post)) { |
| 223: | $users = $this->request->post['carddav_usernames']; |
| 224: | } |
| 225: | if (array_key_exists('carddav_passwords', $this->request->post)) { |
| 226: | $passwords = $this->request->post['carddav_passwords']; |
| 227: | } |
| 228: | |
| 229: | if(empty($settings)){ |
| 230: | $settings = array_fill_keys(array_keys($users),[]); |
| 231: | } |
| 232: | foreach ($settings as $name => $vals) { |
| 233: | if (array_key_exists($name, $users)) { |
| 234: | $results[$name]['user'] = $users[$name]; |
| 235: | } |
| 236: | if (array_key_exists($name, $passwords)) { |
| 237: | $results[$name]['pass'] = $passwords[$name]; |
| 238: | } |
| 239: | } |
| 240: | if (count($results) > 0) { |
| 241: | $new_settings = $this->get('new_user_settings'); |
| 242: | $new_settings['carddav_contacts_auth_setting'] = $results; |
| 243: | $this->out('new_user_settings', $new_settings, false); |
| 244: | } |
| 245: | } |
| 246: | } |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | class Hm_Output_carddav_auth_settings extends Hm_Output_Module { |
| 252: | protected function output() { |
| 253: | $settings = $this->get('carddav_settings', array()); |
| 254: | $auths = $this->get('carddav_auth', array()); |
| 255: | if (count($settings) == 0) { |
| 256: | return; |
| 257: | } |
| 258: | $res = '<tr><td data-target=".carddav_settings" colspan="2" class="settings_subtitle cursor-pointer border-bottom p-2">'. |
| 259: | '<i class="bi bi-people-fill fs-5 me-2"></i>'. |
| 260: | $this->trans('CardDav Addressbooks').'</td></tr>'; |
| 261: | foreach ($settings as $name => $vals) { |
| 262: | $user = ''; |
| 263: | $pass = false; |
| 264: | if (array_key_exists($name, $auths)) { |
| 265: | $user = $auths[$name]['user']; |
| 266: | if (array_key_exists('pass', $auths[$name]) && $auths[$name]['pass']) { |
| 267: | $pass = true; |
| 268: | } |
| 269: | } |
| 270: | $res .= '<tr class="carddav_settings"><td>'.$this->html_safe($name).'</td><td>'; |
| 271: | $res .= '<input autocomplete="username" type="text" value="'.$user.'" name="carddav_usernames['.$this->html_safe($name).']" '; |
| 272: | $res .= 'placeholder="'.$this->trans('Username').'" class="form-control warn_on_paste" /> <input type="password" class="form-control warn_on_paste"'; |
| 273: | if ($pass) { |
| 274: | $res .= 'disabled="disabled" placeholder="'.$this->trans('Password saved').'" '; |
| 275: | $res .= 'name="carddav_passwords['.$this->html_safe($name).']" /> <input type="button" '; |
| 276: | $res .= 'value="'.$this->trans('Unlock').'" class="carddav_password_change btn btn-primary" /></td></tr>'; |
| 277: | } |
| 278: | else { |
| 279: | $res .= 'autocomplete="new-password" placeholder="'.$this->trans('Password').'" '; |
| 280: | $res .= 'name="carddav_passwords['.$this->html_safe($name).']" /></td></tr>'; |
| 281: | } |
| 282: | } |
| 283: | return $res; |
| 284: | } |
| 285: | } |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: | class Hm_Output_carddav_contacts_form extends Hm_Output_Module { |
| 291: | protected function output() { |
| 292: | |
| 293: | $email = ''; |
| 294: | $name = ''; |
| 295: | $phone = ''; |
| 296: | $sources = $this->get('carddav_sources', array()); |
| 297: | if (count($sources) == 0) { |
| 298: | return ''; |
| 299: | } |
| 300: | $form_class = 'contact_form'; |
| 301: | $button = '<input class="add_contact_submit btn btn-primary" type="submit" name="add_contact" value="'.$this->trans('Add').'" />'; |
| 302: | $title = $this->trans('Add Carddav'); |
| 303: | $current = $this->get('current_carddav_contact', array()); |
| 304: | $current_source = false; |
| 305: | if (!empty($current)) { |
| 306: | $current_source = $current['source']; |
| 307: | if (array_key_exists('email_address', $current)) { |
| 308: | $email = $current['email_address']; |
| 309: | } |
| 310: | if (array_key_exists('fn', $current)) { |
| 311: | $name = $current['fn']; |
| 312: | } |
| 313: | if (array_key_exists('phone_number', $current)) { |
| 314: | $phone = $current['phone_number']; |
| 315: | } |
| 316: | $form_class = 'contact_update_form mt-3'; |
| 317: | $title = sprintf($this->trans('Update Carddav - %s'), $this->html_safe($current['source'])); |
| 318: | $button = '<input type="hidden" name="contact_id" value="'.$this->html_safe($current['id']).'" />'. |
| 319: | '<input class="edit_contact_submit btn btn-primary" type="submit" name="edit_contact" value="'.$this->trans('Update').'" />'; |
| 320: | } |
| 321: | if ($current_source) { |
| 322: | $target = '<input type="hidden" name="carddav_email_id" value="'.$this->html_safe($current['carddav_email_id']).'" />'. |
| 323: | '<input type="hidden" name="carddav_phone_id" value="'.$this->html_safe($current['carddav_phone_id']).'" />'. |
| 324: | '<input type="hidden" name="carddav_fn_id" value="'.$this->html_safe($current['carddav_fn_id']).'" />'. |
| 325: | '<input type="hidden" name="contact_source" value="'.$this->html_safe($current_source).'" />'; |
| 326: | } |
| 327: | else { |
| 328: | $target = '<label class="form-label" for="contact_source">'.$this->trans('Account').'</label><select id="contact_source" '. |
| 329: | 'name="contact_source" class="form-control">'; |
| 330: | foreach ($sources as $src => $details) { |
| 331: | $target .= '<option value="'.$this->html_safe($src).'">'.$this->html_safe($src).'</option>'; |
| 332: | } |
| 333: | $target .= '</select><br />'; |
| 334: | } |
| 335: | return '<div class="add_contact_responsive"><form class="add_contact_form" method="POST">'. |
| 336: | '<button class="server_title mt-2 btn btn-light"><i class="bi bi-person-add me-2"></i>'.$title.'</button>'. |
| 337: | '<div class="'.$form_class.'">'.$target.''. |
| 338: | '<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />'. |
| 339: | '<label class="form-label" for="carddav_email">'.$this->trans('E-mail Address').' *</label>'. |
| 340: | '<input required placeholder="'.$this->trans('E-mail Address').'" id="carddav_email" type="email" name="carddav_email" '. |
| 341: | 'value="'.$this->html_safe($email).'" class="form-control" /><br />'. |
| 342: | '<label class="form-label" for="carddav_fn">'.$this->trans('Full Name').' *</label>'. |
| 343: | '<input required placeholder="'.$this->trans('Full Name').'" id="carddav_fn" type="text" name="carddav_fn" '. |
| 344: | 'value="'.$this->html_safe($name).'" class="form-control" /><br />'. |
| 345: | '<label class="form-label" for="carddav_phone">'.$this->trans('Telephone Number').'</label>'. |
| 346: | '<input placeholder="'.$this->trans('Telephone Number').'" id="carddav_phone" type="text" name="carddav_phone" '. |
| 347: | 'value="'.$this->html_safe($phone).'" class="form-control"><br />'.$button.' <input type="button" class="reset_contact btn btn-secondary" value="'. |
| 348: | $this->trans('Cancel').'" /></div></form></div>'; |
| 349: | } |
| 350: | } |
| 351: | |