remove old readme
[atutor.git] / mods / _core / imscp / oauth / OAuthUtility.class.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 /**
16 * OAuth Utility functions 
17 * @access       public
18 * @author       Cindy Qi Li
19 */
20
21 if (!defined('AT_INCLUDE_PATH')) exit;
22
23 class OAuthUtility {
24
25         /**
26         * This function checks whether the given URL is accessible.
27         * @access  public
28         * @param   URL
29         * @return  true if accessible, otherwise, false
30         * @author  Cindy Qi Li
31         */
32         public static function isAccessible($URL)
33         {
34                 if (!@file_get_contents($URL))
35                         return false;
36                 return true;
37         }
38
39         /**
40          * This function checks whether the last access token for the current user
41          * is expired. If not, return it, otherwise, return empty.
42          * @access public
43          * @param  none 
44          * @return the access token if it's not expired, otherwise, empty.
45          * @author Cindy Qi Li
46          */
47         public static function getUnexpiredAccessToken()
48         {
49                 global $db;
50                 
51                 $sql = "SELECT token, 
52                                unix_timestamp(now()) now_timestamp, 
53                                ocs.expire_threshold,
54                                unix_timestamp(addtime(oct.assign_date, sec_to_time(ocs.expire_threshold))) expire_timestamp
55                           FROM ".TABLE_PREFIX."oauth_client_servers ocs, ".TABLE_PREFIX."oauth_client_tokens oct
56                          WHERE ocs.oauth_server_id=oct.oauth_server_id
57                            AND oct.member_id=".$_SESSION['member_id']."
58                            AND oct.token_type='access'
59                          ORDER BY oct.assign_date DESC";
60                 
61                 $result = mysql_query($sql, $db);
62                 
63                 if (mysql_num_rows($result) == 0) {
64                         return '';
65                 }
66                 else
67                 {
68                         $row = mysql_fetch_assoc($result);
69                         
70                         if ($row['expire_threshold'] == 0 || $row['now_timestamp'] < $row['expire_timestamp']) {
71                                 return $row['token'];
72                         } else {
73                                 return '';
74                         }
75                 }
76         }
77 }
78 ?>