За последние 24 часа нас посетил 22661 программист и 1281 робот. Сейчас ищут 782 программиста ...

Нужна помощь

Тема в разделе "Вопросы от блондинок", создана пользователем SoldierOfHeart, 30 май 2017.

  1. SoldierOfHeart

    SoldierOfHeart Новичок

    С нами с:
    30 май 2017
    Сообщения:
    4
    Симпатии:
    1
    Warning: file_put_contents(C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json): failed to open stream: Invalid argument in C:\Apache24\htdocs\www\captcha-protector-master\src\CaptchaProtector\Protector.php on line 154

    Warning: file_get_contents(C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json): failed to open stream: Invalid argument in C:\Apache24\htdocs\www\captcha-protector-master\src\CaptchaProtector\Protector.php on line 156

    Warning: file_put_contents(C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json): failed to open stream: Invalid argument in C:\Apache24\htdocs\www\captcha-protector-master\src\CaptchaProtector\Protector.php on line 170

    Целый день мучаюсь и не могу решить данную проблему(Помогите кто может
     
  2. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.752
    Симпатии:
    1.322
    Адрес:
    Лень
  3. SoldierOfHeart

    SoldierOfHeart Новичок

    С нами с:
    30 май 2017
    Сообщения:
    4
    Симпатии:
    1
    Проблема в том, что я написал по мануалу, но вылетает ошибка,потому и прошу помощи)

    PHP:
    1. public function getClientInfo($ip)
    2.   {
    3.   $file = $this->getClientFilename($ip);
    4.   if(!file_exists($file)) {
    5.   if(!is_dir(dirname($file))) {
    6.   if(!mkdir(dirname($file), 0777, true)) {
    7.   throw new \RuntimeException('Unable to create storage dir: '. $this->storageDir);
    8.   }
    9.   }
    10.   }
    11.   $info = file_get_contents($file);
    12.   $json = json_decode($info, true);
    13.   return $json ? $json : [];
    14.   }
    15.   /**
    16.   * @param $ip
    17.   * @param array $info
    18.   * @return int
    19.   */
    20.   public function setClientInfo($ip, $info = [])
    21.   {
    22.   if(mt_rand(0, 100) < $this->gcFrequency) {
    23.   $this->runGC();
    24.   }
    25.   return file_put_contents($this->getClientFilename($ip), json_encode($info));
    26.   }
     
    denis01 нравится это.
  4. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.752
    Симпатии:
    1.322
    Адрес:
    Лень
    on line 154

    в 153 строку запиши
    var_dump ( $this->getClientFilename($ip) );

    покажи что пришлет
     
  5. SoldierOfHeart

    SoldierOfHeart Новичок

    С нами с:
    30 май 2017
    Сообщения:
    4
    Симпатии:
    1
    string(85) "C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json"
    Warning: file_put_contents(C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json): failed to open stream: Invalid argument in C:\Apache24\htdocs\www\captcha-protector-master\src\CaptchaProtector\Protector.php on line 155

    Warning: file_get_contents(C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json): failed to open stream: Invalid argument in C:\Apache24\htdocs\www\captcha-protector-master\src\CaptchaProtector\Protector.php on line 157

    Warning: file_put_contents(C:\Apache24\htdocs\www\captcha-protector-master\app/../var/captcha_protector/::1.json): failed to open stream: Invalid argument in C:\Apache24\htdocs\www\captcha-protector-master\src\CaptchaProtector\Protector.php on line 171
     
  6. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    7.752
    Симпатии:
    1.322
    Адрес:
    Лень
    и? надо сказать "не правильный путь прописывает твой скрипт" ?
    создай на рабочем столе "Новая папка" и задай имя =>
    Код (Text):
    1. ::1.json
     
  7. SoldierOfHeart

    SoldierOfHeart Новичок

    С нами с:
    30 май 2017
    Сообщения:
    4
    Симпатии:
    1
    Он все равно ругается на предыдущие строки.
    Я могу прислать вам полностью весь класс.может так вам будет легче мне помочь?
    --- Добавлено ---
    PHP:
    1. <?php
    2. namespace CaptchaProtector;
    3.  
    4. use Gregwar\Captcha\CaptchaBuilder;
    5.  
    6. /**
    7. * Class Protector
    8. * @package CaptchaProtector
    9. */
    10. class Protector
    11. {
    12.   /** @var string */
    13.   protected $storageDir;
    14.   /** @var bool */
    15.   protected $needCaptcha = false;
    16.   /** @var bool */
    17.   protected $captchaShown = false;
    18.   /** @var int */
    19.   protected $requestLimit = 0;
    20.   /** @var int */
    21.   protected $timeLimit;
    22.   /** @var int */
    23.   protected $requestCount = 0;
    24.   /** @var CaptchaBuilder */
    25.   protected $captchaBuilder;
    26.   /** @var string */
    27.   public $captchaCookieName = 'captcha_protector';
    28.   /** @var int */
    29.   public $gcFrequency = 10;
    30.   /** @var string */
    31.   protected $request;
    32.   /** @var int */
    33.   public $clientInfoExpires = 3600;
    34.   /** @var int */
    35.   public $captchaInfoExpires = 3600;
    36.  
    37.   /**
    38.   * @param string $storageDir
    39.   * @param int $requestLimit
    40.   * @param int|null $timeLimit
    41.   */
    42.   public function __construct($storageDir = '/tmp', $requestLimit = 3, $timeLimit = null)
    43.   {
    44.   $this->storageDir = $storageDir .'/captcha_protector';
    45.   $this->requestLimit = $requestLimit;
    46.   $this->timeLimit = $timeLimit;
    47.   }
    48.  
    49.   /**
    50.   * @param null|string $request
    51.   * @return $this
    52.   */
    53.   public function protect($request = null)
    54.   {
    55.   if($request === null) {
    56.   $uri = $_SERVER['REQUEST_METHOD'] . '/ ' . $_SERVER['REQUEST_URI'];
    57.   $request = explode('?', $uri)[0];
    58.   }
    59.  
    60.   if(!$request) {
    61.   throw new \RuntimeException('Unable to protect empty request: '. var_export($request, true));
    62.   }
    63.   $this->request = $request;
    64.  
    65.   $clientIp = $this->getClientIp();
    66.   $info = $this->processClientInfo($this->getClientInfo($clientIp));
    67.   $this->setClientInfo($clientIp, $info);
    68.  
    69.   $this->needCaptcha = $this->requestCount > $this->requestLimit;
    70.   if($this->needCaptcha) {
    71.   $this->captchaShown = $this->requestCount - $this->requestLimit === 1 ? false : true;
    72.   }
    73.  
    74.   return $this;
    75.   }
    76.  
    77.   /**
    78.   * @param $info
    79.   * @return mixed
    80.   */
    81.   protected function processClientInfo($info)
    82.   {
    83.   if($this->timeLimit !== null) {
    84.   if(!isset($info[$this->request])) {
    85.   $info[$this->request] = [
    86.   time()
    87.   ];
    88.   }
    89.   else {
    90.   $info[$this->request][] = time();
    91.   }
    92.   $actualRequests = [];
    93.   $actualTime = time() - $this->timeLimit;
    94.   foreach($info[$this->request] as $time) {
    95.   if($time > $actualTime) {
    96.   $this->requestCount++;
    97.   $actualRequests[] = $time;
    98.   }
    99.   }
    100.   $info[$this->request] = $actualRequests;
    101.   }
    102.   else {
    103.   if(!isset($info[$this->request])) {
    104.   $info[$this->request] = 1;
    105.   }
    106.   else {
    107.   $info[$this->request] += 1;
    108.   }
    109.   $this->requestCount = $info[$this->request];
    110.   }
    111.  
    112.   return $info;
    113.   }
    114.  
    115.   /**
    116.   * @return int
    117.   */
    118.   public function getRequestCount()
    119.   {
    120.   return $this->requestCount;
    121.   }
    122.  
    123.   /**
    124.   * @param null|string $request
    125.   * @return $this
    126.   */
    127.   public function forgive($request = null)
    128.   {
    129.   if($request === null) {
    130.   $uri = $_SERVER['REQUEST_METHOD'] . '/ ' . $_SERVER['REQUEST_URI'];
    131.   $request = explode('?', $uri)[0];
    132.   }
    133.  
    134.   if(!$request) {
    135.   throw new \RuntimeException('Unable to protect empty request: '. var_export($request, true));
    136.   }
    137.   $this->request = $request;
    138.  
    139.   $clientIp = $this->getClientIp();
    140.   $info = $this->getClientInfo($clientIp);
    141.   if(isset($info[$request])) {
    142.   $info[$request] = $this->timeLimit === null ? 0 : [];
    143.   $this->setClientInfo($clientIp, $info);
    144.   }
    145.  
    146.   $this->needCaptcha = false;
    147.  
    148.   return $this;
    149.   }
    150.  
    151.   /**
    152.   * @return boolean
    153.   */
    154.   public function isNeedCaptcha()
    155.   {
    156.   return $this->needCaptcha;
    157.   }
    158.  
    159.   /**
    160.   * @param $ip
    161.   * @return array
    162.   */
    163.   public function getClientInfo($ip)
    164.   {
    165.   $file = $this->getClientFilename($ip);
    166.   if(!file_exists($file)) {
    167.   if(!is_dir(dirname($file))) {
    168.   if(!mkdir(dirname($file), 0777, true)) {
    169.   throw new \RuntimeException('Unable to create storage dir: '. $this->storageDir);
    170.   }
    171.   }
    172.   var_dump ( $this->getClientFilename($ip) );
    173.   }
    174.  
    175.   $info = file_get_contents($file);
    176.   $json = json_decode($info, true);
    177.  
    178.   return $json ? $json : [];
    179.   }
    180.  
    181.   /**
    182.   * @param $ip
    183.   * @param array $info
    184.   * @return int
    185.   */
    186.   public function setClientInfo($ip, $info = [])
    187.   {
    188.   if(mt_rand(0, 100) < $this->gcFrequency) {
    189.   $this->runGC();
    190.   }
    191.  
    192.   return file_put_contents($this->getClientFilename($ip), json_encode($info));
    193.   }
    194.  
    195.   /**
    196.   * @return int
    197.   */
    198.   public function runGC()
    199.   {
    200.   $ipDir = $this->storageDir;
    201.   $time = time() - $this->clientInfoExpires;
    202.   $c = $this->clearUnused($ipDir, $time);
    203.  
    204.   $captchaDir = $this->storageDir .'/captcha';
    205.   $time = time() - $this->captchaInfoExpires;
    206.   $c += $this->clearUnused($captchaDir, $time);
    207.  
    208.   return $c;
    209.   }
    210.  
    211.   /**
    212.   * @param $dir
    213.   * @param $modifiedLaterThan
    214.   * @return int
    215.   */
    216.   protected function clearUnused($dir, $modifiedLaterThan)
    217.   {
    218.   $c = 0;
    219.   if(is_dir($dir)) {
    220.   $dh = opendir($dir);
    221.   while ($f = readdir($dh)) {
    222.   $file = $dir .'/'. $f;
    223.   if(is_file($file) && (filemtime($file) < $modifiedLaterThan)) {
    224.   unlink($file);
    225.   $c++;
    226.   }
    227.   }
    228.   }
    229.  
    230.   return $c;
    231.   }
    232.  
    233.   /**
    234.   * @param $ip
    235.   * @return string
    236.   */
    237.   public function getClientFilename($ip)
    238.   {
    239.   return $this->storageDir .'/'. $ip .'.json';
    240.   }
    241.  
    242.   /**
    243.   * @return string
    244.   */
    245.   public function getClientIp()
    246.   {
    247.   $client  = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : '';
    248.   $forward = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
    249.   $remote  = $_SERVER['REMOTE_ADDR'];
    250.  
    251.   if(filter_var($client, FILTER_VALIDATE_IP)) {
    252.   $ip = $client;
    253.   }
    254.   elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
    255.   $ip = $forward;
    256.   }
    257.   else {
    258.   $ip = $remote;
    259.   }
    260.  
    261.   return $ip;
    262.   }
    263.  
    264.   /**
    265.   * @return CaptchaBuilder
    266.   */
    267.   protected function getCaptchaBuilder()
    268.   {
    269.   if($this->request === null) {
    270.   throw new \RuntimeException('Unable to start captcha protecting without calling protect method');
    271.   }
    272.  
    273.   if($this->captchaBuilder === null) {
    274.   $this->captchaBuilder = new CaptchaBuilder();
    275.   }
    276.  
    277.   return $this->captchaBuilder;
    278.   }
    279.  
    280.   /**
    281.   * @param $width
    282.   * @param $height
    283.   * @param null $phrase
    284.   * @param int $quality
    285.   * @return string
    286.   */
    287.   public function getCaptcha($width, $height, $phrase = null, $quality = 90)
    288.   {
    289.   if($phrase) {
    290.   $this->getCaptchaBuilder()->setPhrase($phrase);
    291.   }
    292.  
    293.   $content = $this->getCaptchaBuilder()->build($width, $height)->inline($quality);
    294.   $this->saveCaptchaRequest();
    295.  
    296.   return $content;
    297.   }
    298.  
    299.   /**
    300.   * @return int
    301.   */
    302.   protected function saveCaptchaRequest()
    303.   {
    304.   $file = $this->getCaptchaRequestFile($this->getCaptchaRequestUid());
    305.   if(!is_dir(dirname($file))) {
    306.   mkdir(dirname($file), 0777, true);
    307.   }
    308.  
    309.   return file_put_contents($file, $this->getCaptchaBuilder()->getPhrase());
    310.   }
    311.  
    312.   /**
    313.   * @return string
    314.   */
    315.   protected function getCaptchaRequestUid()
    316.   {
    317.   if(!isset($_COOKIE[$this->captchaCookieName]) || !$_COOKIE[$this->captchaCookieName]) {
    318.   $requestUid = md5(mt_rand(10000, 999999) .'_'. time());
    319.   setcookie($this->captchaCookieName, $requestUid, time() + 3600, '/');
    320.   }
    321.   else {
    322.   $requestUid = $_COOKIE[$this->captchaCookieName];
    323.   }
    324.  
    325.   return $requestUid;
    326.   }
    327.  
    328.   /**
    329.   * @param $requestId
    330.   * @return string
    331.   */
    332.   public function getCaptchaRequestFile($requestId)
    333.   {
    334.   return $this->storageDir .'/captcha/'. $requestId;
    335.   }
    336.  
    337.   /**
    338.   * @param $userInput
    339.   * @return bool
    340.   */
    341.   public function isCaptchaResolved($userInput)
    342.   {
    343.   $file = $this->getCaptchaRequestFile($this->getCaptchaRequestUid());
    344.   if(file_exists($file) && trim($userInput)) {
    345.   if ($userInput == file_get_contents($file)) {
    346.   unlink($file);
    347.   return true;
    348.   }
    349.   }
    350.  
    351.   return false;
    352.   }
    353.  
    354.   /**
    355.   * @return boolean
    356.   */
    357.   public function isCaptchaShown()
    358.   {
    359.   return $this->captchaShown;
    360.   }
    361. }
     
    #7 SoldierOfHeart, 30 май 2017
    Последнее редактирование модератором: 31 май 2017