/** * @param array $data * @return waContact */ protected function afterAuth($data) { $app_id = $this->getStorage()->get('auth_app'); $contact_id = 0; // find contact by auth adapter id, i.e. facebook_id $contact_data_model = new waContactDataModel(); $row = $contact_data_model->getByField(array('field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); if ($row) { $contact_id = $row['contact_id']; } // try find user by email if (!$contact_id && isset($data['email'])) { $sql = "SELECT c.id FROM wa_contact_emails e\n JOIN wa_contact c ON e.contact_id = c.id\n WHERE e.email = s:email AND e.sort = 0 AND c.password != ''"; $contact_model = new waContactModel(); $contact_id = $contact_model->query($sql, array('email' => $data['email']))->fetchField('id'); // save source_id if ($contact_id) { $contact_data_model->insert(array('contact_id' => $contact_id, 'field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); } } // create new contact if (!$contact_id) { $contact = new waContact(); $data[$data['source'] . '_id'] = $data['source_id']; $data['create_method'] = $data['source']; $data['create_app_id'] = $app_id; // set random password (length = default hash length - 1, to disable ability auth using login and password) $contact->setPassword(substr(waContact::getPasswordHash(uniqid(time(), true)), 0, -1), true); unset($data['source']); unset($data['source_id']); if (isset($data['photo_url'])) { $photo_url = $data['photo_url']; unset($data['photo_url']); } else { $photo_url = false; } $contact->save($data); $contact_id = $contact->getId(); if ($contact_id && $photo_url) { $photo_url_parts = explode('/', $photo_url); // copy photo to tmp dir $path = wa()->getTempPath('auth_photo/' . $contact_id . '.' . end($photo_url_parts), $app_id); $photo = file_get_contents($photo_url); file_put_contents($path, $photo); $contact->setPhoto($path); } } else { $contact = new waContact($contact_id); } // auth user if ($contact_id) { wa()->getAuth()->auth(array('id' => $contact_id)); return $contact; } return false; }
/** * @param array $data * @return waContact */ protected function afterAuth($data) { $contact_id = 0; // find contact by auth adapter id, i.e. facebook_id $contact_data_model = new waContactDataModel(); $row = $contact_data_model->getByField(array('field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); if ($row) { $contact_id = $row['contact_id']; } if (wa()->getUser()->isAuth()) { $contact = wa()->getUser(); if ($contact_id && $contact_id != $contact->getId()) { // delete old link $contact_data_model->deleteByField(array('contact_id' => $contact_id, 'field' => $data['source'] . '_id')); // save new link $contact->save(array($data['source'] . '_id' => $data['source_id'])); } $contact_id = $contact->getId(); } // try find user by email if (!$contact_id && isset($data['email'])) { $contact_model = new waContactModel(); $sql = "SELECT c.id FROM wa_contact_emails e\n JOIN wa_contact c ON e.contact_id = c.id\n WHERE e.email LIKE '" . $contact_model->escape($data['email'], 'like') . "' AND e.sort = 0 AND c.password != ''"; $contact_id = $contact_model->query($sql)->fetchField('id'); // save source_id if ($contact_id) { $tmp = array('contact_id' => $contact_id, 'field' => $data['source'] . '_id', 'sort' => 0); // contact already has this source $row = $contact_data_model->getByField($tmp); if ($row) { $contact_data_model->updateByField($tmp, array('value' => $data['source_id'])); } else { $tmp['value'] = $data['source_id']; $contact_data_model->insert($tmp); } } } // create new contact if (!$contact_id) { $contact = $this->createContact($data); if ($contact) { $contact_id = $contact->getId(); } } elseif (empty($contact)) { $contact = new waContact($contact_id); } // auth user if ($contact_id) { if (!wa()->getUser()->isAuth()) { wa()->getAuth()->auth(array('id' => $contact_id)); } return $contact; } return false; }
/** * @param string $login * @param waAuth $auth * @return waContact|bool */ protected function findContact($login, $auth) { $contact_model = new waContactModel(); $is_user = $auth->getOption('is_user'); if (strpos($login, '@')) { $sql = "SELECT c.* FROM wa_contact c\n JOIN wa_contact_emails e ON c.id = e.contact_id\n WHERE " . ($is_user ? "c.is_user = 1 AND " : "") . "e.email LIKE s:email AND e.sort = 0\n ORDER BY c.id LIMIT 1"; $contact_info = $contact_model->query($sql, array('email' => $login))->fetch(); } else { $contact_info = $contact_model->getByField('login', $login); } if ($contact_info && (!$is_user || $contact_info['is_user'])) { $contact = new waContact($contact_info['id']); $contact->setCache($contact_info); return $contact; } return false; }
public function getByLogin($login) { $result = array(); $model = new waContactModel(); if ($this->options['login'] == 'login') { $result = $model->getByField('login', $login); } elseif ($this->options['login'] == 'email') { if (strpos($login, '@') === false) { $result = $model->getByField('login', $login); } else { $sql = "SELECT c.* FROM wa_contact c\n JOIN wa_contact_emails e ON c.id = e.contact_id\n WHERE " . ($this->options['is_user'] ? "c.is_user = 1 AND " : "") . "e.email LIKE s:email AND e.sort = 0 AND c.password != ''\n ORDER BY c.id LIMIT 1"; $result = $model->query($sql, array('email' => $login))->fetch(); } } if ($result) { $this->checkBan($result); } return $result; }
public function findDuplicatesFor($field, $values, $excludeIds = array()) { if (!$values) { return array(); } // Check if field exists, is active and is kept in this storage if (!$field instanceof waContactField) { $field = waContactFields::get($field); if (!$field) { return array(); } } if ($field->getParameter('storage') != 'info') { return array(); } $field = $field->getId(); $sql = "SELECT `{$field}` AS f, id\n FROM wa_contact\n WHERE `{$field}` IN (:values)" . ($excludeIds ? " AND id NOT IN (:excludeIds) " : ' ') . "GROUP BY f"; $this->getModel(); $r = $this->model->query($sql, array('values' => $values, 'excludeIds' => $excludeIds)); return $r->fetchAll('f', true); }
/** * Returns list of the users * * @param string $app_id - if specified returns only users whish has access to the application * @return array */ public static function getUsers($app_id = null) { $contact_model = new waContactModel(); if ($app_id) { $sql = "SELECT c.id, c.name\n FROM " . $contact_model->getTableName() . " c JOIN\n wa_contact_rights r ON c.id = -r.group_id AND c.is_user = 1\n WHERE (r.app_id = s:app_id OR (r.app_id = 'webasyst' AND r.name = 'backend')) AND r.value > 0\n UNION\n (SELECT c.id, c.name\n FROM " . $contact_model->getTableName() . " c JOIN\n wa_user_groups g ON c.id = g.contact_id AND c.is_user = 1 JOIN\n wa_contact_rights r ON g.group_id = r.group_id\n WHERE (r.app_id = s:app_id OR (r.app_id = 'webasyst' AND r.name = 'backend')) AND r.value > 0\n ) ORDER BY name"; } else { $sql = "SELECT c.id, c.name FROM " . $contact_model->getTableName() . " c\n WHERE c.is_user = 1\n ORDER BY c.name"; } return $contact_model->query($sql, array('app_id' => $app_id))->fetchAll('id', true); }
/** * @param string $email * @return array */ protected function getByEmail($email) { $model = new waContactModel(); $sql = "SELECT c.* FROM wa_contact c\n JOIN wa_contact_emails e ON c.id = e.contact_id\n WHERE " . ($this->options['is_user'] ? "c.is_user = 1 AND " : "") . "e.email LIKE s:email AND e.sort = 0 AND c.password != ''\n ORDER BY c.id LIMIT 1"; return $model->query($sql, array('email' => $email))->fetch(); }
<?php $category_model = new waContactCategoryModel(); $category = $category_model->getBySystemId('blog'); $contact_model = new waContactModel(); $sql = "SELECT id FROM wa_contact WHERE create_app_id='blog'"; $contact_ids = $contact_model->query($sql)->fetchAll(null, true); if ($contact_ids) { $contact_categories_model = new waContactCategoriesModel(); $contact_categories_model->add($contact_ids, $category['id']); }
/** * @param array $data * @return waContact */ protected function afterAuth($data) { $app_id = $this->getStorage()->get('auth_app'); $contact_id = 0; // find contact by auth adapter id, i.e. facebook_id $contact_data_model = new waContactDataModel(); $row = $contact_data_model->getByField(array('field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); if ($row) { $contact_id = $row['contact_id']; } // try find user by email if (!$contact_id && isset($data['email'])) { $contact_model = new waContactModel(); $sql = "SELECT c.id FROM wa_contact_emails e\n JOIN wa_contact c ON e.contact_id = c.id\n WHERE e.email LIKE '" . $contact_model->escape($data['email'], 'like') . "' AND e.sort = 0 AND c.password != ''"; $contact_id = $contact_model->query($sql)->fetchField('id'); // save source_id if ($contact_id) { $contact_data_model->insert(array('contact_id' => $contact_id, 'field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); } } // create new contact if (!$contact_id) { $contact = new waContact(); $data[$data['source'] . '_id'] = $data['source_id']; $data['create_method'] = $data['source']; $data['create_app_id'] = $app_id; // set random password (length = default hash length - 1, to disable ability auth using login and password) $contact->setPassword(substr(waContact::getPasswordHash(uniqid(time(), true)), 0, -1), true); unset($data['source']); unset($data['source_id']); if (isset($data['photo_url'])) { $photo_url = $data['photo_url']; unset($data['photo_url']); } else { $photo_url = false; } $contact->save($data); $contact_id = $contact->getId(); if ($contact_id && $photo_url) { $photo_url_parts = explode('/', $photo_url); // copy photo to tmp dir $path = wa()->getTempPath('auth_photo/' . $contact_id . '.' . md5(end($photo_url_parts)), $app_id); $s = parse_url($photo_url, PHP_URL_SCHEME); $w = stream_get_wrappers(); if (in_array($s, $w) && ini_get('allow_url_fopen')) { $photo = file_get_contents($photo_url); } elseif (function_exists('curl_init')) { $ch = curl_init($photo_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25); $photo = curl_exec($ch); curl_close($ch); } else { $photo = null; } if ($photo) { file_put_contents($path, $photo); $contact->setPhoto($path); } } } else { $contact = new waContact($contact_id); } // auth user if ($contact_id) { wa()->getAuth()->auth(array('id' => $contact_id)); return $contact; } return false; }
public function oauth($provider, $config, $token, $code = null) { /** * @var waOAuth2Adapter $auth */ $auth = wa()->getAuth($provider, $config); if (!$token && $code) { $token = $auth->getAccessToken($code); } $data = $auth->getUserData($token); if (wa()->getUser()->getId()) { wa()->getUser()->save(array($data['source'] . '_id' => $data['source_id'])); return wa()->getUser(); } $app_id = wa()->getApp(); $contact_id = 0; // find contact by auth adapter id, i.e. facebook_id $contact_data_model = new waContactDataModel(); $row = $contact_data_model->getByField(array('field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); if ($row) { $contact_id = $row['contact_id']; } // try find user by email if (!$contact_id && isset($data['email'])) { $sql = "SELECT c.id FROM wa_contact_emails e\n JOIN wa_contact c ON e.contact_id = c.id\n WHERE e.email = s:email AND e.sort = 0 AND c.password != ''"; $contact_model = new waContactModel(); $contact_id = $contact_model->query($sql, array('email' => $data['email']))->fetchField('id'); // save source_id if ($contact_id) { $contact_data_model->insert(array('contact_id' => $contact_id, 'field' => $data['source'] . '_id', 'value' => $data['source_id'], 'sort' => 0)); } } // create new contact if (!$contact_id) { $contact = new waContact(); $data[$data['source'] . '_id'] = $data['source_id']; $data['create_method'] = $data['source']; $data['create_app_id'] = $app_id; // set random password (length = default hash length - 1, to disable ability auth using login and password) $contact->setPassword(substr(waContact::getPasswordHash(uniqid(time(), true)), 0, -1), true); unset($data['source']); unset($data['source_id']); if (isset($data['photo_url'])) { $photo_url = $data['photo_url']; unset($data['photo_url']); } else { $photo_url = false; } $contact->save($data); $contact_id = $contact->getId(); if ($contact_id && $photo_url) { $photo_url_parts = explode('/', $photo_url); // copy photo to tmp dir $path = wa()->getTempPath('auth_photo/' . $contact_id . '.' . md5(end($photo_url_parts)), $app_id); if (function_exists('curl_init')) { $ch = curl_init($photo_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 25); $photo = curl_exec($ch); curl_close($ch); } else { $photo = file_get_contents($photo_url); } if ($photo) { file_put_contents($path, $photo); $contact->setPhoto($path); } } } else { $contact = new waContact($contact_id); } // auth user if ($contact_id) { wa()->getAuth()->auth(array('id' => $contact_id)); return $contact; } return false; }