Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. <?php
  2. //Main WP-DGL Class
  3. class WP_DGL {
  4. /**
  5. * Adds the required hooks.
  6. */
  7. public function __construct() {
  8. add_action( 'admin_init', array( $this, 'settings_init' ) );
  9. add_action( 'init', array( $this, 'wp_dgl_shortcodes_init' ) );
  10.  
  11. //$this->load_textdomain();
  12. }
  13.  
  14. /**
  15. * Inits the settings page
  16. */
  17. public function settings_init() {
  18. add_settings_section(
  19. 'wp_dgl_dgl_settings',
  20. esc_html__( 'WP DGL Settings', 'wp-dgl' ),
  21. array( $this, 'settings_callback' ),
  22. 'general'
  23. );
  24.  
  25. add_settings_field(
  26. 'wp_dgl_dgl_username',
  27. esc_html__( 'Account Username', 'wp-dgl' ),
  28. array( $this, 'print_dgl_username_field' ),
  29. 'general',
  30. 'wp_dgl_dgl_settings'
  31. );
  32.  
  33. add_settings_field(
  34. 'wp_dgl_dgl_password',
  35. esc_html__( 'Account Password', 'wp-dgl' ),
  36. array( $this, 'print_dgl_password_field' ),
  37. 'general',
  38. 'wp_dgl_dgl_settings'
  39. );
  40.  
  41. register_setting( 'general', 'wp_dgl_dgl_username' );
  42. register_setting( 'general', 'wp_dgl_dgl_password' );
  43. }
  44.  
  45. /**
  46. * Prints the description in the settings page.
  47. */
  48. public function settings_callback() {
  49. esc_html_e( 'Setup Up DGL website access.', 'wp-dgl' );
  50. }
  51.  
  52. /**
  53. * Prints the Bot Username settings field.
  54. */
  55. public function print_dgl_username_field() {
  56. $value = get_option( 'wp_dgl_dgl_username' );
  57.  
  58. echo '<input type="text" name="wp_dgl_dgl_username" value="' . esc_attr( $value ) . '" style="width:300px;margin-right:10px;" />';
  59. echo '<span class="description">' . esc_html__( 'The username of the account.', 'wp-dgl' ) . '</span>';
  60. }
  61.  
  62. /**
  63. * Prints the Avatar URL settings field.
  64. */
  65. public function print_dgl_password_field() {
  66. $value = get_option( 'wp_dgl_dgl_password' );
  67.  
  68. echo '<input type="text" name="wp_dgl_dgl_password" value="' . esc_attr( $value ) . '" style="width:300px;margin-right:10px;" />';
  69. echo '<span class="description">' . esc_html__( 'The password of the account.', 'wp-dgl' ) . '</span>';
  70. }
  71.  
  72. /**
  73. * Loads the plugin localization files.
  74.  
  75. public function load_textdomain() {
  76. $locale = apply_filters( 'plugin_locale', get_locale(), 'wp-dgl' );
  77. load_textdomain( 'wp-dgl', WP_LANG_DIR . '/wp-discord-post/discord-post-' . $locale . '.mo' );
  78. load_plugin_textdomain( 'wp-dgl', false, plugin_basename( __DIR__ ) . '/languages' );
  79. }*/
  80.  
  81. public function get_cups() {
  82. //username and password of account
  83. $username = get_option( 'wp_dgl_dgl_username' );
  84. $password = get_option( 'wp_dgl_dgh_password' );
  85.  
  86. if (is_null($username) or is_null($password))
  87. {
  88. return "Please check the settings!";
  89. }
  90.  
  91. //set the directory for the cookie using defined document root var
  92. $dir = plugin_dir_path( __FILE__ );
  93. //build a unique path with every request to store
  94. //the info per user with custom func.
  95. $path = $dir;
  96.  
  97. //login form action url
  98. $url="https://example.com/login.php";
  99. $postinfo = "password=".$password."&username=".$username;
  100.  
  101. $cookie_file_path = $path."/cookies.txt";
  102. $request_headers = [
  103. 'Accept: */*',
  104. 'Accept-Encoding: gzip, deflate',
  105. 'Content-Type: application/x-www-form-urlencoded',
  106. ];
  107.  
  108. $ch = curl_init();
  109. curl_setopt($ch, CURLOPT_HEADER, 1);
  110. curl_setopt($ch, CURLOPT_NOBODY, false);
  111. curl_setopt($ch, CURLOPT_URL, $url);
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  113.  
  114. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  115. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  116. //set the cookie the site has for certain features, this is optional
  117. curl_setopt($ch, CURLOPT_COOKIE, "cookiename=dgl");
  118. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
  119. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  120. curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
  121. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  122. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  123.  
  124. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  125. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  126. curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
  127. curl_setopt($ch, CURLOPT_ENCODING, "");
  128. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  129.  
  130. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  131. curl_setopt($ch, CURLOPT_POST, 1);
  132. curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
  133. curl_exec($ch);
  134.  
  135. if(curl_error($ch)) //Catch errors and return them
  136. {
  137. $info = curl_getinfo($ch);
  138. return 'Error: ' . curl_error($ch) . "<br>Info: " . $info['request_header'];
  139. }
  140. elseif ($cups_raw == null) //If the text is null return failed.
  141. {
  142. return "Failed to recieve anything";
  143. }
  144.  
  145. //page with the content I want to grab
  146. curl_setopt($ch, CURLOPT_POST, 0);
  147. curl_setopt($ch, CURLOPT_URL, "https://example.com/api/json");
  148. //do stuff with the info with DomDocument() etc
  149. $cups_raw = curl_exec($ch);
  150.  
  151. if(curl_error($ch))
  152. {
  153. $info = curl_getinfo($ch);
  154. return 'Error: ' . curl_error($ch) . "<br>Info: " . $info['request_header'];
  155. }
  156. elseif ($cups_raw == null)
  157. {
  158.  
  159. curl_close($ch);
  160. return "Failed to recieve anything";
  161. }
  162.  
  163. curl_close($ch);
  164.  
  165. return $cups_raw;
  166. }
  167.  
  168. /**
  169. Creates The Shortcodes to be used
  170. */
  171. public function wp_dgl_shortcodes_init()
  172. {
  173. function dgl_cups_shortcode($atts = [], $content = null)
  174. {
  175. return WP_DGL::get_cups();
  176. }
  177. add_shortcode('dgl_cups', 'dgl_cups_shortcode');
  178.  
  179. function dgl_clan_shortcode($atts = [], $content = null)
  180. {
  181. return "<strong><em>NOT SETUP YET</em></strong>";
  182. }
  183. add_shortcode('dgl_clan', 'dgl_clan_shortcode');
  184.  
  185. function dgl_team_shortcode($atts = [], $content = null)
  186. {
  187. return "<strong><em>NOT SETUP YET</em></strong>";
  188. }
  189. add_shortcode('dgl_team', 'dgl_team_shortcode');
  190.  
  191. function dgl_user_shortcode($atts = [], $content = null)
  192. {
  193. return "<strong><em>NOT SETUP YET</em></strong>";
  194. }
  195. add_shortcode('dgl_user', 'dgl_user_shortcode');
  196. }
  197. }
  198.  
  199. new WP_DGL();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement