@@ -18,12 +18,12 @@ discard block |
||
| 18 | 18 | private $excludeTags = array(); |
| 19 | 19 | private $maxLinks = 0; |
| 20 | 20 | |
| 21 | - public function index(){ |
|
| 21 | + public function index() { |
|
| 22 | 22 | $this->addAutomatedLinks(); |
| 23 | 23 | |
| 24 | 24 | // If we have a crawl request check the CrawlID so we're sure we didn't hit another SS site running our module |
| 25 | - if( $crawl_id = $this->owner->request->getHeader('X-Crawl-Id') ){ |
|
| 26 | - return( $crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID ) |
|
| 25 | + if ($crawl_id = $this->owner->request->getHeader('X-Crawl-Id')) { |
|
| 26 | + return($crawl_id == GlobalAutoLinkSettings::get_current()->CrawlID) |
|
| 27 | 27 | ? $this->crawl_response() |
| 28 | 28 | : $this->owner->redirect(SEOTestSiteTreeController::getPermissionDeniedPage()->Link()); |
| 29 | 29 | } |
@@ -31,15 +31,15 @@ discard block |
||
| 31 | 31 | return array(); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - private function crawl_response(){ |
|
| 34 | + private function crawl_response() { |
|
| 35 | 35 | // Encoded version to detect which fields are being used |
| 36 | 36 | $customize = array(); |
| 37 | 37 | $dbFields = Config::inst()->get($this->owner->ClassName, 'db'); |
| 38 | - if(is_array($dbFields)) { |
|
| 39 | - foreach ( $dbFields as $field => $type) { |
|
| 38 | + if (is_array($dbFields)) { |
|
| 39 | + foreach ($dbFields as $field => $type) { |
|
| 40 | 40 | if (strtolower($type) == 'htmltext') { |
| 41 | 41 | $data = ($this->owner->hasMethod($field)) ? $this->owner->$field() : $this->owner->$field; |
| 42 | - if($data){ |
|
| 42 | + if ($data) { |
|
| 43 | 43 | $tmp = new HTMLText('tmp'); |
| 44 | 44 | $tmp->setValue($data); |
| 45 | 45 | $data = base64_encode($tmp->forTemplate()); |
@@ -90,31 +90,31 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return void |
| 92 | 92 | */ |
| 93 | - public function addAutomatedLinks(){ |
|
| 94 | - if( GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage' ) { |
|
| 93 | + public function addAutomatedLinks() { |
|
| 94 | + if (GlobalAutoLinkSettings::$enabled && $this->owner->class != 'RedirectorPage') { |
|
| 95 | 95 | $this->getSettings(); |
| 96 | - if( !$this->addLinks ) { |
|
| 96 | + if (!$this->addLinks) { |
|
| 97 | 97 | return; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - foreach( $this->getSettings()->IncludeInFields() as $field ){ |
|
| 100 | + foreach ($this->getSettings()->IncludeInFields() as $field) { |
|
| 101 | 101 | // Check that the field provided by user exists in this object, is of type HTMLText and has content |
| 102 | - if( AutomatedLink::isFieldParsable( $this->owner->data(), $field ) ){ |
|
| 102 | + if (AutomatedLink::isFieldParsable($this->owner->data(), $field)) { |
|
| 103 | 103 | |
| 104 | 104 | // Create dummy object so we can parse the HTML |
| 105 | - $dummy = new HTMLText( $field ); |
|
| 106 | - $dummy->setValue( $this->owner->$field ); |
|
| 105 | + $dummy = new HTMLText($field); |
|
| 106 | + $dummy->setValue($this->owner->$field); |
|
| 107 | 107 | // Create DOMDocument Object |
| 108 | - $content = mb_convert_encoding( $dummy->forTemplate(), 'html-entities', GlobalAutoLinkSettings::$encoding ); |
|
| 108 | + $content = mb_convert_encoding($dummy->forTemplate(), 'html-entities', GlobalAutoLinkSettings::$encoding); |
|
| 109 | 109 | $dom = AutomatedLink::constructDOMDocument($content); |
| 110 | 110 | |
| 111 | 111 | // Check current link count and if it's already exceeded do nothing |
| 112 | - $this->linkCount += (int) $dom->getElementsByTagName( 'a' )->length; |
|
| 113 | - if( $this->linkCount >= $this->maxLinks ) { |
|
| 112 | + $this->linkCount += (int) $dom->getElementsByTagName('a')->length; |
|
| 113 | + if ($this->linkCount >= $this->maxLinks) { |
|
| 114 | 114 | return; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - $parsed = $this->parseField( $dom, $field ); |
|
| 117 | + $parsed = $this->parseField($dom, $field); |
|
| 118 | 118 | $this->owner->data()->$field = $parsed; |
| 119 | 119 | $this->owner->$field = $parsed; |
| 120 | 120 | } |
@@ -130,19 +130,19 @@ discard block |
||
| 130 | 130 | * @param array $hash_tags |
| 131 | 131 | * @return array |
| 132 | 132 | */ |
| 133 | - private function hashTags( DOMDocument $html, $hash_tags ){ |
|
| 133 | + private function hashTags(DOMDocument $html, $hash_tags) { |
|
| 134 | 134 | $excluded = array(); |
| 135 | - foreach( $hash_tags as $eTag ){ |
|
| 136 | - while( $tags = $html->getElementsByTagName( $eTag ) ){ |
|
| 137 | - if( !$tags->length ) break 1; |
|
| 138 | - $tag = $tags->item(0); |
|
| 139 | - $value = $html->saveHTML( $tag ); |
|
| 140 | - $key = (string) crc32( $value ); |
|
| 135 | + foreach ($hash_tags as $eTag) { |
|
| 136 | + while ($tags = $html->getElementsByTagName($eTag)) { |
|
| 137 | + if (!$tags->length) break 1; |
|
| 138 | + $tag = $tags->item(0); |
|
| 139 | + $value = $html->saveHTML($tag); |
|
| 140 | + $key = (string) crc32($value); |
|
| 141 | 141 | |
| 142 | 142 | // Convert back children nodes of this node if they were already hashed |
| 143 | - $excluded[$key] = str_replace( array_keys( $excluded ), array_values( $excluded ), $value ); |
|
| 143 | + $excluded[$key] = str_replace(array_keys($excluded), array_values($excluded), $value); |
|
| 144 | 144 | |
| 145 | - $tag->parentNode->replaceChild( $html->createTextNode( $key ), $tag ); |
|
| 145 | + $tag->parentNode->replaceChild($html->createTextNode($key), $tag); |
|
| 146 | 146 | } |
| 147 | 147 | } |
| 148 | 148 | |
@@ -157,38 +157,38 @@ discard block |
||
| 157 | 157 | * @param string $content |
| 158 | 158 | * @return array |
| 159 | 159 | */ |
| 160 | - public function addLinkToContent(AutomatedLink $link, &$content){ |
|
| 160 | + public function addLinkToContent(AutomatedLink $link, &$content) { |
|
| 161 | 161 | $links = array(); |
| 162 | - $max = (int) ( $link->MaxLinksPerPage > 0 ) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 163 | - $escape = (string) preg_quote( $link->Phrase, '/' ); |
|
| 164 | - $regex = (string) ( $link->CaseSensitive ) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 162 | + $max = (int) ($link->MaxLinksPerPage > 0) ? $link->MaxLinksPerPage : PHP_INT_MAX; |
|
| 163 | + $escape = (string) preg_quote($link->Phrase, '/'); |
|
| 164 | + $regex = (string) ($link->CaseSensitive) ? "/(\b{$escape}\b)/" : "/(\b{$escape}\b)/i"; |
|
| 165 | 165 | |
| 166 | 166 | // Count the matches |
| 167 | - preg_match_all( $regex, $content, $count ); |
|
| 168 | - $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
|
| 169 | - if( $count < 1 ) $links; |
|
| 167 | + preg_match_all($regex, $content, $count); |
|
| 168 | + $count = (is_array($count) && isset($count[0])) ? count($count[0]) : 0; |
|
| 169 | + if ($count < 1) $links; |
|
| 170 | 170 | |
| 171 | - if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) { |
|
| 171 | + if (isset($this->maxLinksPerPage[$link->ID])) { |
|
| 172 | 172 | $max -= $this->maxLinksPerPage[$link->ID]; |
| 173 | - } else { |
|
| 173 | + }else { |
|
| 174 | 174 | $this->maxLinksPerPage[$link->ID] = 0; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | - for( $x = 0; $x < $count; $x++ ){ |
|
| 177 | + for ($x = 0; $x < $count; $x++) { |
|
| 178 | 178 | // Stop adding links if we reached the link or page limit |
| 179 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 179 | + if ($x >= $max || $this->linkCount >= $this->maxLinks) break; |
|
| 180 | 180 | |
| 181 | 181 | // Check if there is anything else to replace else stop |
| 182 | - preg_match( $regex, $content, $match ); |
|
| 183 | - if( !is_array( $match ) || !count( $match ) ) break; |
|
| 182 | + preg_match($regex, $content, $match); |
|
| 183 | + if (!is_array($match) || !count($match)) break; |
|
| 184 | 184 | |
| 185 | - if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
|
| 186 | - $key = (string) crc32( $html ); |
|
| 187 | - $links[ $key ] = (string) $html; |
|
| 185 | + if (!$html = (string) $link->getHTML($match[0])) continue; |
|
| 186 | + $key = (string) crc32($html); |
|
| 187 | + $links[$key] = (string) $html; |
|
| 188 | 188 | |
| 189 | - $content = preg_replace( $regex, $key, $content, 1 ); |
|
| 189 | + $content = preg_replace($regex, $key, $content, 1); |
|
| 190 | 190 | $this->linkCount++; |
| 191 | - $this->maxLinksPerPage[ $link->ID ]++; |
|
| 191 | + $this->maxLinksPerPage[$link->ID]++; |
|
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | return $links; |
@@ -201,24 +201,24 @@ discard block |
||
| 201 | 201 | * @param String $field |
| 202 | 202 | * @return string |
| 203 | 203 | */ |
| 204 | - private function parseField( DOMDocument $html, $field ){ |
|
| 205 | - $this->owner->extend( 'beforeParseField', $html, $field ); |
|
| 204 | + private function parseField(DOMDocument $html, $field) { |
|
| 205 | + $this->owner->extend('beforeParseField', $html, $field); |
|
| 206 | 206 | |
| 207 | 207 | // Remove Tags from Content we wown't be using |
| 208 | 208 | $excluded = $this->hashTags($html, $this->excludeTags); |
| 209 | - $body = (string)$html->saveHTML( $html->getElementsByTagName('body')->item(0) ); |
|
| 210 | - $content = preg_replace( array( '/\<body\>/is', '/\<\/body\>/is' ), '', $body, 1 ); |
|
| 209 | + $body = (string) $html->saveHTML($html->getElementsByTagName('body')->item(0)); |
|
| 210 | + $content = preg_replace(array('/\<body\>/is', '/\<\/body\>/is'), '', $body, 1); |
|
| 211 | 211 | |
| 212 | - foreach( AutomatedLink::get()->sort('Priority') as $link){ |
|
| 213 | - if( $this->linkCount < $this->maxLinks && $link->canBeAdded( $this->owner, $field ) ) { |
|
| 212 | + foreach (AutomatedLink::get()->sort('Priority') as $link) { |
|
| 213 | + if ($this->linkCount < $this->maxLinks && $link->canBeAdded($this->owner, $field)) { |
|
| 214 | 214 | $links = $this->addLinkToContent($link, $content); |
| 215 | - if( is_array($links) && count($links) > 0 ) { |
|
| 216 | - $excluded = $excluded + $links; |
|
| 215 | + if (is_array($links) && count($links) > 0) { |
|
| 216 | + $excluded = $excluded+$links; |
|
| 217 | 217 | } |
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | // Re-add the excluded Tags |
| 222 | - return str_replace( array_keys( $excluded ), array_values( $excluded ), $content ); |
|
| 222 | + return str_replace(array_keys($excluded), array_values($excluded), $content); |
|
| 223 | 223 | } |
| 224 | 224 | } |
@@ -73,12 +73,16 @@ discard block |
||
| 73 | 73 | private function getSettings() { |
| 74 | 74 | if ($this->settings === null) { |
| 75 | 75 | $this->settings = GlobalAutoLinkSettings::get_current(); |
| 76 | - if (!$this->settings) return $this->addLinks = false; |
|
| 76 | + if (!$this->settings) { |
|
| 77 | + return $this->addLinks = false; |
|
| 78 | + } |
|
| 77 | 79 | |
| 78 | 80 | $this->excludeTags = (array) $this->settings->ExcludeTags(); |
| 79 | 81 | $this->maxLinks = (int) ($this->settings->MaxLinksPerPage) ? $this->settings->MaxLinksPerPage : PHP_INT_MAX; |
| 80 | 82 | |
| 81 | - if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) $this->addLinks = false; |
|
| 83 | + if (!in_array($this->owner->ClassName, $this->settings->AllowedIn())) { |
|
| 84 | + $this->addLinks = false; |
|
| 85 | + } |
|
| 82 | 86 | } |
| 83 | 87 | |
| 84 | 88 | return $this->settings; |
@@ -134,7 +138,9 @@ discard block |
||
| 134 | 138 | $excluded = array(); |
| 135 | 139 | foreach( $hash_tags as $eTag ){ |
| 136 | 140 | while( $tags = $html->getElementsByTagName( $eTag ) ){ |
| 137 | - if( !$tags->length ) break 1; |
|
| 141 | + if( !$tags->length ) { |
|
| 142 | + break 1; |
|
| 143 | + } |
|
| 138 | 144 | $tag = $tags->item(0); |
| 139 | 145 | $value = $html->saveHTML( $tag ); |
| 140 | 146 | $key = (string) crc32( $value ); |
@@ -166,7 +172,9 @@ discard block |
||
| 166 | 172 | // Count the matches |
| 167 | 173 | preg_match_all( $regex, $content, $count ); |
| 168 | 174 | $count = ( is_array( $count ) && isset( $count[0] ) ) ? count( $count[0] ) : 0; |
| 169 | - if( $count < 1 ) $links; |
|
| 175 | + if( $count < 1 ) { |
|
| 176 | + $links; |
|
| 177 | + } |
|
| 170 | 178 | |
| 171 | 179 | if( isset( $this->maxLinksPerPage[ $link->ID ] ) ) { |
| 172 | 180 | $max -= $this->maxLinksPerPage[$link->ID]; |
@@ -176,13 +184,19 @@ discard block |
||
| 176 | 184 | |
| 177 | 185 | for( $x = 0; $x < $count; $x++ ){ |
| 178 | 186 | // Stop adding links if we reached the link or page limit |
| 179 | - if( $x >= $max || $this->linkCount >= $this->maxLinks ) break; |
|
| 187 | + if( $x >= $max || $this->linkCount >= $this->maxLinks ) { |
|
| 188 | + break; |
|
| 189 | + } |
|
| 180 | 190 | |
| 181 | 191 | // Check if there is anything else to replace else stop |
| 182 | 192 | preg_match( $regex, $content, $match ); |
| 183 | - if( !is_array( $match ) || !count( $match ) ) break; |
|
| 193 | + if( !is_array( $match ) || !count( $match ) ) { |
|
| 194 | + break; |
|
| 195 | + } |
|
| 184 | 196 | |
| 185 | - if( !$html = (string) $link->getHTML( $match[0] ) ) continue; |
|
| 197 | + if( !$html = (string) $link->getHTML( $match[0] ) ) { |
|
| 198 | + continue; |
|
| 199 | + } |
|
| 186 | 200 | $key = (string) crc32( $html ); |
| 187 | 201 | $links[ $key ] = (string) $html; |
| 188 | 202 | |