Advertisement
NFL

API v.1

NFL
Aug 25th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.11 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this template, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7.  
  8. /**
  9.  * Description of APIController
  10.  *
  11.  * @author Admin
  12.  *
  13.  * Для верификации нужно отправить POST-запрос на /api в поле data
  14.  * В ответе придет подпись, которую необходимо отправить на /api/json либо /api/xml
  15.  * Формат XML:
  16.  * <request>
  17.  *  <api_id>API_ID</api_id>
  18.  *  <api_user_id>API_USER_ID</api_user_id>
  19.  *  <signature>SIGNATURE</signature>
  20.  * <request>
  21.  * Генерация SIGNATURE: base64_encode(md5(API_USER_ID.md5(SECRET_KEY)))
  22.  * API_USER_ID и SECRET_KEY генерируются в момент активации партнера администратором.
  23.  * SECRET_KEY может быть изменен партнером в любой момент (требует правок партнерского скрипта)
  24.  *
  25.  * После отправки запрос придет ответ следующего формата:
  26.  * <response>
  27.  *  <result>(0/1/2/3)</result>
  28.  *  <result_description>(success или информация об ошибке)<result_descrription>
  29.  *  <signature>SIGNATURE_FROM_SCRIPT</signature>
  30.  * </response>
  31.  * Подпись, полученную в response нужно подставить без изменений в скрипт запроса данных.
  32.  *
  33.  * Формат скрипта запроса данных:
  34.  * <request>
  35.  *  <api_id>API_ID</api_id>
  36.  *  <signature>SIGNATURE_FROM_SCRIPT</signature>
  37.  *  <count>количество записей, необязательно. Не указано - возвращаются все доступные</count>
  38.  * </request>
  39.  *
  40.  * Сформированный пакет нужно отправить на /api/xml или /api/json методом POST.
  41.  *
  42.  * В ответ придет XML/JSON следующего формата:
  43.  *
  44.  * <response>
  45.  * <status>0/1/2/3</status>
  46.  * <api_id>API_ID</api_id>
  47.  * <status_text>success или информация оь ошибке</status_text>
  48.  *  <data>
  49.  *  <id></id>
  50.  *  <alias></alias>
  51.  *  <author_name></author_name>
  52.  *  <author_surname></author_surname>
  53.  *  <genre></genre>
  54.  *  <price></price>
  55.  * </data>
  56.  * <data>
  57.  *  <id></id>
  58.  *  <buy_uri></buy_uri>
  59.  *  <author_name></author_name>
  60.  *  <author_surname></author_surname>
  61.  *  <genre></genre>
  62.  *  <price></price>
  63.  * </data>
  64.  * </response>
  65.  */
  66. class APIController extends Zend_Controller_Action {
  67.  
  68.     private $_modelAPIUsers = null;
  69.     private $_modelAPIStats = null;
  70.     private $_modelAPISettings = null;
  71.     private $_modelCatalog = null;
  72.     private $_modelAuthors = null;
  73.     private $_modelConfig = null;
  74.     private $_PartnerPriceRatio = 1;
  75.  
  76.     public function init() {
  77.  
  78.         $this->_modelAPIUsers = new Application_Model_Api_Users();
  79.         $this->_modelAPIStats = new Application_Model_Api_Stats();
  80.         $this->_modelAPISettings = new Application_Model_Api_Settings();
  81.         $this->_modelCatalog = new Application_Model_Catalog_Table();
  82.         $this->_modelAuthors = new Application_Model_Catalog_Author();
  83.         $this->_modelConfig = new Application_Model_Settings_Site();
  84.     }
  85.  
  86.     public function indexAction() {
  87.         if ($this->getRequest()->isPost()) {
  88.             $data = $this->getRequest()->getPost();
  89.             // exit(base64_decode($data['data']));
  90.             $data = simplexml_load_string(base64_decode($data['data']));
  91.             //  exit($data->api_id);
  92.             $code = $this->_modelAPIUsers->verify($data->api_id, $data->user_id, $data->signature);
  93.             if ($code === true) {
  94.                 $signature = base64_encode(str_rot13(md5($data->signature)));
  95.                 $response = '<?xml version="1.0" encoding="UTF-8"?>
  96.               <response>
  97.                <result>0</result>
  98.                <result_description>success</result_description>
  99.                <signature>' . $signature . '</signature>
  100.               </response>';
  101.             } else {
  102.                 switch ($code) {
  103.                     case 3:
  104.                         $response = '<?xml version="1.0" encoding="UTF-8"?>
  105.                    <response>
  106.                        <result>3</result>
  107.                        <result_description>Authorisation failed: Incorrect signature or API ID</result_description>
  108.                        <signature>NULL</signature>
  109.                    </response>';
  110.                         break;
  111.                     case 1:
  112.                         $response = '<?xml version="1.0" encoding="UTF-8"?>
  113.                    <response>
  114.                        <result>1</result>
  115.                        <result_description>Request failed: Your API ID is NOT yet activated</result_description>
  116.                        <signature>NULL</signature>
  117.                    </response>';
  118.                         break;
  119.                 }
  120.             }
  121.         } else {
  122.             $response = '
  123.             <?xml version="1.0" encoding="UTF-8"?>
  124.               <response>  
  125.                  <result>2</result>
  126.                  <result_description>NO POST DATA PROVIDED</result_description>
  127.                  <signature />
  128.               </response>';
  129.         }
  130.         echo $response;
  131.         exit;
  132.     }
  133.  
  134.     public function xmlAction() {
  135.         if ($this->getRequest()->isPost()) {
  136.             $response = '';
  137.             $data = $this->getRequest()->getPost();
  138.             $request = base64_decode($data['data']);
  139.             $obj = simplexml_load_string($request);
  140.             $data = $this->_modelAPIUsers->get($obj->api_id);
  141.             Zend_Debug::dump($data);
  142.             $sig = base64_encode(md5($data['api_id'] . md5($data['secret_key'])));
  143.             $sign = base64_encode(str_rot13(md5($sig)));
  144.             if ($sign == $obj->signature) {
  145.                 $apiId = (int) $data['api_id'];
  146.                 $data = $this->_modelCatalog->getList($obj->count);
  147.                 $this->_PartnerPriceRatio = $this->_modelAPISettings->getPartnerPriceRatio($apiId);
  148.                 //var_dump($this->_PartnerPriceRatio); exit;
  149.                 foreach ($data as $i => $v) {
  150.                     $data[$i]['price']*=$this->_PartnerPriceRatio;
  151.                 }
  152.                 Zend_Debug::dump($data);
  153.                 $mainSiteUri = trim($this->_modelConfig->get('site_url'), '/');
  154.                 $response = '<response>
  155.                    <status>0</status>
  156.                    <api_id>' . $data['api_id'] . '</api_id>
  157.                    <status_text>success</status_text>';
  158.                 foreach ($data as $v) {
  159.                     $author = $this->_modelAuthors->get($v['author_id']);
  160.                     $response.='<data>';
  161.                     $response.='<id>' . $v['id'] . '</id>';
  162.                     $response.='<alias>' . $v['id'] . '</alias>';
  163.                     $response.='<author_name>' . $author['name'] . '</author_name>';
  164.                     $response.='<author_surname>' . $author['surname'] . '</author_surname>';
  165.                     $response.='<genre>' . $v['genre_title'] . '</genre>';
  166.                     $response.='<price>' . $v['price'] . '</price>';
  167.                     $response.='<buy_url>' . $mainSiteUri . '/bp/' . base64_encode((md5($v['id']) . '_' . md5($v['api_id']))) . '</buy_uri>';
  168.                     $response.='</data>';
  169.                 }
  170.                 $response.='</response>';
  171.             } else {
  172.                 $response = '<response>
  173.                    <status>1</status>
  174.                    <api_id>' . $data['api_id'] . '</api_id>
  175.                    <status_text>Signature check failed: signature incorret</status_text>
  176.                    </response>';
  177.             }
  178.         } else {
  179.             $response = '<response>
  180.                    <status>3</status>
  181.                    <api_id>' . $data['api_id'] . '</api_id>
  182.                    <status_text>No POST data provided</status_text>
  183.                    </response>';
  184.         }
  185.         echo $response;
  186.         exit;
  187.     }
  188.  
  189.     public function jsonAction() {
  190.         /**
  191.          *IN DEVELOPMENT
  192.          */
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement