/[drupal]/contributions/modules/textlinkads/textlinkads.module
ViewVC logotype

Diff of /contributions/modules/textlinkads/textlinkads.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7, Fri Jun 20 16:18:17 2008 UTC revision 1.8, Mon Jul 14 17:26:36 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: textlinkads.module,v 1.6 2008/06/05 23:36:00 mikejoconnor Exp $  // $Id: textlinkads.module,v 1.7 2008/06/20 16:18:17 mikejoconnor Exp $
3    
4  define('TEXTLINKADS_VERSION', '$Id: textlinkads.module,v 1.6 2008/06/05 23:36:00 mikejoconnor Exp $');  define('TEXTLINKADS_VERSION', '$Id: textlinkads.module,v 1.7 2008/06/20 16:18:17 mikejoconnor Exp $');
5  //define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads'));  //define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads'));
6    
7  function textlinkads_help($path, $arg) {  function textlinkads_help($path, $arg) {
# Line 34  function textlinkads_help($path, $arg) { Line 34  function textlinkads_help($path, $arg) {
34  }  }
35    
36  function textlinkads_perm() {  function textlinkads_perm() {
37    return array('administer text link ads');    return array('administer text link ads', 'administer inlinks by node');
38  }  }
39    
40  function textlinkads_cron() {  function textlinkads_cron() {
# Line 45  function textlinkads_cron() { Line 45  function textlinkads_cron() {
45    
46  function textlinkads_menu() {  function textlinkads_menu() {
47    $items = array();    $items = array();
48      $items['textlinkads/stats'] = array(    $items['textlinkads/stats'] = array(
49        'title'    => t('TextLinkAds stats'),      'title'    => t('TextLinkAds stats'),
50        'page callback' => 'textlinkads_stats_page',      'page callback' => 'textlinkads_stats_page',
51        'access arguments'   => array('administer text link ads'),      'access arguments'   => array('administer text link ads'),
52        'type'     => MENU_NORMAL_ITEM,      'type'     => MENU_NORMAL_ITEM,
53        );      );
54    
55      $items['textlinkads/update_now'] = array(    $items['textlinkads/update_now'] = array(
56        'page callback' => 'textlinkads_check_update',      'page callback' => 'textlinkads_check_update',
57        'type'     => MENU_NORMAL_ITEM,      'type'     => MENU_NORMAL_ITEM,
58        'access arguments'   => array('administer text link ads'),      'access arguments'   => array('administer text link ads'),
59        );      );
60    
61    $items['admin/settings/textlinkads'] = array(    $items['admin/settings/textlinkads'] = array(
62      'title' => t('TextLinkAds Settings'),      'title' => t('TextLinkAds Settings'),
# Line 65  function textlinkads_menu() { Line 65  function textlinkads_menu() {
65      'page arguments' => array('textlinkads_admin_settings'),      'page arguments' => array('textlinkads_admin_settings'),
66      'access arguments' => array('administer site configuration'),      'access arguments' => array('administer site configuration'),
67    );    );
68        $items['textlinkads/sync'] = array(
69        'title' => 'Textlinkads Sync',
70        'page callback' => 'textlinkads_inlinks_sync',
71        'access arguments' => array('access content'),
72        'type' => MENU_CALLBACK
73      );
74    return $items;    return $items;
75  }  }
76    
# Line 76  function textlinkads_block($op = 'list', Line 82  function textlinkads_block($op = 'list',
82      case 'view':      case 'view':
83        switch ($delta) {        switch ($delta) {
84          case 0:          case 0:
85            $block['subject'] = t(variable_get('textlinkads_ad_block_title', 'Advertisments'));            $block = textlinkads_get_block();
           $block['content'] = textlinkads_content();  
86            break;            break;
87        }        }
88        return $block;        return $block;
89    }    }
90  }  }
91    
92    function textlinkads_get_block() {
93      $block['content'] = textlinkads_content();
94      if (!empty($block['content'])) {
95        $block['subject'] = t(variable_get('textlinkads_ad_block_title', 'Advertisments'));
96        return $block;
97      }
98    }
99  function textlinkads_theme() {  function textlinkads_theme() {
100    return array(    return array(
101      'textlinkads_rss_ad' => array(      'textlinkads_rss_ad' => array(
# Line 111  function textlinkads_admin_settings() { Line 123  function textlinkads_admin_settings() {
123    if (!empty($_POST)) {    if (!empty($_POST)) {
124      variable_set('textlinkads_last_update', 0);      variable_set('textlinkads_last_update', 0);
125    }    }
126      $types_enabled = variable_get('textlinkads_inlinks_nodes_enabled', array());
127      $types = array();
128      foreach(node_get_types() as $key => $value) {
129        $types[$key] = $value->name;
130      }
131      $form['textlinkads_inlinks'] =  array (
132        '#type' => 'fieldset',
133        '#access' => true,
134        '#title' => t('Text-Link-Ads Inlink'),
135        '#collapsible' => true,
136        '#collapsed' => true,
137        'textlinkads_inlinks_nodes_enabled' =>
138          array (
139            '#type' => 'checkboxes',
140            '#title' => t('Enable Inlinks'),
141            '#default_value' => $types_enabled,
142            '#options' => $types,
143          ),
144        );
145    $form['textlinkads_website_xml_key'] = array(    $form['textlinkads_website_xml_key'] = array(
146      '#type' => 'textfield',      '#type' => 'textfield',
147      '#title' => t('Website XML Key'),      '#title' => t('Website XML Key'),
# Line 203  function textlinkads_nodeapi(&$node, $op Line 233  function textlinkads_nodeapi(&$node, $op
233          $link = theme('textlinkads_rss_ad', textlinkads_next_rss_ad());          $link = theme('textlinkads_rss_ad', textlinkads_next_rss_ad());
234          $node->teaser .= $link;          $node->teaser .= $link;
235        }        }
236          $node->textlinkads_inlinks = db_result(db_query("SELECT status FROM {textlinkads_inlinks} WHERE nid = %d", $node->nid));
237        break;
238        case 'view':
239          if(textlinkads_inlinks_allow_links($node)) {
240            $node->content['body']['#value'] = textlinkads_inlinks_add_links($node->content['body']['#value'], $node->nid);
241          }
242        break;
243        case 'update':
244        case 'insert':
245          textlinkads_inlinks_notify_tla('add', $node->nid);
246          if($node->textlinkads_inlinks_status === 0) {
247            db_query("DELETE FROM {textlinkads_inlinks} WHERE nid = %d", $node->nid);
248          }
249          else {
250            db_query("DELETE FROM {textlinkads_inlinks} WHERE nid = %d", $node->nid);
251            db_query("INSERT INTO {textlinkads_inlinks} (nid, status) VALUES (%d, %d)", $node->nid, $node->textlinkads_inlinks_status);
252          }
253        break;
254    }    }
255    
256  }  }
# Line 281  function theme_textlinkads_rss_ad($link) Line 329  function theme_textlinkads_rss_ad($link)
329    
330  function theme_textlinkads_ads($links) {  function theme_textlinkads_ads($links) {
331    $output = '';    $output = '';
332      if(count($links) > 0) {
333    
334    $font_size = variable_get('textlinkads_font', 12);      $font_size = variable_get('textlinkads_font', 12);
335    $rows = variable_get('textlinkads_rows', 4);      $rows = variable_get('textlinkads_rows', 4);
336    $columns = ceil(count($links) / $rows);      $columns = ceil(count($links) / $rows);
337    $link_color = variable_get('textlinkads_link_color', '#0000ff');      $link_color = variable_get('textlinkads_link_color', '#0000ff');
338    $bg_color = variable_get('textlinkads_bg_color', '#0000ff');      $bg_color = variable_get('textlinkads_bg_color', '#0000ff');
339    $border_color = variable_get('textlinkads_border_color', '#0000ff');      $border_color = variable_get('textlinkads_border_color', '#0000ff');
340    $no_border = variable_get('textlinkads_no_border', 0);      $no_border = variable_get('textlinkads_no_border', 0);
341    $no_bg = variable_get('textlinkads_no_bg', 0);      $no_bg = variable_get('textlinkads_no_bg', 0);
342    
343    $output =  '<table style="width: 100%; margin: 5px auto;border: '. ($no_border ? 'none' : "1px solid $border_color") .'; border-spacing: 0px;background: '. ($no_bg ? 'transparent' : $bg_color) .';" cellpadding="5" cellspacing="0"><tbody>';      $output =  '<table style="width: 100%; margin: 5px auto;border: '. ($no_border ? 'none' : "1px solid $border_color") .'; border-spacing: 0px;background: '. ($no_bg ? 'transparent' : $bg_color) .';" cellpadding="5" cellspacing="0"><tbody>';
344    
345    while (count($links) > 0) {      while (count($links) > 0) {
346      $output .= "<tr>";        $output .= "<tr>";
347      for ($j = 0; $j < $columns; $j++) {        for ($j = 0; $j < $columns; $j++) {
348        if ($link = array_pop($links)) {          if ($link = array_pop($links)) {
349          $output .= "<td align='center'><span style='font-size:{$font_size}px; color=#000000;'>{$link['beforetext']} <a href='{$link['url']}' style='font-size:{$font_size}px; color:{$link_color}'>{$link['text']}</a> {$link['aftertext'][$i]}</span></td>";            $output .= "<td align='center'><span style='font-size:{$font_size}px; color=#000000;'>{$link['beforetext']} <a href='{$link['url']}' style='font-size:{$font_size}px; color:{$link_color}'>{$link['text']}</a> {$link['aftertext'][$i]}</span></td>";
350        }          }
351        else {          else {
352          $output .= "<td></td>";            $output .= "<td></td>";
353            }
354        }        }
355            $output .= "</tr>";
356      }      }
357        $output .= "</tr>";      $output .= "</tbody></table>";
358    }    }
   $output .= "</tbody></table>";  
   
359    return $output;    return $output;
360  }  }
361    
# Line 391  function textlinkads_update_links($url, Line 440  function textlinkads_update_links($url,
440        }        }
441      }      }
442      foreach ($links as $key => $values) {      foreach ($links as $key => $values) {
443        db_query("INSERT INTO {textlinkads} (url, text, beforetext, aftertext, rsstext, rssbeforetext, rssaftertext) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')", $values['URL'], $values['TEXT'], $values['BEFORETEXT'], $values['AFTERTEXT'], $values['RSSTEXT'], $values['RSSBEFORETEXT'], $values['RSSAFTERTEXT']);        db_query("INSERT INTO {textlinkads} (url, text, beforetext, aftertext, rsstext, rssbeforetext, rssaftertext, nid) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)", $values['URL'], $values['TEXT'], $values['BEFORETEXT'], $values['AFTERTEXT'], $values['RSSTEXT'], $values['RSSBEFORETEXT'], $values['RSSAFTERTEXT'], $values['POSTID']);
444      }      }
445    }    }
446    $count=count($links);    $count=count($links);
# Line 426  function textlinkads_get_links($type = ' Line 475  function textlinkads_get_links($type = '
475    $links = array();    $links = array();
476    $limit = ($type == 'text') ? variable_get('textlinkads_total', 4) : variable_get('feed_default_items', 10);    $limit = ($type == 'text') ? variable_get('textlinkads_total', 4) : variable_get('feed_default_items', 10);
477    $where = ($type == 'text') ? 'text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL' : 'rsstext IS NOT NULL';    $where = ($type == 'text') ? 'text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL' : 'rsstext IS NOT NULL';
478    $result = db_query("SELECT * FROM {textlinkads}  WHERE $where LIMIT %d", $limit);    $result = db_query("SELECT * FROM {textlinkads}  WHERE $where AND nid = 0 LIMIT %d", $limit);
479    while ($row = db_fetch_array($result)) {    while ($row = db_fetch_array($result)) {
480    
481      $links[] = $row;      $links[] = $row;
# Line 544  var link_color_txt = 'link_color'; Line 593  var link_color_txt = 'link_color';
593    
594  __END__;  __END__;
595    return $js;    return $js;
596    }
597    
598    /**
599     * function textlinkads_inlinks_add_links()
600     * @param string $data The text to be searched for keyword/links
601     * @param array $links An array of keyword => link entries to be replaced
602     *
603     * @return string The text after link replacement
604     */
605    function textlinkads_inlinks_add_links($data = NULL, $nid = NULL) {
606      $split = array("'(<script[^>]*?>.*?</script>)'", // strip out javascript
607        "'<style[^>]*?>.*?</style>'", // strip out styelsheets
608        "'(<a[\/\!]*?[^<>]*?/a>)'", // strip out html tags
609        "'(<[\/\!]--*?[^<>]*?-->)'", // strip out html comments
610        "'(<[\/\!]*?[^<>]*?>)'si", // strip out html tags
611      );
612      global $textlinkads_inlinks_links;
613      if (is_numeric($nid)) {
614        $results = db_query("SELECT text, url FROM {textlinkads} WHERE nid = %d", $nid);
615        $links = array();
616        while($link = db_fetch_object($results)) {
617          $links[$link->text] = $link->url;
618        }
619        $textlinkads_inlinks_links = $links;
620        $return = _textlinkads_inlinks_add_links($data, $split);
621        unset($textlinkads_inlinks_links);
622        return $return;
623      }
624    }
625    
626    /**
627     * function _textlinkads_inlinks_add_links()
628     * @param $data string The string to be parsed for links
629     * @param array split The the regex to avoid replaceing unwanted items
630     *
631     * @return string The string after link replacement as occured
632     */
633    
634    function _textlinkads_inlinks_add_links($data, $split) {
635      if(count($split) >= 1) {
636        $data2 = preg_split($split[0], $data, NULL, PREG_SPLIT_DELIM_CAPTURE);
637        array_shift($split);
638        if($data2) {
639          foreach($data2 as $key => $value) {
640            $return .= _textlinkads_inlinks_add_links($value, $split, $return);
641          }
642          return $return;
643        }
644        else {
645          return _textlinkads_inlinks_add_links_helper($data);
646        }
647      }
648      else {
649        return _textlinkads_inlinks_add_links_helper($data);
650      }
651    }
652    
653    /**
654     * Function _textlinkads_inlinks_add_links_helper()
655     * A helper function for replacing keywords with links
656     * @param string $data The string to be searched for keywords
657     *
658     * @return string the data after keyword search/replace
659     */
660    
661    function _textlinkads_inlinks_add_links_helper($data) {
662      global $textlinkads_inlinks_links;
663      //Don't replace anything within tags
664      if(substr($data, 0, 1) == '<') {
665        return $data;
666      }
667    
668      elseif(is_array($textlinkads_inlinks_links)) {
669        $lower_data = strtolower($data);
670        //replace any keyword once
671        foreach ($textlinkads_inlinks_links as $keyword => $link) {
672          if (strpos(strtolower($lower_data), strtolower($keyword))) {
673            $start = strpos(strtolower($lower_data), strtolower($keyword));
674            $length = strlen($keyword);
675            $link_text = substr($data, $start, $length);
676            $link = '<a href="'. $link .'">'. $link_text .'</a>';
677            $replace_count = 1; // replace count can only be a variable??
678            $data = str_replace($link_text, $link, $data, $replace_count);
679            unset($textlinkads_inlinks_links[$keyword]);
680          }
681        }
682        return $data;
683      }
684    }
685    /**
686     * Function textlinkads_inlinks_sync()
687     * This funciton enables the tla servers to sync with our database
688     */
689    function textlinkads_inlinks_sync() {
690      header('Content-type: application/xml; charset="utf-8"',true);
691      $options = array();
692      $options['textlinkads_key'] = $_GET['textlinkads_key'];
693      $options['textlinkads_action'] = $_GET['textlinkads_action'];
694      $opitons['post_id'] = $_GET['post_id'];
695      watchdog('TextLinkAds', 'Textlinks sync initiated<br /><pre>'. var_export($options, true) .'</pre>');
696      $key = variable_get('textlinkads_website_xml_key', '');
697      if (!empty($_GET['textlinkads_key']) && $_GET['textlinkads_key'] == $key) {
698        $op = check_plain($_GET['textlinkads_action']);
699        switch ($op) {
700          case 'debug':
701            //perform debugging
702          break;
703          case 'sync_posts':
704            print textlinkads_inlinks_sync_posts();
705            exit;
706          break;
707          case 'rest_sync_limit':
708            textlinkads_inlinks_set_max_post_id();
709          break;
710          case 'reset_syncing':
711            variable_set('textlinkads_inlinks_last_sync_post_id', 0);
712          break;
713        }
714      }
715      else {
716        return false;
717      }
718    }
719    
720    /**
721     * function textlinkads_inlinks_sync_posts()
722     * Format xml for the tla inlinks service.
723     *
724     * @return string xml of each post, formated for the tla inlinks service
725     */
726    function textlinkads_inlinks_sync_posts() {
727      $xml = '<?xml version="1.0" encoding="utf-8"?>';
728      $xml .= "  <posts>\n";
729      if (is_numeric($_GET['post_id'])) {
730        $xml .= textlinkads_inlinks_prepare_node($_GET['post_id']);
731      }
732      else {
733        $last_post = variable_get('textlinkads_inlinks_last_sync_post_id', 0);
734        $results = db_query("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC LIMIT 100", $last_post);
735        while ($node = db_fetch_object($results)) {
736          $xml .= textlinkads_inlinks_prepare_node($node->nid);
737        }
738      }
739      $xml .= "</posts>\n";
740      return $xml;
741    }
742    
743    /**
744     * function textlinkads_inlinks_prepare_node()
745     * This function should verify the node is allowed to have inlinks.  If so it should
746     * update the last sync post id, and return the formated xml to send to inlinks.
747     * @param int nid the nid to prepare
748     *
749     * @return string Formatted xml for a single post
750     */
751    function textlinkads_inlinks_prepare_node($nid) {
752      $node = node_load($nid);
753      $last_post = variable_get('textlinkads_inlinks_last_sync_post_id', 0);
754    
755      if($last_post < $nid) {
756        variable_set('textlinkads_inlinks_last_sync_post_id', $nid);
757      }
758      $body = urlencode(strip_tags($node->body));
759      $title = urlencode($node->title);
760      $post = "    <post>\n";
761      $post .= '      <id>'. check_plain($node->nid) ."</id>\n";
762      $post .= '      <title>'. $title ."</title>\n";
763      $post .= '      <date>'. date('Y-m-d H:i:s', $node->created) ."</date>\n";
764      $post .= '      <url>'. url('node/'. $node->nid, array('absolute' => TRUE)) ."</url>\n";
765      $post .= '      <body>'. $body ."</body>\n";
766      $post .= "    </post>\n";
767      return $post;
768    }
769    
770    /**
771     * Function textlinkads_inlinks_notify_tla()
772     *
773     * @param string op The operation being performed.  'install' or 'add'
774     * @param int pid The id of the node being added.
775     */
776    function textlinkads_inlinks_notify_tla($op = NULL, $pid = NULL) {
777      $params = array( 'op' => $op, 'pid' => $pid);
778    
779      switch ($op) {
780        case 'install':
781          $key = variable_get('textlinkads_website_xml_key', '');
782          $site_url = url('textlinkads/sync', array('absolute' => TRUE));
783          if ($key && $site_url) {
784            $url = 'http://www.text-link-ads.com/post_level_sync.php?'.
785              'action=install&inlinks=true&'.
786              'inventory_key='. $key .
787              '&site_url='.$site_url;
788            $file = file_get_contents($url);
789            if ($file) {
790              variable_set('textlinkads_inlinks_install', true);
791            }
792          }
793          $options['url'] = $url;
794          $options['key'] = $key;
795          watchdog('TextLinkAds', 'Notified Textlink ads<br /><pre>'. var_export($options, true) .'</pre>');
796        break;
797        case 'add':
798          $key = variable_get('textlinkads_website_xml_key', '');
799          if ($pid && $key) {
800            $url = 'http://www.text-link-ads.com/post_level_sync.php?'.
801              'action=add&'.
802              'inventory_key='. $key .
803              '&post_id='.$pid;
804            $file = file_get_contents($url);
805          }
806          $options['url'] = $url;
807          watchdog('TextLinkAds', 'Notified Textlink ads<br /><pre>'. var_export($options, true) .'</pre>');
808        break;
809      }
810    }
811    
812    /**
813     * Function textlinkads_inlinks_set_max_post_id()
814     * Reset the textlinkads_inlinks_max_sync_post_id varialbe to the most recent nid
815     */
816    function textlinkads_inlinks_set_max_post_id() {
817      $max_nid = db_result(db_query("SELECT MAX(nid) FROM {node};"));
818      if ($max_nid) {
819        variable_set('textlinkads_inlinks_max_sync_post_id', $max_node);
820      }
821      else {
822        variable_set('textlinkads_inlinks_max_sync_post_id', 0);
823      }
824    }
825    
826    function textlinkads_form_alter(&$form, $form_state, $form_id) {
827      if ($form['#id'] == 'node-form') {
828        if (user_access('administer inlinks by node')) {
829          $form['textlinkads_inlinks'] = array (
830          '#type' => 'fieldset',
831          '#access' => true,
832          '#title' => t('Text-Link-Ads Inlink'),
833          '#collapsible' => true,
834          '#collapsed' => true,
835          '#weight' => 25,
836          'textlinkads_inlinks_status' =>
837            array (
838              '#type' => 'radios',
839              '#title' => t('Allow Inlinks'),
840              '#default_value' => $form['#node']->textlinkads_inlinks,
841              '#options' => array(
842                0 => t('Default'),
843                1 => t('Allow'),
844                2 => t('Deny'),
845              )
846            ),
847          );
848        }
849      }
850    }
851    
852    /**
853     * function textlinkads_inlink_allow_links()
854     * @param object node The node object
855     *
856     * @return boolean True if links are allowed, otherwise false
857     */
858    function textlinkads_inlinks_allow_links($node) {
859      $types = variable_get('textlinkads_inlinks_nodes_enabled', array());
860      if($node->textlinkads_inlinks == 2) {
861        return FALSE;
862      }
863      elseif ($types[$node->type] || $node->textlinkads_inlinks == 1) {
864        return TRUE;
865      }
866  }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.2