PageRenderTime 73ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/utilidades/GestionRedSocial.php

https://bitbucket.org/veroreinah/bookworm
PHP | 539 lines | 505 code | 20 blank | 14 comment | 5 complexity | d20f6590115b2af5fa8e35a9c08ab5a3 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'conexion.php';
  3. require_once 'clases/Usuario.php';
  4. require_once 'clases/Invitacion.php';
  5. require_once 'GestionUsuarios.php';
  6. require_once 'constant.php';
  7. /*
  8. * To change this template, choose Tools | Templates
  9. * and open the template in the editor.
  10. */
  11. /**
  12. * Description of gestionRedSocial
  13. *
  14. * @author Vero
  15. */
  16. class GestionRedSocial {
  17. public static function invitar($usuarioInvita, $usuarioInvitado) {
  18. global $conexion;
  19. $uInvita = new Usuario();
  20. $uInvita = $usuarioInvita;
  21. $uInvitado = new Usuario();
  22. $uInvitado = $usuarioInvitado;
  23. $enlace = uniqid();
  24. try {
  25. $query = "insert into t_usuario_usuario
  26. (id_usuario_inv, id_usuario_rec, fecha_inv, aceptada, enlace)
  27. values ('" . $uInvita->getId() . "',
  28. '" . $uInvitado->getId() . "',
  29. '" . date("Y-m-d") . "',
  30. null,
  31. '" . $enlace . "')";
  32. $result = mysql_query($query, $conexion);
  33. } catch (Exception $e) {
  34. $_SESSION["error"] = "No se ha podido enviar la invitación en este momento. Inténtelo de nuevo más tarde.";
  35. }
  36. }
  37. public static function invitarPorEmail($invitacion) {
  38. global $conexion;
  39. $i = new Invitacion();
  40. $i = $invitacion;
  41. $u = new Usuario();
  42. $u = $i->getUsuario();
  43. $enlace = uniqid();
  44. try {
  45. $query = "insert into t_invitacion
  46. (fecha_envio, email_inv, texto, aceptada, id_usuario, enlace)
  47. values ('" . date("Y-m-d") . "',
  48. '" . $i->getEmail() . "',
  49. '" . $i->getTexto() . "',
  50. null,
  51. '" . $u->getId() . "',
  52. '" . $enlace . "')";
  53. $result = mysql_query($query, $conexion);
  54. $i->setEnlace($enlace);
  55. $enviado = GestionRedSocial::enviarMail($i);
  56. return $enviado;
  57. } catch (Exception $e) {
  58. $_SESSION["error"] = "No se ha podido enviar la invitación en este momento. Inténtelo de nuevo más tarde.";
  59. }
  60. }
  61. public static function enviarMail($invitacion) {
  62. if ($_SERVER['SERVER_NAME'] == 'localhost') {
  63. $base = "http://localhost:8080/bookworm";
  64. } else {
  65. $base = "http://bookworm.phpfogapp.com";
  66. }
  67. $i = new Invitacion();
  68. $i = $invitacion;
  69. $u = new Usuario();
  70. $u = $i->getUsuario();
  71. require_once '../utilidades/constant.php';
  72. require_once '../lib/Swift-4.1.7/lib/swift_required.php';
  73. global $email;
  74. set_time_limit(-1);
  75. $body = "<html><body>";
  76. $body .= $u->getNombre() . " (" . $u->getEmail() . ") te ha intivado a unirte a <a href='$base/index.php'>BookWorm</a>, la nueva red social de lectores.<br /><br />";
  77. if ($i->getTexto() != "") {
  78. $body .= "Su mensaje: <em>" . $i->getTexto() . "</em><br /><br />";
  79. }
  80. $body .= "Si deseas unirte a <a href='$base/index.php'>BookWorm</a> y estar al día de las lecturas de " . $u->getNombre() . ", haz clic en el siguiente enlace:<br />";
  81. $body .= "<a href='$base/register.php?inv=" . $i->getEnlace() . "'>Unirse...</a><br /><br /><br />¡Hasta pronto!";
  82. $body .= "</body></html>";
  83. // Create a message
  84. $message = Swift_Message::newInstance()
  85. // Give the message a subject
  86. ->setSubject($u->getNombre() . " te invita a BookWorm.")
  87. // Set the From address with an associative array
  88. ->setFrom(array($email))
  89. // Set the To addresses with an associative array
  90. ->setTo(array($i->getEmail()))
  91. // Give it a body
  92. ->setBody($body, 'text/html')
  93. ;
  94. $password = getenv('EMAIL_PASSWORD');
  95. $password = str_replace('\\', '', $password);
  96. // Create the Transport
  97. $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  98. ->setUsername($email)
  99. ->setPassword($password)
  100. ;
  101. // Create the Mailer using your created Transport
  102. $mailer = Swift_Mailer::newInstance($transport);
  103. // Send the message
  104. $result = $mailer->send($message);
  105. if ($result) {
  106. return 1;
  107. } else {
  108. return 0;
  109. }
  110. }
  111. public static function responderInvitacion($enlace, $respuesta) {
  112. global $conexion;
  113. try {
  114. if ($respuesta) {
  115. $query = "update t_usuario_usuario
  116. set aceptada = true
  117. where enlace = '" . $enlace . "'";
  118. $result = mysql_query($query, $conexion);
  119. } else {
  120. $query = "delete from t_usuario_usuario
  121. where enlace = '" . $enlace . "'";
  122. $result = mysql_query($query, $conexion);
  123. }
  124. } catch (Exception $e) {
  125. $_SESSION["error"] = "No se ha podido gestionar su respuesta a la invitación. Inténtelo de nuevo más tarde.";
  126. }
  127. }
  128. public static function responderInvitacionEmail($enlace, $usuarioInvita, $usuario) {
  129. global $conexion;
  130. $uInvita = new Usuario();
  131. $uInvita = $usuarioInvita;
  132. $u = new Usuario();
  133. $u = $usuario;
  134. try {
  135. $query = "update t_invitacion
  136. set aceptada = true
  137. where enlace = '" . $enlace . "'";
  138. $result = mysql_query($query, $conexion);
  139. $query2 = "insert into t_usuario_usuario
  140. (id_usuario_inv, id_usuario_rec, fecha_inv, aceptada, enlace)
  141. values ('" . $uInvita->getId() . "',
  142. '" . $u->getId() . "',
  143. '" . date("Y-m-d") . "',
  144. true,
  145. '$enlace')";
  146. $result2 = mysql_query($query2, $conexion);
  147. } catch (Exception $e) {
  148. $_SESSION["error"] = "No se ha podido gestionar su respuesta a la invitación. Inténtelo de nuevo más tarde.";
  149. }
  150. }
  151. public static function terminarAmistad($usuarioInvita, $usuarioInvitado) {
  152. global $conexion;
  153. $uInvita = new Usuario();
  154. $uInvita = $usuarioInvita;
  155. $uInvitado = new Usuario();
  156. $uInvitado = $usuarioInvitado;
  157. try {
  158. $query = "delete from t_usuario_usuario
  159. where (id_usuario_inv = '" . $uInvita->getId() . "'
  160. and id_usuario_rec = '" . $uInvitado->getId() . "')
  161. or (id_usuario_rec = '" . $uInvita->getId() . "'
  162. and id_usuario_inv = '" . $uInvitado->getId() . "')";
  163. $result = mysql_query($query, $conexion);
  164. } catch (Exception $e) {
  165. $_SESSION["error"] = "No se ha podido llevar a cabo su solicitud en este momento. Inténtelo de nuevo más tarde.";
  166. }
  167. }
  168. public static function recuperarInvitaciones($id) {
  169. global $conexion;
  170. $invitaciones = array();
  171. try {
  172. $query = "select * from t_usuario_usuario
  173. where id_usuario_rec = '" . $id . "'
  174. and (aceptada is null or aceptada = false)";
  175. $result = mysql_query($query, $conexion);
  176. while ($row = mysql_fetch_array($result)) {
  177. $datos = array();
  178. $datos[] = GestionUsuarios::recuperarUsuario($row["id_usuario_inv"]);
  179. $datos[] = $row["fecha_inv"];
  180. $datos[] = $row["enlace"];
  181. $invitaciones[] = $datos;
  182. }
  183. return $invitaciones;
  184. } catch (Exception $e) {
  185. $_SESSION["error"] = "No se han podido recuperar las invitaciones pendientes. Inténtelo de nuevo más tarde.";
  186. }
  187. }
  188. public static function recuperarInvitacionesL($begin, $limit, $field="", $order="", $where="") {
  189. global $conexion;
  190. $user = new Usuario();
  191. $user = unserialize($_SESSION["usuario"]);
  192. $id = $user->getId();
  193. if ($begin != "" && $limit != "") {
  194. $l = " limit $begin, $limit ";
  195. }
  196. if ($field != "" && $order != "") {
  197. $orderBy = " order by $field $order ";
  198. }
  199. if ($where != "") {
  200. $w = " and u.nombre_usuario like '%$where%' ";
  201. }
  202. $invitaciones = array();
  203. try {
  204. $query = "select * from t_usuario_usuario
  205. inner join t_usuarios as u
  206. on t_usuario_usuario.id_usuario_inv = u.id_usuario
  207. where id_usuario_rec = '" . $id . "'
  208. and (aceptada is null or aceptada = false)";
  209. if (isset($w)) {
  210. $query = $query . $w;
  211. }
  212. if (isset($orderBy)) {
  213. $query = $query . $orderBy;
  214. }
  215. if (isset($l)) {
  216. $query = $query . $l;
  217. }
  218. $result = mysql_query($query, $conexion);
  219. while ($row = mysql_fetch_array($result)) {
  220. $datos = array();
  221. $datos[] = GestionUsuarios::recuperarUsuario($row["id_usuario_inv"]);
  222. $datos[] = $row["fecha_inv"];
  223. $datos[] = $row["enlace"];
  224. $invitaciones[] = $datos;
  225. }
  226. return $invitaciones;
  227. } catch (Exception $e) {
  228. $_SESSION["error"] = "No se han podido recuperar las invitaciones pendientes. Inténtelo de nuevo más tarde.";
  229. }
  230. }
  231. public static function recuperarAmigos($id) {
  232. global $conexion;
  233. $amigos = array();
  234. try {
  235. $query = "select * from t_usuario_usuario
  236. where (id_usuario_rec = '" . $id . "'
  237. or id_usuario_inv = '" . $id . "')
  238. and aceptada = true";
  239. $result = mysql_query($query, $conexion);
  240. while ($row = mysql_fetch_array($result)) {
  241. $u = new Usuario();
  242. if ($row["id_usuario_rec"] == $id) {
  243. $u = ($row["id_usuario_inv"]);
  244. }
  245. if ($row["id_usuario_inv"] == $id) {
  246. $u = ($row["id_usuario_rec"]);
  247. }
  248. $amigos[] = $u;
  249. }
  250. if (count($amigos) == 0) {
  251. $_SESSION["noHayA"] = "Todavía no sigues a ningún usuario de BookWorm. Puedes invitar a tus amigos desde el panel de la derecha o seguir navegando y seguir a los usuarios con los que tengas más afinidad.";
  252. }
  253. return $amigos;
  254. } catch (Exception $e) {
  255. $_SESSION["error"] = "No se han podido recuperar sus contactos. Inténtelo de nuevo más tarde.";
  256. }
  257. }
  258. public static function recuperarUsuarioInvita($enlace) {
  259. global $conexion;
  260. try {
  261. $query = "select * from t_invitacion
  262. where enlace = '$enlace'";
  263. $result = mysql_query($query, $conexion);
  264. while ($row = mysql_fetch_array($result)) {
  265. return $row['id_usuario'];
  266. }
  267. } catch (Exception $e) {
  268. $_SESSION["error"] = "No se ha podido recuperar el usuario. Inténtelo de nuevo más tarde.";
  269. }
  270. }
  271. public static function recuperarAmigosL($begin, $limit, $field="", $order="", $where="") {
  272. global $conexion;
  273. $u = new Usuario();
  274. $u = unserialize($_SESSION["usuario"]);
  275. $id = $u->getId();
  276. if ($begin != "" && $limit != "") {
  277. $l = " limit $begin, $limit ";
  278. }
  279. if ($field != "" && $order != "") {
  280. $orderBy = " order by $field $order ";
  281. }
  282. if ($where != "") {
  283. $w = " and u.nombre_usuario like '%$where%' ";
  284. }
  285. $amigos = array();
  286. try {
  287. $query = "select distinct * from t_usuario_usuario as uu
  288. inner join t_usuarios as u
  289. on (u.id_usuario = uu.id_usuario_rec or u.id_usuario = uu.id_usuario_inv)
  290. where (uu.id_usuario_rec = '" . $id . "'
  291. or uu.id_usuario_inv = '" . $id . "')
  292. and uu.aceptada = true
  293. and u.id_usuario != $id ";
  294. if (isset($w)) {
  295. $query = $query . $w;
  296. }
  297. if (isset($orderBy)) {
  298. $query = $query . $orderBy;
  299. }
  300. if (isset($l)) {
  301. $query = $query . $l;
  302. }
  303. $result = mysql_query($query, $conexion);
  304. while ($row = mysql_fetch_array($result)) {
  305. $u = new Usuario();
  306. if ($row["id_usuario_rec"] == $id) {
  307. $u = GestionUsuarios::recuperarUsuario($row["id_usuario_inv"]);
  308. }
  309. if ($row["id_usuario_inv"] == $id) {
  310. $u = GestionUsuarios::recuperarUsuario($row["id_usuario_rec"]);
  311. }
  312. $amigos[] = $u;
  313. }
  314. if (count($amigos) > 0) {
  315. } else {
  316. $_SESSION["noHay"] = "Todavía no sigues a ningún usuario de BookWorm. Puedes invitar a tus amigos desde el panel de la derecha o seguir navegando y seguir a los usuarios con los que tengas más afinidad.";
  317. }
  318. return $amigos;
  319. } catch (Exception $e) {
  320. $_SESSION["error"] = "No se han podido recuperar sus contactos. Inténtelo de nuevo más tarde.";
  321. }
  322. }
  323. public static function totalAmigos() {
  324. global $conexion;
  325. $u = new Usuario();
  326. $u = unserialize($_SESSION["usuario"]);
  327. $id = $u->getId();
  328. try {
  329. $query = "select count(*) as total
  330. from t_usuario_usuario as uu
  331. where (uu.id_usuario_rec = '" . $id . "'
  332. or uu.id_usuario_inv = '" . $id . "')
  333. and uu.aceptada = true ";
  334. $result = mysql_query($query, $conexion);
  335. $row = mysql_fetch_array($result);
  336. $total = $row["total"];
  337. return $total;
  338. } catch (Exception $e) {
  339. $_SESSION["error"] = "No se han podido recuperar sus contactos. Inténtelo de nuevo más tarde.";
  340. }
  341. }
  342. public static function totalFiltered($where) {
  343. global $conexion;
  344. $u = new Usuario();
  345. $u = unserialize($_SESSION["usuario"]);
  346. $id = $u->getId();
  347. if ($where != "") {
  348. $w = " and u.nombre_usuario like '%$where%' ";
  349. }
  350. try {
  351. $query = "select count(*) as total
  352. from t_usuario_usuario as uu inner join t_usuarios as u
  353. on (u.id_usuario = uu.id_usuario_rec or u.id_usuario = uu.id_usuario_inv)
  354. where (uu.id_usuario_rec = '" . $id . "'
  355. or uu.id_usuario_inv = '" . $id . "')
  356. and uu.aceptada = true
  357. and u.id_usuario != $id $w";
  358. $result = mysql_query($query, $conexion);
  359. $row = mysql_fetch_array($result);
  360. $total = $row["total"];
  361. return $total;
  362. } catch (Exception $e) {
  363. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  364. }
  365. }
  366. public static function totalInvitaciones() {
  367. global $conexion;
  368. $u = new Usuario();
  369. $u = unserialize($_SESSION["usuario"]);
  370. $id = $u->getId();
  371. try {
  372. $query = "select count(*) as total from t_usuario_usuario
  373. where id_usuario_rec = '" . $id . "'
  374. and (aceptada is null or aceptada = false)";
  375. $result = mysql_query($query, $conexion);
  376. $row = mysql_fetch_array($result);
  377. $total = $row["total"];
  378. return $total;
  379. } catch (Exception $e) {
  380. $_SESSION["error"] = "No se han podido recuperar sus contactos. Inténtelo de nuevo más tarde.";
  381. }
  382. }
  383. public static function totalFilteredInv($where) {
  384. global $conexion;
  385. $u = new Usuario();
  386. $u = unserialize($_SESSION["usuario"]);
  387. $id = $u->getId();
  388. if ($where != "") {
  389. $w = " and u.nombre_usuario like '%$where%' ";
  390. }
  391. try {
  392. $query = "select count(*) as total from t_usuario_usuario
  393. inner join t_usuarios as u
  394. on t_usuario_usuario.id_usuario_inv = u.id_usuario
  395. where id_usuario_rec = '" . $id . "'
  396. and (aceptada is null or aceptada = false) $w";
  397. $result = mysql_query($query, $conexion);
  398. $row = mysql_fetch_array($result);
  399. $total = $row["total"];
  400. return $total;
  401. } catch (Exception $e) {
  402. $_SESSION["error"] = "No se han podido recuperar los libros. Inténtelo de nuevo más tarde.";
  403. }
  404. }
  405. public static function invitado($idUsuario) {
  406. global $conexion;
  407. $u = new Usuario();
  408. $u = unserialize($_SESSION["usuario"]);
  409. try {
  410. $query = "select * from t_usuario_usuario
  411. where ((id_usuario_rec = '" . $idUsuario . "'
  412. and id_usuario_inv = '" . $u->getId() . "'))
  413. and aceptada is null";
  414. $result = mysql_query($query, $conexion);
  415. $row = mysql_fetch_array($result);
  416. if (!$row) {
  417. return false;
  418. } else {
  419. return true;
  420. }
  421. } catch (Exception $e) {
  422. $_SESSION["error"] = "No se han podido recuperar sus contactos. Inténtelo de nuevo más tarde.";
  423. }
  424. }
  425. public static function invitado2($idUsuario) {
  426. global $conexion;
  427. $u = new Usuario();
  428. $u = unserialize($_SESSION["usuario"]);
  429. try {
  430. $query = "select * from t_usuario_usuario
  431. where ((id_usuario_inv = '" . $idUsuario . "'
  432. and id_usuario_rec = '" . $u->getId() . "'))
  433. and aceptada is null";
  434. $result = mysql_query($query, $conexion);
  435. $row = mysql_fetch_array($result);
  436. if (!$row) {
  437. return false;
  438. } else {
  439. return true;
  440. }
  441. } catch (Exception $e) {
  442. $_SESSION["error"] = "No se han podido recuperar sus contactos. Inténtelo de nuevo más tarde.";
  443. }
  444. }
  445. }
  446. ?>