--- /Users/bevan/Desktop/adsense_injector/adsense_injector.module +++ adsense_injector.module @@ -29,7 +29,7 @@ define('ADSENSE_INJECTOR_MODULE_VERSION', '$Id: adsense_injector.module,v 1.1.2.6 2007/04/06 16:05:55 inactivist Exp $' ); define('ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT', TRUE); -define('ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT', '
[adsense:120x240:1:1]
%body
[adsense:468x60:1:1]'); +define('ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT', '%content1[adsense:468x60:1:1]%content2'); define('ADSENSE_INJECTOR_BODY_MINWORDS_DEFAULT', 75); define('ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT', FALSE); @@ -73,41 +73,52 @@ */ function adsense_injector_nodeapi(&$node, $op, $teaser, $page) { // insert an ad into the body. - if (module_exists('adsense') && _adsense_page_match() && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) { - if ($op == 'view') { - if ($page) { - if (variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) { - $minwords = _adsense_injector_minwords_cfg($node->type); - $oldBody = $node->content['body']['#value']; - $wordcount = _adsense_injector_count_words($oldBody, $minwords); - if ($wordcount >= $minwords) { - $template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT); - // Process adsense module tags in the template text, if enabled and possible - if (function_exists('_adsense_process_tags')) { - $template = _adsense_process_tags($template); + if ( + $op == 'view' + && module_exists('adsense') + && _adsense_page_match() + && variable_get(ADSENSE_INJECTOR_INSERT_AD_NODETYPE . $node->type, FALSE)) { + + if ($page && variable_get('adsense_injector_insert_body_ad', ADSENSE_INJECTOR_INSERT_BODY_AD_DEFAULT)) { + $minwords = _adsense_injector_minwords_cfg($node->type); + $content['old'] = $node->content['body']['#value']; + $wordcount = _adsense_injector_count_words($content['old'], $minwords); + if (!($wordcount >= $minwords)) { + $content['new'] = "" . $content['old']; + } + else { + $template = variable_get('adsense_injector_body_template', ADSENSE_INJECTOR_BODY_INSERTION_TEMPLATE_DEFAULT); + $template = _adsense_injector_process_tags($template); + + if (!$dom = adsense_injector_xmlstrtodom($content['old'])) { + $content['new'] = $content['old']; + } + else { + $children = $dom->documentElement->childNodes; + $i = 3; + foreach ($children as $child) { + if ($i>>0) { + $content['parts'][1] .= $child->ownerDocument->saveXML($child); + if ($child->nodeName == 'p') { + $i--; + } } else { - watchdog('adsense_injector', t('adsense module function _adsense_process_tags() not found'), WATCHDOG_ERROR); + $content['parts'][2] .= $child->ownerDocument->saveXML($child); } - $node->body = - $node->content['body']['#value'] = strtr($template, array('%body' => $oldBody)); - } else { - $node->content['body']['#value'] = "" . $oldBody; } + $content['new'] = strtr($template, array('%content1' => $content['parts'][1], '%content2' => $content['parts'][2])); } } - else if ($teaser - && variable_get('adsense_injector_append_in_listview', ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT)) { - $template = variable_get('adsense_injector_listview_insertion_template', ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT); - // Process adsense module tags in the template text, if enabled and possible - if (function_exists('_adsense_process_tags')) { - $template = _adsense_process_tags($template); - } - else { - watchdog('adsense_injector', t('adsense module function _adsense_process_tags() not found'), WATCHDOG_ERROR); - } - $node->content['body']['#value'] = strtr($template, array('%teaser' => $node->teaser)); - } + $node->content['body']['#value'] = $content['new']; + } + elseif ( + $teaser + && variable_get('adsense_injector_append_in_listview', ADSENSE_INJECTOR_APPEND_IN_LISTVIEW_DEFAULT)) { + + $template = variable_get('adsense_injector_listview_insertion_template', ADSENSE_INJECTOR_LISTVIEW_INSERTION_TEMPLATE_DEFAULT); + $template = _adsense_injector_process_tags($template); + $node->content['body']['#value'] = strtr($template, array('%teaser' => $node->teaser)); } } } @@ -251,3 +262,52 @@ '#value' => '
' . $d . '
Module development sponsored by Exodus Development
'); $form['module_id'] = array('#type' => 'markup', '#value' => ADSENSE_INJECTOR_MODULE_VERSION . '
'); } + +/** + * Wrapper fnctn for _adsense_process_tags(). logs an error if _adsense_process_tags() doesn't exist + * @param $template + * @return $template + */ +function _adsense_injector_process_tags($template) { + // Process adsense module tags in the template text, if enabled and possible + if (function_exists('_adsense_process_tags')) { + $template = _adsense_process_tags($template); + } + else { + watchdog('adsense_injector', t('adsense module function _adsense_process_tags() not found'), WATCHDOG_ERROR); + } + return $template; +} + +/** +* Turns an XML string into a $dom object +* @param string of xml +* @return object DOMdocument object +*/ +function adsense_injector_xmlstrtodom($xmlstr) { +/* $tidy = new tidy; + $tidy->parseString($xmlstr); + $tidy->cleanRepair; + $xmlstr = $tidy->body();*/ + $dom = new DOMDocument(); +/* $dom->substituteEntities = false; + $dom->strictErrorChecking = false; + $dom->resolveExternals = true;*/ + $dom->loadXML(''); + $fragment = $dom->createDocumentFragment(); + if (!$fragment->appendXML($xmlstr)) { + return watchdog('adsense_injector', $dom->doctype->entities .'This string could not be parsed as XML
'. htmlspecialchars($xmlstr) .'
', WATCHDOG_WARNING); + } + else { + $dom->documentElement->appendChild($fragment); + return $dom; + } +} + +/** +* Send Debug messages to watchdog +*/ +function adsense_injector_debug($variable) { + watchdog('adsense_injector_debug', '
'. print_r($variable, true) .'
'); + return false; +}