PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/expiry.php

https://github.com/jmjdamen/expiry-check
PHP | 415 lines | 286 code | 82 blank | 47 comment | 22 complexity | 460c45f6e6122d99205c018546b6a29c MD5 | raw file
  1. <?php
  2. //use credentials and mailsettings from external file
  3. include 'settings.php';
  4. //swift mailer include and configuration
  5. require_once './swiftmailer/lib/swift_required.php';
  6. //outgoing mail server is configured for gmail, change it if needed
  7. $transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  8. ->setUsername($mail_username)
  9. ->setPassword($mail_password);
  10. $mailer = Swift_Mailer::newInstance($transporter);
  11. //create the mail transport configuration
  12. $transport = Swift_MailTransport::newInstance();
  13. //set the wanted entity state
  14. //state 'testaccepted' or 'prodaccepted'
  15. //dont change type to idp, queries need to be modified for idp use
  16. $type = "'saml20-sp'";
  17. $state = "'testaccepted'";
  18. //set expiry values in days
  19. //amount of days ago the last login was made
  20. $lastlogin_value = '180';
  21. //amount of days that service registry entries that were created have to be excluded
  22. $sr_created_value = '60';
  23. //amount of days a SP gets to perform a login and so he can keep his entity in SURFconext
  24. $nextlogin_value = '14';
  25. //set current date
  26. $date = date('d-m-Y');
  27. //get expired entities and entities that can be removed from files
  28. if (file_exists("expired.json")){
  29. $get_expired_check = json_decode(file_get_contents("expired.json"), true);
  30. if (file_exists("remove.json")){
  31. $get_remove_check = json_decode(file_get_contents("remove.json"), true);
  32. foreach($get_expired_check as $eidkey => $datevalue){
  33. if (strcmp ($datevalue, $date) == 0) {
  34. print_r($datevalue);
  35. array_push($get_remove_check[$eidkey]="");
  36. unset($get_expired_check[$eidkey]);
  37. }
  38. }
  39. file_put_contents("remove.json",json_encode($get_remove_check));
  40. file_put_contents("expired.json",json_encode($get_expired_check));
  41. }
  42. }
  43. try {
  44. $conn = new PDO('mysql:host=localhost;dbname=stats', $db_username, $db_password);
  45. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  46. print_r("Script running, started at: ");
  47. print_r(date("m/d/y G.i:s", time())."\n");
  48. // Check for Test SPs that had their last login more than x days ago
  49. $q_expired = $conn->prepare("
  50. SELECT c.id, c.revisionNr, c.name, l.maxstamp
  51. FROM sr.janus__connection c
  52. INNER JOIN
  53. (SELECT spentityid, MAX(loginstamp) as maxstamp
  54. FROM stats.log_logins
  55. GROUP BY spentityid
  56. HAVING MAX(loginstamp) < (now() - interval $lastlogin_value DAY)) l
  57. ON c.name = l.spentityid
  58. INNER JOIN
  59. (SELECT eid, revisionid, state
  60. FROM sr.janus__connectionRevision
  61. WHERE type LIKE $type
  62. AND state LIKE $state) r
  63. ON c.id = r.eid
  64. AND c.revisionNr = r.revisionid
  65. GROUP BY c.id
  66. ");
  67. //execute x days expired check and fill array
  68. print_r("Executing x days expired\n");
  69. $q_expired->execute();
  70. $arr_expired = $q_expired->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  71. //print_r($arr_expired);
  72. print_r(count($arr_expired)."\n");
  73. //check for Test SPs that had their last login more than 0 days ago for comparison with JANUS entities
  74. $q_stats = $conn->prepare("
  75. SELECT c.id, c.revisionNr, c.name, l.maxstamp
  76. FROM sr.janus__connection c
  77. INNER JOIN
  78. (SELECT spentityid, MAX(loginstamp) as maxstamp
  79. FROM stats.log_logins
  80. GROUP BY spentityid
  81. HAVING MAX(loginstamp) < (now() - interval '0' DAY)) l
  82. ON c.name = l.spentityid
  83. INNER JOIN
  84. (SELECT eid, revisionid, state
  85. FROM sr.janus__connectionRevision
  86. WHERE type LIKE $type
  87. AND state LIKE $state) r
  88. ON c.id = r.eid
  89. AND c.revisionNr = r.revisionid
  90. GROUP BY c.id
  91. ");
  92. //execute 0 days expired check and fill array
  93. sleep(5);
  94. print_r("Executing 0 days expired\n");
  95. $q_stats->execute();
  96. $arr_stats = $q_stats->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  97. print_r(count($arr_stats)."\n");
  98. //check for Test SPs in JANUS that are created more than x days ago
  99. //otherwise new SP's would receive notifications that they will be removed
  100. $q_janusentities = $conn->prepare("
  101. SELECT c.id, c.revisionNr, c.name, c.created
  102. FROM sr.janus__connection c
  103. INNER JOIN
  104. (SELECT eid, revisionid, state
  105. FROM sr.janus__connectionRevision
  106. WHERE type LIKE $type
  107. AND state LIKE $state) r
  108. ON c.id = r.eid
  109. AND c.revisionNr = r.revisionid
  110. WHERE c.created < (now() - interval $sr_created_value DAY)
  111. GROUP BY c.id
  112. ");
  113. //execute JANUS entities > x days check and fill array
  114. sleep(5);
  115. print_r("Executing JANUS created > x days\n");
  116. $q_janusentities->execute();
  117. $arr_janusentities = $q_janusentities->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  118. //print_r($arr_janusentities);
  119. print_r(count($arr_janusentities)."\n");
  120. //check for entries that contain a value for the 'coin:gadgetbaseurl' attribute (OAuth) and where ACS Location is empty
  121. $q_oauth = $conn->prepare("
  122. SELECT m.eid, c.revisionNr, m.key, m.value
  123. FROM stats.janus__metadata m
  124. INNER JOIN
  125. (SELECT id, revisionNr
  126. FROM sr.janus__connection
  127. GROUP BY id) c
  128. ON m.eid = c.id
  129. INNER JOIN
  130. (SELECT m.eid, m.key, m.value
  131. FROM stats.janus__metadata m
  132. WHERE m.key like 'coin:gadgetbaseurl'
  133. AND (m.value IS NOT NULL OR m.value NOT LIKE '' )) g
  134. ON m.eid = g.eid
  135. WHERE m.key like 'AssertionConsumerService:0:Location'
  136. AND m.value LIKE ''
  137. AND m.revisionid = c.revisionNr
  138. GROUP BY m.eid
  139. ");
  140. //execute OAuth check and fill array
  141. sleep(5);
  142. print_r("Executing Oauth check\n");
  143. $q_oauth->execute();
  144. $arr_oauth = $q_oauth->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  145. //print_r($arr_oauth);
  146. print_r(count($arr_oauth)."\n");
  147. //*** Compare and fill final array ***
  148. //compare found JANUS entries to 0 days entries and fill array with JANUS entries that are not in 0 days
  149. $arr_missing = array_diff_key($arr_janusentities,$arr_stats);
  150. //print_r($arr_missing);
  151. print_r(count($arr_missing)."\n");
  152. //combine the 180 days expired array with the missing entries from JANUS
  153. $arr_result = $arr_expired + $arr_missing;
  154. //check arr_result for entities that contain oauth gadgetbase urls and where ACS Location was empty remove them
  155. $arr_def = array_diff_key($arr_result, $arr_oauth);
  156. //create array $keys, fetch array keys from $arr_def and fill $keys with those values (eid's)
  157. $keys = array();
  158. foreach(array_keys($arr_def) as $key_def)
  159. array_push($keys, $key_def);
  160. file_put_contents('keys.txt', print_r($keys, true));
  161. //put all eid's in one string for use in SQL contacts query
  162. $eids = implode(',',$keys);
  163. file_put_contents('eids.txt', print_r($eids, true));
  164. //print_r($eids);
  165. //print_r("start contact query!\n");
  166. //execute contacts query with statement that it only returns records where the eid is in $eids
  167. $q_contacts = $conn->prepare("
  168. SELECT * FROM (
  169. SELECT * FROM (
  170. SELECT m.eid,
  171. m.revisionid,
  172. e.state,
  173. e.type,
  174. e.entityid,
  175. m.created as lastupdated,
  176. GROUP_CONCAT(IF (m.key = 'name:en',m.value,NULL)) AS 'name:en',
  177. GROUP_CONCAT(IF (m.key = 'contacts:0:emailAddress',m.value,NULL)) AS 'email',
  178. GROUP_CONCAT(IF (m.key = 'contacts:0:contactType',m.value,NULL)) AS 'contactType',
  179. GROUP_CONCAT(IF (m.key = 'contacts:0:givenName',m.value,NULL)) AS 'givenName',
  180. GROUP_CONCAT(IF (m.key = 'contacts:0:surName',m.value,NULL)) AS 'surName'
  181. FROM `stats`.`janus__metadata` m,
  182. (select e1.eid, e1.revisionid, e1.entityid, e1.state, e1.type from `sr`.`janus__connectionRevision` e1,
  183. (SELECT id, revisionNr AS lastrevisionid
  184. FROM `sr`.`janus__connection`
  185. GROUP by id) e2
  186. WHERE e1.eid = e2.id
  187. AND e1.revisionid = e2.lastrevisionid) e
  188. WHERE m.eid = e.eid
  189. AND m.revisionid = e.revisionid
  190. AND m.key IN ('contacts:0:emailAddress','contacts:0:contactType','contacts:0:givenName','contacts:0:surName')
  191. GROUP BY m.eid
  192. ORDER BY type, state, `name:en`) k
  193. where (contactType = 'technical' AND (email IS NOT NULL AND email != '' ))
  194. union
  195. SELECT * FROM (
  196. SELECT m.eid,
  197. m.revisionid,
  198. e.state,
  199. e.type,
  200. e.entityid,
  201. m.created as lastupdated,
  202. GROUP_CONCAT(IF (m.key = 'name:en',m.value,NULL)) AS 'name:en',
  203. GROUP_CONCAT(IF (m.key = 'contacts:1:emailAddress',m.value,NULL)) AS 'email',
  204. GROUP_CONCAT(IF (m.key = 'contacts:1:contactType',m.value,NULL)) AS 'contactType',
  205. GROUP_CONCAT(IF (m.key = 'contacts:1:givenName',m.value,NULL)) AS 'givenName',
  206. GROUP_CONCAT(IF (m.key = 'contacts:1:surName',m.value,NULL)) AS 'surName'
  207. FROM `stats`.`janus__metadata` m,
  208. (select e1.eid, e1.revisionid, e1.entityid, e1.state, e1.type from `sr`.`janus__connectionRevision` e1,
  209. (SELECT id, revisionNr AS lastrevisionid
  210. FROM `sr`.`janus__connection`
  211. GROUP by id) e2
  212. WHERE e1.eid = e2.id
  213. AND e1.revisionid = e2.lastrevisionid) e
  214. WHERE m.eid = e.eid
  215. AND m.revisionid = e.revisionid
  216. AND m.key IN ('contacts:1:emailAddress','contacts:1:contactType','contacts:1:givenName','contacts:1:surName')
  217. GROUP BY m.eid
  218. ORDER BY type, state, `name:en`) i
  219. where (contactType = 'technical' AND (email IS NOT NULL AND email != '' ))
  220. union
  221. SELECT * FROM (
  222. SELECT m.eid,
  223. m.revisionid,
  224. e.state,
  225. e.type,
  226. e.entityid,
  227. m.created as lastupdated,
  228. GROUP_CONCAT(IF (m.key = 'name:en',m.value,NULL)) AS 'name:en',
  229. GROUP_CONCAT(IF (m.key = 'contacts:2:emailAddress',m.value,NULL)) AS 'email',
  230. GROUP_CONCAT(IF (m.key = 'contacts:2:contactType',m.value,NULL)) AS 'contactType',
  231. GROUP_CONCAT(IF (m.key = 'contacts:2:givenName',m.value,NULL)) AS 'givenName',
  232. GROUP_CONCAT(IF (m.key = 'contacts:2:surName',m.value,NULL)) AS 'surName'
  233. FROM `stats`.`janus__metadata` m,
  234. ( select e1.eid, e1.revisionid, e1.entityid, e1.state, e1.type from sr.janus__connectionRevision e1,
  235. (SELECT id, revisionNr AS lastrevisionid
  236. FROM `sr`.`janus__connection`
  237. GROUP by id) e2
  238. WHERE e1.eid = e2.id
  239. AND e1.revisionid = e2.lastrevisionid) e
  240. WHERE m.eid = e.eid
  241. AND m.revisionid = e.revisionid
  242. AND m.key IN ('contacts:2:emailAddress','contacts:2:contactType','contacts:2:givenName','contacts:2:surName')
  243. GROUP BY m.eid
  244. ORDER BY type, state, `name:en`) o
  245. where (contactType = 'technical' AND (email IS NOT NULL AND email != '' )) ) a
  246. WHERE eid IN ($eids)
  247. GROUP BY eid
  248. ");
  249. $q_contacts->execute();
  250. $arr_contacts = $q_contacts->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
  251. //write arrays to files
  252. file_put_contents('janus.txt', print_r($arr_janusentities, true));
  253. file_put_contents('stats.txt', print_r($arr_stats, true));
  254. file_put_contents('oauth.txt', print_r($arr_oauth, true));
  255. file_put_contents('expired.txt', print_r($arr_expired, true));
  256. file_put_contents('missing.txt', print_r($arr_missing, true));
  257. file_put_contents('result.txt', print_r($arr_result, true));
  258. file_put_contents('def.txt', print_r($arr_def, true));
  259. file_put_contents('contacts.txt', print_r($arr_contacts, true));
  260. print_r("Total results (expired+missing)-oauth:" . (count($arr_def)) . "\n");
  261. //preparing for mailer
  262. //create array to keep record to which eid (contacts) have been emailed
  263. //check if file with mailed eid's exists, if not dont try to open, create empty array
  264. if (file_exists("expired.json")){
  265. $get_expired = json_decode(file_get_contents("expired.json"), true);}
  266. else { $get_expired = array();}
  267. if (file_exists("remove.json")){
  268. $get_remove = json_decode(file_get_contents("remove.json"), true);}
  269. else { $get_remove = array();}
  270. //TEST
  271. file_put_contents("expiredtest.json",json_encode($get_expired), true);
  272. //iterate through nested arrays
  273. foreach($arr_contacts as $mainkey => $item){
  274. $eid = $mainkey;
  275. if(!array_key_exists($eid, $get_expired) && !array_key_exists($eid, $get_remove)){
  276. foreach($item as $subkey=>$item2){
  277. foreach($item2 as $key=>$value){
  278. if ($key == "entityid")
  279. {$id = $value;}
  280. if ($key == "email")
  281. {$mail = $value;}
  282. if ($key == "givenName")
  283. {$firstname = $value;}
  284. if ($key == "surName")
  285. {$lastname = $value;}
  286. }
  287. }
  288. echo("entityid: ".$id."\n");
  289. echo("email: ".$mail."\n");
  290. echo("firstname: ".$firstname."\n");
  291. echo("lastname: ".$lastname."\n");
  292. echo("eid: ".$eid."\n");
  293. echo("\n");
  294. //set eid with expiration date in array
  295. $expired_set[$eid] = date('d-m-Y', strtotime($date. ' + ' . $nextlogin_value . ' days'));
  296. //create the message
  297. $message = Swift_Message::newInstance();
  298. //file is standard configured for testpurposes the next line is the one that emails to contacts
  299. //$message->setTo(array($mail => $firstname . ' ' . $lastname));
  300. //the TO mailaddress and name are set for testpurposes, for example the service desk address in the settings file
  301. $message->setTo(array($mail_to_address => $mail_to_name));
  302. $message->setSubject("Connection expired");
  303. $message->setBody('Dear, ' . $firstname . ' ' . $lastname .',
  304. According to our administration there is a connection registered on SURFconext in testmodus.
  305. Entityid: ' . $id . '
  306. Your contact details are registered with this connection.
  307. Either your Service Provider has not been used for over ' . $lastlogin_value . ' days with SURFconext.
  308. Or you are registered in SURFconext for over ' . $sr_created_value . ' days and did not have a login yet.
  309. If you want to maintain this connection and want to use this connection in the future:
  310. * Make sure you perform a login within the next ' . $nextlogin_value . ' days
  311. * Otherwise this connection will be removed automatically
  312. * Please do not reply to this email, if you have an urging question about this message please send an email to support@surfconext.nl');
  313. $message->setFrom(array($mail_from_address => $mail_from_name));
  314. // Send the email
  315. $mailer = Swift_Mailer::newInstance($transport);
  316. $mailer->send($message);
  317. }
  318. else{
  319. if(!array_key_exists($eid, $get_remove)){
  320. $datetext = $get_expired[$eid];
  321. $expired_set[$eid] = $datetext;}
  322. }
  323. }
  324. file_put_contents("expired.json",json_encode($expired_set), true);
  325. print_r("Script finished, ended at: ");
  326. print_r(date("m/d/y G.i:s", time())."\n");
  327. }
  328. catch(PDOException $e) {
  329. echo 'ERROR: ' . $e->getMessage();
  330. }
  331. ?>