urlyar.urlyar.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * URLYar Name: URLYar Support
  4. * URLYar Description: This component adds support for URLYar's URL Shortening Service
  5. * classname: urlyar_urlyar
  6. * version: 1.1.0
  7. * link: https://salamzadeh.net
  8. * author: Sasan Salamzadeh
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  11. /*
  12. *****************************************
  13. *
  14. * Dependencies
  15. *
  16. *****************************************
  17. */
  18. if (!class_exists('urlyar_shared')) :
  19. include(dirname(__FILE__) . '/lib/urlyar.shared.php');
  20. endif;
  21. /*
  22. *****************************************
  23. *
  24. * Main Class Declaration
  25. *
  26. *****************************************
  27. */
  28. if (!class_exists('urlyar_urlyar')) :
  29. class urlyar_urlyar extends urlyar_shared
  30. {
  31. /*
  32. *****************************************
  33. *
  34. * Class Variables
  35. *
  36. *****************************************
  37. */
  38. private $api_config = array(
  39. 'urlyar' => array(
  40. 'name' => 'URLYar',
  41. 'endpoint' => 'http://urlyar.ir/api/',
  42. 'format' => 'json',
  43. 'ua' => FALSE,
  44. 'override' => TRUE,
  45. 'method' => 'GET',
  46. 'type' => 2,
  47. 'sticky' => TRUE,
  48. ),
  49. );
  50. /*
  51. *****************************************
  52. *
  53. * Constructors
  54. *
  55. *****************************************
  56. */
  57. //php 5.3.3
  58. public function __construct($service = NULL, $status = 'dev')
  59. {
  60. $this->urlyar_urlyar($service, $status);
  61. }
  62. //backward compatibility
  63. public function urlyar_urlyar($service = NULL, $status = 'dev')
  64. {
  65. $this->api_name = 'urlyar';
  66. $this->loaded_api = $this->api_config['urlyar'];
  67. $this->status = $status;
  68. }
  69. /*
  70. *****************************************
  71. *
  72. * Methods
  73. *
  74. *****************************************
  75. */
  76. /*
  77. *****************************************
  78. * Config Requirements
  79. *****************************************
  80. */
  81. public function set_service($service,$api_config = '')
  82. {
  83. parent::set_service('urlyar', $this->api_config['urlyar']);
  84. }
  85. public function config($key = '', $user = '',$loaded_api='', $generic = '')
  86. {
  87. $loaded_api = $this->api_config['urlyar'];
  88. parent::config($key, $user, $loaded_api, $generic);
  89. }
  90. public function api_list($list='')
  91. {
  92. return parent::api_list($this->api_config);
  93. }
  94. /*
  95. *****************************************
  96. * Main Generator
  97. *****************************************
  98. */
  99. public function generate($url)
  100. {
  101. $this->url = $url;
  102. if ($this->url) {
  103. $request_url = $this->loaded_api['endpoint'] ."url/add";
  104. $body = json_encode(["url" => $this->url]);
  105. if ($this->key) {
  106. $post_opt['httpheader'] = array('Content-Type: application/json',
  107. 'Authorization: Bearer '.$this->key);
  108. } else {
  109. echo __("Please use a valid API Key");
  110. return "";
  111. }
  112. if ($this->loaded_api['ua']) {
  113. $post_opt['useragent'] = $ua_string;
  114. }
  115. $result = parent::request_gateway($request_url, TRUE, "POST", $post_opt, $body);
  116. //die($result);
  117. if ($result) {
  118. $result = parent::json_process($result);
  119. if($result->error == 1){
  120. echo $result->message;
  121. $result = '';
  122. }
  123. else
  124. $result = $result->shorturl;
  125. }
  126. //remove url request
  127. $this->url = '';
  128. return $result;
  129. }
  130. }
  131. //end class
  132. }
  133. endif;
  134. ?>