PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/adminstrator/tabs/AdminWebservice.php

http://marocmall.googlecode.com/
PHP | 236 lines | 198 code | 13 blank | 25 comment | 34 complexity | 1b3d5f77ab299a741d116d031a1a1e30 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2011 PrestaShop SA
  23. * @version Release: $Revision: 7666 $
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * International Registered Trademark & Property of PrestaShop SA
  26. */
  27. include_once(dirname(__FILE__).'/../../classes/AdminTab.php');
  28. class AdminWebservice extends AdminTab
  29. {
  30. public function __construct()
  31. {
  32. $this->table = 'webservice_account';
  33. $this->className = 'WebserviceKey';
  34. $this->lang = false;
  35. $this->edit = true;
  36. $this->delete = true;
  37. $this->id_lang_default = Configuration::get('PS_LANG_DEFAULT');
  38. $this->fieldsDisplay = array(
  39. 'key' => array('title' => $this->l('Key'), 'align' => 'center', 'width' => 32),
  40. 'active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false),
  41. 'description' => array('title' => $this->l('Key description'), 'align' => 'center', 'orderby' => false)
  42. );
  43. $this->optionTitle = $this->l('Configuration');
  44. if (file_exists(_PS_ROOT_DIR_.'/.htaccess'))
  45. $this->_fieldsOptions = array('PS_WEBSERVICE' => array('title' => $this->l('Enable PrestaShop Webservice:'),
  46. 'desc' => $this->l('Before activating the webservice, you must be sure to: ').
  47. '<ol><li>'.$this->l('be certain URL rewrite is available on this server').
  48. '</li><li>'.$this->l('be certain that the 5 methods GET, POST, PUT, DELETE and HEAD are supported by this server').
  49. '</li></ol>',
  50. 'cast' => 'intval',
  51. 'type' => 'bool'));
  52. parent::__construct();
  53. }
  54. protected function afterAdd($object) {
  55. WebserviceKey::setPermissionForAccount($object->id, Tools::getValue('resources', array()));
  56. }
  57. protected function afterUpdate($object) {
  58. WebserviceKey::setPermissionForAccount($object->id, Tools::getValue('resources', array()));
  59. }
  60. public function displayList()
  61. {
  62. global $cookie, $currentIndex;
  63. $warnings = array();
  64. if (!file_exists(_PS_ROOT_DIR_.'/.htaccess'))
  65. $warnings[] = $this->l('In order to enable the PrestaShop Webservice, please generate the .htaccess file via the "Generators" tab (in the "Tools" tab).');
  66. if (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === false)
  67. $warnings[] = $this->l('To avoid operating problems, please use an Apache server.');
  68. {
  69. if (function_exists('apache_get_modules'))
  70. {
  71. $apache_modules = apache_get_modules();
  72. if (!in_array('mod_auth_basic', $apache_modules))
  73. $warnings[] = $this->l('Please activate the Apache module \'mod_auth_basic\' to allow authentication of PrestaShop webservice.');
  74. if (!in_array('mod_rewrite', $apache_modules))
  75. $warnings[] = $this->l('Please activate the Apache module \'mod_rewrite\' to allow using the PrestaShop webservice.');
  76. }
  77. else
  78. {
  79. $warnings[] = $this->l('We could not check if basic authentication and rewrite extensions are activated. Please manually check if they are activated in order to use the PrestaShop webservice.');
  80. }
  81. }
  82. if (!extension_loaded('SimpleXML'))
  83. $warnings[] = $this->l('Please activate the PHP extension \'SimpleXML\' to allow testing of PrestaShop webservice.');
  84. if (!configuration::get('PS_SSL_ENABLED'))
  85. $warnings[] = $this->l('If possible, it is preferable to use SSL (https) for webservice calls, as it avoids the security issues of type "man in the middle".');
  86. $this->displayWarning($warnings);
  87. foreach ($this->_list as $k => $item)
  88. if ($item['is_module'] && $item['class_name'] && $item['module_name'] &&
  89. ($instance = Module::getInstanceByName($item['module_name'])) &&
  90. !$instance->useNormalPermissionBehaviour())
  91. unset($this->_list[$k]);
  92. parent::displayList();
  93. }
  94. public function displayForm($isMainTab = true)
  95. {
  96. global $currentIndex;
  97. parent::displayForm();
  98. if (!($obj = $this->loadObject(true)))
  99. return;
  100. echo '
  101. <form action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post" enctype="multipart/form-data">
  102. '.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" />' : '').'
  103. <fieldset><legend><img src="../img/admin/access.png" />'.$this->l('Webservice Accounts').'</legend>
  104. <label>'.$this->l('Key:').'</label>
  105. <div class="margin-form">
  106. <input type="text" size="32" name="key" id="code" value="'.htmlentities(Tools::getValue('key', $obj->key), ENT_COMPAT, 'UTF-8').'" />
  107. <input type="button" value="'.$this->l(' Generate! ').'" class="button" onclick="gencode(32)" />
  108. <sup>*</sup>
  109. <p class="clear">'.$this->l('Webservice account key').'</p>
  110. </div>
  111. <label>'.$this->l('Key description').'</label>
  112. <div class="margin-form">
  113. <textarea rows="3" style="width:400px" name="description">'.htmlentities(Tools::getValue('description', $obj->description), ENT_COMPAT, 'UTF-8').'</textarea>
  114. <p class="clear">'.$this->l('Key description').'</p>
  115. </div>
  116. <label>'.$this->l('Status:').' </label>
  117. <div class="margin-form">
  118. <input type="radio" name="active" id="active_on" value="1" '.((!$obj->id OR Tools::getValue('active', $obj->active)) ? 'checked="checked" ' : '').'/>
  119. <label class="t" for="active_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
  120. <input type="radio" name="active" id="active_off" value="0" '.((!Tools::getValue('active', $obj->active) AND $obj->id) ? 'checked="checked" ' : '').'/>
  121. <label class="t" for="active_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
  122. </div>
  123. <label>'.$this->l('Permissions:').' </label>
  124. <div class="margin-form">
  125. <p>'.$this->l('Set the resource permissions for this key:').'</p>
  126. <table border="0" cellspacing="0" cellpadding="0" class="permissions">
  127. <thead>
  128. <tr>
  129. <th>'.$this->l('Resource').'</th>
  130. <th width="30"></th>
  131. <th width="50">'.$this->l('View (GET)').'</th>
  132. <th width="50">'.$this->l('Modify (PUT)').'</th>
  133. <th width="50">'.$this->l('Add (POST)').'</th>
  134. <th width="50">'.$this->l('Delete (DELETE)').'</th>
  135. <th width="50">'.$this->l('Fast view (HEAD)').'</th>
  136. </tr>
  137. </thead>
  138. <tbody>
  139. <tr class="all" style="vertical-align:cen">
  140. <th></th>
  141. <th></th>
  142. <th><input type="checkbox" class="all_get get " /></th>
  143. <th><input type="checkbox" class="all_put put " /></th>
  144. <th><input type="checkbox" class="all_post post " /></th>
  145. <th><input type="checkbox" class="all_delete delete" /></th>
  146. <th><input type="checkbox" class="all_head head" /></th>
  147. </tr>
  148. ';
  149. $ressources = WebserviceRequest::getResources();
  150. $permissions = WebserviceKey::getPermissionForAccount($obj->key);
  151. foreach ($ressources as $resourceName => $resource)
  152. echo '
  153. <tr>
  154. <th>'.$resourceName.'</th>
  155. <th><input type="checkbox" class="all"/></th>
  156. <td><input type="checkbox" '.(isset($ressources[$resourceName]['forbidden_method']) && in_array('GET', $ressources[$resourceName]['forbidden_method']) ? 'disabled="disabled"' : '').' class="get" name="resources['.$resourceName.'][GET]" '.(isset($permissions[$resourceName]) && in_array('GET', $permissions[$resourceName]) ? 'checked="checked"' : '').' /></td>
  157. <td><input type="checkbox" '.(isset($ressources[$resourceName]['forbidden_method']) && in_array('PUT', $ressources[$resourceName]['forbidden_method']) ? 'disabled="disabled"' : '').' class="put" name="resources['.$resourceName.'][PUT]" '.(isset($permissions[$resourceName]) && in_array('PUT', $permissions[$resourceName]) ? 'checked="checked"' : '').'/></td>
  158. <td><input type="checkbox" '.(isset($ressources[$resourceName]['forbidden_method']) && in_array('POST', $ressources[$resourceName]['forbidden_method']) ? 'disabled="disabled"' : '').' class="post" name="resources['.$resourceName.'][POST]" '.(isset($permissions[$resourceName]) && in_array('POST', $permissions[$resourceName]) ? 'checked="checked"' : '').'/></td>
  159. <td><input type="checkbox" '.(isset($ressources[$resourceName]['forbidden_method']) && in_array('DELETE', $ressources[$resourceName]['forbidden_method']) ? 'disabled="disabled"' : '').' class="delete" name="resources['.$resourceName.'][DELETE]" '.(isset($permissions[$resourceName]) && in_array('DELETE', $permissions[$resourceName]) ? 'checked="checked"' : '').'/></td>
  160. <td><input type="checkbox" '.(isset($ressources[$resourceName]['forbidden_method']) && in_array('HEAD', $ressources[$resourceName]['forbidden_method']) ? 'disabled="disabled"' : '').' class="head" name="resources['.$resourceName.'][HEAD]" '.(isset($permissions[$resourceName]) && in_array('HEAD', $permissions[$resourceName]) ? 'checked="checked"' : '').'/></td>
  161. </tr>';
  162. echo '
  163. </tbody>
  164. </table>
  165. <script>';?>
  166. $(function() {
  167. $('table.permissions input.all').click(function() {
  168. if($(this).is(':checked'))
  169. $(this).parent().parent().find('input.get:not(:checked), input.put:not(:checked), input.post:not(:checked), input.delete:not(:checked), input.head:not(:checked)').click();
  170. else
  171. $(this).parent().parent().find('input.get:checked, input.put:checked, input.post:checked, input.delete:checked, input.head:checked').click();
  172. });
  173. $('table.permissions .all_get').click(function() {
  174. if($(this).is(':checked'))
  175. $(this).parent().parent().parent().find('input.get:not(:checked)').click();
  176. else
  177. $(this).parent().parent().parent().find('input.get:checked').click();
  178. });
  179. $('table.permissions .all_put').click(function() {
  180. if($(this).is(':checked'))
  181. $(this).parent().parent().parent().find('input.put:not(:checked)').click();
  182. else
  183. $(this).parent().parent().parent().find('input.put:checked').click();
  184. });
  185. $('table.permissions .all_post').click(function() {
  186. if($(this).is(':checked'))
  187. $(this).parent().parent().parent().find('input.post:not(:checked)').click();
  188. else
  189. $(this).parent().parent().parent().find('input.post:checked').click();
  190. });
  191. $('table.permissions .all_delete').click(function() {
  192. if($(this).is(':checked'))
  193. $(this).parent().parent().parent().find('input.delete:not(:checked)').click();
  194. else
  195. $(this).parent().parent().parent().find('input.delete:checked').click();
  196. });
  197. $('table.permissions .all_head').click(function() {
  198. if($(this).is(':checked'))
  199. $(this).parent().parent().parent().find('input.head:not(:checked)').click();
  200. else
  201. $(this).parent().parent().parent().find('input.head:checked').click();
  202. });
  203. });
  204. <?php echo '
  205. </script>
  206. </div>
  207. <div class="margin-form">
  208. <input type="submit" value="'.$this->l(' Save ').'" name="submitAdd'.$this->table.'" class="button" />
  209. </div>
  210. <div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
  211. </fieldset>
  212. </form>';
  213. }
  214. public function postProcess()
  215. {
  216. if (Tools::getValue('key') && strlen(Tools::getValue('key')) < 32)
  217. $this->_errors[] = Tools::displayError($this->l('Key length must be 32 character long'));
  218. if (WebserviceKey::keyExists(Tools::getValue('key')) && !Tools::getValue('id_webservice_account'))
  219. $this->_errors[] = Tools::displayError($this->l('Key already exists'));
  220. return parent::postProcess();
  221. }
  222. }