From: Cameron Ball Date: Wed, 15 Oct 2014 07:36:37 +0000 (+0800) Subject: Facebook auth stuff. X-Git-Url: http://git.cameron1729.xyz/?a=commitdiff_plain;h=b6ae326e4f2f76675f5c0b8c23fa27a9e435adea;p=rock.divinelegy.git Facebook auth stuff. --- diff --git a/.gitignore b/.gitignore index f3cc513..008e5e6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock vendor db.php files +FacebookApp.php diff --git a/Controllers/UserAuthController.php b/Controllers/UserAuthController.php new file mode 100644 index 0000000..f261547 --- /dev/null +++ b/Controllers/UserAuthController.php @@ -0,0 +1,125 @@ +_request = $request; + $this->_response = $response; + $this->_facebookSessionFactory = $facebookSessionFactory; + $this->_userRepository = $userRepository; + $this->_userStepByStepBuilder = $userStepByStepBuilder; + } + + public function indexAction() { + $token = $this->validateAuthRequest(); + $facebookSession = $this->_facebookSessionFactory->createInstance($token); + + $this->_facebookSession = $this->isSessionLongLived($facebookSession) ? $facebookSession->getLongLivedSession() : $facebookSession; + $this->_facebookRequest = (new FacebookRequest($this->_facebookSession, 'GET', '/me?fields=hometown,first_name,last_name'))->execute(); + + $id = $this->_facebookRequest->getGraphObject(GraphUser::className())->getId(); + + // If the user is not in the DB, create them. + $user = $this->_userRepository->findByFacebookId($id) ?: $this->registerUser(); + // Update stored auth token if needed. + $this->updateAuthToken($user); + + $this->_response->setHeader('Content-Type', 'application/json') + ->setBody(json_encode(array('token' => $this->_facebookSession->getToken(), 'expires' => $this->getSessionExpiryTimestamp($this->_facebookSession), 'displayName' => $user->getDisplayName()))) + ->sendResponse(); + } + + private function validateAuthRequest() + { + $request = $this->_request->get(); + $response = $this->_response->setHeader('Content-Type', 'application/json'); + + if(!isset($request['token'])) + { + $response->setBody(json_encode(array('result' => 'error', 'message' => 'missing auth token'))) + ->sendResponse(); + die(); + } + + return $request['token']; + } + + private function registerUser() + { + $userProfile = $this->_facebookRequest->getGraphObject(GraphUser::className()); + + $homeTownPageId = $userProfile->getProperty('hometown')->getProperty('id'); + $pageRequest = (new FacebookRequest($this->_facebookSession, 'GET', '/' . $homeTownPageId ))->execute(); + $pageLocation = $pageRequest->getGraphObject(GraphLocation::className())->getProperty('location')->cast(GraphLocation::className()); + + $country = Util::countryNameFromLatLong($pageLocation->getLatitude(), $pageLocation->getLongitude()); + $firstName = $userProfile->getFirstName(); + $lastName = $userProfile->getLastName(); + $facebookId = $userProfile->getId(); + + //TODO: Is insantiating the VO classes here a good idea? + $newUser = $this->_userStepByStepBuilder->With_Country(new \Domain\VOs\Country($country)) + ->With_DisplayName($firstName) + ->With_Name(new \Domain\VOs\Name($firstName, $lastName)) + ->With_Tags(array()) + ->With_FacebookId($facebookId) + ->build(); + + $newUser->setAuthToken($this->_facebookSession->getToken()); + + $this->_userRepository->save($newUser); + + return $newUser; + } + + private function updateAuthToken(IUser $user) + { + if($user->getAuthToken() != $this->_facebookSession->getToken()) + { + $user->setAuthToken($this->_facebookSession->getToken()); + $this->_userRepository->save($user); + } + } + + private function isSessionLongLived(FacebookSession $session) + { + return $this->getSessionExpiryTimestamp($session) - time() >= 60; + } + + private function getSessionExpiryTimestamp(FacebookSession $session) + { + return $session->getSessionInfo()->getExpiresAt()->format('U'); + } +} diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index 692763b..2a8c9e6 100644 --- a/DataAccess/DataMapper/DataMapper.php +++ b/DataAccess/DataMapper/DataMapper.php @@ -49,7 +49,7 @@ class DataMapper implements IDataMapper $class->setId($row['id']); $entities[$row['id']] = $class; } - + return $entities; } @@ -57,9 +57,53 @@ class DataMapper implements IDataMapper { $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db); + $flattened = array(); + $flattened_tables = array(); + foreach($queries as $index => $query) + { + $this_table = $query['table']; + $this_columns = $query['columns']; + $add = false; + + for($i = $index+1; $i $this_columns, 'table' => $this_table, 'prepared' => $prepared, 'id' => $id); + } + } + + $queries = array(); + + foreach($flattened as $info) + { + if(isset($info['id'])) + { + $query = $info['prepared']; + $query = substr($query, 0, -2); + $query .= sprintf(' WHERE id=%u', $info['id']); + } else { + $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', + $info['table'], + implode(', ', array_keys($info['columns'])), + implode(', ', $info['columns'])); + } + + $queries[] = $query; + } + // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE) // { - unset($queries['TYPE']); $idMap = []; foreach($queries as $index => $query) { @@ -98,10 +142,6 @@ class DataMapper implements IDataMapper $statement->execute(); } //} - - echo '
';
-        print_r($queries);
-        echo '
'; } //TODO: Implement diff --git a/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php b/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php index 6f17458..034a8d6 100644 --- a/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php +++ b/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php @@ -41,7 +41,7 @@ class AbstractPopulationHelper static function generateUpdateSaveQuery($maps, $entity, $id, $db, &$queries = array(), $extraColumns = array()) { $entityMapsIndex = self::getMapsNameFromEntityObject($entity, $maps); - + if($id) { $query = sprintf('update %s set ', $maps[$entityMapsIndex]['table']); @@ -113,8 +113,6 @@ class AbstractPopulationHelper break; case self::REFERENCE_BACK: - echo 'bleh'; - $voId = self::findVOInDB($maps, $property, $db, @@ -221,10 +219,11 @@ class AbstractPopulationHelper // in the case of setting up a new entity, the VOs should never // exist in the first place, so we just make them. case 'DataAccess\DataMapper\Helpers\VOArrayMapsHelper': - if($id) + if($id && isset($property[0])) { // If we assume that all elements in the array are the same then // we can just use the first one to figure out which maps entry to use + $subEntityMapsIndex = self::getMapsNameFromEntityObject($property[0], $maps); $voIds = self::mapVOArrayToIds($maps[$subEntityMapsIndex]['table'], array(strtolower($entityMapsIndex . '_id'), $id), @@ -254,22 +253,32 @@ class AbstractPopulationHelper } } } +// +// if($id) +// { +// $query = substr($query, 0, -2); +// $query .= sprintf(' WHERE id=%u', $id); +// $queries['TYPE'] = self::QUERY_TYPE_UPDATE; +// } else { +// $queryColumnNamesAndValues = array_merge($queryColumnNamesAndValues, $extraColumns); +// $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', +// $maps[$entityMapsIndex]['table'], +// implode(', ', array_keys($queryColumnNamesAndValues)), +// implode(', ', $queryColumnNamesAndValues)); +// $queries['TYPE'] = self::QUERY_TYPE_CREATE; +// } +// print_r($queryColumnNamesAndValues); +// echo '
'; if($id) { - $query = substr($query, 0, -2); - $query .= sprintf(' WHERE id=%u', $id); - $queries['TYPE'] = self::QUERY_TYPE_UPDATE; + $queryColumnNamesAndValues = @$queryColumnNamesAndValues ?: array(); + $queries[] = array('id' => $id, 'prepared' => $query, 'table' => $maps[$entityMapsIndex]['table'], 'columns' => $queryColumnNamesAndValues); } else { $queryColumnNamesAndValues = array_merge($queryColumnNamesAndValues, $extraColumns); - $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', - $maps[$entityMapsIndex]['table'], - implode(', ', array_keys($queryColumnNamesAndValues)), - implode(', ', $queryColumnNamesAndValues)); - $queries['TYPE'] = self::QUERY_TYPE_CREATE; + $queries[] = array('table' => $maps[$entityMapsIndex]['table'], 'columns' => $queryColumnNamesAndValues); } - - $queries[] = $query; + return $queries; } diff --git a/DataAccess/IUserRepository.php b/DataAccess/IUserRepository.php new file mode 100644 index 0000000..f34efc5 --- /dev/null +++ b/DataAccess/IUserRepository.php @@ -0,0 +1,17 @@ +applyJoinClauses() ->applyWhereClauses() ->applyLimitClause(); - + return $this->_queryString; } diff --git a/DataAccess/UserRepository.php b/DataAccess/UserRepository.php new file mode 100644 index 0000000..70fe069 --- /dev/null +++ b/DataAccess/UserRepository.php @@ -0,0 +1,67 @@ +_dataMapper = $dataMapper; + $this->_queryBuilderFactory = $queryBuilderFactory; + } + + public function findById($id) { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('id', '=', "$id"); + + return $this->_dataMapper->map( + 'User', + $queryBuilder + ); + } + + public function findRange($id, $limit) + { + return $this->_dataMapper->findRange( + 'User', + 'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit + ); + } + + public function findByFacebookId($id) { + $queryBuilder = $this->_queryBuilderFactory->createInstance(); + $queryBuilder->where('facebook_id', '=', "$id"); + + $results = $this->_dataMapper->map( + 'User', + $queryBuilder + ); + + return reset($results); + } + + public function findByAuthToken($token) { + return $this->_dataMapper->map( + 'User', + 'SELECT * FROM %s WHERE auth_token=' . $token + ); + } + + public function save(IUser $entity) { + $this->_dataMapper->save($entity); + } + + //TODO: Implement + public function remove(IUser $entity) { + ; + } + +} diff --git a/Domain/Entities/IUser.php b/Domain/Entities/IUser.php index 3bbc462..143afc8 100644 --- a/Domain/Entities/IUser.php +++ b/Domain/Entities/IUser.php @@ -11,4 +11,8 @@ interface IUser public function getCountry(); public function getDisplayName(); public function getYearsStepArtist(); + public function getFacebookId(); + public function setFacebookId($id); + public function getAuthToken(); + public function setAuthToken($token); } diff --git a/Domain/Entities/IUserBuilder.php b/Domain/Entities/IUserBuilder.php index 552e00c..a938f0e 100644 --- a/Domain/Entities/IUserBuilder.php +++ b/Domain/Entities/IUserBuilder.php @@ -11,6 +11,7 @@ interface IUserBuilder public function With_DisplayName($name); public function With_Name(IName $name); public function With_Tags(array $tags); + public function With_FacebookId($id); public function With_YearsStepArtist($years); public function build(); } diff --git a/Domain/Entities/User.php b/Domain/Entities/User.php index 1628017..3b65ef1 100644 --- a/Domain/Entities/User.php +++ b/Domain/Entities/User.php @@ -15,17 +15,23 @@ class User extends AbstractEntity implements IUser private $_name; private $_tags; private $_yearsStepArtist; + private $_facebookId; + private $_authToken; public function __construct( ICountry $country, $displayName, IName $name, - array $tags + array $tags, + $facebookId, + $authToken = null ) { $this->_country = $country; $this->_displayName = $displayName; $this->_name = $name; $this->_tags = $tags; + $this->_facebookId = $facebookId; + $this->_authToken = $authToken; } public function getCountry() { @@ -44,6 +50,26 @@ class User extends AbstractEntity implements IUser return $this->_tags; } + public function getFacebookId() + { + return $this->_facebookId; + } + + public function setFacebookId($id) + { + $this->_facebookId = $id; + } + + public function getAuthToken() + { + return $this->_authToken; + } + + public function setAuthToken($token) + { + $this->_authToken = $token; + } + public function getYearsStepArtist() { return $this->_yearsStepArtist; } diff --git a/Domain/Entities/UserBuilder.php b/Domain/Entities/UserBuilder.php index 44608f3..8957a3d 100644 --- a/Domain/Entities/UserBuilder.php +++ b/Domain/Entities/UserBuilder.php @@ -14,6 +14,7 @@ class UserBuilder implements IUserBuilder private $_displayName; private $_name; private $_tags; + private $_facebookId; private $_yearsStepArtist; public function __construct(IUserFactory $userFactory) @@ -41,6 +42,11 @@ class UserBuilder implements IUserBuilder return $this; } + public function With_FacebookId($id) { + $this->_facebookId = $id; + return $this; + } + public function With_YearsStepArtist($years) { $this->_yearsStepArtist = $years; return $this; @@ -52,6 +58,6 @@ class UserBuilder implements IUserBuilder $this->_displayName, $this->_name, $this->_tags, - $this->_yearsStepArtist); + $this->_facebookId); } } \ No newline at end of file diff --git a/Domain/Entities/UserFactory.php b/Domain/Entities/UserFactory.php index a9a5402..94235be 100644 --- a/Domain/Entities/UserFactory.php +++ b/Domain/Entities/UserFactory.php @@ -13,7 +13,7 @@ interface IUserFactory $displayName, IName $name, array $tags, - $yearsStepArtist + $facebookId ); } @@ -24,14 +24,14 @@ class UserFactory implements IUserFactory $displayName, IName $name, array $tags, - $yearsStepArtist + $facebookId ) { return new User( $country, $displayName, $name, $tags, - $yearsStepArtist + $facebookId ); } } \ No newline at end of file diff --git a/Domain/Entities/UserStepByStepBuilder.php b/Domain/Entities/UserStepByStepBuilder.php index 068c191..e586e64 100644 --- a/Domain/Entities/UserStepByStepBuilder.php +++ b/Domain/Entities/UserStepByStepBuilder.php @@ -3,8 +3,7 @@ namespace Domain\Entities; use Domain\VOs\ICountry; -use Domain\Vos\IName; -use Domain\Entities\IUser; +use Domain\VOs\IName; interface IUserStepByStepBuilder { @@ -28,6 +27,11 @@ interface IUserStepByStepBuilder_With_Name interface IUserStepByStepBuilder_With_Tags { + public function With_FacebookId($id); +} + +interface IUserStepByStepBuilder_With_FacebookId +{ public function With_YearsStepArtist($years); //not going to make this mandatory as it is kind of a joke public function build(); } @@ -76,6 +80,14 @@ class UserStepByStepBuilder_With_Name extends AbstractUserStepByStepBuilder impl class UserStepByStepBuilder_With_Tags extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_Tags { + public function With_FacebookId($id) { + $this->_userBuilder->With_FacebookId($id); + return new UserStepByStepBuilder_With_FacebookId($this->_userBuilder); + } +} + +class UserStepByStepBuilder_With_FacebookId extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_FacebookId +{ public function With_YearsStepArtist($years) { $this->_userBuilder->With_YearsStepArtist($years); return $this; diff --git a/Domain/Util.php b/Domain/Util.php new file mode 100644 index 0000000..1b0d555 --- /dev/null +++ b/Domain/Util.php @@ -0,0 +1,276 @@ + 'Afghanistan', + 'AX' => 'Åland Islands', + 'AL' => 'Albania', + 'DZ' => 'Algeria', + 'AS' => 'American Samoa', + 'AD' => 'Andorra', + 'AO' => 'Angola', + 'AI' => 'Anguilla', + 'AQ' => 'Antarctica', + 'AG' => 'Antigua and Barbuda', + 'AR' => 'Argentina', + 'AU' => 'Australia', + 'AT' => 'Austria', + 'AZ' => 'Azerbaijan', + 'BS' => 'Bahamas', + 'BH' => 'Bahrain', + 'BD' => 'Bangladesh', + 'BB' => 'Barbados', + 'BY' => 'Belarus', + 'BE' => 'Belgium', + 'BZ' => 'Belize', + 'BJ' => 'Benin', + 'BM' => 'Bermuda', + 'BT' => 'Bhutan', + 'BO' => 'Bolivia', + 'BA' => 'Bosnia and Herzegovina', + 'BW' => 'Botswana', + 'BV' => 'Bouvet Island', + 'BR' => 'Brazil', + 'IO' => 'British Indian Ocean Territory', + 'BN' => 'Brunei Darussalam', + 'BG' => 'Bulgaria', + 'BF' => 'Burkina Faso', + 'BI' => 'Burundi', + 'KH' => 'Cambodia', + 'CM' => 'Cameroon', + 'CA' => 'Canada', + 'CV' => 'Cape Verde', + 'KY' => 'Cayman Islands', + 'CF' => 'Central African Republic', + 'TD' => 'Chad', + 'CL' => 'Chile', + 'CN' => 'China', + 'CX' => 'Christmas Island', + 'CC' => 'Cocos (Keeling) Islands', + 'CO' => 'Colombia', + 'KM' => 'Comoros', + 'CG' => 'Congo', + 'CD' => 'Zaire', + 'CK' => 'Cook Islands', + 'CR' => 'Costa Rica', + 'CI' => 'Côte D\'Ivoire', + 'HR' => 'Croatia', + 'CU' => 'Cuba', + 'CY' => 'Cyprus', + 'CZ' => 'Czech Republic', + 'DK' => 'Denmark', + 'DJ' => 'Djibouti', + 'DM' => 'Dominica', + 'DO' => 'Dominican Republic', + 'EC' => 'Ecuador', + 'EG' => 'Egypt', + 'SV' => 'El Salvador', + 'GQ' => 'Equatorial Guinea', + 'ER' => 'Eritrea', + 'EE' => 'Estonia', + 'ET' => 'Ethiopia', + 'FK' => 'Falkland Islands (Malvinas)', + 'FO' => 'Faroe Islands', + 'FJ' => 'Fiji', + 'FI' => 'Finland', + 'FR' => 'France', + 'GF' => 'French Guiana', + 'PF' => 'French Polynesia', + 'TF' => 'French Southern Territories', + 'GA' => 'Gabon', + 'GM' => 'Gambia', + 'GE' => 'Georgia', + 'DE' => 'Germany', + 'GH' => 'Ghana', + 'GI' => 'Gibraltar', + 'GR' => 'Greece', + 'GL' => 'Greenland', + 'GD' => 'Grenada', + 'GP' => 'Guadeloupe', + 'GU' => 'Guam', + 'GT' => 'Guatemala', + 'GG' => 'Guernsey', + 'GN' => 'Guinea', + 'GW' => 'Guinea-Bissau', + 'GY' => 'Guyana', + 'HT' => 'Haiti', + 'HM' => 'Heard Island and Mcdonald Islands', + 'VA' => 'Vatican City State', + 'HN' => 'Honduras', + 'HK' => 'Hong Kong', + 'HU' => 'Hungary', + 'IS' => 'Iceland', + 'IN' => 'India', + 'ID' => 'Indonesia', + 'IR' => 'Iran, Islamic Republic of', + 'IQ' => 'Iraq', + 'IE' => 'Ireland', + 'IM' => 'Isle of Man', + 'IL' => 'Israel', + 'IT' => 'Italy', + 'JM' => 'Jamaica', + 'JP' => 'Japan', + 'JE' => 'Jersey', + 'JO' => 'Jordan', + 'KZ' => 'Kazakhstan', + 'KE' => 'KENYA', + 'KI' => 'Kiribati', + 'KP' => 'Korea, Democratic People\'s Republic of', + 'KR' => 'Korea, Republic of', + 'KW' => 'Kuwait', + 'KG' => 'Kyrgyzstan', + 'LA' => 'Lao People\'s Democratic Republic', + 'LV' => 'Latvia', + 'LB' => 'Lebanon', + 'LS' => 'Lesotho', + 'LR' => 'Liberia', + 'LY' => 'Libyan Arab Jamahiriya', + 'LI' => 'Liechtenstein', + 'LT' => 'Lithuania', + 'LU' => 'Luxembourg', + 'MO' => 'Macao', + 'MK' => 'Macedonia, the Former Yugoslav Republic of', + 'MG' => 'Madagascar', + 'MW' => 'Malawi', + 'MY' => 'Malaysia', + 'MV' => 'Maldives', + 'ML' => 'Mali', + 'MT' => 'Malta', + 'MH' => 'Marshall Islands', + 'MQ' => 'Martinique', + 'MR' => 'Mauritania', + 'MU' => 'Mauritius', + 'YT' => 'Mayotte', + 'MX' => 'Mexico', + 'FM' => 'Micronesia, Federated States of', + 'MD' => 'Moldova, Republic of', + 'MC' => 'Monaco', + 'MN' => 'Mongolia', + 'ME' => 'Montenegro', + 'MS' => 'Montserrat', + 'MA' => 'Morocco', + 'MZ' => 'Mozambique', + 'MM' => 'Myanmar', + 'NA' => 'Namibia', + 'NR' => 'Nauru', + 'NP' => 'Nepal', + 'NL' => 'Netherlands', + 'AN' => 'Netherlands Antilles', + 'NC' => 'New Caledonia', + 'NZ' => 'New Zealand', + 'NI' => 'Nicaragua', + 'NE' => 'Niger', + 'NG' => 'Nigeria', + 'NU' => 'Niue', + 'NF' => 'Norfolk Island', + 'MP' => 'Northern Mariana Islands', + 'NO' => 'Norway', + 'OM' => 'Oman', + 'PK' => 'Pakistan', + 'PW' => 'Palau', + 'PS' => 'Palestinian Territory, Occupied', + 'PA' => 'Panama', + 'PG' => 'Papua New Guinea', + 'PY' => 'Paraguay', + 'PE' => 'Peru', + 'PH' => 'Philippines', + 'PN' => 'Pitcairn', + 'PL' => 'Poland', + 'PT' => 'Portugal', + 'PR' => 'Puerto Rico', + 'QA' => 'Qatar', + 'RE' => 'Réunion', + 'RO' => 'Romania', + 'RU' => 'Russian Federation', + 'RW' => 'Rwanda', + 'SH' => 'Saint Helena', + 'KN' => 'Saint Kitts and Nevis', + 'LC' => 'Saint Lucia', + 'PM' => 'Saint Pierre and Miquelon', + 'VC' => 'Saint Vincent and the Grenadines', + 'WS' => 'Samoa', + 'SM' => 'San Marino', + 'ST' => 'Sao Tome and Principe', + 'SA' => 'Saudi Arabia', + 'SN' => 'Senegal', + 'RS' => 'Serbia', + 'SC' => 'Seychelles', + 'SL' => 'Sierra Leone', + 'SG' => 'Singapore', + 'SK' => 'Slovakia', + 'SI' => 'Slovenia', + 'SB' => 'Solomon Islands', + 'SO' => 'Somalia', + 'ZA' => 'South Africa', + 'GS' => 'South Georgia and the South Sandwich Islands', + 'ES' => 'Spain', + 'LK' => 'Sri Lanka', + 'SD' => 'Sudan', + 'SR' => 'Suriname', + 'SJ' => 'Svalbard and Jan Mayen', + 'SZ' => 'Swaziland', + 'SE' => 'Sweden', + 'CH' => 'Switzerland', + 'SY' => 'Syrian Arab Republic', + 'TW' => 'Taiwan, Province of China', + 'TJ' => 'Tajikistan', + 'TZ' => 'Tanzania, United Republic of', + 'TH' => 'Thailand', + 'TL' => 'Timor-Leste', + 'TG' => 'Togo', + 'TK' => 'Tokelau', + 'TO' => 'Tonga', + 'TT' => 'Trinidad and Tobago', + 'TN' => 'Tunisia', + 'TR' => 'Turkey', + 'TM' => 'Turkmenistan', + 'TC' => 'Turks and Caicos Islands', + 'TV' => 'Tuvalu', + 'UG' => 'Uganda', + 'UA' => 'Ukraine', + 'AE' => 'United Arab Emirates', + 'GB' => 'United Kingdom', + 'US' => 'United States', + 'UM' => 'United States Minor Outlying Islands', + 'UY' => 'Uruguay', + 'UZ' => 'Uzbekistan', + 'VU' => 'Vanuatu', + 'VE' => 'Venezuela', + 'VN' => 'Viet Nam', + 'VG' => 'Virgin Islands, British', + 'VI' => 'Virgin Islands, U.S.', + 'WF' => 'Wallis and Futuna', + 'EH' => 'Western Sahara', + 'YE' => 'Yemen', + 'ZM' => 'Zambia', + 'ZW' => 'Zimbabwe', + ); + + public static function countryNameToCode($name) + { + return array_search($name, self::$_countryCodes); + } + + public static function countryNameFromLatLong($lat, $long) + { + $url = 'http://maps.google.com/maps/api/geocode/json?address=' . $lat .',' . $long .'&sensor=false'; + $get = file_get_contents($url); + $geoData = json_decode($get); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \InvalidArgumentException('Invalid geocoding results'); + } + + if(isset($geoData->results[0])) { + foreach($geoData->results[0]->address_components as $addressComponent) { + if(in_array('country', $addressComponent->types)) { + return $addressComponent->long_name; + } + } + } + return null; + } +} \ No newline at end of file diff --git a/Services/FacebookSessionFactory.php b/Services/FacebookSessionFactory.php new file mode 100644 index 0000000..cfac400 --- /dev/null +++ b/Services/FacebookSessionFactory.php @@ -0,0 +1,24 @@ +_appId = $config['appId']; + $this->_appSecret = $config['appSecret']; + } + + public function createInstance($token) + { + FacebookSession::setDefaultApplication($this->_appId, $this->_appSecret); + return new FacebookSession($token); + } +} diff --git a/Services/IFacebookSessionFactory.php b/Services/IFacebookSessionFactory.php new file mode 100644 index 0000000..7bd1c02 --- /dev/null +++ b/Services/IFacebookSessionFactory.php @@ -0,0 +1,7 @@ + '../config/DataMaps.php', 'router.maps' => '../config/Routes.php', 'db.credentials' => '../config/db.php', + 'facebook.app' => '../config/FacebookApp.php', //entites 'Domain\Entities\StepMania\ISimfile' => DI\object('Domain\Entities\StepMania\Simfile'), + 'Domain\Entities\IUserStepByStepBuilder' => DI\object('Domain\Entities\UserStepByStepBuilder'), + 'Domain\Entities\IUserBuilder' => DI\object('Domain\Entities\UserBuilder'), + 'Domain\Entities\IUserFactory' => DI\object('Domain\Entities\UserFactory'), //services 'Services\Http\IHttpResponse' => DI\object('Services\Http\HttpResponse'), @@ -16,12 +20,16 @@ return [ ->constructor(DI\link('router.maps')), 'Services\Uploads\IUploadManager' => DI\object('Services\Uploads\UploadManager'), 'Services\Uploads\IFileFactory' => DI\object('Services\Uploads\FileFactory'), + 'Services\IFacebookSessionFactory' => DI\object('Services\FacebookSessionFactory') + ->constructor(DI\link('facebook.app')), //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), + 'DataAccess\IUserRepository' => DI\object('DataAccess\UserRepository'), 'DataAccess\IDatabaseFactory' => DI\object('DataAccess\DatabaseFactory') ->constructor(DI\link('db.credentials')), 'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper') ->constructor(DI\link('datamapper.maps')), - 'DataAccess\Queries\IQueryBuilderFactory' => DI\object('DataAccess\Queries\QueryBuilderFactory') + 'DataAccess\Queries\IQueryBuilderFactory' => DI\object('DataAccess\Queries\QueryBuilderFactory'), + ]; diff --git a/config/DataMaps.php b/config/DataMaps.php index 6de6c02..dbe5157 100644 --- a/config/DataMaps.php +++ b/config/DataMaps.php @@ -40,7 +40,9 @@ return [ 'country' => DataAccess\VO('Country'), 'displayName' => DataAccess\Varchar('display_name'), 'name' => DataAccess\VO('Name'), - 'tags' => DataAccess\VOArray('Tag', 'getTags') // TODO: Make VarcharArray class + 'tags' => DataAccess\VOArray('Tag', 'getTags'), // TODO: Make VarcharArray class + 'facebookId' => DataAccess\Varchar('facebook_id'), + 'authToken' => DataAccess\Varchar('auth_token') ] ], diff --git a/config/FacebookApp.php.example b/config/FacebookApp.php.example new file mode 100644 index 0000000..8a4ae41 --- /dev/null +++ b/config/FacebookApp.php.example @@ -0,0 +1,7 @@ + 'id', + 'appSecret' => 'secret' +]; + diff --git a/config/Routes.php b/config/Routes.php index 32a4255..e3188f6 100644 --- a/config/Routes.php +++ b/config/Routes.php @@ -17,5 +17,10 @@ return [ 'methods' => ['GET'], 'controller' => 'Simfile', 'action' => 'test' + ], + + '/user/auth' => [ + 'method' => ['GET'], + 'controller' => 'UserAuth' ] ]; diff --git a/public_html/index.php b/public_html/index.php index d892c87..8ed4f0b 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -2,6 +2,7 @@ //TODO: Config this header("Access-Control-Allow-Origin: http://172.17.12.110:8000"); +header("Access-Control-Allow-Origin: http://roll.divinelegy.meeples:8000"); require_once('../vendor/autoload.php');