· 5 years ago · Aug 24, 2020, 07:00 AM
1<?php
2//BETA VERSION OF THE PHP's API
3//there's no traffic encryption in here, as php is a server sided language
4//remember, this is just a implementation of the universal api to php
5
6if(!isset($_SESSION))
7 session_start();
8
9class c_api{
10
11 public static function c_init($c_program_key, $c_api_key) {
12 //there's not an initialization function anymore in the universal api, so i just use this function to store the program key and enc key
13
14 $_SESSION["program_key"] = $c_program_key;
15 $_SESSION["api_key"] = $c_api_key;
16
17 //you can display the success messages by removing the if checks in the functions below
18 //the received values are the success bool, the response ( static response you can parse if you want ) and the message, that is used to show what happened
19 }
20
21 public static function c_login($c_username, $c_password){
22 $c_url = curl_init(self::$api_link . "ins_handler.php?type=login");
23 curl_setopt($c_url, CURLOPT_USERAGENT, self::$user_agent);
24 curl_setopt($c_url, CURLOPT_RETURNTRANSFER, true);
25
26 curl_setopt($c_url, CURLOPT_SSL_VERIFYHOST, false);
27 curl_setopt($c_url, CURLOPT_SSL_VERIFYPEER, false);
28 curl_setopt($c_url, CURLOPT_PINNEDPUBLICKEY, self::$pub_key);
29
30 $values = array(
31 "username" => $c_username,
32 "password" => $c_password,
33 "program_key" => $_SESSION["program_key"],
34 "api_key" => $_SESSION["api_key"]
35 );
36
37 curl_setopt($c_url, CURLOPT_POSTFIELDS, $values);
38
39 $result = json_decode(curl_exec($c_url)); curl_close($c_url);
40
41 $success = $result->success;
42
43 if($success != true)
44 c_api::alert($result->message);
45 else
46 $_SESSION["user_data"] = $result->user_data; //example call : $_SESSION["user_data"]->email
47
48 return $success;
49 }
50
51 public static function c_register($c_username, $c_email, $c_password, $c_token){
52 $c_url = curl_init(self::$api_link . "ins_handler.php?type=register");
53 curl_setopt($c_url, CURLOPT_USERAGENT, self::$user_agent);
54 curl_setopt($c_url, CURLOPT_RETURNTRANSFER, true);
55
56 curl_setopt($c_url, CURLOPT_SSL_VERIFYHOST, false);
57 curl_setopt($c_url, CURLOPT_SSL_VERIFYPEER, false);
58 curl_setopt($c_url, CURLOPT_PINNEDPUBLICKEY, self::$pub_key);
59
60 $values = array(
61 "username" => $c_username,
62 "email" => $c_email,
63 "password" => $c_password,
64 "token" => $c_token,
65 "program_key" => $_SESSION["program_key"],
66 "api_key" => $_SESSION["api_key"]
67 );
68
69 curl_setopt($c_url, CURLOPT_POSTFIELDS, $values);
70
71 $result = json_decode(curl_exec($c_url)); curl_close($c_url);
72
73 $success = $result->success;
74
75 if($success != true)
76 c_api::alert($result->message);
77
78 return $success;
79 }
80
81 public static function c_activate($c_username, $c_password, $c_token){
82 $c_url = curl_init(self::$api_link . "ins_handler.php?type=activate");
83 curl_setopt($c_url, CURLOPT_USERAGENT, self::$user_agent);
84 curl_setopt($c_url, CURLOPT_RETURNTRANSFER, true);
85
86 curl_setopt($c_url, CURLOPT_SSL_VERIFYHOST, false);
87 curl_setopt($c_url, CURLOPT_SSL_VERIFYPEER, false);
88 curl_setopt($c_url, CURLOPT_PINNEDPUBLICKEY, self::$pub_key);
89
90 $values = array(
91 "username" => $c_username,
92 "password" => $c_password,
93 "token" => $c_token,
94 "program_key" => $_SESSION["program_key"],
95 "api_key" => $_SESSION["api_key"]
96 );
97
98 curl_setopt($c_url, CURLOPT_POSTFIELDS, $values);
99
100 $result = json_decode(curl_exec($c_url)); curl_close($c_url);
101
102 $success = $result->success;
103
104 if($success != true)
105 c_api::alert($result->message);
106
107 return $success;
108 }
109
110 public static function c_all_in_one($c_token){
111 if(c_api::c_login($c_token, $c_token))
112 return true;
113
114 else if(c_api::c_register($c_token, $c_token . "@gmail.com", $c_token, $c_token))
115 return true;
116
117 return false;
118 }
119
120 public static function c_log($c_message){
121 if(empty($_SESSION["username"]) || !isset($_SESSION["username"])) $_SESSION["username"] = "NONE";
122
123 $c_url = curl_init(self::$api_link . "ins_handler.php?type=log");
124 curl_setopt($c_url, CURLOPT_USERAGENT, self::$user_agent);
125 curl_setopt($c_url, CURLOPT_RETURNTRANSFER, true);
126
127 curl_setopt($c_url, CURLOPT_SSL_VERIFYHOST, false);
128 curl_setopt($c_url, CURLOPT_SSL_VERIFYPEER, false);
129 curl_setopt($c_url, CURLOPT_PINNEDPUBLICKEY, self::$pub_key);
130
131 $values = array(
132 "username" => $_SESSION["username"],
133 "message" => $c_message,
134 "program_key" => $_SESSION["program_key"],
135 "api_key" => $_SESSION["api_key"]
136 );
137
138 curl_setopt($c_url, CURLOPT_POSTFIELDS, $values);
139
140 $result = json_decode(curl_exec($c_url)); curl_close($c_url);
141
142 $success = $result->success;
143
144 if($success != true)
145 c_api::alert($result->message);
146
147 return $success;
148 }
149
150 public static function alert($string){
151 echo "<script type=\"text/javascript\">";
152 echo "alert(\"$string\")"; //ugly
153 echo "</script>";
154 }
155
156 private static $user_agent = "Mozilla cAuth";
157 private static $api_link = "https://cauth.me/api/";
158 private static $pub_key = "sha256//Mk6vhbkCoRzUhXoUryC8tjIxmehtu4uLVhwqGQM9Cmc=";
159}
160