summaryrefslogtreecommitdiff
blob: 480113dfccaa0f54fc0688d334ebc1fd541fa0e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

if ( ! class_exists( 'WMobilePack_Tokens' ) ) {

    /**
     * Class WMobilePack_Tokens
     *
     * Contains different methods for setting / getting tokens
     */
    class WMobilePack_Tokens
    {

        /**
         *
         * Method used to create a token for the comments form.
         *
         * The method returns a string formed using the encoded domain and a timestamp.
         *
         * @return string
         *
         */
        public static function get_token()
        {

            $token = md5(md5(get_bloginfo("wpurl")).WMP_CODE_KEY);

            // encode token again
            $token = base64_encode($token.'_'.strtotime('+1 hour'));

            // generate token
            return $token;
        }


        /**
         *
         * Method used to check if a generated token is valid.
         *
         * The method returns true if the token is valid and false otherwise.
         *
         * @param $token - string
         * @return bool
         *
         */
        public static function check_token($token)
        {

            if (base64_decode($token,true)){

                // decode token to get timestamp and encoded url
                $decoded_token = base64_decode($token,true);

                if (strpos($decoded_token, "_") !== FALSE) {

                    // get params
                    $arrParams = explode('_',$decoded_token);

                    if (is_array($arrParams) && !empty($arrParams) && count($arrParams) == 2) {

                        // check timestamp
                        if (time() < $arrParams[1]) {

                            // get the generated encoded domain
                            $generated_url = md5(md5(get_bloginfo("wpurl")).WMP_CODE_KEY);

                            // check encoded domain
                            if ($arrParams[0] ==  $generated_url)
                                return true;
                        }
                    }
                }
            }

            // by default return false;
            return false;
        }

    }
}