class.ss_shortener.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /*
  3. * class.ss_shortener - URL Shortener Plugin Component
  4. * @link: http://salamzadeh.net/plugins/urlyar
  5. * @author Sasan Salamzadeh
  6. * @version 1.0
  7. */
  8. /*
  9. Class Description
  10. This is the main shortener driver for the URL Shortener plugin
  11. It dictates the supported services and functions of the URL shortener WordPress Plugin
  12. For usage, please refer to http://salamzadeh.net/plugins/urlyar/
  13. */
  14. /*
  15. *****************************************
  16. *
  17. * Dependencies
  18. *
  19. *****************************************
  20. */
  21. /*
  22. if ( !class_exists('urlyar_urlyar') ) :
  23. include( dirname(__FILE__) . '/ss_shortener/urlyar.urlyar.php' );
  24. endif;
  25. */
  26. /*
  27. *****************************************
  28. *
  29. * Main Class Declaration
  30. *
  31. *****************************************
  32. */
  33. if (!class_exists('ss_shortener')) :
  34. class ss_shortener
  35. {
  36. /*
  37. *****************************************
  38. *
  39. * Class Variables
  40. *
  41. *****************************************
  42. */
  43. private $service_keys = array();
  44. private $service_details = array();
  45. private $supported, //combined services
  46. $status, //class status (beta, stable, dev)
  47. $service = '', //service name
  48. $key = '', //keys
  49. $user = '', //user / identity
  50. $generic = '', //generic field to pass
  51. $shortener = ''; //shortener class
  52. private $shortener_modules = array(); //shortener modules
  53. /*
  54. *****************************************
  55. *
  56. * Constructors
  57. *
  58. *****************************************
  59. */
  60. //php 5.3.3
  61. public function __construct($status = 'dev')
  62. {
  63. $this->ss_shortener($status);
  64. }
  65. //backward compatibility
  66. public function ss_shortener($status = 'dev')
  67. {
  68. $this->status = $status;
  69. $this->init_modules();
  70. $this->supported = array_combine($this->service_keys, $this->service_details);
  71. }
  72. /*
  73. *****************************************
  74. *
  75. * Initialization
  76. *
  77. *****************************************
  78. */
  79. /*
  80. *****************************************
  81. * Load and Init modules
  82. *****************************************
  83. */
  84. private function init_modules()
  85. {
  86. $dirpath = dirname(__FILE__) . '/ss_shortener';
  87. $this->load_modules($dirpath);
  88. foreach ($this->shortener_modules as $module) {
  89. include($module);
  90. $mod = $this->is_module($module, true);
  91. $this->add_support($mod['classname']);
  92. }
  93. }
  94. /*
  95. *****************************************
  96. * Check if PHP file is a module.
  97. * Option to return $module information.
  98. *****************************************
  99. */
  100. public function is_module($module, $info = false)
  101. {
  102. $headers = array(
  103. 'name' => 'URLYar Name',
  104. 'description' => 'URLYar Description',
  105. 'version' => 'version',
  106. 'classname' => 'classname',
  107. );
  108. $mod = get_file_data($module, $headers);
  109. if (empty($mod['name']))
  110. return false;
  111. return ($info) ? $mod : true;
  112. }
  113. /*
  114. *****************************************
  115. * Scan a list of component classes
  116. * Put list into array
  117. * component classes
  118. *****************************************
  119. */
  120. private function load_modules($filepath)
  121. {
  122. $filepath = untrailingslashit($filepath);
  123. if (!$dir = opendir($filepath)) {
  124. return $files;
  125. }
  126. while (false !== ($file = readdir($dir))) {
  127. if ('.' == substr($file, 0, 1) || '.php' != substr($file, -4)) {
  128. continue;
  129. }
  130. $full_file = $filepath . '/' . $file;
  131. if (!is_file($full_file) || !$this->is_module($full_file)) {
  132. continue;
  133. }
  134. array_push($this->shortener_modules, $full_file);
  135. }
  136. closedir($dir);
  137. }
  138. /*
  139. *****************************************
  140. * Load a list of supported services from
  141. * component classes
  142. *****************************************
  143. */
  144. private function add_support($classname)
  145. {
  146. $list = new $classname();
  147. foreach ($list->api_list() as $keys => $values) {
  148. //name, type, sticky.
  149. //name, class, type
  150. $detail = array($values[0], $classname, $values[1]);
  151. if (empty($values[2])) {
  152. array_push($this->service_keys, $keys);
  153. array_push($this->service_details, $detail);
  154. } else {
  155. array_unshift($this->service_keys, $keys);
  156. array_unshift($this->service_details, $detail);
  157. }
  158. }
  159. }
  160. /*
  161. *****************************************
  162. * Component method... for use in another function
  163. * This loads the correct class or file into the common shortener variable.
  164. * Methods in different classes are the same.
  165. *****************************************
  166. */
  167. private function load_shortener()
  168. {
  169. $data = 'OK';
  170. $this->shortener = '';
  171. if (!empty($this->service) && in_array($this->service, $this->service_keys)) {
  172. $opt = $this->supported[$this->service];
  173. switch ($opt[2]) {
  174. case '5':
  175. if (empty($this->key)) {
  176. $data = 'Error in Generating Shortlink (Invalid or No Key)';
  177. }
  178. break;
  179. case '6':
  180. if (empty($this->key) || empty($this->user)) {
  181. $data = 'Error in Generating Shortlink (Invalid or No Username/Key)';
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. if ($data == 'OK') {
  188. //assign shortener
  189. $this->shortener = new $opt[1]($this->service, $this->status);
  190. //set configurations for object
  191. $this->shortener->config($this->key, $this->user, $this->generic);
  192. }
  193. } else { //end if in_array
  194. $data = 'Service not specified or not supported';
  195. }
  196. return $data;
  197. }//end load adapter
  198. /*
  199. *****************************************
  200. * Public initialization call
  201. *****************************************
  202. */
  203. public function init_shortener()
  204. {
  205. return $this->load_shortener();
  206. }
  207. /*
  208. *****************************************
  209. * Public link generation call
  210. *****************************************
  211. */
  212. public function generate($url)
  213. {
  214. $data = NULL;
  215. if (!empty($this->shortener)) {
  216. if (!empty($url)) {
  217. //check for empty request
  218. $data = $this->shortener->generate($url);
  219. } else {
  220. $data = ''; //'No URL specified';
  221. }
  222. } else {
  223. $data = ''; //'Shortener not initialized';
  224. }
  225. return $data;
  226. }
  227. /*
  228. *****************************************
  229. *
  230. * Configurations
  231. *
  232. *****************************************
  233. */
  234. public function config($service = '', $key = '', $user = '', $generic = '')
  235. {
  236. $this->service = $service;
  237. $this->key = $key;
  238. $this->user = $user;
  239. $this->generic = $generic;
  240. }
  241. public function set_user($value)
  242. {
  243. $this->user = $value;
  244. }
  245. public function set_key($value)
  246. {
  247. $this->key = $value;
  248. }
  249. public function set_service($value)
  250. {
  251. $this->service = $value;
  252. }
  253. public function set_generic($value)
  254. {
  255. $this->generic = $value;
  256. }
  257. /*
  258. *****************************************
  259. *
  260. * Misc
  261. *
  262. *****************************************
  263. */
  264. /*
  265. *****************************************
  266. * Public listing of services, users, and keys.
  267. *****************************************
  268. */
  269. public function service_list($format = 'keys')
  270. {
  271. $data = array();
  272. switch ($format) {
  273. case 'authuser':
  274. foreach ($this->supported as $keys => $values) {
  275. if ($values[2] == 1 || $values[2] == 3 || $values[2] == 4 || $values[2] == 6) {
  276. array_push($data, $keys);
  277. }
  278. }
  279. break;
  280. case 'requser':
  281. foreach ($this->supported as $keys => $values) {
  282. if ($values[2] == 4 || $values[2] == 6) {
  283. array_push($data, $keys);
  284. }
  285. }
  286. break;
  287. case 'authkey':
  288. foreach ($this->supported as $keys => $values) {
  289. if ($values[2] == 2 || $values[2] == 3 || $values[2] == 5 || $values[2] == 6) {
  290. array_push($data, $keys);
  291. }
  292. }
  293. break;
  294. case 'reqkey':
  295. foreach ($this->supported as $keys => $values) {
  296. if ($values[2] == 5 || $values[2] == 6) {
  297. array_push($data, $keys);
  298. }
  299. }
  300. break;
  301. case 'detailed':
  302. $data = $this->supported;
  303. break;
  304. case 'keys':
  305. default:
  306. $data = $this->service_keys;
  307. break;
  308. }
  309. return $data;
  310. }
  311. /*
  312. *****************************************
  313. * Public check status of shortener
  314. *****************************************
  315. */
  316. public function load_status()
  317. {
  318. return (empty($this->shortener)) ? false : true;
  319. }
  320. /*
  321. *****************************************
  322. * Public list modules
  323. *****************************************
  324. */
  325. public function get_modules()
  326. {
  327. $list = array();
  328. foreach ($this->shortener_modules as $module) {
  329. $mod = $this->is_module($module, true);
  330. $mod['path'] = $module;
  331. array_push($list, $mod);
  332. }
  333. return $list;
  334. }
  335. //end class
  336. }
  337. endif;
  338. ?>