Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of APIController
- *
- * @author Admin
- *
- * Для верификации нужно отправить POST-запрос на /api в поле data
- * В ответе придет подпись, которую необходимо отправить на /api/json либо /api/xml
- * Формат XML:
- * <request>
- * <api_id>API_ID</api_id>
- * <api_user_id>API_USER_ID</api_user_id>
- * <signature>SIGNATURE</signature>
- * <request>
- * Генерация SIGNATURE: base64_encode(md5(API_USER_ID.md5(SECRET_KEY)))
- * API_USER_ID и SECRET_KEY генерируются в момент активации партнера администратором.
- * SECRET_KEY может быть изменен партнером в любой момент (требует правок партнерского скрипта)
- *
- * После отправки запрос придет ответ следующего формата:
- * <response>
- * <result>(0/1/2/3)</result>
- * <result_description>(success или информация об ошибке)<result_descrription>
- * <signature>SIGNATURE_FROM_SCRIPT</signature>
- * </response>
- * Подпись, полученную в response нужно подставить без изменений в скрипт запроса данных.
- *
- * Формат скрипта запроса данных:
- * <request>
- * <api_id>API_ID</api_id>
- * <signature>SIGNATURE_FROM_SCRIPT</signature>
- * <count>количество записей, необязательно. Не указано - возвращаются все доступные</count>
- * </request>
- *
- * Сформированный пакет нужно отправить на /api/xml или /api/json методом POST.
- *
- * В ответ придет XML/JSON следующего формата:
- *
- * <response>
- * <status>0/1/2/3</status>
- * <api_id>API_ID</api_id>
- * <status_text>success или информация оь ошибке</status_text>
- * <data>
- * <id></id>
- * <alias></alias>
- * <author_name></author_name>
- * <author_surname></author_surname>
- * <genre></genre>
- * <price></price>
- * </data>
- * <data>
- * <id></id>
- * <buy_uri></buy_uri>
- * <author_name></author_name>
- * <author_surname></author_surname>
- * <genre></genre>
- * <price></price>
- * </data>
- * </response>
- */
- class APIController extends Zend_Controller_Action {
- private $_modelAPIUsers = null;
- private $_modelAPIStats = null;
- private $_modelAPISettings = null;
- private $_modelCatalog = null;
- private $_modelAuthors = null;
- private $_modelConfig = null;
- private $_PartnerPriceRatio = 1;
- public function init() {
- $this->_modelAPIUsers = new Application_Model_Api_Users();
- $this->_modelAPIStats = new Application_Model_Api_Stats();
- $this->_modelAPISettings = new Application_Model_Api_Settings();
- $this->_modelCatalog = new Application_Model_Catalog_Table();
- $this->_modelAuthors = new Application_Model_Catalog_Author();
- $this->_modelConfig = new Application_Model_Settings_Site();
- }
- public function indexAction() {
- if ($this->getRequest()->isPost()) {
- $data = $this->getRequest()->getPost();
- // exit(base64_decode($data['data']));
- $data = simplexml_load_string(base64_decode($data['data']));
- // exit($data->api_id);
- $code = $this->_modelAPIUsers->verify($data->api_id, $data->user_id, $data->signature);
- if ($code === true) {
- $signature = base64_encode(str_rot13(md5($data->signature)));
- $response = '<?xml version="1.0" encoding="UTF-8"?>
- <response>
- <result>0</result>
- <result_description>success</result_description>
- <signature>' . $signature . '</signature>
- </response>';
- } else {
- switch ($code) {
- case 3:
- $response = '<?xml version="1.0" encoding="UTF-8"?>
- <response>
- <result>3</result>
- <result_description>Authorisation failed: Incorrect signature or API ID</result_description>
- <signature>NULL</signature>
- </response>';
- break;
- case 1:
- $response = '<?xml version="1.0" encoding="UTF-8"?>
- <response>
- <result>1</result>
- <result_description>Request failed: Your API ID is NOT yet activated</result_description>
- <signature>NULL</signature>
- </response>';
- break;
- }
- }
- } else {
- $response = '
- <?xml version="1.0" encoding="UTF-8"?>
- <response>
- <result>2</result>
- <result_description>NO POST DATA PROVIDED</result_description>
- <signature />
- </response>';
- }
- echo $response;
- exit;
- }
- public function xmlAction() {
- if ($this->getRequest()->isPost()) {
- $response = '';
- $data = $this->getRequest()->getPost();
- $request = base64_decode($data['data']);
- $obj = simplexml_load_string($request);
- $data = $this->_modelAPIUsers->get($obj->api_id);
- Zend_Debug::dump($data);
- $sig = base64_encode(md5($data['api_id'] . md5($data['secret_key'])));
- $sign = base64_encode(str_rot13(md5($sig)));
- if ($sign == $obj->signature) {
- $apiId = (int) $data['api_id'];
- $data = $this->_modelCatalog->getList($obj->count);
- $this->_PartnerPriceRatio = $this->_modelAPISettings->getPartnerPriceRatio($apiId);
- //var_dump($this->_PartnerPriceRatio); exit;
- foreach ($data as $i => $v) {
- $data[$i]['price']*=$this->_PartnerPriceRatio;
- }
- Zend_Debug::dump($data);
- $mainSiteUri = trim($this->_modelConfig->get('site_url'), '/');
- $response = '<response>
- <status>0</status>
- <api_id>' . $data['api_id'] . '</api_id>
- <status_text>success</status_text>';
- foreach ($data as $v) {
- $author = $this->_modelAuthors->get($v['author_id']);
- $response.='<data>';
- $response.='<id>' . $v['id'] . '</id>';
- $response.='<alias>' . $v['id'] . '</alias>';
- $response.='<author_name>' . $author['name'] . '</author_name>';
- $response.='<author_surname>' . $author['surname'] . '</author_surname>';
- $response.='<genre>' . $v['genre_title'] . '</genre>';
- $response.='<price>' . $v['price'] . '</price>';
- $response.='<buy_url>' . $mainSiteUri . '/bp/' . base64_encode((md5($v['id']) . '_' . md5($v['api_id']))) . '</buy_uri>';
- $response.='</data>';
- }
- $response.='</response>';
- } else {
- $response = '<response>
- <status>1</status>
- <api_id>' . $data['api_id'] . '</api_id>
- <status_text>Signature check failed: signature incorret</status_text>
- </response>';
- }
- } else {
- $response = '<response>
- <status>3</status>
- <api_id>' . $data['api_id'] . '</api_id>
- <status_text>No POST data provided</status_text>
- </response>';
- }
- echo $response;
- exit;
- }
- public function jsonAction() {
- /**
- *IN DEVELOPMENT
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement