Created
February 24, 2014 20:19
-
-
Save werdender/9196185 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'crypt.php'; | |
include 'db.php'; | |
if( ! empty($_POST['phone']) && ! empty($_POST['email'])) { | |
$key = $_POST['email']; | |
$crypt = new Crypt(); | |
$data = array( | |
'phone' => $crypt->encrypt($_POST['phone'], $key), | |
'email' => $crypt->encrypt($_POST['email'], $key), | |
); | |
$req = $DB->prepare("INSERT INTO phones (phone, email) values (:phone, :email)"); | |
$req->execute($data); | |
echo 'OK'; | |
}else{ | |
throw new Exception('Empty phone or email'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Crypt { | |
public static $iv = '12345678'; | |
public function __construct() { | |
$this->cipher = mcrypt_module_open(MCRYPT_BLOWFISH, '', 'cbc', ''); | |
} | |
/** | |
* | |
* @param string $decripted | |
* @param string $key | |
* @return string | |
*/ | |
public function encrypt($decripted, $key) { | |
mcrypt_generic_init($this->cipher, $key, self::$iv); | |
$encrypted = mcrypt_generic($this->cipher, $decripted); | |
mcrypt_generic_deinit($this->cipher); | |
return $encrypted; | |
} | |
/** | |
* | |
* @param string $encrypted | |
* @param string $key | |
* @return string | |
*/ | |
public function decrypt($encrypted, $key) { | |
mcrypt_generic_init($this->cipher, $key, self::$iv); | |
$decrypted = mdecrypt_generic($this->cipher, $encrypted); | |
mcrypt_generic_deinit($this->cipher); | |
return $decrypted; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$DB = new PDO("mysql:host=localhost;dbname=auslogics", 'root', false); | |
} catch (PDOException $e) { | |
echo $e->getMessage(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Скрипт сгенерирован Devart dbForge Studio for MySQL, Версия 6.1.154.0 | |
-- Домашняя страница продукта: http://www.devart.com/ru/dbforge/mysql/studio | |
-- Дата скрипта: 25.02.2014 4:16:19 | |
-- Версия сервера: 5.1.40-community | |
-- Версия клиента: 4.1 | |
-- | |
-- Отключение внешних ключей | |
-- | |
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; | |
-- | |
-- Установить режим SQL (SQL mode) | |
-- | |
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; | |
-- | |
-- Установка кодировки, с использованием которой клиент будет посылать запросы на сервер | |
-- | |
SET NAMES 'utf8'; | |
-- | |
-- Установка базы данных по умолчанию | |
-- | |
USE auslogics; | |
-- | |
-- Описание для таблицы phones | |
-- | |
DROP TABLE IF EXISTS phones; | |
CREATE TABLE phones ( | |
id INT(11) NOT NULL AUTO_INCREMENT, | |
phone VARCHAR(255) NOT NULL, | |
email VARCHAR(255) NOT NULL, | |
PRIMARY KEY (id), | |
UNIQUE INDEX UK_phones_email (email) | |
) | |
ENGINE = INNODB | |
AUTO_INCREMENT = 2 | |
AVG_ROW_LENGTH = 16384 | |
CHARACTER SET utf8 | |
COLLATE utf8_general_ci; | |
-- | |
-- Вывод данных для таблицы phones | |
-- | |
INSERT INTO phones VALUES | |
(1, 'K¶®3d.1w‡и|эGГu', '›рXVбјAЮ"\r~'); | |
-- | |
-- Восстановить предыдущий режим SQL (SQL mode) | |
-- | |
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; | |
-- | |
-- Включение внешних ключей | |
-- | |
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
Add your phone number:<br> | |
<form method="post" action="/addphone.php"> | |
Phone number: <input type="text" name="phone"><br> | |
Email address: <input type="email" name="email"><br> | |
<input type="submit" value="Add phone"> | |
</form> | |
Retrieve your phone number:<br> | |
<form method="post" action="/retrieve.php"> | |
Email address: <input type="email" name="email"><br> | |
<input type="submit" value="Retrieve phone"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'crypt.php'; | |
include 'db.php'; | |
if( ! empty($_POST['email'])) { | |
$key = $_POST['email']; | |
$crypt = new Crypt(); | |
$data = array( | |
'email' => $crypt->encrypt($_POST['email'], $key), | |
); | |
$req = $DB->prepare('SELECT phone FROM phones WHERE email = :email'); | |
$req->execute($data); | |
$res = $req->fetch(PDO::FETCH_OBJ); | |
if($res) { | |
$phone = $crypt->decrypt($res->phone, $key); | |
echo $phone; | |
}else{ | |
throw new Exception('Unknown email'); | |
} | |
}else{ | |
throw new Exception('Empty email'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment