You are here

function alinks_make_links in Alinks 6

Same name and namespace in other branches
  1. 7 alinks.module \alinks_make_links()

Transform the first instance of any word defined to links

1 call to alinks_make_links()
alinks_nodeapi in ./alinks.module
Implementation of hook_nodeapi().

File

./alinks.module, line 153

Code

function alinks_make_links($body, $words) {

  // do we have alinks?
  if (is_array($words)) {

    // create the replacement array
    $url = drupal_lookup_path('alias', $_GET['q']) ? drupal_lookup_path('alias', $_GET['q']) : $_GET['q'];
    $i = 0;
    $links_chars = array(
      '/',
      '-',
    );
    $links_chars_replace = array(
      'alink_slash',
      'alink_minus',
    );
    foreach ($words as $word) {
      if ($word['alink_url'] != $url) {
        $alink_start_boundary = $word['alink_start_boundary'] == 1 ? 'B' : 'b';
        $alink_end_boundary = $word['alink_end_boundary'] == 1 ? 'B' : 'b';
        $alink_case_insensivity = $word['alink_case_insensitive'] == 1 ? 'i' : '';
        $word['alink_text'] = htmlspecialchars($word['alink_text']);
        $alink_text[] = '$\\' . $alink_start_boundary . '(' . preg_quote($word['alink_text'], '$') . ')\\' . $alink_end_boundary . '$' . $alink_case_insensivity;
        if ($word['alink_external'] == 1) {

          //print_r(str_replace($links_chars, $links_chars_replace, $word['alink_url']));exit;
          $alink_url[] = l('alink_check\\1alink_check', str_replace($links_chars, $links_chars_replace, $word['alink_url']) . 'alink_check', array(
            'attributes' => array(
              'class' => 'alink_check' . str_replace(' ', 'alink_space', $word['alink_class']) . 'alink_check',
              'title' => 'alink_check' . str_replace(' ', 'alink_space', check_plain($word['url_title'])) . 'alink_check',
            ),
            'absolute' => true,
          ));
        }
        else {
          $alink_url[] = l('alink_check\\1alink_check', 'alink_check' . str_replace('/', 'alink_slash', $word['alink_url']) . 'alink_check', array(
            'attributes' => array(
              'class' => 'alink_check' . str_replace(' ', 'alink_space', $word['alink_class']) . 'alink_check',
              'title' => 'alink_check' . str_replace(' ', 'alink_space', check_plain($word['url_title'])) . 'alink_check',
            ),
          ));
        }
        $i++;
      }
    }
    if ($i > 0) {
      $alink_url = str_replace(array(
        '&',
        '<',
        '>',
      ), array(
        '&',
        '<',
        '>',
      ), $alink_url);

      // we replace new lines with a temporary delimiter
      $carriage = array(
        "\r\n",
        "\n",
        "\r",
      );
      $carriage_replacement = array(
        " cariage_replacement_rn ",
        " cariage_replacement_n ",
        " cariage_replacement_r ",
      );
      $body = str_replace($carriage, $carriage_replacement, $body);

      // we get out the already existing links
      preg_match_all('/\\<a\\s.*?\\>(.*?)\\<\\/a\\>/', $body, $linka);
      $i = 0;
      $replacement = array();

      // create the replacement array
      foreach ($linka[0] as $key => $values) {
        $replacement[] = ' alink_delimiter_' . $i . ' ';
        $i++;
      }

      // replace the links with the replacement text
      $body = str_replace($linka[0], $replacement, $body);

      // we get all the text that is not inside a html tag
      // from the modified text
      preg_match_all('/\\>(.*?)\\</', $body, $output);
      $output[0] = array_unique($output[0]);
      $output[1] = array_unique($output[1]);

      // transform the result array to a string so we can use the limit argument
      $text = implode(' alink_delimiter_one_string ', $output[1]);
      $limit = variable_get('alinks_limit', 1);

      // make the actual replacement
      if ($limit == -1) {
        $output[1] = preg_replace($alink_text, $alink_url, $text);
      }
      else {
        $output[1] = preg_replace($alink_text, $alink_url, $text, $limit);
      }

      // rebuild the array
      $output[1] = explode(' alink_delimiter_one_string ', $output[1]);
      $our_output = array();
      $i = 0;

      // we make sure the text will pe replaced outside any tag
      foreach ($output[1] as $key => $values) {
        if (!$values) {
          $our_output[$i] = '><';
        }
        else {
          $our_output[$i] = str_replace($values, '>' . $values . '<', $values);
        }
        $i++;
      }

      // insert the new text in the full text
      $body = str_replace($output[0], $our_output, $body);

      // and put back the links in the text
      $body = str_replace($replacement, $linka[0], $body);
      $body = str_replace('alink_check', '', $body);
      $body = str_replace('alink_space', ' ', $body);
      $body = str_replace($links_chars_replace, $links_chars, $body);

      // and finaly put back the new lines
      $body = str_replace($carriage_replacement, $carriage, $body);
    }
  }
  return $body;
}