| 1 |
<?php |
| 2 |
|
| 3 |
class scriptSeqManipulate { |
| 4 |
public $buffer = null; |
| 5 |
|
| 6 |
function setBuffer($buffer) { |
| 7 |
$this->buffer = $buffer; |
| 8 |
} |
| 9 |
|
| 10 |
function appendAfter($scriptMatch, $match) { |
| 11 |
$mainScript = $this->getScript($scriptMatch); |
| 12 |
$html = str_get_html($this->buffer); |
| 13 |
$scriptTags = $html->find('script'); |
| 14 |
$matchFound = false; |
| 15 |
|
| 16 |
foreach ($scriptTags as $script) { |
| 17 |
if (strpos($script->outertext, $match) !== false) { |
| 18 |
$script->outertext = $script->outertext . $mainScript; |
| 19 |
$matchFound = true; |
| 20 |
break; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
$this->buffer = $html->save(); |
| 26 |
$html->clear(); |
| 27 |
unset($html); |
| 28 |
|
| 29 |
// if ($matchFound) { |
| 30 |
// $this->removeScript($scriptMatch); |
| 31 |
// } |
| 32 |
} |
| 33 |
|
| 34 |
function appendBefore($scriptMatch, $match) { |
| 35 |
$mainScript = $this->getScript($scriptMatch); |
| 36 |
$html = str_get_html($this->buffer); |
| 37 |
$scriptTags = $html->find('script'); |
| 38 |
$matchFound = false; |
| 39 |
|
| 40 |
foreach ($scriptTags as $script) { |
| 41 |
if (strpos($script->outertext, $match) !== false) { |
| 42 |
$script->outertext = $mainScript . $script->outertext; |
| 43 |
$matchFound = true; |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
$this->buffer = $html->save(); |
| 50 |
$html->clear(); |
| 51 |
unset($html); |
| 52 |
|
| 53 |
// if ($matchFound) { |
| 54 |
// $this->removeScript($scriptMatch); |
| 55 |
// } |
| 56 |
} |
| 57 |
|
| 58 |
function getScript($scriptMatch) { |
| 59 |
$html = str_get_html($this->buffer); |
| 60 |
$scriptTags = $html->find('script'); |
| 61 |
$found_tag = null; |
| 62 |
|
| 63 |
foreach ($scriptTags as $script) { |
| 64 |
if (strpos($script->outertext, $scriptMatch) !== false) { |
| 65 |
$found_tag = $script->outertext; |
| 66 |
$script->outertext = ''; |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$this->buffer = $html->save(); |
| 72 |
$html->clear(); |
| 73 |
unset($html); |
| 74 |
|
| 75 |
return $found_tag; |
| 76 |
} |
| 77 |
|
| 78 |
function removeScript($match) { |
| 79 |
$html = str_get_html($this->buffer); |
| 80 |
$scriptTags = $html->find('script'); |
| 81 |
|
| 82 |
foreach ($scriptTags as $script) { |
| 83 |
if (strpos($script->outertext, $match) !== false) { |
| 84 |
$script->outertext = ''; |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$this->buffer = $html->save(); |
| 90 |
$html->clear(); |
| 91 |
unset($html); |
| 92 |
} |
| 93 |
|
| 94 |
function getBuffer() { |
| 95 |
return $this->buffer; |
| 96 |
} |
| 97 |
|
| 98 |
function clean() { |
| 99 |
$this->buffer = null; |
| 100 |
} |
| 101 |
} |