PluginProbe
ONTRApages / 1.2
ONTRApages v1.2
trunk 1.1.7 1.1.8 1.2 1.2.10 1.2.11 1.2.12
ontrapages / OPCoreFunctions.php

OPCoreFunctions.php in ONTRApages 1.2, at OPCoreFunctions.php

147 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Manages the API requests and any other core functionality that gets used accross multiple classes.
3 class OPCoreFunctions
4 {
5 // Performs an API request to api.ontrapages.com or api.ontraport.com/1/. Returns the API response.
6 public static function apiRequest( $requestUrl, $appid, $key, $httpCodeOnly=false )
7 {
8 $session = curl_init( $requestUrl );
9 curl_setopt( $session, CURLOPT_HTTPHEADER, array("Api-Appid: $appid", "Api-Key: $key") );
10 curl_setopt( $session, CURLOPT_HEADER, false );
11
12 if ( $httpCodeOnly !== false )
13 {
14 curl_setopt( $session, CURLOPT_HEADER, true );
15 curl_setopt( $session, CURLOPT_NOBODY, true );
16 }
17
18 curl_setopt( $session, CURLOPT_RETURNTRANSFER, true );
19 curl_setopt( $session, CURLOPT_SSL_VERIFYPEER, false );
20 curl_setopt( $session, CURLOPT_SSL_VERIFYHOST, 0 );
21 $response = curl_exec( $session );
22
23 if ( $httpCodeOnly !== false )
24 {
25 $httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);
26 return $httpcode;
27 }
28
29 return $response;
30 }
31
32
33 // Get's the contents of the URL. Returns the HTML.
34 public static function getURLContent( $url )
35 {
36 $timeout = 5;
37 $session = curl_init();
38 curl_setopt($session, CURLOPT_URL, $url);
39 curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
40 curl_setopt($session, CURLOPT_CONNECTTIMEOUT, $timeout);
41 $html = curl_exec($session);
42 curl_close($session);
43
44 return $html;
45 }
46
47
48 /**
49 * Check db options to see if there are valid api creds.
50 *
51 * @param $returnType You can pass 'echo' or 'code' as the return type. If echo then it will echo the response message. If code it will just return the response code. If nothing then it will return the entire response.
52 * @param $emptyObject If there is an empty object check it to see if it's an auth error. If so let the user know.
53 *
54 * @return Echos a warning message if not and returns an error code.
55 *
56 * Error Code 0 - All good in the hood
57 * Error Code 1 - API Creds are wrong
58 * Error Code 2 - Missing API Creds
59 * Error Code 3 - No ONTRApages available
60 */
61 public static function checkAPICreds( $returnType=false, $emptyObject=false )
62 {
63 $appid = get_option( 'opAppID' );
64 $validCreds = get_option( 'opValidCreds' );
65
66 if ( $appid === false )
67 {
68 $code = 2;
69 }
70 else if ( $validCreds !== false && $validCreds == '0' )
71 {
72 $code = 1;
73 }
74 else if ( $validCreds !== false && $validCreds == '1' && $emptyObject !== false )
75 {
76 if ( $emptyObject == 'auth-error' )
77 {
78 $code = 1;
79 }
80 else
81 {
82 $code = 3;
83 }
84 }
85 else
86 {
87 $code = 0;
88 }
89
90 $response = self::buildResponse( $code );
91
92 if ( $returnType === 'echo' || $returnType === '' )
93 {
94 echo $response['message'];
95 }
96 else if ( $returnType === 'code' )
97 {
98 return $response['code'];
99 }
100 else
101 {
102 return $response;
103 }
104 }
105
106
107 /**
108 * Takes the code that it gets passed and builds a response with the code and message
109 * @param integer $code A response code
110 *
111 * @return array A complete response with a code and message
112 */
113 public static function buildResponse( $code )
114 {
115 switch( $code )
116 {
117 case 0:
118 $message = 'Success';
119 break;
120
121 case 1:
122 $message = '<div class="op-error-message">It looks like your API credentials are incorrect. Your ONTRApages will not display properly until you fix this. Please <a href="edit.php?post_type=ontrapage&page=opsettings">click here</a> to remedy that & then try again.</div>';
123 break;
124
125 case 2:
126 $message = '<div id="message" class="error is-dismissible">
127 <p>ONTRApages Warning - It looks like you don\'t have any API credentials setup just yet. <a href="edit.php?post_type=ontrapage&page=opsettings">Click here</a> to remedy that & then try again.</p>
128 </div>';
129 break;
130
131 case 3:
132 $message = '<div class="op-error-message">Unfortunately it appears that you don\'t have any ONTRApages available. Create one by logging into your <a href="https://ontrapages.com" target="_blank">ONTRApages account</a> and then return when it has been saved!</div>';
133 break;
134
135 default:
136 $message = '';
137 break;
138 }
139
140 $response = array(
141 'code' => $code,
142 'message' => $message
143 );
144
145 return $response;
146 }
147 }