| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- <?php
- /*
- * class.ss_shortener - URL Shortener Plugin Component
- * @link: http://salamzadeh.net/plugins/urlyar
- * @author Sasan Salamzadeh
- * @version 1.0
- */
- /*
- Class Description
- This is the main shortener driver for the URL Shortener plugin
- It dictates the supported services and functions of the URL shortener WordPress Plugin
- For usage, please refer to http://salamzadeh.net/plugins/urlyar/
-
- */
- /*
- *****************************************
- *
- * Dependencies
- *
- *****************************************
- */
- /*
- if ( !class_exists('urlyar_urlyar') ) :
- include( dirname(__FILE__) . '/ss_shortener/urlyar.urlyar.php' );
- endif;
- */
- /*
- *****************************************
- *
- * Main Class Declaration
- *
- *****************************************
- */
- if (!class_exists('ss_shortener')) :
- class ss_shortener
- {
- /*
- *****************************************
- *
- * Class Variables
- *
- *****************************************
- */
- private $service_keys = array();
- private $service_details = array();
- private $supported, //combined services
- $status, //class status (beta, stable, dev)
- $service = '', //service name
- $key = '', //keys
- $user = '', //user / identity
- $generic = '', //generic field to pass
- $shortener = ''; //shortener class
- private $shortener_modules = array(); //shortener modules
- /*
- *****************************************
- *
- * Constructors
- *
- *****************************************
- */
- //php 5.3.3
- public function __construct($status = 'dev')
- {
- $this->ss_shortener($status);
- }
- //backward compatibility
- public function ss_shortener($status = 'dev')
- {
- $this->status = $status;
- $this->init_modules();
- $this->supported = array_combine($this->service_keys, $this->service_details);
- }
- /*
- *****************************************
- *
- * Initialization
- *
- *****************************************
- */
- /*
- *****************************************
- * Load and Init modules
- *****************************************
- */
- private function init_modules()
- {
- $dirpath = dirname(__FILE__) . '/ss_shortener';
- $this->load_modules($dirpath);
- foreach ($this->shortener_modules as $module) {
- include($module);
- $mod = $this->is_module($module, true);
- $this->add_support($mod['classname']);
- }
- }
- /*
- *****************************************
- * Check if PHP file is a module.
- * Option to return $module information.
- *****************************************
- */
- public function is_module($module, $info = false)
- {
- $headers = array(
- 'name' => 'URLYar Name',
- 'description' => 'URLYar Description',
- 'version' => 'version',
- 'classname' => 'classname',
- );
- $mod = get_file_data($module, $headers);
- if (empty($mod['name']))
- return false;
- return ($info) ? $mod : true;
- }
- /*
- *****************************************
- * Scan a list of component classes
- * Put list into array
- * component classes
- *****************************************
- */
- private function load_modules($filepath)
- {
- $filepath = untrailingslashit($filepath);
- if (!$dir = opendir($filepath)) {
- return $files;
- }
- while (false !== ($file = readdir($dir))) {
- if ('.' == substr($file, 0, 1) || '.php' != substr($file, -4)) {
- continue;
- }
- $full_file = $filepath . '/' . $file;
- if (!is_file($full_file) || !$this->is_module($full_file)) {
- continue;
- }
- array_push($this->shortener_modules, $full_file);
- }
- closedir($dir);
- }
- /*
- *****************************************
- * Load a list of supported services from
- * component classes
- *****************************************
- */
- private function add_support($classname)
- {
- $list = new $classname();
- foreach ($list->api_list() as $keys => $values) {
- //name, type, sticky.
- //name, class, type
- $detail = array($values[0], $classname, $values[1]);
- if (empty($values[2])) {
- array_push($this->service_keys, $keys);
- array_push($this->service_details, $detail);
- } else {
- array_unshift($this->service_keys, $keys);
- array_unshift($this->service_details, $detail);
- }
- }
- }
- /*
- *****************************************
- * Component method... for use in another function
- * This loads the correct class or file into the common shortener variable.
- * Methods in different classes are the same.
- *****************************************
- */
- private function load_shortener()
- {
- $data = 'OK';
- $this->shortener = '';
- if (!empty($this->service) && in_array($this->service, $this->service_keys)) {
- $opt = $this->supported[$this->service];
- switch ($opt[2]) {
- case '5':
- if (empty($this->key)) {
- $data = 'Error in Generating Shortlink (Invalid or No Key)';
- }
- break;
- case '6':
- if (empty($this->key) || empty($this->user)) {
- $data = 'Error in Generating Shortlink (Invalid or No Username/Key)';
- }
- break;
- default:
- break;
- }
- if ($data == 'OK') {
- //assign shortener
- $this->shortener = new $opt[1]($this->service, $this->status);
- //set configurations for object
- $this->shortener->config($this->key, $this->user, $this->generic);
- }
- } else { //end if in_array
- $data = 'Service not specified or not supported';
- }
- return $data;
- }//end load adapter
- /*
- *****************************************
- * Public initialization call
- *****************************************
- */
- public function init_shortener()
- {
- return $this->load_shortener();
- }
- /*
- *****************************************
- * Public link generation call
- *****************************************
- */
- public function generate($url)
- {
- $data = NULL;
- if (!empty($this->shortener)) {
- if (!empty($url)) {
- //check for empty request
- $data = $this->shortener->generate($url);
- } else {
- $data = ''; //'No URL specified';
- }
- } else {
- $data = ''; //'Shortener not initialized';
- }
- return $data;
- }
- /*
- *****************************************
- *
- * Configurations
- *
- *****************************************
- */
- public function config($service = '', $key = '', $user = '', $generic = '')
- {
- $this->service = $service;
- $this->key = $key;
- $this->user = $user;
- $this->generic = $generic;
- }
- public function set_user($value)
- {
- $this->user = $value;
- }
- public function set_key($value)
- {
- $this->key = $value;
- }
- public function set_service($value)
- {
- $this->service = $value;
- }
- public function set_generic($value)
- {
- $this->generic = $value;
- }
- /*
- *****************************************
- *
- * Misc
- *
- *****************************************
- */
- /*
- *****************************************
- * Public listing of services, users, and keys.
- *****************************************
- */
- public function service_list($format = 'keys')
- {
- $data = array();
- switch ($format) {
- case 'authuser':
- foreach ($this->supported as $keys => $values) {
- if ($values[2] == 1 || $values[2] == 3 || $values[2] == 4 || $values[2] == 6) {
- array_push($data, $keys);
- }
- }
- break;
- case 'requser':
- foreach ($this->supported as $keys => $values) {
- if ($values[2] == 4 || $values[2] == 6) {
- array_push($data, $keys);
- }
- }
- break;
- case 'authkey':
- foreach ($this->supported as $keys => $values) {
- if ($values[2] == 2 || $values[2] == 3 || $values[2] == 5 || $values[2] == 6) {
- array_push($data, $keys);
- }
- }
- break;
- case 'reqkey':
- foreach ($this->supported as $keys => $values) {
- if ($values[2] == 5 || $values[2] == 6) {
- array_push($data, $keys);
- }
- }
- break;
- case 'detailed':
- $data = $this->supported;
- break;
- case 'keys':
- default:
- $data = $this->service_keys;
- break;
- }
- return $data;
- }
- /*
- *****************************************
- * Public check status of shortener
- *****************************************
- */
- public function load_status()
- {
- return (empty($this->shortener)) ? false : true;
- }
- /*
- *****************************************
- * Public list modules
- *****************************************
- */
- public function get_modules()
- {
- $list = array();
- foreach ($this->shortener_modules as $module) {
- $mod = $this->is_module($module, true);
- $mod['path'] = $module;
- array_push($list, $mod);
- }
- return $list;
- }
- //end class
- }
- endif;
- ?>
|