users
[ class tree: users ] [ index: users ] [ all elements ]

Source for file solvemedialib.php

Documentation is available at solvemedialib.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 by Jeff Weisberg
  4.  * Author: Jeff Weisberg
  5.  * Created: 2009-Jun-22 16:44 (EDT)
  6.  * Function: SolveMedia API php code
  7.  *
  8.  * $Id$
  9.  *
  10.  * This is a PHP library that handles calling SolveMedia.
  11.  *    - Documentation and latest version
  12.  *          http://www.solvemedia.com/
  13.  *    - Get a SolveMedia API Keys
  14.  *          http://api.solvemedia.com/public/signup
  15.  *
  16.  * @package users
  17.  * @subpackage functions
  18.  */
  19.  
  20. /* This code is based on code from,
  21.  * and copied, modified and distributed with permission in accordance with its terms:
  22.  *
  23.  *   Copyright (c) 2007 reCAPTCHA
  24.  *   AUTHORS:
  25.  *     Mike Crawford
  26.  *     Ben Maurer
  27.  *
  28.  *   Permission is hereby granted, free of charge, to any person obtaining a copy
  29.  *   of this software and associated documentation files (the "Software"), to deal
  30.  *   in the Software without restriction, including without limitation the rights
  31.  *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32.  *   copies of the Software, and to permit persons to whom the Software is
  33.  *   furnished to do so, subject to the following conditions:
  34.  *
  35.  *   The above copyright notice and this permission notice shall be included in
  36.  *   all copies or substantial portions of the Software.
  37.  *
  38.  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39.  *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40.  *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41.  *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42.  *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43.  *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  44.  *   THE SOFTWARE.
  45.  */
  46.  
  47. /**
  48.  * The solvemedia server URL's
  49.  */
  50. define("ADCOPY_API_SERVER",        "http://api.solvemedia.com");
  51. define("ADCOPY_API_SECURE_SERVER""https://api-secure.solvemedia.com");
  52. define("ADCOPY_VERIFY_SERVER",     "verify.solvemedia.com");
  53. define("ADCOPY_SIGNUP",            "http://api.solvemedia.com/public/signup");
  54.  
  55. /**
  56.  * Encodes the given data into a query string format
  57.  * @param $data - array of string elements to be encoded
  58.  * @return string - encoded request
  59.  */
  60. function _adcopy_qsencode ($data{
  61.         $req "";
  62.         foreach $data as $key => $value )
  63.                 $req .= $key '=' urlencodestripslashes($value) ) '&';
  64.  
  65.         // Cut the last '&'
  66.         $req=substr($req,0,strlen($req)-1);
  67.         return $req;
  68. }
  69.  
  70.  
  71.  
  72. /**
  73.  * Submits an HTTP POST to a solvemedia server
  74.  * @param string $host 
  75.  * @param string $path 
  76.  * @param array $data 
  77.  * @param int port
  78.  * @return array response
  79.  */
  80. function _adcopy_http_post($host$path$data$port 80{
  81.  
  82.         $req _adcopy_qsencode ($data);
  83.  
  84.         $http_request  "POST $path HTTP/1.0\r\n";
  85.         $http_request .= "Host: $host\r\n";
  86.         $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  87.         $http_request .= "Content-Length: " strlen($req"\r\n";
  88.         $http_request .= "User-Agent: solvemedia/PHP\r\n";
  89.         $http_request .= "\r\n";
  90.         $http_request .= $req;
  91.  
  92.         $response '';
  93.         iffalse == $fs @fsockopen($host$port$errno$errstr10) ) ) {
  94.                 die ('Could not open socket');
  95.         }
  96.  
  97.         fwrite($fs$http_request);
  98.  
  99.         while !feof($fs) )
  100.                 $response .= fgets($fs1024)// One TCP-IP packet [sic]
  101.         fclose($fs);
  102.         $response explode("\r\n\r\n"$response2);
  103.  
  104.         return $response;
  105. }
  106.  
  107.  
  108.  
  109. /**
  110.  * Gets the challenge HTML (javascript and non-javascript version).
  111.  * This is called from the browser, and the resulting solvemedia HTML widget
  112.  * is embedded within the HTML form it was called from.
  113.  * @param string $pubkey A public key for solvemedia
  114.  * @param string $error The error given by solvemedia (optional, default is null)
  115.  * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
  116.  
  117.  * @return string - The HTML to be embedded in the user's form.
  118.  */
  119. function solvemedia_get_html ($pubkey$error null$use_ssl false)
  120. {
  121.     if ($pubkey == null || $pubkey == ''{
  122.         die ("To use solvemedia you must get an API key from <a href='" ADCOPY_SIGNUP "'>" ADCOPY_SIGNUP "</a>");
  123.     }
  124.  
  125.     if ($use_ssl{
  126.                 $server ADCOPY_API_SECURE_SERVER;
  127.         else {
  128.                 $server ADCOPY_API_SERVER;
  129.         }
  130.  
  131.         $errorpart "";
  132.         if ($error{
  133.            $errorpart ";error=1";
  134.         }
  135.         return '<script type="text/javascript" src="'$server '/papi/challenge.script?k=' $pubkey $errorpart '"></script>
  136.  
  137.     <noscript>
  138.           <iframe src="'$server '/papi/challenge.noscript?k=' $pubkey $errorpart '" height="300" width="500" frameborder="0"></iframe><br/>
  139.           <textarea name="adcopy_challenge" rows="3" cols="40"></textarea>
  140.           <input type="hidden" name="adcopy_response" value="manual_challenge"/>
  141.     </noscript>';
  142. }
  143.  
  144.  
  145.  
  146.  
  147. /**
  148.  * A SolveMediaResponse is returned from solvemedia_check_answer()
  149.  *
  150.  * @package users
  151.  */
  152.         var $is_valid;
  153.         var $error;
  154. }
  155.  
  156.  
  157. /**
  158.   * Calls an HTTP POST function to verify if the user's guess was correct
  159.   * @param string $privkey 
  160.   * @param string $remoteip 
  161.   * @param string $challenge 
  162.   * @param string $response 
  163.   * @param string $hashkey 
  164.   * @return SolveMediaResponse 
  165.   */
  166. function solvemedia_check_answer ($privkey$remoteip$challenge$response$hashkey '' )
  167. {
  168.     if ($privkey == null || $privkey == ''{
  169.         die ("To use solvemedia you must get an API key from <a href='" ADCOPY_SIGNUP "'>" ADCOPY_SIGNUP "</a>");
  170.     }
  171.  
  172.     if ($remoteip == null || $remoteip == ''{
  173.         die ("For security reasons, you must pass the remote ip to solvemedia");
  174.     }
  175.  
  176.         //discard spam submissions
  177.         if ($challenge == null || strlen($challenge== || $response == null || strlen($response== 0{
  178.                 $adcopy_response new SolveMediaResponse();
  179.                 $adcopy_response->is_valid false;
  180.                 $adcopy_response->error 'incorrect-solution';
  181.                 return $adcopy_response;
  182.         }
  183.  
  184.         $response _adcopy_http_post (ADCOPY_VERIFY_SERVER"/papi/verify",
  185.                                           array (
  186.                                                  'privatekey' => $privkey,
  187.                                                  'remoteip'   => $remoteip,
  188.                                                  'challenge'  => $challenge,
  189.                                                  'response'   => $response
  190.                                                  )
  191.                                           );
  192.  
  193.         $answers explode ("\n"$response [1]);
  194.         $adcopy_response new SolveMediaResponse();
  195.  
  196.         ifstrlen($hashkey) ){
  197.                 # validate message authenticator
  198.                 $hash sha1$answers[0$challenge $hashkey );
  199.  
  200.                 if$hash != $answers[2){
  201.                         $adcopy_response->is_valid false;
  202.                         $adcopy_response->error 'hash-fail';
  203.                         return $adcopy_response;
  204.                 }
  205.         }
  206.  
  207.         if (trim ($answers [0]== 'true'{
  208.                 $adcopy_response->is_valid true;
  209.         }
  210.         else {
  211.                 $adcopy_response->is_valid false;
  212.                 $adcopy_response->error $answers [1];
  213.         }
  214.         return $adcopy_response;
  215.  
  216. }
  217.  
  218. /**
  219.  * gets a URL where the user can sign up for solvemedia. If your application
  220.  * has a configuration page where you enter a key, you should provide a link
  221.  * using this function.
  222.  * @param string $domain The domain where the page is hosted
  223.  * @param string $appname The name of your application
  224.  */
  225. function solvemedia_get_signup_url ($domain null$appname null{
  226.     return ADCOPY_SIGNUP "?" .  _adcopy_qsencode (array ('domain' => $domain'app' => $appname));
  227. }
  228.  
  229.  
  230. /* Mailhide related code */
  231. /* [ deleted ] */
  232.  
  233. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:28 +0000 by phpDocumentor 1.5.0-lsces