PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/users_controller.php

https://github.com/rogerwu99/randomizr
PHP | 1768 lines | 1376 code | 241 blank | 151 comment | 321 complexity | afbf92807913ffc6e7890d599ffa49ed MD5 | raw file
Possible License(s): MIT, LGPL-3.0
  1. <?php
  2. App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'oauth_consumer.php'));
  3. App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'OAuth.php'));
  4. App::import('Vendor', 'oauth', array('file' => 'OAuth'.DS.'OAuth2.php'));
  5. class UsersController extends AppController {
  6. var $name = 'Users';
  7. var $helpers = array('Html', 'Form', 'Ajax');
  8. var $components = array('Auth', 'Email','Paypal','Session');
  9. var $uses = array('User', 'Mail', 'Movie', 'Interest','Place','Work','School','Userprofile','Wlookup','Twitter');
  10. function index()
  11. {
  12. if(is_null($this->Auth->getUserId())){
  13. Controller::render('/deny');
  14. }
  15. else {
  16. $this->redirect(array('controller'=>'users','action'=>'view_my_profile'));
  17. }
  18. }
  19. function _login($username=null, $password=null)
  20. {
  21. if ($username && $password){
  22. $user_record_1=array();
  23. $user_record_1['Auth']['username']=$username;
  24. $user_record_1['Auth']['password']=$password;
  25. $this->Auth->authenticate_from_oauth($user_record_1['Auth']);
  26. return;
  27. }
  28. }
  29. function login()
  30. {
  31. $this->_login($this->data['Auth']['username'],$this->Auth->hasher($this->data['Auth']['password']));
  32. if ($this->Session->check('hash_value')){
  33. $this->redirect(array('controller'=>'beta','action'=>'index',$this->Session->read('hash_value')));
  34. }
  35. else {
  36. $this->redirect(array('action'=>'view_my_profile'));
  37. }
  38. }
  39. function register($step=null)
  40. {
  41. if (!empty($this->data)){
  42. if ($this->data['User']['step']==1){
  43. $email = $this->data['User']['email'];
  44. $name=$this->data['User']['screen_name'];
  45. $password = $this->data['User']['new_password'];
  46. $confirm =$this->data['User']['confirm_password'];
  47. $accept = $this->data['User']['accept'];
  48. $this->data=array();
  49. $this->User->create();
  50. $this->data['User']['screen_name']=strtolower($name);
  51. $this->data['User']['email'] = (string) $email;
  52. $this->data['User']['new_password']=$password;
  53. $this->data['User']['confirm_password']=$confirm;
  54. $this->data['User']['accept']=$accept;
  55. $password = $this->data['User']['password'] = $this->Auth->hasher($password);
  56. $username = $this->data['User']['username']= (string) $email;
  57. $this->data['User']['path']='default.png';
  58. $this->User->set($this->data);
  59. if ($this->User->validates()){
  60. $this->User->save();
  61. $this->_login($username,$password);
  62. $this->set('step',2);
  63. }
  64. else {
  65. $this->set('errors', $this->User->validationErrors);
  66. unset($this->data['User']['new_password']);
  67. unset($this->data['User']['confirm_password']);
  68. $this->set('step',1);
  69. }
  70. }
  71. }
  72. elseif ($step==2){
  73. $this->set('step',2);
  74. }
  75. elseif ($step==3){ //after fb auth
  76. list ($master, $fb_data, $fb_movies, $fb_user_likes) = $this->getFacebookData();
  77. $this->set('master',$fb_user_likes);
  78. $this->set('step',3);
  79. //,$fb_user_likes,$fb_data,$fb_movies);
  80. }
  81. else {
  82. $this->set('step',1);
  83. $this->render();
  84. }
  85. }
  86. function logout()
  87. {
  88. $user=$this->Auth->getUserInfo();
  89. $this->Session->destroy();
  90. if(!empty($session)){
  91. $this->Auth->logout($url);
  92. }
  93. else {
  94. $this->Auth->logout();
  95. }
  96. }
  97. private function createConsumer($type) {
  98. switch ($type) {
  99. case 'facebook':
  100. return new OAuth_Consumer('189267044425329','b127d742f40502d8a9c05b31d6acc43b');
  101. }
  102. }
  103. function getOAuth($service=NULL){
  104. $consumer = $this->createConsumer($service);
  105. $redirect_url = '';
  106. switch ($service){
  107. case 'facebook':
  108. $redirect_url = 'https://www.facebook.com/dialog/oauth?client_id=189267044425329&redirect_uri='.ROOT_URL.'/users/callback/facebook'.'&scope=user_about_me,user_activities,user_birthday,user_education_history,user_events,user_groups,user_hometown,user_interests,user_relationships,user_religion_politics,user_status,user_website,user_work_history,email,user_checkins,user_likes,friends_likes,friends_interests,friends_checkins,friends_activities,friends_work_history,friends_relationship_details,friends_website,friends_religion_politics,friends_relationships,friends_location,friends_relationship_details,friends_hometown,friends_education_history,friends_birthday,friends_about_me';
  109. break;
  110. }
  111. $this->redirect($redirect_url);
  112. }
  113. function callback($service=NULL){
  114. $consumer = $this->createConsumer($service);
  115. $requestTokenName = $service.'_request_token';
  116. $accessTokenName = $service.'_access_token';
  117. $accessKeyName = $service.'_access_key';
  118. $accessSecretName = $service.'_access_secret';
  119. $access_url = '';
  120. switch ($service){
  121. case 'facebook':
  122. $access_url = 'https://graph.facebook.com/oauth/access_token?client_id=189267044425329&redirect_uri='.ROOT_URL.'/users/callback/facebook&client_secret=b127d742f40502d8a9c05b31d6acc43b&code='.$this->params['url']['code'];
  123. break;
  124. }
  125. // echo 'done';
  126. $this->User->read(null,$this->Auth->getUserId());
  127. $accessToken = file_get_contents($access_url);
  128. if ($service=='facebook'){
  129. $this->data['User']['facebook_access_key'] = $accessToken;
  130. $this->Session->write('facebook_access_key',$accessToken);
  131. }
  132. // echo 'saved';
  133. $this->User->save($this->data);
  134. //$this->new_data($service);
  135. //echo 'pull data';
  136. $this->getFacebookData();
  137. $this->redirect(array('action'=>'view_my_friends/50'));
  138. }
  139. function getFacebookData(){
  140. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  141. $accessToken = $this->Session->read('facebook_access_key');
  142. $fb_user = json_decode(file_get_contents('https://graph.facebook.com/me?' . $accessToken));
  143. $name = preg_split('/[\f\n\r\t\v ]/',$fb_user->name);
  144. $last_name = '';
  145. $first_name = '';
  146. for ($counter=0;$counter<sizeof($name);$counter++){
  147. if($counter==(sizeof($name)-1)) {
  148. $last_name = $name[$counter];
  149. }
  150. else {
  151. $first_name .= $name[$counter];
  152. }
  153. }
  154. if (empty($user['Userprofile']['id'])){
  155. $this->Userprofile->create();
  156. }
  157. else {
  158. $this->Userprofile->read(null,$user['Userprofile']['id']);
  159. }
  160. if (sizeof($name) == 1) $this->data['Userprofile']['first_name'] = $name[0];
  161. else {
  162. $this->data['Userprofile']['first_name']=ucwords($first_name);
  163. $this->data['Userprofile']['last_name'] = ucwords($last_name);
  164. }
  165. $this->data['Userprofile']['hometown']= $fb_user->hometown->name;
  166. $this->data['Userprofile']['birthday']=date("Y-m-d H:i:s", strtotime($fb_user->birthday));
  167. $this->data['Userprofile']['gender']=$fb_user->gender;
  168. $this->data['Userprofile']['location']=$fb_user->location->name;
  169. $this->data['Userprofile']['relationship']=$fb_user->relationship_status;
  170. $this->data['Userprofile']['religion']=$fb_user->religion;
  171. $this->data['Userprofile']['political']=$fb_user->political;
  172. $this->data['Userprofile']['user_id']=$this->Auth->getUserId();
  173. $this->Userprofile->set($this->data);
  174. $this->Userprofile->save();
  175. $this->User->read(null,$this->Auth->getUserId());
  176. $this->data['User']['name']=$this->data['Userprofile']['first_name'].' '.$this->data['Userprofile']['last_name'];
  177. $this->User->set($this->data);
  178. $this->User->save();
  179. // echo 'Userprofile saved';
  180. $fb_user_likes = json_decode(file_get_contents('https://graph.facebook.com/me/likes?'.$accessToken));
  181. $fb_user_activities = json_decode(file_get_contents('https://graph.facebook.com/me/activities?'.$accessToken));
  182. $fb_user_interests = json_decode(file_get_contents('https://graph.facebook.com/me/interests?'.$accessToken));
  183. if (empty($user['Interest']['id'])){
  184. $this->Interest->create();
  185. }
  186. else {
  187. $this->Interest->read(null,$user['Interest']['id']);
  188. }
  189. $this->data['Interest']['likes']=json_encode($fb_user_likes->data);
  190. $this->data['Interest']['activities']=json_encode($fb_user_activities->data);
  191. $this->data['Interest']['interests']=json_encode($fb_user_interests->data);
  192. $this->data['Interest']['user_id']=$this->Auth->getUserId();
  193. $this->Interest->set($this->data);
  194. $this->Interest->save();
  195. // echo 'interests saved';
  196. if (empty($user['Work']['id'])){
  197. $this->Work->create();
  198. }
  199. else {
  200. $this->Work->read(null,$user['Work']['id']);
  201. }
  202. $this->data['Work']['body'] = json_encode($fb_user->work);
  203. $this->Work->set($this->data);
  204. $this->Work->save();
  205. // echo 'work saved';
  206. if (empty($user['School']['id'])){
  207. $this->School->create();
  208. }
  209. else {
  210. $this->School->read(null,$user['School']['id']);
  211. }
  212. $this->data['School']['body'] = json_encode($fb_user->education);
  213. $this->School->set($this->data);
  214. $this->School->save();
  215. // echo 'school saved';
  216. $fb_user_checkins = json_decode(file_get_contents('https://graph.facebook.com/me/checkins?'.$accessToken));
  217. if (empty($user['Place']['id'])){
  218. $this->Place->create();
  219. }
  220. else {
  221. $this->Place->read(null,$user['Place']['id']);
  222. }
  223. $this->data['Place']['body'] = json_encode($fb_user_checkins->data);
  224. $this->Place->set($this->data);
  225. $this->Place->save();
  226. }
  227. function new_data($type){
  228. switch ($type){
  229. case 'facebook':
  230. $this->getFacebookData();
  231. break;
  232. }
  233. }
  234. function friends($flag=false){
  235. /*if (!$flag){
  236. $this->Session->write('friends','true');
  237. $this->getOAuth('facebook');
  238. }
  239. else {*/
  240. $count=0;
  241. $friend_array = array();
  242. $accessToken = $this->Session->read('facebook_access_key');
  243. $friend_url = json_decode(file_get_contents('https://graph.facebook.com/me/friends?' . $accessToken));
  244. // var_dump($friend_url);
  245. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  246. $control = json_decode($user['Interest']['likes']);
  247. for ($counter=0;$counter<sizeof($friend_url->data); $counter++){
  248. $likes = json_decode(file_get_contents('https://graph.facebook.com/'.$friend_url->data[$counter]->id.'/likes?'.$accessToken));
  249. $intersect = array();
  250. for ($mid_counter = 0; $mid_counter<sizeof($control);$mid_counter++){
  251. for($inner_counter=0;$inner_counter<sizeof($likes->data);$inner_counter++){
  252. if ($control[$mid_counter]->id == $likes->data[$inner_counter]->id){
  253. array_push($intersect,$control[$mid_counter]);
  254. }
  255. }
  256. }
  257. if (!empty($intersect)){
  258. $friend_array[$count]->person = $friend_url->data[$counter];
  259. $friend_array[$count]->likes = $intersect;
  260. $friend_array[$count]->pic = 'http://graph.facebook.com/'.$friend_url->data[$counter]->id.'/picture';
  261. }
  262. }
  263. //$this->redirect(array('action'=>'view_my_profile'));
  264. return $friend_array;
  265. //}
  266. }
  267. function getFriends(){
  268. $friend_array = array();
  269. //$count =0;
  270. $user = $this->Auth->getUserInfo();
  271. $friend_url = json_decode(file_get_contents('https://graph.facebook.com/me/friends?' . $user['facebook_access_key']));
  272. usort($friend_url->data, array(&$this, "friend_sort"));
  273. return $friend_url->data;
  274. }
  275. function friend_sort($a,$b){
  276. if ($a->name > $b->name) return 1;
  277. elseif($a->name == $b->name) return 0;
  278. else return -1;
  279. }
  280. function fbcompare($fb_id){
  281. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  282. $remote_user = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_id.'?'. $user['User']['facebook_access_key']));
  283. $remote_user_likes = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_id.'/likes?'.$user['User']['facebook_access_key']));
  284. $control = json_decode($user['Interest']['likes']);
  285. $likes = json_decode(file_get_contents('https://graph.facebook.com/'.$fb_id.'/likes?'.$user['User']['facebook_access_key']));
  286. $intersect = array();
  287. for ($mid_counter = 0; $mid_counter<sizeof($control);$mid_counter++){
  288. for($inner_counter=0;$inner_counter<sizeof($likes->data);$inner_counter++){
  289. if ($control[$mid_counter]->id == $likes->data[$inner_counter]->id){
  290. array_push($intersect,$control[$mid_counter]);
  291. }
  292. }
  293. }
  294. $this->set('user',$user);
  295. $this->set('remote_user',$remote_user);
  296. $this->set('intersect',$intersect);
  297. if (!is_null($user['Userprofile']['hometown']) && !is_null($remote_user->hometown->name)){
  298. $url = "http://where.yahooapis.com/geocode?line2=".urlencode($user['Userprofile']['hometown'])."&flags=J&gflags=R&appid=cENXMi4g";
  299. $address = json_decode(file_get_contents($url));
  300. $lat_home = $address->ResultSet->Results[0]->latitude;
  301. $long_home = $address->ResultSet->Results[0]->longitude;
  302. $url2 = "http://where.yahooapis.com/geocode?line2=".urlencode($remote_user->hometown->name)."&flags=J&gflags=R&appid=cENXMi4g";
  303. $address2 = json_decode(file_get_contents($url2));
  304. $lat_away = $address2->ResultSet->Results[0]->latitude;
  305. $long_away = $address2->ResultSet->Results[0]->longitude;
  306. $earth_radius = 6371;
  307. $delta_lat = deg2rad($lat_home - $lat_away);
  308. $delta_long = deg2rad($long_home - $long_away);
  309. $a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($lat_home)) * cos(deg2rad($lat_away)) * sin($delta_long/2) * sin($delta_long/2);
  310. $c = 2 * atan2(sqrt($a),sqrt(1-$a));
  311. $distance = $earth_radius * $c;
  312. $d_miles = $distance * 0.621371192;
  313. $this->set('d_miles',$d_miles);
  314. }
  315. if (!is_null($user['Userprofile']['birthday']) && !is_null($remote_user->birthday)){
  316. $user_sign = $this->getSign($user['Userprofile']['birthday']);
  317. $other_user_sign = $this->getSign($remote_user->birthday);
  318. $diff = abs(strtotime($user['Userprofile']['birthday']) - strtotime($remote_user->birthday));
  319. $years = floor($diff / (365*60*60*24));
  320. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  321. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
  322. if (date('Y',strtotime($remote_user->birthday)) == date('Y') || date('Y',strtotime($user['Userprofile']['birthday'])) == date('Y')){
  323. $years = 0;
  324. }
  325. $this->set('user_sign',$user_sign);
  326. $this->set('other_user_sign',$other_user_sign);
  327. $this->set('years',$years);
  328. $this->set('months',$months);
  329. $this->set('days',$days);
  330. }
  331. if (!is_null($user['Userprofile']['location']) && !is_null($remote_user->location->name)){
  332. $url = "http://where.yahooapis.com/geocode?line2=".urlencode($user['Userprofile']['location'])."&flags=J&gflags=R&appid=cENXMi4g";
  333. $address = json_decode(file_get_contents($url));
  334. $lat_home = $address->ResultSet->Results[0]->latitude;
  335. $long_home = $address->ResultSet->Results[0]->longitude;
  336. $url2 = "http://where.yahooapis.com/geocode?line2=".urlencode($remote_user->location->name)."&flags=J&gflags=R&appid=cENXMi4g";
  337. $address2 = json_decode(file_get_contents($url2));
  338. $lat_away = $address2->ResultSet->Results[0]->latitude;
  339. $long_away = $address2->ResultSet->Results[0]->longitude;
  340. $earth_radius = 6371;
  341. $delta_lat = deg2rad($lat_home - $lat_away);
  342. $delta_long = deg2rad($long_home - $long_away);
  343. $a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($lat_home)) * cos(deg2rad($lat_away)) * sin($delta_long/2) * sin($delta_long/2);
  344. $c = 2 * atan2(sqrt($a),sqrt(1-$a));
  345. $distance = $earth_radius * $c;
  346. $d_miles = $distance * 0.621371192;
  347. $this->set('l_miles',$d_miles);
  348. }
  349. // compare school and work
  350. $matches = array();
  351. $count = 0;
  352. var_dump($user['School']['body']);
  353. var_dump($remote_user->education);
  354. if (!is_null($user['School']['body']) && !is_null($remote_user->education)){
  355. $my_schools = json_decode($user['School']['body']);
  356. for ($counter = 0;$counter<sizeof($my_schools);$counter++){
  357. for ($inner_counter =0;$inner_counter<sizeof($remote_user->education); $inner_counter++){
  358. similar_text($my_schools[$counter]->school->name, $remote_user->education[$inner_counter]->school->name,$percent);
  359. if (($my_schools[$counter]->school->id==$remote_user->education[$inner_counter]->school->id &&
  360. $my_schools[$counter]->school->id!=0) || $percent > 90 ){
  361. $matches[$count]->value = $my_schools[$counter]->school;
  362. $matches[$count]->type = 'school';
  363. $count++;
  364. }
  365. if ($my_schools[$counter]->degree->id == $remote_user->education[$inner_counter]->degree->id &&
  366. $my_schools[$counter]->degree->id!=0){
  367. $matches[$count]->value = $my_schools[$counter]->degree;
  368. $matches[$count]->type = 'degree';
  369. $count++;
  370. }
  371. for ($concentration_counter = 0;$concentration_counter < sizeof($my_schools[$counter]->concentration); $concentration_counter++){
  372. if ($my_schools[$counter]->concentration[$concentration_counter]->id == $remote_user->education[$inner_counter]->concentration[$concentration_counter]->id &&
  373. $my_schools[$counter]->concentration[$concentration_counter]->id!=0){
  374. //echo 'match';
  375. $matches[$count]->value = $my_schools[$counter]->concentration[$concentration_counter];
  376. $matches[$count]->type = 'concentration';
  377. $count++;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. $this->set('school_matches',$matches);
  384. // var_dump($remote_user->work);
  385. // var_dump($user['Work']['body']);
  386. // let's do a free text search match on work as well i.e. the teddy platt problem of Bloomberg and Bloomberg LP
  387. $matches = array();
  388. if (!is_null($user['Work']['body']) && !is_null($remote_user->work)){
  389. $my_work = json_decode($user['Work']['body']);
  390. for ($counter = 0;$counter<sizeof($my_work);$counter++){
  391. for ($inner_counter =0;$inner_counter<sizeof($remote_user->work); $inner_counter++){
  392. similar_text($my_work[$counter]->employer->name, $remote_user->work[$inner_counter]->employer->name,$percent);
  393. if (($my_work[$counter]->employer->id==$remote_user->work[$inner_counter]->employer->id &&
  394. $my_work[$counter]->employer->id!=0) || $percent > 80){
  395. $match->value = $my_work[$counter]->employer;
  396. $match->type = 'employer';
  397. array_push($matches, $match);
  398. }
  399. if ($my_work[$counter]->location->id == $remote_user->work[$inner_counter]->location->id &&
  400. $my_work[$counter]->location->id!=0){
  401. $match->value = $my_work[$counter]->location;
  402. $match->type = 'location';
  403. array_push($matches, $match);
  404. }
  405. if ($my_work[$counter]->position->id == $remote_user->work[$inner_counter]->position->id &&
  406. $my_work[$counter]->position->id!=0){
  407. $match->value = $my_work[$counter]->position;
  408. $match->type = 'position';
  409. array_push($matches, $match);
  410. }
  411. }
  412. }
  413. }
  414. $this->set('work_matches',$matches);
  415. // get activities and interests
  416. }
  417. function view_my_profile(){
  418. if(is_null($this->Auth->getUserId())){
  419. Controller::render('/deny');
  420. }
  421. else {
  422. $user = $this->Auth->getUserInfo();
  423. $db_results = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  424. if (!empty($db_results)){
  425. $movie_data = (isset($db_results['Movie']['body'])) ? true : false;
  426. }
  427. $this->set('pic',$db_results['User']['path']);
  428. if (!empty($db_results['Movie']['body'])){
  429. $movies = json_decode($db_results['Movie']['body']);
  430. $top_movies = array();
  431. foreach ($movies as $key=>$value){
  432. array_push($top_movies,$key);
  433. }
  434. $this->set('top_movies',$top_movies);
  435. }
  436. if (!empty($db_results['Interest']['body'])){
  437. $interests = json_decode($db_results['Interest']['body']);
  438. $top_interests = array();
  439. foreach ($interests as $key=>$value){
  440. array_push($top_interests,$key);
  441. }
  442. $this->set('top_interests',$top_interests);
  443. }
  444. if (!empty($db_results['Place']['locations'])){
  445. $locations = json_decode($db_results['Place']['locations']);
  446. $top_locations = array();
  447. foreach ($locations as $key=>$value){
  448. array_push($top_locations,$key);
  449. }
  450. $this->set('top_locations',$top_locations);
  451. }
  452. if (!empty($db_results['Place']['categories'])){
  453. $categories = json_decode($db_results['Place']['categories']);
  454. $top_categories = array();
  455. //var_dump($db_results['Place']['categories']);
  456. foreach ($categories as $key=>$value){
  457. array_push($top_categories,$key);
  458. }
  459. $this->set('top_categories',$top_categories);
  460. }
  461. if (!empty($db_results['Interest']['you_body'])){
  462. $you = json_decode($db_results['Interest']['you_body']);
  463. $top_you = array();
  464. foreach ($you as $key=>$value){
  465. array_push($top_you,$key);
  466. }
  467. $this->set('top_you',$top_you);
  468. }
  469. if (!empty($db_results['Work']['body'])){
  470. $work = json_decode($db_results['Work']['body']);
  471. }
  472. if (!empty($db_results['Work']['titles'])){
  473. $work = json_decode($db_results['Work']['titles']);
  474. foreach ($work as $key=>$value){
  475. $this->set('titles',$key);
  476. break;
  477. }
  478. }
  479. if (!empty($db_results['Work']['industries'])){
  480. $work = json_decode($db_results['Work']['industries'],true);
  481. $first = true;
  482. arsort($work, SORT_NUMERIC);
  483. $top_industries = array();
  484. foreach ($work as $key=>$value){
  485. if ($first){
  486. $this->set('industries',$key);
  487. $first = false;
  488. }
  489. array_push($top_industries, $key);
  490. }
  491. $this->set('top_industries',$top_industries);
  492. }
  493. if (!empty($db_results['School']['body'])){
  494. $school = json_decode($db_results['School']['body'],true);
  495. $parsable_array = array();
  496. $schools =array();
  497. $areas_of_focus = array();
  498. for ($counter = 0;$counter<sizeof($school);$counter++){
  499. foreach($school[$counter] as $key=>$value){
  500. if ($key == 'degree') $parsable_array[$key].=$value;
  501. if ($key == 'school') array_push($schools,$value);
  502. if ($key == 'major') array_push($areas_of_focus,$value);
  503. }
  504. }
  505. $master_degree = false;
  506. $bach_degree = false;
  507. $doctor_degree = false;
  508. $doctor_array = array('/\b(?i)phd*\b/','/\bdoctor*\b/');
  509. $master_array = array('/\bMS\b/','/\b(?i)master*/');
  510. $bach_array = array('/\b(?i)bachelor*\b/','/\bBS\b/');
  511. preg_replace($doctor_array,'',$parsable_array['degree'],-1,$doctor);
  512. if ($doctor>0) $doctor_degree = true;
  513. preg_replace($master_array,'',$parsable_array['degree'],-1,$masters);
  514. if ($masters>0) $master_degree = true;
  515. preg_replace($bach_array,'',$parsable_array['degree'],-1,$bachelors);
  516. if ($bachelors>0) $bach_degree = true;
  517. $this->set('bach_degree',$bach_degree);
  518. $this->set('master_degree',$master_degree);
  519. $this->set('areas_of_focus',$areas_of_focus);
  520. $this->set('schools',$schools);
  521. }
  522. //list($your_interests,$aboutme) = $this->getCategories($db_results['Interest']['body']);
  523. //$this->set('your_interests',$your_interests);
  524. //$this->set(compact('aboutme'));
  525. /* $books = array();
  526. if (!empty($db_results['Interest']['likes'])){
  527. $likes = json_decode($db_results['Interest']['likes']);
  528. for ($counter =0;$counter<sizeof($likes);$counter++){
  529. if ($likes[$counter]->category == "Book"){
  530. array_push($books , $likes[$counter]);
  531. }
  532. }
  533. }
  534. */
  535. $this->set('user',$db_results);
  536. }
  537. // for travel look at 4sq for outside of the country
  538. }
  539. function view_my_friends($limit){
  540. $user = $this->Auth->getUserInfo();
  541. $this->set('pic',$user['path']);
  542. $friend_array = $this->getFriends();
  543. $this->set('friends',$friend_array);
  544. $this->set('start',$limit-50);
  545. $this->set('limit',$limit);
  546. }
  547. function getFacebookInterests($tag_cloud,$fb_user_likes){
  548. $fb_interests=array();
  549. for($counter=0;$counter<sizeof($fb_user_likes->data);$counter++){
  550. //if($fb_user_likes->data[$counter]->category == "Interest"){
  551. array_push($fb_interests,$fb_user_likes->data[$counter]->name);
  552. //}
  553. }
  554. // weighting is heavier because of explicit interest on fb
  555. for($counter=0;$counter<sizeof($fb_interests);$counter++){
  556. if (!isset($tag_cloud[$fb_interests[$counter]])){
  557. $tag_cloud[$fb_interests[$counter]]=5;
  558. }
  559. else $tag_cloud[$fb_interests[$counter]]+=5;
  560. }
  561. return $tag_cloud;
  562. }
  563. function scrub_interests($tag_cloud,$mu_interests,$tw_following=NULL,$tw_lists=NULL){
  564. $sizes = array(sizeof($mu_interests),sizeof($tw_following),sizeof($tw_lists));
  565. arsort($sizes,SORT_NUMERIC);
  566. if (!is_null($tw_lists) && !is_null($tw_following)){
  567. if ($sizes[0]==sizeof($mu_interests)) $small=$mu_interests;
  568. elseif ($sizes[0]==sizeof($tw_following)) $small=$tw_following;
  569. else $small=$tw_lists;
  570. if ($sizes[1]==sizeof($mu_interests)) $med=$mu_interests;
  571. elseif ($sizes[1]==sizeof($tw_following)) $med=$tw_following;
  572. else $med=$tw_lists;
  573. if ($sizes[2]==sizeof($mu_interests)) $large=$mu_interests;
  574. elseif ($sizes[2]==sizeof($tw_following)) $large=$tw_following;
  575. else $large=$tw_lists;
  576. $sort_size=3;
  577. }
  578. elseif (!is_null($tw_following)) {
  579. if (sizeof($mu_interests)<sizeof($tw_following)) {
  580. $med=$mu_interests;
  581. $large=$tw_following;
  582. }
  583. else {
  584. $med=$tw_following;
  585. $large=$mu_interests;
  586. }
  587. $sort_size=2;
  588. }
  589. else {
  590. $large=$mu_interests;
  591. $sort_size=1;
  592. }
  593. if ($sort_size>0){
  594. //echo $sizes[0]. ' SIZES';
  595. //for($counter=$sizes[1];$counter<$sizes[2];$counter++){
  596. for($counter=0;$counter<$sizes[0];$counter++){
  597. $interest_string_lge = preg_split('/[,? ]+/',$large[$counter]->description);
  598. $interest_string_lge = preg_replace($this->stop_words,'',$interest_string_lge);
  599. //$interest_string_lge = preg_split('/[,? ]+/',$interest_string_lge);
  600. $large_in = $interest_string_lge;
  601. for ($inner_counter=0;$inner_counter<sizeof($interest_string_lge);$inner_counter++){
  602. if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
  603. //echo $large_in[$inner_counter];
  604. $tag_cloud[strtolower($large_in[$inner_counter])]=1;
  605. }
  606. else $tag_cloud[strtolower($large_in[$inner_counter])]=$tag_cloud[strtolower($large_in[$inner_counter])]+1;
  607. }
  608. }
  609. }
  610. if ($sort_size>1){
  611. for($counter=$sizes[0];$counter<$sizes[1];$counter++){
  612. $interest_string_med = preg_split('/[,? ]+/',$med[$counter]->description);
  613. $interest_string_med = preg_replace($this->stop_words,'',$interest_string_med);
  614. $interest_string_lge = preg_split('/[,? ]+/',$large[$counter]->description);
  615. $interest_string_lge = preg_replace($this->stop_words,'',$interest_string_lge);
  616. $inner_loop_strings = array(sizeof($interest_string_med),sizeof($interest_string_lge));
  617. sort($inner_loop_strings,SORT_NUMERIC);
  618. if ($inner_loop_strings[0]==sizeof($interest_string_med)) {
  619. $med_in=$interest_string_med;
  620. $large_in=$interest_string_lge;
  621. }
  622. else {
  623. $med_in=$interest_string_lge;
  624. $large_in=$interest_string_med;
  625. }
  626. for ($inner_counter=0;$inner_counter<$inner_loop_strings[0];$inner_counter++){
  627. if (!isset($tag_cloud[strtolower($med_in[$inner_counter])])){
  628. $tag_cloud[strtolower($med_in[$inner_counter])]=1;
  629. }
  630. else $tag_cloud[strtolower($med_in[$inner_counter])]++;
  631. if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
  632. $tag_cloud[strtolower($large_in[$inner_counter])]=1;
  633. }
  634. else $tag_cloud[strtolower($large_in[$inner_counter])]++;
  635. }
  636. for ($inner_counter=$inner_loop_strings[0];$inner_counter<$inner_loop_strings[1];$inner_counter++){
  637. if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
  638. $tag_cloud[strtolower($large_in[$inner_counter])]=1;
  639. }
  640. else $tag_cloud[strtolower($large_in[$inner_counter])]++;
  641. }
  642. }
  643. }
  644. if ($sort_size>2){
  645. for($counter=$sizes[1];$counter<$sizes[2];$counter++){
  646. $interest_string_mu = preg_split('/[,? ]+/',$small[$counter]);
  647. $interest_string_mu = preg_replace($this->stop_words,'',$interest_string_mu->description);
  648. $interest_string_twf = preg_split('/[,? ]+/',$med[$counter]->description);
  649. $interest_string_twf = preg_replace($this->stop_words,'',$interest_string_twf);
  650. $interest_string_twl = preg_split('/[,? ]+/',$large[$counter]->description);
  651. $interest_string_twl = preg_replace($this->stop_words,'',$interest_string_twl);
  652. $inner_loop_strings = array(sizeof($interest_string_mu),sizeof($interest_string_twf),sizeof($interest_string_twl));
  653. sort($inner_loop_strings,SORT_NUMERIC);
  654. if ($inner_loop_strings[0]==sizeof($interest_string_mu)) $small_in=$interest_string_mu;
  655. elseif ($inner_loop_strings[0]==sizeof($interest_string_twf)) $small_in=$interest_string_twf;
  656. else $small_in=$interest_string_twl;
  657. if ($inner_loop_strings[1]==sizeof($interest_string_mu)) $med_in=$interest_string_mu;
  658. elseif ($inner_loop_strings[1]==sizeof($interest_string_twf)) $med_in=$interest_string_twf;
  659. else $med_in=$interest_string_twl;
  660. if ($inner_loop_strings[2]==sizeof($interest_string_mu)) $large_in=$interest_string_mu;
  661. elseif ($inner_loop_strings[2]==sizeof($interest_string_twf)) $large_in=$interest_string_twf;
  662. else $large_in=$interest_string_twl;
  663. for ($inner_counter=0;$inner_counter<$inner_loop_strings[0];$inner_counter++){
  664. if (!isset($tag_cloud[strtolower($interest_string_mu[$inner_counter])])){
  665. $tag_cloud[strtolower($interest_string_mu[$inner_counter])]=1;
  666. }
  667. else $tag_cloud[strtolower($interest_string_mu[$inner_counter])]++;
  668. if (!isset($tag_cloud[strtolower($interest_string_twf[$inner_counter])])){
  669. $tag_cloud[strtolower($interest_string_twf[$inner_counter])]=1;
  670. }
  671. else $tag_cloud[strtolower($interest_string_twf[$inner_counter])]++;
  672. if (!isset($tag_cloud[strtolower($interest_string_twl[$inner_counter])])){
  673. $tag_cloud[strtolower($interest_string_twl[$inner_counter])]=1;
  674. }
  675. else $tag_cloud[strtolower($interest_string_twl[$inner_counter])]++;
  676. }
  677. for ($inner_counter=$inner_loop_strings[0];$inner_counter<$inner_loop_strings[1];$inner_counter++){
  678. if (!isset($tag_cloud[strtolower($med_in[$inner_counter])])){
  679. $tag_cloud[strtolower($med_in[$inner_counter])]=1;
  680. }
  681. else $tag_cloud[strtolower($med_in[$inner_counter])]++;
  682. if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
  683. $tag_cloud[strtolower($large_in[$inner_counter])]=1;
  684. }
  685. else $tag_cloud[strtolower($large_in[$inner_counter])]++;
  686. }
  687. for ($inner_counter=$inner_loop_strings[1];$inner_counter<$inner_loop_strings[2];$inner_counter++){
  688. if (!isset($tag_cloud[strtolower($large_in[$inner_counter])])){
  689. $tag_cloud[strtolower($large_in[$inner_counter])]=1;
  690. }
  691. else $tag_cloud[strtolower($large_in[$inner_counter])]++;
  692. }
  693. }
  694. }
  695. // $lists_f[$counter]->name = $tw_user->lists[$counter]->name;
  696. // $lists_f[$counter]->description = $tw_user->lists[$counter]->description;
  697. arsort($tag_cloud, SORT_NUMERIC);
  698. unset($tag_cloud['']);
  699. unset($tag_cloud[' ']);
  700. // var_dump($tag_cloud);
  701. return $tag_cloud;
  702. }
  703. function like_sort($a,$b){
  704. if ($a['category'] > $b['category']) return -1;
  705. elseif ($a['category'] == $b['category']) return 0;
  706. else return 1;
  707. }
  708. function edit($type=null){
  709. if(is_null($this->Auth->getUserId())){
  710. Controller::render('/deny');
  711. }
  712. else {
  713. if (!empty($this->data)) {
  714. if ($this->data['User']['new_password']!='' && $this->data['User']['new_password']==$this->data['User']['confirm_password']){
  715. $this->data['User']['password'] = $this->Auth->hasher($this->data['User']['new_password']);
  716. }
  717. $this->data['User']['screen_name']=strtolower($this->data['User']['screen_name']);
  718. switch ($type){
  719. }
  720. $username=$this->User->read(null,$this->Auth->getUserId());
  721. $this->User->set($this->data);
  722. if ($this->User->validates()){
  723. $this->User->save();
  724. $this->_login($username['User']['email'],$username['User']['password']);
  725. $this->redirect(array( 'action'=>'view_my_profile'));
  726. }
  727. else {
  728. $this->set('errors', $this->User->validationErrors);
  729. }
  730. }
  731. else {
  732. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  733. // let's sort this by category
  734. $interest = json_decode($user['Interest']['likes'],true);
  735. usort($interest, array(&$this, "like_sort"));
  736. // var_dump($interest);
  737. $aspirations = json_decode($user['Interest']['body'],true);
  738. $work = json_decode($user['Work']['body'],true);
  739. $schools = json_decode($user['School']['body'],true);
  740. $this->set(compact('schools'));
  741. $this->set(compact('work'));
  742. $this->set(compact('aspirations'));
  743. $this->set(compact('interest'));
  744. $this->set(compact('user'));
  745. $relationship = array(
  746. 'Single'=>'Single',
  747. 'In a relationship'=>'In a relationship',
  748. 'Engaged'=>'Engaged',
  749. 'Married'=>'Married',
  750. 'Widowed'=>'Widowed',
  751. 'Separated'=>'Separated',
  752. 'Divorced'=>'Divorced'
  753. );
  754. $this->set(compact('relationship'));
  755. $months = array(
  756. "Jan"=>"Jan",
  757. "Feb"=>"Feb",
  758. "Mar"=>"Mar",
  759. "Apr"=>"Apr",
  760. "May"=>"May",
  761. "Jun"=>"Jun",
  762. "Jul"=>"Jul",
  763. "Aug"=>"Aug",
  764. "Sep"=>"Sep",
  765. "Oct"=>"Oct",
  766. "Nov"=>"Nov",
  767. "Dec"=>"Dec"
  768. );
  769. $this->set(compact('months'));
  770. $this->set('dates',range(1,31));
  771. $this->set('years',range(1900,(int)date('Y')-18));
  772. }
  773. }
  774. }
  775. function edit_interests($type){
  776. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  777. if ($type==1){
  778. // need to pull new data
  779. $interest = json_decode($user['Interest']['likes'],true);
  780. // var_dump($this->data);
  781. $new_array = array();
  782. for ($counter=0;$counter<sizeof($interest);$counter++){
  783. $name = 'delete_'.$counter;
  784. if (!$this->data['User'][$name]){
  785. array_push($new_array,$interest[$counter]);
  786. }
  787. }
  788. $this->data['Interest']['likes'] = json_encode($new_array);
  789. $this->Interest->read(null,$user['Interest']['id']);
  790. $this->Interest->set($this->data);
  791. $this->Interest->save();
  792. // if the registration cookie is set then go thru each page
  793. //otherwise redirect to view my profile
  794. //var_dump($this->data);
  795. }
  796. elseif ($type==2){
  797. $aspirations = json_decode($user['Interest']['body'],true);
  798. $new_array = array();
  799. foreach ($aspirations as $key=>$value){
  800. $name = 'delete_'.$key;
  801. if (!$this->data['User'][$name]){
  802. $new_array[$key]=$aspirations[$key];
  803. }
  804. }
  805. $this->data['Interest']['body'] = json_encode($new_array);
  806. $this->Interest->read(null,$user['Interest']['id']);
  807. $this->Interest->set($this->data);
  808. $this->Interest->save();
  809. //var_dump($this->data);
  810. }
  811. $this->redirect(array('action'=>'view_my_profile'));
  812. }
  813. function edit_pic($service){
  814. if(is_null($this->Auth->getUserId())){
  815. Controller::render('/deny');
  816. }
  817. else {
  818. //echo 'hihihi';
  819. $user = $this->Auth->getUserInfo();
  820. $consumer = $this->createConsumer($service);
  821. $access_key = $service.'_access_key';
  822. $access_secret = $service.'_access_secret';
  823. switch ($service){
  824. case 'linkedin':
  825. $pic_url = $consumer->get($user[$access_key],$user[$access_secret],'http://api.linkedin.com/v1/people/~:(picture-url)', array());;
  826. $li_user = simplexml_load_string($pic_url);
  827. // var_dump($li_user->children());
  828. // $pic = $li_user->children[0];
  829. foreach($li_user->children() as $child){
  830. if ($child->getName()=='picture-url'){
  831. $pic = $child;
  832. }
  833. }
  834. //}
  835. break;
  836. case 'foursquare':
  837. $fs_user = json_decode(file_get_contents('https://api.foursquare.com/v2/users/self?oauth_token='.$user['foursquare_access_token']));
  838. $pic = $fs_user->response->user->photo;
  839. break;
  840. case 'meetup':
  841. $getData = array('relation'=>'self',
  842. 'sess'=>'oauth_session');
  843. $pic_url = $consumer->get($user[$access_key],$user[$access_secret],'https://api.meetup.com/members.json',$getData);
  844. $mu_user = json_decode($pic_url);
  845. // var_dump($mu_user);
  846. $pic = $mu_user->results[0]->photo_url;
  847. break;
  848. case 'facebook':
  849. $fb_user = json_decode(file_get_contents('https://graph.facebook.com/me?' . $user['facebook_access_key']));
  850. $pic = 'http://graph.facebook.com/'.$fb_user->id.'/picture';
  851. break;
  852. case 'twitter':
  853. $pic_url = $consumer->get($user[$access_key],$user[$access_secret],'http://api.twitter.com/1/account/verify_credentials.json', array());;
  854. $tw_user = json_decode($pic_url);
  855. $pic = $tw_user->profile_image_url;
  856. break;
  857. }
  858. //var_dump($user);
  859. $this->User->read(null,$this->Auth->getUserId());
  860. $this->data['User']['path']=$pic;
  861. $this->User->set($this->data);
  862. $this->User->save();
  863. $this->redirect(array('action'=>'view_my_profile'));
  864. }
  865. }
  866. function view($screen_name = null){
  867. $user = $this->Auth->getUserInfo();
  868. $screen_name = strtolower($screen_name);
  869. if ($screen_name == $user['screen_name']){
  870. $this->set('own_profile', true);
  871. }
  872. else $this->set('own_profile',false);
  873. $db_results = $this->User->find('first', array('conditions' => (array('User.screen_name'=>$screen_name))));
  874. //var_dump($db_results);
  875. if (empty($db_results)){
  876. $this->set('exists',false);
  877. }
  878. else{
  879. $this->set('exists',true);
  880. $this->set('pic',$db_results['User']['path']);
  881. if (!empty($db_results['Movie']['body'])){
  882. $movies = json_decode($db_results['Movie']['body']);
  883. $top_movies = array();
  884. foreach ($movies as $key=>$value){
  885. array_push($top_movies,$key);
  886. }
  887. $this->set('top_movies',$top_movies);
  888. }
  889. if (!empty($db_results['Interest']['body'])){
  890. $interests = json_decode($db_results['Interest']['body']);
  891. $top_interests = array();
  892. foreach ($interests as $key=>$value){
  893. array_push($top_interests,$key);
  894. }
  895. $this->set('top_interests',$top_interests);
  896. }
  897. if (!empty($db_results['Interest']['likes'])){
  898. $interests = json_decode($db_results['Interest']['likes']);
  899. $hobbies = array();
  900. foreach ($interests as $key=>$value){
  901. array_push($hobbies,$key);
  902. }
  903. $this->set('top_hobbies',$hobbies);
  904. }
  905. if (!empty($db_results['Place']['locations'])){
  906. $locations = json_decode($db_results['Place']['locations']);
  907. $top_locations = array();
  908. foreach ($locations as $key=>$value){
  909. array_push($top_locations,$key);
  910. }
  911. $this->set('top_locations',$top_locations);
  912. }
  913. if (!empty($db_results['Place']['categories'])){
  914. $categories = json_decode($db_results['Place']['categories']);
  915. $top_categories = array();
  916. foreach ($categories as $key=>$value){
  917. array_push($top_categories,$key);
  918. }
  919. $this->set('top_categories',$top_categories);
  920. }
  921. if (!empty($db_results['Interest']['you_body'])){
  922. $you = json_decode($db_results['Interest']['you_body']);
  923. $top_you = array();
  924. foreach ($you as $key=>$value){
  925. array_push($top_you,$key);
  926. }
  927. $this->set('top_you',$top_you);
  928. }
  929. if (!empty($db_results['Work']['body'])){
  930. $work = json_decode($db_results['Work']['body']);
  931. }
  932. if (!empty($db_results['Work']['titles'])){
  933. $work = json_decode($db_results['Work']['titles']);
  934. foreach ($work as $key=>$value){
  935. $this->set('titles',$key);
  936. break;
  937. }
  938. }
  939. if (!empty($db_results['Work']['industries'])){
  940. $work = json_decode($db_results['Work']['industries'],true);
  941. $first = true;
  942. arsort($work, SORT_NUMERIC);
  943. $top_industries = array();
  944. foreach ($work as $key=>$value){
  945. if ($first){
  946. $this->set('industries',$key);
  947. $first = false;
  948. }
  949. array_push($top_industries, $key);
  950. }
  951. $this->set('top_industries',$top_industries);
  952. }
  953. if (!empty($db_results['School']['body'])){
  954. $school = json_decode($db_results['School']['body']);
  955. $parsable_array = array();
  956. $schools =array();
  957. $areas_of_focus = array();
  958. for ($counter = 0;$counter<sizeof($school);$counter++){
  959. foreach($school[$counter] as $key=>$value){
  960. if ($key == 'degree') {
  961. if (!isset($parsable_array['degree'])) $parsable_array[$key]=$value;
  962. else $parsable_array[$key].=(string)$value;
  963. }
  964. if ($key == 'school') array_push($schools,$value);
  965. if ($key == 'major') array_push($areas_of_focus,$value);
  966. }
  967. }
  968. $master_degree = false;
  969. $bach_degree = false;
  970. $doctor_degree = false;
  971. $doctor_array = array('/\b(?i)phd*\b/','/\bdoctor*\b/');
  972. $master_array = array('/\bMS\b/','/\b(?i)master*/');
  973. $bach_array = array('/\b(?i)bachelor*\b/','/\bBS\b/');
  974. if (isset($parsable_array['degree'])){
  975. preg_replace($doctor_array,'',$parsable_array['degree'],-1,$doctor);
  976. if ($doctor>0) $doctor_degree = true;
  977. preg_replace($master_array,'',$parsable_array['degree'],-1,$masters);
  978. if ($masters>0) $master_degree = true;
  979. preg_replace($bach_array,'',$parsable_array['degree'],-1,$bachelors);
  980. if ($bachelors>0) $bach_degree = true;
  981. }
  982. $this->set('bach_degree',$bach_degree);
  983. $this->set('master_degree',$master_degree);
  984. $this->set('doctor_degree',$doctor_degree);
  985. $this->set('areas_of_focus',$areas_of_focus);
  986. $this->set('schools',$schools);
  987. }
  988. $sign = $this->getSign($db_results['Userprofile']['birthday']);
  989. $this->set('zodiac', $sign);
  990. $this->set('user',$db_results);
  991. $this->set('screen_name',$screen_name);
  992. }
  993. }
  994. function compare($screen_name=null){
  995. if(is_null($this->Auth->getUserId())){
  996. Controller::render('/deny');
  997. }
  998. $this->set('exists',true);
  999. $this->set('screen_name',$screen_name);
  1000. $user = $this->Auth->getUserInfo();
  1001. if ($screen_name == $user['screen_name']){
  1002. // you are comparing yourself to yourself
  1003. $this->redirect(array('action'=>'view_my_profile'));
  1004. }
  1005. else {
  1006. $score = 0;
  1007. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  1008. $other_user = $this->User->find('first', array('conditions' => (array('User.screen_name'=>$screen_name))));
  1009. $this->set(compact('user'));
  1010. $this->set('other_user',$other_user);
  1011. if (empty($other_user)) $this->set('exists',false);
  1012. // compare interests
  1013. //var_dump($user['Interest']);
  1014. // var_dump($other_user['Interest']);
  1015. $astrology = 0;
  1016. $personal = 0;
  1017. if (!is_null($user['Userprofile']['id']) && !is_null($other_user['Userprofile']['id'])){
  1018. // check difference in birthday
  1019. // same sign?
  1020. $user_sign = $this->getSign($user['Userprofile']['birthday']);
  1021. $other_user_sign = $this->getSign($other_user['Userprofile']['birthday']);
  1022. $diff = abs(strtotime($user['Userprofile']['birthday']) - strtotime($other_user['Userprofile']['birthday']));
  1023. $years = floor($diff / (365*60*60*24));
  1024. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
  1025. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
  1026. if ($months == 0 && $days < 10) {
  1027. $astrology += 10;
  1028. $score += 5;
  1029. }
  1030. elseif ($month > 1 && $month < 3) {
  1031. $astrology += 5;
  1032. $score+=2;
  1033. }
  1034. if ($years < 10){
  1035. $score++;
  1036. }
  1037. if ($user_sign == $other_user_sign){
  1038. $astrology += 10;
  1039. $score += 3;
  1040. }
  1041. if ($user['Userprofile']['gender']==$other_user['Userprofile']['gender']){
  1042. $score++;
  1043. $personal++;
  1044. }
  1045. similar_text($user['Userprofile']['relationship'],$other_user['Userprofile']['relationship'],$r_match);
  1046. if ($r_match > 50) {
  1047. $personal++;
  1048. $score++;
  1049. }
  1050. similar_text($user['Userprofile']['religion'],$other_user['Userprofile']['religion'],$re_match);
  1051. if ($re_match > 50) {
  1052. $personal += 2;
  1053. $score+=2;
  1054. }
  1055. similar_text($user['Userprofile']['political'],$other_user['Userprofile']['political'],$p_match);
  1056. if ($p_match > 50) {
  1057. $personal += 3;
  1058. $score+=3;
  1059. }
  1060. if (!is_null($user['Userprofile']['hometown']) && !is_null($other_user['Userprofile']['hometown'])){
  1061. $url = "http://where.yahooapis.com/geocode?line2=".urlencode($user['Userprofile']['hometown'])."&flags=J&gflags=R&appid=cENXMi4g";
  1062. $address = json_decode(file_get_contents($url));
  1063. $lat_home = $address->ResultSet->Results[0]->latitude;
  1064. $long_home = $address->ResultSet->Results[0]->longitude;
  1065. $url2 = "http://where.yahooapis.com/geocode?line2=".urlencode($other_user['Userprofile']['hometown'])."&flags=J&gflags=R&appid=cENXMi4g";
  1066. $address2 = json_decode(file_get_contents($url2));
  1067. $lat_away = $address2->ResultSet->Results[0]->latitude;
  1068. $long_away = $address2->ResultSet->Results[0]->longitude;
  1069. $earth_radius = 6371;
  1070. $delta_lat = deg2rad($lat_home - $lat_away);
  1071. $delta_long = deg2rad($long_home - $long_away);
  1072. $a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($lat_home)) * cos(deg2rad($lat_away)) * sin($delta_long/2) * sin($delta_long/2);
  1073. $c = 2 * atan2(sqrt($a),sqrt(1-$a));
  1074. $distance = $earth_radius * $c;
  1075. $d_miles = $distance * 0.621371192;
  1076. if ($d_miles < 1) {
  1077. $score += 20;
  1078. }
  1079. elseif ($d_miles < 10) {
  1080. $score += 16;
  1081. }
  1082. elseif ($d_miles < 25) {
  1083. $score += 12;
  1084. }
  1085. elseif ($d_miles < 50) {
  1086. $score += 10;
  1087. }
  1088. elseif ($d_miles < 100) {
  1089. $score += 7;
  1090. }
  1091. elseif ($d_miles < 150) {
  1092. $score += 3;
  1093. }
  1094. elseif ($d_miles < 200) {
  1095. $score++;
  1096. }
  1097. $this->set('distance',$d_miles);
  1098. }
  1099. }
  1100. $this->set('astrology',$astrology);
  1101. $this->set('personal',$personal);
  1102. if (!is_null($user['Interest']['id']) && !is_null($other_user['Interest']['id'])){
  1103. $user_interest = json_decode($user['Interest']['body'], true);
  1104. $other_user_interest =json_decode($other_user['Interest']['body'], true);
  1105. arsort($user_interest,SORT_NUMERIC);
  1106. arsort($other_user_interest,SORT_NUMERIC);
  1107. $common_interests = array_intersect_key($user_interest,$other_user_interest);
  1108. // var_dump($user_interest);
  1109. // var_dump($other_user_interest);
  1110. }
  1111. /***************
  1112. array_ intersect_ key
  1113. array_count_values
  1114. ******************/
  1115. /* put data into arrays
  1116. if simliar tag a 1
  1117. if not skip and continue
  1118. */
  1119. // use fb ids?
  1120. // compare places
  1121. if (!is_null($user['Place']['id']) && !is_null($other_user['Place']['id'])){
  1122. //var_dump($user['Place']['body']);
  1123. //var_dump($other_user['Place']['body']); echo 'checking places';
  1124. }
  1125. // locations
  1126. // categories
  1127. // compare movies
  1128. if (!is_null($user['Movie']['id']) && !is_null($other_user['Movie']['id'])){
  1129. echo 'checking movies';
  1130. }
  1131. // compare schools
  1132. if (!is_null($user['School']['id']) && !is_null($other_user['School']['id'])){
  1133. //var_dump($user['School']['body']);
  1134. $my_schools = json_decode($user['School']['body'],true);
  1135. $your_schools = json_decode($other_user['School']['body'],true);
  1136. $earth_radius = 6371;
  1137. $education = 0;
  1138. $doctor_array = array('/\b(?i)phd*\b/','/\bdoctor*\b/');
  1139. $master_array = array('/\bMS\b/','/\b(?i)master*/');
  1140. $bach_array = array('/\b(?i)bachelor*\b/','/\bBS\b/');
  1141. for ($counter = 0;$counter<sizeof($my_schools);$counter++){
  1142. if (isset($my_schools[$counter]['degree'])){
  1143. preg_replace($doctor_array,'',$my_schools[$counter]['degree'],-1,$doctor);
  1144. if ($doctor>0) $my_doctor_degree = true;
  1145. preg_replace($master_array,'',$my_schools[$counter]['degree'],-1,$masters);
  1146. if ($masters>0) $my_master_degree = true;
  1147. preg_replace($bach_array,'',$my_schools[$counter]['degree'],-1,$bachelors);
  1148. if ($bachelors>0) $my_bach_degree = true;
  1149. }
  1150. }
  1151. for ($counter = 0;$counter<sizeof($your_schools);$counter++){
  1152. if (isset($your_schools[$counter]['degree'])){
  1153. preg_replace($doctor_array,'',$your_schools[$counter]['degree'],-1,$doctor);
  1154. if ($doctor>0) $your_doctor_degree = true;
  1155. preg_replace($master_array,'',$your_schools[$counter]['degree'],-1,$masters);
  1156. if ($masters>0) $your_master_degree = true;
  1157. preg_replace($bach_array,'',$your_schools[$counter]['degree'],-1,$bachelors);
  1158. if ($bachelors>0) $your_bach_degree = true;
  1159. }
  1160. }
  1161. if ($my_bach_degree && $your_bach_degree) $bach_degree = true;
  1162. if ($my_master_degree && $your_master_degree) $master_degree = true;
  1163. if ($my_doctor_degree && $your_doctor_degree) $doctor_degree = true;
  1164. $this->set('bachelor',$bach_degree);
  1165. $this->set('master',$master_degree);
  1166. $this->set('doctor',$doctor_degree);
  1167. $my_major = array();
  1168. $your_major = array();
  1169. // look at majors
  1170. for ($counter = 0;$counter<sizeof($my_schools);$counter++){
  1171. if (isset($my_schools[$counter]['major'])){
  1172. $tok = strtok($my_schools[$counter]['major']," ");
  1173. while ($tok !== false) {
  1174. array_push($my_major, $tok);
  1175. $tok = strtok(" ");
  1176. }
  1177. }
  1178. }
  1179. for ($counter = 0;$counter<sizeof($your_schools);$counter++){
  1180. if (isset($your_schools[$counter]['major'])){
  1181. $tok = strtok($your_schools[$counter]['major']," ");
  1182. while ($tok !== false) {
  1183. array_push($your_major, $tok);
  1184. $tok = strtok(" ");
  1185. }
  1186. }
  1187. }
  1188. $majors = 0;
  1189. for ($counter = 0; $counter < sizeof($my_major); $counter++){
  1190. for ($inner_counter = 0; $inner_counter<sizeof($your_major); $inner_counter++){
  1191. similar_text($my_major[$counter],$your_major[$inner_counter],$match);
  1192. if ($match > 50) {
  1193. // var_dump($my_major[$counter]);
  1194. // var_dump($your_major[$inner_counter]);
  1195. $score += 20;
  1196. $majors += 10;
  1197. }
  1198. }
  1199. }
  1200. $this->set('majors',$majors);
  1201. for ($counter=0;$counter<sizeof($my_schools);$counter++){
  1202. $url = "http://where.yahooapis.com/geocode?q=".urlencode($my_schools[$counter]['school'])."&flags=J&gflags=R&appid=cENXMi4g";
  1203. $address = json_decode(file_get_contents($url));
  1204. // echo $my_schools['counter']['school'];
  1205. //var_dump($address);
  1206. if (!$address->ResultSet->Error){
  1207. $my_lat[$counter] = $address->ResultSet->Results[0]->latitude;
  1208. $my_long[$counter] = $address->ResultSet->Results[0]->longitude;
  1209. }
  1210. for ($inner_counter=0;$inner_counter<sizeof($your_schools);$inner_counter++){
  1211. similar_text($my_schools[$counter]['school'],$your_schools[$inner_counter]['school'],$match);
  1212. if ($match > 75) {
  1213. $education += 10;
  1214. $score+=30;
  1215. }
  1216. if ($counter==0){
  1217. $url = "http://where.yahooapis.com/geocode?q=".urlencode($your_schools[$inner_counter]['school'])."&flags=J&gflags=R&appid=cENXMi4g";
  1218. $address = json_decode(file_get_contents($url));
  1219. // echo $your_schools[$inner_counter]['school'];
  1220. // var_dump($address);
  1221. if (!$address->ResultSet->Error){
  1222. $your_lat[$inner_counter] = $address->ResultSet->Results[0]->latitude;
  1223. $your_long[$inner_counter] = $address->ResultSet->Results[0]->longitude;
  1224. }
  1225. }
  1226. $delta_lat = deg2rad($my_lat[$counter] - $your_lat[$inner_counter]);
  1227. $delta_long = deg2rad($my_long[$counter] - $your_long[$inner_counter]);
  1228. $a = sin($delta_lat/2) * sin($delta_lat/2) + cos(deg2rad($my_lat[$counter])) * cos(deg2rad($your_lat[$inner_counter])) * sin($delta_long/2) * sin($delta_long/2);
  1229. $c = 2 * atan2(sqrt($a),sqrt(1-$a));
  1230. $distance = $earth_radius * $c;
  1231. $d_miles = $distance * 0.621371192;
  1232. if ($d_miles < 1) $score += 20;
  1233. elseif ($d_miles < 10) $score += 16;
  1234. elseif ($d_miles < 25) $score += 12;
  1235. elseif ($d_miles < 50) $score += 10;
  1236. elseif ($d_miles < 100) $score += 7;
  1237. elseif ($d_miles < 150) $score += 3;
  1238. elseif ($d_miles < 200) $score++;
  1239. $this->set('edu_distance',$d_miles);
  1240. }
  1241. }
  1242. $this->set('education',$education);
  1243. }
  1244. // compare works
  1245. if (!is_null($user['Work']['id']) && !is_null($other_user['Work']['id'])){
  1246. $my_industries = array();
  1247. $your_industries = array();
  1248. $work = 0;
  1249. $my_work = json_decode($user['Work']['body']);
  1250. $your_work = json_decode($other_user['Work']['body']);
  1251. for($counter = 0; $counter < sizeof($my_work); $counter++){
  1252. $cats = $this->Wlookup->find('first',array('conditions' => (array('Wlookup.description'=>$my_work[$counter]->industry))));
  1253. $tok = strtok($cats['Wlookup']['group']," ");
  1254. while ($tok !== false) {
  1255. array_push($my_industries, $tok);
  1256. $tok = strtok(" ");
  1257. }
  1258. }
  1259. for($counter = 0; $counter < sizeof($your_work); $counter++){
  1260. $cats = $this->Wlookup->find('first',array('conditions' => (array('Wlookup.description'=>$your_work[$counter]->industry))));
  1261. $tok = strtok($cats['Wlookup']['group']," ");
  1262. while ($tok !== false) {
  1263. array_push($your_industries, $tok);
  1264. $tok = strtok(" ");
  1265. }
  1266. }
  1267. $result = array_intersect($my_industries, $your_industries);
  1268. if (!empty($result)) {
  1269. $score += sizeof($result) * 2;
  1270. $work += 10;
  1271. }
  1272. // titles
  1273. // we need a list of titles that are similar
  1274. for($counter = 0; $counter < sizeof($my_work); $counter++){
  1275. for($inner_counter = 0; $inner_counter < sizeof($your_work); $inner_counter++){
  1276. similar_text($my_work[$counter]->company,$your_work[$inner_counter]->company,$match);
  1277. if ($match > 50){
  1278. $work += 10;
  1279. $score += 10;
  1280. }
  1281. }
  1282. }
  1283. //companys
  1284. // how do we compare companies
  1285. for($counter = 0; $counter < sizeof($my_work); $counter++){
  1286. for($inner_counter = 0; $inner_counter < sizeof($your_work); $inner_counter++){
  1287. similar_text($my_work[$counter]->title,$your_work[$inner_counter]->title,$match);
  1288. if ($match > 50){
  1289. $work += 10;
  1290. $score += 10;
  1291. }
  1292. }
  1293. }
  1294. $this->set('work',$work);
  1295. }
  1296. $this->set('score',$score);
  1297. }
  1298. }
  1299. function getSign($birthday){
  1300. $birthmonth = date('n',strtotime($birthday));
  1301. $birthdate = date('d',strtotime($birthday));
  1302. $sign = '';
  1303. switch($birthmonth){
  1304. case '1':
  1305. if ($birthdate>19) $sign = "Aquarius";
  1306. else $sign = "Capricorn";
  1307. break;
  1308. case '2':
  1309. if ($birthdate>18) $sign = "Pisces";
  1310. else $sign = "Aquarius";
  1311. break;
  1312. case '3':
  1313. if ($birthdate>20) $sign = "Aries";
  1314. else $sign = "Pisces";
  1315. break;
  1316. case '4':
  1317. if ($birthdate>19) $sign = "Taurus";
  1318. else $sign = "Aries";
  1319. break;
  1320. case '5':
  1321. if ($birthdate>20) $sign = "Gemini";
  1322. else $sign = "Taurus";
  1323. break;
  1324. case '6':
  1325. if ($birthdate>20) $sign = "Cancer";
  1326. else $sign = "Gemini";
  1327. break;
  1328. case '7':
  1329. if ($birthdate>22) $sign = "Leo";
  1330. else $sign = "Cancer";
  1331. break;
  1332. case '8':
  1333. if ($birthdate>22) $sign = "Virgo";
  1334. else $sign = "Leo";
  1335. break;
  1336. case '9':
  1337. if ($birthdate>22) $sign = "Libra";
  1338. else $sign = "Virgo";
  1339. break;
  1340. case '10':
  1341. if ($birthdate>22) $sign = "Scorpio";
  1342. else $sign = "Libra";
  1343. break;
  1344. case '11':
  1345. if ($birthdate>21) $sign = "Sagittarius";
  1346. else $sign = "Scorpio";
  1347. break;
  1348. case '12':
  1349. if ($birthdate>21) $sign = "Capricorn";
  1350. else $sign = "Sagittarius";
  1351. break;
  1352. }
  1353. return $sign;
  1354. }
  1355. function edit_interest($id=null){
  1356. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  1357. $interests = json_decode($user['Interest']['body'],true);
  1358. // var_dump($interests);
  1359. //var_dump($id);
  1360. $div_name = 'Interest_'.$id;
  1361. $this->set('editing',true);
  1362. $this->set('div_name',$div_name);
  1363. $this->set('id',$id);
  1364. // need to make sure you are the owner
  1365. if (is_null($this->Auth->getUserId())){
  1366. Controller::render('/deny');
  1367. }
  1368. if (!empty($this->data)){
  1369. // var_dump($this->data);
  1370. $id = $this->data['User']['id'];
  1371. $key = $this->data['User']['key'];
  1372. $interests[$key]=(int)$interests[$id];
  1373. unset($interests[$id]);
  1374. arsort($interests,SORT_NUMERIC);
  1375. $this->Interest->read(null,$user['Interest']['id']);
  1376. $this->data['Interest']['body'] = json_encode($interests);
  1377. $this->Interest->set($this->data);
  1378. $this->Interest->save();
  1379. $this->set('editing',false);
  1380. $this->set('key',$key);
  1381. $this->render();
  1382. }
  1383. else {
  1384. // $this->set(
  1385. $this->render();
  1386. }
  1387. }
  1388. function edit_work($id=null,$key=null,$type='Work'){
  1389. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  1390. //$work = json_decode($user[$type]['body'],true);
  1391. // var_dump($user);
  1392. //echo $this->Auth->getUserId();
  1393. // var_dump($user['Work']['body']);
  1394. //var_dump($work);
  1395. //echo $id.' id';
  1396. //echo $key.' key';
  1397. //var_dump($this->data);
  1398. if (!empty($this->data)){
  1399. $id = $this->data['User']['id'];
  1400. $key = $this->data['User']['key'];
  1401. $type = $this->data['User']['type'];
  1402. $work = json_decode($user[$type]['body'],true);
  1403. $work[$id][$key] = $this->data['User']['val'];
  1404. // var_dump($work);
  1405. if ($type=='Work'){
  1406. $this->Work->read(null,$user[$type]['id']);
  1407. $this->data[$type]['body'] = json_encode($work);
  1408. $this->Work->set($this->data);
  1409. $this->Work->save();
  1410. }
  1411. else { // must be school then
  1412. $this->School->read(null,$user[$type]['id']);
  1413. $this->data[$type]['body'] = json_encode($work);
  1414. $this->School->set($this->data);
  1415. $this->School->save();
  1416. }
  1417. // var_dump($this->data);
  1418. $this->set('editing',false);
  1419. $this->set('value',$this->data['User']['val']);
  1420. $this->set('id',$id);
  1421. $this->set('key',$key);
  1422. $this->set('type',$type);
  1423. }
  1424. elseif ($key == 'delete'){
  1425. echo 'Data Deleted';
  1426. //echo $type;
  1427. $work = json_decode($user[$type]['body'],true);
  1428. //echo $user['Work']['id'];
  1429. //echo $user[$type]['id'];
  1430. // delete the whole entry
  1431. if ($type=='Work'){
  1432. $this->Work->delete($user[$type]['id']);
  1433. //$this->data[$type]['body'] = json_encode($work);
  1434. //$this->Work->set($this->data);
  1435. //$this->Work->save();
  1436. }
  1437. else { // must be school then
  1438. $this->School->delete($user[$type]['id']);
  1439. //$this->data[$type]['body'] = json_encode($work);
  1440. //$this->School->set($this->data);
  1441. //$this->School->save();
  1442. }
  1443. }
  1444. else {
  1445. $this->set('value',$work[$id][$key]);
  1446. $this->set('editing',true);
  1447. $this->set('id',$id);
  1448. $this->set('type',$type);
  1449. $this->set('key',$key);
  1450. }
  1451. $this->set('div_name','edit_'.$type.'_'.$key.'_'.$id);
  1452. $this->render();
  1453. }
  1454. function add_work($type='Work'){
  1455. $user = $this->User->find('first', array('conditions' => (array('User.id'=>$this->Auth->getUserId()))));
  1456. if (!empty($this->data)){
  1457. // var_dump($this->data);
  1458. $type = $this->data['User']['type'];
  1459. $work = json_decode($user[$type]['body'],true);
  1460. if (sizeof($work)<1) $work = array();
  1461. foreach($this->data['User'] as $key=>$value){
  1462. if ($key != 'type'){
  1463. $temp[$type][$key] = $value;
  1464. }
  1465. }
  1466. array_push($work,$temp[$type]);
  1467. // var_dump($work);
  1468. if ($type=='Work'){
  1469. $this->Work->read(null,$user[$type]['id']);
  1470. $this->data[$type]['body'] = json_encode($work);
  1471. $this->Work->set($this->data);
  1472. $this->Work->save();
  1473. }
  1474. else { // must be school then
  1475. $this->School->read(null,$user[$type]['id']);
  1476. $this->data[$type]['body'] = json_encode($work);
  1477. $this->School->set($this->data);
  1478. $this->School->save();
  1479. }
  1480. // var_dump($this->data);
  1481. $this->set('editing',true);
  1482. $this->set('value',$this->data['User']['val']);
  1483. $this->set('id',$id);
  1484. $this->set('key',$key);
  1485. $this->set('type',$type);
  1486. }
  1487. else {
  1488. /*if ($type=='Work'){
  1489. $this->set('first','title');
  1490. $this->set('second','company');
  1491. $this->set('third','industry');
  1492. $this->set('type',$type);
  1493. }
  1494. else {
  1495. $this->set('value',$work[$id][$key]);
  1496. $this->set('editing',true);
  1497. $this->set('id',$id);
  1498. $this->set('type',$type);
  1499. $this->set('key',$key);
  1500. }*/
  1501. }
  1502. if ($type=='Work'){
  1503. $this->set('first','title');
  1504. $this->set('second','company');
  1505. $this->set('third','industry');
  1506. $this->set('type',$type);
  1507. }
  1508. elseif ($type=='School'){
  1509. $this->set('first','degree');
  1510. $this->set('second','school');
  1511. $this->set('third','major');
  1512. $this->set('type',$type);
  1513. }
  1514. $this->set('div_name','add_'.$type);
  1515. $this->render();
  1516. }
  1517. }
  1518. ?>