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

/catalog/admin/htaccess.php

http://google-checkout-oscommerce.googlecode.com/
PHP | 269 lines | 153 code | 18 blank | 98 comment | 31 complexity | ba24dfe80a8fb6ab04bc2b1439fc1b61 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. Copyright (C) 2007 Google Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. /**
  17. * Google Checkout v1.5.0
  18. * $Id: htaccess.php 224 2009-03-11 16:31:28Z ed.davisson $
  19. *
  20. * .htaccess .htpasswd pair for Google Checkout Basic Authentication on CGI php
  21. *
  22. * ChangeLog:
  23. * v0.2
  24. * 02-22-2007
  25. * Add sandbox and checkout account
  26. * Add check for directory
  27. * Add cwd or get['url'] to set defaul dir
  28. * Add file creation
  29. *
  30. * v0.1
  31. * 02-14-2006 st. Valentine's day :D
  32. * Basic creation of text to paste in files
  33. *
  34. * README:
  35. *
  36. * NOTE: This must be used if you run PHP over CGI
  37. *
  38. * Run this script, fill the form with your Google Checkout Merchant Id/Key
  39. * and with the absolute path to your catalog/googlechekout/ directoy.
  40. * ie. /home/ropu/public_html/catalog/googlecheckout
  41. *
  42. * Click "Create" button
  43. *
  44. * If you select not to create files, copy the contents for .htaccess
  45. * and .htpasswd into those files and place them in that directory.
  46. *
  47. * googlecheckout/responsehandler.php folowing code will be disabled if
  48. * CGI config is set to True
  49. *
  50. *
  51. [CODE]
  52. //Parse the HTTP header to verify the source.
  53. if(isset($HTTP_SERVER_VARS['PHP_AUTH_USER']) && isset($HTTP_SERVER_VARS['PHP_AUTH_PW'])) {
  54. $compare_mer_id = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
  55. $compare_mer_key = $HTTP_SERVER_VARS['PHP_AUTH_PW'];
  56. }
  57. else {
  58. error_func("HTTP Basic Authentication failed.\n");
  59. exit(1);
  60. }
  61. if($compare_mer_id != $merchant_id || $compare_mer_key != $merchant_key) {
  62. error_func("HTTP Basic Authentication failed.\n");
  63. exit(1);
  64. }
  65. [/CODE]
  66. *
  67. * Test the responsehandler.php with the responsehandler_test.php
  68. *
  69. */
  70. ?>
  71. <html>
  72. <head>
  73. <script language="JavaScript" type="text/javascript">
  74. function checkCreate(){
  75. var check = document.getElementById('check');
  76. var create = document.getElementById('create');
  77. if(check.checked) {
  78. create.disabled = false;
  79. }
  80. else {
  81. create.disabled = true;
  82. }
  83. }
  84. </script>
  85. <title>.htaccess .htpasswd pair for Google Checkout Basic authentication on CGI php installations</title>
  86. </head>
  87. <body>
  88. <?
  89. if(isset($_POST['submit'])) {
  90. $errors = array();
  91. if(isset($_POST['sb']) && empty($_POST['sb_id'])){
  92. $errors[] = "Your SandBox Merchant ID must not by empty";
  93. }
  94. if(isset($_POST['sb']) && empty($_POST['sb_key'])){
  95. $errors[] = "Your SandBox Merchant KEY must not by empty";
  96. }
  97. if(isset($_POST['gc']) && empty($_POST['gc_id'])){
  98. $errors[] = "Your Checkout Merchant ID must not by empty";
  99. }
  100. if(isset($_POST['gc']) && empty($_POST['gc_key'])){
  101. $errors[] = "Your Checkout Merchant KEY must not by empty";
  102. }
  103. if(!isset($_POST['sb']) && !isset($_POST['gc'])) {
  104. $errors[] = "Select at least SandBox or Checkout Account";
  105. }
  106. if(empty($_POST['path']) || (isset($_POST['check']) && !is_dir($_POST['path']))){
  107. $errors[] = "The path is not valid";
  108. }
  109. if(isset($_POST['create']) && !is_writable($_POST['path'])) {
  110. $errors[] = $_POST['path'] . " is NOT writable";
  111. }
  112. if(empty($errors)) {
  113. $htaccess = 'AuthName "Google checkout Basic Authentication"' . "\n";
  114. $htaccess .= 'AuthType Basic' . "\n";
  115. $htaccess .= 'AuthUserFile ' . $_POST['path'] . "/.htpasswd\n";
  116. $htaccess .= 'require valid-user';
  117. echo "<xmp>.htaccess file:\n<<<Start---\n";
  118. echo $htaccess;
  119. echo "\n---End>>>\n";
  120. $htpasswd = "";
  121. if(isset($_POST['sb'])) {
  122. $sb_user = @$_POST['sb_id'];
  123. $sb_pass = @$_POST['sb_key'];
  124. $sb_crypt_pass = rand_salt_crypt($sb_pass);
  125. $htpasswd .= $sb_user . ":" . $sb_crypt_pass ."\n";
  126. }
  127. if(isset($_POST['gc'])) {
  128. $gc_user = @$_POST['gc_id'];
  129. $gc_pass = @$_POST['gc_key'];
  130. $gc_crypt_pass = rand_salt_crypt($gc_pass);
  131. $htpasswd .= $gc_user . ":" . $gc_crypt_pass ."\n";
  132. }
  133. echo "\n\n.htpasswd file:\n<<<Start---\n";
  134. echo $htpasswd;
  135. echo "---End>>>\n</xmp>\n";
  136. if(isset($_POST['create'])){
  137. $htaccess_file = fopen($_POST['path']. "/.htaccess", w);
  138. $htpasswd_file = fopen($_POST['path']. "/.htpasswd", w);
  139. fwrite($htaccess_file, $htaccess);
  140. fwrite($htpasswd_file, $htpasswd);
  141. fclose($htaccess_file);
  142. fclose($htpasswd_file);
  143. echo "Files Created!<br />";
  144. }
  145. }
  146. else {
  147. echo "<table align=center border=0 cellpadding=0 cellspacing=0>\n";
  148. echo "<tr><th style='color:red'>Errors:</th><tr>\n";
  149. foreach($errors as $error){
  150. echo "<tr>\n";
  151. echo "<td style='color:red'><li>" . $error . "</li></td>\n";
  152. echo "</tr>\n";
  153. }
  154. echo "</table>";
  155. // print_r($errors);
  156. }
  157. }
  158. if(!isset($_POST['path']) || empty($_POST['path'])){
  159. chdir("../googlecheckout");
  160. $_POST['path'] = isset($_GET['url'])?$_GET['url']:getcwd();
  161. }
  162. // For function rand_salt_crypt()
  163. // .htpasswd file functions
  164. // Copyright (C) 2004,2005 Jarno Elonen <elonen@iki.fi>
  165. //
  166. // Redistribution and use in source and binary forms, with or without modification,
  167. // are permitted provided that the following conditions are met:
  168. //
  169. // * Redistributions of source code must retain the above copyright notice, this
  170. // list of conditions and the following disclaimer.
  171. // * Redistributions in binary form must reproduce the above copyright notice,
  172. // this list of conditions and the following disclaimer in the documentation
  173. // and/or other materials provided with the distribution.
  174. // * The name of the author may not be used to endorse or promote products derived
  175. // from this software without specific prior written permission.
  176. //
  177. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
  178. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  179. // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
  180. // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  181. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  182. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  183. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  184. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  185. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  186. // Generates a htpasswd compatible crypted password string.
  187. function rand_salt_crypt( $pass )
  188. {
  189. $salt = "";
  190. mt_srand((double)microtime()*1000000);
  191. for ($i=0; $i<CRYPT_SALT_LENGTH; $i++)
  192. $salt .= substr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./", mt_rand() & 63, 1);
  193. return crypt($pass, $salt);
  194. }
  195. ?>
  196. <h2 align=center>.htaccess .htpasswd pair for Google Checkout Basic authentication on CGI php installations</h2>
  197. <form action="" method="post">
  198. <table border=1 cellpadding=2 cellspacing=0 align=center>
  199. <tr>
  200. <th align="center" colspan="2">Sandbox Account: <input type="checkbox" value="true" name="sb"<?php echo (!isset($_POST['submit']) || isset($_POST['sb']))?' checked':'';?>/></th>
  201. </tr>
  202. <tr>
  203. <th align="right">Merchant ID: </th>
  204. <td><input type="text" value="<?=@$_POST['sb_id'];?>" name="sb_id" size="40"/></td>
  205. </tr>
  206. <tr>
  207. <th align="right">Merchant Key: </th>
  208. <td><input type="text" value="<?=@$_POST['sb_key'];?>" name="sb_key" size="40"/></td>
  209. </tr>
  210. <tr>
  211. <th align="center" colspan="2">Checkout Account: <input type="checkbox" value="true" name="gc"<?php echo (!isset($_POST['submit']) || isset($_POST['gc']))?' checked':'';?>/></th>
  212. </tr>
  213. <tr>
  214. <th align="right">Merchant ID: </th>
  215. <td><input type="text" value="<?=@$_POST['gc_id'];?>" name="gc_id" size="40"/></td>
  216. </tr>
  217. <tr>
  218. <th align="right">Merchant Key: </th>
  219. <td><input type="text" value="<?=@$_POST['gc_key'];?>" name="gc_key" size="40"/></td>
  220. </tr>
  221. <tr>
  222. <th align="center" colspan="2">&nbsp</th>
  223. </tr>
  224. <tr>
  225. <th align="right">Absolute <i>dir</i> to googlecheckout/ :</th>
  226. <td><input type="text" value="<?=@$_POST['path'];?>" name="path" size="40"/>
  227. <br /><small>( ie. <b>/home/ropu/public_html/catalog/googlecheckout</b> )</small>
  228. </td>
  229. </tr>
  230. <tr>
  231. <th align="right">Check if <i>dir</i> exists: </th>
  232. <td><input type="checkbox" value="true" id="check" onChange="checkCreate()" name="check"<?php echo isset($_POST['check'])?' checked':'';?>/></td>
  233. </tr>
  234. <tr>
  235. <th valign=top align="right">Create Files: </th>
  236. <td><input type="checkbox" value="true" id="create" name="create"<?php echo (isset($_POST['create'])&&isset($_POST['check']))?' checked':'';?><?php echo (!isset($_POST['check']))?' disabled':'';?>/>
  237. <br /><small>(Tip: To create files <i>dir</i> must have <b>Write</b> (777) permission)</small>
  238. <br /><small>Old files will be overrided!</small>
  239. </td>
  240. </tr>
  241. <tr>
  242. <td align="center" colspan="2"><input type="submit" name="submit" value="Create"/><div align=right><small>Coded by:<b>Ropu</b></small></div></td>
  243. </tr>
  244. </table>
  245. </form>
  246. </body>
  247. </html>