За последние 24 часа нас посетили 19459 программистов и 1601 робот. Сейчас ищут 1052 программиста ...

Где ошибка в коде?

Тема в разделе "Прочие вопросы по PHP", создана пользователем nikes, 24 дек 2013.

  1. nikes

    nikes Новичок

    С нами с:
    24 дек 2013
    Сообщения:
    1
    Симпатии:
    0
    Всем здравствуйте. Подскажите пожалуйста по такому вопросу:
    Этот код некорректно работает. Там есть условия, если в условии 0, то выводим информацию 1, если в условии 3 значения, то выводим список условий, иначе выводим одно условие, иначе выводим информацию 2. (поймете).

    Так вот, когда я ввожу 3 значения (к примеру, 5,7,10), то мне выводит информацию 2, а должно выполняться условие, где будет высвечиваться список.

    Нужно построить код так, чтобы правильно выводилась информация.
    P.S. В 3х условиях могут быть только цифры (без символов и букв)


    Код (Text):
    1. $tr = array();
    2. preg_match_all('/\[\-private\-data\-([0-9]+)\-([0-9]+)\-\]/', $post['message'], $matches);
    3. foreach ($matches[0] as $i => $entire_match)
    4. {
    5.     $post_id = $matches[1][$i];
    6.     $hide_id = $matches[2][$i];
    7.     if ($post_id != $post['postid'])
    8.     {
    9.         continue;
    10.     }
    11.     if (!isset($GLOBALS['hides'][$post_id]))
    12.     {
    13.         $row = $this->registry->db->query_first("
    14.             SELECT hide
    15.             FROM " . TABLE_PREFIX . "post
    16.             WHERE postid = " . intval($post_id) . "
    17.         ");
    18.         if ($row)
    19.         {
    20.             $GLOBALS['hides'][$post_id] = unserialize($row['hide']);
    21.         }
    22.     }
    23.     if (isset($GLOBALS['hides'][$post_id][$hide_id]))
    24.     {
    25.         global $permission, $forumid, $vbulletin;
    26.         $condition = trim(trim(trim($GLOBALS['hides'][$post_id][$hide_id]['condition']), '\'"'));
    27.         $passed = false;
    28.         $requirements = array();
    29.         $requirements_f = 'AND';
    30.         if ($permissions['adminpermissions'] & $vbulletin->bf_ugp_adminpermissions['cancontrolpanel'])
    31.         {
    32.             $passed = true;
    33.         }
    34.         else if ($post['userid'] == $this->registry->userinfo['userid'])
    35.         {
    36.             $passed = true;
    37.         }
    38.  
    39.     else if (($permissions['adminpermissions']) & ($vbulletin->bf_ugp_adminpermissions['ismoderator']))
    40.         {
    41.             $passed = true;
    42.  
    43.         }
    44.  
    45.     else if ((can_moderate($forumid)) & ($vbulletin->options['mod_hide']))
    46.         {
    47.             $passed = true;
    48.  
    49.         }
    50. else if (preg_match('#^[0-9]+$#', $condition) || (preg_match('#^[0-9,]+$#', $condition) && substr_count($condition, ',') ==  3))
    51.         {
    52.        
    53.             if ($condition == '0')
    54.             {
    55.                 $requirements[] = array('Быть зарегистрированным на форуме', $vbulletin->userinfo['userid'] != 0);
    56.             }
    57.             else
    58.             {
    59.                 if (substr_count($condition, ',') == 3)
    60.                 {
    61.                     list ($required_messages, $required_reputation, $required_registration) = explode(",", $condition);
    62.                 }
    63.                 else
    64.                 {
    65.                     $required_messages     = round($condition * $condition * $vbulletin->options['hide_post']);
    66.                     $required_reputation   = round($condition * $vbulletin->options['hide_rep']);
    67.                     $required_registration = $condition * $vbulletin->options['hide_reg'];
    68.                 }
    69.  
    70.                 $requirements[] = array('Иметь <b>' . $required_messages   . '</b> сообщений        (' . max(0, $required_messages     - $vbulletin->userinfo['posts'])                     . ' осталось)', $vbulletin->userinfo['posts']                     >= $required_messages);
    71.                 $requirements[] = array('Иметь <b>' . $required_reputation . '</b> очков репутации (' . max(0, $required_reputation   - $vbulletin->userinfo['reputation'])                . ' осталось)', $vbulletin->userinfo['reputation']                >= $required_reputation);
    72.                 $requirements[] = array('Давность регистрации на форуме не менее <b>' . $required_registration . '</b> дней назад', (time() - ($vbulletin->userinfo['userid'] ? $vbulletin->userinfo['joindate'] : time())) / 86400 >= $required_registration);
    73.             }
    74.         }
    75.         else
    76.         {
    77.             $requirements_f = 'OR';
    78.             foreach (explode(',', $condition) as $username)
    79.             {
    80.                 $requirements[] = array('Быть пользователем с ником <b>' . htmlspecialchars_uni($username) . '</b>', strtolower($vbulletin->userinfo['username']) == strtolower($username));
    81.             }
    82.         }
    83.         if (!$passed)
    84.         {
    85.             if ($requirements_f == 'AND')
    86.             {
    87.                 $passed = true;
    88.             }
    89.             else if ($requirements_f == 'OR')
    90.             {
    91.                 $passed = false;
    92.             }
    93.             else
    94.             {
    95.                 // ???
    96.             }
    97.  
    98.             foreach ($requirements as $requirement)
    99.             {
    100.                 if ($requirements_f == 'AND')
    101.                 {
    102.                     $passed = $passed && $requirement[1];
    103.                 }
    104.                 else if ($requirements_f == 'OR')
    105.                 {
    106.                     $passed = $passed || $requirement[1];
    107.                 }
    108.                 else
    109.                 {
    110.                     // ???
    111.                 }
    112.             }
    113.         }        
    114.  
    115.         if ($passed)
    116.         {
    117.             $condition_escaped = htmlspecialchars_uni($condition);
    118.  
    119.             $tr[$entire_match] = <<<EOF
    120.                 <div class="bbcode_container">
    121.                     <div class="bbcode_quote">
    122.                         <div class="quote_container">
    123.                             <div class="bbcode_postedby">
    124.                                 <img src="images/hide/key.png" alt="" /> <b>Скрытый текст, необходимо выполнение следующих условий: [hide=$condition_escaped]</b>
    125.                             </div>
    126.                             <div class="message" style="font-style: normal;">
    127. EOF;
    128.             $tr[$entire_match] .= $this->bbcode_parser->parse(
    129.                 $GLOBALS['hides'][$post_id][$hide_id]['text'],
    130.                 $this->forum['forumid'],
    131.                 $this->post['allowsmilie'],
    132.                 false,
    133.                 '',
    134.                 3,
    135.                 false,
    136.                 $this->post['htmlstate']
    137.             );
    138.             $tr[$entire_match] .= <<<EOF
    139.                             </div>
    140.                         </div>
    141.                     </div>
    142.                 </div>
    143. EOF;
    144.         }
    145.         else
    146.         {
    147.             $condition_escaped = htmlspecialchars_uni($condition);
    148.  
    149.             $requirements_list = array();
    150.             foreach ($requirements as $requirement)
    151.             {
    152.                 $requirements_list[] = ($requirement[1] ? '<img src="images/hide/success.png" />' : '<img src="images/hide/fail.gif" />') . ' ' . $requirement[0];
    153.             }
    154.             $requirements_list = implode('<br />', $requirements_list);
    155.  
    156.             $tr[$entire_match] = <<<EOF
    157.                 <div class="bbcode_container">
    158.                     <div class="bbcode_quote">
    159.                         <div class="quote_container">
    160.                             <div class="bbcode_quote_container"></div>
    161.                             <div class="bbcode_postedby">
    162.                                 <img src="images/hide/key.png" alt="" /> <b>Скрытый текст, необходимо выполнение следующих условий: [hide=$condition_escaped]</b>
    163.                             </div>
    164.                             <div class="message" style="font-style: normal;">$requirements_list</div>
    165.                         </div>
    166.                     </div>
    167.                 </div>
    168. EOF;
    169.         }
    170.     }
    171. }
    172. $post['message'] = strtr($post['message'], $tr);